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>
69 #include <net/nexthop.h>
72 struct fib_rule common
;
79 /* Big lock, protecting vif table, mrt cache and mroute socket state.
80 * Note that the changes are semaphored via rtnl_lock.
83 static DEFINE_RWLOCK(mrt_lock
);
85 /* Multicast router control variables */
87 /* Special spinlock for queue of unresolved entries */
88 static DEFINE_SPINLOCK(mfc_unres_lock
);
90 /* We return to original Alan's scheme. Hash table of resolved
91 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
95 * In this case data path is free of exclusive locks at all.
98 static struct kmem_cache
*mrt_cachep __read_mostly
;
100 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
);
101 static void ipmr_free_table(struct mr_table
*mrt
);
103 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
104 struct sk_buff
*skb
, struct mfc_cache
*cache
,
106 static int ipmr_cache_report(struct mr_table
*mrt
,
107 struct sk_buff
*pkt
, vifi_t vifi
, int assert);
108 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
109 struct mfc_cache
*c
, struct rtmsg
*rtm
);
110 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
112 static void mroute_clean_tables(struct mr_table
*mrt
, bool all
);
113 static void ipmr_expire_process(unsigned long arg
);
115 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116 #define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
119 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
121 struct mr_table
*mrt
;
123 ipmr_for_each_table(mrt
, net
) {
130 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
131 struct mr_table
**mrt
)
134 struct ipmr_result res
;
135 struct fib_lookup_arg arg
= {
137 .flags
= FIB_LOOKUP_NOREF
,
140 err
= fib_rules_lookup(net
->ipv4
.mr_rules_ops
,
141 flowi4_to_flowi(flp4
), 0, &arg
);
148 static int ipmr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
149 int flags
, struct fib_lookup_arg
*arg
)
151 struct ipmr_result
*res
= arg
->result
;
152 struct mr_table
*mrt
;
154 switch (rule
->action
) {
157 case FR_ACT_UNREACHABLE
:
159 case FR_ACT_PROHIBIT
:
161 case FR_ACT_BLACKHOLE
:
166 mrt
= ipmr_get_table(rule
->fr_net
, rule
->table
);
173 static int ipmr_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
178 static const struct nla_policy ipmr_rule_policy
[FRA_MAX
+ 1] = {
182 static int ipmr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
183 struct fib_rule_hdr
*frh
, struct nlattr
**tb
)
188 static int ipmr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
194 static int ipmr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
195 struct fib_rule_hdr
*frh
)
203 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template
= {
204 .family
= RTNL_FAMILY_IPMR
,
205 .rule_size
= sizeof(struct ipmr_rule
),
206 .addr_size
= sizeof(u32
),
207 .action
= ipmr_rule_action
,
208 .match
= ipmr_rule_match
,
209 .configure
= ipmr_rule_configure
,
210 .compare
= ipmr_rule_compare
,
211 .fill
= ipmr_rule_fill
,
212 .nlgroup
= RTNLGRP_IPV4_RULE
,
213 .policy
= ipmr_rule_policy
,
214 .owner
= THIS_MODULE
,
217 static int __net_init
ipmr_rules_init(struct net
*net
)
219 struct fib_rules_ops
*ops
;
220 struct mr_table
*mrt
;
223 ops
= fib_rules_register(&ipmr_rules_ops_template
, net
);
227 INIT_LIST_HEAD(&net
->ipv4
.mr_tables
);
229 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
235 err
= fib_default_rule_add(ops
, 0x7fff, RT_TABLE_DEFAULT
, 0);
239 net
->ipv4
.mr_rules_ops
= ops
;
243 ipmr_free_table(mrt
);
245 fib_rules_unregister(ops
);
249 static void __net_exit
ipmr_rules_exit(struct net
*net
)
251 struct mr_table
*mrt
, *next
;
254 list_for_each_entry_safe(mrt
, next
, &net
->ipv4
.mr_tables
, list
) {
255 list_del(&mrt
->list
);
256 ipmr_free_table(mrt
);
258 fib_rules_unregister(net
->ipv4
.mr_rules_ops
);
262 #define ipmr_for_each_table(mrt, net) \
263 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
265 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
267 return net
->ipv4
.mrt
;
270 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
271 struct mr_table
**mrt
)
273 *mrt
= net
->ipv4
.mrt
;
277 static int __net_init
ipmr_rules_init(struct net
*net
)
279 struct mr_table
*mrt
;
281 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
288 static void __net_exit
ipmr_rules_exit(struct net
*net
)
291 ipmr_free_table(net
->ipv4
.mrt
);
292 net
->ipv4
.mrt
= NULL
;
297 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
)
299 struct mr_table
*mrt
;
302 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
303 if (id
!= RT_TABLE_DEFAULT
&& id
>= 1000000000)
304 return ERR_PTR(-EINVAL
);
306 mrt
= ipmr_get_table(net
, id
);
310 mrt
= kzalloc(sizeof(*mrt
), GFP_KERNEL
);
312 return ERR_PTR(-ENOMEM
);
313 write_pnet(&mrt
->net
, net
);
316 /* Forwarding cache */
317 for (i
= 0; i
< MFC_LINES
; i
++)
318 INIT_LIST_HEAD(&mrt
->mfc_cache_array
[i
]);
320 INIT_LIST_HEAD(&mrt
->mfc_unres_queue
);
322 setup_timer(&mrt
->ipmr_expire_timer
, ipmr_expire_process
,
325 mrt
->mroute_reg_vif_num
= -1;
326 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
327 list_add_tail_rcu(&mrt
->list
, &net
->ipv4
.mr_tables
);
332 static void ipmr_free_table(struct mr_table
*mrt
)
334 del_timer_sync(&mrt
->ipmr_expire_timer
);
335 mroute_clean_tables(mrt
, true);
339 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
341 static void ipmr_del_tunnel(struct net_device
*dev
, struct vifctl
*v
)
343 struct net
*net
= dev_net(dev
);
347 dev
= __dev_get_by_name(net
, "tunl0");
349 const struct net_device_ops
*ops
= dev
->netdev_ops
;
351 struct ip_tunnel_parm p
;
353 memset(&p
, 0, sizeof(p
));
354 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
355 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
358 p
.iph
.protocol
= IPPROTO_IPIP
;
359 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
360 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
362 if (ops
->ndo_do_ioctl
) {
363 mm_segment_t oldfs
= get_fs();
366 ops
->ndo_do_ioctl(dev
, &ifr
, SIOCDELTUNNEL
);
372 /* Initialize ipmr pimreg/tunnel in_device */
373 static bool ipmr_init_vif_indev(const struct net_device
*dev
)
375 struct in_device
*in_dev
;
379 in_dev
= __in_dev_get_rtnl(dev
);
382 ipv4_devconf_setall(in_dev
);
383 neigh_parms_data_state_setall(in_dev
->arp_parms
);
384 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
389 static struct net_device
*ipmr_new_tunnel(struct net
*net
, struct vifctl
*v
)
391 struct net_device
*dev
;
393 dev
= __dev_get_by_name(net
, "tunl0");
396 const struct net_device_ops
*ops
= dev
->netdev_ops
;
399 struct ip_tunnel_parm p
;
401 memset(&p
, 0, sizeof(p
));
402 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
403 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
406 p
.iph
.protocol
= IPPROTO_IPIP
;
407 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
408 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
410 if (ops
->ndo_do_ioctl
) {
411 mm_segment_t oldfs
= get_fs();
414 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
422 (dev
= __dev_get_by_name(net
, p
.name
)) != NULL
) {
423 dev
->flags
|= IFF_MULTICAST
;
424 if (!ipmr_init_vif_indev(dev
))
434 unregister_netdevice(dev
);
438 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
439 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
441 struct net
*net
= dev_net(dev
);
442 struct mr_table
*mrt
;
443 struct flowi4 fl4
= {
444 .flowi4_oif
= dev
->ifindex
,
445 .flowi4_iif
= skb
->skb_iif
? : LOOPBACK_IFINDEX
,
446 .flowi4_mark
= skb
->mark
,
450 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
456 read_lock(&mrt_lock
);
457 dev
->stats
.tx_bytes
+= skb
->len
;
458 dev
->stats
.tx_packets
++;
459 ipmr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, IGMPMSG_WHOLEPKT
);
460 read_unlock(&mrt_lock
);
465 static int reg_vif_get_iflink(const struct net_device
*dev
)
470 static const struct net_device_ops reg_vif_netdev_ops
= {
471 .ndo_start_xmit
= reg_vif_xmit
,
472 .ndo_get_iflink
= reg_vif_get_iflink
,
475 static void reg_vif_setup(struct net_device
*dev
)
477 dev
->type
= ARPHRD_PIMREG
;
478 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
479 dev
->flags
= IFF_NOARP
;
480 dev
->netdev_ops
= ®_vif_netdev_ops
;
481 dev
->destructor
= free_netdev
;
482 dev
->features
|= NETIF_F_NETNS_LOCAL
;
485 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
487 struct net_device
*dev
;
490 if (mrt
->id
== RT_TABLE_DEFAULT
)
491 sprintf(name
, "pimreg");
493 sprintf(name
, "pimreg%u", mrt
->id
);
495 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, reg_vif_setup
);
500 dev_net_set(dev
, net
);
502 if (register_netdevice(dev
)) {
507 if (!ipmr_init_vif_indev(dev
))
517 unregister_netdevice(dev
);
521 /* called with rcu_read_lock() */
522 static int __pim_rcv(struct mr_table
*mrt
, struct sk_buff
*skb
,
525 struct net_device
*reg_dev
= NULL
;
528 encap
= (struct iphdr
*)(skb_transport_header(skb
) + pimlen
);
530 * a. packet is really sent to a multicast group
531 * b. packet is not a NULL-REGISTER
532 * c. packet is not truncated
534 if (!ipv4_is_multicast(encap
->daddr
) ||
535 encap
->tot_len
== 0 ||
536 ntohs(encap
->tot_len
) + pimlen
> skb
->len
)
539 read_lock(&mrt_lock
);
540 if (mrt
->mroute_reg_vif_num
>= 0)
541 reg_dev
= mrt
->vif_table
[mrt
->mroute_reg_vif_num
].dev
;
542 read_unlock(&mrt_lock
);
547 skb
->mac_header
= skb
->network_header
;
548 skb_pull(skb
, (u8
*)encap
- skb
->data
);
549 skb_reset_network_header(skb
);
550 skb
->protocol
= htons(ETH_P_IP
);
551 skb
->ip_summed
= CHECKSUM_NONE
;
553 skb_tunnel_rx(skb
, reg_dev
, dev_net(reg_dev
));
557 return NET_RX_SUCCESS
;
560 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
567 * vif_delete - Delete a VIF entry
568 * @notify: Set to 1, if the caller is a notifier_call
570 static int vif_delete(struct mr_table
*mrt
, int vifi
, int notify
,
571 struct list_head
*head
)
573 struct vif_device
*v
;
574 struct net_device
*dev
;
575 struct in_device
*in_dev
;
577 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
578 return -EADDRNOTAVAIL
;
580 v
= &mrt
->vif_table
[vifi
];
582 write_lock_bh(&mrt_lock
);
587 write_unlock_bh(&mrt_lock
);
588 return -EADDRNOTAVAIL
;
591 if (vifi
== mrt
->mroute_reg_vif_num
)
592 mrt
->mroute_reg_vif_num
= -1;
594 if (vifi
+ 1 == mrt
->maxvif
) {
597 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
598 if (VIF_EXISTS(mrt
, tmp
))
604 write_unlock_bh(&mrt_lock
);
606 dev_set_allmulti(dev
, -1);
608 in_dev
= __in_dev_get_rtnl(dev
);
610 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)--;
611 inet_netconf_notify_devconf(dev_net(dev
),
612 NETCONFA_MC_FORWARDING
,
613 dev
->ifindex
, &in_dev
->cnf
);
614 ip_rt_multicast_event(in_dev
);
617 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
) && !notify
)
618 unregister_netdevice_queue(dev
, head
);
624 static void ipmr_cache_free_rcu(struct rcu_head
*head
)
626 struct mfc_cache
*c
= container_of(head
, struct mfc_cache
, rcu
);
628 kmem_cache_free(mrt_cachep
, c
);
631 static inline void ipmr_cache_free(struct mfc_cache
*c
)
633 call_rcu(&c
->rcu
, ipmr_cache_free_rcu
);
636 /* Destroy an unresolved cache entry, killing queued skbs
637 * and reporting error to netlink readers.
639 static void ipmr_destroy_unres(struct mr_table
*mrt
, struct mfc_cache
*c
)
641 struct net
*net
= read_pnet(&mrt
->net
);
645 atomic_dec(&mrt
->cache_resolve_queue_len
);
647 while ((skb
= skb_dequeue(&c
->mfc_un
.unres
.unresolved
))) {
648 if (ip_hdr(skb
)->version
== 0) {
649 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
650 nlh
->nlmsg_type
= NLMSG_ERROR
;
651 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
652 skb_trim(skb
, nlh
->nlmsg_len
);
654 e
->error
= -ETIMEDOUT
;
655 memset(&e
->msg
, 0, sizeof(e
->msg
));
657 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
666 /* Timer process for the unresolved queue. */
667 static void ipmr_expire_process(unsigned long arg
)
669 struct mr_table
*mrt
= (struct mr_table
*)arg
;
671 unsigned long expires
;
672 struct mfc_cache
*c
, *next
;
674 if (!spin_trylock(&mfc_unres_lock
)) {
675 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+HZ
/10);
679 if (list_empty(&mrt
->mfc_unres_queue
))
685 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
686 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
687 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
688 if (interval
< expires
)
694 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
695 ipmr_destroy_unres(mrt
, c
);
698 if (!list_empty(&mrt
->mfc_unres_queue
))
699 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
702 spin_unlock(&mfc_unres_lock
);
705 /* Fill oifs list. It is called under write locked mrt_lock. */
706 static void ipmr_update_thresholds(struct mr_table
*mrt
, struct mfc_cache
*cache
,
711 cache
->mfc_un
.res
.minvif
= MAXVIFS
;
712 cache
->mfc_un
.res
.maxvif
= 0;
713 memset(cache
->mfc_un
.res
.ttls
, 255, MAXVIFS
);
715 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
716 if (VIF_EXISTS(mrt
, vifi
) &&
717 ttls
[vifi
] && ttls
[vifi
] < 255) {
718 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
719 if (cache
->mfc_un
.res
.minvif
> vifi
)
720 cache
->mfc_un
.res
.minvif
= vifi
;
721 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
722 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
727 static int vif_add(struct net
*net
, struct mr_table
*mrt
,
728 struct vifctl
*vifc
, int mrtsock
)
730 int vifi
= vifc
->vifc_vifi
;
731 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
732 struct net_device
*dev
;
733 struct in_device
*in_dev
;
737 if (VIF_EXISTS(mrt
, vifi
))
740 switch (vifc
->vifc_flags
) {
742 if (!ipmr_pimsm_enabled())
744 /* Special Purpose VIF in PIM
745 * All the packets will be sent to the daemon
747 if (mrt
->mroute_reg_vif_num
>= 0)
749 dev
= ipmr_reg_vif(net
, mrt
);
752 err
= dev_set_allmulti(dev
, 1);
754 unregister_netdevice(dev
);
760 dev
= ipmr_new_tunnel(net
, vifc
);
763 err
= dev_set_allmulti(dev
, 1);
765 ipmr_del_tunnel(dev
, vifc
);
770 case VIFF_USE_IFINDEX
:
772 if (vifc
->vifc_flags
== VIFF_USE_IFINDEX
) {
773 dev
= dev_get_by_index(net
, vifc
->vifc_lcl_ifindex
);
774 if (dev
&& !__in_dev_get_rtnl(dev
)) {
776 return -EADDRNOTAVAIL
;
779 dev
= ip_dev_find(net
, vifc
->vifc_lcl_addr
.s_addr
);
782 return -EADDRNOTAVAIL
;
783 err
= dev_set_allmulti(dev
, 1);
793 in_dev
= __in_dev_get_rtnl(dev
);
796 return -EADDRNOTAVAIL
;
798 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)++;
799 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
, dev
->ifindex
,
801 ip_rt_multicast_event(in_dev
);
803 /* Fill in the VIF structures */
805 v
->rate_limit
= vifc
->vifc_rate_limit
;
806 v
->local
= vifc
->vifc_lcl_addr
.s_addr
;
807 v
->remote
= vifc
->vifc_rmt_addr
.s_addr
;
808 v
->flags
= vifc
->vifc_flags
;
810 v
->flags
|= VIFF_STATIC
;
811 v
->threshold
= vifc
->vifc_threshold
;
816 v
->link
= dev
->ifindex
;
817 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
))
818 v
->link
= dev_get_iflink(dev
);
820 /* And finish update writing critical data */
821 write_lock_bh(&mrt_lock
);
823 if (v
->flags
& VIFF_REGISTER
)
824 mrt
->mroute_reg_vif_num
= vifi
;
825 if (vifi
+1 > mrt
->maxvif
)
826 mrt
->maxvif
= vifi
+1;
827 write_unlock_bh(&mrt_lock
);
831 /* called with rcu_read_lock() */
832 static struct mfc_cache
*ipmr_cache_find(struct mr_table
*mrt
,
836 int line
= MFC_HASH(mcastgrp
, origin
);
839 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
) {
840 if (c
->mfc_origin
== origin
&& c
->mfc_mcastgrp
== mcastgrp
)
846 /* Look for a (*,*,oif) entry */
847 static struct mfc_cache
*ipmr_cache_find_any_parent(struct mr_table
*mrt
,
850 int line
= MFC_HASH(htonl(INADDR_ANY
), htonl(INADDR_ANY
));
853 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
)
854 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
855 c
->mfc_mcastgrp
== htonl(INADDR_ANY
) &&
856 c
->mfc_un
.res
.ttls
[vifi
] < 255)
862 /* Look for a (*,G) entry */
863 static struct mfc_cache
*ipmr_cache_find_any(struct mr_table
*mrt
,
864 __be32 mcastgrp
, int vifi
)
866 int line
= MFC_HASH(mcastgrp
, htonl(INADDR_ANY
));
867 struct mfc_cache
*c
, *proxy
;
869 if (mcastgrp
== htonl(INADDR_ANY
))
872 list_for_each_entry_rcu(c
, &mrt
->mfc_cache_array
[line
], list
)
873 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
874 c
->mfc_mcastgrp
== mcastgrp
) {
875 if (c
->mfc_un
.res
.ttls
[vifi
] < 255)
878 /* It's ok if the vifi is part of the static tree */
879 proxy
= ipmr_cache_find_any_parent(mrt
,
881 if (proxy
&& proxy
->mfc_un
.res
.ttls
[vifi
] < 255)
886 return ipmr_cache_find_any_parent(mrt
, vifi
);
889 /* Allocate a multicast cache entry */
890 static struct mfc_cache
*ipmr_cache_alloc(void)
892 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
895 c
->mfc_un
.res
.minvif
= MAXVIFS
;
899 static struct mfc_cache
*ipmr_cache_alloc_unres(void)
901 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
904 skb_queue_head_init(&c
->mfc_un
.unres
.unresolved
);
905 c
->mfc_un
.unres
.expires
= jiffies
+ 10*HZ
;
910 /* A cache entry has gone into a resolved state from queued */
911 static void ipmr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
912 struct mfc_cache
*uc
, struct mfc_cache
*c
)
917 /* Play the pending entries through our router */
918 while ((skb
= __skb_dequeue(&uc
->mfc_un
.unres
.unresolved
))) {
919 if (ip_hdr(skb
)->version
== 0) {
920 struct nlmsghdr
*nlh
= (struct nlmsghdr
*)skb_pull(skb
, sizeof(struct iphdr
));
922 if (__ipmr_fill_mroute(mrt
, skb
, c
, nlmsg_data(nlh
)) > 0) {
923 nlh
->nlmsg_len
= skb_tail_pointer(skb
) -
926 nlh
->nlmsg_type
= NLMSG_ERROR
;
927 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
928 skb_trim(skb
, nlh
->nlmsg_len
);
930 e
->error
= -EMSGSIZE
;
931 memset(&e
->msg
, 0, sizeof(e
->msg
));
934 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
936 ip_mr_forward(net
, mrt
, skb
, c
, 0);
941 /* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
942 * expects the following bizarre scheme.
944 * Called under mrt_lock.
946 static int ipmr_cache_report(struct mr_table
*mrt
,
947 struct sk_buff
*pkt
, vifi_t vifi
, int assert)
949 const int ihl
= ip_hdrlen(pkt
);
950 struct sock
*mroute_sk
;
951 struct igmphdr
*igmp
;
956 if (assert == IGMPMSG_WHOLEPKT
)
957 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
959 skb
= alloc_skb(128, GFP_ATOMIC
);
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
));
982 /* Copy the IP header */
983 skb_set_network_header(skb
, skb
->len
);
985 skb_copy_to_linear_data(skb
, pkt
->data
, ihl
);
986 /* Flag to the kernel this is a route add */
987 ip_hdr(skb
)->protocol
= 0;
988 msg
= (struct igmpmsg
*)skb_network_header(skb
);
990 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
992 igmp
= (struct igmphdr
*)skb_put(skb
, sizeof(struct igmphdr
));
994 msg
->im_msgtype
= assert;
996 ip_hdr(skb
)->tot_len
= htons(skb
->len
); /* Fix the length */
997 skb
->transport_header
= skb
->network_header
;
1001 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1008 /* Deliver to mrouted */
1009 ret
= sock_queue_rcv_skb(mroute_sk
, skb
);
1012 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1019 /* Queue a packet for resolution. It gets locked cache entry! */
1020 static int ipmr_cache_unresolved(struct mr_table
*mrt
, vifi_t vifi
,
1021 struct sk_buff
*skb
)
1025 struct mfc_cache
*c
;
1026 const struct iphdr
*iph
= ip_hdr(skb
);
1028 spin_lock_bh(&mfc_unres_lock
);
1029 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, list
) {
1030 if (c
->mfc_mcastgrp
== iph
->daddr
&&
1031 c
->mfc_origin
== iph
->saddr
) {
1038 /* Create a new entry if allowable */
1039 if (atomic_read(&mrt
->cache_resolve_queue_len
) >= 10 ||
1040 (c
= ipmr_cache_alloc_unres()) == NULL
) {
1041 spin_unlock_bh(&mfc_unres_lock
);
1047 /* Fill in the new cache entry */
1049 c
->mfc_origin
= iph
->saddr
;
1050 c
->mfc_mcastgrp
= iph
->daddr
;
1052 /* Reflect first query at mrouted. */
1053 err
= ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_NOCACHE
);
1055 /* If the report failed throw the cache entry
1058 spin_unlock_bh(&mfc_unres_lock
);
1065 atomic_inc(&mrt
->cache_resolve_queue_len
);
1066 list_add(&c
->list
, &mrt
->mfc_unres_queue
);
1067 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1069 if (atomic_read(&mrt
->cache_resolve_queue_len
) == 1)
1070 mod_timer(&mrt
->ipmr_expire_timer
, c
->mfc_un
.unres
.expires
);
1073 /* See if we can append the packet */
1074 if (c
->mfc_un
.unres
.unresolved
.qlen
> 3) {
1078 skb_queue_tail(&c
->mfc_un
.unres
.unresolved
, skb
);
1082 spin_unlock_bh(&mfc_unres_lock
);
1086 /* MFC cache manipulation by user space mroute daemon */
1088 static int ipmr_mfc_delete(struct mr_table
*mrt
, struct mfcctl
*mfc
, int parent
)
1091 struct mfc_cache
*c
, *next
;
1093 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1095 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[line
], list
) {
1096 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1097 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
&&
1098 (parent
== -1 || parent
== c
->mfc_parent
)) {
1099 list_del_rcu(&c
->list
);
1100 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1108 static int ipmr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1109 struct mfcctl
*mfc
, int mrtsock
, int parent
)
1113 struct mfc_cache
*uc
, *c
;
1115 if (mfc
->mfcc_parent
>= MAXVIFS
)
1118 line
= MFC_HASH(mfc
->mfcc_mcastgrp
.s_addr
, mfc
->mfcc_origin
.s_addr
);
1120 list_for_each_entry(c
, &mrt
->mfc_cache_array
[line
], list
) {
1121 if (c
->mfc_origin
== mfc
->mfcc_origin
.s_addr
&&
1122 c
->mfc_mcastgrp
== mfc
->mfcc_mcastgrp
.s_addr
&&
1123 (parent
== -1 || parent
== c
->mfc_parent
)) {
1130 write_lock_bh(&mrt_lock
);
1131 c
->mfc_parent
= mfc
->mfcc_parent
;
1132 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1134 c
->mfc_flags
|= MFC_STATIC
;
1135 write_unlock_bh(&mrt_lock
);
1136 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1140 if (mfc
->mfcc_mcastgrp
.s_addr
!= htonl(INADDR_ANY
) &&
1141 !ipv4_is_multicast(mfc
->mfcc_mcastgrp
.s_addr
))
1144 c
= ipmr_cache_alloc();
1148 c
->mfc_origin
= mfc
->mfcc_origin
.s_addr
;
1149 c
->mfc_mcastgrp
= mfc
->mfcc_mcastgrp
.s_addr
;
1150 c
->mfc_parent
= mfc
->mfcc_parent
;
1151 ipmr_update_thresholds(mrt
, c
, mfc
->mfcc_ttls
);
1153 c
->mfc_flags
|= MFC_STATIC
;
1155 list_add_rcu(&c
->list
, &mrt
->mfc_cache_array
[line
]);
1157 /* Check to see if we resolved a queued list. If so we
1158 * need to send on the frames and tidy up.
1161 spin_lock_bh(&mfc_unres_lock
);
1162 list_for_each_entry(uc
, &mrt
->mfc_unres_queue
, list
) {
1163 if (uc
->mfc_origin
== c
->mfc_origin
&&
1164 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
1165 list_del(&uc
->list
);
1166 atomic_dec(&mrt
->cache_resolve_queue_len
);
1171 if (list_empty(&mrt
->mfc_unres_queue
))
1172 del_timer(&mrt
->ipmr_expire_timer
);
1173 spin_unlock_bh(&mfc_unres_lock
);
1176 ipmr_cache_resolve(net
, mrt
, uc
, c
);
1177 ipmr_cache_free(uc
);
1179 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1183 /* Close the multicast socket, and clear the vif tables etc */
1184 static void mroute_clean_tables(struct mr_table
*mrt
, bool all
)
1188 struct mfc_cache
*c
, *next
;
1190 /* Shut down all active vif entries */
1191 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1192 if (!all
&& (mrt
->vif_table
[i
].flags
& VIFF_STATIC
))
1194 vif_delete(mrt
, i
, 0, &list
);
1196 unregister_netdevice_many(&list
);
1198 /* Wipe the cache */
1199 for (i
= 0; i
< MFC_LINES
; i
++) {
1200 list_for_each_entry_safe(c
, next
, &mrt
->mfc_cache_array
[i
], list
) {
1201 if (!all
&& (c
->mfc_flags
& MFC_STATIC
))
1203 list_del_rcu(&c
->list
);
1204 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1209 if (atomic_read(&mrt
->cache_resolve_queue_len
) != 0) {
1210 spin_lock_bh(&mfc_unres_lock
);
1211 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
1213 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1214 ipmr_destroy_unres(mrt
, c
);
1216 spin_unlock_bh(&mfc_unres_lock
);
1220 /* called from ip_ra_control(), before an RCU grace period,
1221 * we dont need to call synchronize_rcu() here
1223 static void mrtsock_destruct(struct sock
*sk
)
1225 struct net
*net
= sock_net(sk
);
1226 struct mr_table
*mrt
;
1229 ipmr_for_each_table(mrt
, net
) {
1230 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1231 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)--;
1232 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
,
1233 NETCONFA_IFINDEX_ALL
,
1234 net
->ipv4
.devconf_all
);
1235 RCU_INIT_POINTER(mrt
->mroute_sk
, NULL
);
1236 mroute_clean_tables(mrt
, false);
1242 /* Socket options and virtual interface manipulation. The whole
1243 * virtual interface system is a complete heap, but unfortunately
1244 * that's how BSD mrouted happens to think. Maybe one day with a proper
1245 * MOSPF/PIM router set up we can clean this up.
1248 int ip_mroute_setsockopt(struct sock
*sk
, int optname
, char __user
*optval
,
1249 unsigned int optlen
)
1251 struct net
*net
= sock_net(sk
);
1252 int val
, ret
= 0, parent
= 0;
1253 struct mr_table
*mrt
;
1258 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1260 if (sk
->sk_type
!= SOCK_RAW
||
1261 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
) {
1266 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1271 if (optname
!= MRT_INIT
) {
1272 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
) &&
1273 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
)) {
1281 if (optlen
!= sizeof(int)) {
1285 if (rtnl_dereference(mrt
->mroute_sk
)) {
1290 ret
= ip_ra_control(sk
, 1, mrtsock_destruct
);
1292 rcu_assign_pointer(mrt
->mroute_sk
, sk
);
1293 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)++;
1294 inet_netconf_notify_devconf(net
, NETCONFA_MC_FORWARDING
,
1295 NETCONFA_IFINDEX_ALL
,
1296 net
->ipv4
.devconf_all
);
1300 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
)) {
1303 /* We need to unlock here because mrtsock_destruct takes
1304 * care of rtnl itself and we can't change that due to
1305 * the IP_ROUTER_ALERT setsockopt which runs without it.
1308 ret
= ip_ra_control(sk
, 0, NULL
);
1314 if (optlen
!= sizeof(vif
)) {
1318 if (copy_from_user(&vif
, optval
, sizeof(vif
))) {
1322 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
);
1333 /* Manipulate the forwarding caches. These live
1334 * in a sort of kernel/user symbiosis.
1339 case MRT_ADD_MFC_PROXY
:
1340 case MRT_DEL_MFC_PROXY
:
1341 if (optlen
!= sizeof(mfc
)) {
1345 if (copy_from_user(&mfc
, optval
, sizeof(mfc
))) {
1350 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
),
1358 /* Control PIM assert. */
1360 if (optlen
!= sizeof(val
)) {
1364 if (get_user(val
, (int __user
*)optval
)) {
1368 mrt
->mroute_do_assert
= val
;
1371 if (!ipmr_pimsm_enabled()) {
1375 if (optlen
!= sizeof(val
)) {
1379 if (get_user(val
, (int __user
*)optval
)) {
1385 if (val
!= mrt
->mroute_do_pim
) {
1386 mrt
->mroute_do_pim
= val
;
1387 mrt
->mroute_do_assert
= val
;
1391 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES
)) {
1395 if (optlen
!= sizeof(uval
)) {
1399 if (get_user(uval
, (u32 __user
*)optval
)) {
1404 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1407 mrt
= ipmr_new_table(net
, uval
);
1411 raw_sk(sk
)->ipmr_table
= uval
;
1414 /* Spurious command, or MRT_VERSION which you cannot set. */
1424 /* Getsock opt support for the multicast routing system. */
1425 int ip_mroute_getsockopt(struct sock
*sk
, int optname
, char __user
*optval
, int __user
*optlen
)
1429 struct net
*net
= sock_net(sk
);
1430 struct mr_table
*mrt
;
1432 if (sk
->sk_type
!= SOCK_RAW
||
1433 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1436 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1445 if (!ipmr_pimsm_enabled())
1446 return -ENOPROTOOPT
;
1447 val
= mrt
->mroute_do_pim
;
1450 val
= mrt
->mroute_do_assert
;
1453 return -ENOPROTOOPT
;
1456 if (get_user(olr
, optlen
))
1458 olr
= min_t(unsigned int, olr
, sizeof(int));
1461 if (put_user(olr
, optlen
))
1463 if (copy_to_user(optval
, &val
, olr
))
1468 /* The IP multicast ioctl support routines. */
1469 int ipmr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1471 struct sioc_sg_req sr
;
1472 struct sioc_vif_req vr
;
1473 struct vif_device
*vif
;
1474 struct mfc_cache
*c
;
1475 struct net
*net
= sock_net(sk
);
1476 struct mr_table
*mrt
;
1478 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1484 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1486 if (vr
.vifi
>= mrt
->maxvif
)
1488 read_lock(&mrt_lock
);
1489 vif
= &mrt
->vif_table
[vr
.vifi
];
1490 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1491 vr
.icount
= vif
->pkt_in
;
1492 vr
.ocount
= vif
->pkt_out
;
1493 vr
.ibytes
= vif
->bytes_in
;
1494 vr
.obytes
= vif
->bytes_out
;
1495 read_unlock(&mrt_lock
);
1497 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1501 read_unlock(&mrt_lock
);
1502 return -EADDRNOTAVAIL
;
1504 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1508 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1510 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1511 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1512 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1515 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1520 return -EADDRNOTAVAIL
;
1522 return -ENOIOCTLCMD
;
1526 #ifdef CONFIG_COMPAT
1527 struct compat_sioc_sg_req
{
1530 compat_ulong_t pktcnt
;
1531 compat_ulong_t bytecnt
;
1532 compat_ulong_t wrong_if
;
1535 struct compat_sioc_vif_req
{
1536 vifi_t vifi
; /* Which iface */
1537 compat_ulong_t icount
;
1538 compat_ulong_t ocount
;
1539 compat_ulong_t ibytes
;
1540 compat_ulong_t obytes
;
1543 int ipmr_compat_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
1545 struct compat_sioc_sg_req sr
;
1546 struct compat_sioc_vif_req vr
;
1547 struct vif_device
*vif
;
1548 struct mfc_cache
*c
;
1549 struct net
*net
= sock_net(sk
);
1550 struct mr_table
*mrt
;
1552 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1558 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1560 if (vr
.vifi
>= mrt
->maxvif
)
1562 read_lock(&mrt_lock
);
1563 vif
= &mrt
->vif_table
[vr
.vifi
];
1564 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1565 vr
.icount
= vif
->pkt_in
;
1566 vr
.ocount
= vif
->pkt_out
;
1567 vr
.ibytes
= vif
->bytes_in
;
1568 vr
.obytes
= vif
->bytes_out
;
1569 read_unlock(&mrt_lock
);
1571 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1575 read_unlock(&mrt_lock
);
1576 return -EADDRNOTAVAIL
;
1578 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1582 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1584 sr
.pktcnt
= c
->mfc_un
.res
.pkt
;
1585 sr
.bytecnt
= c
->mfc_un
.res
.bytes
;
1586 sr
.wrong_if
= c
->mfc_un
.res
.wrong_if
;
1589 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1594 return -EADDRNOTAVAIL
;
1596 return -ENOIOCTLCMD
;
1601 static int ipmr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1603 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1604 struct net
*net
= dev_net(dev
);
1605 struct mr_table
*mrt
;
1606 struct vif_device
*v
;
1609 if (event
!= NETDEV_UNREGISTER
)
1612 ipmr_for_each_table(mrt
, net
) {
1613 v
= &mrt
->vif_table
[0];
1614 for (ct
= 0; ct
< mrt
->maxvif
; ct
++, v
++) {
1616 vif_delete(mrt
, ct
, 1, NULL
);
1622 static struct notifier_block ip_mr_notifier
= {
1623 .notifier_call
= ipmr_device_event
,
1626 /* Encapsulate a packet by attaching a valid IPIP header to it.
1627 * This avoids tunnel drivers and other mess and gives us the speed so
1628 * important for multicast video.
1630 static void ip_encap(struct net
*net
, struct sk_buff
*skb
,
1631 __be32 saddr
, __be32 daddr
)
1634 const struct iphdr
*old_iph
= ip_hdr(skb
);
1636 skb_push(skb
, sizeof(struct iphdr
));
1637 skb
->transport_header
= skb
->network_header
;
1638 skb_reset_network_header(skb
);
1642 iph
->tos
= old_iph
->tos
;
1643 iph
->ttl
= old_iph
->ttl
;
1647 iph
->protocol
= IPPROTO_IPIP
;
1649 iph
->tot_len
= htons(skb
->len
);
1650 ip_select_ident(net
, skb
, NULL
);
1653 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1657 static inline int ipmr_forward_finish(struct net
*net
, struct sock
*sk
,
1658 struct sk_buff
*skb
)
1660 struct ip_options
*opt
= &(IPCB(skb
)->opt
);
1662 IP_INC_STATS(net
, IPSTATS_MIB_OUTFORWDATAGRAMS
);
1663 IP_ADD_STATS(net
, IPSTATS_MIB_OUTOCTETS
, skb
->len
);
1665 if (unlikely(opt
->optlen
))
1666 ip_forward_options(skb
);
1668 return dst_output(net
, sk
, skb
);
1671 /* Processing handlers for ipmr_forward */
1673 static void ipmr_queue_xmit(struct net
*net
, struct mr_table
*mrt
,
1674 struct sk_buff
*skb
, struct mfc_cache
*c
, int vifi
)
1676 const struct iphdr
*iph
= ip_hdr(skb
);
1677 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1678 struct net_device
*dev
;
1686 if (vif
->flags
& VIFF_REGISTER
) {
1688 vif
->bytes_out
+= skb
->len
;
1689 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1690 vif
->dev
->stats
.tx_packets
++;
1691 ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_WHOLEPKT
);
1695 if (vif
->flags
& VIFF_TUNNEL
) {
1696 rt
= ip_route_output_ports(net
, &fl4
, NULL
,
1697 vif
->remote
, vif
->local
,
1700 RT_TOS(iph
->tos
), vif
->link
);
1703 encap
= sizeof(struct iphdr
);
1705 rt
= ip_route_output_ports(net
, &fl4
, NULL
, iph
->daddr
, 0,
1708 RT_TOS(iph
->tos
), vif
->link
);
1715 if (skb
->len
+encap
> dst_mtu(&rt
->dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1716 /* Do not fragment multicasts. Alas, IPv4 does not
1717 * allow to send ICMP, so that packets will disappear
1720 IP_INC_STATS(net
, IPSTATS_MIB_FRAGFAILS
);
1725 encap
+= LL_RESERVED_SPACE(dev
) + rt
->dst
.header_len
;
1727 if (skb_cow(skb
, encap
)) {
1733 vif
->bytes_out
+= skb
->len
;
1736 skb_dst_set(skb
, &rt
->dst
);
1737 ip_decrease_ttl(ip_hdr(skb
));
1739 /* FIXME: forward and output firewalls used to be called here.
1740 * What do we do with netfilter? -- RR
1742 if (vif
->flags
& VIFF_TUNNEL
) {
1743 ip_encap(net
, skb
, vif
->local
, vif
->remote
);
1744 /* FIXME: extra output firewall step used to be here. --RR */
1745 vif
->dev
->stats
.tx_packets
++;
1746 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1749 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1751 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1752 * not only before forwarding, but after forwarding on all output
1753 * interfaces. It is clear, if mrouter runs a multicasting
1754 * program, it should receive packets not depending to what interface
1755 * program is joined.
1756 * If we will not make it, the program will have to join on all
1757 * interfaces. On the other hand, multihoming host (or router, but
1758 * not mrouter) cannot join to more than one interface - it will
1759 * result in receiving multiple packets.
1761 NF_HOOK(NFPROTO_IPV4
, NF_INET_FORWARD
,
1762 net
, NULL
, skb
, skb
->dev
, dev
,
1763 ipmr_forward_finish
);
1770 static int ipmr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
1774 for (ct
= mrt
->maxvif
-1; ct
>= 0; ct
--) {
1775 if (mrt
->vif_table
[ct
].dev
== dev
)
1781 /* "local" means that we should preserve one skb (for local delivery) */
1782 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
1783 struct sk_buff
*skb
, struct mfc_cache
*cache
,
1788 int true_vifi
= ipmr_find_vif(mrt
, skb
->dev
);
1790 vif
= cache
->mfc_parent
;
1791 cache
->mfc_un
.res
.pkt
++;
1792 cache
->mfc_un
.res
.bytes
+= skb
->len
;
1794 if (cache
->mfc_origin
== htonl(INADDR_ANY
) && true_vifi
>= 0) {
1795 struct mfc_cache
*cache_proxy
;
1797 /* For an (*,G) entry, we only check that the incomming
1798 * interface is part of the static tree.
1800 cache_proxy
= ipmr_cache_find_any_parent(mrt
, vif
);
1802 cache_proxy
->mfc_un
.res
.ttls
[true_vifi
] < 255)
1806 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1807 if (mrt
->vif_table
[vif
].dev
!= skb
->dev
) {
1808 if (rt_is_output_route(skb_rtable(skb
))) {
1809 /* It is our own packet, looped back.
1810 * Very complicated situation...
1812 * The best workaround until routing daemons will be
1813 * fixed is not to redistribute packet, if it was
1814 * send through wrong interface. It means, that
1815 * multicast applications WILL NOT work for
1816 * (S,G), which have default multicast route pointing
1817 * to wrong oif. In any case, it is not a good
1818 * idea to use multicasting applications on router.
1823 cache
->mfc_un
.res
.wrong_if
++;
1825 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
1826 /* pimsm uses asserts, when switching from RPT to SPT,
1827 * so that we cannot check that packet arrived on an oif.
1828 * It is bad, but otherwise we would need to move pretty
1829 * large chunk of pimd to kernel. Ough... --ANK
1831 (mrt
->mroute_do_pim
||
1832 cache
->mfc_un
.res
.ttls
[true_vifi
] < 255) &&
1834 cache
->mfc_un
.res
.last_assert
+ MFC_ASSERT_THRESH
)) {
1835 cache
->mfc_un
.res
.last_assert
= jiffies
;
1836 ipmr_cache_report(mrt
, skb
, true_vifi
, IGMPMSG_WRONGVIF
);
1842 mrt
->vif_table
[vif
].pkt_in
++;
1843 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
1845 /* Forward the frame */
1846 if (cache
->mfc_origin
== htonl(INADDR_ANY
) &&
1847 cache
->mfc_mcastgrp
== htonl(INADDR_ANY
)) {
1848 if (true_vifi
>= 0 &&
1849 true_vifi
!= cache
->mfc_parent
&&
1851 cache
->mfc_un
.res
.ttls
[cache
->mfc_parent
]) {
1852 /* It's an (*,*) entry and the packet is not coming from
1853 * the upstream: forward the packet to the upstream
1856 psend
= cache
->mfc_parent
;
1861 for (ct
= cache
->mfc_un
.res
.maxvif
- 1;
1862 ct
>= cache
->mfc_un
.res
.minvif
; ct
--) {
1863 /* For (*,G) entry, don't forward to the incoming interface */
1864 if ((cache
->mfc_origin
!= htonl(INADDR_ANY
) ||
1866 ip_hdr(skb
)->ttl
> cache
->mfc_un
.res
.ttls
[ct
]) {
1868 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1871 ipmr_queue_xmit(net
, mrt
, skb2
, cache
,
1880 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1883 ipmr_queue_xmit(net
, mrt
, skb2
, cache
, psend
);
1885 ipmr_queue_xmit(net
, mrt
, skb
, cache
, psend
);
1895 static struct mr_table
*ipmr_rt_fib_lookup(struct net
*net
, struct sk_buff
*skb
)
1897 struct rtable
*rt
= skb_rtable(skb
);
1898 struct iphdr
*iph
= ip_hdr(skb
);
1899 struct flowi4 fl4
= {
1900 .daddr
= iph
->daddr
,
1901 .saddr
= iph
->saddr
,
1902 .flowi4_tos
= RT_TOS(iph
->tos
),
1903 .flowi4_oif
= (rt_is_output_route(rt
) ?
1904 skb
->dev
->ifindex
: 0),
1905 .flowi4_iif
= (rt_is_output_route(rt
) ?
1908 .flowi4_mark
= skb
->mark
,
1910 struct mr_table
*mrt
;
1913 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
1915 return ERR_PTR(err
);
1919 /* Multicast packets for forwarding arrive here
1920 * Called with rcu_read_lock();
1922 int ip_mr_input(struct sk_buff
*skb
)
1924 struct mfc_cache
*cache
;
1925 struct net
*net
= dev_net(skb
->dev
);
1926 int local
= skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
;
1927 struct mr_table
*mrt
;
1929 /* Packet is looped back after forward, it should not be
1930 * forwarded second time, but still can be delivered locally.
1932 if (IPCB(skb
)->flags
& IPSKB_FORWARDED
)
1935 mrt
= ipmr_rt_fib_lookup(net
, skb
);
1938 return PTR_ERR(mrt
);
1941 if (IPCB(skb
)->opt
.router_alert
) {
1942 if (ip_call_ra_chain(skb
))
1944 } else if (ip_hdr(skb
)->protocol
== IPPROTO_IGMP
) {
1945 /* IGMPv1 (and broken IGMPv2 implementations sort of
1946 * Cisco IOS <= 11.2(8)) do not put router alert
1947 * option to IGMP packets destined to routable
1948 * groups. It is very bad, because it means
1949 * that we can forward NO IGMP messages.
1951 struct sock
*mroute_sk
;
1953 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1956 raw_rcv(mroute_sk
, skb
);
1962 /* already under rcu_read_lock() */
1963 cache
= ipmr_cache_find(mrt
, ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
1965 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
1968 cache
= ipmr_cache_find_any(mrt
, ip_hdr(skb
)->daddr
,
1972 /* No usable cache entry */
1977 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
1978 ip_local_deliver(skb
);
1984 read_lock(&mrt_lock
);
1985 vif
= ipmr_find_vif(mrt
, skb
->dev
);
1987 int err2
= ipmr_cache_unresolved(mrt
, vif
, skb
);
1988 read_unlock(&mrt_lock
);
1992 read_unlock(&mrt_lock
);
1997 read_lock(&mrt_lock
);
1998 ip_mr_forward(net
, mrt
, skb
, cache
, local
);
1999 read_unlock(&mrt_lock
);
2002 return ip_local_deliver(skb
);
2008 return ip_local_deliver(skb
);
2013 #ifdef CONFIG_IP_PIMSM_V1
2014 /* Handle IGMP messages of PIMv1 */
2015 int pim_rcv_v1(struct sk_buff
*skb
)
2017 struct igmphdr
*pim
;
2018 struct net
*net
= dev_net(skb
->dev
);
2019 struct mr_table
*mrt
;
2021 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2024 pim
= igmp_hdr(skb
);
2026 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2029 if (!mrt
->mroute_do_pim
||
2030 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
2033 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2041 #ifdef CONFIG_IP_PIMSM_V2
2042 static int pim_rcv(struct sk_buff
*skb
)
2044 struct pimreghdr
*pim
;
2045 struct net
*net
= dev_net(skb
->dev
);
2046 struct mr_table
*mrt
;
2048 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2051 pim
= (struct pimreghdr
*)skb_transport_header(skb
);
2052 if (pim
->type
!= ((PIM_VERSION
<< 4) | (PIM_REGISTER
)) ||
2053 (pim
->flags
& PIM_NULL_REGISTER
) ||
2054 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
2055 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
2058 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2061 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2069 static int __ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2070 struct mfc_cache
*c
, struct rtmsg
*rtm
)
2073 struct rtnexthop
*nhp
;
2074 struct nlattr
*mp_attr
;
2075 struct rta_mfc_stats mfcs
;
2077 /* If cache is unresolved, don't try to parse IIF and OIF */
2078 if (c
->mfc_parent
>= MAXVIFS
)
2081 if (VIF_EXISTS(mrt
, c
->mfc_parent
) &&
2082 nla_put_u32(skb
, RTA_IIF
, mrt
->vif_table
[c
->mfc_parent
].dev
->ifindex
) < 0)
2085 if (!(mp_attr
= nla_nest_start(skb
, RTA_MULTIPATH
)))
2088 for (ct
= c
->mfc_un
.res
.minvif
; ct
< c
->mfc_un
.res
.maxvif
; ct
++) {
2089 if (VIF_EXISTS(mrt
, ct
) && c
->mfc_un
.res
.ttls
[ct
] < 255) {
2090 if (!(nhp
= nla_reserve_nohdr(skb
, sizeof(*nhp
)))) {
2091 nla_nest_cancel(skb
, mp_attr
);
2095 nhp
->rtnh_flags
= 0;
2096 nhp
->rtnh_hops
= c
->mfc_un
.res
.ttls
[ct
];
2097 nhp
->rtnh_ifindex
= mrt
->vif_table
[ct
].dev
->ifindex
;
2098 nhp
->rtnh_len
= sizeof(*nhp
);
2102 nla_nest_end(skb
, mp_attr
);
2104 mfcs
.mfcs_packets
= c
->mfc_un
.res
.pkt
;
2105 mfcs
.mfcs_bytes
= c
->mfc_un
.res
.bytes
;
2106 mfcs
.mfcs_wrong_if
= c
->mfc_un
.res
.wrong_if
;
2107 if (nla_put(skb
, RTA_MFC_STATS
, sizeof(mfcs
), &mfcs
) < 0)
2110 rtm
->rtm_type
= RTN_MULTICAST
;
2114 int ipmr_get_route(struct net
*net
, struct sk_buff
*skb
,
2115 __be32 saddr
, __be32 daddr
,
2116 struct rtmsg
*rtm
, int nowait
)
2118 struct mfc_cache
*cache
;
2119 struct mr_table
*mrt
;
2122 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2127 cache
= ipmr_cache_find(mrt
, saddr
, daddr
);
2128 if (!cache
&& skb
->dev
) {
2129 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
2132 cache
= ipmr_cache_find_any(mrt
, daddr
, vif
);
2135 struct sk_buff
*skb2
;
2137 struct net_device
*dev
;
2146 read_lock(&mrt_lock
);
2148 vif
= ipmr_find_vif(mrt
, dev
);
2150 read_unlock(&mrt_lock
);
2154 skb2
= skb_clone(skb
, GFP_ATOMIC
);
2156 read_unlock(&mrt_lock
);
2161 skb_push(skb2
, sizeof(struct iphdr
));
2162 skb_reset_network_header(skb2
);
2164 iph
->ihl
= sizeof(struct iphdr
) >> 2;
2168 err
= ipmr_cache_unresolved(mrt
, vif
, skb2
);
2169 read_unlock(&mrt_lock
);
2174 read_lock(&mrt_lock
);
2175 err
= __ipmr_fill_mroute(mrt
, skb
, cache
, rtm
);
2176 read_unlock(&mrt_lock
);
2181 static int ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2182 u32 portid
, u32 seq
, struct mfc_cache
*c
, int cmd
,
2185 struct nlmsghdr
*nlh
;
2189 nlh
= nlmsg_put(skb
, portid
, seq
, cmd
, sizeof(*rtm
), flags
);
2193 rtm
= nlmsg_data(nlh
);
2194 rtm
->rtm_family
= RTNL_FAMILY_IPMR
;
2195 rtm
->rtm_dst_len
= 32;
2196 rtm
->rtm_src_len
= 32;
2198 rtm
->rtm_table
= mrt
->id
;
2199 if (nla_put_u32(skb
, RTA_TABLE
, mrt
->id
))
2200 goto nla_put_failure
;
2201 rtm
->rtm_type
= RTN_MULTICAST
;
2202 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2203 if (c
->mfc_flags
& MFC_STATIC
)
2204 rtm
->rtm_protocol
= RTPROT_STATIC
;
2206 rtm
->rtm_protocol
= RTPROT_MROUTED
;
2209 if (nla_put_in_addr(skb
, RTA_SRC
, c
->mfc_origin
) ||
2210 nla_put_in_addr(skb
, RTA_DST
, c
->mfc_mcastgrp
))
2211 goto nla_put_failure
;
2212 err
= __ipmr_fill_mroute(mrt
, skb
, c
, rtm
);
2213 /* do not break the dump if cache is unresolved */
2214 if (err
< 0 && err
!= -ENOENT
)
2215 goto nla_put_failure
;
2217 nlmsg_end(skb
, nlh
);
2221 nlmsg_cancel(skb
, nlh
);
2225 static size_t mroute_msgsize(bool unresolved
, int maxvif
)
2228 NLMSG_ALIGN(sizeof(struct rtmsg
))
2229 + nla_total_size(4) /* RTA_TABLE */
2230 + nla_total_size(4) /* RTA_SRC */
2231 + nla_total_size(4) /* RTA_DST */
2236 + nla_total_size(4) /* RTA_IIF */
2237 + nla_total_size(0) /* RTA_MULTIPATH */
2238 + maxvif
* NLA_ALIGN(sizeof(struct rtnexthop
))
2240 + nla_total_size(sizeof(struct rta_mfc_stats
))
2246 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
2249 struct net
*net
= read_pnet(&mrt
->net
);
2250 struct sk_buff
*skb
;
2253 skb
= nlmsg_new(mroute_msgsize(mfc
->mfc_parent
>= MAXVIFS
, mrt
->maxvif
),
2258 err
= ipmr_fill_mroute(mrt
, skb
, 0, 0, mfc
, cmd
, 0);
2262 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_MROUTE
, NULL
, GFP_ATOMIC
);
2268 rtnl_set_sk_err(net
, RTNLGRP_IPV4_MROUTE
, err
);
2271 static int ipmr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2273 struct net
*net
= sock_net(skb
->sk
);
2274 struct mr_table
*mrt
;
2275 struct mfc_cache
*mfc
;
2276 unsigned int t
= 0, s_t
;
2277 unsigned int h
= 0, s_h
;
2278 unsigned int e
= 0, s_e
;
2285 ipmr_for_each_table(mrt
, net
) {
2290 for (h
= s_h
; h
< MFC_LINES
; h
++) {
2291 list_for_each_entry_rcu(mfc
, &mrt
->mfc_cache_array
[h
], list
) {
2294 if (ipmr_fill_mroute(mrt
, skb
,
2295 NETLINK_CB(cb
->skb
).portid
,
2305 spin_lock_bh(&mfc_unres_lock
);
2306 list_for_each_entry(mfc
, &mrt
->mfc_unres_queue
, list
) {
2309 if (ipmr_fill_mroute(mrt
, skb
,
2310 NETLINK_CB(cb
->skb
).portid
,
2314 spin_unlock_bh(&mfc_unres_lock
);
2320 spin_unlock_bh(&mfc_unres_lock
);
2336 static const struct nla_policy rtm_ipmr_policy
[RTA_MAX
+ 1] = {
2337 [RTA_SRC
] = { .type
= NLA_U32
},
2338 [RTA_DST
] = { .type
= NLA_U32
},
2339 [RTA_IIF
] = { .type
= NLA_U32
},
2340 [RTA_TABLE
] = { .type
= NLA_U32
},
2341 [RTA_MULTIPATH
] = { .len
= sizeof(struct rtnexthop
) },
2344 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol
)
2346 switch (rtm_protocol
) {
2348 case RTPROT_MROUTED
:
2354 static int ipmr_nla_get_ttls(const struct nlattr
*nla
, struct mfcctl
*mfcc
)
2356 struct rtnexthop
*rtnh
= nla_data(nla
);
2357 int remaining
= nla_len(nla
), vifi
= 0;
2359 while (rtnh_ok(rtnh
, remaining
)) {
2360 mfcc
->mfcc_ttls
[vifi
] = rtnh
->rtnh_hops
;
2361 if (++vifi
== MAXVIFS
)
2363 rtnh
= rtnh_next(rtnh
, &remaining
);
2366 return remaining
> 0 ? -EINVAL
: vifi
;
2369 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2370 static int rtm_to_ipmr_mfcc(struct net
*net
, struct nlmsghdr
*nlh
,
2371 struct mfcctl
*mfcc
, int *mrtsock
,
2372 struct mr_table
**mrtret
)
2374 struct net_device
*dev
= NULL
;
2375 u32 tblid
= RT_TABLE_DEFAULT
;
2376 struct mr_table
*mrt
;
2377 struct nlattr
*attr
;
2381 ret
= nlmsg_validate(nlh
, sizeof(*rtm
), RTA_MAX
, rtm_ipmr_policy
);
2384 rtm
= nlmsg_data(nlh
);
2387 if (rtm
->rtm_family
!= RTNL_FAMILY_IPMR
|| rtm
->rtm_dst_len
!= 32 ||
2388 rtm
->rtm_type
!= RTN_MULTICAST
||
2389 rtm
->rtm_scope
!= RT_SCOPE_UNIVERSE
||
2390 !ipmr_rtm_validate_proto(rtm
->rtm_protocol
))
2393 memset(mfcc
, 0, sizeof(*mfcc
));
2394 mfcc
->mfcc_parent
= -1;
2396 nlmsg_for_each_attr(attr
, nlh
, sizeof(struct rtmsg
), rem
) {
2397 switch (nla_type(attr
)) {
2399 mfcc
->mfcc_origin
.s_addr
= nla_get_be32(attr
);
2402 mfcc
->mfcc_mcastgrp
.s_addr
= nla_get_be32(attr
);
2405 dev
= __dev_get_by_index(net
, nla_get_u32(attr
));
2412 if (ipmr_nla_get_ttls(attr
, mfcc
) < 0) {
2421 tblid
= nla_get_u32(attr
);
2425 mrt
= ipmr_get_table(net
, tblid
);
2431 *mrtsock
= rtm
->rtm_protocol
== RTPROT_MROUTED
? 1 : 0;
2433 mfcc
->mfcc_parent
= ipmr_find_vif(mrt
, dev
);
2439 /* takes care of both newroute and delroute */
2440 static int ipmr_rtm_route(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
2442 struct net
*net
= sock_net(skb
->sk
);
2443 int ret
, mrtsock
, parent
;
2444 struct mr_table
*tbl
;
2449 ret
= rtm_to_ipmr_mfcc(net
, nlh
, &mfcc
, &mrtsock
, &tbl
);
2453 parent
= ret
? mfcc
.mfcc_parent
: -1;
2454 if (nlh
->nlmsg_type
== RTM_NEWROUTE
)
2455 return ipmr_mfc_add(net
, tbl
, &mfcc
, mrtsock
, parent
);
2457 return ipmr_mfc_delete(tbl
, &mfcc
, parent
);
2460 #ifdef CONFIG_PROC_FS
2461 /* The /proc interfaces to multicast routing :
2462 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2464 struct ipmr_vif_iter
{
2465 struct seq_net_private p
;
2466 struct mr_table
*mrt
;
2470 static struct vif_device
*ipmr_vif_seq_idx(struct net
*net
,
2471 struct ipmr_vif_iter
*iter
,
2474 struct mr_table
*mrt
= iter
->mrt
;
2476 for (iter
->ct
= 0; iter
->ct
< mrt
->maxvif
; ++iter
->ct
) {
2477 if (!VIF_EXISTS(mrt
, iter
->ct
))
2480 return &mrt
->vif_table
[iter
->ct
];
2485 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2486 __acquires(mrt_lock
)
2488 struct ipmr_vif_iter
*iter
= seq
->private;
2489 struct net
*net
= seq_file_net(seq
);
2490 struct mr_table
*mrt
;
2492 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2494 return ERR_PTR(-ENOENT
);
2498 read_lock(&mrt_lock
);
2499 return *pos
? ipmr_vif_seq_idx(net
, seq
->private, *pos
- 1)
2503 static void *ipmr_vif_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2505 struct ipmr_vif_iter
*iter
= seq
->private;
2506 struct net
*net
= seq_file_net(seq
);
2507 struct mr_table
*mrt
= iter
->mrt
;
2510 if (v
== SEQ_START_TOKEN
)
2511 return ipmr_vif_seq_idx(net
, iter
, 0);
2513 while (++iter
->ct
< mrt
->maxvif
) {
2514 if (!VIF_EXISTS(mrt
, iter
->ct
))
2516 return &mrt
->vif_table
[iter
->ct
];
2521 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
2522 __releases(mrt_lock
)
2524 read_unlock(&mrt_lock
);
2527 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
2529 struct ipmr_vif_iter
*iter
= seq
->private;
2530 struct mr_table
*mrt
= iter
->mrt
;
2532 if (v
== SEQ_START_TOKEN
) {
2534 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2536 const struct vif_device
*vif
= v
;
2537 const char *name
= vif
->dev
? vif
->dev
->name
: "none";
2540 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2541 vif
- mrt
->vif_table
,
2542 name
, vif
->bytes_in
, vif
->pkt_in
,
2543 vif
->bytes_out
, vif
->pkt_out
,
2544 vif
->flags
, vif
->local
, vif
->remote
);
2549 static const struct seq_operations ipmr_vif_seq_ops
= {
2550 .start
= ipmr_vif_seq_start
,
2551 .next
= ipmr_vif_seq_next
,
2552 .stop
= ipmr_vif_seq_stop
,
2553 .show
= ipmr_vif_seq_show
,
2556 static int ipmr_vif_open(struct inode
*inode
, struct file
*file
)
2558 return seq_open_net(inode
, file
, &ipmr_vif_seq_ops
,
2559 sizeof(struct ipmr_vif_iter
));
2562 static const struct file_operations ipmr_vif_fops
= {
2563 .owner
= THIS_MODULE
,
2564 .open
= ipmr_vif_open
,
2566 .llseek
= seq_lseek
,
2567 .release
= seq_release_net
,
2570 struct ipmr_mfc_iter
{
2571 struct seq_net_private p
;
2572 struct mr_table
*mrt
;
2573 struct list_head
*cache
;
2578 static struct mfc_cache
*ipmr_mfc_seq_idx(struct net
*net
,
2579 struct ipmr_mfc_iter
*it
, loff_t pos
)
2581 struct mr_table
*mrt
= it
->mrt
;
2582 struct mfc_cache
*mfc
;
2585 for (it
->ct
= 0; it
->ct
< MFC_LINES
; it
->ct
++) {
2586 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2587 list_for_each_entry_rcu(mfc
, it
->cache
, list
)
2593 spin_lock_bh(&mfc_unres_lock
);
2594 it
->cache
= &mrt
->mfc_unres_queue
;
2595 list_for_each_entry(mfc
, it
->cache
, list
)
2598 spin_unlock_bh(&mfc_unres_lock
);
2605 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2607 struct ipmr_mfc_iter
*it
= seq
->private;
2608 struct net
*net
= seq_file_net(seq
);
2609 struct mr_table
*mrt
;
2611 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2613 return ERR_PTR(-ENOENT
);
2618 return *pos
? ipmr_mfc_seq_idx(net
, seq
->private, *pos
- 1)
2622 static void *ipmr_mfc_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2624 struct mfc_cache
*mfc
= v
;
2625 struct ipmr_mfc_iter
*it
= seq
->private;
2626 struct net
*net
= seq_file_net(seq
);
2627 struct mr_table
*mrt
= it
->mrt
;
2631 if (v
== SEQ_START_TOKEN
)
2632 return ipmr_mfc_seq_idx(net
, seq
->private, 0);
2634 if (mfc
->list
.next
!= it
->cache
)
2635 return list_entry(mfc
->list
.next
, struct mfc_cache
, list
);
2637 if (it
->cache
== &mrt
->mfc_unres_queue
)
2640 BUG_ON(it
->cache
!= &mrt
->mfc_cache_array
[it
->ct
]);
2642 while (++it
->ct
< MFC_LINES
) {
2643 it
->cache
= &mrt
->mfc_cache_array
[it
->ct
];
2644 if (list_empty(it
->cache
))
2646 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2649 /* exhausted cache_array, show unresolved */
2651 it
->cache
= &mrt
->mfc_unres_queue
;
2654 spin_lock_bh(&mfc_unres_lock
);
2655 if (!list_empty(it
->cache
))
2656 return list_first_entry(it
->cache
, struct mfc_cache
, list
);
2659 spin_unlock_bh(&mfc_unres_lock
);
2665 static void ipmr_mfc_seq_stop(struct seq_file
*seq
, void *v
)
2667 struct ipmr_mfc_iter
*it
= seq
->private;
2668 struct mr_table
*mrt
= it
->mrt
;
2670 if (it
->cache
== &mrt
->mfc_unres_queue
)
2671 spin_unlock_bh(&mfc_unres_lock
);
2672 else if (it
->cache
== &mrt
->mfc_cache_array
[it
->ct
])
2676 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
2680 if (v
== SEQ_START_TOKEN
) {
2682 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2684 const struct mfc_cache
*mfc
= v
;
2685 const struct ipmr_mfc_iter
*it
= seq
->private;
2686 const struct mr_table
*mrt
= it
->mrt
;
2688 seq_printf(seq
, "%08X %08X %-3hd",
2689 (__force u32
) mfc
->mfc_mcastgrp
,
2690 (__force u32
) mfc
->mfc_origin
,
2693 if (it
->cache
!= &mrt
->mfc_unres_queue
) {
2694 seq_printf(seq
, " %8lu %8lu %8lu",
2695 mfc
->mfc_un
.res
.pkt
,
2696 mfc
->mfc_un
.res
.bytes
,
2697 mfc
->mfc_un
.res
.wrong_if
);
2698 for (n
= mfc
->mfc_un
.res
.minvif
;
2699 n
< mfc
->mfc_un
.res
.maxvif
; n
++) {
2700 if (VIF_EXISTS(mrt
, n
) &&
2701 mfc
->mfc_un
.res
.ttls
[n
] < 255)
2704 n
, mfc
->mfc_un
.res
.ttls
[n
]);
2707 /* unresolved mfc_caches don't contain
2708 * pkt, bytes and wrong_if values
2710 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2712 seq_putc(seq
, '\n');
2717 static const struct seq_operations ipmr_mfc_seq_ops
= {
2718 .start
= ipmr_mfc_seq_start
,
2719 .next
= ipmr_mfc_seq_next
,
2720 .stop
= ipmr_mfc_seq_stop
,
2721 .show
= ipmr_mfc_seq_show
,
2724 static int ipmr_mfc_open(struct inode
*inode
, struct file
*file
)
2726 return seq_open_net(inode
, file
, &ipmr_mfc_seq_ops
,
2727 sizeof(struct ipmr_mfc_iter
));
2730 static const struct file_operations ipmr_mfc_fops
= {
2731 .owner
= THIS_MODULE
,
2732 .open
= ipmr_mfc_open
,
2734 .llseek
= seq_lseek
,
2735 .release
= seq_release_net
,
2739 #ifdef CONFIG_IP_PIMSM_V2
2740 static const struct net_protocol pim_protocol
= {
2746 /* Setup for IP multicast routing */
2747 static int __net_init
ipmr_net_init(struct net
*net
)
2751 err
= ipmr_rules_init(net
);
2755 #ifdef CONFIG_PROC_FS
2757 if (!proc_create("ip_mr_vif", 0, net
->proc_net
, &ipmr_vif_fops
))
2759 if (!proc_create("ip_mr_cache", 0, net
->proc_net
, &ipmr_mfc_fops
))
2760 goto proc_cache_fail
;
2764 #ifdef CONFIG_PROC_FS
2766 remove_proc_entry("ip_mr_vif", net
->proc_net
);
2768 ipmr_rules_exit(net
);
2774 static void __net_exit
ipmr_net_exit(struct net
*net
)
2776 #ifdef CONFIG_PROC_FS
2777 remove_proc_entry("ip_mr_cache", net
->proc_net
);
2778 remove_proc_entry("ip_mr_vif", net
->proc_net
);
2780 ipmr_rules_exit(net
);
2783 static struct pernet_operations ipmr_net_ops
= {
2784 .init
= ipmr_net_init
,
2785 .exit
= ipmr_net_exit
,
2788 int __init
ip_mr_init(void)
2792 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
2793 sizeof(struct mfc_cache
),
2794 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
2797 err
= register_pernet_subsys(&ipmr_net_ops
);
2799 goto reg_pernet_fail
;
2801 err
= register_netdevice_notifier(&ip_mr_notifier
);
2803 goto reg_notif_fail
;
2804 #ifdef CONFIG_IP_PIMSM_V2
2805 if (inet_add_protocol(&pim_protocol
, IPPROTO_PIM
) < 0) {
2806 pr_err("%s: can't add PIM protocol\n", __func__
);
2808 goto add_proto_fail
;
2811 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETROUTE
,
2812 NULL
, ipmr_rtm_dumproute
, NULL
);
2813 rtnl_register(RTNL_FAMILY_IPMR
, RTM_NEWROUTE
,
2814 ipmr_rtm_route
, NULL
, NULL
);
2815 rtnl_register(RTNL_FAMILY_IPMR
, RTM_DELROUTE
,
2816 ipmr_rtm_route
, NULL
, NULL
);
2819 #ifdef CONFIG_IP_PIMSM_V2
2821 unregister_netdevice_notifier(&ip_mr_notifier
);
2824 unregister_pernet_subsys(&ipmr_net_ops
);
2826 kmem_cache_destroy(mrt_cachep
);