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
;
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
;
85 bool mroute_do_assert
;
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 void ipmr_free_table(struct mr_table
*mrt
);
128 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
129 struct sk_buff
*skb
, struct mfc_cache
*cache
,
131 static int ipmr_cache_report(struct mr_table
*mrt
,
132 struct sk_buff
*pkt
, vifi_t vifi
, int assert);
133 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
134 struct mfc_cache
*c
, struct rtmsg
*rtm
);
135 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
137 static void mroute_clean_tables(struct mr_table
*mrt
, bool all
);
138 static void ipmr_expire_process(unsigned long arg
);
140 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
141 #define ipmr_for_each_table(mrt, net) \
142 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
144 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
146 struct mr_table
*mrt
;
148 ipmr_for_each_table(mrt
, net
) {
155 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
156 struct mr_table
**mrt
)
159 struct ipmr_result res
;
160 struct fib_lookup_arg arg
= {
162 .flags
= FIB_LOOKUP_NOREF
,
165 err
= fib_rules_lookup(net
->ipv4
.mr_rules_ops
,
166 flowi4_to_flowi(flp4
), 0, &arg
);
173 static int ipmr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
174 int flags
, struct fib_lookup_arg
*arg
)
176 struct ipmr_result
*res
= arg
->result
;
177 struct mr_table
*mrt
;
179 switch (rule
->action
) {
182 case FR_ACT_UNREACHABLE
:
184 case FR_ACT_PROHIBIT
:
186 case FR_ACT_BLACKHOLE
:
191 mrt
= ipmr_get_table(rule
->fr_net
, rule
->table
);
198 static int ipmr_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
203 static const struct nla_policy ipmr_rule_policy
[FRA_MAX
+ 1] = {
207 static int ipmr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
208 struct fib_rule_hdr
*frh
, struct nlattr
**tb
)
213 static int ipmr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
219 static int ipmr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
220 struct fib_rule_hdr
*frh
)
228 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template
= {
229 .family
= RTNL_FAMILY_IPMR
,
230 .rule_size
= sizeof(struct ipmr_rule
),
231 .addr_size
= sizeof(u32
),
232 .action
= ipmr_rule_action
,
233 .match
= ipmr_rule_match
,
234 .configure
= ipmr_rule_configure
,
235 .compare
= ipmr_rule_compare
,
236 .fill
= ipmr_rule_fill
,
237 .nlgroup
= RTNLGRP_IPV4_RULE
,
238 .policy
= ipmr_rule_policy
,
239 .owner
= THIS_MODULE
,
242 static int __net_init
ipmr_rules_init(struct net
*net
)
244 struct fib_rules_ops
*ops
;
245 struct mr_table
*mrt
;
248 ops
= fib_rules_register(&ipmr_rules_ops_template
, net
);
252 INIT_LIST_HEAD(&net
->ipv4
.mr_tables
);
254 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
260 err
= fib_default_rule_add(ops
, 0x7fff, RT_TABLE_DEFAULT
, 0);
264 net
->ipv4
.mr_rules_ops
= ops
;
268 ipmr_free_table(mrt
);
270 fib_rules_unregister(ops
);
274 static void __net_exit
ipmr_rules_exit(struct net
*net
)
276 struct mr_table
*mrt
, *next
;
279 list_for_each_entry_safe(mrt
, next
, &net
->ipv4
.mr_tables
, list
) {
280 list_del(&mrt
->list
);
281 ipmr_free_table(mrt
);
283 fib_rules_unregister(net
->ipv4
.mr_rules_ops
);
287 #define ipmr_for_each_table(mrt, net) \
288 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
290 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
292 return net
->ipv4
.mrt
;
295 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
296 struct mr_table
**mrt
)
298 *mrt
= net
->ipv4
.mrt
;
302 static int __net_init
ipmr_rules_init(struct net
*net
)
304 net
->ipv4
.mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
305 return net
->ipv4
.mrt
? 0 : -ENOMEM
;
308 static void __net_exit
ipmr_rules_exit(struct net
*net
)
311 ipmr_free_table(net
->ipv4
.mrt
);
312 net
->ipv4
.mrt
= NULL
;
317 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
)
319 struct mr_table
*mrt
;
322 mrt
= ipmr_get_table(net
, id
);
326 mrt
= kzalloc(sizeof(*mrt
), GFP_KERNEL
);
329 write_pnet(&mrt
->net
, net
);
332 /* Forwarding cache */
333 for (i
= 0; i
< MFC_LINES
; i
++)
334 INIT_LIST_HEAD(&mrt
->mfc_cache_array
[i
]);
336 INIT_LIST_HEAD(&mrt
->mfc_unres_queue
);
338 setup_timer(&mrt
->ipmr_expire_timer
, ipmr_expire_process
,
341 #ifdef CONFIG_IP_PIMSM
342 mrt
->mroute_reg_vif_num
= -1;
344 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
345 list_add_tail_rcu(&mrt
->list
, &net
->ipv4
.mr_tables
);
350 static void ipmr_free_table(struct mr_table
*mrt
)
352 del_timer_sync(&mrt
->ipmr_expire_timer
);
353 mroute_clean_tables(mrt
, true);
357 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
359 static void ipmr_del_tunnel(struct net_device
*dev
, struct vifctl
*v
)
361 struct net
*net
= dev_net(dev
);
365 dev
= __dev_get_by_name(net
, "tunl0");
367 const struct net_device_ops
*ops
= dev
->netdev_ops
;
369 struct ip_tunnel_parm p
;
371 memset(&p
, 0, sizeof(p
));
372 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
373 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
376 p
.iph
.protocol
= IPPROTO_IPIP
;
377 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
378 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
380 if (ops
->ndo_do_ioctl
) {
381 mm_segment_t oldfs
= get_fs();
384 ops
->ndo_do_ioctl(dev
, &ifr
, SIOCDELTUNNEL
);
391 struct net_device
*ipmr_new_tunnel(struct net
*net
, struct vifctl
*v
)
393 struct net_device
*dev
;
395 dev
= __dev_get_by_name(net
, "tunl0");
398 const struct net_device_ops
*ops
= dev
->netdev_ops
;
401 struct ip_tunnel_parm p
;
402 struct in_device
*in_dev
;
404 memset(&p
, 0, sizeof(p
));
405 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
406 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
409 p
.iph
.protocol
= IPPROTO_IPIP
;
410 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
411 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
413 if (ops
->ndo_do_ioctl
) {
414 mm_segment_t oldfs
= get_fs();
417 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
425 (dev
= __dev_get_by_name(net
, p
.name
)) != NULL
) {
426 dev
->flags
|= IFF_MULTICAST
;
428 in_dev
= __in_dev_get_rtnl(dev
);
432 ipv4_devconf_setall(in_dev
);
433 neigh_parms_data_state_setall(in_dev
->arp_parms
);
434 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
444 unregister_netdevice(dev
);
448 #ifdef CONFIG_IP_PIMSM
450 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
452 struct net
*net
= dev_net(dev
);
453 struct mr_table
*mrt
;
454 struct flowi4 fl4
= {
455 .flowi4_oif
= dev
->ifindex
,
456 .flowi4_iif
= skb
->skb_iif
? : LOOPBACK_IFINDEX
,
457 .flowi4_mark
= skb
->mark
,
461 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
467 read_lock(&mrt_lock
);
468 dev
->stats
.tx_bytes
+= skb
->len
;
469 dev
->stats
.tx_packets
++;
470 ipmr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, IGMPMSG_WHOLEPKT
);
471 read_unlock(&mrt_lock
);
476 static int reg_vif_get_iflink(const struct net_device
*dev
)
481 static const struct net_device_ops reg_vif_netdev_ops
= {
482 .ndo_start_xmit
= reg_vif_xmit
,
483 .ndo_get_iflink
= reg_vif_get_iflink
,
486 static void reg_vif_setup(struct net_device
*dev
)
488 dev
->type
= ARPHRD_PIMREG
;
489 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
490 dev
->flags
= IFF_NOARP
;
491 dev
->netdev_ops
= ®_vif_netdev_ops
;
492 dev
->destructor
= free_netdev
;
493 dev
->features
|= NETIF_F_NETNS_LOCAL
;
496 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
498 struct net_device
*dev
;
499 struct in_device
*in_dev
;
502 if (mrt
->id
== RT_TABLE_DEFAULT
)
503 sprintf(name
, "pimreg");
505 sprintf(name
, "pimreg%u", mrt
->id
);
507 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, reg_vif_setup
);
512 dev_net_set(dev
, net
);
514 if (register_netdevice(dev
)) {
520 in_dev
= __in_dev_get_rcu(dev
);
526 ipv4_devconf_setall(in_dev
);
527 neigh_parms_data_state_setall(in_dev
->arp_parms
);
528 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
539 unregister_netdevice(dev
);
545 * vif_delete - Delete a VIF entry
546 * @notify: Set to 1, if the caller is a notifier_call
549 static int vif_delete(struct mr_table
*mrt
, int vifi
, int notify
,
550 struct list_head
*head
)
552 struct vif_device
*v
;
553 struct net_device
*dev
;
554 struct in_device
*in_dev
;
556 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
557 return -EADDRNOTAVAIL
;
559 v
= &mrt
->vif_table
[vifi
];
561 write_lock_bh(&mrt_lock
);
566 write_unlock_bh(&mrt_lock
);
567 return -EADDRNOTAVAIL
;
570 #ifdef CONFIG_IP_PIMSM
571 if (vifi
== mrt
->mroute_reg_vif_num
)
572 mrt
->mroute_reg_vif_num
= -1;
575 if (vifi
+ 1 == mrt
->maxvif
) {
578 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
579 if (VIF_EXISTS(mrt
, tmp
))
585 write_unlock_bh(&mrt_lock
);
587 dev_set_allmulti(dev
, -1);
589 in_dev
= __in_dev_get_rtnl(dev
);
591 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)--;
592 inet_netconf_notify_devconf(dev_net(dev
),
593 NETCONFA_MC_FORWARDING
,
594 dev
->ifindex
, &in_dev
->cnf
);
595 ip_rt_multicast_event(in_dev
);
598 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
) && !notify
)
599 unregister_netdevice_queue(dev
, head
);
605 static void ipmr_cache_free_rcu(struct rcu_head
*head
)
607 struct mfc_cache
*c
= container_of(head
, struct mfc_cache
, rcu
);
609 kmem_cache_free(mrt_cachep
, c
);
612 static inline void ipmr_cache_free(struct mfc_cache
*c
)
614 call_rcu(&c
->rcu
, ipmr_cache_free_rcu
);
617 /* Destroy an unresolved cache entry, killing queued skbs
618 * and reporting error to netlink readers.
621 static void ipmr_destroy_unres(struct mr_table
*mrt
, struct mfc_cache
*c
)
623 struct net
*net
= read_pnet(&mrt
->net
);
627 atomic_dec(&mrt
->cache_resolve_queue_len
);
629 while ((skb
= skb_dequeue(&c
->mfc_un
.unres
.unresolved
))) {
630 if (ip_hdr(skb
)->version
== 0) {
631 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
632 nlh
->nlmsg_type
= NLMSG_ERROR
;
633 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
634 skb_trim(skb
, nlh
->nlmsg_len
);
636 e
->error
= -ETIMEDOUT
;
637 memset(&e
->msg
, 0, sizeof(e
->msg
));
639 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
649 /* Timer process for the unresolved queue. */
651 static void ipmr_expire_process(unsigned long arg
)
653 struct mr_table
*mrt
= (struct mr_table
*)arg
;
655 unsigned long expires
;
656 struct mfc_cache
*c
, *next
;
658 if (!spin_trylock(&mfc_unres_lock
)) {
659 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+HZ
/10);
663 if (list_empty(&mrt
->mfc_unres_queue
))
669 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
670 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
671 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
672 if (interval
< expires
)
678 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
679 ipmr_destroy_unres(mrt
, c
);
682 if (!list_empty(&mrt
->mfc_unres_queue
))
683 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
686 spin_unlock(&mfc_unres_lock
);
689 /* Fill oifs list. It is called under write locked mrt_lock. */
691 static void ipmr_update_thresholds(struct mr_table
*mrt
, struct mfc_cache
*cache
,
696 cache
->mfc_un
.res
.minvif
= MAXVIFS
;
697 cache
->mfc_un
.res
.maxvif
= 0;
698 memset(cache
->mfc_un
.res
.ttls
, 255, MAXVIFS
);
700 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
701 if (VIF_EXISTS(mrt
, vifi
) &&
702 ttls
[vifi
] && ttls
[vifi
] < 255) {
703 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
704 if (cache
->mfc_un
.res
.minvif
> vifi
)
705 cache
->mfc_un
.res
.minvif
= vifi
;
706 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
707 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
712 static int vif_add(struct net
*net
, struct mr_table
*mrt
,
713 struct vifctl
*vifc
, int mrtsock
)
715 int vifi
= vifc
->vifc_vifi
;
716 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
717 struct net_device
*dev
;
718 struct in_device
*in_dev
;
722 if (VIF_EXISTS(mrt
, vifi
))
725 switch (vifc
->vifc_flags
) {
726 #ifdef CONFIG_IP_PIMSM
729 * Special Purpose VIF in PIM
730 * All the packets will be sent to the daemon
732 if (mrt
->mroute_reg_vif_num
>= 0)
734 dev
= ipmr_reg_vif(net
, mrt
);
737 err
= dev_set_allmulti(dev
, 1);
739 unregister_netdevice(dev
);
746 dev
= ipmr_new_tunnel(net
, vifc
);
749 err
= dev_set_allmulti(dev
, 1);
751 ipmr_del_tunnel(dev
, vifc
);
757 case VIFF_USE_IFINDEX
:
759 if (vifc
->vifc_flags
== VIFF_USE_IFINDEX
) {
760 dev
= dev_get_by_index(net
, vifc
->vifc_lcl_ifindex
);
761 if (dev
&& !__in_dev_get_rtnl(dev
)) {
763 return -EADDRNOTAVAIL
;
766 dev
= ip_dev_find(net
, vifc
->vifc_lcl_addr
.s_addr
);
769 return -EADDRNOTAVAIL
;
770 err
= dev_set_allmulti(dev
, 1);
780 in_dev
= __in_dev_get_rtnl(dev
);
783 return -EADDRNOTAVAIL
;
785 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)++;
786 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
, dev
->ifindex
,
788 ip_rt_multicast_event(in_dev
);
790 /* Fill in the VIF structures */
792 v
->rate_limit
= vifc
->vifc_rate_limit
;
793 v
->local
= vifc
->vifc_lcl_addr
.s_addr
;
794 v
->remote
= vifc
->vifc_rmt_addr
.s_addr
;
795 v
->flags
= vifc
->vifc_flags
;
797 v
->flags
|= VIFF_STATIC
;
798 v
->threshold
= vifc
->vifc_threshold
;
803 v
->link
= dev
->ifindex
;
804 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
))
805 v
->link
= dev_get_iflink(dev
);
807 /* And finish update writing critical data */
808 write_lock_bh(&mrt_lock
);
810 #ifdef CONFIG_IP_PIMSM
811 if (v
->flags
& VIFF_REGISTER
)
812 mrt
->mroute_reg_vif_num
= vifi
;
814 if (vifi
+1 > mrt
->maxvif
)
815 mrt
->maxvif
= vifi
+1;
816 write_unlock_bh(&mrt_lock
);
820 /* called with rcu_read_lock() */
821 static struct mfc_cache
*ipmr_cache_find(struct mr_table
*mrt
,
825 int line
= MFC_HASH(mcastgrp
, origin
);
828 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
) {
829 if (c
->mfc_origin
== origin
&& c
->mfc_mcastgrp
== mcastgrp
)
835 /* Look for a (*,*,oif) entry */
836 static struct mfc_cache
*ipmr_cache_find_any_parent(struct mr_table
*mrt
,
839 int line
= MFC_HASH(htonl(INADDR_ANY
), htonl(INADDR_ANY
));
842 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
)
843 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
844 c
->mfc_mcastgrp
== htonl(INADDR_ANY
) &&
845 c
->mfc_un
.res
.ttls
[vifi
] < 255)
851 /* Look for a (*,G) entry */
852 static struct mfc_cache
*ipmr_cache_find_any(struct mr_table
*mrt
,
853 __be32 mcastgrp
, int vifi
)
855 int line
= MFC_HASH(mcastgrp
, htonl(INADDR_ANY
));
856 struct mfc_cache
*c
, *proxy
;
858 if (mcastgrp
== htonl(INADDR_ANY
))
861 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
)
862 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
863 c
->mfc_mcastgrp
== mcastgrp
) {
864 if (c
->mfc_un
.res
.ttls
[vifi
] < 255)
867 /* It's ok if the vifi is part of the static tree */
868 proxy
= ipmr_cache_find_any_parent(mrt
,
870 if (proxy
&& proxy
->mfc_un
.res
.ttls
[vifi
] < 255)
875 return ipmr_cache_find_any_parent(mrt
, vifi
);
879 * Allocate a multicast cache entry
881 static struct mfc_cache
*ipmr_cache_alloc(void)
883 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
886 c
->mfc_un
.res
.minvif
= MAXVIFS
;
890 static struct mfc_cache
*ipmr_cache_alloc_unres(void)
892 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
895 skb_queue_head_init(&c
->mfc_un
.unres
.unresolved
);
896 c
->mfc_un
.unres
.expires
= jiffies
+ 10*HZ
;
902 * A cache entry has gone into a resolved state from queued
905 static void ipmr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
906 struct mfc_cache
*uc
, struct mfc_cache
*c
)
911 /* Play the pending entries through our router */
913 while ((skb
= __skb_dequeue(&uc
->mfc_un
.unres
.unresolved
))) {
914 if (ip_hdr(skb
)->version
== 0) {
915 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
917 if (__ipmr_fill_mroute(mrt
, skb
, c
, nlmsg_data(nlh
)) > 0) {
918 nlh
->nlmsg_len
= skb_tail_pointer(skb
) -
921 nlh
->nlmsg_type
= NLMSG_ERROR
;
922 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
923 skb_trim(skb
, nlh
->nlmsg_len
);
925 e
->error
= -EMSGSIZE
;
926 memset(&e
->msg
, 0, sizeof(e
->msg
));
929 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
931 ip_mr_forward(net
, mrt
, skb
, c
, 0);
937 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
938 * expects the following bizarre scheme.
940 * Called under mrt_lock.
943 static int ipmr_cache_report(struct mr_table
*mrt
,
944 struct sk_buff
*pkt
, vifi_t vifi
, int assert)
947 const int ihl
= ip_hdrlen(pkt
);
948 struct igmphdr
*igmp
;
950 struct sock
*mroute_sk
;
953 #ifdef CONFIG_IP_PIMSM
954 if (assert == IGMPMSG_WHOLEPKT
)
955 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
958 skb
= alloc_skb(128, GFP_ATOMIC
);
963 #ifdef CONFIG_IP_PIMSM
964 if (assert == IGMPMSG_WHOLEPKT
) {
965 /* Ugly, but we have no choice with this interface.
966 * Duplicate old header, fix ihl, length etc.
967 * And all this only to mangle msg->im_msgtype and
968 * to set msg->im_mbz to "mbz" :-)
970 skb_push(skb
, sizeof(struct iphdr
));
971 skb_reset_network_header(skb
);
972 skb_reset_transport_header(skb
);
973 msg
= (struct igmpmsg
*)skb_network_header(skb
);
974 memcpy(msg
, skb_network_header(pkt
), sizeof(struct iphdr
));
975 msg
->im_msgtype
= IGMPMSG_WHOLEPKT
;
977 msg
->im_vif
= mrt
->mroute_reg_vif_num
;
978 ip_hdr(skb
)->ihl
= sizeof(struct iphdr
) >> 2;
979 ip_hdr(skb
)->tot_len
= htons(ntohs(ip_hdr(pkt
)->tot_len
) +
980 sizeof(struct iphdr
));
985 /* Copy the IP header */
987 skb_set_network_header(skb
, skb
->len
);
989 skb_copy_to_linear_data(skb
, pkt
->data
, ihl
);
990 ip_hdr(skb
)->protocol
= 0; /* Flag to the kernel this is a route add */
991 msg
= (struct igmpmsg
*)skb_network_header(skb
);
993 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
997 igmp
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
999 msg
->im_msgtype
= assert;
1001 ip_hdr(skb
)->tot_len
= htons(skb
->len
); /* Fix the length */
1002 skb
->transport_header
= skb
->network_header
;
1006 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1013 /* Deliver to mrouted */
1015 ret
= sock_queue_rcv_skb(mroute_sk
, skb
);
1018 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1026 * Queue a packet for resolution. It gets locked cache entry!
1030 ipmr_cache_unresolved(struct mr_table
*mrt
, vifi_t vifi
, struct sk_buff
*skb
)
1034 struct mfc_cache
*c
;
1035 const struct iphdr
*iph
= ip_hdr(skb
);
1037 spin_lock_bh(&mfc_unres_lock
);
1038 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, list
) {
1039 if (c
->mfc_mcastgrp
== iph
->daddr
&&
1040 c
->mfc_origin
== iph
->saddr
) {
1047 /* Create a new entry if allowable */
1049 if (atomic_read(&mrt
->cache_resolve_queue_len
) >= 10 ||
1050 (c
= ipmr_cache_alloc_unres()) == NULL
) {
1051 spin_unlock_bh(&mfc_unres_lock
);
1057 /* Fill in the new cache entry */
1060 c
->mfc_origin
= iph
->saddr
;
1061 c
->mfc_mcastgrp
= iph
->daddr
;
1063 /* Reflect first query at mrouted. */
1065 err
= ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_NOCACHE
);
1067 /* If the report failed throw the cache entry
1070 spin_unlock_bh(&mfc_unres_lock
);
1077 atomic_inc(&mrt
->cache_resolve_queue_len
);
1078 list_add(&c
->list
, &mrt
->mfc_unres_queue
);
1079 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1081 if (atomic_read(&mrt
->cache_resolve_queue_len
) == 1)
1082 mod_timer(&mrt
->ipmr_expire_timer
, c
->mfc_un
.unres
.expires
);
1085 /* See if we can append the packet */
1087 if (c
->mfc_un
.unres
.unresolved
.qlen
> 3) {
1091 skb_queue_tail(&c
->mfc_un
.unres
.unresolved
, skb
);
1095 spin_unlock_bh(&mfc_unres_lock
);
1100 * MFC cache manipulation by user space mroute daemon
1103 static int ipmr_mfc_delete(struct mr_table
*mrt
, struct mfcctl
*mfc
, int parent
)
1106 struct mfc_cache
*c
, *next
;
1108 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1110 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[line
], list
) {
1111 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1112 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
&&
1113 (parent
== -1 || parent
== c
->mfc_parent
)) {
1114 list_del_rcu(&c
->list
);
1115 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1123 static int ipmr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1124 struct mfcctl
*mfc
, int mrtsock
, int parent
)
1128 struct mfc_cache
*uc
, *c
;
1130 if (mfc
->mfcc_parent
>= MAXVIFS
)
1133 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1135 list_for_each_entry(c
, &mrt
->mfc_cache_array
[line
], list
) {
1136 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1137 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
&&
1138 (parent
== -1 || parent
== c
->mfc_parent
)) {
1145 write_lock_bh(&mrt_lock
);
1146 c
->mfc_parent
= mfc
->mfcc_parent
;
1147 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1149 c
->mfc_flags
|= MFC_STATIC
;
1150 write_unlock_bh(&mrt_lock
);
1151 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1155 if (mfc
->mfcc_mcastgrp
.s_addr
!= htonl(INADDR_ANY
) &&
1156 !ipv4_is_multicast(mfc
->mfcc_mcastgrp
.s_addr
))
1159 c
= ipmr_cache_alloc();
1163 c
->mfc_origin
= mfc
->mfcc_origin
.s_addr
;
1164 c
->mfc_mcastgrp
= mfc
->mfcc_mcastgrp
.s_addr
;
1165 c
->mfc_parent
= mfc
->mfcc_parent
;
1166 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1168 c
->mfc_flags
|= MFC_STATIC
;
1170 list_add_rcu(&c
->list
, &mrt
->mfc_cache_array
[line
]);
1173 * Check to see if we resolved a queued list. If so we
1174 * need to send on the frames and tidy up.
1177 spin_lock_bh(&mfc_unres_lock
);
1178 list_for_each_entry(uc
, &mrt
->mfc_unres_queue
, list
) {
1179 if (uc
->mfc_origin
== c
->mfc_origin
&&
1180 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
1181 list_del(&uc
->list
);
1182 atomic_dec(&mrt
->cache_resolve_queue_len
);
1187 if (list_empty(&mrt
->mfc_unres_queue
))
1188 del_timer(&mrt
->ipmr_expire_timer
);
1189 spin_unlock_bh(&mfc_unres_lock
);
1192 ipmr_cache_resolve(net
, mrt
, uc
, c
);
1193 ipmr_cache_free(uc
);
1195 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1200 * Close the multicast socket, and clear the vif tables etc
1203 static void mroute_clean_tables(struct mr_table
*mrt
, bool all
)
1207 struct mfc_cache
*c
, *next
;
1209 /* Shut down all active vif entries */
1211 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1212 if (!all
&& (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 (!all
&& (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
, false);
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 net
*net
, struct sk_buff
*skb
,
1648 __be32 saddr
, __be32 daddr
)
1651 const struct iphdr
*old_iph
= ip_hdr(skb
);
1653 skb_push(skb
, sizeof(struct iphdr
));
1654 skb
->transport_header
= skb
->network_header
;
1655 skb_reset_network_header(skb
);
1659 iph
->tos
= old_iph
->tos
;
1660 iph
->ttl
= old_iph
->ttl
;
1664 iph
->protocol
= IPPROTO_IPIP
;
1666 iph
->tot_len
= htons(skb
->len
);
1667 ip_select_ident(net
, skb
, NULL
);
1670 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1674 static inline int ipmr_forward_finish(struct net
*net
, struct sock
*sk
,
1675 struct sk_buff
*skb
)
1677 struct ip_options
*opt
= &(IPCB(skb
)->opt
);
1679 IP_INC_STATS(net
, IPSTATS_MIB_OUTFORWDATAGRAMS
);
1680 IP_ADD_STATS(net
, IPSTATS_MIB_OUTOCTETS
, skb
->len
);
1682 if (unlikely(opt
->optlen
))
1683 ip_forward_options(skb
);
1685 return dst_output(net
, sk
, skb
);
1689 * Processing handlers for ipmr_forward
1692 static void ipmr_queue_xmit(struct net
*net
, struct mr_table
*mrt
,
1693 struct sk_buff
*skb
, struct mfc_cache
*c
, int vifi
)
1695 const struct iphdr
*iph
= ip_hdr(skb
);
1696 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1697 struct net_device
*dev
;
1705 #ifdef CONFIG_IP_PIMSM
1706 if (vif
->flags
& VIFF_REGISTER
) {
1708 vif
->bytes_out
+= skb
->len
;
1709 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1710 vif
->dev
->stats
.tx_packets
++;
1711 ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_WHOLEPKT
);
1716 if (vif
->flags
& VIFF_TUNNEL
) {
1717 rt
= ip_route_output_ports(net
, &fl4
, NULL
,
1718 vif
->remote
, vif
->local
,
1721 RT_TOS(iph
->tos
), vif
->link
);
1724 encap
= sizeof(struct iphdr
);
1726 rt
= ip_route_output_ports(net
, &fl4
, NULL
, iph
->daddr
, 0,
1729 RT_TOS(iph
->tos
), vif
->link
);
1736 if (skb
->len
+encap
> dst_mtu(&rt
->dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1737 /* Do not fragment multicasts. Alas, IPv4 does not
1738 * allow to send ICMP, so that packets will disappear
1742 IP_INC_STATS(net
, IPSTATS_MIB_FRAGFAILS
);
1747 encap
+= LL_RESERVED_SPACE(dev
) + rt
->dst
.header_len
;
1749 if (skb_cow(skb
, encap
)) {
1755 vif
->bytes_out
+= skb
->len
;
1758 skb_dst_set(skb
, &rt
->dst
);
1759 ip_decrease_ttl(ip_hdr(skb
));
1761 /* FIXME: forward and output firewalls used to be called here.
1762 * What do we do with netfilter? -- RR
1764 if (vif
->flags
& VIFF_TUNNEL
) {
1765 ip_encap(net
, skb
, vif
->local
, vif
->remote
);
1766 /* FIXME: extra output firewall step used to be here. --RR */
1767 vif
->dev
->stats
.tx_packets
++;
1768 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1771 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1774 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1775 * not only before forwarding, but after forwarding on all output
1776 * interfaces. It is clear, if mrouter runs a multicasting
1777 * program, it should receive packets not depending to what interface
1778 * program is joined.
1779 * If we will not make it, the program will have to join on all
1780 * interfaces. On the other hand, multihoming host (or router, but
1781 * not mrouter) cannot join to more than one interface - it will
1782 * result in receiving multiple packets.
1784 NF_HOOK(NFPROTO_IPV4
, NF_INET_FORWARD
,
1785 net
, NULL
, skb
, skb
->dev
, dev
,
1786 ipmr_forward_finish
);
1793 static int ipmr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
1797 for (ct
= mrt
->maxvif
-1; ct
>= 0; ct
--) {
1798 if (mrt
->vif_table
[ct
].dev
== dev
)
1804 /* "local" means that we should preserve one skb (for local delivery) */
1806 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
1807 struct sk_buff
*skb
, struct mfc_cache
*cache
,
1812 int true_vifi
= ipmr_find_vif(mrt
, skb
->dev
);
1814 vif
= cache
->mfc_parent
;
1815 cache
->mfc_un
.res
.pkt
++;
1816 cache
->mfc_un
.res
.bytes
+= skb
->len
;
1818 if (cache
->mfc_origin
== htonl(INADDR_ANY
) && true_vifi
>= 0) {
1819 struct mfc_cache
*cache_proxy
;
1821 /* For an (*,G) entry, we only check that the incomming
1822 * interface is part of the static tree.
1824 cache_proxy
= ipmr_cache_find_any_parent(mrt
, vif
);
1826 cache_proxy
->mfc_un
.res
.ttls
[true_vifi
] < 255)
1831 * Wrong interface: drop packet and (maybe) send PIM assert.
1833 if (mrt
->vif_table
[vif
].dev
!= skb
->dev
) {
1834 if (rt_is_output_route(skb_rtable(skb
))) {
1835 /* It is our own packet, looped back.
1836 * Very complicated situation...
1838 * The best workaround until routing daemons will be
1839 * fixed is not to redistribute packet, if it was
1840 * send through wrong interface. It means, that
1841 * multicast applications WILL NOT work for
1842 * (S,G), which have default multicast route pointing
1843 * to wrong oif. In any case, it is not a good
1844 * idea to use multicasting applications on router.
1849 cache
->mfc_un
.res
.wrong_if
++;
1851 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
1852 /* pimsm uses asserts, when switching from RPT to SPT,
1853 * so that we cannot check that packet arrived on an oif.
1854 * It is bad, but otherwise we would need to move pretty
1855 * large chunk of pimd to kernel. Ough... --ANK
1857 (mrt
->mroute_do_pim
||
1858 cache
->mfc_un
.res
.ttls
[true_vifi
] < 255) &&
1860 cache
->mfc_un
.res
.last_assert
+ MFC_ASSERT_THRESH
)) {
1861 cache
->mfc_un
.res
.last_assert
= jiffies
;
1862 ipmr_cache_report(mrt
, skb
, true_vifi
, IGMPMSG_WRONGVIF
);
1868 mrt
->vif_table
[vif
].pkt_in
++;
1869 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
1874 if (cache
->mfc_origin
== htonl(INADDR_ANY
) &&
1875 cache
->mfc_mcastgrp
== htonl(INADDR_ANY
)) {
1876 if (true_vifi
>= 0 &&
1877 true_vifi
!= cache
->mfc_parent
&&
1879 cache
->mfc_un
.res
.ttls
[cache
->mfc_parent
]) {
1880 /* It's an (*,*) entry and the packet is not coming from
1881 * the upstream: forward the packet to the upstream
1884 psend
= cache
->mfc_parent
;
1889 for (ct
= cache
->mfc_un
.res
.maxvif
- 1;
1890 ct
>= cache
->mfc_un
.res
.minvif
; ct
--) {
1891 /* For (*,G) entry, don't forward to the incoming interface */
1892 if ((cache
->mfc_origin
!= htonl(INADDR_ANY
) ||
1894 ip_hdr(skb
)->ttl
> cache
->mfc_un
.res
.ttls
[ct
]) {
1896 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1899 ipmr_queue_xmit(net
, mrt
, skb2
, cache
,
1908 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1911 ipmr_queue_xmit(net
, mrt
, skb2
, cache
, psend
);
1913 ipmr_queue_xmit(net
, mrt
, skb
, cache
, psend
);
1923 static struct mr_table
*ipmr_rt_fib_lookup(struct net
*net
, struct sk_buff
*skb
)
1925 struct rtable
*rt
= skb_rtable(skb
);
1926 struct iphdr
*iph
= ip_hdr(skb
);
1927 struct flowi4 fl4
= {
1928 .daddr
= iph
->daddr
,
1929 .saddr
= iph
->saddr
,
1930 .flowi4_tos
= RT_TOS(iph
->tos
),
1931 .flowi4_oif
= (rt_is_output_route(rt
) ?
1932 skb
->dev
->ifindex
: 0),
1933 .flowi4_iif
= (rt_is_output_route(rt
) ?
1936 .flowi4_mark
= skb
->mark
,
1938 struct mr_table
*mrt
;
1941 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
1943 return ERR_PTR(err
);
1948 * Multicast packets for forwarding arrive here
1949 * Called with rcu_read_lock();
1952 int ip_mr_input(struct sk_buff
*skb
)
1954 struct mfc_cache
*cache
;
1955 struct net
*net
= dev_net(skb
->dev
);
1956 int local
= skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
;
1957 struct mr_table
*mrt
;
1959 /* Packet is looped back after forward, it should not be
1960 * forwarded second time, but still can be delivered locally.
1962 if (IPCB(skb
)->flags
& IPSKB_FORWARDED
)
1965 mrt
= ipmr_rt_fib_lookup(net
, skb
);
1968 return PTR_ERR(mrt
);
1971 if (IPCB(skb
)->opt
.router_alert
) {
1972 if (ip_call_ra_chain(skb
))
1974 } else if (ip_hdr(skb
)->protocol
== IPPROTO_IGMP
) {
1975 /* IGMPv1 (and broken IGMPv2 implementations sort of
1976 * Cisco IOS <= 11.2(8)) do not put router alert
1977 * option to IGMP packets destined to routable
1978 * groups. It is very bad, because it means
1979 * that we can forward NO IGMP messages.
1981 struct sock
*mroute_sk
;
1983 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1986 raw_rcv(mroute_sk
, skb
);
1992 /* already under rcu_read_lock() */
1993 cache
= ipmr_cache_find(mrt
, ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
1995 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
1998 cache
= ipmr_cache_find_any(mrt
, ip_hdr(skb
)->daddr
,
2003 * No usable cache entry
2009 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
2010 ip_local_deliver(skb
);
2016 read_lock(&mrt_lock
);
2017 vif
= ipmr_find_vif(mrt
, skb
->dev
);
2019 int err2
= ipmr_cache_unresolved(mrt
, vif
, skb
);
2020 read_unlock(&mrt_lock
);
2024 read_unlock(&mrt_lock
);
2029 read_lock(&mrt_lock
);
2030 ip_mr_forward(net
, mrt
, skb
, cache
, local
);
2031 read_unlock(&mrt_lock
);
2034 return ip_local_deliver(skb
);
2040 return ip_local_deliver(skb
);
2045 #ifdef CONFIG_IP_PIMSM
2046 /* called with rcu_read_lock() */
2047 static int __pim_rcv(struct mr_table
*mrt
, struct sk_buff
*skb
,
2048 unsigned int pimlen
)
2050 struct net_device
*reg_dev
= NULL
;
2051 struct iphdr
*encap
;
2053 encap
= (struct iphdr
*)(skb_transport_header(skb
) + pimlen
);
2056 * a. packet is really sent to a multicast group
2057 * b. packet is not a NULL-REGISTER
2058 * c. packet is not truncated
2060 if (!ipv4_is_multicast(encap
->daddr
) ||
2061 encap
->tot_len
== 0 ||
2062 ntohs(encap
->tot_len
) + pimlen
> skb
->len
)
2065 read_lock(&mrt_lock
);
2066 if (mrt
->mroute_reg_vif_num
>= 0)
2067 reg_dev
= mrt
->vif_table
[mrt
->mroute_reg_vif_num
].dev
;
2068 read_unlock(&mrt_lock
);
2073 skb
->mac_header
= skb
->network_header
;
2074 skb_pull(skb
, (u8
*)encap
- skb
->data
);
2075 skb_reset_network_header(skb
);
2076 skb
->protocol
= htons(ETH_P_IP
);
2077 skb
->ip_summed
= CHECKSUM_NONE
;
2079 skb_tunnel_rx(skb
, reg_dev
, dev_net(reg_dev
));
2083 return NET_RX_SUCCESS
;
2087 #ifdef CONFIG_IP_PIMSM_V1
2089 * Handle IGMP messages of PIMv1
2092 int pim_rcv_v1(struct sk_buff
*skb
)
2094 struct igmphdr
*pim
;
2095 struct net
*net
= dev_net(skb
->dev
);
2096 struct mr_table
*mrt
;
2098 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2101 pim
= igmp_hdr(skb
);
2103 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2106 if (!mrt
->mroute_do_pim
||
2107 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
2110 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2118 #ifdef CONFIG_IP_PIMSM_V2
2119 static int pim_rcv(struct sk_buff
*skb
)
2121 struct pimreghdr
*pim
;
2122 struct net
*net
= dev_net(skb
->dev
);
2123 struct mr_table
*mrt
;
2125 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2128 pim
= (struct pimreghdr
*)skb_transport_header(skb
);
2129 if (pim
->type
!= ((PIM_VERSION
<< 4) | (PIM_REGISTER
)) ||
2130 (pim
->flags
& PIM_NULL_REGISTER
) ||
2131 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
2132 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
2135 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2138 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2146 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2147 struct mfc_cache
*c
, struct rtmsg
*rtm
)
2150 struct rtnexthop
*nhp
;
2151 struct nlattr
*mp_attr
;
2152 struct rta_mfc_stats mfcs
;
2154 /* If cache is unresolved, don't try to parse IIF and OIF */
2155 if (c
->mfc_parent
>= MAXVIFS
)
2158 if (VIF_EXISTS(mrt
, c
->mfc_parent
) &&
2159 nla_put_u32(skb
, RTA_IIF
, mrt
->vif_table
[c
->mfc_parent
].dev
->ifindex
) < 0)
2162 if (!(mp_attr
= nla_nest_start(skb
, RTA_MULTIPATH
)))
2165 for (ct
= c
->mfc_un
.res
.minvif
; ct
< c
->mfc_un
.res
.maxvif
; ct
++) {
2166 if (VIF_EXISTS(mrt
, ct
) && c
->mfc_un
.res
.ttls
[ct
] < 255) {
2167 if (!(nhp
= nla_reserve_nohdr(skb
, sizeof(*nhp
)))) {
2168 nla_nest_cancel(skb
, mp_attr
);
2172 nhp
->rtnh_flags
= 0;
2173 nhp
->rtnh_hops
= c
->mfc_un
.res
.ttls
[ct
];
2174 nhp
->rtnh_ifindex
= mrt
->vif_table
[ct
].dev
->ifindex
;
2175 nhp
->rtnh_len
= sizeof(*nhp
);
2179 nla_nest_end(skb
, mp_attr
);
2181 mfcs
.mfcs_packets
= c
->mfc_un
.res
.pkt
;
2182 mfcs
.mfcs_bytes
= c
->mfc_un
.res
.bytes
;
2183 mfcs
.mfcs_wrong_if
= c
->mfc_un
.res
.wrong_if
;
2184 if (nla_put(skb
, RTA_MFC_STATS
, sizeof(mfcs
), &mfcs
) < 0)
2187 rtm
->rtm_type
= RTN_MULTICAST
;
2191 int ipmr_get_route(struct net
*net
, struct sk_buff
*skb
,
2192 __be32 saddr
, __be32 daddr
,
2193 struct rtmsg
*rtm
, int nowait
)
2195 struct mfc_cache
*cache
;
2196 struct mr_table
*mrt
;
2199 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2204 cache
= ipmr_cache_find(mrt
, saddr
, daddr
);
2205 if (!cache
&& skb
->dev
) {
2206 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
2209 cache
= ipmr_cache_find_any(mrt
, daddr
, vif
);
2212 struct sk_buff
*skb2
;
2214 struct net_device
*dev
;
2223 read_lock(&mrt_lock
);
2225 vif
= ipmr_find_vif(mrt
, dev
);
2227 read_unlock(&mrt_lock
);
2231 skb2
= skb_clone(skb
, GFP_ATOMIC
);
2233 read_unlock(&mrt_lock
);
2238 skb_push(skb2
, sizeof(struct iphdr
));
2239 skb_reset_network_header(skb2
);
2241 iph
->ihl
= sizeof(struct iphdr
) >> 2;
2245 err
= ipmr_cache_unresolved(mrt
, vif
, skb2
);
2246 read_unlock(&mrt_lock
);
2251 read_lock(&mrt_lock
);
2252 if (!nowait
&& (rtm
->rtm_flags
& RTM_F_NOTIFY
))
2253 cache
->mfc_flags
|= MFC_NOTIFY
;
2254 err
= __ipmr_fill_mroute(mrt
, skb
, cache
, rtm
);
2255 read_unlock(&mrt_lock
);
2260 static int ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2261 u32 portid
, u32 seq
, struct mfc_cache
*c
, int cmd
,
2264 struct nlmsghdr
*nlh
;
2268 nlh
= nlmsg_put(skb
, portid
, seq
, cmd
, sizeof(*rtm
), flags
);
2272 rtm
= nlmsg_data(nlh
);
2273 rtm
->rtm_family
= RTNL_FAMILY_IPMR
;
2274 rtm
->rtm_dst_len
= 32;
2275 rtm
->rtm_src_len
= 32;
2277 rtm
->rtm_table
= mrt
->id
;
2278 if (nla_put_u32(skb
, RTA_TABLE
, mrt
->id
))
2279 goto nla_put_failure
;
2280 rtm
->rtm_type
= RTN_MULTICAST
;
2281 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2282 if (c
->mfc_flags
& MFC_STATIC
)
2283 rtm
->rtm_protocol
= RTPROT_STATIC
;
2285 rtm
->rtm_protocol
= RTPROT_MROUTED
;
2288 if (nla_put_in_addr(skb
, RTA_SRC
, c
->mfc_origin
) ||
2289 nla_put_in_addr(skb
, RTA_DST
, c
->mfc_mcastgrp
))
2290 goto nla_put_failure
;
2291 err
= __ipmr_fill_mroute(mrt
, skb
, c
, rtm
);
2292 /* do not break the dump if cache is unresolved */
2293 if (err
< 0 && err
!= -ENOENT
)
2294 goto nla_put_failure
;
2296 nlmsg_end(skb
, nlh
);
2300 nlmsg_cancel(skb
, nlh
);
2304 static size_t mroute_msgsize(bool unresolved
, int maxvif
)
2307 NLMSG_ALIGN(sizeof(struct rtmsg
))
2308 + nla_total_size(4) /* RTA_TABLE */
2309 + nla_total_size(4) /* RTA_SRC */
2310 + nla_total_size(4) /* RTA_DST */
2315 + nla_total_size(4) /* RTA_IIF */
2316 + nla_total_size(0) /* RTA_MULTIPATH */
2317 + maxvif
* NLA_ALIGN(sizeof(struct rtnexthop
))
2319 + nla_total_size(sizeof(struct rta_mfc_stats
))
2325 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
2328 struct net
*net
= read_pnet(&mrt
->net
);
2329 struct sk_buff
*skb
;
2332 skb
= nlmsg_new(mroute_msgsize(mfc
->mfc_parent
>= MAXVIFS
, mrt
->maxvif
),
2337 err
= ipmr_fill_mroute(mrt
, skb
, 0, 0, mfc
, cmd
, 0);
2341 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_MROUTE
, NULL
, GFP_ATOMIC
);
2347 rtnl_set_sk_err(net
, RTNLGRP_IPV4_MROUTE
, err
);
2350 static int ipmr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2352 struct net
*net
= sock_net(skb
->sk
);
2353 struct mr_table
*mrt
;
2354 struct mfc_cache
*mfc
;
2355 unsigned int t
= 0, s_t
;
2356 unsigned int h
= 0, s_h
;
2357 unsigned int e
= 0, s_e
;
2364 ipmr_for_each_table(mrt
, net
) {
2369 for (h
= s_h
; h
< MFC_LINES
; h
++) {
2370 list_for_each_entry_rcu(mfc
, &mrt
->mfc_cache_array
[h
], list
) {
2373 if (ipmr_fill_mroute(mrt
, skb
,
2374 NETLINK_CB(cb
->skb
).portid
,
2384 spin_lock_bh(&mfc_unres_lock
);
2385 list_for_each_entry(mfc
, &mrt
->mfc_unres_queue
, list
) {
2388 if (ipmr_fill_mroute(mrt
, skb
,
2389 NETLINK_CB(cb
->skb
).portid
,
2393 spin_unlock_bh(&mfc_unres_lock
);
2399 spin_unlock_bh(&mfc_unres_lock
);
2415 #ifdef CONFIG_PROC_FS
2417 * The /proc interfaces to multicast routing :
2418 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2420 struct ipmr_vif_iter
{
2421 struct seq_net_private p
;
2422 struct mr_table
*mrt
;
2426 static struct vif_device
*ipmr_vif_seq_idx(struct net
*net
,
2427 struct ipmr_vif_iter
*iter
,
2430 struct mr_table
*mrt
= iter
->mrt
;
2432 for (iter
->ct
= 0; iter
->ct
< mrt
->maxvif
; ++iter
->ct
) {
2433 if (!VIF_EXISTS(mrt
, iter
->ct
))
2436 return &mrt
->vif_table
[iter
->ct
];
2441 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2442 __acquires(mrt_lock
)
2444 struct ipmr_vif_iter
*iter
= seq
->private;
2445 struct net
*net
= seq_file_net(seq
);
2446 struct mr_table
*mrt
;
2448 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2450 return ERR_PTR(-ENOENT
);
2454 read_lock(&mrt_lock
);
2455 return *pos
? ipmr_vif_seq_idx(net
, seq
->private, *pos
- 1)
2459 static void *ipmr_vif_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2461 struct ipmr_vif_iter
*iter
= seq
->private;
2462 struct net
*net
= seq_file_net(seq
);
2463 struct mr_table
*mrt
= iter
->mrt
;
2466 if (v
== SEQ_START_TOKEN
)
2467 return ipmr_vif_seq_idx(net
, iter
, 0);
2469 while (++iter
->ct
< mrt
->maxvif
) {
2470 if (!VIF_EXISTS(mrt
, iter
->ct
))
2472 return &mrt
->vif_table
[iter
->ct
];
2477 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
2478 __releases(mrt_lock
)
2480 read_unlock(&mrt_lock
);
2483 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
2485 struct ipmr_vif_iter
*iter
= seq
->private;
2486 struct mr_table
*mrt
= iter
->mrt
;
2488 if (v
== SEQ_START_TOKEN
) {
2490 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2492 const struct vif_device
*vif
= v
;
2493 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
2496 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2497 vif
- mrt
->vif_table
,
2498 name
, vif
->bytes_in
, vif
->pkt_in
,
2499 vif
->bytes_out
, vif
->pkt_out
,
2500 vif
->flags
, vif
->local
, vif
->remote
);
2505 static const struct seq_operations ipmr_vif_seq_ops
= {
2506 .start
= ipmr_vif_seq_start
,
2507 .next
= ipmr_vif_seq_next
,
2508 .stop
= ipmr_vif_seq_stop
,
2509 .show
= ipmr_vif_seq_show
,
2512 static int ipmr_vif_open(struct inode
*inode
, struct file
*file
)
2514 return seq_open_net(inode
, file
, &ipmr_vif_seq_ops
,
2515 sizeof(struct ipmr_vif_iter
));
2518 static const struct file_operations ipmr_vif_fops
= {
2519 .owner
= THIS_MODULE
,
2520 .open
= ipmr_vif_open
,
2522 .llseek
= seq_lseek
,
2523 .release
= seq_release_net
,
2526 struct ipmr_mfc_iter
{
2527 struct seq_net_private p
;
2528 struct mr_table
*mrt
;
2529 struct list_head
*cache
;
2534 static struct mfc_cache
*ipmr_mfc_seq_idx(struct net
*net
,
2535 struct ipmr_mfc_iter
*it
, loff_t pos
)
2537 struct mr_table
*mrt
= it
->mrt
;
2538 struct mfc_cache
*mfc
;
2541 for (it
->ct
= 0; it
->ct
< MFC_LINES
; it
->ct
++) {
2542 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2543 list_for_each_entry_rcu(mfc
, it
->cache
, list
)
2549 spin_lock_bh(&mfc_unres_lock
);
2550 it
->cache
= &mrt
->mfc_unres_queue
;
2551 list_for_each_entry(mfc
, it
->cache
, list
)
2554 spin_unlock_bh(&mfc_unres_lock
);
2561 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2563 struct ipmr_mfc_iter
*it
= seq
->private;
2564 struct net
*net
= seq_file_net(seq
);
2565 struct mr_table
*mrt
;
2567 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2569 return ERR_PTR(-ENOENT
);
2574 return *pos
? ipmr_mfc_seq_idx(net
, seq
->private, *pos
- 1)
2578 static void *ipmr_mfc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2580 struct mfc_cache
*mfc
= v
;
2581 struct ipmr_mfc_iter
*it
= seq
->private;
2582 struct net
*net
= seq_file_net(seq
);
2583 struct mr_table
*mrt
= it
->mrt
;
2587 if (v
== SEQ_START_TOKEN
)
2588 return ipmr_mfc_seq_idx(net
, seq
->private, 0);
2590 if (mfc
->list
.next
!= it
->cache
)
2591 return list_entry(mfc
->list
.next
, struct mfc_cache
, list
);
2593 if (it
->cache
== &mrt
->mfc_unres_queue
)
2596 BUG_ON(it
->cache
!= &mrt
->mfc_cache_array
[it
->ct
]);
2598 while (++it
->ct
< MFC_LINES
) {
2599 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2600 if (list_empty(it
->cache
))
2602 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2605 /* exhausted cache_array, show unresolved */
2607 it
->cache
= &mrt
->mfc_unres_queue
;
2610 spin_lock_bh(&mfc_unres_lock
);
2611 if (!list_empty(it
->cache
))
2612 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2615 spin_unlock_bh(&mfc_unres_lock
);
2621 static void ipmr_mfc_seq_stop(struct seq_file
*seq
, void *v
)
2623 struct ipmr_mfc_iter
*it
= seq
->private;
2624 struct mr_table
*mrt
= it
->mrt
;
2626 if (it
->cache
== &mrt
->mfc_unres_queue
)
2627 spin_unlock_bh(&mfc_unres_lock
);
2628 else if (it
->cache
== &mrt
->mfc_cache_array
[it
->ct
])
2632 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
2636 if (v
== SEQ_START_TOKEN
) {
2638 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2640 const struct mfc_cache
*mfc
= v
;
2641 const struct ipmr_mfc_iter
*it
= seq
->private;
2642 const struct mr_table
*mrt
= it
->mrt
;
2644 seq_printf(seq
, "%08X %08X %-3hd",
2645 (__force u32
) mfc
->mfc_mcastgrp
,
2646 (__force u32
) mfc
->mfc_origin
,
2649 if (it
->cache
!= &mrt
->mfc_unres_queue
) {
2650 seq_printf(seq
, " %8lu %8lu %8lu",
2651 mfc
->mfc_un
.res
.pkt
,
2652 mfc
->mfc_un
.res
.bytes
,
2653 mfc
->mfc_un
.res
.wrong_if
);
2654 for (n
= mfc
->mfc_un
.res
.minvif
;
2655 n
< mfc
->mfc_un
.res
.maxvif
; n
++) {
2656 if (VIF_EXISTS(mrt
, n
) &&
2657 mfc
->mfc_un
.res
.ttls
[n
] < 255)
2660 n
, mfc
->mfc_un
.res
.ttls
[n
]);
2663 /* unresolved mfc_caches don't contain
2664 * pkt, bytes and wrong_if values
2666 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2668 seq_putc(seq
, '\n');
2673 static const struct seq_operations ipmr_mfc_seq_ops
= {
2674 .start
= ipmr_mfc_seq_start
,
2675 .next
= ipmr_mfc_seq_next
,
2676 .stop
= ipmr_mfc_seq_stop
,
2677 .show
= ipmr_mfc_seq_show
,
2680 static int ipmr_mfc_open(struct inode
*inode
, struct file
*file
)
2682 return seq_open_net(inode
, file
, &ipmr_mfc_seq_ops
,
2683 sizeof(struct ipmr_mfc_iter
));
2686 static const struct file_operations ipmr_mfc_fops
= {
2687 .owner
= THIS_MODULE
,
2688 .open
= ipmr_mfc_open
,
2690 .llseek
= seq_lseek
,
2691 .release
= seq_release_net
,
2695 #ifdef CONFIG_IP_PIMSM_V2
2696 static const struct net_protocol pim_protocol
= {
2704 * Setup for IP multicast routing
2706 static int __net_init
ipmr_net_init(struct net
*net
)
2710 err
= ipmr_rules_init(net
);
2714 #ifdef CONFIG_PROC_FS
2716 if (!proc_create("ip_mr_vif", 0, net
->proc_net
, &ipmr_vif_fops
))
2718 if (!proc_create("ip_mr_cache", 0, net
->proc_net
, &ipmr_mfc_fops
))
2719 goto proc_cache_fail
;
2723 #ifdef CONFIG_PROC_FS
2725 remove_proc_entry("ip_mr_vif", net
->proc_net
);
2727 ipmr_rules_exit(net
);
2733 static void __net_exit
ipmr_net_exit(struct net
*net
)
2735 #ifdef CONFIG_PROC_FS
2736 remove_proc_entry("ip_mr_cache", net
->proc_net
);
2737 remove_proc_entry("ip_mr_vif", net
->proc_net
);
2739 ipmr_rules_exit(net
);
2742 static struct pernet_operations ipmr_net_ops
= {
2743 .init
= ipmr_net_init
,
2744 .exit
= ipmr_net_exit
,
2747 int __init
ip_mr_init(void)
2751 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
2752 sizeof(struct mfc_cache
),
2753 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
2758 err
= register_pernet_subsys(&ipmr_net_ops
);
2760 goto reg_pernet_fail
;
2762 err
= register_netdevice_notifier(&ip_mr_notifier
);
2764 goto reg_notif_fail
;
2765 #ifdef CONFIG_IP_PIMSM_V2
2766 if (inet_add_protocol(&pim_protocol
, IPPROTO_PIM
) < 0) {
2767 pr_err("%s: can't add PIM protocol\n", __func__
);
2769 goto add_proto_fail
;
2772 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETROUTE
,
2773 NULL
, ipmr_rtm_dumproute
, NULL
);
2776 #ifdef CONFIG_IP_PIMSM_V2
2778 unregister_netdevice_notifier(&ip_mr_notifier
);
2781 unregister_pernet_subsys(&ipmr_net_ops
);
2783 kmem_cache_destroy(mrt_cachep
);