2 * IP multicast forwarding procedures
4 * Written by David Waitzman, BBN Labs, August 1988.
5 * Modified by Steve Deering, Stanford, February 1989.
6 * Modified by Mark J. Steiglitz, Stanford, May, 1991
7 * Modified by Van Jacobson, LBL, January 1993
8 * Modified by Ajit Thyagarajan, PARC, August 1993
9 * Modified by Bill Fenner, PARC, April 1995
10 * Modified by Ahmed Helmy, SGI, June 1996
11 * Modified by George Edmond Eddy (Rusty), ISI, February 1998
12 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000
13 * Modified by Hitoshi Asaeda, WIDE, August 2000
14 * Modified by Pavlin Radoslavov, ICSI, October 2002
16 * MROUTING Revision: 3.5
17 * and PIM-SMv2 and PIM-DM support, advanced API support,
18 * bandwidth metering and signaling
20 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.10 2003/08/24 21:37:34 hsu Exp $
21 * $DragonFly: src/sys/net/ip_mroute/ip_mroute.c,v 1.23 2008/06/17 20:50:11 aggelos Exp $
24 #include "opt_mrouting.h"
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
34 #include <sys/protosw.h>
35 #include <sys/socket.h>
36 #include <sys/socketvar.h>
37 #include <sys/sockio.h>
38 #include <sys/sysctl.h>
39 #include <sys/syslog.h>
40 #include <sys/systm.h>
41 #include <sys/thread2.h>
43 #include <sys/in_cksum.h>
45 #include <machine/stdarg.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/igmp.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/in_var.h>
54 #include <netinet/ip.h>
55 #include "ip_mroute.h"
56 #include <netinet/ip_var.h>
58 #include <netinet/pim.h>
59 #include <netinet/pim_var.h>
62 #include <netinet/in_pcb.h>
64 #include <netinet/udp.h>
67 * Control debugging code for rsvp and multicast routing code.
68 * Can only set them with the debugger.
70 static u_int rsvpdebug
; /* non-zero enables debugging */
72 static u_int mrtdebug
; /* any set of the flags below */
74 #define DEBUG_MFC 0x02
75 #define DEBUG_FORWARD 0x04
76 #define DEBUG_EXPIRE 0x08
77 #define DEBUG_XMIT 0x10
78 #define DEBUG_PIM 0x20
80 #define VIFI_INVALID ((vifi_t) -1)
82 #define M_HASCL(m) ((m)->m_flags & M_EXT)
84 static MALLOC_DEFINE(M_MRTABLE
, "mroutetbl", "multicast routing tables");
86 static struct mrtstat mrtstat
;
87 SYSCTL_STRUCT(_net_inet_ip
, OID_AUTO
, mrtstat
, CTLFLAG_RW
,
89 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)");
91 static struct mfc
*mfctable
[MFCTBLSIZ
];
92 SYSCTL_OPAQUE(_net_inet_ip
, OID_AUTO
, mfctable
, CTLFLAG_RD
,
93 &mfctable
, sizeof(mfctable
), "S,*mfc[MFCTBLSIZ]",
94 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)");
96 static struct vif viftable
[MAXVIFS
];
97 SYSCTL_OPAQUE(_net_inet_ip
, OID_AUTO
, viftable
, CTLFLAG_RD
,
98 &viftable
, sizeof(viftable
), "S,vif[MAXVIFS]",
99 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)");
101 static u_char nexpire
[MFCTBLSIZ
];
103 static struct callout expire_upcalls_ch
;
104 static struct callout tbf_reprocess_q_ch
;
105 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
106 #define UPCALL_EXPIRE 6 /* number of timeouts */
109 * Define the token bucket filter structures
110 * tbftable -> each vif has one of these for storing info
113 static struct tbf tbftable
[MAXVIFS
];
114 #define TBF_REPROCESS (hz / 100) /* 100x / second */
117 * 'Interfaces' associated with decapsulator (so we can tell
118 * packets that went through it from ones that get reflected
119 * by a broken gateway). These interfaces are never linked into
120 * the system ifnet list & no routes point to them. I.e., packets
121 * can't be sent this way. They only exist as a placeholder for
122 * multicast source verification.
124 static struct ifnet multicast_decap_if
[MAXVIFS
];
127 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
129 /* prototype IP hdr for encapsulated packets */
130 static struct ip multicast_encap_iphdr
= {
131 #if BYTE_ORDER == LITTLE_ENDIAN
132 sizeof(struct ip
) >> 2, IPVERSION
,
134 IPVERSION
, sizeof(struct ip
) >> 2,
137 sizeof(struct ip
), /* total length */
140 ENCAP_TTL
, ENCAP_PROTO
,
145 * Bandwidth meter variables and constants
147 static MALLOC_DEFINE(M_BWMETER
, "bwmeter", "multicast upcall bw meters");
149 * Pending timeouts are stored in a hash table, the key being the
150 * expiration time. Periodically, the entries are analysed and processed.
152 #define BW_METER_BUCKETS 1024
153 static struct bw_meter
*bw_meter_timers
[BW_METER_BUCKETS
];
154 static struct callout bw_meter_ch
;
155 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */
158 * Pending upcalls are stored in a vector which is flushed when
159 * full, or periodically
161 static struct bw_upcall bw_upcalls
[BW_UPCALLS_MAX
];
162 static u_int bw_upcalls_n
; /* # of pending upcalls */
163 static struct callout bw_upcalls_ch
;
164 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */
167 static struct pimstat pimstat
;
168 SYSCTL_STRUCT(_net_inet_pim
, PIMCTL_STATS
, stats
, CTLFLAG_RD
,
170 "PIM Statistics (struct pimstat, netinet/pim_var.h)");
173 * Note: the PIM Register encapsulation adds the following in front of a
176 * struct pim_encap_hdr {
178 * struct pim_encap_pimhdr pim;
183 struct pim_encap_pimhdr
{
188 static struct ip pim_encap_iphdr
= {
189 #if BYTE_ORDER == LITTLE_ENDIAN
190 sizeof(struct ip
) >> 2,
194 sizeof(struct ip
) >> 2,
197 sizeof(struct ip
), /* total length */
205 static struct pim_encap_pimhdr pim_encap_pimhdr
= {
207 PIM_MAKE_VT(PIM_VERSION
, PIM_REGISTER
), /* PIM vers and message type */
214 static struct ifnet multicast_register_if
;
215 static vifi_t reg_vif_num
= VIFI_INVALID
;
221 static vifi_t numvifs
;
222 static int have_encap_tunnel
;
225 * one-back cache used by ipip_input to locate a tunnel's vif
226 * given a datagram's src ip address.
228 static u_long last_encap_src
;
229 static struct vif
*last_encap_vif
;
231 static u_long
X_ip_mcast_src(int vifi
);
232 static int X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
,
233 struct mbuf
*m
, struct ip_moptions
*imo
);
234 static int X_ip_mrouter_done(void);
235 static int X_ip_mrouter_get(struct socket
*so
, struct sockopt
*m
);
236 static int X_ip_mrouter_set(struct socket
*so
, struct sockopt
*m
);
237 static int X_legal_vif_num(int vif
);
238 static int X_mrt_ioctl(int cmd
, caddr_t data
);
240 static int get_sg_cnt(struct sioc_sg_req
*);
241 static int get_vif_cnt(struct sioc_vif_req
*);
242 static int ip_mrouter_init(struct socket
*, int);
243 static int add_vif(struct vifctl
*);
244 static int del_vif(vifi_t
);
245 static int add_mfc(struct mfcctl2
*);
246 static int del_mfc(struct mfcctl2
*);
247 static int set_api_config(uint32_t *); /* chose API capabilities */
248 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
249 static int set_assert(int);
250 static void expire_upcalls(void *);
251 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*, vifi_t
);
252 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
253 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
254 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
255 static void tbf_queue(struct vif
*, struct mbuf
*);
256 static void tbf_process_q(struct vif
*);
257 static void tbf_reprocess_q(void *);
258 static int tbf_dq_sel(struct vif
*, struct ip
*);
259 static void tbf_send_packet(struct vif
*, struct mbuf
*);
260 static void tbf_update_tokens(struct vif
*);
261 static int priority(struct vif
*, struct ip
*);
264 * Bandwidth monitoring
266 static void free_bw_list(struct bw_meter
*list
);
267 static int add_bw_upcall(struct bw_upcall
*);
268 static int del_bw_upcall(struct bw_upcall
*);
269 static void bw_meter_receive_packet(struct bw_meter
*x
, int plen
,
270 struct timeval
*nowp
);
271 static void bw_meter_prepare_upcall(struct bw_meter
*x
, struct timeval
*nowp
);
272 static void bw_upcalls_send(void);
273 static void schedule_bw_meter(struct bw_meter
*x
, struct timeval
*nowp
);
274 static void unschedule_bw_meter(struct bw_meter
*x
);
275 static void bw_meter_process(void);
276 static void expire_bw_upcalls_send(void *);
277 static void expire_bw_meter_process(void *);
280 static int pim_register_send(struct ip
*, struct vif
*,
281 struct mbuf
*, struct mfc
*);
282 static int pim_register_send_rp(struct ip
*, struct vif
*,
283 struct mbuf
*, struct mfc
*);
284 static int pim_register_send_upcall(struct ip
*, struct vif
*,
285 struct mbuf
*, struct mfc
*);
286 static struct mbuf
*pim_register_prepare(struct ip
*, struct mbuf
*);
290 * whether or not special PIM assert processing is enabled.
292 static int pim_assert
;
294 * Rate limit for assert notification messages, in usec
296 #define ASSERT_MSG_TIME 3000000
299 * Kernel multicast routing API capabilities and setup.
300 * If more API capabilities are added to the kernel, they should be
301 * recorded in `mrt_api_support'.
303 static const uint32_t mrt_api_support
= (MRT_MFC_FLAGS_DISABLE_WRONGVIF
|
304 MRT_MFC_FLAGS_BORDER_VIF
|
307 static uint32_t mrt_api_config
= 0;
310 * Hash function for a source, group entry
312 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
313 ((g) >> 20) ^ ((g) >> 10) ^ (g))
316 * Find a route for a given origin IP address and Multicast group address
317 * Type of service parameter to be added in the future!!!
318 * Statistics are updated by the caller if needed
319 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses)
322 mfc_find(in_addr_t o
, in_addr_t g
)
326 for (rt
= mfctable
[MFCHASH(o
,g
)]; rt
; rt
= rt
->mfc_next
)
327 if ((rt
->mfc_origin
.s_addr
== o
) &&
328 (rt
->mfc_mcastgrp
.s_addr
== g
) && (rt
->mfc_stall
== NULL
))
334 * Macros to compute elapsed time efficiently
335 * Borrowed from Van Jacobson's scheduling code
337 #define TV_DELTA(a, b, delta) { \
339 delta = (a).tv_usec - (b).tv_usec; \
340 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
349 delta += (1000000 * xxs); \
354 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
355 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
358 * Handle MRT setsockopt commands to modify the multicast routing tables.
361 X_ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
)
367 struct bw_upcall bw_upcall
;
370 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
374 switch (sopt
->sopt_name
) {
376 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
, sizeof optval
);
379 error
= ip_mrouter_init(so
, optval
);
383 error
= ip_mrouter_done();
387 error
= soopt_to_kbuf(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
390 error
= add_vif(&vifc
);
394 error
= soopt_to_kbuf(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
397 error
= del_vif(vifi
);
403 * select data size depending on API version.
405 if (sopt
->sopt_name
== MRT_ADD_MFC
&&
406 mrt_api_config
& MRT_API_FLAGS_ALL
) {
407 error
= soopt_to_kbuf(sopt
, &mfc
, sizeof(struct mfcctl2
),
408 sizeof(struct mfcctl2
));
410 error
= soopt_to_kbuf(sopt
, &mfc
, sizeof(struct mfcctl
),
411 sizeof(struct mfcctl
));
412 bzero((caddr_t
)&mfc
+ sizeof(struct mfcctl
),
413 sizeof(mfc
) - sizeof(struct mfcctl
));
417 if (sopt
->sopt_name
== MRT_ADD_MFC
)
418 error
= add_mfc(&mfc
);
420 error
= del_mfc(&mfc
);
424 error
= soopt_to_kbuf(sopt
, &optval
, sizeof optval
, sizeof optval
);
431 error
= soopt_to_kbuf(sopt
, &i
, sizeof i
, sizeof i
);
433 error
= set_api_config(&i
);
435 soopt_from_kbuf(sopt
, &i
, sizeof i
);
438 case MRT_ADD_BW_UPCALL
:
439 case MRT_DEL_BW_UPCALL
:
440 error
= soopt_to_kbuf(sopt
, &bw_upcall
, sizeof bw_upcall
, sizeof bw_upcall
);
443 if (sopt
->sopt_name
== MRT_ADD_BW_UPCALL
)
444 error
= add_bw_upcall(&bw_upcall
);
446 error
= del_bw_upcall(&bw_upcall
);
457 * Handle MRT getsockopt commands
460 X_ip_mrouter_get(struct socket
*so
, struct sockopt
*sopt
)
463 static int version
= 0x0305; /* !!! why is this here? XXX */
466 switch (sopt
->sopt_name
) {
468 soopt_from_kbuf(sopt
, &version
, sizeof version
);
472 soopt_from_kbuf(sopt
, &pim_assert
, sizeof pim_assert
);
475 case MRT_API_SUPPORT
:
476 soopt_from_kbuf(sopt
, &mrt_api_support
, sizeof mrt_api_support
);
480 soopt_from_kbuf(sopt
, &mrt_api_config
, sizeof mrt_api_config
);
491 * Handle ioctl commands to obtain information from the cache
494 X_mrt_ioctl(int cmd
, caddr_t data
)
500 error
= get_vif_cnt((struct sioc_vif_req
*)data
);
504 error
= get_sg_cnt((struct sioc_sg_req
*)data
);
515 * returns the packet, byte, rpf-failure count for the source group provided
518 get_sg_cnt(struct sioc_sg_req
*req
)
523 rt
= mfc_find(req
->src
.s_addr
, req
->grp
.s_addr
);
526 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
527 return EADDRNOTAVAIL
;
529 req
->pktcnt
= rt
->mfc_pkt_cnt
;
530 req
->bytecnt
= rt
->mfc_byte_cnt
;
531 req
->wrong_if
= rt
->mfc_wrong_if
;
536 * returns the input and output packet and byte counts on the vif provided
539 get_vif_cnt(struct sioc_vif_req
*req
)
541 vifi_t vifi
= req
->vifi
;
546 req
->icount
= viftable
[vifi
].v_pkt_in
;
547 req
->ocount
= viftable
[vifi
].v_pkt_out
;
548 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
549 req
->obytes
= viftable
[vifi
].v_bytes_out
;
555 * Enable multicast routing
558 ip_mrouter_init(struct socket
*so
, int version
)
561 log(LOG_DEBUG
, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
562 so
->so_type
, so
->so_proto
->pr_protocol
);
564 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
)
570 if (ip_mrouter
!= NULL
)
575 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
576 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
580 bzero((caddr_t
)bw_meter_timers
, sizeof(bw_meter_timers
));
582 callout_init(&expire_upcalls_ch
);
583 callout_init(&bw_upcalls_ch
);
584 callout_init(&bw_meter_ch
);
585 callout_init(&tbf_reprocess_q_ch
);
587 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
, expire_upcalls
, NULL
);
588 callout_reset(&bw_upcalls_ch
, BW_UPCALLS_PERIOD
,
589 expire_bw_upcalls_send
, NULL
);
590 callout_reset(&bw_meter_ch
, BW_METER_PERIOD
, expire_bw_meter_process
, NULL
);
595 log(LOG_DEBUG
, "ip_mrouter_init\n");
601 * Disable multicast routing
604 X_ip_mrouter_done(void)
616 * For each phyint in use, disable promiscuous reception of all IP
619 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
620 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
621 !(viftable
[vifi
].v_flags
& (VIFF_TUNNEL
| VIFF_REGISTER
))) {
622 struct sockaddr_in
*so
= (struct sockaddr_in
*)&(ifr
.ifr_addr
);
624 so
->sin_len
= sizeof(struct sockaddr_in
);
625 so
->sin_family
= AF_INET
;
626 so
->sin_addr
.s_addr
= INADDR_ANY
;
627 ifp
= viftable
[vifi
].v_ifp
;
631 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
632 bzero((caddr_t
)viftable
, sizeof(viftable
));
636 callout_stop(&expire_upcalls_ch
);
640 callout_stop(&bw_upcalls_ch
);
641 callout_stop(&bw_meter_ch
);
642 callout_stop(&tbf_reprocess_q_ch
);
645 * Free all multicast forwarding cache entries.
647 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
648 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
649 struct mfc
*nr
= rt
->mfc_next
;
651 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
652 struct rtdetq
*n
= rte
->next
;
655 kfree(rte
, M_MRTABLE
);
658 free_bw_list(rt
->mfc_bw_meter
);
659 kfree(rt
, M_MRTABLE
);
664 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
666 bzero(bw_meter_timers
, sizeof(bw_meter_timers
));
669 * Reset de-encapsulation cache
671 last_encap_src
= INADDR_ANY
;
672 last_encap_vif
= NULL
;
674 reg_vif_num
= VIFI_INVALID
;
676 have_encap_tunnel
= 0;
683 log(LOG_DEBUG
, "ip_mrouter_done\n");
689 * Set PIM assert processing global
694 if ((i
!= 1) && (i
!= 0))
703 * Configure API capabilities
706 set_api_config(uint32_t *apival
)
711 * We can set the API capabilities only if it is the first operation
712 * after MRT_INIT. I.e.:
713 * - there are no vifs installed
714 * - pim_assert is not enabled
715 * - the MFC table is empty
725 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
726 if (mfctable
[i
] != NULL
) {
732 mrt_api_config
= *apival
& mrt_api_support
;
733 *apival
= mrt_api_config
;
739 * Add a vif to the vif table
742 add_vif(struct vifctl
*vifcp
)
744 struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
745 struct sockaddr_in sin
= {sizeof sin
, AF_INET
};
749 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
751 if (vifcp
->vifc_vifi
>= MAXVIFS
)
753 if (vifp
->v_lcl_addr
.s_addr
!= INADDR_ANY
)
755 if (vifcp
->vifc_lcl_addr
.s_addr
== INADDR_ANY
)
756 return EADDRNOTAVAIL
;
758 /* Find the interface with an address in AF_INET family */
760 if (vifcp
->vifc_flags
& VIFF_REGISTER
) {
762 * XXX: Because VIFF_REGISTER does not really need a valid
763 * local interface (e.g. it could be 127.0.0.2), we don't
770 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
771 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
773 return EADDRNOTAVAIL
;
777 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
778 if ((vifcp
->vifc_flags
& VIFF_SRCRT
) == 0) {
780 * An encapsulating tunnel is wanted. Tell ipip_input() to
781 * start paying attention to encapsulated packets.
783 if (have_encap_tunnel
== 0) {
784 have_encap_tunnel
= 1;
785 for (i
= 0; i
< MAXVIFS
; i
++) {
786 if_initname(&multicast_decap_if
[i
], "mdecap", i
);
790 * Set interface to fake encapsulator interface
792 ifp
= &multicast_decap_if
[vifcp
->vifc_vifi
];
794 * Prepare cached route entry
796 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
798 log(LOG_ERR
, "source routed tunnels not supported\n");
802 } else if (vifcp
->vifc_flags
& VIFF_REGISTER
) {
803 ifp
= &multicast_register_if
;
805 log(LOG_DEBUG
, "Adding a register vif, ifp: %p\n",
806 (void *)&multicast_register_if
);
807 if (reg_vif_num
== VIFI_INVALID
) {
808 if_initname(&multicast_register_if
, "register_vif", 0);
809 multicast_register_if
.if_flags
= IFF_LOOPBACK
;
810 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
811 reg_vif_num
= vifcp
->vifc_vifi
;
814 } else { /* Make sure the interface supports multicast */
815 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
818 /* Enable promiscuous reception of all IP multicasts from the if */
820 error
= if_allmulti(ifp
, 1);
827 /* define parameters for the tbf structure */
829 GET_TIME(vifp
->v_tbf
->tbf_last_pkt_t
);
830 vifp
->v_tbf
->tbf_n_tok
= 0;
831 vifp
->v_tbf
->tbf_q_len
= 0;
832 vifp
->v_tbf
->tbf_max_q_len
= MAXQSIZE
;
833 vifp
->v_tbf
->tbf_q
= vifp
->v_tbf
->tbf_t
= NULL
;
835 vifp
->v_flags
= vifcp
->vifc_flags
;
836 vifp
->v_threshold
= vifcp
->vifc_threshold
;
837 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
838 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
840 /* scaling up here allows division by 1024 in critical code */
841 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
843 vifp
->v_rsvpd
= NULL
;
844 /* initialize per vif pkt counters */
847 vifp
->v_bytes_in
= 0;
848 vifp
->v_bytes_out
= 0;
851 /* Adjust numvifs up if the vifi is higher than numvifs */
852 if (numvifs
<= vifcp
->vifc_vifi
) numvifs
= vifcp
->vifc_vifi
+ 1;
855 log(LOG_DEBUG
, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
857 (u_long
)ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
858 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
859 (u_long
)ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
860 vifcp
->vifc_threshold
,
861 vifcp
->vifc_rate_limit
);
867 * Delete a vif from the vif table
876 vifp
= &viftable
[vifi
];
877 if (vifp
->v_lcl_addr
.s_addr
== INADDR_ANY
)
878 return EADDRNOTAVAIL
;
882 if (!(vifp
->v_flags
& (VIFF_TUNNEL
| VIFF_REGISTER
)))
883 if_allmulti(vifp
->v_ifp
, 0);
885 if (vifp
== last_encap_vif
) {
886 last_encap_vif
= NULL
;
887 last_encap_src
= INADDR_ANY
;
891 * Free packets queued at the interface
893 while (vifp
->v_tbf
->tbf_q
) {
894 struct mbuf
*m
= vifp
->v_tbf
->tbf_q
;
896 vifp
->v_tbf
->tbf_q
= m
->m_nextpkt
;
901 if (vifp
->v_flags
& VIFF_REGISTER
)
902 reg_vif_num
= VIFI_INVALID
;
905 bzero((caddr_t
)vifp
->v_tbf
, sizeof(*(vifp
->v_tbf
)));
906 bzero((caddr_t
)vifp
, sizeof (*vifp
));
909 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", vifi
, numvifs
);
911 /* Adjust numvifs down */
912 for (vifi
= numvifs
; vifi
> 0; vifi
--)
913 if (viftable
[vifi
-1].v_lcl_addr
.s_addr
!= INADDR_ANY
)
923 * update an mfc entry without resetting counters and S,G addresses.
926 update_mfc_params(struct mfc
*rt
, struct mfcctl2
*mfccp
)
930 rt
->mfc_parent
= mfccp
->mfcc_parent
;
931 for (i
= 0; i
< numvifs
; i
++) {
932 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
933 rt
->mfc_flags
[i
] = mfccp
->mfcc_flags
[i
] & mrt_api_config
&
936 /* set the RP address */
937 if (mrt_api_config
& MRT_MFC_RP
)
938 rt
->mfc_rp
= mfccp
->mfcc_rp
;
940 rt
->mfc_rp
.s_addr
= INADDR_ANY
;
944 * fully initialize an mfc entry from the parameter.
947 init_mfc_params(struct mfc
*rt
, struct mfcctl2
*mfccp
)
949 rt
->mfc_origin
= mfccp
->mfcc_origin
;
950 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
952 update_mfc_params(rt
, mfccp
);
954 /* initialize pkt counters per src-grp */
956 rt
->mfc_byte_cnt
= 0;
957 rt
->mfc_wrong_if
= 0;
958 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
966 add_mfc(struct mfcctl2
*mfccp
)
973 rt
= mfc_find(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
975 /* If an entry already exists, just update the fields */
977 if (mrtdebug
& DEBUG_MFC
)
978 log(LOG_DEBUG
,"add_mfc update o %lx g %lx p %x\n",
979 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
980 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
984 update_mfc_params(rt
, mfccp
);
990 * Find the entry for which the upcall was made and update
993 hash
= MFCHASH(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
994 for (rt
= mfctable
[hash
], nstl
= 0; rt
; rt
= rt
->mfc_next
) {
996 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
997 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
) &&
998 (rt
->mfc_stall
!= NULL
)) {
1001 log(LOG_ERR
, "add_mfc %s o %lx g %lx p %x dbx %p\n",
1002 "multiple kernel entries",
1003 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
1004 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1005 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
1007 if (mrtdebug
& DEBUG_MFC
)
1008 log(LOG_DEBUG
,"add_mfc o %lx g %lx p %x dbg %p\n",
1009 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
1010 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1011 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
1013 init_mfc_params(rt
, mfccp
);
1015 rt
->mfc_expire
= 0; /* Don't clean this guy up */
1018 /* free packets Qed at the end of this entry */
1019 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
1020 struct rtdetq
*n
= rte
->next
;
1022 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
1024 kfree(rte
, M_MRTABLE
);
1027 rt
->mfc_stall
= NULL
;
1032 * It is possible that an entry is being inserted without an upcall
1035 if (mrtdebug
& DEBUG_MFC
)
1036 log(LOG_DEBUG
,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
1037 hash
, (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
1038 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
1039 mfccp
->mfcc_parent
);
1041 for (rt
= mfctable
[hash
]; rt
!= NULL
; rt
= rt
->mfc_next
) {
1042 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
1043 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
)) {
1044 init_mfc_params(rt
, mfccp
);
1051 if (rt
== NULL
) { /* no upcall, so make a new entry */
1052 rt
= kmalloc(sizeof(*rt
), M_MRTABLE
, M_INTWAIT
| M_NULLOK
);
1058 init_mfc_params(rt
, mfccp
);
1060 rt
->mfc_stall
= NULL
;
1062 rt
->mfc_bw_meter
= NULL
;
1063 /* insert new entry at head of hash chain */
1064 rt
->mfc_next
= mfctable
[hash
];
1065 mfctable
[hash
] = rt
;
1073 * Delete an mfc entry
1076 del_mfc(struct mfcctl2
*mfccp
)
1078 struct in_addr origin
;
1079 struct in_addr mcastgrp
;
1083 struct bw_meter
*list
;
1085 origin
= mfccp
->mfcc_origin
;
1086 mcastgrp
= mfccp
->mfcc_mcastgrp
;
1088 if (mrtdebug
& DEBUG_MFC
)
1089 log(LOG_DEBUG
,"del_mfc orig %lx mcastgrp %lx\n",
1090 (u_long
)ntohl(origin
.s_addr
), (u_long
)ntohl(mcastgrp
.s_addr
));
1094 hash
= MFCHASH(origin
.s_addr
, mcastgrp
.s_addr
);
1095 for (nptr
= &mfctable
[hash
]; (rt
= *nptr
) != NULL
; nptr
= &rt
->mfc_next
)
1096 if (origin
.s_addr
== rt
->mfc_origin
.s_addr
&&
1097 mcastgrp
.s_addr
== rt
->mfc_mcastgrp
.s_addr
&&
1098 rt
->mfc_stall
== NULL
)
1102 return EADDRNOTAVAIL
;
1105 *nptr
= rt
->mfc_next
;
1108 * free the bw_meter entries
1110 list
= rt
->mfc_bw_meter
;
1111 rt
->mfc_bw_meter
= NULL
;
1113 kfree(rt
, M_MRTABLE
);
1123 * Send a message to mrouted on the multicast routing socket
1126 socket_send(struct socket
*s
, struct mbuf
*mm
, struct sockaddr_in
*src
)
1129 if (ssb_appendaddr(&s
->so_rcv
, (struct sockaddr
*)src
, mm
, NULL
) != 0) {
1139 * IP multicast forwarding function. This function assumes that the packet
1140 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1141 * pointed to by "ifp", and the packet is to be relayed to other networks
1142 * that have members of the packet's destination IP multicast group.
1144 * The packet is returned unscathed to the caller, unless it is
1145 * erroneous, in which case a non-zero return value tells the caller to
1149 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1152 X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
,
1153 struct ip_moptions
*imo
)
1158 if (mrtdebug
& DEBUG_FORWARD
)
1159 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1160 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1163 if (ip
->ip_hl
< (sizeof(struct ip
) + TUNNEL_LEN
) >> 2 ||
1164 ((u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1166 * Packet arrived via a physical interface or
1167 * an encapsulated tunnel or a register_vif.
1171 * Packet arrived through a source-route tunnel.
1172 * Source-route tunnels are no longer supported.
1174 static int last_log
;
1175 if (last_log
!= time_second
) {
1176 last_log
= time_second
;
1178 "ip_mforward: received source-routed packet from %lx\n",
1179 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1184 if (imo
&& ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1185 if (ip
->ip_ttl
< 255)
1186 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1187 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1188 struct vif
*vifp
= viftable
+ vifi
;
1190 kprintf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n",
1191 (long)ntohl(ip
->ip_src
.s_addr
), (long)ntohl(ip
->ip_dst
.s_addr
),
1193 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1194 vifp
->v_ifp
->if_xname
);
1196 return ip_mdq(m
, ifp
, NULL
, vifi
);
1198 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1199 kprintf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1200 (long)ntohl(ip
->ip_src
.s_addr
), (long)ntohl(ip
->ip_dst
.s_addr
));
1202 kprintf("In fact, no options were specified at all\n");
1206 * Don't forward a packet with time-to-live of zero or one,
1207 * or a packet destined to a local-only group.
1209 if (ip
->ip_ttl
<= 1 || ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1213 * Determine forwarding vifs from the forwarding cache table
1216 ++mrtstat
.mrts_mfc_lookups
;
1217 rt
= mfc_find(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1219 /* Entry exists, so forward if necessary */
1222 return ip_mdq(m
, ifp
, rt
, -1);
1225 * If we don't have a route for packet's origin,
1226 * Make a copy of the packet & send message to routing daemon
1232 int hlen
= ip
->ip_hl
<< 2;
1234 ++mrtstat
.mrts_mfc_misses
;
1236 mrtstat
.mrts_no_route
++;
1237 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1238 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1239 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1240 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1243 * Allocate mbufs early so that we don't do extra work if we are
1244 * just going to fail anyway. Make sure to pullup the header so
1245 * that other people can't step on it.
1247 rte
= kmalloc((sizeof *rte
), M_MRTABLE
, M_INTWAIT
| M_NULLOK
);
1253 mb0
= m_copypacket(m
, MB_DONTWAIT
);
1254 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1255 mb0
= m_pullup(mb0
, hlen
);
1257 kfree(rte
, M_MRTABLE
);
1262 /* is there an upcall waiting for this flow ? */
1263 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1264 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1265 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1266 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1267 (rt
->mfc_stall
!= NULL
))
1274 struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1278 * Locate the vifi for the incoming interface for this packet.
1279 * If none found, drop packet.
1281 for (vifi
=0; vifi
< numvifs
&& viftable
[vifi
].v_ifp
!= ifp
; vifi
++)
1283 if (vifi
>= numvifs
) /* vif not found, drop packet */
1286 /* no upcall, so make a new entry */
1287 rt
= kmalloc(sizeof(*rt
), M_MRTABLE
, M_INTWAIT
| M_NULLOK
);
1291 /* Make a copy of the header to send to the user level process */
1292 mm
= m_copy(mb0
, 0, hlen
);
1297 * Send message to routing daemon to install
1298 * a route into the kernel table
1301 im
= mtod(mm
, struct igmpmsg
*);
1302 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1306 mrtstat
.mrts_upcalls
++;
1308 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1309 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1310 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1311 ++mrtstat
.mrts_upq_sockfull
;
1313 kfree(rt
, M_MRTABLE
);
1315 kfree(rte
, M_MRTABLE
);
1321 /* insert new entry at head of hash chain */
1322 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1323 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1324 rt
->mfc_expire
= UPCALL_EXPIRE
;
1326 for (i
= 0; i
< numvifs
; i
++) {
1327 rt
->mfc_ttls
[i
] = 0;
1328 rt
->mfc_flags
[i
] = 0;
1330 rt
->mfc_parent
= -1;
1332 rt
->mfc_rp
.s_addr
= INADDR_ANY
; /* clear the RP address */
1334 rt
->mfc_bw_meter
= NULL
;
1336 /* link into table */
1337 rt
->mfc_next
= mfctable
[hash
];
1338 mfctable
[hash
] = rt
;
1339 rt
->mfc_stall
= rte
;
1342 /* determine if q has overflowed */
1347 * XXX ouch! we need to append to the list, but we
1348 * only have a pointer to the front, so we have to
1349 * scan the entire list every time.
1351 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1354 if (npkts
> MAX_UPQ
) {
1355 mrtstat
.mrts_upq_ovflw
++;
1357 kfree(rte
, M_MRTABLE
);
1363 /* Add this entry to the end of the queue */
1377 * Clean up the cache entry if upcall is not serviced
1380 expire_upcalls(void *unused
)
1383 struct mfc
*mfc
, **nptr
;
1387 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
1388 if (nexpire
[i
] == 0)
1390 nptr
= &mfctable
[i
];
1391 for (mfc
= *nptr
; mfc
!= NULL
; mfc
= *nptr
) {
1393 * Skip real cache entries
1394 * Make sure it wasn't marked to not expire (shouldn't happen)
1397 if (mfc
->mfc_stall
!= NULL
&& mfc
->mfc_expire
!= 0 &&
1398 --mfc
->mfc_expire
== 0) {
1399 if (mrtdebug
& DEBUG_EXPIRE
)
1400 log(LOG_DEBUG
, "expire_upcalls: expiring (%lx %lx)\n",
1401 (u_long
)ntohl(mfc
->mfc_origin
.s_addr
),
1402 (u_long
)ntohl(mfc
->mfc_mcastgrp
.s_addr
));
1404 * drop all the packets
1405 * free the mbuf with the pkt, if, timing info
1407 for (rte
= mfc
->mfc_stall
; rte
; ) {
1408 struct rtdetq
*n
= rte
->next
;
1411 kfree(rte
, M_MRTABLE
);
1414 ++mrtstat
.mrts_cache_cleanups
;
1418 * free the bw_meter entries
1420 while (mfc
->mfc_bw_meter
!= NULL
) {
1421 struct bw_meter
*x
= mfc
->mfc_bw_meter
;
1423 mfc
->mfc_bw_meter
= x
->bm_mfc_next
;
1424 kfree(x
, M_BWMETER
);
1427 *nptr
= mfc
->mfc_next
;
1428 kfree(mfc
, M_MRTABLE
);
1430 nptr
= &mfc
->mfc_next
;
1434 callout_reset(&expire_upcalls_ch
, EXPIRE_TIMEOUT
, expire_upcalls
, NULL
);
1439 * Packet forwarding routine once entry in the cache is made
1442 ip_mdq(struct mbuf
*m
, struct ifnet
*ifp
, struct mfc
*rt
, vifi_t xmt_vif
)
1444 struct ip
*ip
= mtod(m
, struct ip
*);
1446 int plen
= ip
->ip_len
;
1449 * Macro to send packet on vif. Since RSVP packets don't get counted on
1450 * input, they shouldn't get counted on output, so statistics keeping is
1453 #define MC_SEND(ip,vifp,m) { \
1454 if ((vifp)->v_flags & VIFF_TUNNEL) \
1455 encap_send((ip), (vifp), (m)); \
1457 phyint_send((ip), (vifp), (m)); \
1461 * If xmt_vif is not -1, send on only the requested vif.
1463 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1465 if (xmt_vif
< numvifs
) {
1467 if (viftable
[xmt_vif
].v_flags
& VIFF_REGISTER
)
1468 pim_register_send(ip
, viftable
+ xmt_vif
, m
, rt
);
1471 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1476 * Don't forward if it didn't arrive from the parent vif for its origin.
1478 vifi
= rt
->mfc_parent
;
1479 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1480 /* came in the wrong interface */
1481 if (mrtdebug
& DEBUG_FORWARD
)
1482 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1483 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1484 ++mrtstat
.mrts_wrong_if
;
1487 * If we are doing PIM assert processing, send a message
1488 * to the routing daemon.
1490 * XXX: A PIM-SM router needs the WRONGVIF detection so it
1491 * can complete the SPT switch, regardless of the type
1492 * of the iif (broadcast media, GRE tunnel, etc).
1494 if (pim_assert
&& (vifi
< numvifs
) && viftable
[vifi
].v_ifp
) {
1499 if (ifp
== &multicast_register_if
)
1500 pimstat
.pims_rcv_registers_wrongiif
++;
1503 /* Get vifi for the incoming packet */
1504 for (vifi
=0; vifi
< numvifs
&& viftable
[vifi
].v_ifp
!= ifp
; vifi
++)
1506 if (vifi
>= numvifs
)
1507 return 0; /* The iif is not found: ignore the packet. */
1509 if (rt
->mfc_flags
[vifi
] & MRT_MFC_FLAGS_DISABLE_WRONGVIF
)
1510 return 0; /* WRONGVIF disabled: ignore the packet */
1514 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1516 if (delta
> ASSERT_MSG_TIME
) {
1517 struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1519 int hlen
= ip
->ip_hl
<< 2;
1520 struct mbuf
*mm
= m_copy(m
, 0, hlen
);
1522 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1523 mm
= m_pullup(mm
, hlen
);
1527 rt
->mfc_last_assert
= now
;
1529 im
= mtod(mm
, struct igmpmsg
*);
1530 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1534 mrtstat
.mrts_upcalls
++;
1536 k_igmpsrc
.sin_addr
= im
->im_src
;
1537 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1539 "ip_mforward: ip_mrouter socket queue full\n");
1540 ++mrtstat
.mrts_upq_sockfull
;
1548 /* If I sourced this packet, it counts as output, else it was input. */
1549 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1550 viftable
[vifi
].v_pkt_out
++;
1551 viftable
[vifi
].v_bytes_out
+= plen
;
1553 viftable
[vifi
].v_pkt_in
++;
1554 viftable
[vifi
].v_bytes_in
+= plen
;
1557 rt
->mfc_byte_cnt
+= plen
;
1560 * For each vif, decide if a copy of the packet should be forwarded.
1562 * - the ttl exceeds the vif's threshold
1563 * - there are group members downstream on interface
1565 for (vifi
= 0; vifi
< numvifs
; vifi
++)
1566 if ((rt
->mfc_ttls
[vifi
] > 0) && (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1567 viftable
[vifi
].v_pkt_out
++;
1568 viftable
[vifi
].v_bytes_out
+= plen
;
1570 if (viftable
[vifi
].v_flags
& VIFF_REGISTER
)
1571 pim_register_send(ip
, viftable
+ vifi
, m
, rt
);
1574 MC_SEND(ip
, viftable
+vifi
, m
);
1578 * Perform upcall-related bw measuring.
1580 if (rt
->mfc_bw_meter
!= NULL
) {
1585 for (x
= rt
->mfc_bw_meter
; x
!= NULL
; x
= x
->bm_mfc_next
)
1586 bw_meter_receive_packet(x
, plen
, &now
);
1593 * check if a vif number is legal/ok. This is used by ip_output.
1596 X_legal_vif_num(int vif
)
1598 return (vif
>= 0 && vif
< numvifs
);
1602 * Return the local address used by this vif
1605 X_ip_mcast_src(int vifi
)
1607 if (vifi
>= 0 && vifi
< numvifs
)
1608 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1614 phyint_send(struct ip
*ip
, struct vif
*vifp
, struct mbuf
*m
)
1616 struct mbuf
*mb_copy
;
1617 int hlen
= ip
->ip_hl
<< 2;
1620 * Make a new reference to the packet; make sure that
1621 * the IP header is actually copied, not just referenced,
1622 * so that ip_output() only scribbles on the copy.
1624 mb_copy
= m_copypacket(m
, MB_DONTWAIT
);
1625 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1626 mb_copy
= m_pullup(mb_copy
, hlen
);
1627 if (mb_copy
== NULL
)
1630 if (vifp
->v_rate_limit
== 0)
1631 tbf_send_packet(vifp
, mb_copy
);
1633 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1637 encap_send(struct ip
*ip
, struct vif
*vifp
, struct mbuf
*m
)
1639 struct mbuf
*mb_copy
;
1641 int i
, len
= ip
->ip_len
;
1643 /* Take care of delayed checksums */
1644 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
1645 in_delayed_cksum(m
);
1646 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
1650 * copy the old packet & pullup its IP header into the
1651 * new mbuf so we can modify it. Try to fill the new
1652 * mbuf since if we don't the ethernet driver will.
1654 MGETHDR(mb_copy
, MB_DONTWAIT
, MT_HEADER
);
1655 if (mb_copy
== NULL
)
1657 mb_copy
->m_data
+= max_linkhdr
;
1658 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1660 if ((mb_copy
->m_next
= m_copypacket(m
, MB_DONTWAIT
)) == NULL
) {
1664 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1667 mb_copy
= m_pullup(mb_copy
, i
);
1668 if (mb_copy
== NULL
)
1670 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1673 * fill in the encapsulating IP header.
1675 ip_copy
= mtod(mb_copy
, struct ip
*);
1676 *ip_copy
= multicast_encap_iphdr
;
1677 ip_copy
->ip_id
= ip_newid();
1678 ip_copy
->ip_len
+= len
;
1679 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1680 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1683 * turn the encapsulated IP header back into a valid one.
1685 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1687 ip
->ip_len
= htons(ip
->ip_len
);
1688 ip
->ip_off
= htons(ip
->ip_off
);
1690 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1691 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1692 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1694 if (vifp
->v_rate_limit
== 0)
1695 tbf_send_packet(vifp
, mb_copy
);
1697 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1701 * De-encapsulate a packet and feed it back through ip input (this
1702 * routine is called whenever IP gets a packet with proto type
1703 * ENCAP_PROTO and a local destination address).
1705 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current.
1708 X_ipip_input(struct mbuf
*m
, int off
, int proto
)
1710 struct ip
*ip
= mtod(m
, struct ip
*);
1711 int hlen
= ip
->ip_hl
<< 2;
1713 if (!have_encap_tunnel
) {
1714 rip_input(m
, off
, proto
);
1718 * dump the packet if it's not to a multicast destination or if
1719 * we don't have an encapsulating tunnel with the source.
1720 * Note: This code assumes that the remote site IP address
1721 * uniquely identifies the tunnel (i.e., that this site has
1722 * at most one tunnel with the remote site).
1724 if (!IN_MULTICAST(ntohl(((struct ip
*)((char *)ip
+hlen
))->ip_dst
.s_addr
))) {
1725 ++mrtstat
.mrts_bad_tunnel
;
1729 if (ip
->ip_src
.s_addr
!= last_encap_src
) {
1730 struct vif
*vifp
= viftable
;
1731 struct vif
*vife
= vifp
+ numvifs
;
1733 last_encap_src
= ip
->ip_src
.s_addr
;
1734 last_encap_vif
= NULL
;
1735 for ( ; vifp
< vife
; ++vifp
)
1736 if (vifp
->v_rmt_addr
.s_addr
== ip
->ip_src
.s_addr
) {
1737 if ((vifp
->v_flags
& (VIFF_TUNNEL
|VIFF_SRCRT
))
1739 last_encap_vif
= vifp
;
1743 if (last_encap_vif
== NULL
) {
1744 last_encap_src
= INADDR_ANY
;
1745 mrtstat
.mrts_cant_tunnel
++; /*XXX*/
1748 log(LOG_DEBUG
, "ip_mforward: no tunnel with %lx\n",
1749 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1753 if (hlen
> sizeof(struct ip
))
1755 m
->m_data
+= sizeof(struct ip
);
1756 m
->m_len
-= sizeof(struct ip
);
1757 m
->m_pkthdr
.len
-= sizeof(struct ip
);
1758 m
->m_pkthdr
.rcvif
= last_encap_vif
->v_ifp
;
1760 netisr_queue(NETISR_IP
, m
);
1764 * Token bucket filter module
1768 tbf_control(struct vif
*vifp
, struct mbuf
*m
, struct ip
*ip
, u_long p_len
)
1770 struct tbf
*t
= vifp
->v_tbf
;
1772 if (p_len
> MAX_BKT_SIZE
) { /* drop if packet is too large */
1773 mrtstat
.mrts_pkt2large
++;
1778 tbf_update_tokens(vifp
);
1780 if (t
->tbf_q_len
== 0) { /* queue empty... */
1781 if (p_len
<= t
->tbf_n_tok
) { /* send packet if enough tokens */
1782 t
->tbf_n_tok
-= p_len
;
1783 tbf_send_packet(vifp
, m
);
1784 } else { /* no, queue packet and try later */
1786 callout_reset(&tbf_reprocess_q_ch
, TBF_REPROCESS
,
1787 tbf_reprocess_q
, vifp
);
1789 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1790 /* finite queue length, so queue pkts and process queue */
1792 tbf_process_q(vifp
);
1794 /* queue full, try to dq and queue and process */
1795 if (!tbf_dq_sel(vifp
, ip
)) {
1796 mrtstat
.mrts_q_overflow
++;
1800 tbf_process_q(vifp
);
1806 * adds a packet to the queue at the interface
1809 tbf_queue(struct vif
*vifp
, struct mbuf
*m
)
1811 struct tbf
*t
= vifp
->v_tbf
;
1815 if (t
->tbf_t
== NULL
) /* Queue was empty */
1817 else /* Insert at tail */
1818 t
->tbf_t
->m_nextpkt
= m
;
1820 t
->tbf_t
= m
; /* Set new tail pointer */
1823 /* Make sure we didn't get fed a bogus mbuf */
1825 panic("tbf_queue: m_nextpkt");
1827 m
->m_nextpkt
= NULL
;
1835 * processes the queue at the interface
1838 tbf_process_q(struct vif
*vifp
)
1840 struct tbf
*t
= vifp
->v_tbf
;
1844 /* loop through the queue at the interface and send as many packets
1847 while (t
->tbf_q_len
> 0) {
1848 struct mbuf
*m
= t
->tbf_q
;
1849 int len
= mtod(m
, struct ip
*)->ip_len
;
1851 /* determine if the packet can be sent */
1852 if (len
> t
->tbf_n_tok
) /* not enough tokens, we are done */
1854 /* ok, reduce no of tokens, dequeue and send the packet. */
1855 t
->tbf_n_tok
-= len
;
1857 t
->tbf_q
= m
->m_nextpkt
;
1858 if (--t
->tbf_q_len
== 0)
1861 m
->m_nextpkt
= NULL
;
1862 tbf_send_packet(vifp
, m
);
1868 tbf_reprocess_q(void *xvifp
)
1870 struct vif
*vifp
= xvifp
;
1872 if (ip_mrouter
== NULL
)
1874 tbf_update_tokens(vifp
);
1875 tbf_process_q(vifp
);
1876 if (vifp
->v_tbf
->tbf_q_len
)
1877 callout_reset(&tbf_reprocess_q_ch
, TBF_REPROCESS
,
1878 tbf_reprocess_q
, vifp
);
1881 /* function that will selectively discard a member of the queue
1882 * based on the precedence value and the priority
1885 tbf_dq_sel(struct vif
*vifp
, struct ip
*ip
)
1888 struct mbuf
*m
, *last
;
1890 struct tbf
*t
= vifp
->v_tbf
;
1894 p
= priority(vifp
, ip
);
1898 while ((m
= *np
) != NULL
) {
1899 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1901 /* If we're removing the last packet, fix the tail pointer */
1905 /* It's impossible for the queue to be empty, but check anyways. */
1906 if (--t
->tbf_q_len
== 0)
1909 mrtstat
.mrts_drop_sel
++;
1920 tbf_send_packet(struct vif
*vifp
, struct mbuf
*m
)
1924 if (vifp
->v_flags
& VIFF_TUNNEL
) /* If tunnel options */
1925 ip_output(m
, NULL
, &vifp
->v_route
, IP_FORWARDING
, NULL
, NULL
);
1927 struct ip_moptions imo
;
1929 static struct route ro
; /* XXX check this */
1931 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1932 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1933 imo
.imo_multicast_loop
= 1;
1934 imo
.imo_multicast_vif
= -1;
1937 * Re-entrancy should not be a problem here, because
1938 * the packets that we send out and are looped back at us
1939 * should get rejected because they appear to come from
1940 * the loopback interface, thus preventing looping.
1942 error
= ip_output(m
, NULL
, &ro
, IP_FORWARDING
, &imo
, NULL
);
1944 if (mrtdebug
& DEBUG_XMIT
)
1945 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1946 (int)(vifp
- viftable
), error
);
1951 /* determine the current time and then
1952 * the elapsed time (between the last time and time now)
1953 * in milliseconds & update the no. of tokens in the bucket
1956 tbf_update_tokens(struct vif
*vifp
)
1960 struct tbf
*t
= vifp
->v_tbf
;
1966 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1969 * This formula is actually
1970 * "time in seconds" * "bytes/second".
1972 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1974 * The (1000/1024) was introduced in add_vif to optimize
1975 * this divide into a shift.
1977 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1978 t
->tbf_last_pkt_t
= tp
;
1980 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1981 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1987 priority(struct vif
*vifp
, struct ip
*ip
)
1989 int prio
= 50; /* the lowest priority -- default case */
1991 /* temporary hack; may add general packet classifier some day */
1994 * The UDP port space is divided up into four priority ranges:
1995 * [0, 16384) : unclassified - lowest priority
1996 * [16384, 32768) : audio - highest priority
1997 * [32768, 49152) : whiteboard - medium priority
1998 * [49152, 65536) : video - low priority
2000 * Everything else gets lowest priority.
2002 if (ip
->ip_p
== IPPROTO_UDP
) {
2003 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
2004 switch (ntohs(udp
->uh_dport
) & 0xc000) {
2020 * End of token bucket filter modifications
2024 X_ip_rsvp_vif(struct socket
*so
, struct sockopt
*sopt
)
2028 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2031 error
= soopt_to_kbuf(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
2037 if (vifi
< 0 || vifi
>= numvifs
) { /* Error if vif is invalid */
2039 return EADDRNOTAVAIL
;
2042 if (sopt
->sopt_name
== IP_RSVP_VIF_ON
) {
2043 /* Check if socket is available. */
2044 if (viftable
[vifi
].v_rsvpd
!= NULL
) {
2049 viftable
[vifi
].v_rsvpd
= so
;
2050 /* This may seem silly, but we need to be sure we don't over-increment
2051 * the RSVP counter, in case something slips up.
2053 if (!viftable
[vifi
].v_rsvp_on
) {
2054 viftable
[vifi
].v_rsvp_on
= 1;
2057 } else { /* must be VIF_OFF */
2059 * XXX as an additional consistency check, one could make sure
2060 * that viftable[vifi].v_rsvpd == so, otherwise passing so as
2061 * first parameter is pretty useless.
2063 viftable
[vifi
].v_rsvpd
= NULL
;
2065 * This may seem silly, but we need to be sure we don't over-decrement
2066 * the RSVP counter, in case something slips up.
2068 if (viftable
[vifi
].v_rsvp_on
) {
2069 viftable
[vifi
].v_rsvp_on
= 0;
2078 X_ip_rsvp_force_done(struct socket
*so
)
2082 /* Don't bother if it is not the right type of socket. */
2083 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2088 /* The socket may be attached to more than one vif...this
2089 * is perfectly legal.
2091 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2092 if (viftable
[vifi
].v_rsvpd
== so
) {
2093 viftable
[vifi
].v_rsvpd
= NULL
;
2094 /* This may seem silly, but we need to be sure we don't
2095 * over-decrement the RSVP counter, in case something slips up.
2097 if (viftable
[vifi
].v_rsvp_on
) {
2098 viftable
[vifi
].v_rsvp_on
= 0;
2108 X_rsvp_input(struct mbuf
*m
, ...)
2111 struct ip
*ip
= mtod(m
, struct ip
*);
2112 struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
};
2116 /* support IP_RECVIF used by rsvpd rel4.2a1 */
2124 off
= __va_arg(ap
, int);
2125 proto
= __va_arg(ap
, int);
2129 kprintf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2131 /* Can still get packets with rsvp_on = 0 if there is a local member
2132 * of the group to which the RSVP packet is addressed. But in this
2133 * case we want to throw the packet away.
2143 kprintf("rsvp_input: check vifs\n");
2146 if (!(m
->m_flags
& M_PKTHDR
))
2147 panic("rsvp_input no hdr");
2150 ifp
= m
->m_pkthdr
.rcvif
;
2151 /* Find which vif the packet arrived on. */
2152 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2153 if (viftable
[vifi
].v_ifp
== ifp
)
2157 if (vifi
== numvifs
|| (so
= viftable
[vifi
].v_rsvpd
) == NULL
) {
2159 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2162 * If the old-style non-vif-associated socket is set,
2163 * then use it. Otherwise, drop packet since there
2164 * is no specific socket for this vif.
2166 if (ip_rsvpd
!= NULL
) {
2168 kprintf("rsvp_input: Sending packet up old-style socket\n");
2169 rip_input(m
, off
, proto
); /* xxx */
2171 if (rsvpdebug
&& vifi
== numvifs
)
2172 kprintf("rsvp_input: Can't find vif for packet.\n");
2173 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2174 kprintf("rsvp_input: No socket defined for vif %d\n",vifi
);
2180 rsvp_src
.sin_addr
= ip
->ip_src
;
2183 kprintf("rsvp_input: m->m_len = %d, ssb_space() = %ld\n",
2184 m
->m_len
,ssb_space(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2188 inp
= (struct inpcb
*)so
->so_pcb
;
2189 if (inp
->inp_flags
& INP_CONTROLOPTS
||
2190 inp
->inp_socket
->so_options
& SO_TIMESTAMP
)
2191 ip_savecontrol(inp
, &opts
, ip
, m
);
2192 if (ssb_appendaddr(&so
->so_rcv
,
2193 (struct sockaddr
*)&rsvp_src
,m
, opts
) == 0) {
2198 kprintf("rsvp_input: Failed to append to socket\n");
2203 kprintf("rsvp_input: send packet up\n");
2206 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2208 kprintf("rsvp_input: Failed to append to socket\n");
2211 kprintf("rsvp_input: send packet up\n");
2219 * Code for bandwidth monitors
2223 * Define common interface for timeval-related methods
2225 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp)
2226 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp))
2227 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp))
2230 compute_bw_meter_flags(struct bw_upcall
*req
)
2234 if (req
->bu_flags
& BW_UPCALL_UNIT_PACKETS
)
2235 flags
|= BW_METER_UNIT_PACKETS
;
2236 if (req
->bu_flags
& BW_UPCALL_UNIT_BYTES
)
2237 flags
|= BW_METER_UNIT_BYTES
;
2238 if (req
->bu_flags
& BW_UPCALL_GEQ
)
2239 flags
|= BW_METER_GEQ
;
2240 if (req
->bu_flags
& BW_UPCALL_LEQ
)
2241 flags
|= BW_METER_LEQ
;
2247 * Add a bw_meter entry
2250 add_bw_upcall(struct bw_upcall
*req
)
2253 struct timeval delta
= { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC
,
2254 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC
};
2259 if (!(mrt_api_config
& MRT_MFC_BW_UPCALL
))
2262 /* Test if the flags are valid */
2263 if (!(req
->bu_flags
& (BW_UPCALL_UNIT_PACKETS
| BW_UPCALL_UNIT_BYTES
)))
2265 if (!(req
->bu_flags
& (BW_UPCALL_GEQ
| BW_UPCALL_LEQ
)))
2267 if ((req
->bu_flags
& (BW_UPCALL_GEQ
| BW_UPCALL_LEQ
))
2268 == (BW_UPCALL_GEQ
| BW_UPCALL_LEQ
))
2271 /* Test if the threshold time interval is valid */
2272 if (BW_TIMEVALCMP(&req
->bu_threshold
.b_time
, &delta
, <))
2275 flags
= compute_bw_meter_flags(req
);
2278 * Find if we have already same bw_meter entry
2281 mfc
= mfc_find(req
->bu_src
.s_addr
, req
->bu_dst
.s_addr
);
2284 return EADDRNOTAVAIL
;
2286 for (x
= mfc
->mfc_bw_meter
; x
!= NULL
; x
= x
->bm_mfc_next
) {
2287 if ((BW_TIMEVALCMP(&x
->bm_threshold
.b_time
,
2288 &req
->bu_threshold
.b_time
, ==)) &&
2289 (x
->bm_threshold
.b_packets
== req
->bu_threshold
.b_packets
) &&
2290 (x
->bm_threshold
.b_bytes
== req
->bu_threshold
.b_bytes
) &&
2291 (x
->bm_flags
& BW_METER_USER_FLAGS
) == flags
) {
2293 return 0; /* XXX Already installed */
2298 /* Allocate the new bw_meter entry */
2299 x
= kmalloc(sizeof(*x
), M_BWMETER
, M_INTWAIT
);
2301 /* Set the new bw_meter entry */
2302 x
->bm_threshold
.b_time
= req
->bu_threshold
.b_time
;
2304 x
->bm_start_time
= now
;
2305 x
->bm_threshold
.b_packets
= req
->bu_threshold
.b_packets
;
2306 x
->bm_threshold
.b_bytes
= req
->bu_threshold
.b_bytes
;
2307 x
->bm_measured
.b_packets
= 0;
2308 x
->bm_measured
.b_bytes
= 0;
2309 x
->bm_flags
= flags
;
2310 x
->bm_time_next
= NULL
;
2311 x
->bm_time_hash
= BW_METER_BUCKETS
;
2313 /* Add the new bw_meter entry to the front of entries for this MFC */
2316 x
->bm_mfc_next
= mfc
->mfc_bw_meter
;
2317 mfc
->mfc_bw_meter
= x
;
2318 schedule_bw_meter(x
, &now
);
2325 free_bw_list(struct bw_meter
*list
)
2327 while (list
!= NULL
) {
2328 struct bw_meter
*x
= list
;
2330 list
= list
->bm_mfc_next
;
2331 unschedule_bw_meter(x
);
2332 kfree(x
, M_BWMETER
);
2337 * Delete one or multiple bw_meter entries
2340 del_bw_upcall(struct bw_upcall
*req
)
2345 if (!(mrt_api_config
& MRT_MFC_BW_UPCALL
))
2349 /* Find the corresponding MFC entry */
2350 mfc
= mfc_find(req
->bu_src
.s_addr
, req
->bu_dst
.s_addr
);
2353 return EADDRNOTAVAIL
;
2354 } else if (req
->bu_flags
& BW_UPCALL_DELETE_ALL
) {
2356 * Delete all bw_meter entries for this mfc
2358 struct bw_meter
*list
;
2360 list
= mfc
->mfc_bw_meter
;
2361 mfc
->mfc_bw_meter
= NULL
;
2365 } else { /* Delete a single bw_meter entry */
2366 struct bw_meter
*prev
;
2369 flags
= compute_bw_meter_flags(req
);
2371 /* Find the bw_meter entry to delete */
2372 for (prev
= NULL
, x
= mfc
->mfc_bw_meter
; x
!= NULL
;
2373 prev
= x
, x
= x
->bm_mfc_next
) {
2374 if ((BW_TIMEVALCMP(&x
->bm_threshold
.b_time
,
2375 &req
->bu_threshold
.b_time
, ==)) &&
2376 (x
->bm_threshold
.b_packets
== req
->bu_threshold
.b_packets
) &&
2377 (x
->bm_threshold
.b_bytes
== req
->bu_threshold
.b_bytes
) &&
2378 (x
->bm_flags
& BW_METER_USER_FLAGS
) == flags
)
2381 if (x
!= NULL
) { /* Delete entry from the list for this MFC */
2383 prev
->bm_mfc_next
= x
->bm_mfc_next
; /* remove from middle*/
2385 x
->bm_mfc
->mfc_bw_meter
= x
->bm_mfc_next
;/* new head of list */
2388 unschedule_bw_meter(x
);
2389 /* Free the bw_meter entry */
2390 kfree(x
, M_BWMETER
);
2401 * Perform bandwidth measurement processing that may result in an upcall
2404 bw_meter_receive_packet(struct bw_meter
*x
, int plen
, struct timeval
*nowp
)
2406 struct timeval delta
;
2410 BW_TIMEVALDECR(&delta
, &x
->bm_start_time
);
2412 if (x
->bm_flags
& BW_METER_GEQ
) {
2414 * Processing for ">=" type of bw_meter entry
2416 if (BW_TIMEVALCMP(&delta
, &x
->bm_threshold
.b_time
, >)) {
2417 /* Reset the bw_meter entry */
2418 x
->bm_start_time
= *nowp
;
2419 x
->bm_measured
.b_packets
= 0;
2420 x
->bm_measured
.b_bytes
= 0;
2421 x
->bm_flags
&= ~BW_METER_UPCALL_DELIVERED
;
2424 /* Record that a packet is received */
2425 x
->bm_measured
.b_packets
++;
2426 x
->bm_measured
.b_bytes
+= plen
;
2429 * Test if we should deliver an upcall
2431 if (!(x
->bm_flags
& BW_METER_UPCALL_DELIVERED
)) {
2432 if (((x
->bm_flags
& BW_METER_UNIT_PACKETS
) &&
2433 (x
->bm_measured
.b_packets
>= x
->bm_threshold
.b_packets
)) ||
2434 ((x
->bm_flags
& BW_METER_UNIT_BYTES
) &&
2435 (x
->bm_measured
.b_bytes
>= x
->bm_threshold
.b_bytes
))) {
2436 /* Prepare an upcall for delivery */
2437 bw_meter_prepare_upcall(x
, nowp
);
2438 x
->bm_flags
|= BW_METER_UPCALL_DELIVERED
;
2441 } else if (x
->bm_flags
& BW_METER_LEQ
) {
2443 * Processing for "<=" type of bw_meter entry
2445 if (BW_TIMEVALCMP(&delta
, &x
->bm_threshold
.b_time
, >)) {
2447 * We are behind time with the multicast forwarding table
2448 * scanning for "<=" type of bw_meter entries, so test now
2449 * if we should deliver an upcall.
2451 if (((x
->bm_flags
& BW_METER_UNIT_PACKETS
) &&
2452 (x
->bm_measured
.b_packets
<= x
->bm_threshold
.b_packets
)) ||
2453 ((x
->bm_flags
& BW_METER_UNIT_BYTES
) &&
2454 (x
->bm_measured
.b_bytes
<= x
->bm_threshold
.b_bytes
))) {
2455 /* Prepare an upcall for delivery */
2456 bw_meter_prepare_upcall(x
, nowp
);
2458 /* Reschedule the bw_meter entry */
2459 unschedule_bw_meter(x
);
2460 schedule_bw_meter(x
, nowp
);
2463 /* Record that a packet is received */
2464 x
->bm_measured
.b_packets
++;
2465 x
->bm_measured
.b_bytes
+= plen
;
2468 * Test if we should restart the measuring interval
2470 if ((x
->bm_flags
& BW_METER_UNIT_PACKETS
&&
2471 x
->bm_measured
.b_packets
<= x
->bm_threshold
.b_packets
) ||
2472 (x
->bm_flags
& BW_METER_UNIT_BYTES
&&
2473 x
->bm_measured
.b_bytes
<= x
->bm_threshold
.b_bytes
)) {
2474 /* Don't restart the measuring interval */
2476 /* Do restart the measuring interval */
2478 * XXX: note that we don't unschedule and schedule, because this
2479 * might be too much overhead per packet. Instead, when we process
2480 * all entries for a given timer hash bin, we check whether it is
2481 * really a timeout. If not, we reschedule at that time.
2483 x
->bm_start_time
= *nowp
;
2484 x
->bm_measured
.b_packets
= 0;
2485 x
->bm_measured
.b_bytes
= 0;
2486 x
->bm_flags
&= ~BW_METER_UPCALL_DELIVERED
;
2493 * Prepare a bandwidth-related upcall
2496 bw_meter_prepare_upcall(struct bw_meter
*x
, struct timeval
*nowp
)
2498 struct timeval delta
;
2499 struct bw_upcall
*u
;
2504 * Compute the measured time interval
2507 BW_TIMEVALDECR(&delta
, &x
->bm_start_time
);
2510 * If there are too many pending upcalls, deliver them now
2512 if (bw_upcalls_n
>= BW_UPCALLS_MAX
)
2516 * Set the bw_upcall entry
2518 u
= &bw_upcalls
[bw_upcalls_n
++];
2519 u
->bu_src
= x
->bm_mfc
->mfc_origin
;
2520 u
->bu_dst
= x
->bm_mfc
->mfc_mcastgrp
;
2521 u
->bu_threshold
.b_time
= x
->bm_threshold
.b_time
;
2522 u
->bu_threshold
.b_packets
= x
->bm_threshold
.b_packets
;
2523 u
->bu_threshold
.b_bytes
= x
->bm_threshold
.b_bytes
;
2524 u
->bu_measured
.b_time
= delta
;
2525 u
->bu_measured
.b_packets
= x
->bm_measured
.b_packets
;
2526 u
->bu_measured
.b_bytes
= x
->bm_measured
.b_bytes
;
2528 if (x
->bm_flags
& BW_METER_UNIT_PACKETS
)
2529 u
->bu_flags
|= BW_UPCALL_UNIT_PACKETS
;
2530 if (x
->bm_flags
& BW_METER_UNIT_BYTES
)
2531 u
->bu_flags
|= BW_UPCALL_UNIT_BYTES
;
2532 if (x
->bm_flags
& BW_METER_GEQ
)
2533 u
->bu_flags
|= BW_UPCALL_GEQ
;
2534 if (x
->bm_flags
& BW_METER_LEQ
)
2535 u
->bu_flags
|= BW_UPCALL_LEQ
;
2541 * Send the pending bandwidth-related upcalls
2544 bw_upcalls_send(void)
2547 int len
= bw_upcalls_n
* sizeof(bw_upcalls
[0]);
2548 struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
2549 static struct igmpmsg igmpmsg
= { 0, /* unused1 */
2551 IGMPMSG_BW_UPCALL
,/* im_msgtype */
2556 { 0 } }; /* im_dst */
2558 if (bw_upcalls_n
== 0)
2559 return; /* No pending upcalls */
2564 * Allocate a new mbuf, initialize it with the header and
2565 * the payload for the pending calls.
2567 MGETHDR(m
, MB_DONTWAIT
, MT_HEADER
);
2569 log(LOG_WARNING
, "bw_upcalls_send: cannot allocate mbuf\n");
2573 m
->m_len
= m
->m_pkthdr
.len
= 0;
2574 m_copyback(m
, 0, sizeof(struct igmpmsg
), (caddr_t
)&igmpmsg
);
2575 m_copyback(m
, sizeof(struct igmpmsg
), len
, (caddr_t
)&bw_upcalls
[0]);
2579 * XXX do we need to set the address in k_igmpsrc ?
2581 mrtstat
.mrts_upcalls
++;
2582 if (socket_send(ip_mrouter
, m
, &k_igmpsrc
) < 0) {
2583 log(LOG_WARNING
, "bw_upcalls_send: ip_mrouter socket queue full\n");
2584 ++mrtstat
.mrts_upq_sockfull
;
2589 * Compute the timeout hash value for the bw_meter entries
2591 #define BW_METER_TIMEHASH(bw_meter, hash) \
2593 struct timeval next_timeval = (bw_meter)->bm_start_time; \
2595 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \
2596 (hash) = next_timeval.tv_sec; \
2597 if (next_timeval.tv_usec) \
2598 (hash)++; /* XXX: make sure we don't timeout early */ \
2599 (hash) %= BW_METER_BUCKETS; \
2603 * Schedule a timer to process periodically bw_meter entry of type "<="
2604 * by linking the entry in the proper hash bucket.
2607 schedule_bw_meter(struct bw_meter
*x
, struct timeval
*nowp
)
2611 if (!(x
->bm_flags
& BW_METER_LEQ
))
2612 return; /* XXX: we schedule timers only for "<=" entries */
2615 * Reset the bw_meter entry
2618 x
->bm_start_time
= *nowp
;
2619 x
->bm_measured
.b_packets
= 0;
2620 x
->bm_measured
.b_bytes
= 0;
2621 x
->bm_flags
&= ~BW_METER_UPCALL_DELIVERED
;
2625 * Compute the timeout hash value and insert the entry
2627 BW_METER_TIMEHASH(x
, time_hash
);
2628 x
->bm_time_next
= bw_meter_timers
[time_hash
];
2629 bw_meter_timers
[time_hash
] = x
;
2630 x
->bm_time_hash
= time_hash
;
2634 * Unschedule the periodic timer that processes bw_meter entry of type "<="
2635 * by removing the entry from the proper hash bucket.
2638 unschedule_bw_meter(struct bw_meter
*x
)
2641 struct bw_meter
*prev
, *tmp
;
2643 if (!(x
->bm_flags
& BW_METER_LEQ
))
2644 return; /* XXX: we schedule timers only for "<=" entries */
2647 * Compute the timeout hash value and delete the entry
2649 time_hash
= x
->bm_time_hash
;
2650 if (time_hash
>= BW_METER_BUCKETS
)
2651 return; /* Entry was not scheduled */
2653 for (prev
= NULL
, tmp
= bw_meter_timers
[time_hash
];
2654 tmp
!= NULL
; prev
= tmp
, tmp
= tmp
->bm_time_next
)
2659 panic("unschedule_bw_meter: bw_meter entry not found");
2662 prev
->bm_time_next
= x
->bm_time_next
;
2664 bw_meter_timers
[time_hash
] = x
->bm_time_next
;
2666 x
->bm_time_next
= NULL
;
2667 x
->bm_time_hash
= BW_METER_BUCKETS
;
2672 * Process all "<=" type of bw_meter that should be processed now,
2673 * and for each entry prepare an upcall if necessary. Each processed
2674 * entry is rescheduled again for the (periodic) processing.
2676 * This is run periodically (once per second normally). On each round,
2677 * all the potentially matching entries are in the hash slot that we are
2681 bw_meter_process(void)
2683 static uint32_t last_tv_sec
; /* last time we processed this */
2687 struct timeval now
, process_endtime
;
2690 if (last_tv_sec
== now
.tv_sec
)
2691 return; /* nothing to do */
2694 loops
= now
.tv_sec
- last_tv_sec
;
2695 last_tv_sec
= now
.tv_sec
;
2696 if (loops
> BW_METER_BUCKETS
)
2697 loops
= BW_METER_BUCKETS
;
2700 * Process all bins of bw_meter entries from the one after the last
2701 * processed to the current one. On entry, i points to the last bucket
2702 * visited, so we need to increment i at the beginning of the loop.
2704 for (i
= (now
.tv_sec
- loops
) % BW_METER_BUCKETS
; loops
> 0; loops
--) {
2705 struct bw_meter
*x
, *tmp_list
;
2707 if (++i
>= BW_METER_BUCKETS
)
2710 /* Disconnect the list of bw_meter entries from the bin */
2711 tmp_list
= bw_meter_timers
[i
];
2712 bw_meter_timers
[i
] = NULL
;
2714 /* Process the list of bw_meter entries */
2715 while (tmp_list
!= NULL
) {
2717 tmp_list
= tmp_list
->bm_time_next
;
2719 /* Test if the time interval is over */
2720 process_endtime
= x
->bm_start_time
;
2721 BW_TIMEVALADD(&process_endtime
, &x
->bm_threshold
.b_time
);
2722 if (BW_TIMEVALCMP(&process_endtime
, &now
, >)) {
2723 /* Not yet: reschedule, but don't reset */
2726 BW_METER_TIMEHASH(x
, time_hash
);
2727 if (time_hash
== i
&& process_endtime
.tv_sec
== now
.tv_sec
) {
2729 * XXX: somehow the bin processing is a bit ahead of time.
2730 * Put the entry in the next bin.
2732 if (++time_hash
>= BW_METER_BUCKETS
)
2735 x
->bm_time_next
= bw_meter_timers
[time_hash
];
2736 bw_meter_timers
[time_hash
] = x
;
2737 x
->bm_time_hash
= time_hash
;
2743 * Test if we should deliver an upcall
2745 if (((x
->bm_flags
& BW_METER_UNIT_PACKETS
) &&
2746 (x
->bm_measured
.b_packets
<= x
->bm_threshold
.b_packets
)) ||
2747 ((x
->bm_flags
& BW_METER_UNIT_BYTES
) &&
2748 (x
->bm_measured
.b_bytes
<= x
->bm_threshold
.b_bytes
))) {
2749 /* Prepare an upcall for delivery */
2750 bw_meter_prepare_upcall(x
, &now
);
2754 * Reschedule for next processing
2756 schedule_bw_meter(x
, &now
);
2761 /* Send all upcalls that are pending delivery */
2766 * A periodic function for sending all upcalls that are pending delivery
2769 expire_bw_upcalls_send(void *unused
)
2773 callout_reset(&bw_upcalls_ch
, BW_UPCALLS_PERIOD
,
2774 expire_bw_upcalls_send
, NULL
);
2778 * A periodic function for periodic scanning of the multicast forwarding
2779 * table for processing all "<=" bw_meter entries.
2782 expire_bw_meter_process(void *unused
)
2784 if (mrt_api_config
& MRT_MFC_BW_UPCALL
)
2787 callout_reset(&bw_meter_ch
, BW_METER_PERIOD
,
2788 expire_bw_meter_process
, NULL
);
2792 * End of bandwidth monitoring code
2797 * Send the packet up to the user daemon, or eventually do kernel encapsulation
2801 pim_register_send(struct ip
*ip
, struct vif
*vifp
,
2802 struct mbuf
*m
, struct mfc
*rt
)
2804 struct mbuf
*mb_copy
, *mm
;
2806 if (mrtdebug
& DEBUG_PIM
)
2807 log(LOG_DEBUG
, "pim_register_send: ");
2809 mb_copy
= pim_register_prepare(ip
, m
);
2810 if (mb_copy
== NULL
)
2814 * Send all the fragments. Note that the mbuf for each fragment
2815 * is freed by the sending machinery.
2817 for (mm
= mb_copy
; mm
; mm
= mb_copy
) {
2818 mb_copy
= mm
->m_nextpkt
;
2820 mm
= m_pullup(mm
, sizeof(struct ip
));
2822 ip
= mtod(mm
, struct ip
*);
2823 if ((mrt_api_config
& MRT_MFC_RP
) &&
2824 (rt
->mfc_rp
.s_addr
!= INADDR_ANY
)) {
2825 pim_register_send_rp(ip
, vifp
, mm
, rt
);
2827 pim_register_send_upcall(ip
, vifp
, mm
, rt
);
2836 * Return a copy of the data packet that is ready for PIM Register
2838 * XXX: Note that in the returned copy the IP header is a valid one.
2840 static struct mbuf
*
2841 pim_register_prepare(struct ip
*ip
, struct mbuf
*m
)
2843 struct mbuf
*mb_copy
= NULL
;
2846 /* Take care of delayed checksums */
2847 if (m
->m_pkthdr
.csum_flags
& CSUM_DELAY_DATA
) {
2848 in_delayed_cksum(m
);
2849 m
->m_pkthdr
.csum_flags
&= ~CSUM_DELAY_DATA
;
2853 * Copy the old packet & pullup its IP header into the
2854 * new mbuf so we can modify it.
2856 mb_copy
= m_copypacket(m
, MB_DONTWAIT
);
2857 if (mb_copy
== NULL
)
2859 mb_copy
= m_pullup(mb_copy
, ip
->ip_hl
<< 2);
2860 if (mb_copy
== NULL
)
2863 /* take care of the TTL */
2864 ip
= mtod(mb_copy
, struct ip
*);
2867 /* Compute the MTU after the PIM Register encapsulation */
2868 mtu
= 0xffff - sizeof(pim_encap_iphdr
) - sizeof(pim_encap_pimhdr
);
2870 if (ip
->ip_len
<= mtu
) {
2871 /* Turn the IP header into a valid one */
2872 ip
->ip_len
= htons(ip
->ip_len
);
2873 ip
->ip_off
= htons(ip
->ip_off
);
2875 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
2877 /* Fragment the packet */
2878 if (ip_fragment(ip
, &mb_copy
, mtu
, 0, CSUM_DELAY_IP
) != 0) {
2887 * Send an upcall with the data packet to the user-level process.
2890 pim_register_send_upcall(struct ip
*ip
, struct vif
*vifp
,
2891 struct mbuf
*mb_copy
, struct mfc
*rt
)
2893 struct mbuf
*mb_first
;
2894 int len
= ntohs(ip
->ip_len
);
2896 struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
2899 * Add a new mbuf with an upcall header
2901 MGETHDR(mb_first
, MB_DONTWAIT
, MT_HEADER
);
2902 if (mb_first
== NULL
) {
2906 mb_first
->m_data
+= max_linkhdr
;
2907 mb_first
->m_pkthdr
.len
= len
+ sizeof(struct igmpmsg
);
2908 mb_first
->m_len
= sizeof(struct igmpmsg
);
2909 mb_first
->m_next
= mb_copy
;
2911 /* Send message to routing daemon */
2912 im
= mtod(mb_first
, struct igmpmsg
*);
2913 im
->im_msgtype
= IGMPMSG_WHOLEPKT
;
2915 im
->im_vif
= vifp
- viftable
;
2916 im
->im_src
= ip
->ip_src
;
2917 im
->im_dst
= ip
->ip_dst
;
2919 k_igmpsrc
.sin_addr
= ip
->ip_src
;
2921 mrtstat
.mrts_upcalls
++;
2923 if (socket_send(ip_mrouter
, mb_first
, &k_igmpsrc
) < 0) {
2924 if (mrtdebug
& DEBUG_PIM
)
2926 "mcast: pim_register_send_upcall: ip_mrouter socket queue full");
2927 ++mrtstat
.mrts_upq_sockfull
;
2931 /* Keep statistics */
2932 pimstat
.pims_snd_registers_msgs
++;
2933 pimstat
.pims_snd_registers_bytes
+= len
;
2939 * Encapsulate the data packet in PIM Register message and send it to the RP.
2942 pim_register_send_rp(struct ip
*ip
, struct vif
*vifp
,
2943 struct mbuf
*mb_copy
, struct mfc
*rt
)
2945 struct mbuf
*mb_first
;
2946 struct ip
*ip_outer
;
2947 struct pim_encap_pimhdr
*pimhdr
;
2948 int len
= ntohs(ip
->ip_len
);
2949 vifi_t vifi
= rt
->mfc_parent
;
2951 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_lcl_addr
.s_addr
== 0)) {
2953 return EADDRNOTAVAIL
; /* The iif vif is invalid */
2957 * Add a new mbuf with the encapsulating header
2959 MGETHDR(mb_first
, MB_DONTWAIT
, MT_HEADER
);
2960 if (mb_first
== NULL
) {
2964 mb_first
->m_data
+= max_linkhdr
;
2965 mb_first
->m_len
= sizeof(pim_encap_iphdr
) + sizeof(pim_encap_pimhdr
);
2966 mb_first
->m_next
= mb_copy
;
2968 mb_first
->m_pkthdr
.len
= len
+ mb_first
->m_len
;
2971 * Fill in the encapsulating IP and PIM header
2973 ip_outer
= mtod(mb_first
, struct ip
*);
2974 *ip_outer
= pim_encap_iphdr
;
2975 ip_outer
->ip_id
= ip_newid();
2976 ip_outer
->ip_len
= len
+ sizeof(pim_encap_iphdr
) + sizeof(pim_encap_pimhdr
);
2977 ip_outer
->ip_src
= viftable
[vifi
].v_lcl_addr
;
2978 ip_outer
->ip_dst
= rt
->mfc_rp
;
2980 * Copy the inner header TOS to the outer header, and take care of the
2983 ip_outer
->ip_tos
= ip
->ip_tos
;
2984 if (ntohs(ip
->ip_off
) & IP_DF
)
2985 ip_outer
->ip_off
|= IP_DF
;
2986 pimhdr
= (struct pim_encap_pimhdr
*)((caddr_t
)ip_outer
2987 + sizeof(pim_encap_iphdr
));
2988 *pimhdr
= pim_encap_pimhdr
;
2989 /* If the iif crosses a border, set the Border-bit */
2990 if (rt
->mfc_flags
[vifi
] & MRT_MFC_FLAGS_BORDER_VIF
& mrt_api_config
)
2991 pimhdr
->flags
|= htonl(PIM_BORDER_REGISTER
);
2993 mb_first
->m_data
+= sizeof(pim_encap_iphdr
);
2994 pimhdr
->pim
.pim_cksum
= in_cksum(mb_first
, sizeof(pim_encap_pimhdr
));
2995 mb_first
->m_data
-= sizeof(pim_encap_iphdr
);
2997 if (vifp
->v_rate_limit
== 0)
2998 tbf_send_packet(vifp
, mb_first
);
3000 tbf_control(vifp
, mb_first
, ip
, ip_outer
->ip_len
);
3002 /* Keep statistics */
3003 pimstat
.pims_snd_registers_msgs
++;
3004 pimstat
.pims_snd_registers_bytes
+= len
;
3010 * PIM-SMv2 and PIM-DM messages processing.
3011 * Receives and verifies the PIM control messages, and passes them
3012 * up to the listening socket, using rip_input().
3013 * The only message with special processing is the PIM_REGISTER message
3014 * (used by PIM-SM): the PIM header is stripped off, and the inner packet
3015 * is passed to if_simloop().
3018 pim_input(struct mbuf
*m
, ...)
3021 struct ip
*ip
= mtod(m
, struct ip
*);
3024 int datalen
= ip
->ip_len
;
3030 off
= __va_arg(ap
, int);
3031 proto
= __va_arg(ap
, int);
3036 /* Keep statistics */
3037 pimstat
.pims_rcv_total_msgs
++;
3038 pimstat
.pims_rcv_total_bytes
+= datalen
;
3043 if (datalen
< PIM_MINLEN
) {
3044 pimstat
.pims_rcv_tooshort
++;
3045 log(LOG_ERR
, "pim_input: packet size too small %d from %lx\n",
3046 datalen
, (u_long
)ip
->ip_src
.s_addr
);
3052 * If the packet is at least as big as a REGISTER, go agead
3053 * and grab the PIM REGISTER header size, to avoid another
3054 * possible m_pullup() later.
3056 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8
3057 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28
3059 minlen
= iphlen
+ (datalen
>= PIM_REG_MINLEN
? PIM_REG_MINLEN
: PIM_MINLEN
);
3061 * Get the IP and PIM headers in contiguous memory, and
3062 * possibly the PIM REGISTER header.
3064 if ((m
->m_flags
& M_EXT
|| m
->m_len
< minlen
) &&
3065 (m
= m_pullup(m
, minlen
)) == 0) {
3066 log(LOG_ERR
, "pim_input: m_pullup failure\n");
3069 /* m_pullup() may have given us a new mbuf so reset ip. */
3070 ip
= mtod(m
, struct ip
*);
3071 ip_tos
= ip
->ip_tos
;
3073 /* adjust mbuf to point to the PIM header */
3074 m
->m_data
+= iphlen
;
3076 pim
= mtod(m
, struct pim
*);
3079 * Validate checksum. If PIM REGISTER, exclude the data packet.
3081 * XXX: some older PIMv2 implementations don't make this distinction,
3082 * so for compatibility reason perform the checksum over part of the
3083 * message, and if error, then over the whole message.
3085 if (PIM_VT_T(pim
->pim_vt
) == PIM_REGISTER
&& in_cksum(m
, PIM_MINLEN
) == 0) {
3086 /* do nothing, checksum okay */
3087 } else if (in_cksum(m
, datalen
)) {
3088 pimstat
.pims_rcv_badsum
++;
3089 if (mrtdebug
& DEBUG_PIM
)
3090 log(LOG_DEBUG
, "pim_input: invalid checksum");
3095 /* PIM version check */
3096 if (PIM_VT_V(pim
->pim_vt
) < PIM_VERSION
) {
3097 pimstat
.pims_rcv_badversion
++;
3098 log(LOG_ERR
, "pim_input: incorrect version %d, expecting %d\n",
3099 PIM_VT_V(pim
->pim_vt
), PIM_VERSION
);
3104 /* restore mbuf back to the outer IP */
3105 m
->m_data
-= iphlen
;
3108 if (PIM_VT_T(pim
->pim_vt
) == PIM_REGISTER
) {
3110 * Since this is a REGISTER, we'll make a copy of the register
3111 * headers ip + pim + u_int32 + encap_ip, to be passed up to the
3114 struct sockaddr_in dst
= { sizeof(dst
), AF_INET
};
3116 struct ip
*encap_ip
;
3119 if ((reg_vif_num
>= numvifs
) || (reg_vif_num
== VIFI_INVALID
)) {
3120 if (mrtdebug
& DEBUG_PIM
)
3122 "pim_input: register vif not set: %d\n", reg_vif_num
);
3130 if (datalen
< PIM_REG_MINLEN
) {
3131 pimstat
.pims_rcv_tooshort
++;
3132 pimstat
.pims_rcv_badregisters
++;
3134 "pim_input: register packet size too small %d from %lx\n",
3135 datalen
, (u_long
)ip
->ip_src
.s_addr
);
3140 reghdr
= (u_int32_t
*)(pim
+ 1);
3141 encap_ip
= (struct ip
*)(reghdr
+ 1);
3143 if (mrtdebug
& DEBUG_PIM
) {
3145 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n",
3146 (u_long
)ntohl(encap_ip
->ip_src
.s_addr
),
3147 (u_long
)ntohl(encap_ip
->ip_dst
.s_addr
),
3148 ntohs(encap_ip
->ip_len
));
3151 /* verify the version number of the inner packet */
3152 if (encap_ip
->ip_v
!= IPVERSION
) {
3153 pimstat
.pims_rcv_badregisters
++;
3154 if (mrtdebug
& DEBUG_PIM
) {
3155 log(LOG_DEBUG
, "pim_input: invalid IP version (%d) "
3156 "of the inner packet\n", encap_ip
->ip_v
);
3162 /* verify the inner packet is destined to a mcast group */
3163 if (!IN_MULTICAST(ntohl(encap_ip
->ip_dst
.s_addr
))) {
3164 pimstat
.pims_rcv_badregisters
++;
3165 if (mrtdebug
& DEBUG_PIM
)
3167 "pim_input: inner packet of register is not "
3169 (u_long
)ntohl(encap_ip
->ip_dst
.s_addr
));
3174 /* If a NULL_REGISTER, pass it to the daemon */
3175 if ((ntohl(*reghdr
) & PIM_NULL_REGISTER
))
3176 goto pim_input_to_daemon
;
3179 * Copy the TOS from the outer IP header to the inner IP header.
3181 if (encap_ip
->ip_tos
!= ip_tos
) {
3182 /* Outer TOS -> inner TOS */
3183 encap_ip
->ip_tos
= ip_tos
;
3184 /* Recompute the inner header checksum. Sigh... */
3186 /* adjust mbuf to point to the inner IP header */
3187 m
->m_data
+= (iphlen
+ PIM_MINLEN
);
3188 m
->m_len
-= (iphlen
+ PIM_MINLEN
);
3190 encap_ip
->ip_sum
= 0;
3191 encap_ip
->ip_sum
= in_cksum(m
, encap_ip
->ip_hl
<< 2);
3193 /* restore mbuf to point back to the outer IP header */
3194 m
->m_data
-= (iphlen
+ PIM_MINLEN
);
3195 m
->m_len
+= (iphlen
+ PIM_MINLEN
);
3199 * Decapsulate the inner IP packet and loopback to forward it
3200 * as a normal multicast packet. Also, make a copy of the
3201 * outer_iphdr + pimhdr + reghdr + encap_iphdr
3202 * to pass to the daemon later, so it can take the appropriate
3203 * actions (e.g., send back PIM_REGISTER_STOP).
3204 * XXX: here m->m_data points to the outer IP header.
3206 mcp
= m_copy(m
, 0, iphlen
+ PIM_REG_MINLEN
);
3209 "pim_input: pim register: could not copy register head\n");
3214 /* Keep statistics */
3215 /* XXX: registers_bytes include only the encap. mcast pkt */
3216 pimstat
.pims_rcv_registers_msgs
++;
3217 pimstat
.pims_rcv_registers_bytes
+= ntohs(encap_ip
->ip_len
);
3220 * forward the inner ip packet; point m_data at the inner ip.
3222 m_adj(m
, iphlen
+ PIM_MINLEN
);
3224 if (mrtdebug
& DEBUG_PIM
) {
3226 "pim_input: forwarding decapsulated register: "
3227 "src %lx, dst %lx, vif %d\n",
3228 (u_long
)ntohl(encap_ip
->ip_src
.s_addr
),
3229 (u_long
)ntohl(encap_ip
->ip_dst
.s_addr
),
3232 if_simloop(viftable
[reg_vif_num
].v_ifp
, m
, dst
.sin_family
, 0);
3234 /* prepare the register head to send to the mrouting daemon */
3238 pim_input_to_daemon
:
3240 * Pass the PIM message up to the daemon; if it is a Register message,
3241 * pass the 'head' only up to the daemon. This includes the
3242 * outer IP header, PIM header, PIM-Register header and the
3244 * XXX: the outer IP header pkt size of a Register is not adjust to
3245 * reflect the fact that the inner multicast data is truncated.
3247 rip_input(m
, iphlen
, proto
);
3254 ip_mroute_modevent(module_t mod
, int type
, void *unused
)
3259 /* XXX Protect against multiple loading */
3260 ip_mcast_src
= X_ip_mcast_src
;
3261 ip_mforward
= X_ip_mforward
;
3262 ip_mrouter_done
= X_ip_mrouter_done
;
3263 ip_mrouter_get
= X_ip_mrouter_get
;
3264 ip_mrouter_set
= X_ip_mrouter_set
;
3265 ip_rsvp_force_done
= X_ip_rsvp_force_done
;
3266 ip_rsvp_vif
= X_ip_rsvp_vif
;
3267 ipip_input
= X_ipip_input
;
3268 legal_vif_num
= X_legal_vif_num
;
3269 mrt_ioctl
= X_mrt_ioctl
;
3270 rsvp_input_p
= X_rsvp_input
;
3279 ip_mcast_src
= NULL
;
3281 ip_mrouter_done
= NULL
;
3282 ip_mrouter_get
= NULL
;
3283 ip_mrouter_set
= NULL
;
3284 ip_rsvp_force_done
= NULL
;
3287 legal_vif_num
= NULL
;
3289 rsvp_input_p
= NULL
;
3296 static moduledata_t ip_mroutemod
= {
3301 DECLARE_MODULE(ip_mroute
, ip_mroutemod
, SI_SUB_PSEUDO
, SI_ORDER_ANY
);