]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_mroute.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * IP multicast forwarding procedures
28 * Written by David Waitzman, BBN Labs, August 1988.
29 * Modified by Steve Deering, Stanford, February 1989.
30 * Modified by Mark J. Steiglitz, Stanford, May, 1991
31 * Modified by Van Jacobson, LBL, January 1993
32 * Modified by Ajit Thyagarajan, PARC, August 1993
33 * Modified by Bill Fenner, PARC, April 1995
35 * MROUTING Revision: 3.5
36 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/protosw.h>
48 #include <sys/kernel.h>
49 #include <sys/sockio.h>
50 #include <sys/syslog.h>
52 #include <net/route.h>
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/in_var.h>
58 #include <netinet/igmp.h>
59 #include <netinet/ip_mroute.h>
60 #include <netinet/udp.h>
63 #if BYTE_ORDER != BIG_ENDIAN
64 #define NTOHL(d) ((d) = ntohl((d)))
65 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
66 #define HTONL(d) ((d) = htonl((d)))
67 #define HTONS(d) ((d) = htons((u_short)(d)))
77 extern u_long _ip_mcast_src
__P((int vifi
));
78 extern int _ip_mforward
__P((struct ip
*ip
, struct ifnet
*ifp
,
79 struct mbuf
*m
, struct ip_moptions
*imo
));
80 extern int _ip_mrouter_done
__P((void));
81 extern int _ip_mrouter_get
__P((struct socket
*so
, struct sockopt
*sopt
));
82 extern int _ip_mrouter_set
__P((struct socket
*so
, struct sockopt
*sopt
));
83 extern int _mrt_ioctl
__P((int req
, caddr_t data
, struct proc
*p
));
86 * Dummy routines and globals used when multicast routing is not compiled in.
89 struct socket
*ip_mrouter
= NULL
;
93 _ip_mrouter_set(so
, sopt
)
100 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = _ip_mrouter_set
;
104 _ip_mrouter_get(so
, sopt
)
106 struct sockopt
*sopt
;
111 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = _ip_mrouter_get
;
119 int (*ip_mrouter_done
)(void) = _ip_mrouter_done
;
122 _ip_mforward(ip
, ifp
, m
, imo
)
126 struct ip_moptions
*imo
;
131 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
132 struct ip_moptions
*) = _ip_mforward
;
135 _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
)
140 int (*mrt_ioctl
)(int, caddr_t
, struct proc
*) = _mrt_ioctl
;
143 rsvp_input(m
, iphlen
) /* XXX must fixup manually */
147 /* Can still get packets with rsvp_on = 0 if there is a local member
148 * of the group to which the RSVP packet is addressed. But in this
149 * case we want to throw the packet away.
156 if (ip_rsvpd
!= NULL
) {
158 printf("rsvp_input: Sending packet up old-style socket\n");
159 rip_input(m
, iphlen
);
162 /* Drop the packet */
166 void ipip_input(struct mbuf
*m
, int iphlen
) { /* XXX must fixup manually */
167 rip_input(m
, iphlen
);
170 int (*legal_vif_num
)(int) = 0;
173 * This should never be called, since IP_MULTICAST_VIF should fail, but
174 * just in case it does get called, the code a little lower in ip_output
175 * will assign the packet a local address.
178 _ip_mcast_src(int vifi
) { return INADDR_ANY
; }
179 u_long (*ip_mcast_src
)(int) = _ip_mcast_src
;
182 ip_rsvp_vif_init(so
, sopt
)
184 struct sockopt
*sopt
;
190 ip_rsvp_vif_done(so
, sopt
)
192 struct sockopt
*sopt
;
198 ip_rsvp_force_done(so
)
206 #define M_HASCL(m) ((m)->m_flags & M_EXT)
208 #define INSIZ sizeof(struct in_addr)
209 #define same(a1, a2) \
210 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
214 * Globals. All but ip_mrouter and ip_mrtproto could be static,
215 * except for netstat or debugging purposes.
218 struct socket
*ip_mrouter
= NULL
;
219 static struct mrtstat mrtstat
;
220 #else /* MROUTE_LKM */
221 extern void X_ipip_input
__P((struct mbuf
*m
, int iphlen
));
222 extern struct mrtstat mrtstat
;
223 static int ip_mrtproto
;
226 #define NO_RTE_FOUND 0x1
227 #define RTE_FOUND 0x2
229 static struct mfc
*mfctable
[MFCTBLSIZ
];
230 static u_char nexpire
[MFCTBLSIZ
];
231 static struct vif viftable
[MAXVIFS
];
232 static u_int mrtdebug
= 0; /* debug level */
233 #define DEBUG_MFC 0x02
234 #define DEBUG_FORWARD 0x04
235 #define DEBUG_EXPIRE 0x08
236 #define DEBUG_XMIT 0x10
237 static u_int tbfdebug
= 0; /* tbf debug level */
238 static u_int rsvpdebug
= 0; /* rsvp debug level */
240 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
241 #define UPCALL_EXPIRE 6 /* number of timeouts */
244 * Define the token bucket filter structures
245 * tbftable -> each vif has one of these for storing info
248 static struct tbf tbftable
[MAXVIFS
];
249 #define TBF_REPROCESS (hz / 100) /* 100x / second */
252 * 'Interfaces' associated with decapsulator (so we can tell
253 * packets that went through it from ones that get reflected
254 * by a broken gateway). These interfaces are never linked into
255 * the system ifnet list & no routes point to them. I.e., packets
256 * can't be sent this way. They only exist as a placeholder for
257 * multicast source verification.
259 static struct ifnet multicast_decap_if
[MAXVIFS
];
262 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
264 /* prototype IP hdr for encapsulated packets */
265 static struct ip multicast_encap_iphdr
= {
266 #if BYTE_ORDER == LITTLE_ENDIAN
267 sizeof(struct ip
) >> 2, IPVERSION
,
269 IPVERSION
, sizeof(struct ip
) >> 2,
272 sizeof(struct ip
), /* total length */
275 ENCAP_TTL
, ENCAP_PROTO
,
282 static vifi_t numvifs
= 0;
283 static int have_encap_tunnel
= 0;
286 * one-back cache used by ipip_input to locate a tunnel's vif
287 * given a datagram's src ip address.
289 static u_long last_encap_src
;
290 static struct vif
*last_encap_vif
;
292 static u_long X_ip_mcast_src
__P((int vifi
));
293 static int X_ip_mforward
__P((struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
, struct ip_moptions
*imo
));
294 static int X_ip_mrouter_done
__P((void));
295 static int X_ip_mrouter_get
__P((struct socket
*so
, struct sockopt
*m
));
296 static int X_ip_mrouter_set
__P((struct socket
*so
, struct sockopt
*m
));
297 static int X_legal_vif_num
__P((int vif
));
298 static int X_mrt_ioctl
__P((int cmd
, caddr_t data
));
300 static int get_sg_cnt(struct sioc_sg_req
*);
301 static int get_vif_cnt(struct sioc_vif_req
*);
302 static int ip_mrouter_init(struct socket
*, int);
303 static int add_vif(struct vifctl
*);
304 static int del_vif(vifi_t
);
305 static int add_mfc(struct mfcctl
*);
306 static int del_mfc(struct mfcctl
*);
307 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
308 static int set_assert(int);
309 static void expire_upcalls(void *);
310 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*,
312 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
313 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
314 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
315 static void tbf_queue(struct vif
*, struct mbuf
*);
316 static void tbf_process_q(struct vif
*);
317 static void tbf_reprocess_q(void *);
318 static int tbf_dq_sel(struct vif
*, struct ip
*);
319 static void tbf_send_packet(struct vif
*, struct mbuf
*);
320 static void tbf_update_tokens(struct vif
*);
321 static int priority(struct vif
*, struct ip
*);
322 void multiencap_decap(struct mbuf
*);
325 * whether or not special PIM assert processing is enabled.
327 static int pim_assert
;
329 * Rate limit for assert notification messages, in usec
331 #define ASSERT_MSG_TIME 3000000
334 * Hash function for a source, group entry
336 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
337 ((g) >> 20) ^ ((g) >> 10) ^ (g))
340 * Find a route for a given origin IP address and Multicast group address
341 * Type of service parameter to be added in the future!!!
344 #define MFCFIND(o, g, rt) { \
345 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
347 ++mrtstat.mrts_mfc_lookups; \
349 if ((_rt->mfc_origin.s_addr == o) && \
350 (_rt->mfc_mcastgrp.s_addr == g) && \
351 (_rt->mfc_stall == NULL)) { \
355 _rt = _rt->mfc_next; \
358 ++mrtstat.mrts_mfc_misses; \
364 * Macros to compute elapsed time efficiently
365 * Borrowed from Van Jacobson's scheduling code
367 #define TV_DELTA(a, b, delta) { \
370 delta = (a).tv_usec - (b).tv_usec; \
371 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
380 delta += (1000000 * xxs); \
385 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
386 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
389 u_long upcall_data
[51];
390 static void collate(struct timeval
*);
391 #endif /* UPCALL_TIMING */
395 * Handle MRT setsockopt commands to modify the multicast routing tables.
398 X_ip_mrouter_set(so
, sopt
)
400 struct sockopt
*sopt
;
407 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
411 switch (sopt
->sopt_name
) {
413 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
417 error
= ip_mrouter_init(so
, optval
);
421 error
= ip_mrouter_done();
425 error
= sooptcopyin(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
428 error
= add_vif(&vifc
);
432 error
= sooptcopyin(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
435 error
= del_vif(vifi
);
440 error
= sooptcopyin(sopt
, &mfc
, sizeof mfc
, sizeof mfc
);
443 if (sopt
->sopt_name
== MRT_ADD_MFC
)
444 error
= add_mfc(&mfc
);
446 error
= del_mfc(&mfc
);
450 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
464 #if !defined(MROUTE_LKM) || !MROUTE_LKM
465 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_set
;
469 * Handle MRT getsockopt commands
472 X_ip_mrouter_get(so
, sopt
)
474 struct sockopt
*sopt
;
477 static int version
= 0x0305; /* !!! why is this here? XXX */
479 switch (sopt
->sopt_name
) {
481 error
= sooptcopyout(sopt
, &version
, sizeof version
);
485 error
= sooptcopyout(sopt
, &pim_assert
, sizeof pim_assert
);
494 #if !defined(MROUTE_LKM) || !MROUTE_LKM
495 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_get
;
499 * Handle ioctl commands to obtain information from the cache
502 X_mrt_ioctl(cmd
, data
)
509 case (SIOCGETVIFCNT
):
510 return (get_vif_cnt((struct sioc_vif_req
*)data
));
513 return (get_sg_cnt((struct sioc_sg_req
*)data
));
522 #if !defined(MROUTE_LKM) || !MROUTE_LKM
523 int (*mrt_ioctl
)(int, caddr_t
) = X_mrt_ioctl
;
527 * returns the packet, byte, rpf-failure count for the source group provided
531 register struct sioc_sg_req
*req
;
533 register struct mfc
*rt
;
537 MFCFIND(req
->src
.s_addr
, req
->grp
.s_addr
, rt
);
540 req
->pktcnt
= rt
->mfc_pkt_cnt
;
541 req
->bytecnt
= rt
->mfc_byte_cnt
;
542 req
->wrong_if
= rt
->mfc_wrong_if
;
544 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
550 * returns the input and output packet and byte counts on the vif provided
554 register struct sioc_vif_req
*req
;
556 register vifi_t vifi
= req
->vifi
;
558 if (vifi
>= numvifs
) return EINVAL
;
560 req
->icount
= viftable
[vifi
].v_pkt_in
;
561 req
->ocount
= viftable
[vifi
].v_pkt_out
;
562 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
563 req
->obytes
= viftable
[vifi
].v_bytes_out
;
569 * Enable multicast routing
572 ip_mrouter_init(so
, version
)
577 log(LOG_DEBUG
,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
578 so
->so_type
, so
->so_proto
->pr_protocol
);
580 if (so
->so_type
!= SOCK_RAW
||
581 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
) return EOPNOTSUPP
;
586 if (ip_mrouter
!= NULL
) return EADDRINUSE
;
590 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
591 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
595 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
598 log(LOG_DEBUG
, "ip_mrouter_init\n");
604 * Disable multicast routing
620 * For each phyint in use, disable promiscuous reception of all IP
623 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
624 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
625 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
626 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
627 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
629 ifp
= viftable
[vifi
].v_ifp
;
633 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
634 bzero((caddr_t
)viftable
, sizeof(viftable
));
638 untimeout(expire_upcalls
, (caddr_t
)NULL
);
641 * Free all multicast forwarding cache entries.
643 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
644 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
645 struct mfc
*nr
= rt
->mfc_next
;
647 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
648 struct rtdetq
*n
= rte
->next
;
651 FREE(rte
, M_MRTABLE
);
659 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
662 * Reset de-encapsulation cache
665 last_encap_vif
= NULL
;
666 have_encap_tunnel
= 0;
673 log(LOG_DEBUG
, "ip_mrouter_done\n");
678 #if !defined(MROUTE_LKM) || !MROUTE_LKM
679 int (*ip_mrouter_done
)(void) = X_ip_mrouter_done
;
683 * Set PIM assert processing global
689 if ((i
!= 1) && (i
!= 0))
698 * Add a vif to the vif table
702 register struct vifctl
*vifcp
;
704 register struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
705 static struct sockaddr_in sin
= {sizeof sin
, AF_INET
};
709 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
711 if (vifcp
->vifc_vifi
>= MAXVIFS
) return EINVAL
;
712 if (vifp
->v_lcl_addr
.s_addr
!= 0) return EADDRINUSE
;
714 /* Find the interface with an address in AF_INET family */
715 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
716 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
717 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
;
1083 if (sbappendaddr(&s
->so_rcv
,
1084 (struct sockaddr
*)src
,
1085 mm
, (struct mbuf
*)0) != 0) {
1095 * IP multicast forwarding function. This function assumes that the packet
1096 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1097 * pointed to by "ifp", and the packet is to be relayed to other networks
1098 * that have members of the packet's destination IP multicast group.
1100 * The packet is returned unscathed to the caller, unless it is
1101 * erroneous, in which case a non-zero return value tells the caller to
1105 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1106 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1109 X_ip_mforward(ip
, ifp
, m
, imo
)
1110 register struct ip
*ip
;
1113 struct ip_moptions
*imo
;
1115 register struct mfc
*rt
;
1116 register u_char
*ipoptions
;
1117 static struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1118 static int srctun
= 0;
1119 register struct mbuf
*mm
;
1124 if (mrtdebug
& DEBUG_FORWARD
)
1125 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1126 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1129 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1130 (ipoptions
= (u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1132 * Packet arrived via a physical interface or
1133 * an encapsulated tunnel.
1137 * Packet arrived through a source-route tunnel.
1138 * Source-route tunnels are no longer supported.
1140 if ((srctun
++ % 1000) == 0)
1142 "ip_mforward: received source-routed packet from %lx\n",
1143 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1148 if ((imo
) && ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1149 if (ip
->ip_ttl
< 255)
1150 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1151 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1152 vifp
= viftable
+ vifi
;
1153 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1154 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), vifi
,
1155 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1156 vifp
->v_ifp
->if_name
, vifp
->v_ifp
->if_unit
);
1158 return (ip_mdq(m
, ifp
, NULL
, vifi
));
1160 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1161 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1162 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
));
1164 printf("In fact, no options were specified at all\n");
1168 * Don't forward a packet with time-to-live of zero or one,
1169 * or a packet destined to a local-only group.
1171 if (ip
->ip_ttl
<= 1 ||
1172 ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1176 * Determine forwarding vifs from the forwarding cache table
1179 MFCFIND(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, rt
);
1181 /* Entry exists, so forward if necessary */
1184 return (ip_mdq(m
, ifp
, rt
, -1));
1187 * If we don't have a route for packet's origin,
1188 * Make a copy of the packet &
1189 * send message to routing daemon
1192 register struct mbuf
*mb0
;
1193 register struct rtdetq
*rte
;
1194 register u_long hash
;
1195 int hlen
= ip
->ip_hl
<< 2;
1202 mrtstat
.mrts_no_route
++;
1203 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1204 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1205 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1206 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1209 * Allocate mbufs early so that we don't do extra work if we are
1210 * just going to fail anyway. Make sure to pullup the header so
1211 * that other people can't step on it.
1213 rte
= (struct rtdetq
*) _MALLOC((sizeof *rte
), M_MRTABLE
, M_NOWAIT
);
1218 mb0
= m_copy(m
, 0, M_COPYALL
);
1219 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1220 mb0
= m_pullup(mb0
, hlen
);
1222 FREE(rte
, M_MRTABLE
);
1227 /* is there an upcall waiting for this packet? */
1228 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1229 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1230 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1231 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1232 (rt
->mfc_stall
!= NULL
))
1240 /* no upcall, so make a new entry */
1241 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
1243 FREE(rte
, M_MRTABLE
);
1248 /* Make a copy of the header to send to the user level process */
1249 mm
= m_copy(mb0
, 0, hlen
);
1251 FREE(rte
, M_MRTABLE
);
1253 FREE(rt
, M_MRTABLE
);
1259 * Send message to routing daemon to install
1260 * a route into the kernel table
1262 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1264 im
= mtod(mm
, struct igmpmsg
*);
1265 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1268 mrtstat
.mrts_upcalls
++;
1270 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1271 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1272 ++mrtstat
.mrts_upq_sockfull
;
1273 FREE(rte
, M_MRTABLE
);
1275 FREE(rt
, M_MRTABLE
);
1280 /* insert new entry at head of hash chain */
1281 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1282 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1283 rt
->mfc_expire
= UPCALL_EXPIRE
;
1285 for (i
= 0; i
< numvifs
; i
++)
1286 rt
->mfc_ttls
[i
] = 0;
1287 rt
->mfc_parent
= -1;
1289 /* link into table */
1290 rt
->mfc_next
= mfctable
[hash
];
1291 mfctable
[hash
] = rt
;
1292 rt
->mfc_stall
= rte
;
1295 /* determine if q has overflowed */
1299 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1302 if (npkts
> MAX_UPQ
) {
1303 mrtstat
.mrts_upq_ovflw
++;
1304 FREE(rte
, M_MRTABLE
);
1310 /* Add this entry to the end of the queue */
1327 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1328 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
1329 struct ip_moptions
*) = X_ip_mforward
;
1333 * Clean up the cache entry if upcall is not serviced
1336 expire_upcalls(void *unused
)
1339 struct mfc
*mfc
, **nptr
;
1342 boolean_t funnel_state
;
1345 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
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
);
1388 (void) thread_funnel_set(network_flock
, FALSE
);
1392 * Packet forwarding routine once entry in the cache is made
1395 ip_mdq(m
, ifp
, rt
, xmt_vif
)
1396 register struct mbuf
*m
;
1397 register struct ifnet
*ifp
;
1398 register struct mfc
*rt
;
1399 register vifi_t xmt_vif
;
1401 register struct ip
*ip
= mtod(m
, struct ip
*);
1402 register vifi_t vifi
;
1403 register struct vif
*vifp
;
1404 register int plen
= ip
->ip_len
;
1407 * Macro to send packet on vif. Since RSVP packets don't get counted on
1408 * input, they shouldn't get counted on output, so statistics keeping is
1411 #define MC_SEND(ip,vifp,m) { \
1412 if ((vifp)->v_flags & VIFF_TUNNEL) \
1413 encap_send((ip), (vifp), (m)); \
1415 phyint_send((ip), (vifp), (m)); \
1419 * If xmt_vif is not -1, send on only the requested vif.
1421 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1423 if (xmt_vif
< numvifs
) {
1424 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1429 * Don't forward if it didn't arrive from the parent vif for its origin.
1431 vifi
= rt
->mfc_parent
;
1432 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1433 /* came in the wrong interface */
1434 if (mrtdebug
& DEBUG_FORWARD
)
1435 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1436 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1437 ++mrtstat
.mrts_wrong_if
;
1440 * If we are doing PIM assert processing, and we are forwarding
1441 * packets on this interface, and it is a broadcast medium
1442 * interface (and not a tunnel), send a message to the routing daemon.
1444 if (pim_assert
&& rt
->mfc_ttls
[vifi
] &&
1445 (ifp
->if_flags
& IFF_BROADCAST
) &&
1446 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
1447 struct sockaddr_in k_igmpsrc
;
1450 int hlen
= ip
->ip_hl
<< 2;
1452 register u_long delta
;
1456 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1458 if (delta
> ASSERT_MSG_TIME
) {
1459 mm
= m_copy(m
, 0, hlen
);
1460 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1461 mm
= m_pullup(mm
, hlen
);
1466 rt
->mfc_last_assert
= now
;
1468 im
= mtod(mm
, struct igmpmsg
*);
1469 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1473 k_igmpsrc
.sin_addr
= im
->im_src
;
1475 socket_send(ip_mrouter
, mm
, &k_igmpsrc
);
1481 /* If I sourced this packet, it counts as output, else it was input. */
1482 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1483 viftable
[vifi
].v_pkt_out
++;
1484 viftable
[vifi
].v_bytes_out
+= plen
;
1486 viftable
[vifi
].v_pkt_in
++;
1487 viftable
[vifi
].v_bytes_in
+= plen
;
1490 rt
->mfc_byte_cnt
+= plen
;
1493 * For each vif, decide if a copy of the packet should be forwarded.
1495 * - the ttl exceeds the vif's threshold
1496 * - there are group members downstream on interface
1498 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1499 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1500 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1502 vifp
->v_bytes_out
+= plen
;
1503 MC_SEND(ip
, vifp
, m
);
1510 * check if a vif number is legal/ok. This is used by ip_output, to export
1514 X_legal_vif_num(vif
)
1517 if (vif
>= 0 && vif
< numvifs
)
1523 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1524 int (*legal_vif_num
)(int) = X_legal_vif_num
;
1528 * Return the local address used by this vif
1531 X_ip_mcast_src(vifi
)
1534 if (vifi
>= 0 && vifi
< numvifs
)
1535 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1540 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1541 u_long (*ip_mcast_src
)(int) = X_ip_mcast_src
;
1545 phyint_send(ip
, vifp
, m
)
1550 register struct mbuf
*mb_copy
;
1551 register int hlen
= ip
->ip_hl
<< 2;
1554 * Make a new reference to the packet; make sure that
1555 * the IP header is actually copied, not just referenced,
1556 * so that ip_output() only scribbles on the copy.
1558 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1559 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1560 mb_copy
= m_pullup(mb_copy
, hlen
);
1561 if (mb_copy
== NULL
)
1564 if (vifp
->v_rate_limit
== 0)
1565 tbf_send_packet(vifp
, mb_copy
);
1567 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1571 encap_send(ip
, vifp
, m
)
1572 register struct ip
*ip
;
1573 register struct vif
*vifp
;
1574 register struct mbuf
*m
;
1576 register struct mbuf
*mb_copy
;
1577 register struct ip
*ip_copy
;
1578 register int i
, len
= ip
->ip_len
;
1581 * copy the old packet & pullup its IP header into the
1582 * new mbuf so we can modify it. Try to fill the new
1583 * mbuf since if we don't the ethernet driver will.
1585 MGETHDR(mb_copy
, M_DONTWAIT
, MT_HEADER
);
1586 if (mb_copy
== NULL
)
1588 mb_copy
->m_data
+= max_linkhdr
;
1589 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1591 if ((mb_copy
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1595 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1598 mb_copy
= m_pullup(mb_copy
, i
);
1599 if (mb_copy
== NULL
)
1601 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1604 * fill in the encapsulating IP header.
1606 ip_copy
= mtod(mb_copy
, struct ip
*);
1607 *ip_copy
= multicast_encap_iphdr
;
1609 ip_copy
->ip_id
= ip_randomid();
1611 ip_copy
->ip_id
= htons(ip_id
++);
1613 ip_copy
->ip_len
+= len
;
1614 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1615 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1618 * turn the encapsulated IP header back into a valid one.
1620 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1625 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1626 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1627 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1629 if (vifp
->v_rate_limit
== 0)
1630 tbf_send_packet(vifp
, mb_copy
);
1632 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1636 * De-encapsulate a packet and feed it back through ip input (this
1637 * routine is called whenever IP gets a packet with proto type
1638 * ENCAP_PROTO and a local destination address).
1642 X_ipip_input(m
, iphlen
)
1644 ipip_input(m
, iphlen
)
1646 register struct mbuf
*m
;
1649 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1650 register struct ip
*ip
= mtod(m
, struct ip
*);
1651 register int hlen
= ip
->ip_hl
<< 2;
1653 register struct ifqueue
*ifq
;
1654 register struct vif
*vifp
;
1656 if (!have_encap_tunnel
) {
1657 rip_input(m
, iphlen
);
1661 * dump the packet if it's not to a multicast destination or if
1662 * we don't have an encapsulating tunnel with the source.
1663 * Note: This code assumes that the remote site IP address
1664 * uniquely identifies the tunnel (i.e., that this site has
1665 * at most one tunnel with the remote site).
1667 if (! IN_MULTICAST(ntohl(((struct ip
*)((char *)ip
+ hlen
))->ip_dst
.s_addr
))) {
1668 ++mrtstat
.mrts_bad_tunnel
;
1672 if (ip
->ip_src
.s_addr
!= last_encap_src
) {
1673 register struct vif
*vife
;
1676 vife
= vifp
+ numvifs
;
1677 last_encap_src
= ip
->ip_src
.s_addr
;
1679 for ( ; vifp
< vife
; ++vifp
)
1680 if (vifp
->v_rmt_addr
.s_addr
== ip
->ip_src
.s_addr
) {
1681 if ((vifp
->v_flags
& (VIFF_TUNNEL
|VIFF_SRCRT
))
1683 last_encap_vif
= vifp
;
1687 if ((vifp
= last_encap_vif
) == 0) {
1689 mrtstat
.mrts_cant_tunnel
++; /*XXX*/
1692 log(LOG_DEBUG
, "ip_mforward: no tunnel with %lx\n",
1693 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1698 if (hlen
> IP_HDR_LEN
)
1699 ip_stripoptions(m
, (struct mbuf
*) 0);
1700 m
->m_data
+= IP_HDR_LEN
;
1701 m
->m_len
-= IP_HDR_LEN
;
1702 m
->m_pkthdr
.len
-= IP_HDR_LEN
;
1703 m
->m_pkthdr
.rcvif
= ifp
;
1707 if (IF_QFULL(ifq
)) {
1713 * normally we would need a "schednetisr(NETISR_IP)"
1714 * here but we were called by ip_input and it is going
1715 * to loop back & try to dequeue the packet we just
1716 * queued as soon as we return so we avoid the
1717 * unnecessary software interrrupt.
1724 * Token bucket filter module
1728 tbf_control(vifp
, m
, ip
, p_len
)
1729 register struct vif
*vifp
;
1730 register struct mbuf
*m
;
1731 register struct ip
*ip
;
1732 register u_long p_len
;
1734 register struct tbf
*t
= vifp
->v_tbf
;
1736 if (p_len
> MAX_BKT_SIZE
) {
1737 /* drop if packet is too large */
1738 mrtstat
.mrts_pkt2large
++;
1743 tbf_update_tokens(vifp
);
1745 /* if there are enough tokens,
1746 * and the queue is empty,
1747 * send this packet out
1750 if (t
->tbf_q_len
== 0) {
1751 /* queue empty, send packet if enough tokens */
1752 if (p_len
<= t
->tbf_n_tok
) {
1753 t
->tbf_n_tok
-= p_len
;
1754 tbf_send_packet(vifp
, m
);
1756 /* queue packet and timeout till later */
1758 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1760 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1761 /* finite queue length, so queue pkts and process queue */
1763 tbf_process_q(vifp
);
1765 /* queue length too much, try to dq and queue and process */
1766 if (!tbf_dq_sel(vifp
, ip
)) {
1767 mrtstat
.mrts_q_overflow
++;
1772 tbf_process_q(vifp
);
1779 * adds a packet to the queue at the interface
1783 register struct vif
*vifp
;
1784 register struct mbuf
*m
;
1786 register int s
= splnet();
1787 register struct tbf
*t
= vifp
->v_tbf
;
1789 if (t
->tbf_t
== NULL
) {
1790 /* Queue was empty */
1793 /* Insert at tail */
1794 t
->tbf_t
->m_act
= m
;
1797 /* Set new tail pointer */
1801 /* Make sure we didn't get fed a bogus mbuf */
1803 panic("tbf_queue: m_act");
1814 * processes the queue at the interface
1818 register struct vif
*vifp
;
1820 register struct mbuf
*m
;
1822 register int s
= splnet();
1823 register struct tbf
*t
= vifp
->v_tbf
;
1825 /* loop through the queue at the interface and send as many packets
1828 while (t
->tbf_q_len
> 0) {
1831 len
= mtod(m
, struct ip
*)->ip_len
;
1833 /* determine if the packet can be sent */
1834 if (len
<= t
->tbf_n_tok
) {
1836 * reduce no of tokens, dequeue the packet,
1839 t
->tbf_n_tok
-= len
;
1841 t
->tbf_q
= m
->m_act
;
1842 if (--t
->tbf_q_len
== 0)
1846 tbf_send_packet(vifp
, m
);
1854 tbf_reprocess_q(xvifp
)
1857 register struct vif
*vifp
= xvifp
;
1858 boolean_t funnel_state
;
1860 funnel_state
= thread_funnel_set(network_flock
, TRUE
);
1861 if (ip_mrouter
== NULL
) {
1862 (void) thread_funnel_set(network_flock
, FALSE
);
1866 tbf_update_tokens(vifp
);
1868 tbf_process_q(vifp
);
1870 if (vifp
->v_tbf
->tbf_q_len
)
1871 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1872 (void) thread_funnel_set(network_flock
, FALSE
);
1875 /* function that will selectively discard a member of the queue
1876 * based on the precedence value and the priority
1879 tbf_dq_sel(vifp
, ip
)
1880 register struct vif
*vifp
;
1881 register struct ip
*ip
;
1883 register int s
= splnet();
1885 register struct mbuf
*m
, *last
;
1886 register struct mbuf
**np
;
1887 register struct tbf
*t
= vifp
->v_tbf
;
1889 p
= priority(vifp
, ip
);
1893 while ((m
= *np
) != NULL
) {
1894 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1896 /* If we're removing the last packet, fix the tail pointer */
1900 /* it's impossible for the queue to be empty, but
1901 * we check anyway. */
1902 if (--t
->tbf_q_len
== 0)
1905 mrtstat
.mrts_drop_sel
++;
1916 tbf_send_packet(vifp
, m
)
1917 register struct vif
*vifp
;
1918 register struct mbuf
*m
;
1920 struct ip_moptions imo
;
1922 static struct route ro
;
1925 if (vifp
->v_flags
& VIFF_TUNNEL
) {
1926 /* If tunnel options */
1927 ip_output(m
, (struct mbuf
*)0, &vifp
->v_route
,
1928 IP_FORWARDING
, (struct ip_moptions
*)0);
1930 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1931 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1932 imo
.imo_multicast_loop
= 1;
1933 imo
.imo_multicast_vif
= -1;
1936 * Re-entrancy should not be a problem here, because
1937 * the packets that we send out and are looped back at us
1938 * should get rejected because they appear to come from
1939 * the loopback interface, thus preventing looping.
1941 error
= ip_output(m
, (struct mbuf
*)0, &ro
,
1942 IP_FORWARDING
, &imo
);
1944 if (mrtdebug
& DEBUG_XMIT
)
1945 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1946 vifp
- viftable
, error
);
1951 /* determine the current time and then
1952 * the elapsed time (between the last time and time now)
1953 * in milliseconds & update the no. of tokens in the bucket
1956 tbf_update_tokens(vifp
)
1957 register struct vif
*vifp
;
1961 register int s
= splnet();
1962 register struct tbf
*t
= vifp
->v_tbf
;
1966 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1969 * This formula is actually
1970 * "time in seconds" * "bytes/second".
1972 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1974 * The (1000/1024) was introduced in add_vif to optimize
1975 * this divide into a shift.
1977 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1978 t
->tbf_last_pkt_t
= tp
;
1980 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1981 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1988 register struct vif
*vifp
;
1989 register struct ip
*ip
;
1993 /* temporary hack; may add general packet classifier some day */
1996 * The UDP port space is divided up into four priority ranges:
1997 * [0, 16384) : unclassified - lowest priority
1998 * [16384, 32768) : audio - highest priority
1999 * [32768, 49152) : whiteboard - medium priority
2000 * [49152, 65536) : video - low priority
2002 if (ip
->ip_p
== IPPROTO_UDP
) {
2003 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
2004 switch (ntohs(udp
->uh_dport
) & 0xc000) {
2019 log(LOG_DEBUG
, "port %x prio%d\n", ntohs(udp
->uh_dport
), prio
);
2027 * End of token bucket filter modifications
2031 ip_rsvp_vif_init(so
, sopt
)
2033 struct sockopt
*sopt
;
2038 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2039 so
->so_type
, so
->so_proto
->pr_protocol
);
2041 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2045 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2050 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i
, rsvp_on
);
2055 if (!legal_vif_num(i
)) {
2057 return EADDRNOTAVAIL
;
2060 /* Check if socket is available. */
2061 if (viftable
[i
].v_rsvpd
!= NULL
) {
2066 viftable
[i
].v_rsvpd
= so
;
2067 /* This may seem silly, but we need to be sure we don't over-increment
2068 * the RSVP counter, in case something slips up.
2070 if (!viftable
[i
].v_rsvp_on
) {
2071 viftable
[i
].v_rsvp_on
= 1;
2080 ip_rsvp_vif_done(so
, sopt
)
2082 struct sockopt
*sopt
;
2087 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2088 so
->so_type
, so
->so_proto
->pr_protocol
);
2090 if (so
->so_type
!= SOCK_RAW
||
2091 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2094 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2101 if (!legal_vif_num(i
)) {
2103 return EADDRNOTAVAIL
;
2107 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2108 viftable
[i
].v_rsvpd
, so
);
2110 viftable
[i
].v_rsvpd
= NULL
;
2112 * This may seem silly, but we need to be sure we don't over-decrement
2113 * the RSVP counter, in case something slips up.
2115 if (viftable
[i
].v_rsvp_on
) {
2116 viftable
[i
].v_rsvp_on
= 0;
2125 ip_rsvp_force_done(so
)
2131 /* Don't bother if it is not the right type of socket. */
2132 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2137 /* The socket may be attached to more than one vif...this
2138 * is perfectly legal.
2140 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2141 if (viftable
[vifi
].v_rsvpd
== so
) {
2142 viftable
[vifi
].v_rsvpd
= NULL
;
2143 /* This may seem silly, but we need to be sure we don't
2144 * over-decrement the RSVP counter, in case something slips up.
2146 if (viftable
[vifi
].v_rsvp_on
) {
2147 viftable
[vifi
].v_rsvp_on
= 0;
2158 rsvp_input(m
, iphlen
)
2163 register struct ip
*ip
= mtod(m
, struct ip
*);
2164 static struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
};
2169 printf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2171 /* Can still get packets with rsvp_on = 0 if there is a local member
2172 * of the group to which the RSVP packet is addressed. But in this
2173 * case we want to throw the packet away.
2183 printf("rsvp_input: check vifs\n");
2186 if (!(m
->m_flags
& M_PKTHDR
))
2187 panic("rsvp_input no hdr");
2190 ifp
= m
->m_pkthdr
.rcvif
;
2191 /* Find which vif the packet arrived on. */
2192 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2193 if (viftable
[vifi
].v_ifp
== ifp
)
2196 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2198 * If the old-style non-vif-associated socket is set,
2199 * then use it. Otherwise, drop packet since there
2200 * is no specific socket for this vif.
2202 if (ip_rsvpd
!= NULL
) {
2204 printf("rsvp_input: Sending packet up old-style socket\n");
2205 rip_input(m
, iphlen
); /* xxx */
2207 if (rsvpdebug
&& vifi
== numvifs
)
2208 printf("rsvp_input: Can't find vif for packet.\n");
2209 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2210 printf("rsvp_input: No socket defined for vif %d\n",vifi
);
2216 rsvp_src
.sin_addr
= ip
->ip_src
;
2219 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2220 m
->m_len
,sbspace(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2222 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2224 printf("rsvp_input: Failed to append to socket\n");
2227 printf("rsvp_input: send packet up\n");
2234 #include <sys/conf.h>
2235 #include <sys/exec.h>
2236 #include <sys/sysent.h>
2237 #include <sys/lkm.h>
2239 MOD_MISC("ip_mroute_mod")
2242 ip_mroute_mod_handle(struct lkm_table
*lkmtp
, int cmd
)
2245 struct lkm_misc
*args
= lkmtp
->private.lkm_misc
;
2249 static int (*old_ip_mrouter_cmd
)();
2250 static int (*old_ip_mrouter_done
)();
2251 static int (*old_ip_mforward
)();
2252 static int (*old_mrt_ioctl
)();
2253 static void (*old_proto4_input
)();
2254 static int (*old_legal_vif_num
)();
2255 extern struct protosw inetsw
[];
2258 if(lkmexists(lkmtp
) || ip_mrtproto
)
2260 old_ip_mrouter_cmd
= ip_mrouter_cmd
;
2261 ip_mrouter_cmd
= X_ip_mrouter_cmd
;
2262 old_ip_mrouter_done
= ip_mrouter_done
;
2263 ip_mrouter_done
= X_ip_mrouter_done
;
2264 old_ip_mforward
= ip_mforward
;
2265 ip_mforward
= X_ip_mforward
;
2266 old_mrt_ioctl
= mrt_ioctl
;
2267 mrt_ioctl
= X_mrt_ioctl
;
2268 old_proto4_input
= ip_protox
[ENCAP_PROTO
]->pr_input
;
2269 ip_protox
[ENCAP_PROTO
]->pr_input
= X_ipip_input
;
2270 old_legal_vif_num
= legal_vif_num
;
2271 legal_vif_num
= X_legal_vif_num
;
2272 ip_mrtproto
= IGMP_DVMRP
;
2274 printf("\nIP multicast routing loaded\n");
2281 ip_mrouter_cmd
= old_ip_mrouter_cmd
;
2282 ip_mrouter_done
= old_ip_mrouter_done
;
2283 ip_mforward
= old_ip_mforward
;
2284 mrt_ioctl
= old_mrt_ioctl
;
2285 ip_protox
[ENCAP_PROTO
]->pr_input
= old_proto4_input
;
2286 legal_vif_num
= old_legal_vif_num
;
2299 ip_mroute_mod(struct lkm_table
*lkmtp
, int cmd
, int ver
) {
2300 DISPATCH(lkmtp
, cmd
, ver
, ip_mroute_mod_handle
, ip_mroute_mod_handle
,
2304 #endif /* MROUTE_LKM */
2305 #endif /* MROUTING */