]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_mroute.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * IP multicast forwarding procedures
25 * Written by David Waitzman, BBN Labs, August 1988.
26 * Modified by Steve Deering, Stanford, February 1989.
27 * Modified by Mark J. Steiglitz, Stanford, May, 1991
28 * Modified by Van Jacobson, LBL, January 1993
29 * Modified by Ajit Thyagarajan, PARC, August 1993
30 * Modified by Bill Fenner, PARC, April 1995
32 * MROUTING Revision: 3.5
33 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/protosw.h>
45 #include <sys/kernel.h>
46 #include <sys/sockio.h>
47 #include <sys/syslog.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/in_var.h>
55 #include <netinet/igmp.h>
56 #include <netinet/ip_mroute.h>
57 #include <netinet/udp.h>
60 #if BYTE_ORDER != BIG_ENDIAN
61 #define NTOHL(d) ((d) = ntohl((d)))
62 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
63 #define HTONL(d) ((d) = htonl((d)))
64 #define HTONS(d) ((d) = htons((u_short)(d)))
74 extern u_long _ip_mcast_src
__P((int vifi
));
75 extern int _ip_mforward
__P((struct ip
*ip
, struct ifnet
*ifp
,
76 struct mbuf
*m
, struct ip_moptions
*imo
));
77 extern int _ip_mrouter_done
__P((void));
78 extern int _ip_mrouter_get
__P((struct socket
*so
, struct sockopt
*sopt
));
79 extern int _ip_mrouter_set
__P((struct socket
*so
, struct sockopt
*sopt
));
80 extern int _mrt_ioctl
__P((int req
, caddr_t data
, struct proc
*p
));
83 * Dummy routines and globals used when multicast routing is not compiled in.
86 struct socket
*ip_mrouter
= NULL
;
90 _ip_mrouter_set(so
, sopt
)
97 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = _ip_mrouter_set
;
101 _ip_mrouter_get(so
, sopt
)
103 struct sockopt
*sopt
;
108 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = _ip_mrouter_get
;
116 int (*ip_mrouter_done
)(void) = _ip_mrouter_done
;
119 _ip_mforward(ip
, ifp
, m
, imo
)
123 struct ip_moptions
*imo
;
128 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
129 struct ip_moptions
*) = _ip_mforward
;
132 _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
)
137 int (*mrt_ioctl
)(int, caddr_t
, struct proc
*) = _mrt_ioctl
;
140 rsvp_input(m
, iphlen
) /* XXX must fixup manually */
144 /* Can still get packets with rsvp_on = 0 if there is a local member
145 * of the group to which the RSVP packet is addressed. But in this
146 * case we want to throw the packet away.
153 if (ip_rsvpd
!= NULL
) {
155 printf("rsvp_input: Sending packet up old-style socket\n");
156 rip_input(m
, iphlen
);
159 /* Drop the packet */
163 void ipip_input(struct mbuf
*m
, int iphlen
) { /* XXX must fixup manually */
164 rip_input(m
, iphlen
);
167 int (*legal_vif_num
)(int) = 0;
170 * This should never be called, since IP_MULTICAST_VIF should fail, but
171 * just in case it does get called, the code a little lower in ip_output
172 * will assign the packet a local address.
175 _ip_mcast_src(int vifi
) { return INADDR_ANY
; }
176 u_long (*ip_mcast_src
)(int) = _ip_mcast_src
;
179 ip_rsvp_vif_init(so
, sopt
)
181 struct sockopt
*sopt
;
187 ip_rsvp_vif_done(so
, sopt
)
189 struct sockopt
*sopt
;
195 ip_rsvp_force_done(so
)
203 #define M_HASCL(m) ((m)->m_flags & M_EXT)
205 #define INSIZ sizeof(struct in_addr)
206 #define same(a1, a2) \
207 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
211 * Globals. All but ip_mrouter and ip_mrtproto could be static,
212 * except for netstat or debugging purposes.
215 struct socket
*ip_mrouter
= NULL
;
216 static struct mrtstat mrtstat
;
217 #else /* MROUTE_LKM */
218 extern void X_ipip_input
__P((struct mbuf
*m
, int iphlen
));
219 extern struct mrtstat mrtstat
;
220 static int ip_mrtproto
;
223 #define NO_RTE_FOUND 0x1
224 #define RTE_FOUND 0x2
226 static struct mfc
*mfctable
[MFCTBLSIZ
];
227 static u_char nexpire
[MFCTBLSIZ
];
228 static struct vif viftable
[MAXVIFS
];
229 static u_int mrtdebug
= 0; /* debug level */
230 #define DEBUG_MFC 0x02
231 #define DEBUG_FORWARD 0x04
232 #define DEBUG_EXPIRE 0x08
233 #define DEBUG_XMIT 0x10
234 static u_int tbfdebug
= 0; /* tbf debug level */
235 static u_int rsvpdebug
= 0; /* rsvp debug level */
237 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
238 #define UPCALL_EXPIRE 6 /* number of timeouts */
241 * Define the token bucket filter structures
242 * tbftable -> each vif has one of these for storing info
245 static struct tbf tbftable
[MAXVIFS
];
246 #define TBF_REPROCESS (hz / 100) /* 100x / second */
249 * 'Interfaces' associated with decapsulator (so we can tell
250 * packets that went through it from ones that get reflected
251 * by a broken gateway). These interfaces are never linked into
252 * the system ifnet list & no routes point to them. I.e., packets
253 * can't be sent this way. They only exist as a placeholder for
254 * multicast source verification.
256 static struct ifnet multicast_decap_if
[MAXVIFS
];
259 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
261 /* prototype IP hdr for encapsulated packets */
262 static struct ip multicast_encap_iphdr
= {
263 #if BYTE_ORDER == LITTLE_ENDIAN
264 sizeof(struct ip
) >> 2, IPVERSION
,
266 IPVERSION
, sizeof(struct ip
) >> 2,
269 sizeof(struct ip
), /* total length */
272 ENCAP_TTL
, ENCAP_PROTO
,
279 static vifi_t numvifs
= 0;
280 static int have_encap_tunnel
= 0;
283 * one-back cache used by ipip_input to locate a tunnel's vif
284 * given a datagram's src ip address.
286 static u_long last_encap_src
;
287 static struct vif
*last_encap_vif
;
289 static u_long X_ip_mcast_src
__P((int vifi
));
290 static int X_ip_mforward
__P((struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
, struct ip_moptions
*imo
));
291 static int X_ip_mrouter_done
__P((void));
292 static int X_ip_mrouter_get
__P((struct socket
*so
, struct sockopt
*m
));
293 static int X_ip_mrouter_set
__P((struct socket
*so
, struct sockopt
*m
));
294 static int X_legal_vif_num
__P((int vif
));
295 static int X_mrt_ioctl
__P((int cmd
, caddr_t data
));
297 static int get_sg_cnt(struct sioc_sg_req
*);
298 static int get_vif_cnt(struct sioc_vif_req
*);
299 static int ip_mrouter_init(struct socket
*, int);
300 static int add_vif(struct vifctl
*);
301 static int del_vif(vifi_t
);
302 static int add_mfc(struct mfcctl
*);
303 static int del_mfc(struct mfcctl
*);
304 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
305 static int set_assert(int);
306 static void expire_upcalls(void *);
307 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*,
309 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
310 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
311 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
312 static void tbf_queue(struct vif
*, struct mbuf
*);
313 static void tbf_process_q(struct vif
*);
314 static void tbf_reprocess_q(void *);
315 static int tbf_dq_sel(struct vif
*, struct ip
*);
316 static void tbf_send_packet(struct vif
*, struct mbuf
*);
317 static void tbf_update_tokens(struct vif
*);
318 static int priority(struct vif
*, struct ip
*);
319 void multiencap_decap(struct mbuf
*);
322 * whether or not special PIM assert processing is enabled.
324 static int pim_assert
;
326 * Rate limit for assert notification messages, in usec
328 #define ASSERT_MSG_TIME 3000000
331 * Hash function for a source, group entry
333 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
334 ((g) >> 20) ^ ((g) >> 10) ^ (g))
337 * Find a route for a given origin IP address and Multicast group address
338 * Type of service parameter to be added in the future!!!
341 #define MFCFIND(o, g, rt) { \
342 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
344 ++mrtstat.mrts_mfc_lookups; \
346 if ((_rt->mfc_origin.s_addr == o) && \
347 (_rt->mfc_mcastgrp.s_addr == g) && \
348 (_rt->mfc_stall == NULL)) { \
352 _rt = _rt->mfc_next; \
355 ++mrtstat.mrts_mfc_misses; \
361 * Macros to compute elapsed time efficiently
362 * Borrowed from Van Jacobson's scheduling code
364 #define TV_DELTA(a, b, delta) { \
367 delta = (a).tv_usec - (b).tv_usec; \
368 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
377 delta += (1000000 * xxs); \
382 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
383 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
386 u_long upcall_data
[51];
387 static void collate(struct timeval
*);
388 #endif /* UPCALL_TIMING */
392 * Handle MRT setsockopt commands to modify the multicast routing tables.
395 X_ip_mrouter_set(so
, sopt
)
397 struct sockopt
*sopt
;
404 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
408 switch (sopt
->sopt_name
) {
410 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
414 error
= ip_mrouter_init(so
, optval
);
418 error
= ip_mrouter_done();
422 error
= sooptcopyin(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
425 error
= add_vif(&vifc
);
429 error
= sooptcopyin(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
432 error
= del_vif(vifi
);
437 error
= sooptcopyin(sopt
, &mfc
, sizeof mfc
, sizeof mfc
);
440 if (sopt
->sopt_name
== MRT_ADD_MFC
)
441 error
= add_mfc(&mfc
);
443 error
= del_mfc(&mfc
);
447 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
461 #if !defined(MROUTE_LKM) || !MROUTE_LKM
462 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_set
;
466 * Handle MRT getsockopt commands
469 X_ip_mrouter_get(so
, sopt
)
471 struct sockopt
*sopt
;
474 static int version
= 0x0305; /* !!! why is this here? XXX */
476 switch (sopt
->sopt_name
) {
478 error
= sooptcopyout(sopt
, &version
, sizeof version
);
482 error
= sooptcopyout(sopt
, &pim_assert
, sizeof pim_assert
);
491 #if !defined(MROUTE_LKM) || !MROUTE_LKM
492 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_get
;
496 * Handle ioctl commands to obtain information from the cache
499 X_mrt_ioctl(cmd
, data
)
506 case (SIOCGETVIFCNT
):
507 return (get_vif_cnt((struct sioc_vif_req
*)data
));
510 return (get_sg_cnt((struct sioc_sg_req
*)data
));
519 #if !defined(MROUTE_LKM) || !MROUTE_LKM
520 int (*mrt_ioctl
)(int, caddr_t
) = X_mrt_ioctl
;
524 * returns the packet, byte, rpf-failure count for the source group provided
528 register struct sioc_sg_req
*req
;
530 register struct mfc
*rt
;
534 MFCFIND(req
->src
.s_addr
, req
->grp
.s_addr
, rt
);
537 req
->pktcnt
= rt
->mfc_pkt_cnt
;
538 req
->bytecnt
= rt
->mfc_byte_cnt
;
539 req
->wrong_if
= rt
->mfc_wrong_if
;
541 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
547 * returns the input and output packet and byte counts on the vif provided
551 register struct sioc_vif_req
*req
;
553 register vifi_t vifi
= req
->vifi
;
555 if (vifi
>= numvifs
) return EINVAL
;
557 req
->icount
= viftable
[vifi
].v_pkt_in
;
558 req
->ocount
= viftable
[vifi
].v_pkt_out
;
559 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
560 req
->obytes
= viftable
[vifi
].v_bytes_out
;
566 * Enable multicast routing
569 ip_mrouter_init(so
, version
)
574 log(LOG_DEBUG
,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
575 so
->so_type
, so
->so_proto
->pr_protocol
);
577 if (so
->so_type
!= SOCK_RAW
||
578 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
) return EOPNOTSUPP
;
583 if (ip_mrouter
!= NULL
) return EADDRINUSE
;
587 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
588 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
592 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
595 log(LOG_DEBUG
, "ip_mrouter_init\n");
601 * Disable multicast routing
617 * For each phyint in use, disable promiscuous reception of all IP
620 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
621 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
622 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
623 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
624 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
626 ifp
= viftable
[vifi
].v_ifp
;
630 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
631 bzero((caddr_t
)viftable
, sizeof(viftable
));
635 untimeout(expire_upcalls
, (caddr_t
)NULL
);
638 * Free all multicast forwarding cache entries.
640 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
641 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
642 struct mfc
*nr
= rt
->mfc_next
;
644 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
645 struct rtdetq
*n
= rte
->next
;
648 FREE(rte
, M_MRTABLE
);
656 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
659 * Reset de-encapsulation cache
662 last_encap_vif
= NULL
;
663 have_encap_tunnel
= 0;
670 log(LOG_DEBUG
, "ip_mrouter_done\n");
675 #if !defined(MROUTE_LKM) || !MROUTE_LKM
676 int (*ip_mrouter_done
)(void) = X_ip_mrouter_done
;
680 * Set PIM assert processing global
686 if ((i
!= 1) && (i
!= 0))
695 * Add a vif to the vif table
699 register struct vifctl
*vifcp
;
701 register struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
702 static struct sockaddr_in sin
= {sizeof sin
, AF_INET
};
706 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
708 if (vifcp
->vifc_vifi
>= MAXVIFS
) return EINVAL
;
709 if (vifp
->v_lcl_addr
.s_addr
!= 0) return EADDRINUSE
;
711 /* Find the interface with an address in AF_INET family */
712 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
713 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
714 if (ifa
== 0) return EADDRNOTAVAIL
;
717 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
718 if ((vifcp
->vifc_flags
& VIFF_SRCRT
) == 0) {
720 * An encapsulating tunnel is wanted. Tell ipip_input() to
721 * start paying attention to encapsulated packets.
723 if (have_encap_tunnel
== 0) {
724 have_encap_tunnel
= 1;
725 for (s
= 0; s
< MAXVIFS
; ++s
) {
726 multicast_decap_if
[s
].if_name
= "mdecap";
727 multicast_decap_if
[s
].if_unit
= s
;
728 multicast_decap_if
[s
].if_family
= APPLE_IF_FAM_MDECAP
;
732 * Set interface to fake encapsulator interface
734 ifp
= &multicast_decap_if
[vifcp
->vifc_vifi
];
736 * Prepare cached route entry
738 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
740 log(LOG_ERR
, "source routed tunnels not supported\n");
744 /* Make sure the interface supports multicast */
745 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
748 /* Enable promiscuous reception of all IP multicasts from the if */
750 error
= if_allmulti(ifp
, 1);
757 /* define parameters for the tbf structure */
759 GET_TIME(vifp
->v_tbf
->tbf_last_pkt_t
);
760 vifp
->v_tbf
->tbf_n_tok
= 0;
761 vifp
->v_tbf
->tbf_q_len
= 0;
762 vifp
->v_tbf
->tbf_max_q_len
= MAXQSIZE
;
763 vifp
->v_tbf
->tbf_q
= vifp
->v_tbf
->tbf_t
= NULL
;
765 vifp
->v_flags
= vifcp
->vifc_flags
;
766 vifp
->v_threshold
= vifcp
->vifc_threshold
;
767 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
768 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
770 /* scaling up here allows division by 1024 in critical code */
771 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
773 vifp
->v_rsvpd
= NULL
;
774 /* initialize per vif pkt counters */
777 vifp
->v_bytes_in
= 0;
778 vifp
->v_bytes_out
= 0;
781 /* Adjust numvifs up if the vifi is higher than numvifs */
782 if (numvifs
<= vifcp
->vifc_vifi
) numvifs
= vifcp
->vifc_vifi
+ 1;
785 log(LOG_DEBUG
, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
787 (u_long
)ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
788 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
789 (u_long
)ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
790 vifcp
->vifc_threshold
,
791 vifcp
->vifc_rate_limit
);
797 * Delete a vif from the vif table
803 register struct vif
*vifp
= &viftable
[vifi
];
804 register struct mbuf
*m
;
809 if (vifi
>= numvifs
) return EINVAL
;
810 if (vifp
->v_lcl_addr
.s_addr
== 0) return EADDRNOTAVAIL
;
814 if (!(vifp
->v_flags
& VIFF_TUNNEL
)) {
815 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
816 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
= INADDR_ANY
;
821 if (vifp
== last_encap_vif
) {
827 * Free packets queued at the interface
829 while (vifp
->v_tbf
->tbf_q
) {
830 m
= vifp
->v_tbf
->tbf_q
;
831 vifp
->v_tbf
->tbf_q
= m
->m_act
;
835 bzero((caddr_t
)vifp
->v_tbf
, sizeof(*(vifp
->v_tbf
)));
836 bzero((caddr_t
)vifp
, sizeof (*vifp
));
839 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", vifi
, numvifs
);
841 /* Adjust numvifs down */
842 for (vifi
= numvifs
; vifi
> 0; vifi
--)
843 if (viftable
[vifi
-1].v_lcl_addr
.s_addr
!= 0) break;
856 struct mfcctl
*mfccp
;
861 register u_short nstl
;
865 MFCFIND(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
, rt
);
867 /* If an entry already exists, just update the fields */
869 if (mrtdebug
& DEBUG_MFC
)
870 log(LOG_DEBUG
,"add_mfc update o %lx g %lx p %x\n",
871 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
872 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
876 rt
->mfc_parent
= mfccp
->mfcc_parent
;
877 for (i
= 0; i
< numvifs
; i
++)
878 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
884 * Find the entry for which the upcall was made and update
887 hash
= MFCHASH(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
888 for (rt
= mfctable
[hash
], nstl
= 0; rt
; rt
= rt
->mfc_next
) {
890 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
891 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
) &&
892 (rt
->mfc_stall
!= NULL
)) {
895 log(LOG_ERR
, "add_mfc %s o %lx g %lx p %x dbx %p\n",
896 "multiple kernel entries",
897 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
898 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
899 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
901 if (mrtdebug
& DEBUG_MFC
)
902 log(LOG_DEBUG
,"add_mfc o %lx g %lx p %x dbg %p\n",
903 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
904 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
905 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
907 rt
->mfc_origin
= mfccp
->mfcc_origin
;
908 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
909 rt
->mfc_parent
= mfccp
->mfcc_parent
;
910 for (i
= 0; i
< numvifs
; i
++)
911 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
912 /* initialize pkt counters per src-grp */
914 rt
->mfc_byte_cnt
= 0;
915 rt
->mfc_wrong_if
= 0;
916 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
918 rt
->mfc_expire
= 0; /* Don't clean this guy up */
921 /* free packets Qed at the end of this entry */
922 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
923 struct rtdetq
*n
= rte
->next
;
925 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
929 #endif /* UPCALL_TIMING */
930 FREE(rte
, M_MRTABLE
);
933 rt
->mfc_stall
= NULL
;
938 * It is possible that an entry is being inserted without an upcall
941 if (mrtdebug
& DEBUG_MFC
)
942 log(LOG_DEBUG
,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
943 hash
, (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
944 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
947 for (rt
= mfctable
[hash
]; rt
!= NULL
; rt
= rt
->mfc_next
) {
949 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
950 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
)) {
952 rt
->mfc_origin
= mfccp
->mfcc_origin
;
953 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
954 rt
->mfc_parent
= mfccp
->mfcc_parent
;
955 for (i
= 0; i
< numvifs
; i
++)
956 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
957 /* initialize pkt counters per src-grp */
959 rt
->mfc_byte_cnt
= 0;
960 rt
->mfc_wrong_if
= 0;
961 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
968 /* no upcall, so make a new entry */
969 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
975 /* insert new entry at head of hash chain */
976 rt
->mfc_origin
= mfccp
->mfcc_origin
;
977 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
978 rt
->mfc_parent
= mfccp
->mfcc_parent
;
979 for (i
= 0; i
< numvifs
; i
++)
980 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
981 /* initialize pkt counters per src-grp */
983 rt
->mfc_byte_cnt
= 0;
984 rt
->mfc_wrong_if
= 0;
985 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
987 rt
->mfc_stall
= NULL
;
989 /* link into table */
990 rt
->mfc_next
= mfctable
[hash
];
1000 * collect delay statistics on the upcalls
1002 static void collate(t
)
1003 register struct timeval
*t
;
1006 register struct timeval tp
;
1007 register u_long delta
;
1013 TV_DELTA(tp
, *t
, delta
);
1022 #endif /* UPCALL_TIMING */
1025 * Delete an mfc entry
1029 struct mfcctl
*mfccp
;
1031 struct in_addr origin
;
1032 struct in_addr mcastgrp
;
1038 origin
= mfccp
->mfcc_origin
;
1039 mcastgrp
= mfccp
->mfcc_mcastgrp
;
1040 hash
= MFCHASH(origin
.s_addr
, mcastgrp
.s_addr
);
1042 if (mrtdebug
& DEBUG_MFC
)
1043 log(LOG_DEBUG
,"del_mfc orig %lx mcastgrp %lx\n",
1044 (u_long
)ntohl(origin
.s_addr
), (u_long
)ntohl(mcastgrp
.s_addr
));
1048 nptr
= &mfctable
[hash
];
1049 while ((rt
= *nptr
) != NULL
) {
1050 if (origin
.s_addr
== rt
->mfc_origin
.s_addr
&&
1051 mcastgrp
.s_addr
== rt
->mfc_mcastgrp
.s_addr
&&
1052 rt
->mfc_stall
== NULL
)
1055 nptr
= &rt
->mfc_next
;
1059 return EADDRNOTAVAIL
;
1062 *nptr
= rt
->mfc_next
;
1063 FREE(rt
, M_MRTABLE
);
1071 * Send a message to mrouted on the multicast routing socket
1074 socket_send(s
, mm
, src
)
1077 struct sockaddr_in
*src
;
1080 if (sbappendaddr(&s
->so_rcv
,
1081 (struct sockaddr
*)src
,
1082 mm
, (struct mbuf
*)0) != 0) {
1092 * IP multicast forwarding function. This function assumes that the packet
1093 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1094 * pointed to by "ifp", and the packet is to be relayed to other networks
1095 * that have members of the packet's destination IP multicast group.
1097 * The packet is returned unscathed to the caller, unless it is
1098 * erroneous, in which case a non-zero return value tells the caller to
1102 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1103 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1106 X_ip_mforward(ip
, ifp
, m
, imo
)
1107 register struct ip
*ip
;
1110 struct ip_moptions
*imo
;
1112 register struct mfc
*rt
;
1113 register u_char
*ipoptions
;
1114 static struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1115 static int srctun
= 0;
1116 register struct mbuf
*mm
;
1121 if (mrtdebug
& DEBUG_FORWARD
)
1122 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1123 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1126 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1127 (ipoptions
= (u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1129 * Packet arrived via a physical interface or
1130 * an encapsulated tunnel.
1134 * Packet arrived through a source-route tunnel.
1135 * Source-route tunnels are no longer supported.
1137 if ((srctun
++ % 1000) == 0)
1139 "ip_mforward: received source-routed packet from %lx\n",
1140 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1145 if ((imo
) && ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1146 if (ip
->ip_ttl
< 255)
1147 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1148 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1149 vifp
= viftable
+ vifi
;
1150 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1151 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), vifi
,
1152 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1153 vifp
->v_ifp
->if_name
, vifp
->v_ifp
->if_unit
);
1155 return (ip_mdq(m
, ifp
, NULL
, vifi
));
1157 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1158 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1159 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
));
1161 printf("In fact, no options were specified at all\n");
1165 * Don't forward a packet with time-to-live of zero or one,
1166 * or a packet destined to a local-only group.
1168 if (ip
->ip_ttl
<= 1 ||
1169 ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1173 * Determine forwarding vifs from the forwarding cache table
1176 MFCFIND(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, rt
);
1178 /* Entry exists, so forward if necessary */
1181 return (ip_mdq(m
, ifp
, rt
, -1));
1184 * If we don't have a route for packet's origin,
1185 * Make a copy of the packet &
1186 * send message to routing daemon
1189 register struct mbuf
*mb0
;
1190 register struct rtdetq
*rte
;
1191 register u_long hash
;
1192 int hlen
= ip
->ip_hl
<< 2;
1199 mrtstat
.mrts_no_route
++;
1200 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1201 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1202 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1203 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1206 * Allocate mbufs early so that we don't do extra work if we are
1207 * just going to fail anyway. Make sure to pullup the header so
1208 * that other people can't step on it.
1210 rte
= (struct rtdetq
*) _MALLOC((sizeof *rte
), M_MRTABLE
, M_NOWAIT
);
1215 mb0
= m_copy(m
, 0, M_COPYALL
);
1216 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1217 mb0
= m_pullup(mb0
, hlen
);
1219 FREE(rte
, M_MRTABLE
);
1224 /* is there an upcall waiting for this packet? */
1225 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1226 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1227 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1228 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1229 (rt
->mfc_stall
!= NULL
))
1237 /* no upcall, so make a new entry */
1238 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
1240 FREE(rte
, M_MRTABLE
);
1245 /* Make a copy of the header to send to the user level process */
1246 mm
= m_copy(mb0
, 0, hlen
);
1248 FREE(rte
, M_MRTABLE
);
1250 FREE(rt
, M_MRTABLE
);
1256 * Send message to routing daemon to install
1257 * a route into the kernel table
1259 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1261 im
= mtod(mm
, struct igmpmsg
*);
1262 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1265 mrtstat
.mrts_upcalls
++;
1267 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1268 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1269 ++mrtstat
.mrts_upq_sockfull
;
1270 FREE(rte
, M_MRTABLE
);
1272 FREE(rt
, M_MRTABLE
);
1277 /* insert new entry at head of hash chain */
1278 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1279 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1280 rt
->mfc_expire
= UPCALL_EXPIRE
;
1282 for (i
= 0; i
< numvifs
; i
++)
1283 rt
->mfc_ttls
[i
] = 0;
1284 rt
->mfc_parent
= -1;
1286 /* link into table */
1287 rt
->mfc_next
= mfctable
[hash
];
1288 mfctable
[hash
] = rt
;
1289 rt
->mfc_stall
= rte
;
1292 /* determine if q has overflowed */
1296 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1299 if (npkts
> MAX_UPQ
) {
1300 mrtstat
.mrts_upq_ovflw
++;
1301 FREE(rte
, M_MRTABLE
);
1307 /* Add this entry to the end of the queue */
1324 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1325 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
1326 struct ip_moptions
*) = X_ip_mforward
;
1330 * Clean up the cache entry if upcall is not serviced
1333 expire_upcalls(void *unused
)
1336 struct mfc
*mfc
, **nptr
;
1339 boolean_t funnel_state
;
1342 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1345 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
1346 if (nexpire
[i
] == 0)
1348 nptr
= &mfctable
[i
];
1349 for (mfc
= *nptr
; mfc
!= NULL
; mfc
= *nptr
) {
1351 * Skip real cache entries
1352 * Make sure it wasn't marked to not expire (shouldn't happen)
1355 if (mfc
->mfc_stall
!= NULL
&&
1356 mfc
->mfc_expire
!= 0 &&
1357 --mfc
->mfc_expire
== 0) {
1358 if (mrtdebug
& DEBUG_EXPIRE
)
1359 log(LOG_DEBUG
, "expire_upcalls: expiring (%lx %lx)\n",
1360 (u_long
)ntohl(mfc
->mfc_origin
.s_addr
),
1361 (u_long
)ntohl(mfc
->mfc_mcastgrp
.s_addr
));
1363 * drop all the packets
1364 * free the mbuf with the pkt, if, timing info
1366 for (rte
= mfc
->mfc_stall
; rte
; ) {
1367 struct rtdetq
*n
= rte
->next
;
1370 FREE(rte
, M_MRTABLE
);
1373 ++mrtstat
.mrts_cache_cleanups
;
1376 *nptr
= mfc
->mfc_next
;
1377 FREE(mfc
, M_MRTABLE
);
1379 nptr
= &mfc
->mfc_next
;
1384 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1385 (void) thread_funnel_set(network_flock
, FALSE
);
1389 * Packet forwarding routine once entry in the cache is made
1392 ip_mdq(m
, ifp
, rt
, xmt_vif
)
1393 register struct mbuf
*m
;
1394 register struct ifnet
*ifp
;
1395 register struct mfc
*rt
;
1396 register vifi_t xmt_vif
;
1398 register struct ip
*ip
= mtod(m
, struct ip
*);
1399 register vifi_t vifi
;
1400 register struct vif
*vifp
;
1401 register int plen
= ip
->ip_len
;
1404 * Macro to send packet on vif. Since RSVP packets don't get counted on
1405 * input, they shouldn't get counted on output, so statistics keeping is
1408 #define MC_SEND(ip,vifp,m) { \
1409 if ((vifp)->v_flags & VIFF_TUNNEL) \
1410 encap_send((ip), (vifp), (m)); \
1412 phyint_send((ip), (vifp), (m)); \
1416 * If xmt_vif is not -1, send on only the requested vif.
1418 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1420 if (xmt_vif
< numvifs
) {
1421 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1426 * Don't forward if it didn't arrive from the parent vif for its origin.
1428 vifi
= rt
->mfc_parent
;
1429 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1430 /* came in the wrong interface */
1431 if (mrtdebug
& DEBUG_FORWARD
)
1432 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1433 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1434 ++mrtstat
.mrts_wrong_if
;
1437 * If we are doing PIM assert processing, and we are forwarding
1438 * packets on this interface, and it is a broadcast medium
1439 * interface (and not a tunnel), send a message to the routing daemon.
1441 if (pim_assert
&& rt
->mfc_ttls
[vifi
] &&
1442 (ifp
->if_flags
& IFF_BROADCAST
) &&
1443 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
1444 struct sockaddr_in k_igmpsrc
;
1447 int hlen
= ip
->ip_hl
<< 2;
1449 register u_long delta
;
1453 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1455 if (delta
> ASSERT_MSG_TIME
) {
1456 mm
= m_copy(m
, 0, hlen
);
1457 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1458 mm
= m_pullup(mm
, hlen
);
1463 rt
->mfc_last_assert
= now
;
1465 im
= mtod(mm
, struct igmpmsg
*);
1466 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1470 k_igmpsrc
.sin_addr
= im
->im_src
;
1472 socket_send(ip_mrouter
, mm
, &k_igmpsrc
);
1478 /* If I sourced this packet, it counts as output, else it was input. */
1479 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1480 viftable
[vifi
].v_pkt_out
++;
1481 viftable
[vifi
].v_bytes_out
+= plen
;
1483 viftable
[vifi
].v_pkt_in
++;
1484 viftable
[vifi
].v_bytes_in
+= plen
;
1487 rt
->mfc_byte_cnt
+= plen
;
1490 * For each vif, decide if a copy of the packet should be forwarded.
1492 * - the ttl exceeds the vif's threshold
1493 * - there are group members downstream on interface
1495 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1496 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1497 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1499 vifp
->v_bytes_out
+= plen
;
1500 MC_SEND(ip
, vifp
, m
);
1507 * check if a vif number is legal/ok. This is used by ip_output, to export
1511 X_legal_vif_num(vif
)
1514 if (vif
>= 0 && vif
< numvifs
)
1520 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1521 int (*legal_vif_num
)(int) = X_legal_vif_num
;
1525 * Return the local address used by this vif
1528 X_ip_mcast_src(vifi
)
1531 if (vifi
>= 0 && vifi
< numvifs
)
1532 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1537 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1538 u_long (*ip_mcast_src
)(int) = X_ip_mcast_src
;
1542 phyint_send(ip
, vifp
, m
)
1547 register struct mbuf
*mb_copy
;
1548 register int hlen
= ip
->ip_hl
<< 2;
1551 * Make a new reference to the packet; make sure that
1552 * the IP header is actually copied, not just referenced,
1553 * so that ip_output() only scribbles on the copy.
1555 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1556 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1557 mb_copy
= m_pullup(mb_copy
, hlen
);
1558 if (mb_copy
== NULL
)
1561 if (vifp
->v_rate_limit
== 0)
1562 tbf_send_packet(vifp
, mb_copy
);
1564 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1568 encap_send(ip
, vifp
, m
)
1569 register struct ip
*ip
;
1570 register struct vif
*vifp
;
1571 register struct mbuf
*m
;
1573 register struct mbuf
*mb_copy
;
1574 register struct ip
*ip_copy
;
1575 register int i
, len
= ip
->ip_len
;
1578 * copy the old packet & pullup its IP header into the
1579 * new mbuf so we can modify it. Try to fill the new
1580 * mbuf since if we don't the ethernet driver will.
1582 MGETHDR(mb_copy
, M_DONTWAIT
, MT_HEADER
);
1583 if (mb_copy
== NULL
)
1585 mb_copy
->m_data
+= max_linkhdr
;
1586 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1588 if ((mb_copy
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1592 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1595 mb_copy
= m_pullup(mb_copy
, i
);
1596 if (mb_copy
== NULL
)
1598 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1601 * fill in the encapsulating IP header.
1603 ip_copy
= mtod(mb_copy
, struct ip
*);
1604 *ip_copy
= multicast_encap_iphdr
;
1606 ip_copy
->ip_id
= ip_randomid();
1608 ip_copy
->ip_id
= htons(ip_id
++);
1610 ip_copy
->ip_len
+= len
;
1611 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1612 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1615 * turn the encapsulated IP header back into a valid one.
1617 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1622 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1623 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1624 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1626 if (vifp
->v_rate_limit
== 0)
1627 tbf_send_packet(vifp
, mb_copy
);
1629 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1633 * De-encapsulate a packet and feed it back through ip input (this
1634 * routine is called whenever IP gets a packet with proto type
1635 * ENCAP_PROTO and a local destination address).
1639 X_ipip_input(m
, iphlen
)
1641 ipip_input(m
, iphlen
)
1643 register struct mbuf
*m
;
1646 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1647 register struct ip
*ip
= mtod(m
, struct ip
*);
1648 register int hlen
= ip
->ip_hl
<< 2;
1650 register struct ifqueue
*ifq
;
1651 register struct vif
*vifp
;
1653 if (!have_encap_tunnel
) {
1654 rip_input(m
, iphlen
);
1658 * dump the packet if it's not to a multicast destination or if
1659 * we don't have an encapsulating tunnel with the source.
1660 * Note: This code assumes that the remote site IP address
1661 * uniquely identifies the tunnel (i.e., that this site has
1662 * at most one tunnel with the remote site).
1664 if (! IN_MULTICAST(ntohl(((struct ip
*)((char *)ip
+ hlen
))->ip_dst
.s_addr
))) {
1665 ++mrtstat
.mrts_bad_tunnel
;
1669 if (ip
->ip_src
.s_addr
!= last_encap_src
) {
1670 register struct vif
*vife
;
1673 vife
= vifp
+ numvifs
;
1674 last_encap_src
= ip
->ip_src
.s_addr
;
1676 for ( ; vifp
< vife
; ++vifp
)
1677 if (vifp
->v_rmt_addr
.s_addr
== ip
->ip_src
.s_addr
) {
1678 if ((vifp
->v_flags
& (VIFF_TUNNEL
|VIFF_SRCRT
))
1680 last_encap_vif
= vifp
;
1684 if ((vifp
= last_encap_vif
) == 0) {
1686 mrtstat
.mrts_cant_tunnel
++; /*XXX*/
1689 log(LOG_DEBUG
, "ip_mforward: no tunnel with %lx\n",
1690 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1695 if (hlen
> IP_HDR_LEN
)
1696 ip_stripoptions(m
, (struct mbuf
*) 0);
1697 m
->m_data
+= IP_HDR_LEN
;
1698 m
->m_len
-= IP_HDR_LEN
;
1699 m
->m_pkthdr
.len
-= IP_HDR_LEN
;
1700 m
->m_pkthdr
.rcvif
= ifp
;
1704 if (IF_QFULL(ifq
)) {
1710 * normally we would need a "schednetisr(NETISR_IP)"
1711 * here but we were called by ip_input and it is going
1712 * to loop back & try to dequeue the packet we just
1713 * queued as soon as we return so we avoid the
1714 * unnecessary software interrrupt.
1721 * Token bucket filter module
1725 tbf_control(vifp
, m
, ip
, p_len
)
1726 register struct vif
*vifp
;
1727 register struct mbuf
*m
;
1728 register struct ip
*ip
;
1729 register u_long p_len
;
1731 register struct tbf
*t
= vifp
->v_tbf
;
1733 if (p_len
> MAX_BKT_SIZE
) {
1734 /* drop if packet is too large */
1735 mrtstat
.mrts_pkt2large
++;
1740 tbf_update_tokens(vifp
);
1742 /* if there are enough tokens,
1743 * and the queue is empty,
1744 * send this packet out
1747 if (t
->tbf_q_len
== 0) {
1748 /* queue empty, send packet if enough tokens */
1749 if (p_len
<= t
->tbf_n_tok
) {
1750 t
->tbf_n_tok
-= p_len
;
1751 tbf_send_packet(vifp
, m
);
1753 /* queue packet and timeout till later */
1755 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1757 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1758 /* finite queue length, so queue pkts and process queue */
1760 tbf_process_q(vifp
);
1762 /* queue length too much, try to dq and queue and process */
1763 if (!tbf_dq_sel(vifp
, ip
)) {
1764 mrtstat
.mrts_q_overflow
++;
1769 tbf_process_q(vifp
);
1776 * adds a packet to the queue at the interface
1780 register struct vif
*vifp
;
1781 register struct mbuf
*m
;
1783 register int s
= splnet();
1784 register struct tbf
*t
= vifp
->v_tbf
;
1786 if (t
->tbf_t
== NULL
) {
1787 /* Queue was empty */
1790 /* Insert at tail */
1791 t
->tbf_t
->m_act
= m
;
1794 /* Set new tail pointer */
1798 /* Make sure we didn't get fed a bogus mbuf */
1800 panic("tbf_queue: m_act");
1811 * processes the queue at the interface
1815 register struct vif
*vifp
;
1817 register struct mbuf
*m
;
1819 register int s
= splnet();
1820 register struct tbf
*t
= vifp
->v_tbf
;
1822 /* loop through the queue at the interface and send as many packets
1825 while (t
->tbf_q_len
> 0) {
1828 len
= mtod(m
, struct ip
*)->ip_len
;
1830 /* determine if the packet can be sent */
1831 if (len
<= t
->tbf_n_tok
) {
1833 * reduce no of tokens, dequeue the packet,
1836 t
->tbf_n_tok
-= len
;
1838 t
->tbf_q
= m
->m_act
;
1839 if (--t
->tbf_q_len
== 0)
1843 tbf_send_packet(vifp
, m
);
1851 tbf_reprocess_q(xvifp
)
1854 register struct vif
*vifp
= xvifp
;
1855 boolean_t funnel_state
;
1857 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1858 if (ip_mrouter
== NULL
) {
1859 (void) thread_funnel_set(network_flock
, FALSE
);
1863 tbf_update_tokens(vifp
);
1865 tbf_process_q(vifp
);
1867 if (vifp
->v_tbf
->tbf_q_len
)
1868 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1869 (void) thread_funnel_set(network_flock
, FALSE
);
1872 /* function that will selectively discard a member of the queue
1873 * based on the precedence value and the priority
1876 tbf_dq_sel(vifp
, ip
)
1877 register struct vif
*vifp
;
1878 register struct ip
*ip
;
1880 register int s
= splnet();
1882 register struct mbuf
*m
, *last
;
1883 register struct mbuf
**np
;
1884 register struct tbf
*t
= vifp
->v_tbf
;
1886 p
= priority(vifp
, ip
);
1890 while ((m
= *np
) != NULL
) {
1891 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1893 /* If we're removing the last packet, fix the tail pointer */
1897 /* it's impossible for the queue to be empty, but
1898 * we check anyway. */
1899 if (--t
->tbf_q_len
== 0)
1902 mrtstat
.mrts_drop_sel
++;
1913 tbf_send_packet(vifp
, m
)
1914 register struct vif
*vifp
;
1915 register struct mbuf
*m
;
1917 struct ip_moptions imo
;
1919 static struct route ro
;
1922 if (vifp
->v_flags
& VIFF_TUNNEL
) {
1923 /* If tunnel options */
1924 ip_output(m
, (struct mbuf
*)0, &vifp
->v_route
,
1925 IP_FORWARDING
, (struct ip_moptions
*)0);
1927 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1928 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1929 imo
.imo_multicast_loop
= 1;
1930 imo
.imo_multicast_vif
= -1;
1933 * Re-entrancy should not be a problem here, because
1934 * the packets that we send out and are looped back at us
1935 * should get rejected because they appear to come from
1936 * the loopback interface, thus preventing looping.
1938 error
= ip_output(m
, (struct mbuf
*)0, &ro
,
1939 IP_FORWARDING
, &imo
);
1941 if (mrtdebug
& DEBUG_XMIT
)
1942 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1943 vifp
- viftable
, error
);
1948 /* determine the current time and then
1949 * the elapsed time (between the last time and time now)
1950 * in milliseconds & update the no. of tokens in the bucket
1953 tbf_update_tokens(vifp
)
1954 register struct vif
*vifp
;
1958 register int s
= splnet();
1959 register struct tbf
*t
= vifp
->v_tbf
;
1963 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1966 * This formula is actually
1967 * "time in seconds" * "bytes/second".
1969 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1971 * The (1000/1024) was introduced in add_vif to optimize
1972 * this divide into a shift.
1974 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1975 t
->tbf_last_pkt_t
= tp
;
1977 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1978 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1985 register struct vif
*vifp
;
1986 register struct ip
*ip
;
1990 /* temporary hack; may add general packet classifier some day */
1993 * The UDP port space is divided up into four priority ranges:
1994 * [0, 16384) : unclassified - lowest priority
1995 * [16384, 32768) : audio - highest priority
1996 * [32768, 49152) : whiteboard - medium priority
1997 * [49152, 65536) : video - low priority
1999 if (ip
->ip_p
== IPPROTO_UDP
) {
2000 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
2001 switch (ntohs(udp
->uh_dport
) & 0xc000) {
2016 log(LOG_DEBUG
, "port %x prio%d\n", ntohs(udp
->uh_dport
), prio
);
2024 * End of token bucket filter modifications
2028 ip_rsvp_vif_init(so
, sopt
)
2030 struct sockopt
*sopt
;
2035 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2036 so
->so_type
, so
->so_proto
->pr_protocol
);
2038 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2042 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2047 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i
, rsvp_on
);
2052 if (!legal_vif_num(i
)) {
2054 return EADDRNOTAVAIL
;
2057 /* Check if socket is available. */
2058 if (viftable
[i
].v_rsvpd
!= NULL
) {
2063 viftable
[i
].v_rsvpd
= so
;
2064 /* This may seem silly, but we need to be sure we don't over-increment
2065 * the RSVP counter, in case something slips up.
2067 if (!viftable
[i
].v_rsvp_on
) {
2068 viftable
[i
].v_rsvp_on
= 1;
2077 ip_rsvp_vif_done(so
, sopt
)
2079 struct sockopt
*sopt
;
2084 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2085 so
->so_type
, so
->so_proto
->pr_protocol
);
2087 if (so
->so_type
!= SOCK_RAW
||
2088 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2091 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2098 if (!legal_vif_num(i
)) {
2100 return EADDRNOTAVAIL
;
2104 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2105 viftable
[i
].v_rsvpd
, so
);
2107 viftable
[i
].v_rsvpd
= NULL
;
2109 * This may seem silly, but we need to be sure we don't over-decrement
2110 * the RSVP counter, in case something slips up.
2112 if (viftable
[i
].v_rsvp_on
) {
2113 viftable
[i
].v_rsvp_on
= 0;
2122 ip_rsvp_force_done(so
)
2128 /* Don't bother if it is not the right type of socket. */
2129 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2134 /* The socket may be attached to more than one vif...this
2135 * is perfectly legal.
2137 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2138 if (viftable
[vifi
].v_rsvpd
== so
) {
2139 viftable
[vifi
].v_rsvpd
= NULL
;
2140 /* This may seem silly, but we need to be sure we don't
2141 * over-decrement the RSVP counter, in case something slips up.
2143 if (viftable
[vifi
].v_rsvp_on
) {
2144 viftable
[vifi
].v_rsvp_on
= 0;
2155 rsvp_input(m
, iphlen
)
2160 register struct ip
*ip
= mtod(m
, struct ip
*);
2161 static struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
};
2166 printf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2168 /* Can still get packets with rsvp_on = 0 if there is a local member
2169 * of the group to which the RSVP packet is addressed. But in this
2170 * case we want to throw the packet away.
2180 printf("rsvp_input: check vifs\n");
2183 if (!(m
->m_flags
& M_PKTHDR
))
2184 panic("rsvp_input no hdr");
2187 ifp
= m
->m_pkthdr
.rcvif
;
2188 /* Find which vif the packet arrived on. */
2189 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2190 if (viftable
[vifi
].v_ifp
== ifp
)
2193 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2195 * If the old-style non-vif-associated socket is set,
2196 * then use it. Otherwise, drop packet since there
2197 * is no specific socket for this vif.
2199 if (ip_rsvpd
!= NULL
) {
2201 printf("rsvp_input: Sending packet up old-style socket\n");
2202 rip_input(m
, iphlen
); /* xxx */
2204 if (rsvpdebug
&& vifi
== numvifs
)
2205 printf("rsvp_input: Can't find vif for packet.\n");
2206 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2207 printf("rsvp_input: No socket defined for vif %d\n",vifi
);
2213 rsvp_src
.sin_addr
= ip
->ip_src
;
2216 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2217 m
->m_len
,sbspace(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2219 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2221 printf("rsvp_input: Failed to append to socket\n");
2224 printf("rsvp_input: send packet up\n");
2231 #include <sys/conf.h>
2232 #include <sys/exec.h>
2233 #include <sys/sysent.h>
2234 #include <sys/lkm.h>
2236 MOD_MISC("ip_mroute_mod")
2239 ip_mroute_mod_handle(struct lkm_table
*lkmtp
, int cmd
)
2242 struct lkm_misc
*args
= lkmtp
->private.lkm_misc
;
2246 static int (*old_ip_mrouter_cmd
)();
2247 static int (*old_ip_mrouter_done
)();
2248 static int (*old_ip_mforward
)();
2249 static int (*old_mrt_ioctl
)();
2250 static void (*old_proto4_input
)();
2251 static int (*old_legal_vif_num
)();
2252 extern struct protosw inetsw
[];
2255 if(lkmexists(lkmtp
) || ip_mrtproto
)
2257 old_ip_mrouter_cmd
= ip_mrouter_cmd
;
2258 ip_mrouter_cmd
= X_ip_mrouter_cmd
;
2259 old_ip_mrouter_done
= ip_mrouter_done
;
2260 ip_mrouter_done
= X_ip_mrouter_done
;
2261 old_ip_mforward
= ip_mforward
;
2262 ip_mforward
= X_ip_mforward
;
2263 old_mrt_ioctl
= mrt_ioctl
;
2264 mrt_ioctl
= X_mrt_ioctl
;
2265 old_proto4_input
= ip_protox
[ENCAP_PROTO
]->pr_input
;
2266 ip_protox
[ENCAP_PROTO
]->pr_input
= X_ipip_input
;
2267 old_legal_vif_num
= legal_vif_num
;
2268 legal_vif_num
= X_legal_vif_num
;
2269 ip_mrtproto
= IGMP_DVMRP
;
2271 printf("\nIP multicast routing loaded\n");
2278 ip_mrouter_cmd
= old_ip_mrouter_cmd
;
2279 ip_mrouter_done
= old_ip_mrouter_done
;
2280 ip_mforward
= old_ip_mforward
;
2281 mrt_ioctl
= old_mrt_ioctl
;
2282 ip_protox
[ENCAP_PROTO
]->pr_input
= old_proto4_input
;
2283 legal_vif_num
= old_legal_vif_num
;
2296 ip_mroute_mod(struct lkm_table
*lkmtp
, int cmd
, int ver
) {
2297 DISPATCH(lkmtp
, cmd
, ver
, ip_mroute_mod_handle
, ip_mroute_mod_handle
,
2301 #endif /* MROUTE_LKM */
2302 #endif /* MROUTING */