]>
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_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * IP multicast forwarding procedures
31 * Written by David Waitzman, BBN Labs, August 1988.
32 * Modified by Steve Deering, Stanford, February 1989.
33 * Modified by Mark J. Steiglitz, Stanford, May, 1991
34 * Modified by Van Jacobson, LBL, January 1993
35 * Modified by Ajit Thyagarajan, PARC, August 1993
36 * Modified by Bill Fenner, PARC, April 1995
38 * MROUTING Revision: 3.5
39 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/malloc.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/protosw.h>
51 #include <sys/kernel.h>
52 #include <sys/sockio.h>
53 #include <sys/syslog.h>
55 #include <net/route.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/in_var.h>
61 #include <netinet/igmp.h>
62 #include <netinet/ip_mroute.h>
63 #include <netinet/udp.h>
66 #if BYTE_ORDER != BIG_ENDIAN
67 #define NTOHL(d) ((d) = ntohl((d)))
68 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
69 #define HTONL(d) ((d) = htonl((d)))
70 #define HTONS(d) ((d) = htons((u_short)(d)))
80 extern u_long
_ip_mcast_src(int vifi
);
81 extern int _ip_mforward(struct ip
*ip
, struct ifnet
*ifp
,
82 struct mbuf
*m
, struct ip_moptions
*imo
);
83 extern int _ip_mrouter_done(void);
84 extern int _ip_mrouter_get(struct socket
*so
, struct sockopt
*sopt
);
85 extern int _ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
);
86 extern int _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
);
89 * Dummy routines and globals used when multicast routing is not compiled in.
92 struct socket
*ip_mrouter
= NULL
;
96 _ip_mrouter_set(so
, sopt
)
103 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = _ip_mrouter_set
;
107 _ip_mrouter_get(so
, sopt
)
109 struct sockopt
*sopt
;
114 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = _ip_mrouter_get
;
122 int (*ip_mrouter_done
)(void) = _ip_mrouter_done
;
125 _ip_mforward(ip
, ifp
, m
, imo
)
129 struct ip_moptions
*imo
;
134 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
135 struct ip_moptions
*) = _ip_mforward
;
138 _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
)
143 int (*mrt_ioctl
)(int, caddr_t
, struct proc
*) = _mrt_ioctl
;
146 rsvp_input(m
, iphlen
) /* XXX must fixup manually */
150 /* Can still get packets with rsvp_on = 0 if there is a local member
151 * of the group to which the RSVP packet is addressed. But in this
152 * case we want to throw the packet away.
159 if (ip_rsvpd
!= NULL
) {
161 printf("rsvp_input: Sending packet up old-style socket\n");
162 rip_input(m
, iphlen
);
165 /* Drop the packet */
169 void ipip_input(struct mbuf
*m
, int iphlen
) { /* XXX must fixup manually */
170 rip_input(m
, iphlen
);
173 int (*legal_vif_num
)(int) = 0;
176 * This should never be called, since IP_MULTICAST_VIF should fail, but
177 * just in case it does get called, the code a little lower in ip_output
178 * will assign the packet a local address.
181 _ip_mcast_src(int vifi
) { return INADDR_ANY
; }
182 u_long (*ip_mcast_src
)(int) = _ip_mcast_src
;
185 ip_rsvp_vif_init(so
, sopt
)
187 struct sockopt
*sopt
;
193 ip_rsvp_vif_done(so
, sopt
)
195 struct sockopt
*sopt
;
201 ip_rsvp_force_done(so
)
209 #define M_HASCL(m) ((m)->m_flags & M_EXT)
211 #define INSIZ sizeof(struct in_addr)
212 #define same(a1, a2) \
213 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
217 * Globals. All but ip_mrouter and ip_mrtproto could be static,
218 * except for netstat or debugging purposes.
221 struct socket
*ip_mrouter
= NULL
;
222 static struct mrtstat mrtstat
;
223 #else /* MROUTE_LKM */
224 extern void X_ipip_input(struct mbuf
*m
, int iphlen
);
225 extern struct mrtstat mrtstat
;
226 static int ip_mrtproto
;
229 #define NO_RTE_FOUND 0x1
230 #define RTE_FOUND 0x2
232 static struct mfc
*mfctable
[MFCTBLSIZ
];
233 static u_char nexpire
[MFCTBLSIZ
];
234 static struct vif viftable
[MAXVIFS
];
235 static u_int mrtdebug
= 0; /* debug level */
236 #define DEBUG_MFC 0x02
237 #define DEBUG_FORWARD 0x04
238 #define DEBUG_EXPIRE 0x08
239 #define DEBUG_XMIT 0x10
240 static u_int tbfdebug
= 0; /* tbf debug level */
241 static u_int rsvpdebug
= 0; /* rsvp debug level */
243 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
244 #define UPCALL_EXPIRE 6 /* number of timeouts */
247 * Define the token bucket filter structures
248 * tbftable -> each vif has one of these for storing info
251 static struct tbf tbftable
[MAXVIFS
];
252 #define TBF_REPROCESS (hz / 100) /* 100x / second */
255 * 'Interfaces' associated with decapsulator (so we can tell
256 * packets that went through it from ones that get reflected
257 * by a broken gateway). These interfaces are never linked into
258 * the system ifnet list & no routes point to them. I.e., packets
259 * can't be sent this way. They only exist as a placeholder for
260 * multicast source verification.
262 static struct ifnet multicast_decap_if
[MAXVIFS
];
265 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
267 /* prototype IP hdr for encapsulated packets */
268 static struct ip multicast_encap_iphdr
= {
269 #if BYTE_ORDER == LITTLE_ENDIAN
270 sizeof(struct ip
) >> 2, IPVERSION
,
272 IPVERSION
, sizeof(struct ip
) >> 2,
275 sizeof(struct ip
), /* total length */
278 ENCAP_TTL
, ENCAP_PROTO
,
285 static vifi_t numvifs
= 0;
286 static int have_encap_tunnel
= 0;
289 * one-back cache used by ipip_input to locate a tunnel's vif
290 * given a datagram's src ip address.
292 static u_long last_encap_src
;
293 static struct vif
*last_encap_vif
;
295 static u_long
X_ip_mcast_src(int vifi
);
296 static int X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
, struct ip_moptions
*imo
);
297 static int X_ip_mrouter_done(void);
298 static int X_ip_mrouter_get(struct socket
*so
, struct sockopt
*m
);
299 static int X_ip_mrouter_set(struct socket
*so
, struct sockopt
*m
);
300 static int X_legal_vif_num(int vif
);
301 static int X_mrt_ioctl(int cmd
, caddr_t data
);
303 static int get_sg_cnt(struct sioc_sg_req
*);
304 static int get_vif_cnt(struct sioc_vif_req
*);
305 static int ip_mrouter_init(struct socket
*, int);
306 static int add_vif(struct vifctl
*);
307 static int del_vif(vifi_t
);
308 static int add_mfc(struct mfcctl
*);
309 static int del_mfc(struct mfcctl
*);
310 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
311 static int set_assert(int);
312 static void expire_upcalls(void *);
313 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*,
315 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
316 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
317 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
318 static void tbf_queue(struct vif
*, struct mbuf
*);
319 static void tbf_process_q(struct vif
*);
320 static void tbf_reprocess_q(void *);
321 static int tbf_dq_sel(struct vif
*, struct ip
*);
322 static void tbf_send_packet(struct vif
*, struct mbuf
*);
323 static void tbf_update_tokens(struct vif
*);
324 static int priority(struct vif
*, struct ip
*);
325 void multiencap_decap(struct mbuf
*);
328 * whether or not special PIM assert processing is enabled.
330 static int pim_assert
;
332 * Rate limit for assert notification messages, in usec
334 #define ASSERT_MSG_TIME 3000000
337 * Hash function for a source, group entry
339 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
340 ((g) >> 20) ^ ((g) >> 10) ^ (g))
343 * Find a route for a given origin IP address and Multicast group address
344 * Type of service parameter to be added in the future!!!
347 #define MFCFIND(o, g, rt) { \
348 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
350 ++mrtstat.mrts_mfc_lookups; \
352 if ((_rt->mfc_origin.s_addr == o) && \
353 (_rt->mfc_mcastgrp.s_addr == g) && \
354 (_rt->mfc_stall == NULL)) { \
358 _rt = _rt->mfc_next; \
361 ++mrtstat.mrts_mfc_misses; \
367 * Macros to compute elapsed time efficiently
368 * Borrowed from Van Jacobson's scheduling code
370 #define TV_DELTA(a, b, delta) { \
373 delta = (a).tv_usec - (b).tv_usec; \
374 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
383 delta += (1000000 * xxs); \
388 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
389 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
392 u_long upcall_data
[51];
393 static void collate(struct timeval
*);
394 #endif /* UPCALL_TIMING */
398 * Handle MRT setsockopt commands to modify the multicast routing tables.
401 X_ip_mrouter_set(so
, sopt
)
403 struct sockopt
*sopt
;
410 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
414 switch (sopt
->sopt_name
) {
416 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
420 error
= ip_mrouter_init(so
, optval
);
424 error
= ip_mrouter_done();
428 error
= sooptcopyin(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
431 error
= add_vif(&vifc
);
435 error
= sooptcopyin(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
438 error
= del_vif(vifi
);
443 error
= sooptcopyin(sopt
, &mfc
, sizeof mfc
, sizeof mfc
);
446 if (sopt
->sopt_name
== MRT_ADD_MFC
)
447 error
= add_mfc(&mfc
);
449 error
= del_mfc(&mfc
);
453 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
467 #if !defined(MROUTE_LKM) || !MROUTE_LKM
468 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_set
;
472 * Handle MRT getsockopt commands
475 X_ip_mrouter_get(so
, sopt
)
477 struct sockopt
*sopt
;
480 static int version
= 0x0305; /* !!! why is this here? XXX */
482 switch (sopt
->sopt_name
) {
484 error
= sooptcopyout(sopt
, &version
, sizeof version
);
488 error
= sooptcopyout(sopt
, &pim_assert
, sizeof pim_assert
);
497 #if !defined(MROUTE_LKM) || !MROUTE_LKM
498 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_get
;
502 * Handle ioctl commands to obtain information from the cache
505 X_mrt_ioctl(cmd
, data
)
512 case (SIOCGETVIFCNT
):
513 return (get_vif_cnt((struct sioc_vif_req
*)data
));
516 return (get_sg_cnt((struct sioc_sg_req
*)data
));
525 #if !defined(MROUTE_LKM) || !MROUTE_LKM
526 int (*mrt_ioctl
)(int, caddr_t
) = X_mrt_ioctl
;
530 * returns the packet, byte, rpf-failure count for the source group provided
534 register struct sioc_sg_req
*req
;
536 register struct mfc
*rt
;
540 MFCFIND(req
->src
.s_addr
, req
->grp
.s_addr
, rt
);
543 req
->pktcnt
= rt
->mfc_pkt_cnt
;
544 req
->bytecnt
= rt
->mfc_byte_cnt
;
545 req
->wrong_if
= rt
->mfc_wrong_if
;
547 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
553 * returns the input and output packet and byte counts on the vif provided
557 register struct sioc_vif_req
*req
;
559 register vifi_t vifi
= req
->vifi
;
561 if (vifi
>= numvifs
) return EINVAL
;
563 req
->icount
= viftable
[vifi
].v_pkt_in
;
564 req
->ocount
= viftable
[vifi
].v_pkt_out
;
565 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
566 req
->obytes
= viftable
[vifi
].v_bytes_out
;
572 * Enable multicast routing
575 ip_mrouter_init(so
, version
)
580 log(LOG_DEBUG
,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
581 so
->so_type
, so
->so_proto
->pr_protocol
);
583 if (so
->so_type
!= SOCK_RAW
||
584 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
) return EOPNOTSUPP
;
589 if (ip_mrouter
!= NULL
) return EADDRINUSE
;
593 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
594 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
598 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
601 log(LOG_DEBUG
, "ip_mrouter_init\n");
607 * Disable multicast routing
623 * For each phyint in use, disable promiscuous reception of all IP
626 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
627 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
628 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
629 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
630 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
632 ifp
= viftable
[vifi
].v_ifp
;
636 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
637 bzero((caddr_t
)viftable
, sizeof(viftable
));
641 untimeout(expire_upcalls
, (caddr_t
)NULL
);
644 * Free all multicast forwarding cache entries.
646 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
647 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
648 struct mfc
*nr
= rt
->mfc_next
;
650 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
651 struct rtdetq
*n
= rte
->next
;
654 FREE(rte
, M_MRTABLE
);
662 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
665 * Reset de-encapsulation cache
668 last_encap_vif
= NULL
;
669 have_encap_tunnel
= 0;
676 log(LOG_DEBUG
, "ip_mrouter_done\n");
681 #if !defined(MROUTE_LKM) || !MROUTE_LKM
682 int (*ip_mrouter_done
)(void) = X_ip_mrouter_done
;
686 * Set PIM assert processing global
692 if ((i
!= 1) && (i
!= 0))
701 * Add a vif to the vif table
705 register struct vifctl
*vifcp
;
707 register struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
708 static struct sockaddr_in sin
= {sizeof sin
, AF_INET
};
712 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
714 if (vifcp
->vifc_vifi
>= MAXVIFS
) return EINVAL
;
715 if (vifp
->v_lcl_addr
.s_addr
!= 0) return EADDRINUSE
;
717 /* Find the interface with an address in AF_INET family */
718 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
719 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
720 if (ifa
== 0) return EADDRNOTAVAIL
;
725 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
726 if ((vifcp
->vifc_flags
& VIFF_SRCRT
) == 0) {
728 * An encapsulating tunnel is wanted. Tell ipip_input() to
729 * start paying attention to encapsulated packets.
731 if (have_encap_tunnel
== 0) {
732 have_encap_tunnel
= 1;
733 for (s
= 0; s
< MAXVIFS
; ++s
) {
734 multicast_decap_if
[s
].if_name
= "mdecap";
735 multicast_decap_if
[s
].if_unit
= s
;
736 multicast_decap_if
[s
].if_family
= APPLE_IF_FAM_MDECAP
;
740 * Set interface to fake encapsulator interface
742 ifp
= &multicast_decap_if
[vifcp
->vifc_vifi
];
744 * Prepare cached route entry
746 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
748 log(LOG_ERR
, "source routed tunnels not supported\n");
752 /* Make sure the interface supports multicast */
753 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
756 /* Enable promiscuous reception of all IP multicasts from the if */
758 error
= if_allmulti(ifp
, 1);
765 /* define parameters for the tbf structure */
767 GET_TIME(vifp
->v_tbf
->tbf_last_pkt_t
);
768 vifp
->v_tbf
->tbf_n_tok
= 0;
769 vifp
->v_tbf
->tbf_q_len
= 0;
770 vifp
->v_tbf
->tbf_max_q_len
= MAXQSIZE
;
771 vifp
->v_tbf
->tbf_q
= vifp
->v_tbf
->tbf_t
= NULL
;
773 vifp
->v_flags
= vifcp
->vifc_flags
;
774 vifp
->v_threshold
= vifcp
->vifc_threshold
;
775 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
776 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
778 /* scaling up here allows division by 1024 in critical code */
779 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
781 vifp
->v_rsvpd
= NULL
;
782 /* initialize per vif pkt counters */
785 vifp
->v_bytes_in
= 0;
786 vifp
->v_bytes_out
= 0;
789 /* Adjust numvifs up if the vifi is higher than numvifs */
790 if (numvifs
<= vifcp
->vifc_vifi
) numvifs
= vifcp
->vifc_vifi
+ 1;
793 log(LOG_DEBUG
, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
795 (u_long
)ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
796 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
797 (u_long
)ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
798 vifcp
->vifc_threshold
,
799 vifcp
->vifc_rate_limit
);
805 * Delete a vif from the vif table
811 register struct vif
*vifp
= &viftable
[vifi
];
812 register struct mbuf
*m
;
817 if (vifi
>= numvifs
) return EINVAL
;
818 if (vifp
->v_lcl_addr
.s_addr
== 0) return EADDRNOTAVAIL
;
822 if (!(vifp
->v_flags
& VIFF_TUNNEL
)) {
823 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
824 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
= INADDR_ANY
;
829 if (vifp
== last_encap_vif
) {
835 * Free packets queued at the interface
837 while (vifp
->v_tbf
->tbf_q
) {
838 m
= vifp
->v_tbf
->tbf_q
;
839 vifp
->v_tbf
->tbf_q
= m
->m_act
;
843 bzero((caddr_t
)vifp
->v_tbf
, sizeof(*(vifp
->v_tbf
)));
844 bzero((caddr_t
)vifp
, sizeof (*vifp
));
847 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", vifi
, numvifs
);
849 /* Adjust numvifs down */
850 for (vifi
= numvifs
; vifi
> 0; vifi
--)
851 if (viftable
[vifi
-1].v_lcl_addr
.s_addr
!= 0) break;
864 struct mfcctl
*mfccp
;
869 register u_short nstl
;
873 MFCFIND(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
, rt
);
875 /* If an entry already exists, just update the fields */
877 if (mrtdebug
& DEBUG_MFC
)
878 log(LOG_DEBUG
,"add_mfc update o %lx g %lx p %x\n",
879 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
880 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
884 rt
->mfc_parent
= mfccp
->mfcc_parent
;
885 for (i
= 0; i
< numvifs
; i
++)
886 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
892 * Find the entry for which the upcall was made and update
895 hash
= MFCHASH(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
896 for (rt
= mfctable
[hash
], nstl
= 0; rt
; rt
= rt
->mfc_next
) {
898 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
899 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
) &&
900 (rt
->mfc_stall
!= NULL
)) {
903 log(LOG_ERR
, "add_mfc %s o %lx g %lx p %x dbx %p\n",
904 "multiple kernel entries",
905 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
906 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
907 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
909 if (mrtdebug
& DEBUG_MFC
)
910 log(LOG_DEBUG
,"add_mfc o %lx g %lx p %x dbg %p\n",
911 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
912 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
913 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
915 rt
->mfc_origin
= mfccp
->mfcc_origin
;
916 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
917 rt
->mfc_parent
= mfccp
->mfcc_parent
;
918 for (i
= 0; i
< numvifs
; i
++)
919 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
920 /* initialize pkt counters per src-grp */
922 rt
->mfc_byte_cnt
= 0;
923 rt
->mfc_wrong_if
= 0;
924 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
926 rt
->mfc_expire
= 0; /* Don't clean this guy up */
929 /* free packets Qed at the end of this entry */
930 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
931 struct rtdetq
*n
= rte
->next
;
933 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
937 #endif /* UPCALL_TIMING */
938 FREE(rte
, M_MRTABLE
);
941 rt
->mfc_stall
= NULL
;
946 * It is possible that an entry is being inserted without an upcall
949 if (mrtdebug
& DEBUG_MFC
)
950 log(LOG_DEBUG
,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
951 hash
, (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
952 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
955 for (rt
= mfctable
[hash
]; rt
!= NULL
; rt
= rt
->mfc_next
) {
957 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
958 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
)) {
960 rt
->mfc_origin
= mfccp
->mfcc_origin
;
961 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
962 rt
->mfc_parent
= mfccp
->mfcc_parent
;
963 for (i
= 0; i
< numvifs
; i
++)
964 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
965 /* initialize pkt counters per src-grp */
967 rt
->mfc_byte_cnt
= 0;
968 rt
->mfc_wrong_if
= 0;
969 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
976 /* no upcall, so make a new entry */
977 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
983 /* insert new entry at head of hash chain */
984 rt
->mfc_origin
= mfccp
->mfcc_origin
;
985 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
986 rt
->mfc_parent
= mfccp
->mfcc_parent
;
987 for (i
= 0; i
< numvifs
; i
++)
988 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
989 /* initialize pkt counters per src-grp */
991 rt
->mfc_byte_cnt
= 0;
992 rt
->mfc_wrong_if
= 0;
993 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
995 rt
->mfc_stall
= NULL
;
997 /* link into table */
998 rt
->mfc_next
= mfctable
[hash
];
1008 * collect delay statistics on the upcalls
1010 static void collate(t
)
1011 register struct timeval
*t
;
1014 register struct timeval tp
;
1015 register u_long delta
;
1021 TV_DELTA(tp
, *t
, delta
);
1030 #endif /* UPCALL_TIMING */
1033 * Delete an mfc entry
1037 struct mfcctl
*mfccp
;
1039 struct in_addr origin
;
1040 struct in_addr mcastgrp
;
1046 origin
= mfccp
->mfcc_origin
;
1047 mcastgrp
= mfccp
->mfcc_mcastgrp
;
1048 hash
= MFCHASH(origin
.s_addr
, mcastgrp
.s_addr
);
1050 if (mrtdebug
& DEBUG_MFC
)
1051 log(LOG_DEBUG
,"del_mfc orig %lx mcastgrp %lx\n",
1052 (u_long
)ntohl(origin
.s_addr
), (u_long
)ntohl(mcastgrp
.s_addr
));
1056 nptr
= &mfctable
[hash
];
1057 while ((rt
= *nptr
) != NULL
) {
1058 if (origin
.s_addr
== rt
->mfc_origin
.s_addr
&&
1059 mcastgrp
.s_addr
== rt
->mfc_mcastgrp
.s_addr
&&
1060 rt
->mfc_stall
== NULL
)
1063 nptr
= &rt
->mfc_next
;
1067 return EADDRNOTAVAIL
;
1070 *nptr
= rt
->mfc_next
;
1071 FREE(rt
, M_MRTABLE
);
1079 * Send a message to mrouted on the multicast routing socket
1082 socket_send(s
, mm
, src
)
1085 struct sockaddr_in
*src
;
1089 if (sbappendaddr(&s
->so_rcv
,
1090 (struct sockaddr
*)src
,
1091 mm
, (struct mbuf
*)0, NULL
) != 0) {
1093 socket_unlock(s
, 1);
1097 socket_unlock(s
, 1);
1103 * IP multicast forwarding function. This function assumes that the packet
1104 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1105 * pointed to by "ifp", and the packet is to be relayed to other networks
1106 * that have members of the packet's destination IP multicast group.
1108 * The packet is returned unscathed to the caller, unless it is
1109 * erroneous, in which case a non-zero return value tells the caller to
1113 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1114 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1117 X_ip_mforward(ip
, ifp
, m
, imo
)
1118 register struct ip
*ip
;
1121 struct ip_moptions
*imo
;
1123 register struct mfc
*rt
;
1124 register u_char
*ipoptions
;
1125 static struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1126 static int srctun
= 0;
1127 register struct mbuf
*mm
;
1132 if (mrtdebug
& DEBUG_FORWARD
)
1133 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1134 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1137 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1138 (ipoptions
= (u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1140 * Packet arrived via a physical interface or
1141 * an encapsulated tunnel.
1145 * Packet arrived through a source-route tunnel.
1146 * Source-route tunnels are no longer supported.
1148 if ((srctun
++ % 1000) == 0)
1150 "ip_mforward: received source-routed packet from %lx\n",
1151 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1156 if ((imo
) && ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1157 if (ip
->ip_ttl
< 255)
1158 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1159 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1160 vifp
= viftable
+ vifi
;
1161 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1162 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), vifi
,
1163 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1164 vifp
->v_ifp
->if_name
, vifp
->v_ifp
->if_unit
);
1166 return (ip_mdq(m
, ifp
, NULL
, vifi
));
1168 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1169 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1170 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
));
1172 printf("In fact, no options were specified at all\n");
1176 * Don't forward a packet with time-to-live of zero or one,
1177 * or a packet destined to a local-only group.
1179 if (ip
->ip_ttl
<= 1 ||
1180 ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1184 * Determine forwarding vifs from the forwarding cache table
1187 MFCFIND(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, rt
);
1189 /* Entry exists, so forward if necessary */
1192 return (ip_mdq(m
, ifp
, rt
, -1));
1195 * If we don't have a route for packet's origin,
1196 * Make a copy of the packet &
1197 * send message to routing daemon
1200 register struct mbuf
*mb0
;
1201 register struct rtdetq
*rte
;
1202 register u_long hash
;
1203 int hlen
= ip
->ip_hl
<< 2;
1210 mrtstat
.mrts_no_route
++;
1211 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1212 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1213 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1214 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1217 * Allocate mbufs early so that we don't do extra work if we are
1218 * just going to fail anyway. Make sure to pullup the header so
1219 * that other people can't step on it.
1221 rte
= (struct rtdetq
*) _MALLOC((sizeof *rte
), M_MRTABLE
, M_NOWAIT
);
1226 mb0
= m_copy(m
, 0, M_COPYALL
);
1227 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1228 mb0
= m_pullup(mb0
, hlen
);
1230 FREE(rte
, M_MRTABLE
);
1235 /* is there an upcall waiting for this packet? */
1236 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1237 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1238 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1239 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1240 (rt
->mfc_stall
!= NULL
))
1248 /* no upcall, so make a new entry */
1249 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
1251 FREE(rte
, M_MRTABLE
);
1256 /* Make a copy of the header to send to the user level process */
1257 mm
= m_copy(mb0
, 0, hlen
);
1259 FREE(rte
, M_MRTABLE
);
1261 FREE(rt
, M_MRTABLE
);
1267 * Send message to routing daemon to install
1268 * a route into the kernel table
1270 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1272 im
= mtod(mm
, struct igmpmsg
*);
1273 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1276 mrtstat
.mrts_upcalls
++;
1278 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1279 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1280 ++mrtstat
.mrts_upq_sockfull
;
1281 FREE(rte
, M_MRTABLE
);
1283 FREE(rt
, M_MRTABLE
);
1288 /* insert new entry at head of hash chain */
1289 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1290 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1291 rt
->mfc_expire
= UPCALL_EXPIRE
;
1293 for (i
= 0; i
< numvifs
; i
++)
1294 rt
->mfc_ttls
[i
] = 0;
1295 rt
->mfc_parent
= -1;
1297 /* link into table */
1298 rt
->mfc_next
= mfctable
[hash
];
1299 mfctable
[hash
] = rt
;
1300 rt
->mfc_stall
= rte
;
1303 /* determine if q has overflowed */
1307 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1310 if (npkts
> MAX_UPQ
) {
1311 mrtstat
.mrts_upq_ovflw
++;
1312 FREE(rte
, M_MRTABLE
);
1318 /* Add this entry to the end of the queue */
1335 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1336 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
1337 struct ip_moptions
*) = X_ip_mforward
;
1341 * Clean up the cache entry if upcall is not serviced
1344 expire_upcalls(void *unused
)
1347 struct mfc
*mfc
, **nptr
;
1353 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
1354 if (nexpire
[i
] == 0)
1356 nptr
= &mfctable
[i
];
1357 for (mfc
= *nptr
; mfc
!= NULL
; mfc
= *nptr
) {
1359 * Skip real cache entries
1360 * Make sure it wasn't marked to not expire (shouldn't happen)
1363 if (mfc
->mfc_stall
!= NULL
&&
1364 mfc
->mfc_expire
!= 0 &&
1365 --mfc
->mfc_expire
== 0) {
1366 if (mrtdebug
& DEBUG_EXPIRE
)
1367 log(LOG_DEBUG
, "expire_upcalls: expiring (%lx %lx)\n",
1368 (u_long
)ntohl(mfc
->mfc_origin
.s_addr
),
1369 (u_long
)ntohl(mfc
->mfc_mcastgrp
.s_addr
));
1371 * drop all the packets
1372 * free the mbuf with the pkt, if, timing info
1374 for (rte
= mfc
->mfc_stall
; rte
; ) {
1375 struct rtdetq
*n
= rte
->next
;
1378 FREE(rte
, M_MRTABLE
);
1381 ++mrtstat
.mrts_cache_cleanups
;
1384 *nptr
= mfc
->mfc_next
;
1385 FREE(mfc
, M_MRTABLE
);
1387 nptr
= &mfc
->mfc_next
;
1392 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1396 * Packet forwarding routine once entry in the cache is made
1399 ip_mdq(m
, ifp
, rt
, xmt_vif
)
1400 register struct mbuf
*m
;
1401 register struct ifnet
*ifp
;
1402 register struct mfc
*rt
;
1403 register vifi_t xmt_vif
;
1405 register struct ip
*ip
= mtod(m
, struct ip
*);
1406 register vifi_t vifi
;
1407 register struct vif
*vifp
;
1408 register int plen
= ip
->ip_len
;
1411 * Macro to send packet on vif. Since RSVP packets don't get counted on
1412 * input, they shouldn't get counted on output, so statistics keeping is
1415 #define MC_SEND(ip,vifp,m) { \
1416 if ((vifp)->v_flags & VIFF_TUNNEL) \
1417 encap_send((ip), (vifp), (m)); \
1419 phyint_send((ip), (vifp), (m)); \
1423 * If xmt_vif is not -1, send on only the requested vif.
1425 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1427 if (xmt_vif
< numvifs
) {
1428 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1433 * Don't forward if it didn't arrive from the parent vif for its origin.
1435 vifi
= rt
->mfc_parent
;
1436 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1437 /* came in the wrong interface */
1438 if (mrtdebug
& DEBUG_FORWARD
)
1439 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1440 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1441 ++mrtstat
.mrts_wrong_if
;
1444 * If we are doing PIM assert processing, and we are forwarding
1445 * packets on this interface, and it is a broadcast medium
1446 * interface (and not a tunnel), send a message to the routing daemon.
1448 if (pim_assert
&& rt
->mfc_ttls
[vifi
] &&
1449 (ifp
->if_flags
& IFF_BROADCAST
) &&
1450 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
1451 struct sockaddr_in k_igmpsrc
;
1454 int hlen
= ip
->ip_hl
<< 2;
1456 register u_long delta
;
1460 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1462 if (delta
> ASSERT_MSG_TIME
) {
1463 mm
= m_copy(m
, 0, hlen
);
1464 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1465 mm
= m_pullup(mm
, hlen
);
1470 rt
->mfc_last_assert
= now
;
1472 im
= mtod(mm
, struct igmpmsg
*);
1473 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1477 k_igmpsrc
.sin_addr
= im
->im_src
;
1479 socket_send(ip_mrouter
, mm
, &k_igmpsrc
);
1485 /* If I sourced this packet, it counts as output, else it was input. */
1486 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1487 viftable
[vifi
].v_pkt_out
++;
1488 viftable
[vifi
].v_bytes_out
+= plen
;
1490 viftable
[vifi
].v_pkt_in
++;
1491 viftable
[vifi
].v_bytes_in
+= plen
;
1494 rt
->mfc_byte_cnt
+= plen
;
1497 * For each vif, decide if a copy of the packet should be forwarded.
1499 * - the ttl exceeds the vif's threshold
1500 * - there are group members downstream on interface
1502 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1503 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1504 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1506 vifp
->v_bytes_out
+= plen
;
1507 MC_SEND(ip
, vifp
, m
);
1514 * check if a vif number is legal/ok. This is used by ip_output, to export
1518 X_legal_vif_num(vif
)
1521 if (vif
>= 0 && vif
< numvifs
)
1527 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1528 int (*legal_vif_num
)(int) = X_legal_vif_num
;
1532 * Return the local address used by this vif
1535 X_ip_mcast_src(vifi
)
1538 if (vifi
>= 0 && vifi
< numvifs
)
1539 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1544 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1545 u_long (*ip_mcast_src
)(int) = X_ip_mcast_src
;
1549 phyint_send(ip
, vifp
, m
)
1554 register struct mbuf
*mb_copy
;
1555 register int hlen
= ip
->ip_hl
<< 2;
1558 * Make a new reference to the packet; make sure that
1559 * the IP header is actually copied, not just referenced,
1560 * so that ip_output() only scribbles on the copy.
1562 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1563 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1564 mb_copy
= m_pullup(mb_copy
, hlen
);
1565 if (mb_copy
== NULL
)
1568 if (vifp
->v_rate_limit
== 0)
1569 tbf_send_packet(vifp
, mb_copy
);
1571 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1575 encap_send(ip
, vifp
, m
)
1576 register struct ip
*ip
;
1577 register struct vif
*vifp
;
1578 register struct mbuf
*m
;
1580 register struct mbuf
*mb_copy
;
1581 register struct ip
*ip_copy
;
1582 register int i
, len
= ip
->ip_len
;
1585 * copy the old packet & pullup its IP header into the
1586 * new mbuf so we can modify it. Try to fill the new
1587 * mbuf since if we don't the ethernet driver will.
1589 MGETHDR(mb_copy
, M_DONTWAIT
, MT_HEADER
);
1590 if (mb_copy
== NULL
)
1592 mb_copy
->m_data
+= max_linkhdr
;
1593 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1595 if ((mb_copy
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1599 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1602 mb_copy
= m_pullup(mb_copy
, i
);
1603 if (mb_copy
== NULL
)
1605 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1608 * fill in the encapsulating IP header.
1610 ip_copy
= mtod(mb_copy
, struct ip
*);
1611 *ip_copy
= multicast_encap_iphdr
;
1613 ip_copy
->ip_id
= ip_randomid();
1615 ip_copy
->ip_id
= htons(ip_id
++);
1617 ip_copy
->ip_len
+= len
;
1618 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1619 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1622 * turn the encapsulated IP header back into a valid one.
1624 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1629 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1630 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1631 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1633 if (vifp
->v_rate_limit
== 0)
1634 tbf_send_packet(vifp
, mb_copy
);
1636 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1640 * De-encapsulate a packet and feed it back through ip input (this
1641 * routine is called whenever IP gets a packet with proto type
1642 * ENCAP_PROTO and a local destination address).
1646 X_ipip_input(m
, iphlen
)
1648 ipip_input(m
, iphlen
)
1650 register struct mbuf
*m
;
1653 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1654 register struct ip
*ip
= mtod(m
, struct ip
*);
1655 register int hlen
= ip
->ip_hl
<< 2;
1656 register struct vif
*vifp
;
1658 if (!have_encap_tunnel
) {
1659 rip_input(m
, iphlen
);
1663 * dump the packet if it's not to a multicast destination or if
1664 * we don't have an encapsulating tunnel with the source.
1665 * Note: This code assumes that the remote site IP address
1666 * uniquely identifies the tunnel (i.e., that this site has
1667 * at most one tunnel with the remote site).
1669 if (! IN_MULTICAST(ntohl(((struct ip
*)((char *)ip
+ hlen
))->ip_dst
.s_addr
))) {
1670 ++mrtstat
.mrts_bad_tunnel
;
1674 if (ip
->ip_src
.s_addr
!= last_encap_src
) {
1675 register struct vif
*vife
;
1678 vife
= vifp
+ numvifs
;
1679 last_encap_src
= ip
->ip_src
.s_addr
;
1681 for ( ; vifp
< vife
; ++vifp
)
1682 if (vifp
->v_rmt_addr
.s_addr
== ip
->ip_src
.s_addr
) {
1683 if ((vifp
->v_flags
& (VIFF_TUNNEL
|VIFF_SRCRT
))
1685 last_encap_vif
= vifp
;
1689 if ((vifp
= last_encap_vif
) == 0) {
1691 mrtstat
.mrts_cant_tunnel
++; /*XXX*/
1694 log(LOG_DEBUG
, "ip_mforward: no tunnel with %lx\n",
1695 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1700 if (hlen
> IP_HDR_LEN
)
1701 ip_stripoptions(m
, (struct mbuf
*) 0);
1702 m
->m_data
+= IP_HDR_LEN
;
1703 m
->m_len
-= IP_HDR_LEN
;
1704 m
->m_pkthdr
.len
-= IP_HDR_LEN
;
1705 m
->m_pkthdr
.rcvif
= ifp
;
1707 proto_inject(PF_INET
, m
);
1711 * Token bucket filter module
1715 tbf_control(vifp
, m
, ip
, p_len
)
1716 register struct vif
*vifp
;
1717 register struct mbuf
*m
;
1718 register struct ip
*ip
;
1719 register u_long p_len
;
1721 register struct tbf
*t
= vifp
->v_tbf
;
1723 if (p_len
> MAX_BKT_SIZE
) {
1724 /* drop if packet is too large */
1725 mrtstat
.mrts_pkt2large
++;
1730 tbf_update_tokens(vifp
);
1732 /* if there are enough tokens,
1733 * and the queue is empty,
1734 * send this packet out
1737 if (t
->tbf_q_len
== 0) {
1738 /* queue empty, send packet if enough tokens */
1739 if (p_len
<= t
->tbf_n_tok
) {
1740 t
->tbf_n_tok
-= p_len
;
1741 tbf_send_packet(vifp
, m
);
1743 /* queue packet and timeout till later */
1745 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1747 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1748 /* finite queue length, so queue pkts and process queue */
1750 tbf_process_q(vifp
);
1752 /* queue length too much, try to dq and queue and process */
1753 if (!tbf_dq_sel(vifp
, ip
)) {
1754 mrtstat
.mrts_q_overflow
++;
1759 tbf_process_q(vifp
);
1766 * adds a packet to the queue at the interface
1770 register struct vif
*vifp
;
1771 register struct mbuf
*m
;
1773 register int s
= splnet();
1774 register struct tbf
*t
= vifp
->v_tbf
;
1776 if (t
->tbf_t
== NULL
) {
1777 /* Queue was empty */
1780 /* Insert at tail */
1781 t
->tbf_t
->m_act
= m
;
1784 /* Set new tail pointer */
1788 /* Make sure we didn't get fed a bogus mbuf */
1790 panic("tbf_queue: m_act");
1801 * processes the queue at the interface
1805 register struct vif
*vifp
;
1807 register struct mbuf
*m
;
1809 register int s
= splnet();
1810 register struct tbf
*t
= vifp
->v_tbf
;
1812 /* loop through the queue at the interface and send as many packets
1815 while (t
->tbf_q_len
> 0) {
1818 len
= mtod(m
, struct ip
*)->ip_len
;
1820 /* determine if the packet can be sent */
1821 if (len
<= t
->tbf_n_tok
) {
1823 * reduce no of tokens, dequeue the packet,
1826 t
->tbf_n_tok
-= len
;
1828 t
->tbf_q
= m
->m_act
;
1829 if (--t
->tbf_q_len
== 0)
1833 tbf_send_packet(vifp
, m
);
1841 tbf_reprocess_q(xvifp
)
1844 register struct vif
*vifp
= xvifp
;
1846 if (ip_mrouter
== NULL
) {
1850 tbf_update_tokens(vifp
);
1852 tbf_process_q(vifp
);
1854 if (vifp
->v_tbf
->tbf_q_len
)
1855 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1858 /* function that will selectively discard a member of the queue
1859 * based on the precedence value and the priority
1862 tbf_dq_sel(vifp
, ip
)
1863 register struct vif
*vifp
;
1864 register struct ip
*ip
;
1866 register int s
= splnet();
1868 register struct mbuf
*m
, *last
;
1869 register struct mbuf
**np
;
1870 register struct tbf
*t
= vifp
->v_tbf
;
1872 p
= priority(vifp
, ip
);
1876 while ((m
= *np
) != NULL
) {
1877 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1879 /* If we're removing the last packet, fix the tail pointer */
1883 /* it's impossible for the queue to be empty, but
1884 * we check anyway. */
1885 if (--t
->tbf_q_len
== 0)
1888 mrtstat
.mrts_drop_sel
++;
1899 tbf_send_packet(vifp
, m
)
1900 register struct vif
*vifp
;
1901 register struct mbuf
*m
;
1903 struct ip_moptions imo
;
1905 static struct route ro
;
1908 if (vifp
->v_flags
& VIFF_TUNNEL
) {
1909 /* If tunnel options */
1910 ip_output(m
, (struct mbuf
*)0, &vifp
->v_route
,
1911 IP_FORWARDING
, (struct ip_moptions
*)0);
1913 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1914 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1915 imo
.imo_multicast_loop
= 1;
1916 imo
.imo_multicast_vif
= -1;
1919 * Re-entrancy should not be a problem here, because
1920 * the packets that we send out and are looped back at us
1921 * should get rejected because they appear to come from
1922 * the loopback interface, thus preventing looping.
1924 error
= ip_output(m
, (struct mbuf
*)0, &ro
,
1925 IP_FORWARDING
, &imo
);
1927 if (mrtdebug
& DEBUG_XMIT
)
1928 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1929 vifp
- viftable
, error
);
1934 /* determine the current time and then
1935 * the elapsed time (between the last time and time now)
1936 * in milliseconds & update the no. of tokens in the bucket
1939 tbf_update_tokens(vifp
)
1940 register struct vif
*vifp
;
1944 register int s
= splnet();
1945 register struct tbf
*t
= vifp
->v_tbf
;
1949 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1952 * This formula is actually
1953 * "time in seconds" * "bytes/second".
1955 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1957 * The (1000/1024) was introduced in add_vif to optimize
1958 * this divide into a shift.
1960 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1961 t
->tbf_last_pkt_t
= tp
;
1963 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1964 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1971 register struct vif
*vifp
;
1972 register struct ip
*ip
;
1976 /* temporary hack; may add general packet classifier some day */
1979 * The UDP port space is divided up into four priority ranges:
1980 * [0, 16384) : unclassified - lowest priority
1981 * [16384, 32768) : audio - highest priority
1982 * [32768, 49152) : whiteboard - medium priority
1983 * [49152, 65536) : video - low priority
1985 if (ip
->ip_p
== IPPROTO_UDP
) {
1986 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
1987 switch (ntohs(udp
->uh_dport
) & 0xc000) {
2002 log(LOG_DEBUG
, "port %x prio%d\n", ntohs(udp
->uh_dport
), prio
);
2010 * End of token bucket filter modifications
2014 ip_rsvp_vif_init(so
, sopt
)
2016 struct sockopt
*sopt
;
2021 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2022 so
->so_type
, so
->so_proto
->pr_protocol
);
2024 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2028 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2033 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i
, rsvp_on
);
2038 if (!legal_vif_num(i
)) {
2040 return EADDRNOTAVAIL
;
2043 /* Check if socket is available. */
2044 if (viftable
[i
].v_rsvpd
!= NULL
) {
2049 viftable
[i
].v_rsvpd
= so
;
2050 /* This may seem silly, but we need to be sure we don't over-increment
2051 * the RSVP counter, in case something slips up.
2053 if (!viftable
[i
].v_rsvp_on
) {
2054 viftable
[i
].v_rsvp_on
= 1;
2063 ip_rsvp_vif_done(so
, sopt
)
2065 struct sockopt
*sopt
;
2070 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2071 so
->so_type
, so
->so_proto
->pr_protocol
);
2073 if (so
->so_type
!= SOCK_RAW
||
2074 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2077 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2084 if (!legal_vif_num(i
)) {
2086 return EADDRNOTAVAIL
;
2090 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2091 viftable
[i
].v_rsvpd
, so
);
2093 viftable
[i
].v_rsvpd
= NULL
;
2095 * This may seem silly, but we need to be sure we don't over-decrement
2096 * the RSVP counter, in case something slips up.
2098 if (viftable
[i
].v_rsvp_on
) {
2099 viftable
[i
].v_rsvp_on
= 0;
2108 ip_rsvp_force_done(so
)
2114 /* Don't bother if it is not the right type of socket. */
2115 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2120 /* The socket may be attached to more than one vif...this
2121 * is perfectly legal.
2123 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2124 if (viftable
[vifi
].v_rsvpd
== so
) {
2125 viftable
[vifi
].v_rsvpd
= NULL
;
2126 /* This may seem silly, but we need to be sure we don't
2127 * over-decrement the RSVP counter, in case something slips up.
2129 if (viftable
[vifi
].v_rsvp_on
) {
2130 viftable
[vifi
].v_rsvp_on
= 0;
2141 rsvp_input(m
, iphlen
)
2146 register struct ip
*ip
= mtod(m
, struct ip
*);
2147 static struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
};
2152 printf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2154 /* Can still get packets with rsvp_on = 0 if there is a local member
2155 * of the group to which the RSVP packet is addressed. But in this
2156 * case we want to throw the packet away.
2166 printf("rsvp_input: check vifs\n");
2169 if (!(m
->m_flags
& M_PKTHDR
))
2170 panic("rsvp_input no hdr");
2173 ifp
= m
->m_pkthdr
.rcvif
;
2174 /* Find which vif the packet arrived on. */
2175 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2176 if (viftable
[vifi
].v_ifp
== ifp
)
2179 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2181 * If the old-style non-vif-associated socket is set,
2182 * then use it. Otherwise, drop packet since there
2183 * is no specific socket for this vif.
2185 if (ip_rsvpd
!= NULL
) {
2187 printf("rsvp_input: Sending packet up old-style socket\n");
2188 rip_input(m
, iphlen
); /* xxx */
2190 if (rsvpdebug
&& vifi
== numvifs
)
2191 printf("rsvp_input: Can't find vif for packet.\n");
2192 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2193 printf("rsvp_input: No socket defined for vif %d\n",vifi
);
2199 rsvp_src
.sin_addr
= ip
->ip_src
;
2202 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2203 m
->m_len
,sbspace(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2205 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2207 printf("rsvp_input: Failed to append to socket\n");
2210 printf("rsvp_input: send packet up\n");
2217 #include <sys/conf.h>
2218 #include <sys/exec.h>
2219 #include <sys/sysent.h>
2220 #include <sys/lkm.h>
2222 MOD_MISC("ip_mroute_mod")
2225 ip_mroute_mod_handle(struct lkm_table
*lkmtp
, int cmd
)
2228 struct lkm_misc
*args
= lkmtp
->private.lkm_misc
;
2232 static int (*old_ip_mrouter_cmd
)();
2233 static int (*old_ip_mrouter_done
)();
2234 static int (*old_ip_mforward
)();
2235 static int (*old_mrt_ioctl
)();
2236 static void (*old_proto4_input
)();
2237 static int (*old_legal_vif_num
)();
2238 extern struct protosw inetsw
[];
2241 if(lkmexists(lkmtp
) || ip_mrtproto
)
2243 old_ip_mrouter_cmd
= ip_mrouter_cmd
;
2244 ip_mrouter_cmd
= X_ip_mrouter_cmd
;
2245 old_ip_mrouter_done
= ip_mrouter_done
;
2246 ip_mrouter_done
= X_ip_mrouter_done
;
2247 old_ip_mforward
= ip_mforward
;
2248 ip_mforward
= X_ip_mforward
;
2249 old_mrt_ioctl
= mrt_ioctl
;
2250 mrt_ioctl
= X_mrt_ioctl
;
2251 old_proto4_input
= ip_protox
[ENCAP_PROTO
]->pr_input
;
2252 ip_protox
[ENCAP_PROTO
]->pr_input
= X_ipip_input
;
2253 old_legal_vif_num
= legal_vif_num
;
2254 legal_vif_num
= X_legal_vif_num
;
2255 ip_mrtproto
= IGMP_DVMRP
;
2257 printf("\nIP multicast routing loaded\n");
2264 ip_mrouter_cmd
= old_ip_mrouter_cmd
;
2265 ip_mrouter_done
= old_ip_mrouter_done
;
2266 ip_mforward
= old_ip_mforward
;
2267 mrt_ioctl
= old_mrt_ioctl
;
2268 ip_protox
[ENCAP_PROTO
]->pr_input
= old_proto4_input
;
2269 legal_vif_num
= old_legal_vif_num
;
2282 ip_mroute_mod(struct lkm_table
*lkmtp
, int cmd
, int ver
) {
2283 DISPATCH(lkmtp
, cmd
, ver
, ip_mroute_mod_handle
, ip_mroute_mod_handle
,
2287 #endif /* MROUTE_LKM */
2288 #endif /* MROUTING */