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/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/capability.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/inetdevice.h>
43 #include <linux/igmp.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/mroute.h>
47 #include <linux/init.h>
48 #include <linux/if_ether.h>
49 #include <linux/slab.h>
50 #include <net/net_namespace.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
59 #include <linux/notifier.h>
60 #include <linux/if_arp.h>
61 #include <linux/netfilter_ipv4.h>
62 #include <linux/compat.h>
63 #include <linux/export.h>
64 #include <net/ip_tunnels.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
70 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71 #define CONFIG_IP_PIMSM 1
75 struct list_head list
;
80 struct sock __rcu
*mroute_sk
;
81 struct timer_list ipmr_expire_timer
;
82 struct list_head mfc_unres_queue
;
83 struct list_head mfc_cache_array
[MFC_LINES
];
84 struct vif_device vif_table
[MAXVIFS
];
86 atomic_t cache_resolve_queue_len
;
87 bool mroute_do_assert
;
89 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num
;
95 struct fib_rule common
;
102 /* Big lock, protecting vif table, mrt cache and mroute socket state.
103 * Note that the changes are semaphored via rtnl_lock.
106 static DEFINE_RWLOCK(mrt_lock
);
109 * Multicast router control variables
112 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
114 /* Special spinlock for queue of unresolved entries */
115 static DEFINE_SPINLOCK(mfc_unres_lock
);
117 /* We return to original Alan's scheme. Hash table of resolved
118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
122 * In this case data path is free of exclusive locks at all.
125 static struct kmem_cache
*mrt_cachep __read_mostly
;
127 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
);
128 static void ipmr_free_table(struct mr_table
*mrt
);
130 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
131 struct sk_buff
*skb
, struct mfc_cache
*cache
,
133 static int ipmr_cache_report(struct mr_table
*mrt
,
134 struct sk_buff
*pkt
, vifi_t vifi
, int assert);
135 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
136 struct mfc_cache
*c
, struct rtmsg
*rtm
);
137 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
139 static void mroute_clean_tables(struct mr_table
*mrt
);
140 static void ipmr_expire_process(unsigned long arg
);
142 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143 #define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
146 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
148 struct mr_table
*mrt
;
150 ipmr_for_each_table(mrt
, net
) {
157 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
158 struct mr_table
**mrt
)
161 struct ipmr_result res
;
162 struct fib_lookup_arg arg
= {
164 .flags
= FIB_LOOKUP_NOREF
,
167 err
= fib_rules_lookup(net
->ipv4
.mr_rules_ops
,
168 flowi4_to_flowi(flp4
), 0, &arg
);
175 static int ipmr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
176 int flags
, struct fib_lookup_arg
*arg
)
178 struct ipmr_result
*res
= arg
->result
;
179 struct mr_table
*mrt
;
181 switch (rule
->action
) {
184 case FR_ACT_UNREACHABLE
:
186 case FR_ACT_PROHIBIT
:
188 case FR_ACT_BLACKHOLE
:
193 mrt
= ipmr_get_table(rule
->fr_net
, rule
->table
);
200 static int ipmr_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
205 static const struct nla_policy ipmr_rule_policy
[FRA_MAX
+ 1] = {
209 static int ipmr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
210 struct fib_rule_hdr
*frh
, struct nlattr
**tb
)
215 static int ipmr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
221 static int ipmr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
222 struct fib_rule_hdr
*frh
)
230 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template
= {
231 .family
= RTNL_FAMILY_IPMR
,
232 .rule_size
= sizeof(struct ipmr_rule
),
233 .addr_size
= sizeof(u32
),
234 .action
= ipmr_rule_action
,
235 .match
= ipmr_rule_match
,
236 .configure
= ipmr_rule_configure
,
237 .compare
= ipmr_rule_compare
,
238 .default_pref
= fib_default_rule_pref
,
239 .fill
= ipmr_rule_fill
,
240 .nlgroup
= RTNLGRP_IPV4_RULE
,
241 .policy
= ipmr_rule_policy
,
242 .owner
= THIS_MODULE
,
245 static int __net_init
ipmr_rules_init(struct net
*net
)
247 struct fib_rules_ops
*ops
;
248 struct mr_table
*mrt
;
251 ops
= fib_rules_register(&ipmr_rules_ops_template
, net
);
255 INIT_LIST_HEAD(&net
->ipv4
.mr_tables
);
257 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
263 err
= fib_default_rule_add(ops
, 0x7fff, RT_TABLE_DEFAULT
, 0);
267 net
->ipv4
.mr_rules_ops
= ops
;
273 fib_rules_unregister(ops
);
277 static void __net_exit
ipmr_rules_exit(struct net
*net
)
279 struct mr_table
*mrt
, *next
;
281 list_for_each_entry_safe(mrt
, next
, &net
->ipv4
.mr_tables
, list
) {
282 list_del(&mrt
->list
);
283 ipmr_free_table(mrt
);
285 fib_rules_unregister(net
->ipv4
.mr_rules_ops
);
288 #define ipmr_for_each_table(mrt, net) \
289 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
291 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
293 return net
->ipv4
.mrt
;
296 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
297 struct mr_table
**mrt
)
299 *mrt
= net
->ipv4
.mrt
;
303 static int __net_init
ipmr_rules_init(struct net
*net
)
305 net
->ipv4
.mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
306 return net
->ipv4
.mrt
? 0 : -ENOMEM
;
309 static void __net_exit
ipmr_rules_exit(struct net
*net
)
311 ipmr_free_table(net
->ipv4
.mrt
);
315 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
)
317 struct mr_table
*mrt
;
320 mrt
= ipmr_get_table(net
, id
);
324 mrt
= kzalloc(sizeof(*mrt
), GFP_KERNEL
);
327 write_pnet(&mrt
->net
, net
);
330 /* Forwarding cache */
331 for (i
= 0; i
< MFC_LINES
; i
++)
332 INIT_LIST_HEAD(&mrt
->mfc_cache_array
[i
]);
334 INIT_LIST_HEAD(&mrt
->mfc_unres_queue
);
336 setup_timer(&mrt
->ipmr_expire_timer
, ipmr_expire_process
,
339 #ifdef CONFIG_IP_PIMSM
340 mrt
->mroute_reg_vif_num
= -1;
342 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
343 list_add_tail_rcu(&mrt
->list
, &net
->ipv4
.mr_tables
);
348 static void ipmr_free_table(struct mr_table
*mrt
)
350 del_timer_sync(&mrt
->ipmr_expire_timer
);
351 mroute_clean_tables(mrt
);
355 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
357 static void ipmr_del_tunnel(struct net_device
*dev
, struct vifctl
*v
)
359 struct net
*net
= dev_net(dev
);
363 dev
= __dev_get_by_name(net
, "tunl0");
365 const struct net_device_ops
*ops
= dev
->netdev_ops
;
367 struct ip_tunnel_parm p
;
369 memset(&p
, 0, sizeof(p
));
370 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
371 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
374 p
.iph
.protocol
= IPPROTO_IPIP
;
375 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
376 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
378 if (ops
->ndo_do_ioctl
) {
379 mm_segment_t oldfs
= get_fs();
382 ops
->ndo_do_ioctl(dev
, &ifr
, SIOCDELTUNNEL
);
389 struct net_device
*ipmr_new_tunnel(struct net
*net
, struct vifctl
*v
)
391 struct net_device
*dev
;
393 dev
= __dev_get_by_name(net
, "tunl0");
396 const struct net_device_ops
*ops
= dev
->netdev_ops
;
399 struct ip_tunnel_parm p
;
400 struct in_device
*in_dev
;
402 memset(&p
, 0, sizeof(p
));
403 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
404 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
407 p
.iph
.protocol
= IPPROTO_IPIP
;
408 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
409 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
411 if (ops
->ndo_do_ioctl
) {
412 mm_segment_t oldfs
= get_fs();
415 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
423 (dev
= __dev_get_by_name(net
, p
.name
)) != NULL
) {
424 dev
->flags
|= IFF_MULTICAST
;
426 in_dev
= __in_dev_get_rtnl(dev
);
430 ipv4_devconf_setall(in_dev
);
431 neigh_parms_data_state_setall(in_dev
->arp_parms
);
432 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
442 /* allow the register to be completed before unregistering. */
446 unregister_netdevice(dev
);
450 #ifdef CONFIG_IP_PIMSM
452 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
454 struct net
*net
= dev_net(dev
);
455 struct mr_table
*mrt
;
456 struct flowi4 fl4
= {
457 .flowi4_oif
= dev
->ifindex
,
458 .flowi4_iif
= skb
->skb_iif
? : LOOPBACK_IFINDEX
,
459 .flowi4_mark
= skb
->mark
,
463 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
469 read_lock(&mrt_lock
);
470 dev
->stats
.tx_bytes
+= skb
->len
;
471 dev
->stats
.tx_packets
++;
472 ipmr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, IGMPMSG_WHOLEPKT
);
473 read_unlock(&mrt_lock
);
478 static const struct net_device_ops reg_vif_netdev_ops
= {
479 .ndo_start_xmit
= reg_vif_xmit
,
482 static void reg_vif_setup(struct net_device
*dev
)
484 dev
->type
= ARPHRD_PIMREG
;
485 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
486 dev
->flags
= IFF_NOARP
;
487 dev
->netdev_ops
= ®_vif_netdev_ops
;
488 dev
->destructor
= free_netdev
;
489 dev
->features
|= NETIF_F_NETNS_LOCAL
;
492 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
494 struct net_device
*dev
;
495 struct in_device
*in_dev
;
498 if (mrt
->id
== RT_TABLE_DEFAULT
)
499 sprintf(name
, "pimreg");
501 sprintf(name
, "pimreg%u", mrt
->id
);
503 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, reg_vif_setup
);
508 dev_net_set(dev
, net
);
510 if (register_netdevice(dev
)) {
517 in_dev
= __in_dev_get_rcu(dev
);
523 ipv4_devconf_setall(in_dev
);
524 neigh_parms_data_state_setall(in_dev
->arp_parms
);
525 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
536 /* allow the register to be completed before unregistering. */
540 unregister_netdevice(dev
);
546 * vif_delete - Delete a VIF entry
547 * @notify: Set to 1, if the caller is a notifier_call
550 static int vif_delete(struct mr_table
*mrt
, int vifi
, int notify
,
551 struct list_head
*head
)
553 struct vif_device
*v
;
554 struct net_device
*dev
;
555 struct in_device
*in_dev
;
557 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
558 return -EADDRNOTAVAIL
;
560 v
= &mrt
->vif_table
[vifi
];
562 write_lock_bh(&mrt_lock
);
567 write_unlock_bh(&mrt_lock
);
568 return -EADDRNOTAVAIL
;
571 #ifdef CONFIG_IP_PIMSM
572 if (vifi
== mrt
->mroute_reg_vif_num
)
573 mrt
->mroute_reg_vif_num
= -1;
576 if (vifi
+ 1 == mrt
->maxvif
) {
579 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
580 if (VIF_EXISTS(mrt
, tmp
))
586 write_unlock_bh(&mrt_lock
);
588 dev_set_allmulti(dev
, -1);
590 in_dev
= __in_dev_get_rtnl(dev
);
592 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)--;
593 inet_netconf_notify_devconf(dev_net(dev
),
594 NETCONFA_MC_FORWARDING
,
595 dev
->ifindex
, &in_dev
->cnf
);
596 ip_rt_multicast_event(in_dev
);
599 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
) && !notify
)
600 unregister_netdevice_queue(dev
, head
);
606 static void ipmr_cache_free_rcu(struct rcu_head
*head
)
608 struct mfc_cache
*c
= container_of(head
, struct mfc_cache
, rcu
);
610 kmem_cache_free(mrt_cachep
, c
);
613 static inline void ipmr_cache_free(struct mfc_cache
*c
)
615 call_rcu(&c
->rcu
, ipmr_cache_free_rcu
);
618 /* Destroy an unresolved cache entry, killing queued skbs
619 * and reporting error to netlink readers.
622 static void ipmr_destroy_unres(struct mr_table
*mrt
, struct mfc_cache
*c
)
624 struct net
*net
= read_pnet(&mrt
->net
);
628 atomic_dec(&mrt
->cache_resolve_queue_len
);
630 while ((skb
= skb_dequeue(&c
->mfc_un
.unres
.unresolved
))) {
631 if (ip_hdr(skb
)->version
== 0) {
632 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
633 nlh
->nlmsg_type
= NLMSG_ERROR
;
634 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
635 skb_trim(skb
, nlh
->nlmsg_len
);
637 e
->error
= -ETIMEDOUT
;
638 memset(&e
->msg
, 0, sizeof(e
->msg
));
640 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
650 /* Timer process for the unresolved queue. */
652 static void ipmr_expire_process(unsigned long arg
)
654 struct mr_table
*mrt
= (struct mr_table
*)arg
;
656 unsigned long expires
;
657 struct mfc_cache
*c
, *next
;
659 if (!spin_trylock(&mfc_unres_lock
)) {
660 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+HZ
/10);
664 if (list_empty(&mrt
->mfc_unres_queue
))
670 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
671 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
672 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
673 if (interval
< expires
)
679 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
680 ipmr_destroy_unres(mrt
, c
);
683 if (!list_empty(&mrt
->mfc_unres_queue
))
684 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
687 spin_unlock(&mfc_unres_lock
);
690 /* Fill oifs list. It is called under write locked mrt_lock. */
692 static void ipmr_update_thresholds(struct mr_table
*mrt
, struct mfc_cache
*cache
,
697 cache
->mfc_un
.res
.minvif
= MAXVIFS
;
698 cache
->mfc_un
.res
.maxvif
= 0;
699 memset(cache
->mfc_un
.res
.ttls
, 255, MAXVIFS
);
701 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
702 if (VIF_EXISTS(mrt
, vifi
) &&
703 ttls
[vifi
] && ttls
[vifi
] < 255) {
704 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
705 if (cache
->mfc_un
.res
.minvif
> vifi
)
706 cache
->mfc_un
.res
.minvif
= vifi
;
707 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
708 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
713 static int vif_add(struct net
*net
, struct mr_table
*mrt
,
714 struct vifctl
*vifc
, int mrtsock
)
716 int vifi
= vifc
->vifc_vifi
;
717 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
718 struct net_device
*dev
;
719 struct in_device
*in_dev
;
723 if (VIF_EXISTS(mrt
, vifi
))
726 switch (vifc
->vifc_flags
) {
727 #ifdef CONFIG_IP_PIMSM
730 * Special Purpose VIF in PIM
731 * All the packets will be sent to the daemon
733 if (mrt
->mroute_reg_vif_num
>= 0)
735 dev
= ipmr_reg_vif(net
, mrt
);
738 err
= dev_set_allmulti(dev
, 1);
740 unregister_netdevice(dev
);
747 dev
= ipmr_new_tunnel(net
, vifc
);
750 err
= dev_set_allmulti(dev
, 1);
752 ipmr_del_tunnel(dev
, vifc
);
758 case VIFF_USE_IFINDEX
:
760 if (vifc
->vifc_flags
== VIFF_USE_IFINDEX
) {
761 dev
= dev_get_by_index(net
, vifc
->vifc_lcl_ifindex
);
762 if (dev
&& __in_dev_get_rtnl(dev
) == NULL
) {
764 return -EADDRNOTAVAIL
;
767 dev
= ip_dev_find(net
, vifc
->vifc_lcl_addr
.s_addr
);
770 return -EADDRNOTAVAIL
;
771 err
= dev_set_allmulti(dev
, 1);
781 in_dev
= __in_dev_get_rtnl(dev
);
784 return -EADDRNOTAVAIL
;
786 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)++;
787 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
, dev
->ifindex
,
789 ip_rt_multicast_event(in_dev
);
791 /* Fill in the VIF structures */
793 v
->rate_limit
= vifc
->vifc_rate_limit
;
794 v
->local
= vifc
->vifc_lcl_addr
.s_addr
;
795 v
->remote
= vifc
->vifc_rmt_addr
.s_addr
;
796 v
->flags
= vifc
->vifc_flags
;
798 v
->flags
|= VIFF_STATIC
;
799 v
->threshold
= vifc
->vifc_threshold
;
804 v
->link
= dev
->ifindex
;
805 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
))
806 v
->link
= dev
->iflink
;
808 /* And finish update writing critical data */
809 write_lock_bh(&mrt_lock
);
811 #ifdef CONFIG_IP_PIMSM
812 if (v
->flags
& VIFF_REGISTER
)
813 mrt
->mroute_reg_vif_num
= vifi
;
815 if (vifi
+1 > mrt
->maxvif
)
816 mrt
->maxvif
= vifi
+1;
817 write_unlock_bh(&mrt_lock
);
821 /* called with rcu_read_lock() */
822 static struct mfc_cache
*ipmr_cache_find(struct mr_table
*mrt
,
826 int line
= MFC_HASH(mcastgrp
, origin
);
829 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
) {
830 if (c
->mfc_origin
== origin
&& c
->mfc_mcastgrp
== mcastgrp
)
836 /* Look for a (*,*,oif) entry */
837 static struct mfc_cache
*ipmr_cache_find_any_parent(struct mr_table
*mrt
,
840 int line
= MFC_HASH(htonl(INADDR_ANY
), htonl(INADDR_ANY
));
843 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
)
844 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
845 c
->mfc_mcastgrp
== htonl(INADDR_ANY
) &&
846 c
->mfc_un
.res
.ttls
[vifi
] < 255)
852 /* Look for a (*,G) entry */
853 static struct mfc_cache
*ipmr_cache_find_any(struct mr_table
*mrt
,
854 __be32 mcastgrp
, int vifi
)
856 int line
= MFC_HASH(mcastgrp
, htonl(INADDR_ANY
));
857 struct mfc_cache
*c
, *proxy
;
859 if (mcastgrp
== htonl(INADDR_ANY
))
862 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
)
863 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
864 c
->mfc_mcastgrp
== mcastgrp
) {
865 if (c
->mfc_un
.res
.ttls
[vifi
] < 255)
868 /* It's ok if the vifi is part of the static tree */
869 proxy
= ipmr_cache_find_any_parent(mrt
,
871 if (proxy
&& proxy
->mfc_un
.res
.ttls
[vifi
] < 255)
876 return ipmr_cache_find_any_parent(mrt
, vifi
);
880 * Allocate a multicast cache entry
882 static struct mfc_cache
*ipmr_cache_alloc(void)
884 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
887 c
->mfc_un
.res
.minvif
= MAXVIFS
;
891 static struct mfc_cache
*ipmr_cache_alloc_unres(void)
893 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
896 skb_queue_head_init(&c
->mfc_un
.unres
.unresolved
);
897 c
->mfc_un
.unres
.expires
= jiffies
+ 10*HZ
;
903 * A cache entry has gone into a resolved state from queued
906 static void ipmr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
907 struct mfc_cache
*uc
, struct mfc_cache
*c
)
912 /* Play the pending entries through our router */
914 while ((skb
= __skb_dequeue(&uc
->mfc_un
.unres
.unresolved
))) {
915 if (ip_hdr(skb
)->version
== 0) {
916 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
918 if (__ipmr_fill_mroute(mrt
, skb
, c
, nlmsg_data(nlh
)) > 0) {
919 nlh
->nlmsg_len
= skb_tail_pointer(skb
) -
922 nlh
->nlmsg_type
= NLMSG_ERROR
;
923 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
924 skb_trim(skb
, nlh
->nlmsg_len
);
926 e
->error
= -EMSGSIZE
;
927 memset(&e
->msg
, 0, sizeof(e
->msg
));
930 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
932 ip_mr_forward(net
, mrt
, skb
, c
, 0);
938 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
939 * expects the following bizarre scheme.
941 * Called under mrt_lock.
944 static int ipmr_cache_report(struct mr_table
*mrt
,
945 struct sk_buff
*pkt
, vifi_t vifi
, int assert)
948 const int ihl
= ip_hdrlen(pkt
);
949 struct igmphdr
*igmp
;
951 struct sock
*mroute_sk
;
954 #ifdef CONFIG_IP_PIMSM
955 if (assert == IGMPMSG_WHOLEPKT
)
956 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
959 skb
= alloc_skb(128, GFP_ATOMIC
);
964 #ifdef CONFIG_IP_PIMSM
965 if (assert == IGMPMSG_WHOLEPKT
) {
966 /* Ugly, but we have no choice with this interface.
967 * Duplicate old header, fix ihl, length etc.
968 * And all this only to mangle msg->im_msgtype and
969 * to set msg->im_mbz to "mbz" :-)
971 skb_push(skb
, sizeof(struct iphdr
));
972 skb_reset_network_header(skb
);
973 skb_reset_transport_header(skb
);
974 msg
= (struct igmpmsg
*)skb_network_header(skb
);
975 memcpy(msg
, skb_network_header(pkt
), sizeof(struct iphdr
));
976 msg
->im_msgtype
= IGMPMSG_WHOLEPKT
;
978 msg
->im_vif
= mrt
->mroute_reg_vif_num
;
979 ip_hdr(skb
)->ihl
= sizeof(struct iphdr
) >> 2;
980 ip_hdr(skb
)->tot_len
= htons(ntohs(ip_hdr(pkt
)->tot_len
) +
981 sizeof(struct iphdr
));
986 /* Copy the IP header */
988 skb_set_network_header(skb
, skb
->len
);
990 skb_copy_to_linear_data(skb
, pkt
->data
, ihl
);
991 ip_hdr(skb
)->protocol
= 0; /* Flag to the kernel this is a route add */
992 msg
= (struct igmpmsg
*)skb_network_header(skb
);
994 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
998 igmp
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
1000 msg
->im_msgtype
= assert;
1002 ip_hdr(skb
)->tot_len
= htons(skb
->len
); /* Fix the length */
1003 skb
->transport_header
= skb
->network_header
;
1007 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1008 if (mroute_sk
== NULL
) {
1014 /* Deliver to mrouted */
1016 ret
= sock_queue_rcv_skb(mroute_sk
, skb
);
1019 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1027 * Queue a packet for resolution. It gets locked cache entry!
1031 ipmr_cache_unresolved(struct mr_table
*mrt
, vifi_t vifi
, struct sk_buff
*skb
)
1035 struct mfc_cache
*c
;
1036 const struct iphdr
*iph
= ip_hdr(skb
);
1038 spin_lock_bh(&mfc_unres_lock
);
1039 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, list
) {
1040 if (c
->mfc_mcastgrp
== iph
->daddr
&&
1041 c
->mfc_origin
== iph
->saddr
) {
1048 /* Create a new entry if allowable */
1050 if (atomic_read(&mrt
->cache_resolve_queue_len
) >= 10 ||
1051 (c
= ipmr_cache_alloc_unres()) == NULL
) {
1052 spin_unlock_bh(&mfc_unres_lock
);
1058 /* Fill in the new cache entry */
1061 c
->mfc_origin
= iph
->saddr
;
1062 c
->mfc_mcastgrp
= iph
->daddr
;
1064 /* Reflect first query at mrouted. */
1066 err
= ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_NOCACHE
);
1068 /* If the report failed throw the cache entry
1071 spin_unlock_bh(&mfc_unres_lock
);
1078 atomic_inc(&mrt
->cache_resolve_queue_len
);
1079 list_add(&c
->list
, &mrt
->mfc_unres_queue
);
1080 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1082 if (atomic_read(&mrt
->cache_resolve_queue_len
) == 1)
1083 mod_timer(&mrt
->ipmr_expire_timer
, c
->mfc_un
.unres
.expires
);
1086 /* See if we can append the packet */
1088 if (c
->mfc_un
.unres
.unresolved
.qlen
> 3) {
1092 skb_queue_tail(&c
->mfc_un
.unres
.unresolved
, skb
);
1096 spin_unlock_bh(&mfc_unres_lock
);
1101 * MFC cache manipulation by user space mroute daemon
1104 static int ipmr_mfc_delete(struct mr_table
*mrt
, struct mfcctl
*mfc
, int parent
)
1107 struct mfc_cache
*c
, *next
;
1109 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1111 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[line
], list
) {
1112 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1113 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
&&
1114 (parent
== -1 || parent
== c
->mfc_parent
)) {
1115 list_del_rcu(&c
->list
);
1116 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1124 static int ipmr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1125 struct mfcctl
*mfc
, int mrtsock
, int parent
)
1129 struct mfc_cache
*uc
, *c
;
1131 if (mfc
->mfcc_parent
>= MAXVIFS
)
1134 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1136 list_for_each_entry(c
, &mrt
->mfc_cache_array
[line
], list
) {
1137 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1138 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
&&
1139 (parent
== -1 || parent
== c
->mfc_parent
)) {
1146 write_lock_bh(&mrt_lock
);
1147 c
->mfc_parent
= mfc
->mfcc_parent
;
1148 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1150 c
->mfc_flags
|= MFC_STATIC
;
1151 write_unlock_bh(&mrt_lock
);
1152 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1156 if (mfc
->mfcc_mcastgrp
.s_addr
!= htonl(INADDR_ANY
) &&
1157 !ipv4_is_multicast(mfc
->mfcc_mcastgrp
.s_addr
))
1160 c
= ipmr_cache_alloc();
1164 c
->mfc_origin
= mfc
->mfcc_origin
.s_addr
;
1165 c
->mfc_mcastgrp
= mfc
->mfcc_mcastgrp
.s_addr
;
1166 c
->mfc_parent
= mfc
->mfcc_parent
;
1167 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1169 c
->mfc_flags
|= MFC_STATIC
;
1171 list_add_rcu(&c
->list
, &mrt
->mfc_cache_array
[line
]);
1174 * Check to see if we resolved a queued list. If so we
1175 * need to send on the frames and tidy up.
1178 spin_lock_bh(&mfc_unres_lock
);
1179 list_for_each_entry(uc
, &mrt
->mfc_unres_queue
, list
) {
1180 if (uc
->mfc_origin
== c
->mfc_origin
&&
1181 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
1182 list_del(&uc
->list
);
1183 atomic_dec(&mrt
->cache_resolve_queue_len
);
1188 if (list_empty(&mrt
->mfc_unres_queue
))
1189 del_timer(&mrt
->ipmr_expire_timer
);
1190 spin_unlock_bh(&mfc_unres_lock
);
1193 ipmr_cache_resolve(net
, mrt
, uc
, c
);
1194 ipmr_cache_free(uc
);
1196 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1201 * Close the multicast socket, and clear the vif tables etc
1204 static void mroute_clean_tables(struct mr_table
*mrt
)
1208 struct mfc_cache
*c
, *next
;
1210 /* Shut down all active vif entries */
1212 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1213 if (!(mrt
->vif_table
[i
].flags
& VIFF_STATIC
))
1214 vif_delete(mrt
, i
, 0, &list
);
1216 unregister_netdevice_many(&list
);
1218 /* Wipe the cache */
1220 for (i
= 0; i
< MFC_LINES
; i
++) {
1221 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[i
], list
) {
1222 if (c
->mfc_flags
& MFC_STATIC
)
1224 list_del_rcu(&c
->list
);
1225 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1230 if (atomic_read(&mrt
->cache_resolve_queue_len
) != 0) {
1231 spin_lock_bh(&mfc_unres_lock
);
1232 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
1234 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1235 ipmr_destroy_unres(mrt
, c
);
1237 spin_unlock_bh(&mfc_unres_lock
);
1241 /* called from ip_ra_control(), before an RCU grace period,
1242 * we dont need to call synchronize_rcu() here
1244 static void mrtsock_destruct(struct sock
*sk
)
1246 struct net
*net
= sock_net(sk
);
1247 struct mr_table
*mrt
;
1250 ipmr_for_each_table(mrt
, net
) {
1251 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1252 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)--;
1253 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
,
1254 NETCONFA_IFINDEX_ALL
,
1255 net
->ipv4
.devconf_all
);
1256 RCU_INIT_POINTER(mrt
->mroute_sk
, NULL
);
1257 mroute_clean_tables(mrt
);
1264 * Socket options and virtual interface manipulation. The whole
1265 * virtual interface system is a complete heap, but unfortunately
1266 * that's how BSD mrouted happens to think. Maybe one day with a proper
1267 * MOSPF/PIM router set up we can clean this up.
1270 int ip_mroute_setsockopt(struct sock
*sk
, int optname
, char __user
*optval
, unsigned int optlen
)
1272 int ret
, parent
= 0;
1275 struct net
*net
= sock_net(sk
);
1276 struct mr_table
*mrt
;
1278 if (sk
->sk_type
!= SOCK_RAW
||
1279 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1282 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1286 if (optname
!= MRT_INIT
) {
1287 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
) &&
1288 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1294 if (optlen
!= sizeof(int))
1298 if (rtnl_dereference(mrt
->mroute_sk
)) {
1303 ret
= ip_ra_control(sk
, 1, mrtsock_destruct
);
1305 rcu_assign_pointer(mrt
->mroute_sk
, sk
);
1306 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)++;
1307 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
,
1308 NETCONFA_IFINDEX_ALL
,
1309 net
->ipv4
.devconf_all
);
1314 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
))
1316 return ip_ra_control(sk
, 0, NULL
);
1319 if (optlen
!= sizeof(vif
))
1321 if (copy_from_user(&vif
, optval
, sizeof(vif
)))
1323 if (vif
.vifc_vifi
>= MAXVIFS
)
1326 if (optname
== MRT_ADD_VIF
) {
1327 ret
= vif_add(net
, mrt
, &vif
,
1328 sk
== rtnl_dereference(mrt
->mroute_sk
));
1330 ret
= vif_delete(mrt
, vif
.vifc_vifi
, 0, NULL
);
1336 * Manipulate the forwarding caches. These live
1337 * in a sort of kernel/user symbiosis.
1342 case MRT_ADD_MFC_PROXY
:
1343 case MRT_DEL_MFC_PROXY
:
1344 if (optlen
!= sizeof(mfc
))
1346 if (copy_from_user(&mfc
, optval
, sizeof(mfc
)))
1349 parent
= mfc
.mfcc_parent
;
1351 if (optname
== MRT_DEL_MFC
|| optname
== MRT_DEL_MFC_PROXY
)
1352 ret
= ipmr_mfc_delete(mrt
, &mfc
, parent
);
1354 ret
= ipmr_mfc_add(net
, mrt
, &mfc
,
1355 sk
== rtnl_dereference(mrt
->mroute_sk
),
1360 * Control PIM assert.
1365 if (optlen
!= sizeof(v
))
1367 if (get_user(v
, (int __user
*)optval
))
1369 mrt
->mroute_do_assert
= v
;
1372 #ifdef CONFIG_IP_PIMSM
1377 if (optlen
!= sizeof(v
))
1379 if (get_user(v
, (int __user
*)optval
))
1385 if (v
!= mrt
->mroute_do_pim
) {
1386 mrt
->mroute_do_pim
= v
;
1387 mrt
->mroute_do_assert
= v
;
1393 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1398 if (optlen
!= sizeof(u32
))
1400 if (get_user(v
, (u32 __user
*)optval
))
1403 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1404 if (v
!= RT_TABLE_DEFAULT
&& v
>= 1000000000)
1409 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1412 if (!ipmr_new_table(net
, v
))
1415 raw_sk(sk
)->ipmr_table
= v
;
1422 * Spurious command, or MRT_VERSION which you cannot
1426 return -ENOPROTOOPT
;
1431 * Getsock opt support for the multicast routing system.
1434 int ip_mroute_getsockopt(struct sock
*sk
, int optname
, char __user
*optval
, int __user
*optlen
)
1438 struct net
*net
= sock_net(sk
);
1439 struct mr_table
*mrt
;
1441 if (sk
->sk_type
!= SOCK_RAW
||
1442 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1445 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1449 if (optname
!= MRT_VERSION
&&
1450 #ifdef CONFIG_IP_PIMSM
1451 optname
!= MRT_PIM
&&
1453 optname
!= MRT_ASSERT
)
1454 return -ENOPROTOOPT
;
1456 if (get_user(olr
, optlen
))
1459 olr
= min_t(unsigned int, olr
, sizeof(int));
1463 if (put_user(olr
, optlen
))
1465 if (optname
== MRT_VERSION
)
1467 #ifdef CONFIG_IP_PIMSM
1468 else if (optname
== MRT_PIM
)
1469 val
= mrt
->mroute_do_pim
;
1472 val
= mrt
->mroute_do_assert
;
1473 if (copy_to_user(optval
, &val
, olr
))
1479 * The IP multicast ioctl support routines.
1482 int ipmr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1484 struct sioc_sg_req sr
;
1485 struct sioc_vif_req vr
;
1486 struct vif_device
*vif
;
1487 struct mfc_cache
*c
;
1488 struct net
*net
= sock_net(sk
);
1489 struct mr_table
*mrt
;
1491 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1497 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1499 if (vr
.vifi
>= mrt
->maxvif
)
1501 read_lock(&mrt_lock
);
1502 vif
= &mrt
->vif_table
[vr
.vifi
];
1503 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1504 vr
.icount
= vif
->pkt_in
;
1505 vr
.ocount
= vif
->pkt_out
;
1506 vr
.ibytes
= vif
->bytes_in
;
1507 vr
.obytes
= vif
->bytes_out
;
1508 read_unlock(&mrt_lock
);
1510 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1514 read_unlock(&mrt_lock
);
1515 return -EADDRNOTAVAIL
;
1517 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1521 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1523 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1524 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1525 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1528 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1533 return -EADDRNOTAVAIL
;
1535 return -ENOIOCTLCMD
;
1539 #ifdef CONFIG_COMPAT
1540 struct compat_sioc_sg_req
{
1543 compat_ulong_t pktcnt
;
1544 compat_ulong_t bytecnt
;
1545 compat_ulong_t wrong_if
;
1548 struct compat_sioc_vif_req
{
1549 vifi_t vifi
; /* Which iface */
1550 compat_ulong_t icount
;
1551 compat_ulong_t ocount
;
1552 compat_ulong_t ibytes
;
1553 compat_ulong_t obytes
;
1556 int ipmr_compat_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
1558 struct compat_sioc_sg_req sr
;
1559 struct compat_sioc_vif_req vr
;
1560 struct vif_device
*vif
;
1561 struct mfc_cache
*c
;
1562 struct net
*net
= sock_net(sk
);
1563 struct mr_table
*mrt
;
1565 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1571 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1573 if (vr
.vifi
>= mrt
->maxvif
)
1575 read_lock(&mrt_lock
);
1576 vif
= &mrt
->vif_table
[vr
.vifi
];
1577 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1578 vr
.icount
= vif
->pkt_in
;
1579 vr
.ocount
= vif
->pkt_out
;
1580 vr
.ibytes
= vif
->bytes_in
;
1581 vr
.obytes
= vif
->bytes_out
;
1582 read_unlock(&mrt_lock
);
1584 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1588 read_unlock(&mrt_lock
);
1589 return -EADDRNOTAVAIL
;
1591 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1595 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1597 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1598 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1599 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1602 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1607 return -EADDRNOTAVAIL
;
1609 return -ENOIOCTLCMD
;
1615 static int ipmr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1617 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1618 struct net
*net
= dev_net(dev
);
1619 struct mr_table
*mrt
;
1620 struct vif_device
*v
;
1623 if (event
!= NETDEV_UNREGISTER
)
1626 ipmr_for_each_table(mrt
, net
) {
1627 v
= &mrt
->vif_table
[0];
1628 for (ct
= 0; ct
< mrt
->maxvif
; ct
++, v
++) {
1630 vif_delete(mrt
, ct
, 1, NULL
);
1637 static struct notifier_block ip_mr_notifier
= {
1638 .notifier_call
= ipmr_device_event
,
1642 * Encapsulate a packet by attaching a valid IPIP header to it.
1643 * This avoids tunnel drivers and other mess and gives us the speed so
1644 * important for multicast video.
1647 static void ip_encap(struct sk_buff
*skb
, __be32 saddr
, __be32 daddr
)
1650 const struct iphdr
*old_iph
= ip_hdr(skb
);
1652 skb_push(skb
, sizeof(struct iphdr
));
1653 skb
->transport_header
= skb
->network_header
;
1654 skb_reset_network_header(skb
);
1658 iph
->tos
= old_iph
->tos
;
1659 iph
->ttl
= old_iph
->ttl
;
1663 iph
->protocol
= IPPROTO_IPIP
;
1665 iph
->tot_len
= htons(skb
->len
);
1666 ip_select_ident(skb
, NULL
);
1669 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1673 static inline int ipmr_forward_finish(struct sk_buff
*skb
)
1675 struct ip_options
*opt
= &(IPCB(skb
)->opt
);
1677 IP_INC_STATS_BH(dev_net(skb_dst(skb
)->dev
), IPSTATS_MIB_OUTFORWDATAGRAMS
);
1678 IP_ADD_STATS_BH(dev_net(skb_dst(skb
)->dev
), IPSTATS_MIB_OUTOCTETS
, skb
->len
);
1680 if (unlikely(opt
->optlen
))
1681 ip_forward_options(skb
);
1683 return dst_output(skb
);
1687 * Processing handlers for ipmr_forward
1690 static void ipmr_queue_xmit(struct net
*net
, struct mr_table
*mrt
,
1691 struct sk_buff
*skb
, struct mfc_cache
*c
, int vifi
)
1693 const struct iphdr
*iph
= ip_hdr(skb
);
1694 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1695 struct net_device
*dev
;
1700 if (vif
->dev
== NULL
)
1703 #ifdef CONFIG_IP_PIMSM
1704 if (vif
->flags
& VIFF_REGISTER
) {
1706 vif
->bytes_out
+= skb
->len
;
1707 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1708 vif
->dev
->stats
.tx_packets
++;
1709 ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_WHOLEPKT
);
1714 if (vif
->flags
& VIFF_TUNNEL
) {
1715 rt
= ip_route_output_ports(net
, &fl4
, NULL
,
1716 vif
->remote
, vif
->local
,
1719 RT_TOS(iph
->tos
), vif
->link
);
1722 encap
= sizeof(struct iphdr
);
1724 rt
= ip_route_output_ports(net
, &fl4
, NULL
, iph
->daddr
, 0,
1727 RT_TOS(iph
->tos
), vif
->link
);
1734 if (skb
->len
+encap
> dst_mtu(&rt
->dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1735 /* Do not fragment multicasts. Alas, IPv4 does not
1736 * allow to send ICMP, so that packets will disappear
1740 IP_INC_STATS_BH(dev_net(dev
), IPSTATS_MIB_FRAGFAILS
);
1745 encap
+= LL_RESERVED_SPACE(dev
) + rt
->dst
.header_len
;
1747 if (skb_cow(skb
, encap
)) {
1753 vif
->bytes_out
+= skb
->len
;
1756 skb_dst_set(skb
, &rt
->dst
);
1757 ip_decrease_ttl(ip_hdr(skb
));
1759 /* FIXME: forward and output firewalls used to be called here.
1760 * What do we do with netfilter? -- RR
1762 if (vif
->flags
& VIFF_TUNNEL
) {
1763 ip_encap(skb
, vif
->local
, vif
->remote
);
1764 /* FIXME: extra output firewall step used to be here. --RR */
1765 vif
->dev
->stats
.tx_packets
++;
1766 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1769 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1772 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1773 * not only before forwarding, but after forwarding on all output
1774 * interfaces. It is clear, if mrouter runs a multicasting
1775 * program, it should receive packets not depending to what interface
1776 * program is joined.
1777 * If we will not make it, the program will have to join on all
1778 * interfaces. On the other hand, multihoming host (or router, but
1779 * not mrouter) cannot join to more than one interface - it will
1780 * result in receiving multiple packets.
1782 NF_HOOK(NFPROTO_IPV4
, NF_INET_FORWARD
, skb
, skb
->dev
, dev
,
1783 ipmr_forward_finish
);
1790 static int ipmr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
1794 for (ct
= mrt
->maxvif
-1; ct
>= 0; ct
--) {
1795 if (mrt
->vif_table
[ct
].dev
== dev
)
1801 /* "local" means that we should preserve one skb (for local delivery) */
1803 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
1804 struct sk_buff
*skb
, struct mfc_cache
*cache
,
1809 int true_vifi
= ipmr_find_vif(mrt
, skb
->dev
);
1811 vif
= cache
->mfc_parent
;
1812 cache
->mfc_un
.res
.pkt
++;
1813 cache
->mfc_un
.res
.bytes
+= skb
->len
;
1815 if (cache
->mfc_origin
== htonl(INADDR_ANY
) && true_vifi
>= 0) {
1816 struct mfc_cache
*cache_proxy
;
1818 /* For an (*,G) entry, we only check that the incomming
1819 * interface is part of the static tree.
1821 cache_proxy
= ipmr_cache_find_any_parent(mrt
, vif
);
1823 cache_proxy
->mfc_un
.res
.ttls
[true_vifi
] < 255)
1828 * Wrong interface: drop packet and (maybe) send PIM assert.
1830 if (mrt
->vif_table
[vif
].dev
!= skb
->dev
) {
1831 if (rt_is_output_route(skb_rtable(skb
))) {
1832 /* It is our own packet, looped back.
1833 * Very complicated situation...
1835 * The best workaround until routing daemons will be
1836 * fixed is not to redistribute packet, if it was
1837 * send through wrong interface. It means, that
1838 * multicast applications WILL NOT work for
1839 * (S,G), which have default multicast route pointing
1840 * to wrong oif. In any case, it is not a good
1841 * idea to use multicasting applications on router.
1846 cache
->mfc_un
.res
.wrong_if
++;
1848 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
1849 /* pimsm uses asserts, when switching from RPT to SPT,
1850 * so that we cannot check that packet arrived on an oif.
1851 * It is bad, but otherwise we would need to move pretty
1852 * large chunk of pimd to kernel. Ough... --ANK
1854 (mrt
->mroute_do_pim
||
1855 cache
->mfc_un
.res
.ttls
[true_vifi
] < 255) &&
1857 cache
->mfc_un
.res
.last_assert
+ MFC_ASSERT_THRESH
)) {
1858 cache
->mfc_un
.res
.last_assert
= jiffies
;
1859 ipmr_cache_report(mrt
, skb
, true_vifi
, IGMPMSG_WRONGVIF
);
1865 mrt
->vif_table
[vif
].pkt_in
++;
1866 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
1871 if (cache
->mfc_origin
== htonl(INADDR_ANY
) &&
1872 cache
->mfc_mcastgrp
== htonl(INADDR_ANY
)) {
1873 if (true_vifi
>= 0 &&
1874 true_vifi
!= cache
->mfc_parent
&&
1876 cache
->mfc_un
.res
.ttls
[cache
->mfc_parent
]) {
1877 /* It's an (*,*) entry and the packet is not coming from
1878 * the upstream: forward the packet to the upstream
1881 psend
= cache
->mfc_parent
;
1886 for (ct
= cache
->mfc_un
.res
.maxvif
- 1;
1887 ct
>= cache
->mfc_un
.res
.minvif
; ct
--) {
1888 /* For (*,G) entry, don't forward to the incoming interface */
1889 if ((cache
->mfc_origin
!= htonl(INADDR_ANY
) ||
1891 ip_hdr(skb
)->ttl
> cache
->mfc_un
.res
.ttls
[ct
]) {
1893 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1896 ipmr_queue_xmit(net
, mrt
, skb2
, cache
,
1905 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1908 ipmr_queue_xmit(net
, mrt
, skb2
, cache
, psend
);
1910 ipmr_queue_xmit(net
, mrt
, skb
, cache
, psend
);
1920 static struct mr_table
*ipmr_rt_fib_lookup(struct net
*net
, struct sk_buff
*skb
)
1922 struct rtable
*rt
= skb_rtable(skb
);
1923 struct iphdr
*iph
= ip_hdr(skb
);
1924 struct flowi4 fl4
= {
1925 .daddr
= iph
->daddr
,
1926 .saddr
= iph
->saddr
,
1927 .flowi4_tos
= RT_TOS(iph
->tos
),
1928 .flowi4_oif
= (rt_is_output_route(rt
) ?
1929 skb
->dev
->ifindex
: 0),
1930 .flowi4_iif
= (rt_is_output_route(rt
) ?
1933 .flowi4_mark
= skb
->mark
,
1935 struct mr_table
*mrt
;
1938 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
1940 return ERR_PTR(err
);
1945 * Multicast packets for forwarding arrive here
1946 * Called with rcu_read_lock();
1949 int ip_mr_input(struct sk_buff
*skb
)
1951 struct mfc_cache
*cache
;
1952 struct net
*net
= dev_net(skb
->dev
);
1953 int local
= skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
;
1954 struct mr_table
*mrt
;
1956 /* Packet is looped back after forward, it should not be
1957 * forwarded second time, but still can be delivered locally.
1959 if (IPCB(skb
)->flags
& IPSKB_FORWARDED
)
1962 mrt
= ipmr_rt_fib_lookup(net
, skb
);
1965 return PTR_ERR(mrt
);
1968 if (IPCB(skb
)->opt
.router_alert
) {
1969 if (ip_call_ra_chain(skb
))
1971 } else if (ip_hdr(skb
)->protocol
== IPPROTO_IGMP
) {
1972 /* IGMPv1 (and broken IGMPv2 implementations sort of
1973 * Cisco IOS <= 11.2(8)) do not put router alert
1974 * option to IGMP packets destined to routable
1975 * groups. It is very bad, because it means
1976 * that we can forward NO IGMP messages.
1978 struct sock
*mroute_sk
;
1980 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1983 raw_rcv(mroute_sk
, skb
);
1989 /* already under rcu_read_lock() */
1990 cache
= ipmr_cache_find(mrt
, ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
1991 if (cache
== NULL
) {
1992 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
1995 cache
= ipmr_cache_find_any(mrt
, ip_hdr(skb
)->daddr
,
2000 * No usable cache entry
2002 if (cache
== NULL
) {
2006 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
2007 ip_local_deliver(skb
);
2013 read_lock(&mrt_lock
);
2014 vif
= ipmr_find_vif(mrt
, skb
->dev
);
2016 int err2
= ipmr_cache_unresolved(mrt
, vif
, skb
);
2017 read_unlock(&mrt_lock
);
2021 read_unlock(&mrt_lock
);
2026 read_lock(&mrt_lock
);
2027 ip_mr_forward(net
, mrt
, skb
, cache
, local
);
2028 read_unlock(&mrt_lock
);
2031 return ip_local_deliver(skb
);
2037 return ip_local_deliver(skb
);
2042 #ifdef CONFIG_IP_PIMSM
2043 /* called with rcu_read_lock() */
2044 static int __pim_rcv(struct mr_table
*mrt
, struct sk_buff
*skb
,
2045 unsigned int pimlen
)
2047 struct net_device
*reg_dev
= NULL
;
2048 struct iphdr
*encap
;
2050 encap
= (struct iphdr
*)(skb_transport_header(skb
) + pimlen
);
2053 * a. packet is really sent to a multicast group
2054 * b. packet is not a NULL-REGISTER
2055 * c. packet is not truncated
2057 if (!ipv4_is_multicast(encap
->daddr
) ||
2058 encap
->tot_len
== 0 ||
2059 ntohs(encap
->tot_len
) + pimlen
> skb
->len
)
2062 read_lock(&mrt_lock
);
2063 if (mrt
->mroute_reg_vif_num
>= 0)
2064 reg_dev
= mrt
->vif_table
[mrt
->mroute_reg_vif_num
].dev
;
2065 read_unlock(&mrt_lock
);
2067 if (reg_dev
== NULL
)
2070 skb
->mac_header
= skb
->network_header
;
2071 skb_pull(skb
, (u8
*)encap
- skb
->data
);
2072 skb_reset_network_header(skb
);
2073 skb
->protocol
= htons(ETH_P_IP
);
2074 skb
->ip_summed
= CHECKSUM_NONE
;
2076 skb_tunnel_rx(skb
, reg_dev
, dev_net(reg_dev
));
2080 return NET_RX_SUCCESS
;
2084 #ifdef CONFIG_IP_PIMSM_V1
2086 * Handle IGMP messages of PIMv1
2089 int pim_rcv_v1(struct sk_buff
*skb
)
2091 struct igmphdr
*pim
;
2092 struct net
*net
= dev_net(skb
->dev
);
2093 struct mr_table
*mrt
;
2095 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2098 pim
= igmp_hdr(skb
);
2100 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2103 if (!mrt
->mroute_do_pim
||
2104 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
2107 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2115 #ifdef CONFIG_IP_PIMSM_V2
2116 static int pim_rcv(struct sk_buff
*skb
)
2118 struct pimreghdr
*pim
;
2119 struct net
*net
= dev_net(skb
->dev
);
2120 struct mr_table
*mrt
;
2122 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2125 pim
= (struct pimreghdr
*)skb_transport_header(skb
);
2126 if (pim
->type
!= ((PIM_VERSION
<< 4) | (PIM_REGISTER
)) ||
2127 (pim
->flags
& PIM_NULL_REGISTER
) ||
2128 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
2129 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
2132 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2135 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2143 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2144 struct mfc_cache
*c
, struct rtmsg
*rtm
)
2147 struct rtnexthop
*nhp
;
2148 struct nlattr
*mp_attr
;
2149 struct rta_mfc_stats mfcs
;
2151 /* If cache is unresolved, don't try to parse IIF and OIF */
2152 if (c
->mfc_parent
>= MAXVIFS
)
2155 if (VIF_EXISTS(mrt
, c
->mfc_parent
) &&
2156 nla_put_u32(skb
, RTA_IIF
, mrt
->vif_table
[c
->mfc_parent
].dev
->ifindex
) < 0)
2159 if (!(mp_attr
= nla_nest_start(skb
, RTA_MULTIPATH
)))
2162 for (ct
= c
->mfc_un
.res
.minvif
; ct
< c
->mfc_un
.res
.maxvif
; ct
++) {
2163 if (VIF_EXISTS(mrt
, ct
) && c
->mfc_un
.res
.ttls
[ct
] < 255) {
2164 if (!(nhp
= nla_reserve_nohdr(skb
, sizeof(*nhp
)))) {
2165 nla_nest_cancel(skb
, mp_attr
);
2169 nhp
->rtnh_flags
= 0;
2170 nhp
->rtnh_hops
= c
->mfc_un
.res
.ttls
[ct
];
2171 nhp
->rtnh_ifindex
= mrt
->vif_table
[ct
].dev
->ifindex
;
2172 nhp
->rtnh_len
= sizeof(*nhp
);
2176 nla_nest_end(skb
, mp_attr
);
2178 mfcs
.mfcs_packets
= c
->mfc_un
.res
.pkt
;
2179 mfcs
.mfcs_bytes
= c
->mfc_un
.res
.bytes
;
2180 mfcs
.mfcs_wrong_if
= c
->mfc_un
.res
.wrong_if
;
2181 if (nla_put(skb
, RTA_MFC_STATS
, sizeof(mfcs
), &mfcs
) < 0)
2184 rtm
->rtm_type
= RTN_MULTICAST
;
2188 int ipmr_get_route(struct net
*net
, struct sk_buff
*skb
,
2189 __be32 saddr
, __be32 daddr
,
2190 struct rtmsg
*rtm
, int nowait
)
2192 struct mfc_cache
*cache
;
2193 struct mr_table
*mrt
;
2196 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2201 cache
= ipmr_cache_find(mrt
, saddr
, daddr
);
2202 if (cache
== NULL
&& skb
->dev
) {
2203 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
2206 cache
= ipmr_cache_find_any(mrt
, daddr
, vif
);
2208 if (cache
== NULL
) {
2209 struct sk_buff
*skb2
;
2211 struct net_device
*dev
;
2220 read_lock(&mrt_lock
);
2222 vif
= ipmr_find_vif(mrt
, dev
);
2224 read_unlock(&mrt_lock
);
2228 skb2
= skb_clone(skb
, GFP_ATOMIC
);
2230 read_unlock(&mrt_lock
);
2235 skb_push(skb2
, sizeof(struct iphdr
));
2236 skb_reset_network_header(skb2
);
2238 iph
->ihl
= sizeof(struct iphdr
) >> 2;
2242 err
= ipmr_cache_unresolved(mrt
, vif
, skb2
);
2243 read_unlock(&mrt_lock
);
2248 read_lock(&mrt_lock
);
2249 if (!nowait
&& (rtm
->rtm_flags
& RTM_F_NOTIFY
))
2250 cache
->mfc_flags
|= MFC_NOTIFY
;
2251 err
= __ipmr_fill_mroute(mrt
, skb
, cache
, rtm
);
2252 read_unlock(&mrt_lock
);
2257 static int ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2258 u32 portid
, u32 seq
, struct mfc_cache
*c
, int cmd
,
2261 struct nlmsghdr
*nlh
;
2265 nlh
= nlmsg_put(skb
, portid
, seq
, cmd
, sizeof(*rtm
), flags
);
2269 rtm
= nlmsg_data(nlh
);
2270 rtm
->rtm_family
= RTNL_FAMILY_IPMR
;
2271 rtm
->rtm_dst_len
= 32;
2272 rtm
->rtm_src_len
= 32;
2274 rtm
->rtm_table
= mrt
->id
;
2275 if (nla_put_u32(skb
, RTA_TABLE
, mrt
->id
))
2276 goto nla_put_failure
;
2277 rtm
->rtm_type
= RTN_MULTICAST
;
2278 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2279 if (c
->mfc_flags
& MFC_STATIC
)
2280 rtm
->rtm_protocol
= RTPROT_STATIC
;
2282 rtm
->rtm_protocol
= RTPROT_MROUTED
;
2285 if (nla_put_be32(skb
, RTA_SRC
, c
->mfc_origin
) ||
2286 nla_put_be32(skb
, RTA_DST
, c
->mfc_mcastgrp
))
2287 goto nla_put_failure
;
2288 err
= __ipmr_fill_mroute(mrt
, skb
, c
, rtm
);
2289 /* do not break the dump if cache is unresolved */
2290 if (err
< 0 && err
!= -ENOENT
)
2291 goto nla_put_failure
;
2293 return nlmsg_end(skb
, nlh
);
2296 nlmsg_cancel(skb
, nlh
);
2300 static size_t mroute_msgsize(bool unresolved
, int maxvif
)
2303 NLMSG_ALIGN(sizeof(struct rtmsg
))
2304 + nla_total_size(4) /* RTA_TABLE */
2305 + nla_total_size(4) /* RTA_SRC */
2306 + nla_total_size(4) /* RTA_DST */
2311 + nla_total_size(4) /* RTA_IIF */
2312 + nla_total_size(0) /* RTA_MULTIPATH */
2313 + maxvif
* NLA_ALIGN(sizeof(struct rtnexthop
))
2315 + nla_total_size(sizeof(struct rta_mfc_stats
))
2321 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
2324 struct net
*net
= read_pnet(&mrt
->net
);
2325 struct sk_buff
*skb
;
2328 skb
= nlmsg_new(mroute_msgsize(mfc
->mfc_parent
>= MAXVIFS
, mrt
->maxvif
),
2333 err
= ipmr_fill_mroute(mrt
, skb
, 0, 0, mfc
, cmd
, 0);
2337 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_MROUTE
, NULL
, GFP_ATOMIC
);
2343 rtnl_set_sk_err(net
, RTNLGRP_IPV4_MROUTE
, err
);
2346 static int ipmr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2348 struct net
*net
= sock_net(skb
->sk
);
2349 struct mr_table
*mrt
;
2350 struct mfc_cache
*mfc
;
2351 unsigned int t
= 0, s_t
;
2352 unsigned int h
= 0, s_h
;
2353 unsigned int e
= 0, s_e
;
2360 ipmr_for_each_table(mrt
, net
) {
2365 for (h
= s_h
; h
< MFC_LINES
; h
++) {
2366 list_for_each_entry_rcu(mfc
, &mrt
->mfc_cache_array
[h
], list
) {
2369 if (ipmr_fill_mroute(mrt
, skb
,
2370 NETLINK_CB(cb
->skb
).portid
,
2380 spin_lock_bh(&mfc_unres_lock
);
2381 list_for_each_entry(mfc
, &mrt
->mfc_unres_queue
, list
) {
2384 if (ipmr_fill_mroute(mrt
, skb
,
2385 NETLINK_CB(cb
->skb
).portid
,
2389 spin_unlock_bh(&mfc_unres_lock
);
2395 spin_unlock_bh(&mfc_unres_lock
);
2411 #ifdef CONFIG_PROC_FS
2413 * The /proc interfaces to multicast routing :
2414 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2416 struct ipmr_vif_iter
{
2417 struct seq_net_private p
;
2418 struct mr_table
*mrt
;
2422 static struct vif_device
*ipmr_vif_seq_idx(struct net
*net
,
2423 struct ipmr_vif_iter
*iter
,
2426 struct mr_table
*mrt
= iter
->mrt
;
2428 for (iter
->ct
= 0; iter
->ct
< mrt
->maxvif
; ++iter
->ct
) {
2429 if (!VIF_EXISTS(mrt
, iter
->ct
))
2432 return &mrt
->vif_table
[iter
->ct
];
2437 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2438 __acquires(mrt_lock
)
2440 struct ipmr_vif_iter
*iter
= seq
->private;
2441 struct net
*net
= seq_file_net(seq
);
2442 struct mr_table
*mrt
;
2444 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2446 return ERR_PTR(-ENOENT
);
2450 read_lock(&mrt_lock
);
2451 return *pos
? ipmr_vif_seq_idx(net
, seq
->private, *pos
- 1)
2455 static void *ipmr_vif_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2457 struct ipmr_vif_iter
*iter
= seq
->private;
2458 struct net
*net
= seq_file_net(seq
);
2459 struct mr_table
*mrt
= iter
->mrt
;
2462 if (v
== SEQ_START_TOKEN
)
2463 return ipmr_vif_seq_idx(net
, iter
, 0);
2465 while (++iter
->ct
< mrt
->maxvif
) {
2466 if (!VIF_EXISTS(mrt
, iter
->ct
))
2468 return &mrt
->vif_table
[iter
->ct
];
2473 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
2474 __releases(mrt_lock
)
2476 read_unlock(&mrt_lock
);
2479 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
2481 struct ipmr_vif_iter
*iter
= seq
->private;
2482 struct mr_table
*mrt
= iter
->mrt
;
2484 if (v
== SEQ_START_TOKEN
) {
2486 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2488 const struct vif_device
*vif
= v
;
2489 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
2492 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2493 vif
- mrt
->vif_table
,
2494 name
, vif
->bytes_in
, vif
->pkt_in
,
2495 vif
->bytes_out
, vif
->pkt_out
,
2496 vif
->flags
, vif
->local
, vif
->remote
);
2501 static const struct seq_operations ipmr_vif_seq_ops
= {
2502 .start
= ipmr_vif_seq_start
,
2503 .next
= ipmr_vif_seq_next
,
2504 .stop
= ipmr_vif_seq_stop
,
2505 .show
= ipmr_vif_seq_show
,
2508 static int ipmr_vif_open(struct inode
*inode
, struct file
*file
)
2510 return seq_open_net(inode
, file
, &ipmr_vif_seq_ops
,
2511 sizeof(struct ipmr_vif_iter
));
2514 static const struct file_operations ipmr_vif_fops
= {
2515 .owner
= THIS_MODULE
,
2516 .open
= ipmr_vif_open
,
2518 .llseek
= seq_lseek
,
2519 .release
= seq_release_net
,
2522 struct ipmr_mfc_iter
{
2523 struct seq_net_private p
;
2524 struct mr_table
*mrt
;
2525 struct list_head
*cache
;
2530 static struct mfc_cache
*ipmr_mfc_seq_idx(struct net
*net
,
2531 struct ipmr_mfc_iter
*it
, loff_t pos
)
2533 struct mr_table
*mrt
= it
->mrt
;
2534 struct mfc_cache
*mfc
;
2537 for (it
->ct
= 0; it
->ct
< MFC_LINES
; it
->ct
++) {
2538 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2539 list_for_each_entry_rcu(mfc
, it
->cache
, list
)
2545 spin_lock_bh(&mfc_unres_lock
);
2546 it
->cache
= &mrt
->mfc_unres_queue
;
2547 list_for_each_entry(mfc
, it
->cache
, list
)
2550 spin_unlock_bh(&mfc_unres_lock
);
2557 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2559 struct ipmr_mfc_iter
*it
= seq
->private;
2560 struct net
*net
= seq_file_net(seq
);
2561 struct mr_table
*mrt
;
2563 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2565 return ERR_PTR(-ENOENT
);
2570 return *pos
? ipmr_mfc_seq_idx(net
, seq
->private, *pos
- 1)
2574 static void *ipmr_mfc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2576 struct mfc_cache
*mfc
= v
;
2577 struct ipmr_mfc_iter
*it
= seq
->private;
2578 struct net
*net
= seq_file_net(seq
);
2579 struct mr_table
*mrt
= it
->mrt
;
2583 if (v
== SEQ_START_TOKEN
)
2584 return ipmr_mfc_seq_idx(net
, seq
->private, 0);
2586 if (mfc
->list
.next
!= it
->cache
)
2587 return list_entry(mfc
->list
.next
, struct mfc_cache
, list
);
2589 if (it
->cache
== &mrt
->mfc_unres_queue
)
2592 BUG_ON(it
->cache
!= &mrt
->mfc_cache_array
[it
->ct
]);
2594 while (++it
->ct
< MFC_LINES
) {
2595 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2596 if (list_empty(it
->cache
))
2598 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2601 /* exhausted cache_array, show unresolved */
2603 it
->cache
= &mrt
->mfc_unres_queue
;
2606 spin_lock_bh(&mfc_unres_lock
);
2607 if (!list_empty(it
->cache
))
2608 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2611 spin_unlock_bh(&mfc_unres_lock
);
2617 static void ipmr_mfc_seq_stop(struct seq_file
*seq
, void *v
)
2619 struct ipmr_mfc_iter
*it
= seq
->private;
2620 struct mr_table
*mrt
= it
->mrt
;
2622 if (it
->cache
== &mrt
->mfc_unres_queue
)
2623 spin_unlock_bh(&mfc_unres_lock
);
2624 else if (it
->cache
== &mrt
->mfc_cache_array
[it
->ct
])
2628 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
2632 if (v
== SEQ_START_TOKEN
) {
2634 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2636 const struct mfc_cache
*mfc
= v
;
2637 const struct ipmr_mfc_iter
*it
= seq
->private;
2638 const struct mr_table
*mrt
= it
->mrt
;
2640 seq_printf(seq
, "%08X %08X %-3hd",
2641 (__force u32
) mfc
->mfc_mcastgrp
,
2642 (__force u32
) mfc
->mfc_origin
,
2645 if (it
->cache
!= &mrt
->mfc_unres_queue
) {
2646 seq_printf(seq
, " %8lu %8lu %8lu",
2647 mfc
->mfc_un
.res
.pkt
,
2648 mfc
->mfc_un
.res
.bytes
,
2649 mfc
->mfc_un
.res
.wrong_if
);
2650 for (n
= mfc
->mfc_un
.res
.minvif
;
2651 n
< mfc
->mfc_un
.res
.maxvif
; n
++) {
2652 if (VIF_EXISTS(mrt
, n
) &&
2653 mfc
->mfc_un
.res
.ttls
[n
] < 255)
2656 n
, mfc
->mfc_un
.res
.ttls
[n
]);
2659 /* unresolved mfc_caches don't contain
2660 * pkt, bytes and wrong_if values
2662 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2664 seq_putc(seq
, '\n');
2669 static const struct seq_operations ipmr_mfc_seq_ops
= {
2670 .start
= ipmr_mfc_seq_start
,
2671 .next
= ipmr_mfc_seq_next
,
2672 .stop
= ipmr_mfc_seq_stop
,
2673 .show
= ipmr_mfc_seq_show
,
2676 static int ipmr_mfc_open(struct inode
*inode
, struct file
*file
)
2678 return seq_open_net(inode
, file
, &ipmr_mfc_seq_ops
,
2679 sizeof(struct ipmr_mfc_iter
));
2682 static const struct file_operations ipmr_mfc_fops
= {
2683 .owner
= THIS_MODULE
,
2684 .open
= ipmr_mfc_open
,
2686 .llseek
= seq_lseek
,
2687 .release
= seq_release_net
,
2691 #ifdef CONFIG_IP_PIMSM_V2
2692 static const struct net_protocol pim_protocol
= {
2700 * Setup for IP multicast routing
2702 static int __net_init
ipmr_net_init(struct net
*net
)
2706 err
= ipmr_rules_init(net
);
2710 #ifdef CONFIG_PROC_FS
2712 if (!proc_create("ip_mr_vif", 0, net
->proc_net
, &ipmr_vif_fops
))
2714 if (!proc_create("ip_mr_cache", 0, net
->proc_net
, &ipmr_mfc_fops
))
2715 goto proc_cache_fail
;
2719 #ifdef CONFIG_PROC_FS
2721 remove_proc_entry("ip_mr_vif", net
->proc_net
);
2723 ipmr_rules_exit(net
);
2729 static void __net_exit
ipmr_net_exit(struct net
*net
)
2731 #ifdef CONFIG_PROC_FS
2732 remove_proc_entry("ip_mr_cache", net
->proc_net
);
2733 remove_proc_entry("ip_mr_vif", net
->proc_net
);
2735 ipmr_rules_exit(net
);
2738 static struct pernet_operations ipmr_net_ops
= {
2739 .init
= ipmr_net_init
,
2740 .exit
= ipmr_net_exit
,
2743 int __init
ip_mr_init(void)
2747 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
2748 sizeof(struct mfc_cache
),
2749 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
2754 err
= register_pernet_subsys(&ipmr_net_ops
);
2756 goto reg_pernet_fail
;
2758 err
= register_netdevice_notifier(&ip_mr_notifier
);
2760 goto reg_notif_fail
;
2761 #ifdef CONFIG_IP_PIMSM_V2
2762 if (inet_add_protocol(&pim_protocol
, IPPROTO_PIM
) < 0) {
2763 pr_err("%s: can't add PIM protocol\n", __func__
);
2765 goto add_proto_fail
;
2768 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETROUTE
,
2769 NULL
, ipmr_rtm_dumproute
, NULL
);
2772 #ifdef CONFIG_IP_PIMSM_V2
2774 unregister_netdevice_notifier(&ip_mr_notifier
);
2777 unregister_pernet_subsys(&ipmr_net_ops
);
2779 kmem_cache_destroy(mrt_cachep
);