1 /* $NetBSD: ip_mroute.c,v 1.117 2008/12/19 18:49:39 cegger Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Stephen Deering of Stanford University.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
38 * Copyright (c) 1989 Stephen Deering
40 * This code is derived from software contributed to Berkeley by
41 * Stephen Deering of Stanford University.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 3. All advertising materials mentioning features or use of this software
52 * must display the following acknowledgement:
53 * This product includes software developed by the University of
54 * California, Berkeley and its contributors.
55 * 4. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * @(#)ip_mroute.c 8.2 (Berkeley) 11/15/93
75 * IP multicast forwarding procedures
77 * Written by David Waitzman, BBN Labs, August 1988.
78 * Modified by Steve Deering, Stanford, February 1989.
79 * Modified by Mark J. Steiglitz, Stanford, May, 1991
80 * Modified by Van Jacobson, LBL, January 1993
81 * Modified by Ajit Thyagarajan, PARC, August 1993
82 * Modified by Bill Fenner, PARC, April 1994
83 * Modified by Charles M. Hannum, NetBSD, May 1995.
84 * Modified by Ahmed Helmy, SGI, June 1996
85 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
86 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
87 * Modified by Hitoshi Asaeda, WIDE, August 2000
88 * Modified by Pavlin Radoslavov, ICSI, October 2002
90 * MROUTING Revision: 1.2
91 * and PIM-SMv2 and PIM-DM support, advanced API support,
92 * bandwidth metering and signaling
95 #include <sys/cdefs.h>
96 __KERNEL_RCSID(0, "$NetBSD: ip_mroute.c,v 1.117 2008/12/19 18:49:39 cegger Exp $");
99 #include "opt_ipsec.h"
106 #include <sys/param.h>
107 #include <sys/systm.h>
108 #include <sys/callout.h>
109 #include <sys/mbuf.h>
110 #include <sys/socket.h>
111 #include <sys/socketvar.h>
112 #include <sys/protosw.h>
113 #include <sys/errno.h>
114 #include <sys/time.h>
115 #include <sys/kernel.h>
116 #include <sys/ioctl.h>
117 #include <sys/syslog.h>
120 #include <net/route.h>
121 #include <net/raw_cb.h>
123 #include <netinet/in.h>
124 #include <netinet/in_var.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/ip.h>
127 #include <netinet/ip_var.h>
128 #include <netinet/in_pcb.h>
129 #include <netinet/udp.h>
130 #include <netinet/igmp.h>
131 #include <netinet/igmp_var.h>
132 #include <netinet/ip_mroute.h>
134 #include <netinet/pim.h>
135 #include <netinet/pim_var.h>
137 #include <netinet/ip_encap.h>
140 #include <netinet6/ipsec.h>
141 #include <netkey/key.h>
145 #include <netipsec/ipsec.h>
146 #include <netipsec/key.h>
149 #include <machine/stdarg.h>
151 #define IP_MULTICASTOPTS 0
152 #define M_PULLUP(m, len) \
154 if ((m) && ((m)->m_flags & M_EXT || (m)->m_len < (len))) \
155 (m) = m_pullup((m), (len)); \
156 } while (/*CONSTCOND*/ 0)
159 * Globals. All but ip_mrouter and ip_mrtproto could be static,
160 * except for netstat or debugging purposes.
162 struct socket
*ip_mrouter
= NULL
;
163 int ip_mrtproto
= IGMP_DVMRP
; /* for netstat only */
165 #define NO_RTE_FOUND 0x1
166 #define RTE_FOUND 0x2
168 #define MFCHASH(a, g) \
169 ((((a).s_addr >> 20) ^ ((a).s_addr >> 10) ^ (a).s_addr ^ \
170 ((g).s_addr >> 20) ^ ((g).s_addr >> 10) ^ (g).s_addr) & mfchash)
171 LIST_HEAD(mfchashhdr
, mfc
) *mfchashtbl
;
174 u_char nexpire
[MFCTBLSIZ
];
175 struct vif viftable
[MAXVIFS
];
176 struct mrtstat mrtstat
;
177 u_int mrtdebug
= 0; /* debug level */
178 #define DEBUG_MFC 0x02
179 #define DEBUG_FORWARD 0x04
180 #define DEBUG_EXPIRE 0x08
181 #define DEBUG_XMIT 0x10
182 #define DEBUG_PIM 0x20
184 #define VIFI_INVALID ((vifi_t) -1)
186 u_int tbfdebug
= 0; /* tbf debug level */
188 u_int rsvpdebug
= 0; /* rsvp debug level */
189 extern struct socket
*ip_rsvpd
;
191 #endif /* RSVP_ISI */
193 /* vif attachment using sys/netinet/ip_encap.c */
194 static void vif_input(struct mbuf
*, ...);
195 static int vif_encapcheck(struct mbuf
*, int, int, void *);
197 static const struct protosw vif_protosw
=
198 { SOCK_RAW
, &inetdomain
, IPPROTO_IPV4
, PR_ATOMIC
|PR_ADDR
,
199 vif_input
, rip_output
, 0, rip_ctloutput
,
204 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
205 #define UPCALL_EXPIRE 6 /* number of timeouts */
208 * Define the token bucket filter structures
211 #define TBF_REPROCESS (hz / 100) /* 100x / second */
213 static int get_sg_cnt(struct sioc_sg_req
*);
214 static int get_vif_cnt(struct sioc_vif_req
*);
215 static int ip_mrouter_init(struct socket
*, int);
216 static int set_assert(int);
217 static int add_vif(struct vifctl
*);
218 static int del_vif(vifi_t
*);
219 static void update_mfc_params(struct mfc
*, struct mfcctl2
*);
220 static void init_mfc_params(struct mfc
*, struct mfcctl2
*);
221 static void expire_mfc(struct mfc
*);
222 static int add_mfc(struct sockopt
*);
224 static void collate(struct timeval
*);
226 static int del_mfc(struct sockopt
*);
227 static int set_api_config(struct sockopt
*); /* chose API capabilities */
228 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
229 static void expire_upcalls(void *);
231 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*, vifi_t
);
233 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*);
235 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
236 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
237 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_int32_t
);
238 static void tbf_queue(struct vif
*, struct mbuf
*);
239 static void tbf_process_q(struct vif
*);
240 static void tbf_reprocess_q(void *);
241 static int tbf_dq_sel(struct vif
*, struct ip
*);
242 static void tbf_send_packet(struct vif
*, struct mbuf
*);
243 static void tbf_update_tokens(struct vif
*);
244 static int priority(struct vif
*, struct ip
*);
247 * Bandwidth monitoring
249 static void free_bw_list(struct bw_meter
*);
250 static int add_bw_upcall(struct bw_upcall
*);
251 static int del_bw_upcall(struct bw_upcall
*);
252 static void bw_meter_receive_packet(struct bw_meter
*, int , struct timeval
*);
253 static void bw_meter_prepare_upcall(struct bw_meter
*, struct timeval
*);
254 static void bw_upcalls_send(void);
255 static void schedule_bw_meter(struct bw_meter
*, struct timeval
*);
256 static void unschedule_bw_meter(struct bw_meter
*);
257 static void bw_meter_process(void);
258 static void expire_bw_upcalls_send(void *);
259 static void expire_bw_meter_process(void *);
262 static int pim_register_send(struct ip
*, struct vif
*,
263 struct mbuf
*, struct mfc
*);
264 static int pim_register_send_rp(struct ip
*, struct vif
*,
265 struct mbuf
*, struct mfc
*);
266 static int pim_register_send_upcall(struct ip
*, struct vif
*,
267 struct mbuf
*, struct mfc
*);
268 static struct mbuf
*pim_register_prepare(struct ip
*, struct mbuf
*);
272 * 'Interfaces' associated with decapsulator (so we can tell
273 * packets that went through it from ones that get reflected
274 * by a broken gateway). These interfaces are never linked into
275 * the system ifnet list & no routes point to them. I.e., packets
276 * can't be sent this way. They only exist as a placeholder for
277 * multicast source verification.
280 struct ifnet multicast_decap_if
[MAXVIFS
];
284 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
286 /* prototype IP hdr for encapsulated packets */
287 struct ip multicast_encap_iphdr
= {
288 .ip_hl
= sizeof(struct ip
) >> 2,
290 .ip_len
= sizeof(struct ip
),
296 * Bandwidth meter variables and constants
300 * Pending timeouts are stored in a hash table, the key being the
301 * expiration time. Periodically, the entries are analysed and processed.
303 #define BW_METER_BUCKETS 1024
304 static struct bw_meter
*bw_meter_timers
[BW_METER_BUCKETS
];
305 struct callout bw_meter_ch
;
306 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
309 * Pending upcalls are stored in a vector which is flushed when
310 * full, or periodically
312 static struct bw_upcall bw_upcalls
[BW_UPCALLS_MAX
];
313 static u_int bw_upcalls_n
; /* # of pending upcalls */
314 struct callout bw_upcalls_ch
;
315 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
318 struct pimstat pimstat
;
321 * Note: the PIM Register encapsulation adds the following in front of a
324 * struct pim_encap_hdr {
326 * struct pim_encap_pimhdr pim;
331 struct pim_encap_pimhdr
{
336 static struct ip pim_encap_iphdr
= {
338 .ip_hl
= sizeof(struct ip
) >> 2,
339 .ip_len
= sizeof(struct ip
),
344 static struct pim_encap_pimhdr pim_encap_pimhdr
= {
346 PIM_MAKE_VT(PIM_VERSION
, PIM_REGISTER
), /* PIM vers and message type */
353 static struct ifnet multicast_register_if
;
354 static vifi_t reg_vif_num
= VIFI_INVALID
;
361 static vifi_t numvifs
= 0;
363 static struct callout expire_upcalls_ch
;
366 * whether or not special PIM assert processing is enabled.
368 static int pim_assert
;
370 * Rate limit for assert notification messages, in usec
372 #define ASSERT_MSG_TIME 3000000
375 * Kernel multicast routing API capabilities and setup.
376 * If more API capabilities are added to the kernel, they should be
377 * recorded in `mrt_api_support'.
379 static const u_int32_t mrt_api_support
= (MRT_MFC_FLAGS_DISABLE_WRONGVIF
|
380 MRT_MFC_FLAGS_BORDER_VIF
|
383 static u_int32_t mrt_api_config
= 0;
386 * Find a route for a given origin IP address and Multicast group address
387 * Type of service parameter to be added in the future!!!
388 * Statistics are updated by the caller if needed
389 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
392 mfc_find(struct in_addr
*o
, struct in_addr
*g
)
396 LIST_FOREACH(rt
, &mfchashtbl
[MFCHASH(*o
, *g
)], mfc_hash
) {
397 if (in_hosteq(rt
->mfc_origin
, *o
) &&
398 in_hosteq(rt
->mfc_mcastgrp
, *g
) &&
399 (rt
->mfc_stall
== NULL
))
407 * Macros to compute elapsed time efficiently
408 * Borrowed from Van Jacobson's scheduling code
410 #define TV_DELTA(a, b, delta) do { \
412 delta = (a).tv_usec - (b).tv_usec; \
413 xxs = (a).tv_sec - (b).tv_sec; \
424 delta += (1000000 * xxs); \
427 } while (/*CONSTCOND*/ 0)
430 u_int32_t upcall_data
[51];
431 #endif /* UPCALL_TIMING */
434 * Handle MRT setsockopt commands to modify the multicast routing tables.
437 ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
)
443 struct bw_upcall bwuc
;
445 if (sopt
->sopt_name
!= MRT_INIT
&& so
!= ip_mrouter
)
448 switch (sopt
->sopt_name
) {
450 error
= sockopt_getint(sopt
, &optval
);
454 error
= ip_mrouter_init(so
, optval
);
457 error
= ip_mrouter_done();
460 error
= sockopt_get(sopt
, &vifc
, sizeof(vifc
));
463 error
= add_vif(&vifc
);
466 error
= sockopt_get(sopt
, &vifi
, sizeof(vifi
));
469 error
= del_vif(&vifi
);
472 error
= add_mfc(sopt
);
475 error
= del_mfc(sopt
);
478 error
= sockopt_getint(sopt
, &optval
);
481 error
= set_assert(optval
);
484 error
= set_api_config(sopt
);
486 case MRT_ADD_BW_UPCALL
:
487 error
= sockopt_get(sopt
, &bwuc
, sizeof(bwuc
));
490 error
= add_bw_upcall(&bwuc
);
492 case MRT_DEL_BW_UPCALL
:
493 error
= sockopt_get(sopt
, &bwuc
, sizeof(bwuc
));
496 error
= del_bw_upcall(&bwuc
);
507 * Handle MRT getsockopt commands
510 ip_mrouter_get(struct socket
*so
, struct sockopt
*sopt
)
514 if (so
!= ip_mrouter
)
517 switch (sopt
->sopt_name
) {
519 error
= sockopt_setint(sopt
, 0x0305); /* XXX !!!! */
522 error
= sockopt_setint(sopt
, pim_assert
);
524 case MRT_API_SUPPORT
:
525 error
= sockopt_set(sopt
, &mrt_api_support
,
526 sizeof(mrt_api_support
));
529 error
= sockopt_set(sopt
, &mrt_api_config
,
530 sizeof(mrt_api_config
));
541 * Handle ioctl commands to obtain information from the cache
544 mrt_ioctl(struct socket
*so
, u_long cmd
, void *data
)
548 if (so
!= ip_mrouter
)
553 error
= get_vif_cnt((struct sioc_vif_req
*)data
);
556 error
= get_sg_cnt((struct sioc_sg_req
*)data
);
567 * returns the packet, byte, rpf-failure count for the source group provided
570 get_sg_cnt(struct sioc_sg_req
*req
)
576 rt
= mfc_find(&req
->src
, &req
->grp
);
579 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
580 return (EADDRNOTAVAIL
);
582 req
->pktcnt
= rt
->mfc_pkt_cnt
;
583 req
->bytecnt
= rt
->mfc_byte_cnt
;
584 req
->wrong_if
= rt
->mfc_wrong_if
;
591 * returns the input and output packet and byte counts on the vif provided
594 get_vif_cnt(struct sioc_vif_req
*req
)
596 vifi_t vifi
= req
->vifi
;
601 req
->icount
= viftable
[vifi
].v_pkt_in
;
602 req
->ocount
= viftable
[vifi
].v_pkt_out
;
603 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
604 req
->obytes
= viftable
[vifi
].v_bytes_out
;
610 * Enable multicast routing
613 ip_mrouter_init(struct socket
*so
, int v
)
617 "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
618 so
->so_type
, so
->so_proto
->pr_protocol
);
620 if (so
->so_type
!= SOCK_RAW
||
621 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
)
627 if (ip_mrouter
!= NULL
)
632 mfchashtbl
= hashinit(MFCTBLSIZ
, HASH_LIST
, true, &mfchash
);
633 memset((void *)nexpire
, 0, sizeof(nexpire
));
637 callout_init(&expire_upcalls_ch
, 0);
638 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
,
639 expire_upcalls
, NULL
);
641 callout_init(&bw_upcalls_ch
, 0);
642 callout_reset(&bw_upcalls_ch
, BW_UPCALLS_PERIOD
,
643 expire_bw_upcalls_send
, NULL
);
645 callout_init(&bw_meter_ch
, 0);
646 callout_reset(&bw_meter_ch
, BW_METER_PERIOD
,
647 expire_bw_meter_process
, NULL
);
650 log(LOG_DEBUG
, "ip_mrouter_init\n");
656 * Disable multicast routing
659 ip_mrouter_done(void)
668 /* Clear out all the vifs currently in use. */
669 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
670 vifp
= &viftable
[vifi
];
671 if (!in_nullhost(vifp
->v_lcl_addr
))
679 callout_stop(&expire_upcalls_ch
);
680 callout_stop(&bw_upcalls_ch
);
681 callout_stop(&bw_meter_ch
);
684 * Free all multicast forwarding cache entries.
686 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
687 struct mfc
*rt
, *nrt
;
689 for (rt
= LIST_FIRST(&mfchashtbl
[i
]); rt
; rt
= nrt
) {
690 nrt
= LIST_NEXT(rt
, mfc_hash
);
696 memset((void *)nexpire
, 0, sizeof(nexpire
));
697 hashdone(mfchashtbl
, HASH_LIST
, mfchash
);
701 memset(bw_meter_timers
, 0, sizeof(bw_meter_timers
));
703 /* Reset de-encapsulation cache. */
710 log(LOG_DEBUG
, "ip_mrouter_done\n");
716 ip_mrouter_detach(struct ifnet
*ifp
)
723 /* XXX not sure about side effect to userland routing daemon */
724 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
725 vifp
= &viftable
[vifi
];
726 if (vifp
->v_ifp
== ifp
)
729 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
732 LIST_FOREACH(rt
, &mfchashtbl
[i
], mfc_hash
) {
733 for (rte
= rt
->mfc_stall
; rte
; rte
= rte
->next
) {
742 * Set PIM assert processing global
752 * Configure API capabilities
755 set_api_config(struct sockopt
*sopt
)
761 * We can set the API capabilities only if it is the first operation
762 * after MRT_INIT. I.e.:
763 * - there are no vifs installed
764 * - pim_assert is not enabled
765 * - the MFC table is empty
767 error
= sockopt_get(sopt
, &apival
, sizeof(apival
));
774 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
775 if (LIST_FIRST(&mfchashtbl
[i
]) != NULL
)
779 mrt_api_config
= apival
& mrt_api_support
;
784 * Add a vif to the vif table
787 add_vif(struct vifctl
*vifcp
)
794 struct sockaddr_in sin
;
796 if (vifcp
->vifc_vifi
>= MAXVIFS
)
798 if (in_nullhost(vifcp
->vifc_lcl_addr
))
799 return (EADDRNOTAVAIL
);
801 vifp
= &viftable
[vifcp
->vifc_vifi
];
802 if (!in_nullhost(vifp
->v_lcl_addr
))
805 /* Find the interface with an address in AF_INET family. */
807 if (vifcp
->vifc_flags
& VIFF_REGISTER
) {
809 * XXX: Because VIFF_REGISTER does not really need a valid
810 * local interface (e.g. it could be 127.0.0.2), we don't
817 sockaddr_in_init(&sin
, &vifcp
->vifc_lcl_addr
, 0);
818 ifa
= ifa_ifwithaddr(sintosa(&sin
));
820 return (EADDRNOTAVAIL
);
824 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
825 if (vifcp
->vifc_flags
& VIFF_SRCRT
) {
826 log(LOG_ERR
, "source routed tunnels not supported\n");
830 /* attach this vif to decapsulator dispatch table */
832 * XXX Use addresses in registration so that matching
833 * can be done with radix tree in decapsulator. But,
834 * we need to check inner header for multicast, so
835 * this requires both radix tree lookup and then a
836 * function to check, and this is not supported yet.
838 vifp
->v_encap_cookie
= encap_attach_func(AF_INET
, IPPROTO_IPV4
,
839 vif_encapcheck
, &vif_protosw
, vifp
);
840 if (!vifp
->v_encap_cookie
)
843 /* Create a fake encapsulation interface. */
844 ifp
= malloc(sizeof(*ifp
), M_MRTABLE
, M_WAITOK
|M_ZERO
);
845 snprintf(ifp
->if_xname
, sizeof(ifp
->if_xname
),
846 "mdecap%d", vifcp
->vifc_vifi
);
848 /* Prepare cached route entry. */
849 memset(&vifp
->v_route
, 0, sizeof(vifp
->v_route
));
851 } else if (vifcp
->vifc_flags
& VIFF_REGISTER
) {
852 ifp
= &multicast_register_if
;
854 log(LOG_DEBUG
, "Adding a register vif, ifp: %p\n",
856 if (reg_vif_num
== VIFI_INVALID
) {
857 memset(ifp
, 0, sizeof(*ifp
));
858 snprintf(ifp
->if_xname
, sizeof(ifp
->if_xname
),
860 ifp
->if_flags
= IFF_LOOPBACK
;
861 memset(&vifp
->v_route
, 0, sizeof(vifp
->v_route
));
862 reg_vif_num
= vifcp
->vifc_vifi
;
866 /* Make sure the interface supports multicast. */
867 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
870 /* Enable promiscuous reception of all IP multicasts. */
871 sockaddr_in_init(&sin
, &zeroin_addr
, 0);
872 ifreq_setaddr(SIOCADDMULTI
, &ifr
, sintosa(&sin
));
873 error
= (*ifp
->if_ioctl
)(ifp
, SIOCADDMULTI
, &ifr
);
880 /* Define parameters for the tbf structure. */
882 vifp
->tbf_t
= &vifp
->tbf_q
;
883 microtime(&vifp
->tbf_last_pkt_t
);
886 vifp
->tbf_max_q_len
= MAXQSIZE
;
888 vifp
->v_flags
= vifcp
->vifc_flags
;
889 vifp
->v_threshold
= vifcp
->vifc_threshold
;
890 /* scaling up here allows division by 1024 in critical code */
891 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
892 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
893 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
895 /* Initialize per vif pkt counters. */
898 vifp
->v_bytes_in
= 0;
899 vifp
->v_bytes_out
= 0;
901 callout_init(&vifp
->v_repq_ch
, 0);
905 vifp
->v_rsvpd
= NULL
;
906 #endif /* RSVP_ISI */
910 /* Adjust numvifs up if the vifi is higher than numvifs. */
911 if (numvifs
<= vifcp
->vifc_vifi
)
912 numvifs
= vifcp
->vifc_vifi
+ 1;
915 log(LOG_DEBUG
, "add_vif #%d, lcladdr %x, %s %x, thresh %x, rate %d\n",
917 ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
918 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
919 ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
920 vifcp
->vifc_threshold
,
921 vifcp
->vifc_rate_limit
);
927 reset_vif(struct vif
*vifp
)
932 struct sockaddr_in sin
;
934 callout_stop(&vifp
->v_repq_ch
);
936 /* detach this vif from decapsulator dispatch table */
937 encap_detach(vifp
->v_encap_cookie
);
938 vifp
->v_encap_cookie
= NULL
;
941 * Free packets queued at the interface
943 for (m
= vifp
->tbf_q
; m
!= NULL
; m
= n
) {
948 if (vifp
->v_flags
& VIFF_TUNNEL
)
949 free(vifp
->v_ifp
, M_MRTABLE
);
950 else if (vifp
->v_flags
& VIFF_REGISTER
) {
952 reg_vif_num
= VIFI_INVALID
;
955 sockaddr_in_init(&sin
, &zeroin_addr
, 0);
956 ifreq_setaddr(SIOCDELMULTI
, &ifr
, sintosa(&sin
));
958 (*ifp
->if_ioctl
)(ifp
, SIOCDELMULTI
, &ifr
);
960 memset((void *)vifp
, 0, sizeof(*vifp
));
964 * Delete a vif from the vif table
967 del_vif(vifi_t
*vifip
)
973 if (*vifip
>= numvifs
)
976 vifp
= &viftable
[*vifip
];
977 if (in_nullhost(vifp
->v_lcl_addr
))
978 return (EADDRNOTAVAIL
);
984 /* Adjust numvifs down */
985 for (vifi
= numvifs
; vifi
> 0; vifi
--)
986 if (!in_nullhost(viftable
[vifi
- 1].v_lcl_addr
))
993 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", *vifip
, numvifs
);
999 * update an mfc entry without resetting counters and S,G addresses.
1002 update_mfc_params(struct mfc
*rt
, struct mfcctl2
*mfccp
)
1006 rt
->mfc_parent
= mfccp
->mfcc_parent
;
1007 for (i
= 0; i
< numvifs
; i
++) {
1008 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
1009 rt
->mfc_flags
[i
] = mfccp
->mfcc_flags
[i
] & mrt_api_config
&
1012 /* set the RP address */
1013 if (mrt_api_config
& MRT_MFC_RP
)
1014 rt
->mfc_rp
= mfccp
->mfcc_rp
;
1016 rt
->mfc_rp
= zeroin_addr
;
1020 * fully initialize an mfc entry from the parameter.
1023 init_mfc_params(struct mfc
*rt
, struct mfcctl2
*mfccp
)
1025 rt
->mfc_origin
= mfccp
->mfcc_origin
;
1026 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
1028 update_mfc_params(rt
, mfccp
);
1030 /* initialize pkt counters per src-grp */
1031 rt
->mfc_pkt_cnt
= 0;
1032 rt
->mfc_byte_cnt
= 0;
1033 rt
->mfc_wrong_if
= 0;
1034 timerclear(&rt
->mfc_last_assert
);
1038 expire_mfc(struct mfc
*rt
)
1040 struct rtdetq
*rte
, *nrte
;
1042 free_bw_list(rt
->mfc_bw_meter
);
1044 for (rte
= rt
->mfc_stall
; rte
!= NULL
; rte
= nrte
) {
1047 free(rte
, M_MRTABLE
);
1050 LIST_REMOVE(rt
, mfc_hash
);
1051 free(rt
, M_MRTABLE
);
1058 add_mfc(struct sockopt
*sopt
)
1060 struct mfcctl2 mfcctl2
;
1061 struct mfcctl2
*mfccp
;
1064 struct rtdetq
*rte
, *nrte
;
1067 int mfcctl_size
= sizeof(struct mfcctl
);
1070 if (mrt_api_config
& MRT_API_FLAGS_ALL
)
1071 mfcctl_size
= sizeof(struct mfcctl2
);
1074 * select data size depending on API version.
1077 memset(&mfcctl2
, 0, sizeof(mfcctl2
));
1079 if (mrt_api_config
& MRT_API_FLAGS_ALL
)
1080 error
= sockopt_get(sopt
, mfccp
, sizeof(struct mfcctl2
));
1082 error
= sockopt_get(sopt
, mfccp
, sizeof(struct mfcctl
));
1088 rt
= mfc_find(&mfccp
->mfcc_origin
, &mfccp
->mfcc_mcastgrp
);
1090 /* If an entry already exists, just update the fields */
1092 if (mrtdebug
& DEBUG_MFC
)
1093 log(LOG_DEBUG
, "add_mfc update o %x g %x p %x\n",
1094 ntohl(mfccp
->mfcc_origin
.s_addr
),
1095 ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1096 mfccp
->mfcc_parent
);
1098 update_mfc_params(rt
, mfccp
);
1105 * Find the entry for which the upcall was made and update
1108 hash
= MFCHASH(mfccp
->mfcc_origin
, mfccp
->mfcc_mcastgrp
);
1109 LIST_FOREACH(rt
, &mfchashtbl
[hash
], mfc_hash
) {
1110 if (in_hosteq(rt
->mfc_origin
, mfccp
->mfcc_origin
) &&
1111 in_hosteq(rt
->mfc_mcastgrp
, mfccp
->mfcc_mcastgrp
) &&
1112 rt
->mfc_stall
!= NULL
) {
1114 log(LOG_ERR
, "add_mfc %s o %x g %x p %x dbx %p\n",
1115 "multiple kernel entries",
1116 ntohl(mfccp
->mfcc_origin
.s_addr
),
1117 ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1118 mfccp
->mfcc_parent
, rt
->mfc_stall
);
1120 if (mrtdebug
& DEBUG_MFC
)
1121 log(LOG_DEBUG
, "add_mfc o %x g %x p %x dbg %p\n",
1122 ntohl(mfccp
->mfcc_origin
.s_addr
),
1123 ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1124 mfccp
->mfcc_parent
, rt
->mfc_stall
);
1126 rte
= rt
->mfc_stall
;
1127 init_mfc_params(rt
, mfccp
);
1128 rt
->mfc_stall
= NULL
;
1130 rt
->mfc_expire
= 0; /* Don't clean this guy up */
1133 /* free packets Qed at the end of this entry */
1134 for (; rte
!= NULL
; rte
= nrte
) {
1138 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
1140 ip_mdq(rte
->m
, rte
->ifp
, rt
);
1141 #endif /* RSVP_ISI */
1144 #ifdef UPCALL_TIMING
1146 #endif /* UPCALL_TIMING */
1147 free(rte
, M_MRTABLE
);
1153 * It is possible that an entry is being inserted without an upcall
1157 * No mfc; make a new one
1159 if (mrtdebug
& DEBUG_MFC
)
1160 log(LOG_DEBUG
, "add_mfc no upcall o %x g %x p %x\n",
1161 ntohl(mfccp
->mfcc_origin
.s_addr
),
1162 ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1163 mfccp
->mfcc_parent
);
1165 LIST_FOREACH(rt
, &mfchashtbl
[hash
], mfc_hash
) {
1166 if (in_hosteq(rt
->mfc_origin
, mfccp
->mfcc_origin
) &&
1167 in_hosteq(rt
->mfc_mcastgrp
, mfccp
->mfcc_mcastgrp
)) {
1168 init_mfc_params(rt
, mfccp
);
1175 if (rt
== NULL
) { /* no upcall, so make a new entry */
1176 rt
= (struct mfc
*)malloc(sizeof(*rt
), M_MRTABLE
,
1183 init_mfc_params(rt
, mfccp
);
1185 rt
->mfc_stall
= NULL
;
1186 rt
->mfc_bw_meter
= NULL
;
1188 /* insert new entry at head of hash chain */
1189 LIST_INSERT_HEAD(&mfchashtbl
[hash
], rt
, mfc_hash
);
1197 #ifdef UPCALL_TIMING
1199 * collect delay statistics on the upcalls
1202 collate(struct timeval
*t
)
1210 if (timercmp(t
, &tp
, <)) {
1211 TV_DELTA(tp
, *t
, delta
);
1220 #endif /* UPCALL_TIMING */
1223 * Delete an mfc entry
1226 del_mfc(struct sockopt
*sopt
)
1228 struct mfcctl2 mfcctl2
;
1229 struct mfcctl2
*mfccp
;
1235 * XXX: for deleting MFC entries the information in entries
1236 * of size "struct mfcctl" is sufficient.
1240 memset(&mfcctl2
, 0, sizeof(mfcctl2
));
1242 error
= sockopt_get(sopt
, mfccp
, sizeof(struct mfcctl
));
1244 /* Try with the size of mfcctl2. */
1245 error
= sockopt_get(sopt
, mfccp
, sizeof(struct mfcctl2
));
1250 if (mrtdebug
& DEBUG_MFC
)
1251 log(LOG_DEBUG
, "del_mfc origin %x mcastgrp %x\n",
1252 ntohl(mfccp
->mfcc_origin
.s_addr
),
1253 ntohl(mfccp
->mfcc_mcastgrp
.s_addr
));
1257 rt
= mfc_find(&mfccp
->mfcc_origin
, &mfccp
->mfcc_mcastgrp
);
1260 return (EADDRNOTAVAIL
);
1264 * free the bw_meter entries
1266 free_bw_list(rt
->mfc_bw_meter
);
1267 rt
->mfc_bw_meter
= NULL
;
1269 LIST_REMOVE(rt
, mfc_hash
);
1270 free(rt
, M_MRTABLE
);
1277 socket_send(struct socket
*s
, struct mbuf
*mm
, struct sockaddr_in
*src
)
1280 if (sbappendaddr(&s
->so_rcv
, sintosa(src
), mm
,
1281 (struct mbuf
*)NULL
) != 0) {
1291 * IP multicast forwarding function. This function assumes that the packet
1292 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1293 * pointed to by "ifp", and the packet is to be relayed to other networks
1294 * that have members of the packet's destination IP multicast group.
1296 * The packet is returned unscathed to the caller, unless it is
1297 * erroneous, in which case a non-zero return value tells the caller to
1301 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1302 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1306 ip_mforward(struct mbuf
*m
, struct ifnet
*ifp
, struct ip_moptions
*imo
)
1308 ip_mforward(struct mbuf
*m
, struct ifnet
*ifp
)
1309 #endif /* RSVP_ISI */
1311 struct ip
*ip
= mtod(m
, struct ip
*);
1313 static int srctun
= 0;
1315 struct sockaddr_in sin
;
1319 if (mrtdebug
& DEBUG_FORWARD
)
1320 log(LOG_DEBUG
, "ip_mforward: src %x, dst %x, ifp %p\n",
1321 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), ifp
);
1323 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1324 ((u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1326 * Packet arrived via a physical interface or
1327 * an encapsulated tunnel or a register_vif.
1331 * Packet arrived through a source-route tunnel.
1332 * Source-route tunnels are no longer supported.
1334 if ((srctun
++ % 1000) == 0)
1336 "ip_mforward: received source-routed packet from %x\n",
1337 ntohl(ip
->ip_src
.s_addr
));
1343 * Clear any in-bound checksum flags for this packet.
1345 m
->m_pkthdr
.csum_flags
= 0;
1348 if (imo
&& ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1349 if (ip
->ip_ttl
< MAXTTL
)
1350 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1351 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1352 struct vif
*vifp
= viftable
+ vifi
;
1353 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s)\n",
1354 ntohl(ip
->ip_src
), ntohl(ip
->ip_dst
), vifi
,
1355 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1356 vifp
->v_ifp
->if_xname
);
1358 return (ip_mdq(m
, ifp
, (struct mfc
*)NULL
, vifi
));
1360 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1361 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
1362 ntohl(ip
->ip_src
), ntohl(ip
->ip_dst
));
1364 #endif /* RSVP_ISI */
1367 * Don't forward a packet with time-to-live of zero or one,
1368 * or a packet destined to a local-only group.
1370 if (ip
->ip_ttl
<= 1 || IN_LOCAL_GROUP(ip
->ip_dst
.s_addr
))
1374 * Determine forwarding vifs from the forwarding cache table
1377 ++mrtstat
.mrts_mfc_lookups
;
1378 rt
= mfc_find(&ip
->ip_src
, &ip
->ip_dst
);
1380 /* Entry exists, so forward if necessary */
1384 return (ip_mdq(m
, ifp
, rt
, -1));
1386 return (ip_mdq(m
, ifp
, rt
));
1387 #endif /* RSVP_ISI */
1390 * If we don't have a route for packet's origin,
1391 * Make a copy of the packet & send message to routing daemon
1397 int hlen
= ip
->ip_hl
<< 2;
1398 #ifdef UPCALL_TIMING
1402 #endif /* UPCALL_TIMING */
1404 ++mrtstat
.mrts_mfc_misses
;
1406 mrtstat
.mrts_no_route
++;
1407 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1408 log(LOG_DEBUG
, "ip_mforward: no rte s %x g %x\n",
1409 ntohl(ip
->ip_src
.s_addr
),
1410 ntohl(ip
->ip_dst
.s_addr
));
1413 * Allocate mbufs early so that we don't do extra work if we are
1414 * just going to fail anyway. Make sure to pullup the header so
1415 * that other people can't step on it.
1417 rte
= (struct rtdetq
*)malloc(sizeof(*rte
), M_MRTABLE
,
1423 mb0
= m_copypacket(m
, M_DONTWAIT
);
1424 M_PULLUP(mb0
, hlen
);
1426 free(rte
, M_MRTABLE
);
1431 /* is there an upcall waiting for this flow? */
1432 hash
= MFCHASH(ip
->ip_src
, ip
->ip_dst
);
1433 LIST_FOREACH(rt
, &mfchashtbl
[hash
], mfc_hash
) {
1434 if (in_hosteq(ip
->ip_src
, rt
->mfc_origin
) &&
1435 in_hosteq(ip
->ip_dst
, rt
->mfc_mcastgrp
) &&
1436 rt
->mfc_stall
!= NULL
)
1445 * Locate the vifi for the incoming interface for
1447 * If none found, drop packet.
1449 for (vifi
= 0; vifi
< numvifs
&&
1450 viftable
[vifi
].v_ifp
!= ifp
; vifi
++)
1452 if (vifi
>= numvifs
) /* vif not found, drop packet */
1455 /* no upcall, so make a new entry */
1456 rt
= (struct mfc
*)malloc(sizeof(*rt
), M_MRTABLE
,
1462 * Make a copy of the header to send to the user level
1465 mm
= m_copym(m
, 0, hlen
, M_DONTWAIT
);
1471 * Send message to routing daemon to install
1472 * a route into the kernel table
1475 im
= mtod(mm
, struct igmpmsg
*);
1476 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1480 mrtstat
.mrts_upcalls
++;
1482 sockaddr_in_init(&sin
, &ip
->ip_src
, 0);
1483 if (socket_send(ip_mrouter
, mm
, &sin
) < 0) {
1485 "ip_mforward: ip_mrouter socket queue full\n");
1486 ++mrtstat
.mrts_upq_sockfull
;
1488 free(rt
, M_MRTABLE
);
1490 free(rte
, M_MRTABLE
);
1496 /* insert new entry at head of hash chain */
1497 rt
->mfc_origin
= ip
->ip_src
;
1498 rt
->mfc_mcastgrp
= ip
->ip_dst
;
1499 rt
->mfc_pkt_cnt
= 0;
1500 rt
->mfc_byte_cnt
= 0;
1501 rt
->mfc_wrong_if
= 0;
1502 rt
->mfc_expire
= UPCALL_EXPIRE
;
1504 for (i
= 0; i
< numvifs
; i
++) {
1505 rt
->mfc_ttls
[i
] = 0;
1506 rt
->mfc_flags
[i
] = 0;
1508 rt
->mfc_parent
= -1;
1510 /* clear the RP address */
1511 rt
->mfc_rp
= zeroin_addr
;
1513 rt
->mfc_bw_meter
= NULL
;
1515 /* link into table */
1516 LIST_INSERT_HEAD(&mfchashtbl
[hash
], rt
, mfc_hash
);
1517 /* Add this entry to the end of the queue */
1518 rt
->mfc_stall
= rte
;
1520 /* determine if q has overflowed */
1525 * XXX ouch! we need to append to the list, but we
1526 * only have a pointer to the front, so we have to
1527 * scan the entire list every time.
1529 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1530 if (++npkts
> MAX_UPQ
) {
1531 mrtstat
.mrts_upq_ovflw
++;
1533 free(rte
, M_MRTABLE
);
1539 /* Add this entry to the end of the queue */
1546 #ifdef UPCALL_TIMING
1548 #endif /* UPCALL_TIMING */
1559 expire_upcalls(void *v
)
1566 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
1567 struct mfc
*rt
, *nrt
;
1569 if (nexpire
[i
] == 0)
1572 for (rt
= LIST_FIRST(&mfchashtbl
[i
]); rt
; rt
= nrt
) {
1573 nrt
= LIST_NEXT(rt
, mfc_hash
);
1575 if (rt
->mfc_expire
== 0 || --rt
->mfc_expire
> 0)
1580 * free the bw_meter entries
1582 while (rt
->mfc_bw_meter
!= NULL
) {
1583 struct bw_meter
*x
= rt
->mfc_bw_meter
;
1585 rt
->mfc_bw_meter
= x
->bm_mfc_next
;
1589 ++mrtstat
.mrts_cache_cleanups
;
1590 if (mrtdebug
& DEBUG_EXPIRE
)
1592 "expire_upcalls: expiring (%x %x)\n",
1593 ntohl(rt
->mfc_origin
.s_addr
),
1594 ntohl(rt
->mfc_mcastgrp
.s_addr
));
1601 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
,
1602 expire_upcalls
, NULL
);
1606 * Packet forwarding routine once entry in the cache is made
1610 ip_mdq(struct mbuf
*m
, struct ifnet
*ifp
, struct mfc
*rt
, vifi_t xmt_vif
)
1612 ip_mdq(struct mbuf
*m
, struct ifnet
*ifp
, struct mfc
*rt
)
1613 #endif /* RSVP_ISI */
1615 struct ip
*ip
= mtod(m
, struct ip
*);
1618 struct sockaddr_in sin
;
1619 int plen
= ntohs(ip
->ip_len
) - (ip
->ip_hl
<< 2);
1622 * Macro to send packet on vif. Since RSVP packets don't get counted on
1623 * input, they shouldn't get counted on output, so statistics keeping is
1626 #define MC_SEND(ip, vifp, m) do { \
1627 if ((vifp)->v_flags & VIFF_TUNNEL) \
1628 encap_send((ip), (vifp), (m)); \
1630 phyint_send((ip), (vifp), (m)); \
1631 } while (/*CONSTCOND*/ 0)
1635 * If xmt_vif is not -1, send on only the requested vif.
1637 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.
1639 if (xmt_vif
< numvifs
) {
1641 if (viftable
[xmt_vif
].v_flags
& VIFF_REGISTER
)
1642 pim_register_send(ip
, viftable
+ xmt_vif
, m
, rt
);
1645 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1648 #endif /* RSVP_ISI */
1651 * Don't forward if it didn't arrive from the parent vif for its origin.
1653 vifi
= rt
->mfc_parent
;
1654 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1655 /* came in the wrong interface */
1656 if (mrtdebug
& DEBUG_FORWARD
)
1657 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1659 vifi
>= numvifs
? 0 : viftable
[vifi
].v_ifp
);
1660 ++mrtstat
.mrts_wrong_if
;
1663 * If we are doing PIM assert processing, send a message
1664 * to the routing daemon.
1666 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1667 * can complete the SPT switch, regardless of the type
1668 * of the iif (broadcast media, GRE tunnel, etc).
1670 if (pim_assert
&& (vifi
< numvifs
) && viftable
[vifi
].v_ifp
) {
1675 if (ifp
== &multicast_register_if
)
1676 pimstat
.pims_rcv_registers_wrongiif
++;
1679 /* Get vifi for the incoming packet */
1681 vifi
< numvifs
&& viftable
[vifi
].v_ifp
!= ifp
;
1684 if (vifi
>= numvifs
) {
1685 /* The iif is not found: ignore the packet. */
1689 if (rt
->mfc_flags
[vifi
] &
1690 MRT_MFC_FLAGS_DISABLE_WRONGVIF
) {
1691 /* WRONGVIF disabled: ignore the packet */
1697 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1699 if (delta
> ASSERT_MSG_TIME
) {
1701 int hlen
= ip
->ip_hl
<< 2;
1703 m_copym(m
, 0, hlen
, M_DONTWAIT
);
1709 rt
->mfc_last_assert
= now
;
1711 im
= mtod(mm
, struct igmpmsg
*);
1712 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1716 mrtstat
.mrts_upcalls
++;
1718 sockaddr_in_init(&sin
, &im
->im_src
, 0);
1719 if (socket_send(ip_mrouter
, mm
, &sin
) < 0) {
1721 "ip_mforward: ip_mrouter socket queue full\n");
1722 ++mrtstat
.mrts_upq_sockfull
;
1730 /* If I sourced this packet, it counts as output, else it was input. */
1731 if (in_hosteq(ip
->ip_src
, viftable
[vifi
].v_lcl_addr
)) {
1732 viftable
[vifi
].v_pkt_out
++;
1733 viftable
[vifi
].v_bytes_out
+= plen
;
1735 viftable
[vifi
].v_pkt_in
++;
1736 viftable
[vifi
].v_bytes_in
+= plen
;
1739 rt
->mfc_byte_cnt
+= plen
;
1742 * For each vif, decide if a copy of the packet should be forwarded.
1744 * - the ttl exceeds the vif's threshold
1745 * - there are group members downstream on interface
1747 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1748 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1749 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1751 vifp
->v_bytes_out
+= plen
;
1753 if (vifp
->v_flags
& VIFF_REGISTER
)
1754 pim_register_send(ip
, vifp
, m
, rt
);
1757 MC_SEND(ip
, vifp
, m
);
1761 * Perform upcall-related bw measuring.
1763 if (rt
->mfc_bw_meter
!= NULL
) {
1768 for (x
= rt
->mfc_bw_meter
; x
!= NULL
; x
= x
->bm_mfc_next
)
1769 bw_meter_receive_packet(x
, plen
, &now
);
1777 * check if a vif number is legal/ok. This is used by ip_output.
1780 legal_vif_num(int vif
)
1782 if (vif
>= 0 && vif
< numvifs
)
1787 #endif /* RSVP_ISI */
1790 phyint_send(struct ip
*ip
, struct vif
*vifp
, struct mbuf
*m
)
1792 struct mbuf
*mb_copy
;
1793 int hlen
= ip
->ip_hl
<< 2;
1796 * Make a new reference to the packet; make sure that
1797 * the IP header is actually copied, not just referenced,
1798 * so that ip_output() only scribbles on the copy.
1800 mb_copy
= m_copypacket(m
, M_DONTWAIT
);
1801 M_PULLUP(mb_copy
, hlen
);
1802 if (mb_copy
== NULL
)
1805 if (vifp
->v_rate_limit
<= 0)
1806 tbf_send_packet(vifp
, mb_copy
);
1808 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*),
1813 encap_send(struct ip
*ip
, struct vif
*vifp
, struct mbuf
*m
)
1815 struct mbuf
*mb_copy
;
1817 int i
, len
= ntohs(ip
->ip_len
) + sizeof(multicast_encap_iphdr
);
1819 /* Take care of delayed checksums */
1820 if (m
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
)) {
1821 in_delayed_cksum(m
);
1822 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_TCPv4
|M_CSUM_UDPv4
);
1826 * copy the old packet & pullup it's IP header into the
1827 * new mbuf so we can modify it. Try to fill the new
1828 * mbuf since if we don't the ethernet driver will.
1830 MGETHDR(mb_copy
, M_DONTWAIT
, MT_DATA
);
1831 if (mb_copy
== NULL
)
1833 mb_copy
->m_data
+= max_linkhdr
;
1834 mb_copy
->m_pkthdr
.len
= len
;
1835 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1837 if ((mb_copy
->m_next
= m_copypacket(m
, M_DONTWAIT
)) == NULL
) {
1841 i
= MHLEN
- max_linkhdr
;
1844 mb_copy
= m_pullup(mb_copy
, i
);
1845 if (mb_copy
== NULL
)
1849 * fill in the encapsulating IP header.
1851 ip_copy
= mtod(mb_copy
, struct ip
*);
1852 *ip_copy
= multicast_encap_iphdr
;
1853 if (len
< IP_MINFRAGSIZE
)
1856 ip_copy
->ip_id
= ip_newid(NULL
);
1857 ip_copy
->ip_len
= htons(len
);
1858 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1859 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1862 * turn the encapsulated IP header back into a valid one.
1864 ip
= (struct ip
*)((char *)ip_copy
+ sizeof(multicast_encap_iphdr
));
1867 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1868 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1869 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1871 if (vifp
->v_rate_limit
<= 0)
1872 tbf_send_packet(vifp
, mb_copy
);
1874 tbf_control(vifp
, mb_copy
, ip
, ntohs(ip_copy
->ip_len
));
1878 * De-encapsulate a packet and feed it back through ip input.
1881 vif_input(struct mbuf
*m
, ...)
1887 struct ifqueue
*ifq
;
1890 off
= va_arg(ap
, int);
1891 proto
= va_arg(ap
, int);
1894 vifp
= (struct vif
*)encap_getarg(m
);
1895 if (!vifp
|| proto
!= ENCAP_PROTO
) {
1897 mrtstat
.mrts_bad_tunnel
++;
1902 m
->m_pkthdr
.rcvif
= vifp
->v_ifp
;
1905 if (IF_QFULL(ifq
)) {
1911 * normally we would need a "schednetisr(NETISR_IP)"
1912 * here but we were called by ip_input and it is going
1913 * to loop back & try to dequeue the packet we just
1914 * queued as soon as we return so we avoid the
1915 * unnecessary software interrrupt.
1922 * Check if the packet should be received on the vif denoted by arg.
1923 * (The encap selection code will call this once per vif since each is
1924 * registered separately.)
1927 vif_encapcheck(struct mbuf
*m
, int off
, int proto
, void *arg
)
1933 if (!arg
|| proto
!= IPPROTO_IPV4
)
1934 panic("unexpected arg in vif_encapcheck");
1938 * Accept the packet only if the inner heaader is multicast
1939 * and the outer header matches a tunnel-mode vif. Order
1940 * checks in the hope that common non-matching packets will be
1941 * rejected quickly. Assume that unicast IPv4 traffic in a
1942 * parallel tunnel (e.g. gif(4)) is unlikely.
1945 /* Obtain the outer IP header and the vif pointer. */
1946 m_copydata((struct mbuf
*)m
, 0, sizeof(ip
), (void *)&ip
);
1947 vifp
= (struct vif
*)arg
;
1950 * The outer source must match the vif's remote peer address.
1951 * For a multicast router with several tunnels, this is the
1952 * only check that will fail on packets in other tunnels,
1953 * assuming the local address is the same.
1955 if (!in_hosteq(vifp
->v_rmt_addr
, ip
.ip_src
))
1958 /* The outer destination must match the vif's local address. */
1959 if (!in_hosteq(vifp
->v_lcl_addr
, ip
.ip_dst
))
1962 /* The vif must be of tunnel type. */
1963 if ((vifp
->v_flags
& VIFF_TUNNEL
) == 0)
1966 /* Check that the inner destination is multicast. */
1967 m_copydata((struct mbuf
*)m
, off
, sizeof(ip
), (void *)&ip
);
1968 if (!IN_MULTICAST(ip
.ip_dst
.s_addr
))
1972 * We have checked that both the outer src and dst addresses
1973 * match the vif, and that the inner destination is multicast
1974 * (224/5). By claiming more than 64, we intend to
1975 * preferentially take packets that also match a parallel
1982 * Token bucket filter module
1985 tbf_control(struct vif
*vifp
, struct mbuf
*m
, struct ip
*ip
, u_int32_t len
)
1988 if (len
> MAX_BKT_SIZE
) {
1989 /* drop if packet is too large */
1990 mrtstat
.mrts_pkt2large
++;
1995 tbf_update_tokens(vifp
);
1998 * If there are enough tokens, and the queue is empty, send this packet
1999 * out immediately. Otherwise, try to insert it on this vif's queue.
2001 if (vifp
->tbf_q_len
== 0) {
2002 if (len
<= vifp
->tbf_n_tok
) {
2003 vifp
->tbf_n_tok
-= len
;
2004 tbf_send_packet(vifp
, m
);
2006 /* queue packet and timeout till later */
2008 callout_reset(&vifp
->v_repq_ch
, TBF_REPROCESS
,
2009 tbf_reprocess_q
, vifp
);
2012 if (vifp
->tbf_q_len
>= vifp
->tbf_max_q_len
&&
2013 !tbf_dq_sel(vifp
, ip
)) {
2014 /* queue full, and couldn't make room */
2015 mrtstat
.mrts_q_overflow
++;
2018 /* queue length low enough, or made room */
2020 tbf_process_q(vifp
);
2026 * adds a packet to the queue at the interface
2029 tbf_queue(struct vif
*vifp
, struct mbuf
*m
)
2031 int s
= splsoftnet();
2033 /* insert at tail */
2035 vifp
->tbf_t
= &m
->m_nextpkt
;
2043 * processes the queue at the interface
2046 tbf_process_q(struct vif
*vifp
)
2050 int s
= splsoftnet();
2053 * Loop through the queue at the interface and send as many packets
2056 for (m
= vifp
->tbf_q
; m
!= NULL
; m
= vifp
->tbf_q
) {
2057 len
= ntohs(mtod(m
, struct ip
*)->ip_len
);
2059 /* determine if the packet can be sent */
2060 if (len
<= vifp
->tbf_n_tok
) {
2062 * reduce no of tokens, dequeue the packet,
2065 if ((vifp
->tbf_q
= m
->m_nextpkt
) == NULL
)
2066 vifp
->tbf_t
= &vifp
->tbf_q
;
2069 m
->m_nextpkt
= NULL
;
2070 vifp
->tbf_n_tok
-= len
;
2071 tbf_send_packet(vifp
, m
);
2079 tbf_reprocess_q(void *arg
)
2081 struct vif
*vifp
= arg
;
2083 if (ip_mrouter
== NULL
)
2086 tbf_update_tokens(vifp
);
2087 tbf_process_q(vifp
);
2089 if (vifp
->tbf_q_len
!= 0)
2090 callout_reset(&vifp
->v_repq_ch
, TBF_REPROCESS
,
2091 tbf_reprocess_q
, vifp
);
2094 /* function that will selectively discard a member of the queue
2095 * based on the precedence value and the priority
2098 tbf_dq_sel(struct vif
*vifp
, struct ip
*ip
)
2101 struct mbuf
**mp
, *m
;
2102 int s
= splsoftnet();
2104 p
= priority(vifp
, ip
);
2106 for (mp
= &vifp
->tbf_q
, m
= *mp
;
2108 mp
= &m
->m_nextpkt
, m
= *mp
) {
2109 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
2110 if ((*mp
= m
->m_nextpkt
) == NULL
)
2115 mrtstat
.mrts_drop_sel
++;
2125 tbf_send_packet(struct vif
*vifp
, struct mbuf
*m
)
2128 int s
= splsoftnet();
2130 if (vifp
->v_flags
& VIFF_TUNNEL
) {
2131 /* If tunnel options */
2132 ip_output(m
, (struct mbuf
*)NULL
, &vifp
->v_route
,
2133 IP_FORWARDING
, (struct ip_moptions
*)NULL
,
2134 (struct socket
*)NULL
);
2136 /* if physical interface option, extract the options and then send */
2137 struct ip_moptions imo
;
2139 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
2140 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
2141 imo
.imo_multicast_loop
= 1;
2143 imo
.imo_multicast_vif
= -1;
2146 error
= ip_output(m
, NULL
, NULL
, IP_FORWARDING
|IP_MULTICASTOPTS
,
2149 if (mrtdebug
& DEBUG_XMIT
)
2150 log(LOG_DEBUG
, "phyint_send on vif %ld err %d\n",
2151 (long)(vifp
- viftable
), error
);
2156 /* determine the current time and then
2157 * the elapsed time (between the last time and time now)
2158 * in milliseconds & update the no. of tokens in the bucket
2161 tbf_update_tokens(struct vif
*vifp
)
2165 int s
= splsoftnet();
2169 TV_DELTA(tp
, vifp
->tbf_last_pkt_t
, tm
);
2172 * This formula is actually
2173 * "time in seconds" * "bytes/second".
2175 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
2177 * The (1000/1024) was introduced in add_vif to optimize
2178 * this divide into a shift.
2180 vifp
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 8192;
2181 vifp
->tbf_last_pkt_t
= tp
;
2183 if (vifp
->tbf_n_tok
> MAX_BKT_SIZE
)
2184 vifp
->tbf_n_tok
= MAX_BKT_SIZE
;
2190 priority(struct vif
*vifp
, struct ip
*ip
)
2192 int prio
= 50; /* the lowest priority -- default case */
2194 /* temporary hack; may add general packet classifier some day */
2197 * The UDP port space is divided up into four priority ranges:
2198 * [0, 16384) : unclassified - lowest priority
2199 * [16384, 32768) : audio - highest priority
2200 * [32768, 49152) : whiteboard - medium priority
2201 * [49152, 65536) : video - low priority
2203 if (ip
->ip_p
== IPPROTO_UDP
) {
2204 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
2206 switch (ntohs(udp
->uh_dport
) & 0xc000) {
2219 log(LOG_DEBUG
, "port %x prio %d\n",
2220 ntohs(udp
->uh_dport
), prio
);
2227 * End of token bucket filter modifications
2231 ip_rsvp_vif_init(struct socket
*so
, struct mbuf
*m
)
2236 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2237 so
->so_type
, so
->so_proto
->pr_protocol
);
2239 if (so
->so_type
!= SOCK_RAW
||
2240 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2241 return (EOPNOTSUPP
);
2244 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
2247 vifi
= *(mtod(m
, int *));
2250 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n",
2256 if (!legal_vif_num(vifi
)) {
2258 return (EADDRNOTAVAIL
);
2261 /* Check if socket is available. */
2262 if (viftable
[vifi
].v_rsvpd
!= NULL
) {
2264 return (EADDRINUSE
);
2267 viftable
[vifi
].v_rsvpd
= so
;
2269 * This may seem silly, but we need to be sure we don't over-increment
2270 * the RSVP counter, in case something slips up.
2272 if (!viftable
[vifi
].v_rsvp_on
) {
2273 viftable
[vifi
].v_rsvp_on
= 1;
2282 ip_rsvp_vif_done(struct socket
*so
, struct mbuf
*m
)
2287 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2288 so
->so_type
, so
->so_proto
->pr_protocol
);
2290 if (so
->so_type
!= SOCK_RAW
||
2291 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2292 return (EOPNOTSUPP
);
2295 if (m
== NULL
|| m
->m_len
!= sizeof(int)) {
2298 vifi
= *(mtod(m
, int *));
2303 if (!legal_vif_num(vifi
)) {
2305 return (EADDRNOTAVAIL
);
2309 printf("ip_rsvp_vif_done: v_rsvpd = %x so = %x\n",
2310 viftable
[vifi
].v_rsvpd
, so
);
2312 viftable
[vifi
].v_rsvpd
= NULL
;
2314 * This may seem silly, but we need to be sure we don't over-decrement
2315 * the RSVP counter, in case something slips up.
2317 if (viftable
[vifi
].v_rsvp_on
) {
2318 viftable
[vifi
].v_rsvp_on
= 0;
2327 ip_rsvp_force_done(struct socket
*so
)
2331 /* Don't bother if it is not the right type of socket. */
2332 if (so
->so_type
!= SOCK_RAW
||
2333 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2339 * The socket may be attached to more than one vif...this
2340 * is perfectly legal.
2342 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2343 if (viftable
[vifi
].v_rsvpd
== so
) {
2344 viftable
[vifi
].v_rsvpd
= NULL
;
2346 * This may seem silly, but we need to be sure we don't
2347 * over-decrement the RSVP counter, in case something
2350 if (viftable
[vifi
].v_rsvp_on
) {
2351 viftable
[vifi
].v_rsvp_on
= 0;
2362 rsvp_input(struct mbuf
*m
, struct ifnet
*ifp
)
2365 struct ip
*ip
= mtod(m
, struct ip
*);
2366 struct sockaddr_in rsvp_src
;
2369 printf("rsvp_input: rsvp_on %d\n", rsvp_on
);
2372 * Can still get packets with rsvp_on = 0 if there is a local member
2373 * of the group to which the RSVP packet is addressed. But in this
2374 * case we want to throw the packet away.
2382 * If the old-style non-vif-associated socket is set, then use
2383 * it and ignore the new ones.
2385 if (ip_rsvpd
!= NULL
) {
2387 printf("rsvp_input: "
2388 "Sending packet up old-style socket\n");
2389 rip_input(m
); /*XXX*/
2396 printf("rsvp_input: check vifs\n");
2398 /* Find which vif the packet arrived on. */
2399 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2400 if (viftable
[vifi
].v_ifp
== ifp
)
2404 if (vifi
== numvifs
) {
2405 /* Can't find vif packet arrived on. Drop packet. */
2407 printf("rsvp_input: "
2408 "Can't find vif for packet...dropping it.\n");
2415 printf("rsvp_input: check socket\n");
2417 if (viftable
[vifi
].v_rsvpd
== NULL
) {
2419 * drop packet, since there is no specific socket for this
2423 printf("rsvp_input: No socket defined for vif %d\n",
2430 sockaddr_in_init(&rsvp_src
, &ip
->ip_src
, 0);
2433 printf("rsvp_input: m->m_len = %d, sbspace() = %d\n",
2434 m
->m_len
, sbspace(&viftable
[vifi
].v_rsvpd
->so_rcv
));
2436 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0)
2438 printf("rsvp_input: Failed to append to socket\n");
2441 printf("rsvp_input: send packet up\n");
2445 #endif /* RSVP_ISI */
2448 * Code for bandwidth monitors
2452 * Define common interface for timeval-related methods
2454 #define BW_TIMEVALCMP(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp)
2455 #define BW_TIMEVALDECR(vvp, uvp) timersub((vvp), (uvp), (vvp))
2456 #define BW_TIMEVALADD(vvp, uvp) timeradd((vvp), (uvp), (vvp))
2459 compute_bw_meter_flags(struct bw_upcall
*req
)
2463 if (req
->bu_flags
& BW_UPCALL_UNIT_PACKETS
)
2464 flags
|= BW_METER_UNIT_PACKETS
;
2465 if (req
->bu_flags
& BW_UPCALL_UNIT_BYTES
)
2466 flags
|= BW_METER_UNIT_BYTES
;
2467 if (req
->bu_flags
& BW_UPCALL_GEQ
)
2468 flags
|= BW_METER_GEQ
;
2469 if (req
->bu_flags
& BW_UPCALL_LEQ
)
2470 flags
|= BW_METER_LEQ
;
2476 * Add a bw_meter entry
2479 add_bw_upcall(struct bw_upcall
*req
)
2483 struct timeval delta
= { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC
,
2484 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC
};
2489 if (!(mrt_api_config
& MRT_MFC_BW_UPCALL
))
2492 /* Test if the flags are valid */
2493 if (!(req
->bu_flags
& (BW_UPCALL_UNIT_PACKETS
| BW_UPCALL_UNIT_BYTES
)))
2495 if (!(req
->bu_flags
& (BW_UPCALL_GEQ
| BW_UPCALL_LEQ
)))
2497 if ((req
->bu_flags
& (BW_UPCALL_GEQ
| BW_UPCALL_LEQ
))
2498 == (BW_UPCALL_GEQ
| BW_UPCALL_LEQ
))
2501 /* Test if the threshold time interval is valid */
2502 if (BW_TIMEVALCMP(&req
->bu_threshold
.b_time
, &delta
, <))
2505 flags
= compute_bw_meter_flags(req
);
2508 * Find if we have already same bw_meter entry
2511 mfc
= mfc_find(&req
->bu_src
, &req
->bu_dst
);
2514 return EADDRNOTAVAIL
;
2516 for (x
= mfc
->mfc_bw_meter
; x
!= NULL
; x
= x
->bm_mfc_next
) {
2517 if ((BW_TIMEVALCMP(&x
->bm_threshold
.b_time
,
2518 &req
->bu_threshold
.b_time
, ==)) &&
2519 (x
->bm_threshold
.b_packets
== req
->bu_threshold
.b_packets
) &&
2520 (x
->bm_threshold
.b_bytes
== req
->bu_threshold
.b_bytes
) &&
2521 (x
->bm_flags
& BW_METER_USER_FLAGS
) == flags
) {
2523 return 0; /* XXX Already installed */
2527 /* Allocate the new bw_meter entry */
2528 x
= (struct bw_meter
*)malloc(sizeof(*x
), M_BWMETER
, M_NOWAIT
);
2534 /* Set the new bw_meter entry */
2535 x
->bm_threshold
.b_time
= req
->bu_threshold
.b_time
;
2537 x
->bm_start_time
= now
;
2538 x
->bm_threshold
.b_packets
= req
->bu_threshold
.b_packets
;
2539 x
->bm_threshold
.b_bytes
= req
->bu_threshold
.b_bytes
;
2540 x
->bm_measured
.b_packets
= 0;
2541 x
->bm_measured
.b_bytes
= 0;
2542 x
->bm_flags
= flags
;
2543 x
->bm_time_next
= NULL
;
2544 x
->bm_time_hash
= BW_METER_BUCKETS
;
2546 /* Add the new bw_meter entry to the front of entries for this MFC */
2548 x
->bm_mfc_next
= mfc
->mfc_bw_meter
;
2549 mfc
->mfc_bw_meter
= x
;
2550 schedule_bw_meter(x
, &now
);
2557 free_bw_list(struct bw_meter
*list
)
2559 while (list
!= NULL
) {
2560 struct bw_meter
*x
= list
;
2562 list
= list
->bm_mfc_next
;
2563 unschedule_bw_meter(x
);
2569 * Delete one or multiple bw_meter entries
2572 del_bw_upcall(struct bw_upcall
*req
)
2578 if (!(mrt_api_config
& MRT_MFC_BW_UPCALL
))
2582 /* Find the corresponding MFC entry */
2583 mfc
= mfc_find(&req
->bu_src
, &req
->bu_dst
);
2586 return EADDRNOTAVAIL
;
2587 } else if (req
->bu_flags
& BW_UPCALL_DELETE_ALL
) {
2589 * Delete all bw_meter entries for this mfc
2591 struct bw_meter
*list
;
2593 list
= mfc
->mfc_bw_meter
;
2594 mfc
->mfc_bw_meter
= NULL
;
2598 } else { /* Delete a single bw_meter entry */
2599 struct bw_meter
*prev
;
2602 flags
= compute_bw_meter_flags(req
);
2604 /* Find the bw_meter entry to delete */
2605 for (prev
= NULL
, x
= mfc
->mfc_bw_meter
; x
!= NULL
;
2606 prev
= x
, x
= x
->bm_mfc_next
) {
2607 if ((BW_TIMEVALCMP(&x
->bm_threshold
.b_time
,
2608 &req
->bu_threshold
.b_time
, ==)) &&
2609 (x
->bm_threshold
.b_packets
== req
->bu_threshold
.b_packets
) &&
2610 (x
->bm_threshold
.b_bytes
== req
->bu_threshold
.b_bytes
) &&
2611 (x
->bm_flags
& BW_METER_USER_FLAGS
) == flags
)
2614 if (x
!= NULL
) { /* Delete entry from the list for this MFC */
2616 prev
->bm_mfc_next
= x
->bm_mfc_next
; /* remove from middle*/
2618 x
->bm_mfc
->mfc_bw_meter
= x
->bm_mfc_next
;/* new head of list */
2620 unschedule_bw_meter(x
);
2622 /* Free the bw_meter entry */
2634 * Perform bandwidth measurement processing that may result in an upcall
2637 bw_meter_receive_packet(struct bw_meter
*x
, int plen
, struct timeval
*nowp
)
2639 struct timeval delta
;
2642 BW_TIMEVALDECR(&delta
, &x
->bm_start_time
);
2644 if (x
->bm_flags
& BW_METER_GEQ
) {
2646 * Processing for ">=" type of bw_meter entry
2648 if (BW_TIMEVALCMP(&delta
, &x
->bm_threshold
.b_time
, >)) {
2649 /* Reset the bw_meter entry */
2650 x
->bm_start_time
= *nowp
;
2651 x
->bm_measured
.b_packets
= 0;
2652 x
->bm_measured
.b_bytes
= 0;
2653 x
->bm_flags
&= ~BW_METER_UPCALL_DELIVERED
;
2656 /* Record that a packet is received */
2657 x
->bm_measured
.b_packets
++;
2658 x
->bm_measured
.b_bytes
+= plen
;
2661 * Test if we should deliver an upcall
2663 if (!(x
->bm_flags
& BW_METER_UPCALL_DELIVERED
)) {
2664 if (((x
->bm_flags
& BW_METER_UNIT_PACKETS
) &&
2665 (x
->bm_measured
.b_packets
>= x
->bm_threshold
.b_packets
)) ||
2666 ((x
->bm_flags
& BW_METER_UNIT_BYTES
) &&
2667 (x
->bm_measured
.b_bytes
>= x
->bm_threshold
.b_bytes
))) {
2668 /* Prepare an upcall for delivery */
2669 bw_meter_prepare_upcall(x
, nowp
);
2670 x
->bm_flags
|= BW_METER_UPCALL_DELIVERED
;
2673 } else if (x
->bm_flags
& BW_METER_LEQ
) {
2675 * Processing for "<=" type of bw_meter entry
2677 if (BW_TIMEVALCMP(&delta
, &x
->bm_threshold
.b_time
, >)) {
2679 * We are behind time with the multicast forwarding table
2680 * scanning for "<=" type of bw_meter entries, so test now
2681 * if we should deliver an upcall.
2683 if (((x
->bm_flags
& BW_METER_UNIT_PACKETS
) &&
2684 (x
->bm_measured
.b_packets
<= x
->bm_threshold
.b_packets
)) ||
2685 ((x
->bm_flags
& BW_METER_UNIT_BYTES
) &&
2686 (x
->bm_measured
.b_bytes
<= x
->bm_threshold
.b_bytes
))) {
2687 /* Prepare an upcall for delivery */
2688 bw_meter_prepare_upcall(x
, nowp
);
2690 /* Reschedule the bw_meter entry */
2691 unschedule_bw_meter(x
);
2692 schedule_bw_meter(x
, nowp
);
2695 /* Record that a packet is received */
2696 x
->bm_measured
.b_packets
++;
2697 x
->bm_measured
.b_bytes
+= plen
;
2700 * Test if we should restart the measuring interval
2702 if ((x
->bm_flags
& BW_METER_UNIT_PACKETS
&&
2703 x
->bm_measured
.b_packets
<= x
->bm_threshold
.b_packets
) ||
2704 (x
->bm_flags
& BW_METER_UNIT_BYTES
&&
2705 x
->bm_measured
.b_bytes
<= x
->bm_threshold
.b_bytes
)) {
2706 /* Don't restart the measuring interval */
2708 /* Do restart the measuring interval */
2710 * XXX: note that we don't unschedule and schedule, because this
2711 * might be too much overhead per packet. Instead, when we process
2712 * all entries for a given timer hash bin, we check whether it is
2713 * really a timeout. If not, we reschedule at that time.
2715 x
->bm_start_time
= *nowp
;
2716 x
->bm_measured
.b_packets
= 0;
2717 x
->bm_measured
.b_bytes
= 0;
2718 x
->bm_flags
&= ~BW_METER_UPCALL_DELIVERED
;
2724 * Prepare a bandwidth-related upcall
2727 bw_meter_prepare_upcall(struct bw_meter
*x
, struct timeval
*nowp
)
2729 struct timeval delta
;
2730 struct bw_upcall
*u
;
2733 * Compute the measured time interval
2736 BW_TIMEVALDECR(&delta
, &x
->bm_start_time
);
2739 * If there are too many pending upcalls, deliver them now
2741 if (bw_upcalls_n
>= BW_UPCALLS_MAX
)
2745 * Set the bw_upcall entry
2747 u
= &bw_upcalls
[bw_upcalls_n
++];
2748 u
->bu_src
= x
->bm_mfc
->mfc_origin
;
2749 u
->bu_dst
= x
->bm_mfc
->mfc_mcastgrp
;
2750 u
->bu_threshold
.b_time
= x
->bm_threshold
.b_time
;
2751 u
->bu_threshold
.b_packets
= x
->bm_threshold
.b_packets
;
2752 u
->bu_threshold
.b_bytes
= x
->bm_threshold
.b_bytes
;
2753 u
->bu_measured
.b_time
= delta
;
2754 u
->bu_measured
.b_packets
= x
->bm_measured
.b_packets
;
2755 u
->bu_measured
.b_bytes
= x
->bm_measured
.b_bytes
;
2757 if (x
->bm_flags
& BW_METER_UNIT_PACKETS
)
2758 u
->bu_flags
|= BW_UPCALL_UNIT_PACKETS
;
2759 if (x
->bm_flags
& BW_METER_UNIT_BYTES
)
2760 u
->bu_flags
|= BW_UPCALL_UNIT_BYTES
;
2761 if (x
->bm_flags
& BW_METER_GEQ
)
2762 u
->bu_flags
|= BW_UPCALL_GEQ
;
2763 if (x
->bm_flags
& BW_METER_LEQ
)
2764 u
->bu_flags
|= BW_UPCALL_LEQ
;
2768 * Send the pending bandwidth-related upcalls
2771 bw_upcalls_send(void)
2774 int len
= bw_upcalls_n
* sizeof(bw_upcalls
[0]);
2775 struct sockaddr_in k_igmpsrc
= {
2776 .sin_len
= sizeof(k_igmpsrc
),
2777 .sin_family
= AF_INET
,
2779 static struct igmpmsg igmpmsg
= { 0, /* unused1 */
2781 IGMPMSG_BW_UPCALL
,/* im_msgtype */
2786 { 0 } }; /* im_dst */
2788 if (bw_upcalls_n
== 0)
2789 return; /* No pending upcalls */
2794 * Allocate a new mbuf, initialize it with the header and
2795 * the payload for the pending calls.
2797 MGETHDR(m
, M_DONTWAIT
, MT_HEADER
);
2799 log(LOG_WARNING
, "bw_upcalls_send: cannot allocate mbuf\n");
2803 m
->m_len
= m
->m_pkthdr
.len
= 0;
2804 m_copyback(m
, 0, sizeof(struct igmpmsg
), (void *)&igmpmsg
);
2805 m_copyback(m
, sizeof(struct igmpmsg
), len
, (void *)&bw_upcalls
[0]);
2809 * XXX do we need to set the address in k_igmpsrc ?
2811 mrtstat
.mrts_upcalls
++;
2812 if (socket_send(ip_mrouter
, m
, &k_igmpsrc
) < 0) {
2813 log(LOG_WARNING
, "bw_upcalls_send: ip_mrouter socket queue full\n");
2814 ++mrtstat
.mrts_upq_sockfull
;
2819 * Compute the timeout hash value for the bw_meter entries
2821 #define BW_METER_TIMEHASH(bw_meter, hash) \
2823 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2825 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2826 (hash) = next_timeval.tv_sec; \
2827 if (next_timeval.tv_usec) \
2828 (hash)++; /* XXX: make sure we don't timeout early */ \
2829 (hash) %= BW_METER_BUCKETS; \
2830 } while (/*CONSTCOND*/ 0)
2833 * Schedule a timer to process periodically bw_meter entry of type "<="
2834 * by linking the entry in the proper hash bucket.
2837 schedule_bw_meter(struct bw_meter
*x
, struct timeval
*nowp
)
2841 if (!(x
->bm_flags
& BW_METER_LEQ
))
2842 return; /* XXX: we schedule timers only for "<=" entries */
2845 * Reset the bw_meter entry
2847 x
->bm_start_time
= *nowp
;
2848 x
->bm_measured
.b_packets
= 0;
2849 x
->bm_measured
.b_bytes
= 0;
2850 x
->bm_flags
&= ~BW_METER_UPCALL_DELIVERED
;
2853 * Compute the timeout hash value and insert the entry
2855 BW_METER_TIMEHASH(x
, time_hash
);
2856 x
->bm_time_next
= bw_meter_timers
[time_hash
];
2857 bw_meter_timers
[time_hash
] = x
;
2858 x
->bm_time_hash
= time_hash
;
2862 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2863 * by removing the entry from the proper hash bucket.
2866 unschedule_bw_meter(struct bw_meter
*x
)
2869 struct bw_meter
*prev
, *tmp
;
2871 if (!(x
->bm_flags
& BW_METER_LEQ
))
2872 return; /* XXX: we schedule timers only for "<=" entries */
2875 * Compute the timeout hash value and delete the entry
2877 time_hash
= x
->bm_time_hash
;
2878 if (time_hash
>= BW_METER_BUCKETS
)
2879 return; /* Entry was not scheduled */
2881 for (prev
= NULL
, tmp
= bw_meter_timers
[time_hash
];
2882 tmp
!= NULL
; prev
= tmp
, tmp
= tmp
->bm_time_next
)
2887 panic("unschedule_bw_meter: bw_meter entry not found");
2890 prev
->bm_time_next
= x
->bm_time_next
;
2892 bw_meter_timers
[time_hash
] = x
->bm_time_next
;
2894 x
->bm_time_next
= NULL
;
2895 x
->bm_time_hash
= BW_METER_BUCKETS
;
2899 * Process all "<=" type of bw_meter that should be processed now,
2900 * and for each entry prepare an upcall if necessary. Each processed
2901 * entry is rescheduled again for the (periodic) processing.
2903 * This is run periodically (once per second normally). On each round,
2904 * all the potentially matching entries are in the hash slot that we are
2908 bw_meter_process(void)
2911 static uint32_t last_tv_sec
; /* last time we processed this */
2915 struct timeval now
, process_endtime
;
2918 if (last_tv_sec
== now
.tv_sec
)
2919 return; /* nothing to do */
2921 loops
= now
.tv_sec
- last_tv_sec
;
2922 last_tv_sec
= now
.tv_sec
;
2923 if (loops
> BW_METER_BUCKETS
)
2924 loops
= BW_METER_BUCKETS
;
2928 * Process all bins of bw_meter entries from the one after the last
2929 * processed to the current one. On entry, i points to the last bucket
2930 * visited, so we need to increment i at the beginning of the loop.
2932 for (i
= (now
.tv_sec
- loops
) % BW_METER_BUCKETS
; loops
> 0; loops
--) {
2933 struct bw_meter
*x
, *tmp_list
;
2935 if (++i
>= BW_METER_BUCKETS
)
2938 /* Disconnect the list of bw_meter entries from the bin */
2939 tmp_list
= bw_meter_timers
[i
];
2940 bw_meter_timers
[i
] = NULL
;
2942 /* Process the list of bw_meter entries */
2943 while (tmp_list
!= NULL
) {
2945 tmp_list
= tmp_list
->bm_time_next
;
2947 /* Test if the time interval is over */
2948 process_endtime
= x
->bm_start_time
;
2949 BW_TIMEVALADD(&process_endtime
, &x
->bm_threshold
.b_time
);
2950 if (BW_TIMEVALCMP(&process_endtime
, &now
, >)) {
2951 /* Not yet: reschedule, but don't reset */
2954 BW_METER_TIMEHASH(x
, time_hash
);
2955 if (time_hash
== i
&& process_endtime
.tv_sec
== now
.tv_sec
) {
2957 * XXX: somehow the bin processing is a bit ahead of time.
2958 * Put the entry in the next bin.
2960 if (++time_hash
>= BW_METER_BUCKETS
)
2963 x
->bm_time_next
= bw_meter_timers
[time_hash
];
2964 bw_meter_timers
[time_hash
] = x
;
2965 x
->bm_time_hash
= time_hash
;
2971 * Test if we should deliver an upcall
2973 if (((x
->bm_flags
& BW_METER_UNIT_PACKETS
) &&
2974 (x
->bm_measured
.b_packets
<= x
->bm_threshold
.b_packets
)) ||
2975 ((x
->bm_flags
& BW_METER_UNIT_BYTES
) &&
2976 (x
->bm_measured
.b_bytes
<= x
->bm_threshold
.b_bytes
))) {
2977 /* Prepare an upcall for delivery */
2978 bw_meter_prepare_upcall(x
, &now
);
2982 * Reschedule for next processing
2984 schedule_bw_meter(x
, &now
);
2988 /* Send all upcalls that are pending delivery */
2995 * A periodic function for sending all upcalls that are pending delivery
2998 expire_bw_upcalls_send(void *unused
)
3006 callout_reset(&bw_upcalls_ch
, BW_UPCALLS_PERIOD
,
3007 expire_bw_upcalls_send
, NULL
);
3011 * A periodic function for periodic scanning of the multicast forwarding
3012 * table for processing all "<=" bw_meter entries.
3015 expire_bw_meter_process(void *unused
)
3017 if (mrt_api_config
& MRT_MFC_BW_UPCALL
)
3020 callout_reset(&bw_meter_ch
, BW_METER_PERIOD
,
3021 expire_bw_meter_process
, NULL
);
3025 * End of bandwidth monitoring code
3030 * Send the packet up to the user daemon, or eventually do kernel encapsulation
3033 pim_register_send(struct ip
*ip
, struct vif
*vifp
,
3034 struct mbuf
*m
, struct mfc
*rt
)
3036 struct mbuf
*mb_copy
, *mm
;
3038 if (mrtdebug
& DEBUG_PIM
)
3039 log(LOG_DEBUG
, "pim_register_send: ");
3041 mb_copy
= pim_register_prepare(ip
, m
);
3042 if (mb_copy
== NULL
)
3046 * Send all the fragments. Note that the mbuf for each fragment
3047 * is freed by the sending machinery.
3049 for (mm
= mb_copy
; mm
; mm
= mb_copy
) {
3050 mb_copy
= mm
->m_nextpkt
;
3051 mm
->m_nextpkt
= NULL
;
3052 mm
= m_pullup(mm
, sizeof(struct ip
));
3054 ip
= mtod(mm
, struct ip
*);
3055 if ((mrt_api_config
& MRT_MFC_RP
) &&
3056 !in_nullhost(rt
->mfc_rp
)) {
3057 pim_register_send_rp(ip
, vifp
, mm
, rt
);
3059 pim_register_send_upcall(ip
, vifp
, mm
, rt
);
3068 * Return a copy of the data packet that is ready for PIM Register
3070 * XXX: Note that in the returned copy the IP header is a valid one.
3072 static struct mbuf
*
3073 pim_register_prepare(struct ip
*ip
, struct mbuf
*m
)
3075 struct mbuf
*mb_copy
= NULL
;
3078 /* Take care of delayed checksums */
3079 if (m
->m_pkthdr
.csum_flags
& (M_CSUM_TCPv4
|M_CSUM_UDPv4
)) {
3080 in_delayed_cksum(m
);
3081 m
->m_pkthdr
.csum_flags
&= ~(M_CSUM_TCPv4
|M_CSUM_UDPv4
);
3085 * Copy the old packet & pullup its IP header into the
3086 * new mbuf so we can modify it.
3088 mb_copy
= m_copypacket(m
, M_DONTWAIT
);
3089 if (mb_copy
== NULL
)
3091 mb_copy
= m_pullup(mb_copy
, ip
->ip_hl
<< 2);
3092 if (mb_copy
== NULL
)
3095 /* take care of the TTL */
3096 ip
= mtod(mb_copy
, struct ip
*);
3099 /* Compute the MTU after the PIM Register encapsulation */
3100 mtu
= 0xffff - sizeof(pim_encap_iphdr
) - sizeof(pim_encap_pimhdr
);
3102 if (ntohs(ip
->ip_len
) <= mtu
) {
3103 /* Turn the IP header into a valid one */
3105 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
3107 /* Fragment the packet */
3108 if (ip_fragment(mb_copy
, NULL
, mtu
) != 0) {
3109 /* XXX: mb_copy was freed by ip_fragment() */
3117 * Send an upcall with the data packet to the user-level process.
3120 pim_register_send_upcall(struct ip
*ip
, struct vif
*vifp
,
3121 struct mbuf
*mb_copy
, struct mfc
*rt
)
3123 struct mbuf
*mb_first
;
3124 int len
= ntohs(ip
->ip_len
);
3126 struct sockaddr_in k_igmpsrc
= {
3127 .sin_len
= sizeof(k_igmpsrc
),
3128 .sin_family
= AF_INET
,
3132 * Add a new mbuf with an upcall header
3134 MGETHDR(mb_first
, M_DONTWAIT
, MT_HEADER
);
3135 if (mb_first
== NULL
) {
3139 mb_first
->m_data
+= max_linkhdr
;
3140 mb_first
->m_pkthdr
.len
= len
+ sizeof(struct igmpmsg
);
3141 mb_first
->m_len
= sizeof(struct igmpmsg
);
3142 mb_first
->m_next
= mb_copy
;
3144 /* Send message to routing daemon */
3145 im
= mtod(mb_first
, struct igmpmsg
*);
3146 im
->im_msgtype
= IGMPMSG_WHOLEPKT
;
3148 im
->im_vif
= vifp
- viftable
;
3149 im
->im_src
= ip
->ip_src
;
3150 im
->im_dst
= ip
->ip_dst
;
3152 k_igmpsrc
.sin_addr
= ip
->ip_src
;
3154 mrtstat
.mrts_upcalls
++;
3156 if (socket_send(ip_mrouter
, mb_first
, &k_igmpsrc
) < 0) {
3157 if (mrtdebug
& DEBUG_PIM
)
3159 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
3160 ++mrtstat
.mrts_upq_sockfull
;
3164 /* Keep statistics */
3165 pimstat
.pims_snd_registers_msgs
++;
3166 pimstat
.pims_snd_registers_bytes
+= len
;
3172 * Encapsulate the data packet in PIM Register message and send it to the RP.
3175 pim_register_send_rp(struct ip
*ip
, struct vif
*vifp
,
3176 struct mbuf
*mb_copy
, struct mfc
*rt
)
3178 struct mbuf
*mb_first
;
3179 struct ip
*ip_outer
;
3180 struct pim_encap_pimhdr
*pimhdr
;
3181 int len
= ntohs(ip
->ip_len
);
3182 vifi_t vifi
= rt
->mfc_parent
;
3184 if ((vifi
>= numvifs
) || in_nullhost(viftable
[vifi
].v_lcl_addr
)) {
3186 return EADDRNOTAVAIL
; /* The iif vif is invalid */
3190 * Add a new mbuf with the encapsulating header
3192 MGETHDR(mb_first
, M_DONTWAIT
, MT_HEADER
);
3193 if (mb_first
== NULL
) {
3197 mb_first
->m_data
+= max_linkhdr
;
3198 mb_first
->m_len
= sizeof(pim_encap_iphdr
) + sizeof(pim_encap_pimhdr
);
3199 mb_first
->m_next
= mb_copy
;
3201 mb_first
->m_pkthdr
.len
= len
+ mb_first
->m_len
;
3204 * Fill in the encapsulating IP and PIM header
3206 ip_outer
= mtod(mb_first
, struct ip
*);
3207 *ip_outer
= pim_encap_iphdr
;
3208 if (mb_first
->m_pkthdr
.len
< IP_MINFRAGSIZE
)
3209 ip_outer
->ip_id
= 0;
3211 ip_outer
->ip_id
= ip_newid(NULL
);
3212 ip_outer
->ip_len
= htons(len
+ sizeof(pim_encap_iphdr
) +
3213 sizeof(pim_encap_pimhdr
));
3214 ip_outer
->ip_src
= viftable
[vifi
].v_lcl_addr
;
3215 ip_outer
->ip_dst
= rt
->mfc_rp
;
3217 * Copy the inner header TOS to the outer header, and take care of the
3220 ip_outer
->ip_tos
= ip
->ip_tos
;
3221 if (ntohs(ip
->ip_off
) & IP_DF
)
3222 ip_outer
->ip_off
|= htons(IP_DF
);
3223 pimhdr
= (struct pim_encap_pimhdr
*)((char *)ip_outer
3224 + sizeof(pim_encap_iphdr
));
3225 *pimhdr
= pim_encap_pimhdr
;
3226 /* If the iif crosses a border, set the Border-bit */
3227 if (rt
->mfc_flags
[vifi
] & MRT_MFC_FLAGS_BORDER_VIF
& mrt_api_config
)
3228 pimhdr
->flags
|= htonl(PIM_BORDER_REGISTER
);
3230 mb_first
->m_data
+= sizeof(pim_encap_iphdr
);
3231 pimhdr
->pim
.pim_cksum
= in_cksum(mb_first
, sizeof(pim_encap_pimhdr
));
3232 mb_first
->m_data
-= sizeof(pim_encap_iphdr
);
3234 if (vifp
->v_rate_limit
== 0)
3235 tbf_send_packet(vifp
, mb_first
);
3237 tbf_control(vifp
, mb_first
, ip
, ntohs(ip_outer
->ip_len
));
3239 /* Keep statistics */
3240 pimstat
.pims_snd_registers_msgs
++;
3241 pimstat
.pims_snd_registers_bytes
+= len
;
3247 * PIM-SMv2 and PIM-DM messages processing.
3248 * Receives and verifies the PIM control messages, and passes them
3249 * up to the listening socket, using rip_input().
3250 * The only message with special processing is the PIM_REGISTER message
3251 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3252 * is passed to if_simloop().
3255 pim_input(struct mbuf
*m
, ...)
3257 struct ip
*ip
= mtod(m
, struct ip
*);
3267 iphlen
= va_arg(ap
, int);
3268 proto
= va_arg(ap
, int);
3271 datalen
= ntohs(ip
->ip_len
) - iphlen
;
3273 /* Keep statistics */
3274 pimstat
.pims_rcv_total_msgs
++;
3275 pimstat
.pims_rcv_total_bytes
+= datalen
;
3280 if (datalen
< PIM_MINLEN
) {
3281 pimstat
.pims_rcv_tooshort
++;
3282 log(LOG_ERR
, "pim_input: packet size too small %d from %lx\n",
3283 datalen
, (u_long
)ip
->ip_src
.s_addr
);
3289 * If the packet is at least as big as a REGISTER, go agead
3290 * and grab the PIM REGISTER header size, to avoid another
3291 * possible m_pullup() later.
3293 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3294 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3296 minlen
= iphlen
+ (datalen
>= PIM_REG_MINLEN
? PIM_REG_MINLEN
: PIM_MINLEN
);
3298 * Get the IP and PIM headers in contiguous memory, and
3299 * possibly the PIM REGISTER header.
3301 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
3302 (m
= m_pullup(m
, minlen
)) == NULL
) {
3303 log(LOG_ERR
, "pim_input: m_pullup failure\n");
3306 /* m_pullup() may have given us a new mbuf so reset ip. */
3307 ip
= mtod(m
, struct ip
*);
3308 ip_tos
= ip
->ip_tos
;
3310 /* adjust mbuf to point to the PIM header */
3311 m
->m_data
+= iphlen
;
3313 pim
= mtod(m
, struct pim
*);
3316 * Validate checksum. If PIM REGISTER, exclude the data packet.
3318 * XXX: some older PIMv2 implementations don't make this distinction,
3319 * so for compatibility reason perform the checksum over part of the
3320 * message, and if error, then over the whole message.
3322 if (PIM_VT_T(pim
->pim_vt
) == PIM_REGISTER
&& in_cksum(m
, PIM_MINLEN
) == 0) {
3323 /* do nothing, checksum okay */
3324 } else if (in_cksum(m
, datalen
)) {
3325 pimstat
.pims_rcv_badsum
++;
3326 if (mrtdebug
& DEBUG_PIM
)
3327 log(LOG_DEBUG
, "pim_input: invalid checksum");
3332 /* PIM version check */
3333 if (PIM_VT_V(pim
->pim_vt
) < PIM_VERSION
) {
3334 pimstat
.pims_rcv_badversion
++;
3335 log(LOG_ERR
, "pim_input: incorrect version %d, expecting %d\n",
3336 PIM_VT_V(pim
->pim_vt
), PIM_VERSION
);
3341 /* restore mbuf back to the outer IP */
3342 m
->m_data
-= iphlen
;
3345 if (PIM_VT_T(pim
->pim_vt
) == PIM_REGISTER
) {
3347 * Since this is a REGISTER, we'll make a copy of the register
3348 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3352 struct sockaddr_in dst
= {
3353 .sin_len
= sizeof(dst
),
3354 .sin_family
= AF_INET
,
3357 struct ip
*encap_ip
;
3362 if ((reg_vif_num
>= numvifs
) || (reg_vif_num
== VIFI_INVALID
)) {
3364 if (mrtdebug
& DEBUG_PIM
)
3366 "pim_input: register vif not set: %d\n", reg_vif_num
);
3370 /* XXX need refcnt? */
3371 vifp
= viftable
[reg_vif_num
].v_ifp
;
3377 if (datalen
< PIM_REG_MINLEN
) {
3378 pimstat
.pims_rcv_tooshort
++;
3379 pimstat
.pims_rcv_badregisters
++;
3381 "pim_input: register packet size too small %d from %lx\n",
3382 datalen
, (u_long
)ip
->ip_src
.s_addr
);
3387 reghdr
= (u_int32_t
*)(pim
+ 1);
3388 encap_ip
= (struct ip
*)(reghdr
+ 1);
3390 if (mrtdebug
& DEBUG_PIM
) {
3392 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3393 (u_long
)ntohl(encap_ip
->ip_src
.s_addr
),
3394 (u_long
)ntohl(encap_ip
->ip_dst
.s_addr
),
3395 ntohs(encap_ip
->ip_len
));
3398 /* verify the version number of the inner packet */
3399 if (encap_ip
->ip_v
!= IPVERSION
) {
3400 pimstat
.pims_rcv_badregisters
++;
3401 if (mrtdebug
& DEBUG_PIM
) {
3402 log(LOG_DEBUG
, "pim_input: invalid IP version (%d) "
3403 "of the inner packet\n", encap_ip
->ip_v
);
3409 /* verify the inner packet is destined to a mcast group */
3410 if (!IN_MULTICAST(encap_ip
->ip_dst
.s_addr
)) {
3411 pimstat
.pims_rcv_badregisters
++;
3412 if (mrtdebug
& DEBUG_PIM
)
3414 "pim_input: inner packet of register is not "
3416 (u_long
)ntohl(encap_ip
->ip_dst
.s_addr
));
3421 /* If a NULL_REGISTER, pass it to the daemon */
3422 if ((ntohl(*reghdr
) & PIM_NULL_REGISTER
))
3423 goto pim_input_to_daemon
;
3426 * Copy the TOS from the outer IP header to the inner IP header.
3428 if (encap_ip
->ip_tos
!= ip_tos
) {
3429 /* Outer TOS -> inner TOS */
3430 encap_ip
->ip_tos
= ip_tos
;
3431 /* Recompute the inner header checksum. Sigh... */
3433 /* adjust mbuf to point to the inner IP header */
3434 m
->m_data
+= (iphlen
+ PIM_MINLEN
);
3435 m
->m_len
-= (iphlen
+ PIM_MINLEN
);
3437 encap_ip
->ip_sum
= 0;
3438 encap_ip
->ip_sum
= in_cksum(m
, encap_ip
->ip_hl
<< 2);
3440 /* restore mbuf to point back to the outer IP header */
3441 m
->m_data
-= (iphlen
+ PIM_MINLEN
);
3442 m
->m_len
+= (iphlen
+ PIM_MINLEN
);
3446 * Decapsulate the inner IP packet and loopback to forward it
3447 * as a normal multicast packet. Also, make a copy of the
3448 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3449 * to pass to the daemon later, so it can take the appropriate
3450 * actions (e.g., send back PIM_REGISTER_STOP).
3451 * XXX: here m->m_data points to the outer IP header.
3453 mcp
= m_copym(m
, 0, iphlen
+ PIM_REG_MINLEN
, M_DONTWAIT
);
3456 "pim_input: pim register: could not copy register head\n");
3461 /* Keep statistics */
3462 /* XXX: registers_bytes include only the encap. mcast pkt */
3463 pimstat
.pims_rcv_registers_msgs
++;
3464 pimstat
.pims_rcv_registers_bytes
+= ntohs(encap_ip
->ip_len
);
3467 * forward the inner ip packet; point m_data at the inner ip.
3469 m_adj(m
, iphlen
+ PIM_MINLEN
);
3471 if (mrtdebug
& DEBUG_PIM
) {
3473 "pim_input: forwarding decapsulated register: "
3474 "src %lx, dst %lx, vif %d\n",
3475 (u_long
)ntohl(encap_ip
->ip_src
.s_addr
),
3476 (u_long
)ntohl(encap_ip
->ip_dst
.s_addr
),
3479 /* NB: vifp was collected above; can it change on us? */
3480 looutput(vifp
, m
, (struct sockaddr
*)&dst
, (struct rtentry
*)NULL
);
3482 /* prepare the register head to send to the mrouting daemon */
3486 pim_input_to_daemon
:
3488 * Pass the PIM message up to the daemon; if it is a Register message,
3489 * pass the 'head' only up to the daemon. This includes the
3490 * outer IP header, PIM header, PIM-Register header and the
3492 * XXX: the outer IP header pkt size of a Register is not adjust to
3493 * reflect the fact that the inner multicast data is truncated.
3495 rip_input(m
, iphlen
, proto
);