]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_mroute.c
c12427435e7f846fcf835d7865de05fff6649bda
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * IP multicast forwarding procedures
26 * Written by David Waitzman, BBN Labs, August 1988.
27 * Modified by Steve Deering, Stanford, February 1989.
28 * Modified by Mark J. Steiglitz, Stanford, May, 1991
29 * Modified by Van Jacobson, LBL, January 1993
30 * Modified by Ajit Thyagarajan, PARC, August 1993
31 * Modified by Bill Fenner, PARC, April 1995
33 * MROUTING Revision: 3.5
34 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/malloc.h>
42 #include <sys/socket.h>
43 #include <sys/socketvar.h>
44 #include <sys/protosw.h>
46 #include <sys/kernel.h>
47 #include <sys/sockio.h>
48 #include <sys/syslog.h>
50 #include <net/route.h>
51 #include <netinet/in.h>
52 #include <netinet/in_systm.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_var.h>
55 #include <netinet/in_var.h>
56 #include <netinet/igmp.h>
57 #include <netinet/ip_mroute.h>
58 #include <netinet/udp.h>
61 #if BYTE_ORDER != BIG_ENDIAN
62 #define NTOHL(d) ((d) = ntohl((d)))
63 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
64 #define HTONL(d) ((d) = htonl((d)))
65 #define HTONS(d) ((d) = htons((u_short)(d)))
75 extern u_long
_ip_mcast_src(int vifi
);
76 extern int _ip_mforward(struct ip
*ip
, struct ifnet
*ifp
,
77 struct mbuf
*m
, struct ip_moptions
*imo
);
78 extern int _ip_mrouter_done(void);
79 extern int _ip_mrouter_get(struct socket
*so
, struct sockopt
*sopt
);
80 extern int _ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
);
81 extern int _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
);
84 * Dummy routines and globals used when multicast routing is not compiled in.
87 struct socket
*ip_mrouter
= NULL
;
91 _ip_mrouter_set(so
, sopt
)
98 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = _ip_mrouter_set
;
102 _ip_mrouter_get(so
, sopt
)
104 struct sockopt
*sopt
;
109 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = _ip_mrouter_get
;
117 int (*ip_mrouter_done
)(void) = _ip_mrouter_done
;
120 _ip_mforward(ip
, ifp
, m
, imo
)
124 struct ip_moptions
*imo
;
129 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
130 struct ip_moptions
*) = _ip_mforward
;
133 _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
)
138 int (*mrt_ioctl
)(int, caddr_t
, struct proc
*) = _mrt_ioctl
;
141 rsvp_input(m
, iphlen
) /* XXX must fixup manually */
145 /* Can still get packets with rsvp_on = 0 if there is a local member
146 * of the group to which the RSVP packet is addressed. But in this
147 * case we want to throw the packet away.
154 if (ip_rsvpd
!= NULL
) {
156 printf("rsvp_input: Sending packet up old-style socket\n");
157 rip_input(m
, iphlen
);
160 /* Drop the packet */
164 void ipip_input(struct mbuf
*m
, int iphlen
) { /* XXX must fixup manually */
165 rip_input(m
, iphlen
);
168 int (*legal_vif_num
)(int) = 0;
171 * This should never be called, since IP_MULTICAST_VIF should fail, but
172 * just in case it does get called, the code a little lower in ip_output
173 * will assign the packet a local address.
176 _ip_mcast_src(int vifi
) { return INADDR_ANY
; }
177 u_long (*ip_mcast_src
)(int) = _ip_mcast_src
;
180 ip_rsvp_vif_init(so
, sopt
)
182 struct sockopt
*sopt
;
188 ip_rsvp_vif_done(so
, sopt
)
190 struct sockopt
*sopt
;
196 ip_rsvp_force_done(so
)
204 #define M_HASCL(m) ((m)->m_flags & M_EXT)
206 #define INSIZ sizeof(struct in_addr)
207 #define same(a1, a2) \
208 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
212 * Globals. All but ip_mrouter and ip_mrtproto could be static,
213 * except for netstat or debugging purposes.
216 struct socket
*ip_mrouter
= NULL
;
217 static struct mrtstat mrtstat
;
218 #else /* MROUTE_LKM */
219 extern void X_ipip_input(struct mbuf
*m
, int iphlen
);
220 extern struct mrtstat mrtstat
;
221 static int ip_mrtproto
;
224 #define NO_RTE_FOUND 0x1
225 #define RTE_FOUND 0x2
227 static struct mfc
*mfctable
[MFCTBLSIZ
];
228 static u_char nexpire
[MFCTBLSIZ
];
229 static struct vif viftable
[MAXVIFS
];
230 static u_int mrtdebug
= 0; /* debug level */
231 #define DEBUG_MFC 0x02
232 #define DEBUG_FORWARD 0x04
233 #define DEBUG_EXPIRE 0x08
234 #define DEBUG_XMIT 0x10
235 static u_int tbfdebug
= 0; /* tbf debug level */
236 static u_int rsvpdebug
= 0; /* rsvp debug level */
238 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
239 #define UPCALL_EXPIRE 6 /* number of timeouts */
242 * Define the token bucket filter structures
243 * tbftable -> each vif has one of these for storing info
246 static struct tbf tbftable
[MAXVIFS
];
247 #define TBF_REPROCESS (hz / 100) /* 100x / second */
250 * 'Interfaces' associated with decapsulator (so we can tell
251 * packets that went through it from ones that get reflected
252 * by a broken gateway). These interfaces are never linked into
253 * the system ifnet list & no routes point to them. I.e., packets
254 * can't be sent this way. They only exist as a placeholder for
255 * multicast source verification.
257 static struct ifnet multicast_decap_if
[MAXVIFS
];
260 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
262 /* prototype IP hdr for encapsulated packets */
263 static struct ip multicast_encap_iphdr
= {
264 #if BYTE_ORDER == LITTLE_ENDIAN
265 sizeof(struct ip
) >> 2, IPVERSION
,
267 IPVERSION
, sizeof(struct ip
) >> 2,
270 sizeof(struct ip
), /* total length */
273 ENCAP_TTL
, ENCAP_PROTO
,
280 static vifi_t numvifs
= 0;
281 static int have_encap_tunnel
= 0;
284 * one-back cache used by ipip_input to locate a tunnel's vif
285 * given a datagram's src ip address.
287 static u_long last_encap_src
;
288 static struct vif
*last_encap_vif
;
290 static u_long
X_ip_mcast_src(int vifi
);
291 static int X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
, struct ip_moptions
*imo
);
292 static int X_ip_mrouter_done(void);
293 static int X_ip_mrouter_get(struct socket
*so
, struct sockopt
*m
);
294 static int X_ip_mrouter_set(struct socket
*so
, struct sockopt
*m
);
295 static int X_legal_vif_num(int vif
);
296 static int X_mrt_ioctl(int cmd
, caddr_t data
);
298 static int get_sg_cnt(struct sioc_sg_req
*);
299 static int get_vif_cnt(struct sioc_vif_req
*);
300 static int ip_mrouter_init(struct socket
*, int);
301 static int add_vif(struct vifctl
*);
302 static int del_vif(vifi_t
);
303 static int add_mfc(struct mfcctl
*);
304 static int del_mfc(struct mfcctl
*);
305 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
306 static int set_assert(int);
307 static void expire_upcalls(void *);
308 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*,
310 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
311 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
312 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
313 static void tbf_queue(struct vif
*, struct mbuf
*);
314 static void tbf_process_q(struct vif
*);
315 static void tbf_reprocess_q(void *);
316 static int tbf_dq_sel(struct vif
*, struct ip
*);
317 static void tbf_send_packet(struct vif
*, struct mbuf
*);
318 static void tbf_update_tokens(struct vif
*);
319 static int priority(struct vif
*, struct ip
*);
320 void multiencap_decap(struct mbuf
*);
323 * whether or not special PIM assert processing is enabled.
325 static int pim_assert
;
327 * Rate limit for assert notification messages, in usec
329 #define ASSERT_MSG_TIME 3000000
332 * Hash function for a source, group entry
334 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
335 ((g) >> 20) ^ ((g) >> 10) ^ (g))
338 * Find a route for a given origin IP address and Multicast group address
339 * Type of service parameter to be added in the future!!!
342 #define MFCFIND(o, g, rt) { \
343 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
345 ++mrtstat.mrts_mfc_lookups; \
347 if ((_rt->mfc_origin.s_addr == o) && \
348 (_rt->mfc_mcastgrp.s_addr == g) && \
349 (_rt->mfc_stall == NULL)) { \
353 _rt = _rt->mfc_next; \
356 ++mrtstat.mrts_mfc_misses; \
362 * Macros to compute elapsed time efficiently
363 * Borrowed from Van Jacobson's scheduling code
365 #define TV_DELTA(a, b, delta) { \
368 delta = (a).tv_usec - (b).tv_usec; \
369 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
378 delta += (1000000 * xxs); \
383 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
384 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
387 u_long upcall_data
[51];
388 static void collate(struct timeval
*);
389 #endif /* UPCALL_TIMING */
393 * Handle MRT setsockopt commands to modify the multicast routing tables.
396 X_ip_mrouter_set(so
, sopt
)
398 struct sockopt
*sopt
;
405 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
409 switch (sopt
->sopt_name
) {
411 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
415 error
= ip_mrouter_init(so
, optval
);
419 error
= ip_mrouter_done();
423 error
= sooptcopyin(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
426 error
= add_vif(&vifc
);
430 error
= sooptcopyin(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
433 error
= del_vif(vifi
);
438 error
= sooptcopyin(sopt
, &mfc
, sizeof mfc
, sizeof mfc
);
441 if (sopt
->sopt_name
== MRT_ADD_MFC
)
442 error
= add_mfc(&mfc
);
444 error
= del_mfc(&mfc
);
448 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
462 #if !defined(MROUTE_LKM) || !MROUTE_LKM
463 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_set
;
467 * Handle MRT getsockopt commands
470 X_ip_mrouter_get(so
, sopt
)
472 struct sockopt
*sopt
;
475 static int version
= 0x0305; /* !!! why is this here? XXX */
477 switch (sopt
->sopt_name
) {
479 error
= sooptcopyout(sopt
, &version
, sizeof version
);
483 error
= sooptcopyout(sopt
, &pim_assert
, sizeof pim_assert
);
492 #if !defined(MROUTE_LKM) || !MROUTE_LKM
493 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_get
;
497 * Handle ioctl commands to obtain information from the cache
500 X_mrt_ioctl(cmd
, data
)
507 case (SIOCGETVIFCNT
):
508 return (get_vif_cnt((struct sioc_vif_req
*)data
));
511 return (get_sg_cnt((struct sioc_sg_req
*)data
));
520 #if !defined(MROUTE_LKM) || !MROUTE_LKM
521 int (*mrt_ioctl
)(int, caddr_t
) = X_mrt_ioctl
;
525 * returns the packet, byte, rpf-failure count for the source group provided
529 register struct sioc_sg_req
*req
;
531 register struct mfc
*rt
;
535 MFCFIND(req
->src
.s_addr
, req
->grp
.s_addr
, rt
);
538 req
->pktcnt
= rt
->mfc_pkt_cnt
;
539 req
->bytecnt
= rt
->mfc_byte_cnt
;
540 req
->wrong_if
= rt
->mfc_wrong_if
;
542 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
548 * returns the input and output packet and byte counts on the vif provided
552 register struct sioc_vif_req
*req
;
554 register vifi_t vifi
= req
->vifi
;
556 if (vifi
>= numvifs
) return EINVAL
;
558 req
->icount
= viftable
[vifi
].v_pkt_in
;
559 req
->ocount
= viftable
[vifi
].v_pkt_out
;
560 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
561 req
->obytes
= viftable
[vifi
].v_bytes_out
;
567 * Enable multicast routing
570 ip_mrouter_init(so
, version
)
575 log(LOG_DEBUG
,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
576 so
->so_type
, so
->so_proto
->pr_protocol
);
578 if (so
->so_type
!= SOCK_RAW
||
579 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
) return EOPNOTSUPP
;
584 if (ip_mrouter
!= NULL
) return EADDRINUSE
;
588 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
589 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
593 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
596 log(LOG_DEBUG
, "ip_mrouter_init\n");
602 * Disable multicast routing
618 * For each phyint in use, disable promiscuous reception of all IP
621 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
622 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
623 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
624 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
625 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
627 ifp
= viftable
[vifi
].v_ifp
;
631 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
632 bzero((caddr_t
)viftable
, sizeof(viftable
));
636 untimeout(expire_upcalls
, (caddr_t
)NULL
);
639 * Free all multicast forwarding cache entries.
641 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
642 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
643 struct mfc
*nr
= rt
->mfc_next
;
645 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
646 struct rtdetq
*n
= rte
->next
;
649 FREE(rte
, M_MRTABLE
);
657 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
660 * Reset de-encapsulation cache
663 last_encap_vif
= NULL
;
664 have_encap_tunnel
= 0;
671 log(LOG_DEBUG
, "ip_mrouter_done\n");
676 #if !defined(MROUTE_LKM) || !MROUTE_LKM
677 int (*ip_mrouter_done
)(void) = X_ip_mrouter_done
;
681 * Set PIM assert processing global
687 if ((i
!= 1) && (i
!= 0))
696 * Add a vif to the vif table
700 register struct vifctl
*vifcp
;
702 register struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
703 static struct sockaddr_in sin
= {sizeof sin
, AF_INET
};
707 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
709 if (vifcp
->vifc_vifi
>= MAXVIFS
) return EINVAL
;
710 if (vifp
->v_lcl_addr
.s_addr
!= 0) return EADDRINUSE
;
712 /* Find the interface with an address in AF_INET family */
713 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
714 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
715 if (ifa
== 0) return EADDRNOTAVAIL
;
720 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
721 if ((vifcp
->vifc_flags
& VIFF_SRCRT
) == 0) {
723 * An encapsulating tunnel is wanted. Tell ipip_input() to
724 * start paying attention to encapsulated packets.
726 if (have_encap_tunnel
== 0) {
727 have_encap_tunnel
= 1;
728 for (s
= 0; s
< MAXVIFS
; ++s
) {
729 multicast_decap_if
[s
].if_name
= "mdecap";
730 multicast_decap_if
[s
].if_unit
= s
;
731 multicast_decap_if
[s
].if_family
= APPLE_IF_FAM_MDECAP
;
735 * Set interface to fake encapsulator interface
737 ifp
= &multicast_decap_if
[vifcp
->vifc_vifi
];
739 * Prepare cached route entry
741 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
743 log(LOG_ERR
, "source routed tunnels not supported\n");
747 /* Make sure the interface supports multicast */
748 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
751 /* Enable promiscuous reception of all IP multicasts from the if */
753 error
= if_allmulti(ifp
, 1);
760 /* define parameters for the tbf structure */
762 GET_TIME(vifp
->v_tbf
->tbf_last_pkt_t
);
763 vifp
->v_tbf
->tbf_n_tok
= 0;
764 vifp
->v_tbf
->tbf_q_len
= 0;
765 vifp
->v_tbf
->tbf_max_q_len
= MAXQSIZE
;
766 vifp
->v_tbf
->tbf_q
= vifp
->v_tbf
->tbf_t
= NULL
;
768 vifp
->v_flags
= vifcp
->vifc_flags
;
769 vifp
->v_threshold
= vifcp
->vifc_threshold
;
770 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
771 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
773 /* scaling up here allows division by 1024 in critical code */
774 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
776 vifp
->v_rsvpd
= NULL
;
777 /* initialize per vif pkt counters */
780 vifp
->v_bytes_in
= 0;
781 vifp
->v_bytes_out
= 0;
784 /* Adjust numvifs up if the vifi is higher than numvifs */
785 if (numvifs
<= vifcp
->vifc_vifi
) numvifs
= vifcp
->vifc_vifi
+ 1;
788 log(LOG_DEBUG
, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
790 (u_long
)ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
791 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
792 (u_long
)ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
793 vifcp
->vifc_threshold
,
794 vifcp
->vifc_rate_limit
);
800 * Delete a vif from the vif table
806 register struct vif
*vifp
= &viftable
[vifi
];
807 register struct mbuf
*m
;
812 if (vifi
>= numvifs
) return EINVAL
;
813 if (vifp
->v_lcl_addr
.s_addr
== 0) return EADDRNOTAVAIL
;
817 if (!(vifp
->v_flags
& VIFF_TUNNEL
)) {
818 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
819 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
= INADDR_ANY
;
824 if (vifp
== last_encap_vif
) {
830 * Free packets queued at the interface
832 while (vifp
->v_tbf
->tbf_q
) {
833 m
= vifp
->v_tbf
->tbf_q
;
834 vifp
->v_tbf
->tbf_q
= m
->m_act
;
838 bzero((caddr_t
)vifp
->v_tbf
, sizeof(*(vifp
->v_tbf
)));
839 bzero((caddr_t
)vifp
, sizeof (*vifp
));
842 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", vifi
, numvifs
);
844 /* Adjust numvifs down */
845 for (vifi
= numvifs
; vifi
> 0; vifi
--)
846 if (viftable
[vifi
-1].v_lcl_addr
.s_addr
!= 0) break;
859 struct mfcctl
*mfccp
;
864 register u_short nstl
;
868 MFCFIND(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
, rt
);
870 /* If an entry already exists, just update the fields */
872 if (mrtdebug
& DEBUG_MFC
)
873 log(LOG_DEBUG
,"add_mfc update o %lx g %lx p %x\n",
874 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
875 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
879 rt
->mfc_parent
= mfccp
->mfcc_parent
;
880 for (i
= 0; i
< numvifs
; i
++)
881 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
887 * Find the entry for which the upcall was made and update
890 hash
= MFCHASH(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
891 for (rt
= mfctable
[hash
], nstl
= 0; rt
; rt
= rt
->mfc_next
) {
893 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
894 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
) &&
895 (rt
->mfc_stall
!= NULL
)) {
898 log(LOG_ERR
, "add_mfc %s o %lx g %lx p %x dbx %p\n",
899 "multiple kernel entries",
900 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
901 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
902 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
904 if (mrtdebug
& DEBUG_MFC
)
905 log(LOG_DEBUG
,"add_mfc o %lx g %lx p %x dbg %p\n",
906 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
907 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
908 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
910 rt
->mfc_origin
= mfccp
->mfcc_origin
;
911 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
912 rt
->mfc_parent
= mfccp
->mfcc_parent
;
913 for (i
= 0; i
< numvifs
; i
++)
914 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
915 /* initialize pkt counters per src-grp */
917 rt
->mfc_byte_cnt
= 0;
918 rt
->mfc_wrong_if
= 0;
919 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
921 rt
->mfc_expire
= 0; /* Don't clean this guy up */
924 /* free packets Qed at the end of this entry */
925 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
926 struct rtdetq
*n
= rte
->next
;
928 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
932 #endif /* UPCALL_TIMING */
933 FREE(rte
, M_MRTABLE
);
936 rt
->mfc_stall
= NULL
;
941 * It is possible that an entry is being inserted without an upcall
944 if (mrtdebug
& DEBUG_MFC
)
945 log(LOG_DEBUG
,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
946 hash
, (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
947 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
950 for (rt
= mfctable
[hash
]; rt
!= NULL
; rt
= rt
->mfc_next
) {
952 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
953 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
)) {
955 rt
->mfc_origin
= mfccp
->mfcc_origin
;
956 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
957 rt
->mfc_parent
= mfccp
->mfcc_parent
;
958 for (i
= 0; i
< numvifs
; i
++)
959 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
960 /* initialize pkt counters per src-grp */
962 rt
->mfc_byte_cnt
= 0;
963 rt
->mfc_wrong_if
= 0;
964 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
971 /* no upcall, so make a new entry */
972 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
978 /* insert new entry at head of hash chain */
979 rt
->mfc_origin
= mfccp
->mfcc_origin
;
980 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
981 rt
->mfc_parent
= mfccp
->mfcc_parent
;
982 for (i
= 0; i
< numvifs
; i
++)
983 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
984 /* initialize pkt counters per src-grp */
986 rt
->mfc_byte_cnt
= 0;
987 rt
->mfc_wrong_if
= 0;
988 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
990 rt
->mfc_stall
= NULL
;
992 /* link into table */
993 rt
->mfc_next
= mfctable
[hash
];
1003 * collect delay statistics on the upcalls
1005 static void collate(t
)
1006 register struct timeval
*t
;
1009 register struct timeval tp
;
1010 register u_long delta
;
1016 TV_DELTA(tp
, *t
, delta
);
1025 #endif /* UPCALL_TIMING */
1028 * Delete an mfc entry
1032 struct mfcctl
*mfccp
;
1034 struct in_addr origin
;
1035 struct in_addr mcastgrp
;
1041 origin
= mfccp
->mfcc_origin
;
1042 mcastgrp
= mfccp
->mfcc_mcastgrp
;
1043 hash
= MFCHASH(origin
.s_addr
, mcastgrp
.s_addr
);
1045 if (mrtdebug
& DEBUG_MFC
)
1046 log(LOG_DEBUG
,"del_mfc orig %lx mcastgrp %lx\n",
1047 (u_long
)ntohl(origin
.s_addr
), (u_long
)ntohl(mcastgrp
.s_addr
));
1051 nptr
= &mfctable
[hash
];
1052 while ((rt
= *nptr
) != NULL
) {
1053 if (origin
.s_addr
== rt
->mfc_origin
.s_addr
&&
1054 mcastgrp
.s_addr
== rt
->mfc_mcastgrp
.s_addr
&&
1055 rt
->mfc_stall
== NULL
)
1058 nptr
= &rt
->mfc_next
;
1062 return EADDRNOTAVAIL
;
1065 *nptr
= rt
->mfc_next
;
1066 FREE(rt
, M_MRTABLE
);
1074 * Send a message to mrouted on the multicast routing socket
1077 socket_send(s
, mm
, src
)
1080 struct sockaddr_in
*src
;
1084 if (sbappendaddr(&s
->so_rcv
,
1085 (struct sockaddr
*)src
,
1086 mm
, (struct mbuf
*)0, NULL
) != 0) {
1088 socket_unlock(s
, 1);
1092 socket_unlock(s
, 1);
1098 * IP multicast forwarding function. This function assumes that the packet
1099 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1100 * pointed to by "ifp", and the packet is to be relayed to other networks
1101 * that have members of the packet's destination IP multicast group.
1103 * The packet is returned unscathed to the caller, unless it is
1104 * erroneous, in which case a non-zero return value tells the caller to
1108 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1109 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1112 X_ip_mforward(ip
, ifp
, m
, imo
)
1113 register struct ip
*ip
;
1116 struct ip_moptions
*imo
;
1118 register struct mfc
*rt
;
1119 register u_char
*ipoptions
;
1120 static struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1121 static int srctun
= 0;
1122 register struct mbuf
*mm
;
1127 if (mrtdebug
& DEBUG_FORWARD
)
1128 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1129 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1132 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1133 (ipoptions
= (u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1135 * Packet arrived via a physical interface or
1136 * an encapsulated tunnel.
1140 * Packet arrived through a source-route tunnel.
1141 * Source-route tunnels are no longer supported.
1143 if ((srctun
++ % 1000) == 0)
1145 "ip_mforward: received source-routed packet from %lx\n",
1146 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1151 if ((imo
) && ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1152 if (ip
->ip_ttl
< 255)
1153 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1154 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1155 vifp
= viftable
+ vifi
;
1156 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1157 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), vifi
,
1158 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1159 vifp
->v_ifp
->if_name
, vifp
->v_ifp
->if_unit
);
1161 return (ip_mdq(m
, ifp
, NULL
, vifi
));
1163 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1164 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1165 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
));
1167 printf("In fact, no options were specified at all\n");
1171 * Don't forward a packet with time-to-live of zero or one,
1172 * or a packet destined to a local-only group.
1174 if (ip
->ip_ttl
<= 1 ||
1175 ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1179 * Determine forwarding vifs from the forwarding cache table
1182 MFCFIND(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, rt
);
1184 /* Entry exists, so forward if necessary */
1187 return (ip_mdq(m
, ifp
, rt
, -1));
1190 * If we don't have a route for packet's origin,
1191 * Make a copy of the packet &
1192 * send message to routing daemon
1195 register struct mbuf
*mb0
;
1196 register struct rtdetq
*rte
;
1197 register u_long hash
;
1198 int hlen
= ip
->ip_hl
<< 2;
1205 mrtstat
.mrts_no_route
++;
1206 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1207 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1208 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1209 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1212 * Allocate mbufs early so that we don't do extra work if we are
1213 * just going to fail anyway. Make sure to pullup the header so
1214 * that other people can't step on it.
1216 rte
= (struct rtdetq
*) _MALLOC((sizeof *rte
), M_MRTABLE
, M_NOWAIT
);
1221 mb0
= m_copy(m
, 0, M_COPYALL
);
1222 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1223 mb0
= m_pullup(mb0
, hlen
);
1225 FREE(rte
, M_MRTABLE
);
1230 /* is there an upcall waiting for this packet? */
1231 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1232 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1233 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1234 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1235 (rt
->mfc_stall
!= NULL
))
1243 /* no upcall, so make a new entry */
1244 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
1246 FREE(rte
, M_MRTABLE
);
1251 /* Make a copy of the header to send to the user level process */
1252 mm
= m_copy(mb0
, 0, hlen
);
1254 FREE(rte
, M_MRTABLE
);
1256 FREE(rt
, M_MRTABLE
);
1262 * Send message to routing daemon to install
1263 * a route into the kernel table
1265 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1267 im
= mtod(mm
, struct igmpmsg
*);
1268 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1271 mrtstat
.mrts_upcalls
++;
1273 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1274 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1275 ++mrtstat
.mrts_upq_sockfull
;
1276 FREE(rte
, M_MRTABLE
);
1278 FREE(rt
, M_MRTABLE
);
1283 /* insert new entry at head of hash chain */
1284 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1285 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1286 rt
->mfc_expire
= UPCALL_EXPIRE
;
1288 for (i
= 0; i
< numvifs
; i
++)
1289 rt
->mfc_ttls
[i
] = 0;
1290 rt
->mfc_parent
= -1;
1292 /* link into table */
1293 rt
->mfc_next
= mfctable
[hash
];
1294 mfctable
[hash
] = rt
;
1295 rt
->mfc_stall
= rte
;
1298 /* determine if q has overflowed */
1302 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1305 if (npkts
> MAX_UPQ
) {
1306 mrtstat
.mrts_upq_ovflw
++;
1307 FREE(rte
, M_MRTABLE
);
1313 /* Add this entry to the end of the queue */
1330 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1331 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
1332 struct ip_moptions
*) = X_ip_mforward
;
1336 * Clean up the cache entry if upcall is not serviced
1339 expire_upcalls(void *unused
)
1342 struct mfc
*mfc
, **nptr
;
1348 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
1349 if (nexpire
[i
] == 0)
1351 nptr
= &mfctable
[i
];
1352 for (mfc
= *nptr
; mfc
!= NULL
; mfc
= *nptr
) {
1354 * Skip real cache entries
1355 * Make sure it wasn't marked to not expire (shouldn't happen)
1358 if (mfc
->mfc_stall
!= NULL
&&
1359 mfc
->mfc_expire
!= 0 &&
1360 --mfc
->mfc_expire
== 0) {
1361 if (mrtdebug
& DEBUG_EXPIRE
)
1362 log(LOG_DEBUG
, "expire_upcalls: expiring (%lx %lx)\n",
1363 (u_long
)ntohl(mfc
->mfc_origin
.s_addr
),
1364 (u_long
)ntohl(mfc
->mfc_mcastgrp
.s_addr
));
1366 * drop all the packets
1367 * free the mbuf with the pkt, if, timing info
1369 for (rte
= mfc
->mfc_stall
; rte
; ) {
1370 struct rtdetq
*n
= rte
->next
;
1373 FREE(rte
, M_MRTABLE
);
1376 ++mrtstat
.mrts_cache_cleanups
;
1379 *nptr
= mfc
->mfc_next
;
1380 FREE(mfc
, M_MRTABLE
);
1382 nptr
= &mfc
->mfc_next
;
1387 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1391 * Packet forwarding routine once entry in the cache is made
1394 ip_mdq(m
, ifp
, rt
, xmt_vif
)
1395 register struct mbuf
*m
;
1396 register struct ifnet
*ifp
;
1397 register struct mfc
*rt
;
1398 register vifi_t xmt_vif
;
1400 register struct ip
*ip
= mtod(m
, struct ip
*);
1401 register vifi_t vifi
;
1402 register struct vif
*vifp
;
1403 register int plen
= ip
->ip_len
;
1406 * Macro to send packet on vif. Since RSVP packets don't get counted on
1407 * input, they shouldn't get counted on output, so statistics keeping is
1410 #define MC_SEND(ip,vifp,m) { \
1411 if ((vifp)->v_flags & VIFF_TUNNEL) \
1412 encap_send((ip), (vifp), (m)); \
1414 phyint_send((ip), (vifp), (m)); \
1418 * If xmt_vif is not -1, send on only the requested vif.
1420 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1422 if (xmt_vif
< numvifs
) {
1423 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1428 * Don't forward if it didn't arrive from the parent vif for its origin.
1430 vifi
= rt
->mfc_parent
;
1431 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1432 /* came in the wrong interface */
1433 if (mrtdebug
& DEBUG_FORWARD
)
1434 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1435 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1436 ++mrtstat
.mrts_wrong_if
;
1439 * If we are doing PIM assert processing, and we are forwarding
1440 * packets on this interface, and it is a broadcast medium
1441 * interface (and not a tunnel), send a message to the routing daemon.
1443 if (pim_assert
&& rt
->mfc_ttls
[vifi
] &&
1444 (ifp
->if_flags
& IFF_BROADCAST
) &&
1445 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
1446 struct sockaddr_in k_igmpsrc
;
1449 int hlen
= ip
->ip_hl
<< 2;
1451 register u_long delta
;
1455 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1457 if (delta
> ASSERT_MSG_TIME
) {
1458 mm
= m_copy(m
, 0, hlen
);
1459 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1460 mm
= m_pullup(mm
, hlen
);
1465 rt
->mfc_last_assert
= now
;
1467 im
= mtod(mm
, struct igmpmsg
*);
1468 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1472 k_igmpsrc
.sin_addr
= im
->im_src
;
1474 socket_send(ip_mrouter
, mm
, &k_igmpsrc
);
1480 /* If I sourced this packet, it counts as output, else it was input. */
1481 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1482 viftable
[vifi
].v_pkt_out
++;
1483 viftable
[vifi
].v_bytes_out
+= plen
;
1485 viftable
[vifi
].v_pkt_in
++;
1486 viftable
[vifi
].v_bytes_in
+= plen
;
1489 rt
->mfc_byte_cnt
+= plen
;
1492 * For each vif, decide if a copy of the packet should be forwarded.
1494 * - the ttl exceeds the vif's threshold
1495 * - there are group members downstream on interface
1497 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1498 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1499 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1501 vifp
->v_bytes_out
+= plen
;
1502 MC_SEND(ip
, vifp
, m
);
1509 * check if a vif number is legal/ok. This is used by ip_output, to export
1513 X_legal_vif_num(vif
)
1516 if (vif
>= 0 && vif
< numvifs
)
1522 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1523 int (*legal_vif_num
)(int) = X_legal_vif_num
;
1527 * Return the local address used by this vif
1530 X_ip_mcast_src(vifi
)
1533 if (vifi
>= 0 && vifi
< numvifs
)
1534 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1539 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1540 u_long (*ip_mcast_src
)(int) = X_ip_mcast_src
;
1544 phyint_send(ip
, vifp
, m
)
1549 register struct mbuf
*mb_copy
;
1550 register int hlen
= ip
->ip_hl
<< 2;
1553 * Make a new reference to the packet; make sure that
1554 * the IP header is actually copied, not just referenced,
1555 * so that ip_output() only scribbles on the copy.
1557 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1558 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1559 mb_copy
= m_pullup(mb_copy
, hlen
);
1560 if (mb_copy
== NULL
)
1563 if (vifp
->v_rate_limit
== 0)
1564 tbf_send_packet(vifp
, mb_copy
);
1566 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1570 encap_send(ip
, vifp
, m
)
1571 register struct ip
*ip
;
1572 register struct vif
*vifp
;
1573 register struct mbuf
*m
;
1575 register struct mbuf
*mb_copy
;
1576 register struct ip
*ip_copy
;
1577 register int i
, len
= ip
->ip_len
;
1580 * copy the old packet & pullup its IP header into the
1581 * new mbuf so we can modify it. Try to fill the new
1582 * mbuf since if we don't the ethernet driver will.
1584 MGETHDR(mb_copy
, M_DONTWAIT
, MT_HEADER
);
1585 if (mb_copy
== NULL
)
1587 mb_copy
->m_data
+= max_linkhdr
;
1588 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1590 if ((mb_copy
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1594 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1597 mb_copy
= m_pullup(mb_copy
, i
);
1598 if (mb_copy
== NULL
)
1600 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1603 * fill in the encapsulating IP header.
1605 ip_copy
= mtod(mb_copy
, struct ip
*);
1606 *ip_copy
= multicast_encap_iphdr
;
1608 ip_copy
->ip_id
= ip_randomid();
1610 ip_copy
->ip_id
= htons(ip_id
++);
1612 ip_copy
->ip_len
+= len
;
1613 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1614 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1617 * turn the encapsulated IP header back into a valid one.
1619 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1624 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1625 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1626 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1628 if (vifp
->v_rate_limit
== 0)
1629 tbf_send_packet(vifp
, mb_copy
);
1631 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1635 * De-encapsulate a packet and feed it back through ip input (this
1636 * routine is called whenever IP gets a packet with proto type
1637 * ENCAP_PROTO and a local destination address).
1641 X_ipip_input(m
, iphlen
)
1643 ipip_input(m
, iphlen
)
1645 register struct mbuf
*m
;
1648 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1649 register struct ip
*ip
= mtod(m
, struct ip
*);
1650 register int hlen
= ip
->ip_hl
<< 2;
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
;
1702 proto_inject(PF_INET
, m
);
1706 * Token bucket filter module
1710 tbf_control(vifp
, m
, ip
, p_len
)
1711 register struct vif
*vifp
;
1712 register struct mbuf
*m
;
1713 register struct ip
*ip
;
1714 register u_long p_len
;
1716 register struct tbf
*t
= vifp
->v_tbf
;
1718 if (p_len
> MAX_BKT_SIZE
) {
1719 /* drop if packet is too large */
1720 mrtstat
.mrts_pkt2large
++;
1725 tbf_update_tokens(vifp
);
1727 /* if there are enough tokens,
1728 * and the queue is empty,
1729 * send this packet out
1732 if (t
->tbf_q_len
== 0) {
1733 /* queue empty, send packet if enough tokens */
1734 if (p_len
<= t
->tbf_n_tok
) {
1735 t
->tbf_n_tok
-= p_len
;
1736 tbf_send_packet(vifp
, m
);
1738 /* queue packet and timeout till later */
1740 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1742 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1743 /* finite queue length, so queue pkts and process queue */
1745 tbf_process_q(vifp
);
1747 /* queue length too much, try to dq and queue and process */
1748 if (!tbf_dq_sel(vifp
, ip
)) {
1749 mrtstat
.mrts_q_overflow
++;
1754 tbf_process_q(vifp
);
1761 * adds a packet to the queue at the interface
1765 register struct vif
*vifp
;
1766 register struct mbuf
*m
;
1768 register int s
= splnet();
1769 register struct tbf
*t
= vifp
->v_tbf
;
1771 if (t
->tbf_t
== NULL
) {
1772 /* Queue was empty */
1775 /* Insert at tail */
1776 t
->tbf_t
->m_act
= m
;
1779 /* Set new tail pointer */
1783 /* Make sure we didn't get fed a bogus mbuf */
1785 panic("tbf_queue: m_act");
1796 * processes the queue at the interface
1800 register struct vif
*vifp
;
1802 register struct mbuf
*m
;
1804 register int s
= splnet();
1805 register struct tbf
*t
= vifp
->v_tbf
;
1807 /* loop through the queue at the interface and send as many packets
1810 while (t
->tbf_q_len
> 0) {
1813 len
= mtod(m
, struct ip
*)->ip_len
;
1815 /* determine if the packet can be sent */
1816 if (len
<= t
->tbf_n_tok
) {
1818 * reduce no of tokens, dequeue the packet,
1821 t
->tbf_n_tok
-= len
;
1823 t
->tbf_q
= m
->m_act
;
1824 if (--t
->tbf_q_len
== 0)
1828 tbf_send_packet(vifp
, m
);
1836 tbf_reprocess_q(xvifp
)
1839 register struct vif
*vifp
= xvifp
;
1841 if (ip_mrouter
== NULL
) {
1845 tbf_update_tokens(vifp
);
1847 tbf_process_q(vifp
);
1849 if (vifp
->v_tbf
->tbf_q_len
)
1850 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1853 /* function that will selectively discard a member of the queue
1854 * based on the precedence value and the priority
1857 tbf_dq_sel(vifp
, ip
)
1858 register struct vif
*vifp
;
1859 register struct ip
*ip
;
1861 register int s
= splnet();
1863 register struct mbuf
*m
, *last
;
1864 register struct mbuf
**np
;
1865 register struct tbf
*t
= vifp
->v_tbf
;
1867 p
= priority(vifp
, ip
);
1871 while ((m
= *np
) != NULL
) {
1872 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1874 /* If we're removing the last packet, fix the tail pointer */
1878 /* it's impossible for the queue to be empty, but
1879 * we check anyway. */
1880 if (--t
->tbf_q_len
== 0)
1883 mrtstat
.mrts_drop_sel
++;
1894 tbf_send_packet(vifp
, m
)
1895 register struct vif
*vifp
;
1896 register struct mbuf
*m
;
1898 struct ip_moptions imo
;
1900 static struct route ro
;
1903 if (vifp
->v_flags
& VIFF_TUNNEL
) {
1904 /* If tunnel options */
1905 ip_output(m
, (struct mbuf
*)0, &vifp
->v_route
,
1906 IP_FORWARDING
, (struct ip_moptions
*)0);
1908 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1909 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1910 imo
.imo_multicast_loop
= 1;
1911 imo
.imo_multicast_vif
= -1;
1914 * Re-entrancy should not be a problem here, because
1915 * the packets that we send out and are looped back at us
1916 * should get rejected because they appear to come from
1917 * the loopback interface, thus preventing looping.
1919 error
= ip_output(m
, (struct mbuf
*)0, &ro
,
1920 IP_FORWARDING
, &imo
);
1922 if (mrtdebug
& DEBUG_XMIT
)
1923 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1924 vifp
- viftable
, error
);
1929 /* determine the current time and then
1930 * the elapsed time (between the last time and time now)
1931 * in milliseconds & update the no. of tokens in the bucket
1934 tbf_update_tokens(vifp
)
1935 register struct vif
*vifp
;
1939 register int s
= splnet();
1940 register struct tbf
*t
= vifp
->v_tbf
;
1944 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1947 * This formula is actually
1948 * "time in seconds" * "bytes/second".
1950 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1952 * The (1000/1024) was introduced in add_vif to optimize
1953 * this divide into a shift.
1955 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1956 t
->tbf_last_pkt_t
= tp
;
1958 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1959 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1966 register struct vif
*vifp
;
1967 register struct ip
*ip
;
1971 /* temporary hack; may add general packet classifier some day */
1974 * The UDP port space is divided up into four priority ranges:
1975 * [0, 16384) : unclassified - lowest priority
1976 * [16384, 32768) : audio - highest priority
1977 * [32768, 49152) : whiteboard - medium priority
1978 * [49152, 65536) : video - low priority
1980 if (ip
->ip_p
== IPPROTO_UDP
) {
1981 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
1982 switch (ntohs(udp
->uh_dport
) & 0xc000) {
1997 log(LOG_DEBUG
, "port %x prio%d\n", ntohs(udp
->uh_dport
), prio
);
2005 * End of token bucket filter modifications
2009 ip_rsvp_vif_init(so
, sopt
)
2011 struct sockopt
*sopt
;
2016 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2017 so
->so_type
, so
->so_proto
->pr_protocol
);
2019 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2023 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2028 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i
, rsvp_on
);
2033 if (!legal_vif_num(i
)) {
2035 return EADDRNOTAVAIL
;
2038 /* Check if socket is available. */
2039 if (viftable
[i
].v_rsvpd
!= NULL
) {
2044 viftable
[i
].v_rsvpd
= so
;
2045 /* This may seem silly, but we need to be sure we don't over-increment
2046 * the RSVP counter, in case something slips up.
2048 if (!viftable
[i
].v_rsvp_on
) {
2049 viftable
[i
].v_rsvp_on
= 1;
2058 ip_rsvp_vif_done(so
, sopt
)
2060 struct sockopt
*sopt
;
2065 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2066 so
->so_type
, so
->so_proto
->pr_protocol
);
2068 if (so
->so_type
!= SOCK_RAW
||
2069 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2072 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2079 if (!legal_vif_num(i
)) {
2081 return EADDRNOTAVAIL
;
2085 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2086 viftable
[i
].v_rsvpd
, so
);
2088 viftable
[i
].v_rsvpd
= NULL
;
2090 * This may seem silly, but we need to be sure we don't over-decrement
2091 * the RSVP counter, in case something slips up.
2093 if (viftable
[i
].v_rsvp_on
) {
2094 viftable
[i
].v_rsvp_on
= 0;
2103 ip_rsvp_force_done(so
)
2109 /* Don't bother if it is not the right type of socket. */
2110 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2115 /* The socket may be attached to more than one vif...this
2116 * is perfectly legal.
2118 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2119 if (viftable
[vifi
].v_rsvpd
== so
) {
2120 viftable
[vifi
].v_rsvpd
= NULL
;
2121 /* This may seem silly, but we need to be sure we don't
2122 * over-decrement the RSVP counter, in case something slips up.
2124 if (viftable
[vifi
].v_rsvp_on
) {
2125 viftable
[vifi
].v_rsvp_on
= 0;
2136 rsvp_input(m
, iphlen
)
2141 register struct ip
*ip
= mtod(m
, struct ip
*);
2142 static struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
};
2147 printf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2149 /* Can still get packets with rsvp_on = 0 if there is a local member
2150 * of the group to which the RSVP packet is addressed. But in this
2151 * case we want to throw the packet away.
2161 printf("rsvp_input: check vifs\n");
2164 if (!(m
->m_flags
& M_PKTHDR
))
2165 panic("rsvp_input no hdr");
2168 ifp
= m
->m_pkthdr
.rcvif
;
2169 /* Find which vif the packet arrived on. */
2170 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2171 if (viftable
[vifi
].v_ifp
== ifp
)
2174 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2176 * If the old-style non-vif-associated socket is set,
2177 * then use it. Otherwise, drop packet since there
2178 * is no specific socket for this vif.
2180 if (ip_rsvpd
!= NULL
) {
2182 printf("rsvp_input: Sending packet up old-style socket\n");
2183 rip_input(m
, iphlen
); /* xxx */
2185 if (rsvpdebug
&& vifi
== numvifs
)
2186 printf("rsvp_input: Can't find vif for packet.\n");
2187 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2188 printf("rsvp_input: No socket defined for vif %d\n",vifi
);
2194 rsvp_src
.sin_addr
= ip
->ip_src
;
2197 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2198 m
->m_len
,sbspace(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2200 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2202 printf("rsvp_input: Failed to append to socket\n");
2205 printf("rsvp_input: send packet up\n");
2212 #include <sys/conf.h>
2213 #include <sys/exec.h>
2214 #include <sys/sysent.h>
2215 #include <sys/lkm.h>
2217 MOD_MISC("ip_mroute_mod")
2220 ip_mroute_mod_handle(struct lkm_table
*lkmtp
, int cmd
)
2223 struct lkm_misc
*args
= lkmtp
->private.lkm_misc
;
2227 static int (*old_ip_mrouter_cmd
)();
2228 static int (*old_ip_mrouter_done
)();
2229 static int (*old_ip_mforward
)();
2230 static int (*old_mrt_ioctl
)();
2231 static void (*old_proto4_input
)();
2232 static int (*old_legal_vif_num
)();
2233 extern struct protosw inetsw
[];
2236 if(lkmexists(lkmtp
) || ip_mrtproto
)
2238 old_ip_mrouter_cmd
= ip_mrouter_cmd
;
2239 ip_mrouter_cmd
= X_ip_mrouter_cmd
;
2240 old_ip_mrouter_done
= ip_mrouter_done
;
2241 ip_mrouter_done
= X_ip_mrouter_done
;
2242 old_ip_mforward
= ip_mforward
;
2243 ip_mforward
= X_ip_mforward
;
2244 old_mrt_ioctl
= mrt_ioctl
;
2245 mrt_ioctl
= X_mrt_ioctl
;
2246 old_proto4_input
= ip_protox
[ENCAP_PROTO
]->pr_input
;
2247 ip_protox
[ENCAP_PROTO
]->pr_input
= X_ipip_input
;
2248 old_legal_vif_num
= legal_vif_num
;
2249 legal_vif_num
= X_legal_vif_num
;
2250 ip_mrtproto
= IGMP_DVMRP
;
2252 printf("\nIP multicast routing loaded\n");
2259 ip_mrouter_cmd
= old_ip_mrouter_cmd
;
2260 ip_mrouter_done
= old_ip_mrouter_done
;
2261 ip_mforward
= old_ip_mforward
;
2262 mrt_ioctl
= old_mrt_ioctl
;
2263 ip_protox
[ENCAP_PROTO
]->pr_input
= old_proto4_input
;
2264 legal_vif_num
= old_legal_vif_num
;
2277 ip_mroute_mod(struct lkm_table
*lkmtp
, int cmd
, int ver
) {
2278 DISPATCH(lkmtp
, cmd
, ver
, ip_mroute_mod_handle
, ip_mroute_mod_handle
,
2282 #endif /* MROUTE_LKM */
2283 #endif /* MROUTING */