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>
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 int 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
)
160 struct ipmr_result res
;
161 struct fib_lookup_arg arg
= { .result
= &res
, };
164 err
= fib_rules_lookup(net
->ipv4
.mr_rules_ops
,
165 flowi4_to_flowi(flp4
), 0, &arg
);
172 static int ipmr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
173 int flags
, struct fib_lookup_arg
*arg
)
175 struct ipmr_result
*res
= arg
->result
;
176 struct mr_table
*mrt
;
178 switch (rule
->action
) {
181 case FR_ACT_UNREACHABLE
:
183 case FR_ACT_PROHIBIT
:
185 case FR_ACT_BLACKHOLE
:
190 mrt
= ipmr_get_table(rule
->fr_net
, rule
->table
);
197 static int ipmr_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
202 static const struct nla_policy ipmr_rule_policy
[FRA_MAX
+ 1] = {
206 static int ipmr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
207 struct fib_rule_hdr
*frh
, struct nlattr
**tb
)
212 static int ipmr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
218 static int ipmr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
219 struct fib_rule_hdr
*frh
)
227 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template
= {
228 .family
= RTNL_FAMILY_IPMR
,
229 .rule_size
= sizeof(struct ipmr_rule
),
230 .addr_size
= sizeof(u32
),
231 .action
= ipmr_rule_action
,
232 .match
= ipmr_rule_match
,
233 .configure
= ipmr_rule_configure
,
234 .compare
= ipmr_rule_compare
,
235 .default_pref
= fib_default_rule_pref
,
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
;
270 fib_rules_unregister(ops
);
274 static void __net_exit
ipmr_rules_exit(struct net
*net
)
276 struct mr_table
*mrt
, *next
;
278 list_for_each_entry_safe(mrt
, next
, &net
->ipv4
.mr_tables
, list
) {
279 list_del(&mrt
->list
);
280 ipmr_free_table(mrt
);
282 fib_rules_unregister(net
->ipv4
.mr_rules_ops
);
285 #define ipmr_for_each_table(mrt, net) \
286 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
290 return net
->ipv4
.mrt
;
293 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
294 struct mr_table
**mrt
)
296 *mrt
= net
->ipv4
.mrt
;
300 static int __net_init
ipmr_rules_init(struct net
*net
)
302 net
->ipv4
.mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
303 return net
->ipv4
.mrt
? 0 : -ENOMEM
;
306 static void __net_exit
ipmr_rules_exit(struct net
*net
)
308 ipmr_free_table(net
->ipv4
.mrt
);
312 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
)
314 struct mr_table
*mrt
;
317 mrt
= ipmr_get_table(net
, id
);
321 mrt
= kzalloc(sizeof(*mrt
), GFP_KERNEL
);
324 write_pnet(&mrt
->net
, net
);
327 /* Forwarding cache */
328 for (i
= 0; i
< MFC_LINES
; i
++)
329 INIT_LIST_HEAD(&mrt
->mfc_cache_array
[i
]);
331 INIT_LIST_HEAD(&mrt
->mfc_unres_queue
);
333 setup_timer(&mrt
->ipmr_expire_timer
, ipmr_expire_process
,
336 #ifdef CONFIG_IP_PIMSM
337 mrt
->mroute_reg_vif_num
= -1;
339 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
340 list_add_tail_rcu(&mrt
->list
, &net
->ipv4
.mr_tables
);
345 static void ipmr_free_table(struct mr_table
*mrt
)
347 del_timer_sync(&mrt
->ipmr_expire_timer
);
348 mroute_clean_tables(mrt
);
352 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
354 static void ipmr_del_tunnel(struct net_device
*dev
, struct vifctl
*v
)
356 struct net
*net
= dev_net(dev
);
360 dev
= __dev_get_by_name(net
, "tunl0");
362 const struct net_device_ops
*ops
= dev
->netdev_ops
;
364 struct ip_tunnel_parm p
;
366 memset(&p
, 0, sizeof(p
));
367 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
368 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
371 p
.iph
.protocol
= IPPROTO_IPIP
;
372 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
373 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
375 if (ops
->ndo_do_ioctl
) {
376 mm_segment_t oldfs
= get_fs();
379 ops
->ndo_do_ioctl(dev
, &ifr
, SIOCDELTUNNEL
);
386 struct net_device
*ipmr_new_tunnel(struct net
*net
, struct vifctl
*v
)
388 struct net_device
*dev
;
390 dev
= __dev_get_by_name(net
, "tunl0");
393 const struct net_device_ops
*ops
= dev
->netdev_ops
;
396 struct ip_tunnel_parm p
;
397 struct in_device
*in_dev
;
399 memset(&p
, 0, sizeof(p
));
400 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
401 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
404 p
.iph
.protocol
= IPPROTO_IPIP
;
405 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
406 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
408 if (ops
->ndo_do_ioctl
) {
409 mm_segment_t oldfs
= get_fs();
412 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
420 (dev
= __dev_get_by_name(net
, p
.name
)) != NULL
) {
421 dev
->flags
|= IFF_MULTICAST
;
423 in_dev
= __in_dev_get_rtnl(dev
);
427 ipv4_devconf_setall(in_dev
);
428 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
438 /* allow the register to be completed before unregistering. */
442 unregister_netdevice(dev
);
446 #ifdef CONFIG_IP_PIMSM
448 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
450 struct net
*net
= dev_net(dev
);
451 struct mr_table
*mrt
;
452 struct flowi4 fl4
= {
453 .flowi4_oif
= dev
->ifindex
,
454 .flowi4_iif
= skb
->skb_iif
,
455 .flowi4_mark
= skb
->mark
,
459 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
465 read_lock(&mrt_lock
);
466 dev
->stats
.tx_bytes
+= skb
->len
;
467 dev
->stats
.tx_packets
++;
468 ipmr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, IGMPMSG_WHOLEPKT
);
469 read_unlock(&mrt_lock
);
474 static const struct net_device_ops reg_vif_netdev_ops
= {
475 .ndo_start_xmit
= reg_vif_xmit
,
478 static void reg_vif_setup(struct net_device
*dev
)
480 dev
->type
= ARPHRD_PIMREG
;
481 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
482 dev
->flags
= IFF_NOARP
;
483 dev
->netdev_ops
= ®_vif_netdev_ops
,
484 dev
->destructor
= free_netdev
;
485 dev
->features
|= NETIF_F_NETNS_LOCAL
;
488 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
490 struct net_device
*dev
;
491 struct in_device
*in_dev
;
494 if (mrt
->id
== RT_TABLE_DEFAULT
)
495 sprintf(name
, "pimreg");
497 sprintf(name
, "pimreg%u", mrt
->id
);
499 dev
= alloc_netdev(0, name
, reg_vif_setup
);
504 dev_net_set(dev
, net
);
506 if (register_netdevice(dev
)) {
513 in_dev
= __in_dev_get_rcu(dev
);
519 ipv4_devconf_setall(in_dev
);
520 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
531 /* allow the register to be completed before unregistering. */
535 unregister_netdevice(dev
);
541 * vif_delete - Delete a VIF entry
542 * @notify: Set to 1, if the caller is a notifier_call
545 static int vif_delete(struct mr_table
*mrt
, int vifi
, int notify
,
546 struct list_head
*head
)
548 struct vif_device
*v
;
549 struct net_device
*dev
;
550 struct in_device
*in_dev
;
552 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
553 return -EADDRNOTAVAIL
;
555 v
= &mrt
->vif_table
[vifi
];
557 write_lock_bh(&mrt_lock
);
562 write_unlock_bh(&mrt_lock
);
563 return -EADDRNOTAVAIL
;
566 #ifdef CONFIG_IP_PIMSM
567 if (vifi
== mrt
->mroute_reg_vif_num
)
568 mrt
->mroute_reg_vif_num
= -1;
571 if (vifi
+ 1 == mrt
->maxvif
) {
574 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
575 if (VIF_EXISTS(mrt
, tmp
))
581 write_unlock_bh(&mrt_lock
);
583 dev_set_allmulti(dev
, -1);
585 in_dev
= __in_dev_get_rtnl(dev
);
587 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)--;
588 inet_netconf_notify_devconf(dev_net(dev
),
589 NETCONFA_MC_FORWARDING
,
590 dev
->ifindex
, &in_dev
->cnf
);
591 ip_rt_multicast_event(in_dev
);
594 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
) && !notify
)
595 unregister_netdevice_queue(dev
, head
);
601 static void ipmr_cache_free_rcu(struct rcu_head
*head
)
603 struct mfc_cache
*c
= container_of(head
, struct mfc_cache
, rcu
);
605 kmem_cache_free(mrt_cachep
, c
);
608 static inline void ipmr_cache_free(struct mfc_cache
*c
)
610 call_rcu(&c
->rcu
, ipmr_cache_free_rcu
);
613 /* Destroy an unresolved cache entry, killing queued skbs
614 * and reporting error to netlink readers.
617 static void ipmr_destroy_unres(struct mr_table
*mrt
, struct mfc_cache
*c
)
619 struct net
*net
= read_pnet(&mrt
->net
);
623 atomic_dec(&mrt
->cache_resolve_queue_len
);
625 while ((skb
= skb_dequeue(&c
->mfc_un
.unres
.unresolved
))) {
626 if (ip_hdr(skb
)->version
== 0) {
627 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
628 nlh
->nlmsg_type
= NLMSG_ERROR
;
629 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nlmsgerr
));
630 skb_trim(skb
, nlh
->nlmsg_len
);
632 e
->error
= -ETIMEDOUT
;
633 memset(&e
->msg
, 0, sizeof(e
->msg
));
635 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
645 /* Timer process for the unresolved queue. */
647 static void ipmr_expire_process(unsigned long arg
)
649 struct mr_table
*mrt
= (struct mr_table
*)arg
;
651 unsigned long expires
;
652 struct mfc_cache
*c
, *next
;
654 if (!spin_trylock(&mfc_unres_lock
)) {
655 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+HZ
/10);
659 if (list_empty(&mrt
->mfc_unres_queue
))
665 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
666 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
667 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
668 if (interval
< expires
)
674 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
675 ipmr_destroy_unres(mrt
, c
);
678 if (!list_empty(&mrt
->mfc_unres_queue
))
679 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
682 spin_unlock(&mfc_unres_lock
);
685 /* Fill oifs list. It is called under write locked mrt_lock. */
687 static void ipmr_update_thresholds(struct mr_table
*mrt
, struct mfc_cache
*cache
,
692 cache
->mfc_un
.res
.minvif
= MAXVIFS
;
693 cache
->mfc_un
.res
.maxvif
= 0;
694 memset(cache
->mfc_un
.res
.ttls
, 255, MAXVIFS
);
696 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
697 if (VIF_EXISTS(mrt
, vifi
) &&
698 ttls
[vifi
] && ttls
[vifi
] < 255) {
699 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
700 if (cache
->mfc_un
.res
.minvif
> vifi
)
701 cache
->mfc_un
.res
.minvif
= vifi
;
702 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
703 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
708 static int vif_add(struct net
*net
, struct mr_table
*mrt
,
709 struct vifctl
*vifc
, int mrtsock
)
711 int vifi
= vifc
->vifc_vifi
;
712 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
713 struct net_device
*dev
;
714 struct in_device
*in_dev
;
718 if (VIF_EXISTS(mrt
, vifi
))
721 switch (vifc
->vifc_flags
) {
722 #ifdef CONFIG_IP_PIMSM
725 * Special Purpose VIF in PIM
726 * All the packets will be sent to the daemon
728 if (mrt
->mroute_reg_vif_num
>= 0)
730 dev
= ipmr_reg_vif(net
, mrt
);
733 err
= dev_set_allmulti(dev
, 1);
735 unregister_netdevice(dev
);
742 dev
= ipmr_new_tunnel(net
, vifc
);
745 err
= dev_set_allmulti(dev
, 1);
747 ipmr_del_tunnel(dev
, vifc
);
753 case VIFF_USE_IFINDEX
:
755 if (vifc
->vifc_flags
== VIFF_USE_IFINDEX
) {
756 dev
= dev_get_by_index(net
, vifc
->vifc_lcl_ifindex
);
757 if (dev
&& __in_dev_get_rtnl(dev
) == NULL
) {
759 return -EADDRNOTAVAIL
;
762 dev
= ip_dev_find(net
, vifc
->vifc_lcl_addr
.s_addr
);
765 return -EADDRNOTAVAIL
;
766 err
= dev_set_allmulti(dev
, 1);
776 in_dev
= __in_dev_get_rtnl(dev
);
779 return -EADDRNOTAVAIL
;
781 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)++;
782 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
, dev
->ifindex
,
784 ip_rt_multicast_event(in_dev
);
786 /* Fill in the VIF structures */
788 v
->rate_limit
= vifc
->vifc_rate_limit
;
789 v
->local
= vifc
->vifc_lcl_addr
.s_addr
;
790 v
->remote
= vifc
->vifc_rmt_addr
.s_addr
;
791 v
->flags
= vifc
->vifc_flags
;
793 v
->flags
|= VIFF_STATIC
;
794 v
->threshold
= vifc
->vifc_threshold
;
799 v
->link
= dev
->ifindex
;
800 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
))
801 v
->link
= dev
->iflink
;
803 /* And finish update writing critical data */
804 write_lock_bh(&mrt_lock
);
806 #ifdef CONFIG_IP_PIMSM
807 if (v
->flags
& VIFF_REGISTER
)
808 mrt
->mroute_reg_vif_num
= vifi
;
810 if (vifi
+1 > mrt
->maxvif
)
811 mrt
->maxvif
= vifi
+1;
812 write_unlock_bh(&mrt_lock
);
816 /* called with rcu_read_lock() */
817 static struct mfc_cache
*ipmr_cache_find(struct mr_table
*mrt
,
821 int line
= MFC_HASH(mcastgrp
, origin
);
824 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
) {
825 if (c
->mfc_origin
== origin
&& c
->mfc_mcastgrp
== mcastgrp
)
832 * Allocate a multicast cache entry
834 static struct mfc_cache
*ipmr_cache_alloc(void)
836 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
839 c
->mfc_un
.res
.minvif
= MAXVIFS
;
843 static struct mfc_cache
*ipmr_cache_alloc_unres(void)
845 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
848 skb_queue_head_init(&c
->mfc_un
.unres
.unresolved
);
849 c
->mfc_un
.unres
.expires
= jiffies
+ 10*HZ
;
855 * A cache entry has gone into a resolved state from queued
858 static void ipmr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
859 struct mfc_cache
*uc
, struct mfc_cache
*c
)
864 /* Play the pending entries through our router */
866 while ((skb
= __skb_dequeue(&uc
->mfc_un
.unres
.unresolved
))) {
867 if (ip_hdr(skb
)->version
== 0) {
868 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
870 if (__ipmr_fill_mroute(mrt
, skb
, c
, NLMSG_DATA(nlh
)) > 0) {
871 nlh
->nlmsg_len
= skb_tail_pointer(skb
) -
874 nlh
->nlmsg_type
= NLMSG_ERROR
;
875 nlh
->nlmsg_len
= NLMSG_LENGTH(sizeof(struct nlmsgerr
));
876 skb_trim(skb
, nlh
->nlmsg_len
);
878 e
->error
= -EMSGSIZE
;
879 memset(&e
->msg
, 0, sizeof(e
->msg
));
882 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
884 ip_mr_forward(net
, mrt
, skb
, c
, 0);
890 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
891 * expects the following bizarre scheme.
893 * Called under mrt_lock.
896 static int ipmr_cache_report(struct mr_table
*mrt
,
897 struct sk_buff
*pkt
, vifi_t vifi
, int assert)
900 const int ihl
= ip_hdrlen(pkt
);
901 struct igmphdr
*igmp
;
903 struct sock
*mroute_sk
;
906 #ifdef CONFIG_IP_PIMSM
907 if (assert == IGMPMSG_WHOLEPKT
)
908 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
911 skb
= alloc_skb(128, GFP_ATOMIC
);
916 #ifdef CONFIG_IP_PIMSM
917 if (assert == IGMPMSG_WHOLEPKT
) {
918 /* Ugly, but we have no choice with this interface.
919 * Duplicate old header, fix ihl, length etc.
920 * And all this only to mangle msg->im_msgtype and
921 * to set msg->im_mbz to "mbz" :-)
923 skb_push(skb
, sizeof(struct iphdr
));
924 skb_reset_network_header(skb
);
925 skb_reset_transport_header(skb
);
926 msg
= (struct igmpmsg
*)skb_network_header(skb
);
927 memcpy(msg
, skb_network_header(pkt
), sizeof(struct iphdr
));
928 msg
->im_msgtype
= IGMPMSG_WHOLEPKT
;
930 msg
->im_vif
= mrt
->mroute_reg_vif_num
;
931 ip_hdr(skb
)->ihl
= sizeof(struct iphdr
) >> 2;
932 ip_hdr(skb
)->tot_len
= htons(ntohs(ip_hdr(pkt
)->tot_len
) +
933 sizeof(struct iphdr
));
938 /* Copy the IP header */
940 skb
->network_header
= skb
->tail
;
942 skb_copy_to_linear_data(skb
, pkt
->data
, ihl
);
943 ip_hdr(skb
)->protocol
= 0; /* Flag to the kernel this is a route add */
944 msg
= (struct igmpmsg
*)skb_network_header(skb
);
946 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
950 igmp
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
952 msg
->im_msgtype
= assert;
954 ip_hdr(skb
)->tot_len
= htons(skb
->len
); /* Fix the length */
955 skb
->transport_header
= skb
->network_header
;
959 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
960 if (mroute_sk
== NULL
) {
966 /* Deliver to mrouted */
968 ret
= sock_queue_rcv_skb(mroute_sk
, skb
);
971 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
979 * Queue a packet for resolution. It gets locked cache entry!
983 ipmr_cache_unresolved(struct mr_table
*mrt
, vifi_t vifi
, struct sk_buff
*skb
)
988 const struct iphdr
*iph
= ip_hdr(skb
);
990 spin_lock_bh(&mfc_unres_lock
);
991 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, list
) {
992 if (c
->mfc_mcastgrp
== iph
->daddr
&&
993 c
->mfc_origin
== iph
->saddr
) {
1000 /* Create a new entry if allowable */
1002 if (atomic_read(&mrt
->cache_resolve_queue_len
) >= 10 ||
1003 (c
= ipmr_cache_alloc_unres()) == NULL
) {
1004 spin_unlock_bh(&mfc_unres_lock
);
1010 /* Fill in the new cache entry */
1013 c
->mfc_origin
= iph
->saddr
;
1014 c
->mfc_mcastgrp
= iph
->daddr
;
1016 /* Reflect first query at mrouted. */
1018 err
= ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_NOCACHE
);
1020 /* If the report failed throw the cache entry
1023 spin_unlock_bh(&mfc_unres_lock
);
1030 atomic_inc(&mrt
->cache_resolve_queue_len
);
1031 list_add(&c
->list
, &mrt
->mfc_unres_queue
);
1032 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1034 if (atomic_read(&mrt
->cache_resolve_queue_len
) == 1)
1035 mod_timer(&mrt
->ipmr_expire_timer
, c
->mfc_un
.unres
.expires
);
1038 /* See if we can append the packet */
1040 if (c
->mfc_un
.unres
.unresolved
.qlen
> 3) {
1044 skb_queue_tail(&c
->mfc_un
.unres
.unresolved
, skb
);
1048 spin_unlock_bh(&mfc_unres_lock
);
1053 * MFC cache manipulation by user space mroute daemon
1056 static int ipmr_mfc_delete(struct mr_table
*mrt
, struct mfcctl
*mfc
)
1059 struct mfc_cache
*c
, *next
;
1061 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1063 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[line
], list
) {
1064 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1065 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
) {
1066 list_del_rcu(&c
->list
);
1067 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1075 static int ipmr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1076 struct mfcctl
*mfc
, int mrtsock
)
1080 struct mfc_cache
*uc
, *c
;
1082 if (mfc
->mfcc_parent
>= MAXVIFS
)
1085 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1087 list_for_each_entry(c
, &mrt
->mfc_cache_array
[line
], list
) {
1088 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1089 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
) {
1096 write_lock_bh(&mrt_lock
);
1097 c
->mfc_parent
= mfc
->mfcc_parent
;
1098 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1100 c
->mfc_flags
|= MFC_STATIC
;
1101 write_unlock_bh(&mrt_lock
);
1102 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1106 if (!ipv4_is_multicast(mfc
->mfcc_mcastgrp
.s_addr
))
1109 c
= ipmr_cache_alloc();
1113 c
->mfc_origin
= mfc
->mfcc_origin
.s_addr
;
1114 c
->mfc_mcastgrp
= mfc
->mfcc_mcastgrp
.s_addr
;
1115 c
->mfc_parent
= mfc
->mfcc_parent
;
1116 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1118 c
->mfc_flags
|= MFC_STATIC
;
1120 list_add_rcu(&c
->list
, &mrt
->mfc_cache_array
[line
]);
1123 * Check to see if we resolved a queued list. If so we
1124 * need to send on the frames and tidy up.
1127 spin_lock_bh(&mfc_unres_lock
);
1128 list_for_each_entry(uc
, &mrt
->mfc_unres_queue
, list
) {
1129 if (uc
->mfc_origin
== c
->mfc_origin
&&
1130 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
1131 list_del(&uc
->list
);
1132 atomic_dec(&mrt
->cache_resolve_queue_len
);
1137 if (list_empty(&mrt
->mfc_unres_queue
))
1138 del_timer(&mrt
->ipmr_expire_timer
);
1139 spin_unlock_bh(&mfc_unres_lock
);
1142 ipmr_cache_resolve(net
, mrt
, uc
, c
);
1143 ipmr_cache_free(uc
);
1145 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1150 * Close the multicast socket, and clear the vif tables etc
1153 static void mroute_clean_tables(struct mr_table
*mrt
)
1157 struct mfc_cache
*c
, *next
;
1159 /* Shut down all active vif entries */
1161 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1162 if (!(mrt
->vif_table
[i
].flags
& VIFF_STATIC
))
1163 vif_delete(mrt
, i
, 0, &list
);
1165 unregister_netdevice_many(&list
);
1167 /* Wipe the cache */
1169 for (i
= 0; i
< MFC_LINES
; i
++) {
1170 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[i
], list
) {
1171 if (c
->mfc_flags
& MFC_STATIC
)
1173 list_del_rcu(&c
->list
);
1174 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1179 if (atomic_read(&mrt
->cache_resolve_queue_len
) != 0) {
1180 spin_lock_bh(&mfc_unres_lock
);
1181 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
1183 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1184 ipmr_destroy_unres(mrt
, c
);
1186 spin_unlock_bh(&mfc_unres_lock
);
1190 /* called from ip_ra_control(), before an RCU grace period,
1191 * we dont need to call synchronize_rcu() here
1193 static void mrtsock_destruct(struct sock
*sk
)
1195 struct net
*net
= sock_net(sk
);
1196 struct mr_table
*mrt
;
1199 ipmr_for_each_table(mrt
, net
) {
1200 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1201 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)--;
1202 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
,
1203 NETCONFA_IFINDEX_ALL
,
1204 net
->ipv4
.devconf_all
);
1205 RCU_INIT_POINTER(mrt
->mroute_sk
, NULL
);
1206 mroute_clean_tables(mrt
);
1213 * Socket options and virtual interface manipulation. The whole
1214 * virtual interface system is a complete heap, but unfortunately
1215 * that's how BSD mrouted happens to think. Maybe one day with a proper
1216 * MOSPF/PIM router set up we can clean this up.
1219 int ip_mroute_setsockopt(struct sock
*sk
, int optname
, char __user
*optval
, unsigned int optlen
)
1224 struct net
*net
= sock_net(sk
);
1225 struct mr_table
*mrt
;
1227 if (sk
->sk_type
!= SOCK_RAW
||
1228 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1231 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1235 if (optname
!= MRT_INIT
) {
1236 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
) &&
1237 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
))
1243 if (optlen
!= sizeof(int))
1247 if (rtnl_dereference(mrt
->mroute_sk
)) {
1252 ret
= ip_ra_control(sk
, 1, mrtsock_destruct
);
1254 rcu_assign_pointer(mrt
->mroute_sk
, sk
);
1255 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)++;
1256 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
,
1257 NETCONFA_IFINDEX_ALL
,
1258 net
->ipv4
.devconf_all
);
1263 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
))
1265 return ip_ra_control(sk
, 0, NULL
);
1268 if (optlen
!= sizeof(vif
))
1270 if (copy_from_user(&vif
, optval
, sizeof(vif
)))
1272 if (vif
.vifc_vifi
>= MAXVIFS
)
1275 if (optname
== MRT_ADD_VIF
) {
1276 ret
= vif_add(net
, mrt
, &vif
,
1277 sk
== rtnl_dereference(mrt
->mroute_sk
));
1279 ret
= vif_delete(mrt
, vif
.vifc_vifi
, 0, NULL
);
1285 * Manipulate the forwarding caches. These live
1286 * in a sort of kernel/user symbiosis.
1290 if (optlen
!= sizeof(mfc
))
1292 if (copy_from_user(&mfc
, optval
, sizeof(mfc
)))
1295 if (optname
== MRT_DEL_MFC
)
1296 ret
= ipmr_mfc_delete(mrt
, &mfc
);
1298 ret
= ipmr_mfc_add(net
, mrt
, &mfc
,
1299 sk
== rtnl_dereference(mrt
->mroute_sk
));
1303 * Control PIM assert.
1308 if (optlen
!= sizeof(v
))
1310 if (get_user(v
, (int __user
*)optval
))
1312 mrt
->mroute_do_assert
= v
;
1315 #ifdef CONFIG_IP_PIMSM
1320 if (optlen
!= sizeof(v
))
1322 if (get_user(v
, (int __user
*)optval
))
1328 if (v
!= mrt
->mroute_do_pim
) {
1329 mrt
->mroute_do_pim
= v
;
1330 mrt
->mroute_do_assert
= v
;
1336 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1341 if (optlen
!= sizeof(u32
))
1343 if (get_user(v
, (u32 __user
*)optval
))
1346 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1347 if (v
!= RT_TABLE_DEFAULT
&& v
>= 1000000000)
1352 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1355 if (!ipmr_new_table(net
, v
))
1358 raw_sk(sk
)->ipmr_table
= v
;
1365 * Spurious command, or MRT_VERSION which you cannot
1369 return -ENOPROTOOPT
;
1374 * Getsock opt support for the multicast routing system.
1377 int ip_mroute_getsockopt(struct sock
*sk
, int optname
, char __user
*optval
, int __user
*optlen
)
1381 struct net
*net
= sock_net(sk
);
1382 struct mr_table
*mrt
;
1384 if (sk
->sk_type
!= SOCK_RAW
||
1385 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1388 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1392 if (optname
!= MRT_VERSION
&&
1393 #ifdef CONFIG_IP_PIMSM
1394 optname
!= MRT_PIM
&&
1396 optname
!= MRT_ASSERT
)
1397 return -ENOPROTOOPT
;
1399 if (get_user(olr
, optlen
))
1402 olr
= min_t(unsigned int, olr
, sizeof(int));
1406 if (put_user(olr
, optlen
))
1408 if (optname
== MRT_VERSION
)
1410 #ifdef CONFIG_IP_PIMSM
1411 else if (optname
== MRT_PIM
)
1412 val
= mrt
->mroute_do_pim
;
1415 val
= mrt
->mroute_do_assert
;
1416 if (copy_to_user(optval
, &val
, olr
))
1422 * The IP multicast ioctl support routines.
1425 int ipmr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1427 struct sioc_sg_req sr
;
1428 struct sioc_vif_req vr
;
1429 struct vif_device
*vif
;
1430 struct mfc_cache
*c
;
1431 struct net
*net
= sock_net(sk
);
1432 struct mr_table
*mrt
;
1434 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1440 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1442 if (vr
.vifi
>= mrt
->maxvif
)
1444 read_lock(&mrt_lock
);
1445 vif
= &mrt
->vif_table
[vr
.vifi
];
1446 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1447 vr
.icount
= vif
->pkt_in
;
1448 vr
.ocount
= vif
->pkt_out
;
1449 vr
.ibytes
= vif
->bytes_in
;
1450 vr
.obytes
= vif
->bytes_out
;
1451 read_unlock(&mrt_lock
);
1453 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1457 read_unlock(&mrt_lock
);
1458 return -EADDRNOTAVAIL
;
1460 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1464 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1466 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1467 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1468 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1471 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1476 return -EADDRNOTAVAIL
;
1478 return -ENOIOCTLCMD
;
1482 #ifdef CONFIG_COMPAT
1483 struct compat_sioc_sg_req
{
1486 compat_ulong_t pktcnt
;
1487 compat_ulong_t bytecnt
;
1488 compat_ulong_t wrong_if
;
1491 struct compat_sioc_vif_req
{
1492 vifi_t vifi
; /* Which iface */
1493 compat_ulong_t icount
;
1494 compat_ulong_t ocount
;
1495 compat_ulong_t ibytes
;
1496 compat_ulong_t obytes
;
1499 int ipmr_compat_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
1501 struct compat_sioc_sg_req sr
;
1502 struct compat_sioc_vif_req vr
;
1503 struct vif_device
*vif
;
1504 struct mfc_cache
*c
;
1505 struct net
*net
= sock_net(sk
);
1506 struct mr_table
*mrt
;
1508 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1514 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1516 if (vr
.vifi
>= mrt
->maxvif
)
1518 read_lock(&mrt_lock
);
1519 vif
= &mrt
->vif_table
[vr
.vifi
];
1520 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1521 vr
.icount
= vif
->pkt_in
;
1522 vr
.ocount
= vif
->pkt_out
;
1523 vr
.ibytes
= vif
->bytes_in
;
1524 vr
.obytes
= vif
->bytes_out
;
1525 read_unlock(&mrt_lock
);
1527 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1531 read_unlock(&mrt_lock
);
1532 return -EADDRNOTAVAIL
;
1534 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1538 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1540 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1541 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1542 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1545 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1550 return -EADDRNOTAVAIL
;
1552 return -ENOIOCTLCMD
;
1558 static int ipmr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1560 struct net_device
*dev
= ptr
;
1561 struct net
*net
= dev_net(dev
);
1562 struct mr_table
*mrt
;
1563 struct vif_device
*v
;
1566 if (event
!= NETDEV_UNREGISTER
)
1569 ipmr_for_each_table(mrt
, net
) {
1570 v
= &mrt
->vif_table
[0];
1571 for (ct
= 0; ct
< mrt
->maxvif
; ct
++, v
++) {
1573 vif_delete(mrt
, ct
, 1, NULL
);
1580 static struct notifier_block ip_mr_notifier
= {
1581 .notifier_call
= ipmr_device_event
,
1585 * Encapsulate a packet by attaching a valid IPIP header to it.
1586 * This avoids tunnel drivers and other mess and gives us the speed so
1587 * important for multicast video.
1590 static void ip_encap(struct sk_buff
*skb
, __be32 saddr
, __be32 daddr
)
1593 const struct iphdr
*old_iph
= ip_hdr(skb
);
1595 skb_push(skb
, sizeof(struct iphdr
));
1596 skb
->transport_header
= skb
->network_header
;
1597 skb_reset_network_header(skb
);
1601 iph
->tos
= old_iph
->tos
;
1602 iph
->ttl
= old_iph
->ttl
;
1606 iph
->protocol
= IPPROTO_IPIP
;
1608 iph
->tot_len
= htons(skb
->len
);
1609 ip_select_ident(iph
, skb_dst(skb
), NULL
);
1612 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1616 static inline int ipmr_forward_finish(struct sk_buff
*skb
)
1618 struct ip_options
*opt
= &(IPCB(skb
)->opt
);
1620 IP_INC_STATS_BH(dev_net(skb_dst(skb
)->dev
), IPSTATS_MIB_OUTFORWDATAGRAMS
);
1621 IP_ADD_STATS_BH(dev_net(skb_dst(skb
)->dev
), IPSTATS_MIB_OUTOCTETS
, skb
->len
);
1623 if (unlikely(opt
->optlen
))
1624 ip_forward_options(skb
);
1626 return dst_output(skb
);
1630 * Processing handlers for ipmr_forward
1633 static void ipmr_queue_xmit(struct net
*net
, struct mr_table
*mrt
,
1634 struct sk_buff
*skb
, struct mfc_cache
*c
, int vifi
)
1636 const struct iphdr
*iph
= ip_hdr(skb
);
1637 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1638 struct net_device
*dev
;
1643 if (vif
->dev
== NULL
)
1646 #ifdef CONFIG_IP_PIMSM
1647 if (vif
->flags
& VIFF_REGISTER
) {
1649 vif
->bytes_out
+= skb
->len
;
1650 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1651 vif
->dev
->stats
.tx_packets
++;
1652 ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_WHOLEPKT
);
1657 if (vif
->flags
& VIFF_TUNNEL
) {
1658 rt
= ip_route_output_ports(net
, &fl4
, NULL
,
1659 vif
->remote
, vif
->local
,
1662 RT_TOS(iph
->tos
), vif
->link
);
1665 encap
= sizeof(struct iphdr
);
1667 rt
= ip_route_output_ports(net
, &fl4
, NULL
, iph
->daddr
, 0,
1670 RT_TOS(iph
->tos
), vif
->link
);
1677 if (skb
->len
+encap
> dst_mtu(&rt
->dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1678 /* Do not fragment multicasts. Alas, IPv4 does not
1679 * allow to send ICMP, so that packets will disappear
1683 IP_INC_STATS_BH(dev_net(dev
), IPSTATS_MIB_FRAGFAILS
);
1688 encap
+= LL_RESERVED_SPACE(dev
) + rt
->dst
.header_len
;
1690 if (skb_cow(skb
, encap
)) {
1696 vif
->bytes_out
+= skb
->len
;
1699 skb_dst_set(skb
, &rt
->dst
);
1700 ip_decrease_ttl(ip_hdr(skb
));
1702 /* FIXME: forward and output firewalls used to be called here.
1703 * What do we do with netfilter? -- RR
1705 if (vif
->flags
& VIFF_TUNNEL
) {
1706 ip_encap(skb
, vif
->local
, vif
->remote
);
1707 /* FIXME: extra output firewall step used to be here. --RR */
1708 vif
->dev
->stats
.tx_packets
++;
1709 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1712 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1715 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1716 * not only before forwarding, but after forwarding on all output
1717 * interfaces. It is clear, if mrouter runs a multicasting
1718 * program, it should receive packets not depending to what interface
1719 * program is joined.
1720 * If we will not make it, the program will have to join on all
1721 * interfaces. On the other hand, multihoming host (or router, but
1722 * not mrouter) cannot join to more than one interface - it will
1723 * result in receiving multiple packets.
1725 NF_HOOK(NFPROTO_IPV4
, NF_INET_FORWARD
, skb
, skb
->dev
, dev
,
1726 ipmr_forward_finish
);
1733 static int ipmr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
1737 for (ct
= mrt
->maxvif
-1; ct
>= 0; ct
--) {
1738 if (mrt
->vif_table
[ct
].dev
== dev
)
1744 /* "local" means that we should preserve one skb (for local delivery) */
1746 static int ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
1747 struct sk_buff
*skb
, struct mfc_cache
*cache
,
1753 vif
= cache
->mfc_parent
;
1754 cache
->mfc_un
.res
.pkt
++;
1755 cache
->mfc_un
.res
.bytes
+= skb
->len
;
1758 * Wrong interface: drop packet and (maybe) send PIM assert.
1760 if (mrt
->vif_table
[vif
].dev
!= skb
->dev
) {
1763 if (rt_is_output_route(skb_rtable(skb
))) {
1764 /* It is our own packet, looped back.
1765 * Very complicated situation...
1767 * The best workaround until routing daemons will be
1768 * fixed is not to redistribute packet, if it was
1769 * send through wrong interface. It means, that
1770 * multicast applications WILL NOT work for
1771 * (S,G), which have default multicast route pointing
1772 * to wrong oif. In any case, it is not a good
1773 * idea to use multicasting applications on router.
1778 cache
->mfc_un
.res
.wrong_if
++;
1779 true_vifi
= ipmr_find_vif(mrt
, skb
->dev
);
1781 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
1782 /* pimsm uses asserts, when switching from RPT to SPT,
1783 * so that we cannot check that packet arrived on an oif.
1784 * It is bad, but otherwise we would need to move pretty
1785 * large chunk of pimd to kernel. Ough... --ANK
1787 (mrt
->mroute_do_pim
||
1788 cache
->mfc_un
.res
.ttls
[true_vifi
] < 255) &&
1790 cache
->mfc_un
.res
.last_assert
+ MFC_ASSERT_THRESH
)) {
1791 cache
->mfc_un
.res
.last_assert
= jiffies
;
1792 ipmr_cache_report(mrt
, skb
, true_vifi
, IGMPMSG_WRONGVIF
);
1797 mrt
->vif_table
[vif
].pkt_in
++;
1798 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
1803 for (ct
= cache
->mfc_un
.res
.maxvif
- 1;
1804 ct
>= cache
->mfc_un
.res
.minvif
; ct
--) {
1805 if (ip_hdr(skb
)->ttl
> cache
->mfc_un
.res
.ttls
[ct
]) {
1807 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1810 ipmr_queue_xmit(net
, mrt
, skb2
, cache
,
1818 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1821 ipmr_queue_xmit(net
, mrt
, skb2
, cache
, psend
);
1823 ipmr_queue_xmit(net
, mrt
, skb
, cache
, psend
);
1834 static struct mr_table
*ipmr_rt_fib_lookup(struct net
*net
, struct sk_buff
*skb
)
1836 struct rtable
*rt
= skb_rtable(skb
);
1837 struct iphdr
*iph
= ip_hdr(skb
);
1838 struct flowi4 fl4
= {
1839 .daddr
= iph
->daddr
,
1840 .saddr
= iph
->saddr
,
1841 .flowi4_tos
= RT_TOS(iph
->tos
),
1842 .flowi4_oif
= (rt_is_output_route(rt
) ?
1843 skb
->dev
->ifindex
: 0),
1844 .flowi4_iif
= (rt_is_output_route(rt
) ?
1847 .flowi4_mark
= skb
->mark
,
1849 struct mr_table
*mrt
;
1852 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
1854 return ERR_PTR(err
);
1859 * Multicast packets for forwarding arrive here
1860 * Called with rcu_read_lock();
1863 int ip_mr_input(struct sk_buff
*skb
)
1865 struct mfc_cache
*cache
;
1866 struct net
*net
= dev_net(skb
->dev
);
1867 int local
= skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
;
1868 struct mr_table
*mrt
;
1870 /* Packet is looped back after forward, it should not be
1871 * forwarded second time, but still can be delivered locally.
1873 if (IPCB(skb
)->flags
& IPSKB_FORWARDED
)
1876 mrt
= ipmr_rt_fib_lookup(net
, skb
);
1879 return PTR_ERR(mrt
);
1882 if (IPCB(skb
)->opt
.router_alert
) {
1883 if (ip_call_ra_chain(skb
))
1885 } else if (ip_hdr(skb
)->protocol
== IPPROTO_IGMP
) {
1886 /* IGMPv1 (and broken IGMPv2 implementations sort of
1887 * Cisco IOS <= 11.2(8)) do not put router alert
1888 * option to IGMP packets destined to routable
1889 * groups. It is very bad, because it means
1890 * that we can forward NO IGMP messages.
1892 struct sock
*mroute_sk
;
1894 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1897 raw_rcv(mroute_sk
, skb
);
1903 /* already under rcu_read_lock() */
1904 cache
= ipmr_cache_find(mrt
, ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
1907 * No usable cache entry
1909 if (cache
== NULL
) {
1913 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1914 ip_local_deliver(skb
);
1920 read_lock(&mrt_lock
);
1921 vif
= ipmr_find_vif(mrt
, skb
->dev
);
1923 int err2
= ipmr_cache_unresolved(mrt
, vif
, skb
);
1924 read_unlock(&mrt_lock
);
1928 read_unlock(&mrt_lock
);
1933 read_lock(&mrt_lock
);
1934 ip_mr_forward(net
, mrt
, skb
, cache
, local
);
1935 read_unlock(&mrt_lock
);
1938 return ip_local_deliver(skb
);
1944 return ip_local_deliver(skb
);
1949 #ifdef CONFIG_IP_PIMSM
1950 /* called with rcu_read_lock() */
1951 static int __pim_rcv(struct mr_table
*mrt
, struct sk_buff
*skb
,
1952 unsigned int pimlen
)
1954 struct net_device
*reg_dev
= NULL
;
1955 struct iphdr
*encap
;
1957 encap
= (struct iphdr
*)(skb_transport_header(skb
) + pimlen
);
1960 * a. packet is really sent to a multicast group
1961 * b. packet is not a NULL-REGISTER
1962 * c. packet is not truncated
1964 if (!ipv4_is_multicast(encap
->daddr
) ||
1965 encap
->tot_len
== 0 ||
1966 ntohs(encap
->tot_len
) + pimlen
> skb
->len
)
1969 read_lock(&mrt_lock
);
1970 if (mrt
->mroute_reg_vif_num
>= 0)
1971 reg_dev
= mrt
->vif_table
[mrt
->mroute_reg_vif_num
].dev
;
1972 read_unlock(&mrt_lock
);
1974 if (reg_dev
== NULL
)
1977 skb
->mac_header
= skb
->network_header
;
1978 skb_pull(skb
, (u8
*)encap
- skb
->data
);
1979 skb_reset_network_header(skb
);
1980 skb
->protocol
= htons(ETH_P_IP
);
1981 skb
->ip_summed
= CHECKSUM_NONE
;
1982 skb
->pkt_type
= PACKET_HOST
;
1984 skb_tunnel_rx(skb
, reg_dev
);
1988 return NET_RX_SUCCESS
;
1992 #ifdef CONFIG_IP_PIMSM_V1
1994 * Handle IGMP messages of PIMv1
1997 int pim_rcv_v1(struct sk_buff
*skb
)
1999 struct igmphdr
*pim
;
2000 struct net
*net
= dev_net(skb
->dev
);
2001 struct mr_table
*mrt
;
2003 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2006 pim
= igmp_hdr(skb
);
2008 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2011 if (!mrt
->mroute_do_pim
||
2012 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
2015 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2023 #ifdef CONFIG_IP_PIMSM_V2
2024 static int pim_rcv(struct sk_buff
*skb
)
2026 struct pimreghdr
*pim
;
2027 struct net
*net
= dev_net(skb
->dev
);
2028 struct mr_table
*mrt
;
2030 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2033 pim
= (struct pimreghdr
*)skb_transport_header(skb
);
2034 if (pim
->type
!= ((PIM_VERSION
<< 4) | (PIM_REGISTER
)) ||
2035 (pim
->flags
& PIM_NULL_REGISTER
) ||
2036 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
2037 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
2040 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2043 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2051 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2052 struct mfc_cache
*c
, struct rtmsg
*rtm
)
2055 struct rtnexthop
*nhp
;
2056 struct nlattr
*mp_attr
;
2057 struct rta_mfc_stats mfcs
;
2059 /* If cache is unresolved, don't try to parse IIF and OIF */
2060 if (c
->mfc_parent
>= MAXVIFS
)
2063 if (VIF_EXISTS(mrt
, c
->mfc_parent
) &&
2064 nla_put_u32(skb
, RTA_IIF
, mrt
->vif_table
[c
->mfc_parent
].dev
->ifindex
) < 0)
2067 if (!(mp_attr
= nla_nest_start(skb
, RTA_MULTIPATH
)))
2070 for (ct
= c
->mfc_un
.res
.minvif
; ct
< c
->mfc_un
.res
.maxvif
; ct
++) {
2071 if (VIF_EXISTS(mrt
, ct
) && c
->mfc_un
.res
.ttls
[ct
] < 255) {
2072 if (!(nhp
= nla_reserve_nohdr(skb
, sizeof(*nhp
)))) {
2073 nla_nest_cancel(skb
, mp_attr
);
2077 nhp
->rtnh_flags
= 0;
2078 nhp
->rtnh_hops
= c
->mfc_un
.res
.ttls
[ct
];
2079 nhp
->rtnh_ifindex
= mrt
->vif_table
[ct
].dev
->ifindex
;
2080 nhp
->rtnh_len
= sizeof(*nhp
);
2084 nla_nest_end(skb
, mp_attr
);
2086 mfcs
.mfcs_packets
= c
->mfc_un
.res
.pkt
;
2087 mfcs
.mfcs_bytes
= c
->mfc_un
.res
.bytes
;
2088 mfcs
.mfcs_wrong_if
= c
->mfc_un
.res
.wrong_if
;
2089 if (nla_put(skb
, RTA_MFC_STATS
, sizeof(mfcs
), &mfcs
) < 0)
2092 rtm
->rtm_type
= RTN_MULTICAST
;
2096 int ipmr_get_route(struct net
*net
, struct sk_buff
*skb
,
2097 __be32 saddr
, __be32 daddr
,
2098 struct rtmsg
*rtm
, int nowait
)
2100 struct mfc_cache
*cache
;
2101 struct mr_table
*mrt
;
2104 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2109 cache
= ipmr_cache_find(mrt
, saddr
, daddr
);
2111 if (cache
== NULL
) {
2112 struct sk_buff
*skb2
;
2114 struct net_device
*dev
;
2123 read_lock(&mrt_lock
);
2125 vif
= ipmr_find_vif(mrt
, dev
);
2127 read_unlock(&mrt_lock
);
2131 skb2
= skb_clone(skb
, GFP_ATOMIC
);
2133 read_unlock(&mrt_lock
);
2138 skb_push(skb2
, sizeof(struct iphdr
));
2139 skb_reset_network_header(skb2
);
2141 iph
->ihl
= sizeof(struct iphdr
) >> 2;
2145 err
= ipmr_cache_unresolved(mrt
, vif
, skb2
);
2146 read_unlock(&mrt_lock
);
2151 read_lock(&mrt_lock
);
2152 if (!nowait
&& (rtm
->rtm_flags
& RTM_F_NOTIFY
))
2153 cache
->mfc_flags
|= MFC_NOTIFY
;
2154 err
= __ipmr_fill_mroute(mrt
, skb
, cache
, rtm
);
2155 read_unlock(&mrt_lock
);
2160 static int ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2161 u32 portid
, u32 seq
, struct mfc_cache
*c
, int cmd
)
2163 struct nlmsghdr
*nlh
;
2167 nlh
= nlmsg_put(skb
, portid
, seq
, cmd
, sizeof(*rtm
), NLM_F_MULTI
);
2171 rtm
= nlmsg_data(nlh
);
2172 rtm
->rtm_family
= RTNL_FAMILY_IPMR
;
2173 rtm
->rtm_dst_len
= 32;
2174 rtm
->rtm_src_len
= 32;
2176 rtm
->rtm_table
= mrt
->id
;
2177 if (nla_put_u32(skb
, RTA_TABLE
, mrt
->id
))
2178 goto nla_put_failure
;
2179 rtm
->rtm_type
= RTN_MULTICAST
;
2180 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2181 if (c
->mfc_flags
& MFC_STATIC
)
2182 rtm
->rtm_protocol
= RTPROT_STATIC
;
2184 rtm
->rtm_protocol
= RTPROT_MROUTED
;
2187 if (nla_put_be32(skb
, RTA_SRC
, c
->mfc_origin
) ||
2188 nla_put_be32(skb
, RTA_DST
, c
->mfc_mcastgrp
))
2189 goto nla_put_failure
;
2190 err
= __ipmr_fill_mroute(mrt
, skb
, c
, rtm
);
2191 /* do not break the dump if cache is unresolved */
2192 if (err
< 0 && err
!= -ENOENT
)
2193 goto nla_put_failure
;
2195 return nlmsg_end(skb
, nlh
);
2198 nlmsg_cancel(skb
, nlh
);
2202 static size_t mroute_msgsize(bool unresolved
, int maxvif
)
2205 NLMSG_ALIGN(sizeof(struct rtmsg
))
2206 + nla_total_size(4) /* RTA_TABLE */
2207 + nla_total_size(4) /* RTA_SRC */
2208 + nla_total_size(4) /* RTA_DST */
2213 + nla_total_size(4) /* RTA_IIF */
2214 + nla_total_size(0) /* RTA_MULTIPATH */
2215 + maxvif
* NLA_ALIGN(sizeof(struct rtnexthop
))
2217 + nla_total_size(sizeof(struct rta_mfc_stats
))
2223 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
2226 struct net
*net
= read_pnet(&mrt
->net
);
2227 struct sk_buff
*skb
;
2230 skb
= nlmsg_new(mroute_msgsize(mfc
->mfc_parent
>= MAXVIFS
, mrt
->maxvif
),
2235 err
= ipmr_fill_mroute(mrt
, skb
, 0, 0, mfc
, cmd
);
2239 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_MROUTE
, NULL
, GFP_ATOMIC
);
2245 rtnl_set_sk_err(net
, RTNLGRP_IPV4_MROUTE
, err
);
2248 static int ipmr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2250 struct net
*net
= sock_net(skb
->sk
);
2251 struct mr_table
*mrt
;
2252 struct mfc_cache
*mfc
;
2253 unsigned int t
= 0, s_t
;
2254 unsigned int h
= 0, s_h
;
2255 unsigned int e
= 0, s_e
;
2262 ipmr_for_each_table(mrt
, net
) {
2267 for (h
= s_h
; h
< MFC_LINES
; h
++) {
2268 list_for_each_entry_rcu(mfc
, &mrt
->mfc_cache_array
[h
], list
) {
2271 if (ipmr_fill_mroute(mrt
, skb
,
2272 NETLINK_CB(cb
->skb
).portid
,
2274 mfc
, RTM_NEWROUTE
) < 0)
2281 spin_lock_bh(&mfc_unres_lock
);
2282 list_for_each_entry(mfc
, &mrt
->mfc_unres_queue
, list
) {
2285 if (ipmr_fill_mroute(mrt
, skb
,
2286 NETLINK_CB(cb
->skb
).portid
,
2288 mfc
, RTM_NEWROUTE
) < 0) {
2289 spin_unlock_bh(&mfc_unres_lock
);
2295 spin_unlock_bh(&mfc_unres_lock
);
2311 #ifdef CONFIG_PROC_FS
2313 * The /proc interfaces to multicast routing :
2314 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2316 struct ipmr_vif_iter
{
2317 struct seq_net_private p
;
2318 struct mr_table
*mrt
;
2322 static struct vif_device
*ipmr_vif_seq_idx(struct net
*net
,
2323 struct ipmr_vif_iter
*iter
,
2326 struct mr_table
*mrt
= iter
->mrt
;
2328 for (iter
->ct
= 0; iter
->ct
< mrt
->maxvif
; ++iter
->ct
) {
2329 if (!VIF_EXISTS(mrt
, iter
->ct
))
2332 return &mrt
->vif_table
[iter
->ct
];
2337 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2338 __acquires(mrt_lock
)
2340 struct ipmr_vif_iter
*iter
= seq
->private;
2341 struct net
*net
= seq_file_net(seq
);
2342 struct mr_table
*mrt
;
2344 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2346 return ERR_PTR(-ENOENT
);
2350 read_lock(&mrt_lock
);
2351 return *pos
? ipmr_vif_seq_idx(net
, seq
->private, *pos
- 1)
2355 static void *ipmr_vif_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2357 struct ipmr_vif_iter
*iter
= seq
->private;
2358 struct net
*net
= seq_file_net(seq
);
2359 struct mr_table
*mrt
= iter
->mrt
;
2362 if (v
== SEQ_START_TOKEN
)
2363 return ipmr_vif_seq_idx(net
, iter
, 0);
2365 while (++iter
->ct
< mrt
->maxvif
) {
2366 if (!VIF_EXISTS(mrt
, iter
->ct
))
2368 return &mrt
->vif_table
[iter
->ct
];
2373 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
2374 __releases(mrt_lock
)
2376 read_unlock(&mrt_lock
);
2379 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
2381 struct ipmr_vif_iter
*iter
= seq
->private;
2382 struct mr_table
*mrt
= iter
->mrt
;
2384 if (v
== SEQ_START_TOKEN
) {
2386 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2388 const struct vif_device
*vif
= v
;
2389 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
2392 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2393 vif
- mrt
->vif_table
,
2394 name
, vif
->bytes_in
, vif
->pkt_in
,
2395 vif
->bytes_out
, vif
->pkt_out
,
2396 vif
->flags
, vif
->local
, vif
->remote
);
2401 static const struct seq_operations ipmr_vif_seq_ops
= {
2402 .start
= ipmr_vif_seq_start
,
2403 .next
= ipmr_vif_seq_next
,
2404 .stop
= ipmr_vif_seq_stop
,
2405 .show
= ipmr_vif_seq_show
,
2408 static int ipmr_vif_open(struct inode
*inode
, struct file
*file
)
2410 return seq_open_net(inode
, file
, &ipmr_vif_seq_ops
,
2411 sizeof(struct ipmr_vif_iter
));
2414 static const struct file_operations ipmr_vif_fops
= {
2415 .owner
= THIS_MODULE
,
2416 .open
= ipmr_vif_open
,
2418 .llseek
= seq_lseek
,
2419 .release
= seq_release_net
,
2422 struct ipmr_mfc_iter
{
2423 struct seq_net_private p
;
2424 struct mr_table
*mrt
;
2425 struct list_head
*cache
;
2430 static struct mfc_cache
*ipmr_mfc_seq_idx(struct net
*net
,
2431 struct ipmr_mfc_iter
*it
, loff_t pos
)
2433 struct mr_table
*mrt
= it
->mrt
;
2434 struct mfc_cache
*mfc
;
2437 for (it
->ct
= 0; it
->ct
< MFC_LINES
; it
->ct
++) {
2438 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2439 list_for_each_entry_rcu(mfc
, it
->cache
, list
)
2445 spin_lock_bh(&mfc_unres_lock
);
2446 it
->cache
= &mrt
->mfc_unres_queue
;
2447 list_for_each_entry(mfc
, it
->cache
, list
)
2450 spin_unlock_bh(&mfc_unres_lock
);
2457 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2459 struct ipmr_mfc_iter
*it
= seq
->private;
2460 struct net
*net
= seq_file_net(seq
);
2461 struct mr_table
*mrt
;
2463 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2465 return ERR_PTR(-ENOENT
);
2470 return *pos
? ipmr_mfc_seq_idx(net
, seq
->private, *pos
- 1)
2474 static void *ipmr_mfc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2476 struct mfc_cache
*mfc
= v
;
2477 struct ipmr_mfc_iter
*it
= seq
->private;
2478 struct net
*net
= seq_file_net(seq
);
2479 struct mr_table
*mrt
= it
->mrt
;
2483 if (v
== SEQ_START_TOKEN
)
2484 return ipmr_mfc_seq_idx(net
, seq
->private, 0);
2486 if (mfc
->list
.next
!= it
->cache
)
2487 return list_entry(mfc
->list
.next
, struct mfc_cache
, list
);
2489 if (it
->cache
== &mrt
->mfc_unres_queue
)
2492 BUG_ON(it
->cache
!= &mrt
->mfc_cache_array
[it
->ct
]);
2494 while (++it
->ct
< MFC_LINES
) {
2495 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2496 if (list_empty(it
->cache
))
2498 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2501 /* exhausted cache_array, show unresolved */
2503 it
->cache
= &mrt
->mfc_unres_queue
;
2506 spin_lock_bh(&mfc_unres_lock
);
2507 if (!list_empty(it
->cache
))
2508 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2511 spin_unlock_bh(&mfc_unres_lock
);
2517 static void ipmr_mfc_seq_stop(struct seq_file
*seq
, void *v
)
2519 struct ipmr_mfc_iter
*it
= seq
->private;
2520 struct mr_table
*mrt
= it
->mrt
;
2522 if (it
->cache
== &mrt
->mfc_unres_queue
)
2523 spin_unlock_bh(&mfc_unres_lock
);
2524 else if (it
->cache
== &mrt
->mfc_cache_array
[it
->ct
])
2528 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
2532 if (v
== SEQ_START_TOKEN
) {
2534 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2536 const struct mfc_cache
*mfc
= v
;
2537 const struct ipmr_mfc_iter
*it
= seq
->private;
2538 const struct mr_table
*mrt
= it
->mrt
;
2540 seq_printf(seq
, "%08X %08X %-3hd",
2541 (__force u32
) mfc
->mfc_mcastgrp
,
2542 (__force u32
) mfc
->mfc_origin
,
2545 if (it
->cache
!= &mrt
->mfc_unres_queue
) {
2546 seq_printf(seq
, " %8lu %8lu %8lu",
2547 mfc
->mfc_un
.res
.pkt
,
2548 mfc
->mfc_un
.res
.bytes
,
2549 mfc
->mfc_un
.res
.wrong_if
);
2550 for (n
= mfc
->mfc_un
.res
.minvif
;
2551 n
< mfc
->mfc_un
.res
.maxvif
; n
++) {
2552 if (VIF_EXISTS(mrt
, n
) &&
2553 mfc
->mfc_un
.res
.ttls
[n
] < 255)
2556 n
, mfc
->mfc_un
.res
.ttls
[n
]);
2559 /* unresolved mfc_caches don't contain
2560 * pkt, bytes and wrong_if values
2562 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2564 seq_putc(seq
, '\n');
2569 static const struct seq_operations ipmr_mfc_seq_ops
= {
2570 .start
= ipmr_mfc_seq_start
,
2571 .next
= ipmr_mfc_seq_next
,
2572 .stop
= ipmr_mfc_seq_stop
,
2573 .show
= ipmr_mfc_seq_show
,
2576 static int ipmr_mfc_open(struct inode
*inode
, struct file
*file
)
2578 return seq_open_net(inode
, file
, &ipmr_mfc_seq_ops
,
2579 sizeof(struct ipmr_mfc_iter
));
2582 static const struct file_operations ipmr_mfc_fops
= {
2583 .owner
= THIS_MODULE
,
2584 .open
= ipmr_mfc_open
,
2586 .llseek
= seq_lseek
,
2587 .release
= seq_release_net
,
2591 #ifdef CONFIG_IP_PIMSM_V2
2592 static const struct net_protocol pim_protocol
= {
2600 * Setup for IP multicast routing
2602 static int __net_init
ipmr_net_init(struct net
*net
)
2606 err
= ipmr_rules_init(net
);
2610 #ifdef CONFIG_PROC_FS
2612 if (!proc_net_fops_create(net
, "ip_mr_vif", 0, &ipmr_vif_fops
))
2614 if (!proc_net_fops_create(net
, "ip_mr_cache", 0, &ipmr_mfc_fops
))
2615 goto proc_cache_fail
;
2619 #ifdef CONFIG_PROC_FS
2621 proc_net_remove(net
, "ip_mr_vif");
2623 ipmr_rules_exit(net
);
2629 static void __net_exit
ipmr_net_exit(struct net
*net
)
2631 #ifdef CONFIG_PROC_FS
2632 proc_net_remove(net
, "ip_mr_cache");
2633 proc_net_remove(net
, "ip_mr_vif");
2635 ipmr_rules_exit(net
);
2638 static struct pernet_operations ipmr_net_ops
= {
2639 .init
= ipmr_net_init
,
2640 .exit
= ipmr_net_exit
,
2643 int __init
ip_mr_init(void)
2647 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
2648 sizeof(struct mfc_cache
),
2649 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
2654 err
= register_pernet_subsys(&ipmr_net_ops
);
2656 goto reg_pernet_fail
;
2658 err
= register_netdevice_notifier(&ip_mr_notifier
);
2660 goto reg_notif_fail
;
2661 #ifdef CONFIG_IP_PIMSM_V2
2662 if (inet_add_protocol(&pim_protocol
, IPPROTO_PIM
) < 0) {
2663 pr_err("%s: can't add PIM protocol\n", __func__
);
2665 goto add_proto_fail
;
2668 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETROUTE
,
2669 NULL
, ipmr_rtm_dumproute
, NULL
);
2672 #ifdef CONFIG_IP_PIMSM_V2
2674 unregister_netdevice_notifier(&ip_mr_notifier
);
2677 unregister_pernet_subsys(&ipmr_net_ops
);
2679 kmem_cache_destroy(mrt_cachep
);