]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_mroute.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * IP multicast forwarding procedures
25 * Written by David Waitzman, BBN Labs, August 1988.
26 * Modified by Steve Deering, Stanford, February 1989.
27 * Modified by Mark J. Steiglitz, Stanford, May, 1991
28 * Modified by Van Jacobson, LBL, January 1993
29 * Modified by Ajit Thyagarajan, PARC, August 1993
30 * Modified by Bill Fenner, PARC, April 1995
32 * MROUTING Revision: 3.5
33 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
41 #include <sys/socket.h>
42 #include <sys/socketvar.h>
43 #include <sys/protosw.h>
45 #include <sys/kernel.h>
46 #include <sys/sockio.h>
47 #include <sys/syslog.h>
49 #include <net/route.h>
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/in_var.h>
55 #include <netinet/igmp.h>
56 #include <netinet/ip_mroute.h>
57 #include <netinet/udp.h>
60 #if BYTE_ORDER != BIG_ENDIAN
61 #define NTOHL(d) ((d) = ntohl((d)))
62 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
63 #define HTONL(d) ((d) = htonl((d)))
64 #define HTONS(d) ((d) = htons((u_short)(d)))
74 extern u_long
_ip_mcast_src(int vifi
);
75 extern int _ip_mforward(struct ip
*ip
, struct ifnet
*ifp
,
76 struct mbuf
*m
, struct ip_moptions
*imo
);
77 extern int _ip_mrouter_done(void);
78 extern int _ip_mrouter_get(struct socket
*so
, struct sockopt
*sopt
);
79 extern int _ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
);
80 extern int _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
);
83 * Dummy routines and globals used when multicast routing is not compiled in.
86 struct socket
*ip_mrouter
= NULL
;
90 _ip_mrouter_set(so
, sopt
)
97 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = _ip_mrouter_set
;
101 _ip_mrouter_get(so
, sopt
)
103 struct sockopt
*sopt
;
108 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = _ip_mrouter_get
;
116 int (*ip_mrouter_done
)(void) = _ip_mrouter_done
;
119 _ip_mforward(ip
, ifp
, m
, imo
)
123 struct ip_moptions
*imo
;
128 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
129 struct ip_moptions
*) = _ip_mforward
;
132 _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
)
137 int (*mrt_ioctl
)(int, caddr_t
, struct proc
*) = _mrt_ioctl
;
140 rsvp_input(m
, iphlen
) /* XXX must fixup manually */
144 /* Can still get packets with rsvp_on = 0 if there is a local member
145 * of the group to which the RSVP packet is addressed. But in this
146 * case we want to throw the packet away.
153 if (ip_rsvpd
!= NULL
) {
155 printf("rsvp_input: Sending packet up old-style socket\n");
156 rip_input(m
, iphlen
);
159 /* Drop the packet */
163 void ipip_input(struct mbuf
*m
, int iphlen
) { /* XXX must fixup manually */
164 rip_input(m
, iphlen
);
167 int (*legal_vif_num
)(int) = 0;
170 * This should never be called, since IP_MULTICAST_VIF should fail, but
171 * just in case it does get called, the code a little lower in ip_output
172 * will assign the packet a local address.
175 _ip_mcast_src(int vifi
) { return INADDR_ANY
; }
176 u_long (*ip_mcast_src
)(int) = _ip_mcast_src
;
179 ip_rsvp_vif_init(so
, sopt
)
181 struct sockopt
*sopt
;
187 ip_rsvp_vif_done(so
, sopt
)
189 struct sockopt
*sopt
;
195 ip_rsvp_force_done(so
)
203 #define M_HASCL(m) ((m)->m_flags & M_EXT)
205 #define INSIZ sizeof(struct in_addr)
206 #define same(a1, a2) \
207 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
211 * Globals. All but ip_mrouter and ip_mrtproto could be static,
212 * except for netstat or debugging purposes.
215 struct socket
*ip_mrouter
= NULL
;
216 static struct mrtstat mrtstat
;
217 #else /* MROUTE_LKM */
218 extern void X_ipip_input(struct mbuf
*m
, int iphlen
);
219 extern struct mrtstat mrtstat
;
220 static int ip_mrtproto
;
223 #define NO_RTE_FOUND 0x1
224 #define RTE_FOUND 0x2
226 static struct mfc
*mfctable
[MFCTBLSIZ
];
227 static u_char nexpire
[MFCTBLSIZ
];
228 static struct vif viftable
[MAXVIFS
];
229 static u_int mrtdebug
= 0; /* debug level */
230 #define DEBUG_MFC 0x02
231 #define DEBUG_FORWARD 0x04
232 #define DEBUG_EXPIRE 0x08
233 #define DEBUG_XMIT 0x10
234 static u_int tbfdebug
= 0; /* tbf debug level */
235 static u_int rsvpdebug
= 0; /* rsvp debug level */
237 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
238 #define UPCALL_EXPIRE 6 /* number of timeouts */
241 * Define the token bucket filter structures
242 * tbftable -> each vif has one of these for storing info
245 static struct tbf tbftable
[MAXVIFS
];
246 #define TBF_REPROCESS (hz / 100) /* 100x / second */
249 * 'Interfaces' associated with decapsulator (so we can tell
250 * packets that went through it from ones that get reflected
251 * by a broken gateway). These interfaces are never linked into
252 * the system ifnet list & no routes point to them. I.e., packets
253 * can't be sent this way. They only exist as a placeholder for
254 * multicast source verification.
256 static struct ifnet multicast_decap_if
[MAXVIFS
];
259 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
261 /* prototype IP hdr for encapsulated packets */
262 static struct ip multicast_encap_iphdr
= {
263 #if BYTE_ORDER == LITTLE_ENDIAN
264 sizeof(struct ip
) >> 2, IPVERSION
,
266 IPVERSION
, sizeof(struct ip
) >> 2,
269 sizeof(struct ip
), /* total length */
272 ENCAP_TTL
, ENCAP_PROTO
,
279 static vifi_t numvifs
= 0;
280 static int have_encap_tunnel
= 0;
283 * one-back cache used by ipip_input to locate a tunnel's vif
284 * given a datagram's src ip address.
286 static u_long last_encap_src
;
287 static struct vif
*last_encap_vif
;
289 static u_long
X_ip_mcast_src(int vifi
);
290 static int X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
, struct ip_moptions
*imo
);
291 static int X_ip_mrouter_done(void);
292 static int X_ip_mrouter_get(struct socket
*so
, struct sockopt
*m
);
293 static int X_ip_mrouter_set(struct socket
*so
, struct sockopt
*m
);
294 static int X_legal_vif_num(int vif
);
295 static int X_mrt_ioctl(int cmd
, caddr_t data
);
297 static int get_sg_cnt(struct sioc_sg_req
*);
298 static int get_vif_cnt(struct sioc_vif_req
*);
299 static int ip_mrouter_init(struct socket
*, int);
300 static int add_vif(struct vifctl
*);
301 static int del_vif(vifi_t
);
302 static int add_mfc(struct mfcctl
*);
303 static int del_mfc(struct mfcctl
*);
304 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
305 static int set_assert(int);
306 static void expire_upcalls(void *);
307 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*,
309 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
310 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
311 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
312 static void tbf_queue(struct vif
*, struct mbuf
*);
313 static void tbf_process_q(struct vif
*);
314 static void tbf_reprocess_q(void *);
315 static int tbf_dq_sel(struct vif
*, struct ip
*);
316 static void tbf_send_packet(struct vif
*, struct mbuf
*);
317 static void tbf_update_tokens(struct vif
*);
318 static int priority(struct vif
*, struct ip
*);
319 void multiencap_decap(struct mbuf
*);
322 * whether or not special PIM assert processing is enabled.
324 static int pim_assert
;
326 * Rate limit for assert notification messages, in usec
328 #define ASSERT_MSG_TIME 3000000
331 * Hash function for a source, group entry
333 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
334 ((g) >> 20) ^ ((g) >> 10) ^ (g))
337 * Find a route for a given origin IP address and Multicast group address
338 * Type of service parameter to be added in the future!!!
341 #define MFCFIND(o, g, rt) { \
342 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
344 ++mrtstat.mrts_mfc_lookups; \
346 if ((_rt->mfc_origin.s_addr == o) && \
347 (_rt->mfc_mcastgrp.s_addr == g) && \
348 (_rt->mfc_stall == NULL)) { \
352 _rt = _rt->mfc_next; \
355 ++mrtstat.mrts_mfc_misses; \
361 * Macros to compute elapsed time efficiently
362 * Borrowed from Van Jacobson's scheduling code
364 #define TV_DELTA(a, b, delta) { \
367 delta = (a).tv_usec - (b).tv_usec; \
368 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
377 delta += (1000000 * xxs); \
382 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
383 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
386 u_long upcall_data
[51];
387 static void collate(struct timeval
*);
388 #endif /* UPCALL_TIMING */
392 * Handle MRT setsockopt commands to modify the multicast routing tables.
395 X_ip_mrouter_set(so
, sopt
)
397 struct sockopt
*sopt
;
404 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
408 switch (sopt
->sopt_name
) {
410 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
414 error
= ip_mrouter_init(so
, optval
);
418 error
= ip_mrouter_done();
422 error
= sooptcopyin(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
425 error
= add_vif(&vifc
);
429 error
= sooptcopyin(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
432 error
= del_vif(vifi
);
437 error
= sooptcopyin(sopt
, &mfc
, sizeof mfc
, sizeof mfc
);
440 if (sopt
->sopt_name
== MRT_ADD_MFC
)
441 error
= add_mfc(&mfc
);
443 error
= del_mfc(&mfc
);
447 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
461 #if !defined(MROUTE_LKM) || !MROUTE_LKM
462 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_set
;
466 * Handle MRT getsockopt commands
469 X_ip_mrouter_get(so
, sopt
)
471 struct sockopt
*sopt
;
474 static int version
= 0x0305; /* !!! why is this here? XXX */
476 switch (sopt
->sopt_name
) {
478 error
= sooptcopyout(sopt
, &version
, sizeof version
);
482 error
= sooptcopyout(sopt
, &pim_assert
, sizeof pim_assert
);
491 #if !defined(MROUTE_LKM) || !MROUTE_LKM
492 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_get
;
496 * Handle ioctl commands to obtain information from the cache
499 X_mrt_ioctl(cmd
, data
)
506 case (SIOCGETVIFCNT
):
507 return (get_vif_cnt((struct sioc_vif_req
*)data
));
510 return (get_sg_cnt((struct sioc_sg_req
*)data
));
519 #if !defined(MROUTE_LKM) || !MROUTE_LKM
520 int (*mrt_ioctl
)(int, caddr_t
) = X_mrt_ioctl
;
524 * returns the packet, byte, rpf-failure count for the source group provided
528 register struct sioc_sg_req
*req
;
530 register struct mfc
*rt
;
534 MFCFIND(req
->src
.s_addr
, req
->grp
.s_addr
, rt
);
537 req
->pktcnt
= rt
->mfc_pkt_cnt
;
538 req
->bytecnt
= rt
->mfc_byte_cnt
;
539 req
->wrong_if
= rt
->mfc_wrong_if
;
541 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
547 * returns the input and output packet and byte counts on the vif provided
551 register struct sioc_vif_req
*req
;
553 register vifi_t vifi
= req
->vifi
;
555 if (vifi
>= numvifs
) return EINVAL
;
557 req
->icount
= viftable
[vifi
].v_pkt_in
;
558 req
->ocount
= viftable
[vifi
].v_pkt_out
;
559 req
->ibytes
= viftable
[vifi
].v_bytes_in
;
560 req
->obytes
= viftable
[vifi
].v_bytes_out
;
566 * Enable multicast routing
569 ip_mrouter_init(so
, version
)
574 log(LOG_DEBUG
,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
575 so
->so_type
, so
->so_proto
->pr_protocol
);
577 if (so
->so_type
!= SOCK_RAW
||
578 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
) return EOPNOTSUPP
;
583 if (ip_mrouter
!= NULL
) return EADDRINUSE
;
587 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
588 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
592 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
595 log(LOG_DEBUG
, "ip_mrouter_init\n");
601 * Disable multicast routing
617 * For each phyint in use, disable promiscuous reception of all IP
620 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
621 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
622 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
623 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
624 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
626 ifp
= viftable
[vifi
].v_ifp
;
630 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
631 bzero((caddr_t
)viftable
, sizeof(viftable
));
635 untimeout(expire_upcalls
, (caddr_t
)NULL
);
638 * Free all multicast forwarding cache entries.
640 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
641 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
642 struct mfc
*nr
= rt
->mfc_next
;
644 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
645 struct rtdetq
*n
= rte
->next
;
648 FREE(rte
, M_MRTABLE
);
656 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
659 * Reset de-encapsulation cache
662 last_encap_vif
= NULL
;
663 have_encap_tunnel
= 0;
670 log(LOG_DEBUG
, "ip_mrouter_done\n");
675 #if !defined(MROUTE_LKM) || !MROUTE_LKM
676 int (*ip_mrouter_done
)(void) = X_ip_mrouter_done
;
680 * Set PIM assert processing global
686 if ((i
!= 1) && (i
!= 0))
695 * Add a vif to the vif table
699 register struct vifctl
*vifcp
;
701 register struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
702 static struct sockaddr_in sin
= {sizeof sin
, AF_INET
};
706 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
708 if (vifcp
->vifc_vifi
>= MAXVIFS
) return EINVAL
;
709 if (vifp
->v_lcl_addr
.s_addr
!= 0) return EADDRINUSE
;
711 /* Find the interface with an address in AF_INET family */
712 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
713 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
714 if (ifa
== 0) return EADDRNOTAVAIL
;
719 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
720 if ((vifcp
->vifc_flags
& VIFF_SRCRT
) == 0) {
722 * An encapsulating tunnel is wanted. Tell ipip_input() to
723 * start paying attention to encapsulated packets.
725 if (have_encap_tunnel
== 0) {
726 have_encap_tunnel
= 1;
727 for (s
= 0; s
< MAXVIFS
; ++s
) {
728 multicast_decap_if
[s
].if_name
= "mdecap";
729 multicast_decap_if
[s
].if_unit
= s
;
730 multicast_decap_if
[s
].if_family
= APPLE_IF_FAM_MDECAP
;
734 * Set interface to fake encapsulator interface
736 ifp
= &multicast_decap_if
[vifcp
->vifc_vifi
];
738 * Prepare cached route entry
740 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
742 log(LOG_ERR
, "source routed tunnels not supported\n");
746 /* Make sure the interface supports multicast */
747 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
750 /* Enable promiscuous reception of all IP multicasts from the if */
752 error
= if_allmulti(ifp
, 1);
759 /* define parameters for the tbf structure */
761 GET_TIME(vifp
->v_tbf
->tbf_last_pkt_t
);
762 vifp
->v_tbf
->tbf_n_tok
= 0;
763 vifp
->v_tbf
->tbf_q_len
= 0;
764 vifp
->v_tbf
->tbf_max_q_len
= MAXQSIZE
;
765 vifp
->v_tbf
->tbf_q
= vifp
->v_tbf
->tbf_t
= NULL
;
767 vifp
->v_flags
= vifcp
->vifc_flags
;
768 vifp
->v_threshold
= vifcp
->vifc_threshold
;
769 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
770 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
772 /* scaling up here allows division by 1024 in critical code */
773 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
775 vifp
->v_rsvpd
= NULL
;
776 /* initialize per vif pkt counters */
779 vifp
->v_bytes_in
= 0;
780 vifp
->v_bytes_out
= 0;
783 /* Adjust numvifs up if the vifi is higher than numvifs */
784 if (numvifs
<= vifcp
->vifc_vifi
) numvifs
= vifcp
->vifc_vifi
+ 1;
787 log(LOG_DEBUG
, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
789 (u_long
)ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
790 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
791 (u_long
)ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
792 vifcp
->vifc_threshold
,
793 vifcp
->vifc_rate_limit
);
799 * Delete a vif from the vif table
805 register struct vif
*vifp
= &viftable
[vifi
];
806 register struct mbuf
*m
;
811 if (vifi
>= numvifs
) return EINVAL
;
812 if (vifp
->v_lcl_addr
.s_addr
== 0) return EADDRNOTAVAIL
;
816 if (!(vifp
->v_flags
& VIFF_TUNNEL
)) {
817 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
818 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
= INADDR_ANY
;
823 if (vifp
== last_encap_vif
) {
829 * Free packets queued at the interface
831 while (vifp
->v_tbf
->tbf_q
) {
832 m
= vifp
->v_tbf
->tbf_q
;
833 vifp
->v_tbf
->tbf_q
= m
->m_act
;
837 bzero((caddr_t
)vifp
->v_tbf
, sizeof(*(vifp
->v_tbf
)));
838 bzero((caddr_t
)vifp
, sizeof (*vifp
));
841 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", vifi
, numvifs
);
843 /* Adjust numvifs down */
844 for (vifi
= numvifs
; vifi
> 0; vifi
--)
845 if (viftable
[vifi
-1].v_lcl_addr
.s_addr
!= 0) break;
858 struct mfcctl
*mfccp
;
863 register u_short nstl
;
867 MFCFIND(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
, rt
);
869 /* If an entry already exists, just update the fields */
871 if (mrtdebug
& DEBUG_MFC
)
872 log(LOG_DEBUG
,"add_mfc update o %lx g %lx p %x\n",
873 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
874 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
878 rt
->mfc_parent
= mfccp
->mfcc_parent
;
879 for (i
= 0; i
< numvifs
; i
++)
880 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
886 * Find the entry for which the upcall was made and update
889 hash
= MFCHASH(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
890 for (rt
= mfctable
[hash
], nstl
= 0; rt
; rt
= rt
->mfc_next
) {
892 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
893 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
) &&
894 (rt
->mfc_stall
!= NULL
)) {
897 log(LOG_ERR
, "add_mfc %s o %lx g %lx p %x dbx %p\n",
898 "multiple kernel entries",
899 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
900 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
901 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
903 if (mrtdebug
& DEBUG_MFC
)
904 log(LOG_DEBUG
,"add_mfc o %lx g %lx p %x dbg %p\n",
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 rt
->mfc_origin
= mfccp
->mfcc_origin
;
910 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
911 rt
->mfc_parent
= mfccp
->mfcc_parent
;
912 for (i
= 0; i
< numvifs
; i
++)
913 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
914 /* initialize pkt counters per src-grp */
916 rt
->mfc_byte_cnt
= 0;
917 rt
->mfc_wrong_if
= 0;
918 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
920 rt
->mfc_expire
= 0; /* Don't clean this guy up */
923 /* free packets Qed at the end of this entry */
924 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
925 struct rtdetq
*n
= rte
->next
;
927 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
931 #endif /* UPCALL_TIMING */
932 FREE(rte
, M_MRTABLE
);
935 rt
->mfc_stall
= NULL
;
940 * It is possible that an entry is being inserted without an upcall
943 if (mrtdebug
& DEBUG_MFC
)
944 log(LOG_DEBUG
,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
945 hash
, (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
946 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
949 for (rt
= mfctable
[hash
]; rt
!= NULL
; rt
= rt
->mfc_next
) {
951 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
952 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
)) {
954 rt
->mfc_origin
= mfccp
->mfcc_origin
;
955 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
956 rt
->mfc_parent
= mfccp
->mfcc_parent
;
957 for (i
= 0; i
< numvifs
; i
++)
958 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
959 /* initialize pkt counters per src-grp */
961 rt
->mfc_byte_cnt
= 0;
962 rt
->mfc_wrong_if
= 0;
963 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
970 /* no upcall, so make a new entry */
971 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
977 /* insert new entry at head of hash chain */
978 rt
->mfc_origin
= mfccp
->mfcc_origin
;
979 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
980 rt
->mfc_parent
= mfccp
->mfcc_parent
;
981 for (i
= 0; i
< numvifs
; i
++)
982 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
983 /* initialize pkt counters per src-grp */
985 rt
->mfc_byte_cnt
= 0;
986 rt
->mfc_wrong_if
= 0;
987 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
989 rt
->mfc_stall
= NULL
;
991 /* link into table */
992 rt
->mfc_next
= mfctable
[hash
];
1002 * collect delay statistics on the upcalls
1004 static void collate(t
)
1005 register struct timeval
*t
;
1008 register struct timeval tp
;
1009 register u_long delta
;
1015 TV_DELTA(tp
, *t
, delta
);
1024 #endif /* UPCALL_TIMING */
1027 * Delete an mfc entry
1031 struct mfcctl
*mfccp
;
1033 struct in_addr origin
;
1034 struct in_addr mcastgrp
;
1040 origin
= mfccp
->mfcc_origin
;
1041 mcastgrp
= mfccp
->mfcc_mcastgrp
;
1042 hash
= MFCHASH(origin
.s_addr
, mcastgrp
.s_addr
);
1044 if (mrtdebug
& DEBUG_MFC
)
1045 log(LOG_DEBUG
,"del_mfc orig %lx mcastgrp %lx\n",
1046 (u_long
)ntohl(origin
.s_addr
), (u_long
)ntohl(mcastgrp
.s_addr
));
1050 nptr
= &mfctable
[hash
];
1051 while ((rt
= *nptr
) != NULL
) {
1052 if (origin
.s_addr
== rt
->mfc_origin
.s_addr
&&
1053 mcastgrp
.s_addr
== rt
->mfc_mcastgrp
.s_addr
&&
1054 rt
->mfc_stall
== NULL
)
1057 nptr
= &rt
->mfc_next
;
1061 return EADDRNOTAVAIL
;
1064 *nptr
= rt
->mfc_next
;
1065 FREE(rt
, M_MRTABLE
);
1073 * Send a message to mrouted on the multicast routing socket
1076 socket_send(s
, mm
, src
)
1079 struct sockaddr_in
*src
;
1083 if (sbappendaddr(&s
->so_rcv
,
1084 (struct sockaddr
*)src
,
1085 mm
, (struct mbuf
*)0, NULL
) != 0) {
1087 socket_unlock(s
, 1);
1091 socket_unlock(s
, 1);
1097 * IP multicast forwarding function. This function assumes that the packet
1098 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1099 * pointed to by "ifp", and the packet is to be relayed to other networks
1100 * that have members of the packet's destination IP multicast group.
1102 * The packet is returned unscathed to the caller, unless it is
1103 * erroneous, in which case a non-zero return value tells the caller to
1107 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1108 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1111 X_ip_mforward(ip
, ifp
, m
, imo
)
1112 register struct ip
*ip
;
1115 struct ip_moptions
*imo
;
1117 register struct mfc
*rt
;
1118 register u_char
*ipoptions
;
1119 static struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
};
1120 static int srctun
= 0;
1121 register struct mbuf
*mm
;
1126 if (mrtdebug
& DEBUG_FORWARD
)
1127 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1128 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1131 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1132 (ipoptions
= (u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1134 * Packet arrived via a physical interface or
1135 * an encapsulated tunnel.
1139 * Packet arrived through a source-route tunnel.
1140 * Source-route tunnels are no longer supported.
1142 if ((srctun
++ % 1000) == 0)
1144 "ip_mforward: received source-routed packet from %lx\n",
1145 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1150 if ((imo
) && ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1151 if (ip
->ip_ttl
< 255)
1152 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1153 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1154 vifp
= viftable
+ vifi
;
1155 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n",
1156 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), vifi
,
1157 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1158 vifp
->v_ifp
->if_name
, vifp
->v_ifp
->if_unit
);
1160 return (ip_mdq(m
, ifp
, NULL
, vifi
));
1162 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1163 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n",
1164 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
));
1166 printf("In fact, no options were specified at all\n");
1170 * Don't forward a packet with time-to-live of zero or one,
1171 * or a packet destined to a local-only group.
1173 if (ip
->ip_ttl
<= 1 ||
1174 ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1178 * Determine forwarding vifs from the forwarding cache table
1181 MFCFIND(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, rt
);
1183 /* Entry exists, so forward if necessary */
1186 return (ip_mdq(m
, ifp
, rt
, -1));
1189 * If we don't have a route for packet's origin,
1190 * Make a copy of the packet &
1191 * send message to routing daemon
1194 register struct mbuf
*mb0
;
1195 register struct rtdetq
*rte
;
1196 register u_long hash
;
1197 int hlen
= ip
->ip_hl
<< 2;
1204 mrtstat
.mrts_no_route
++;
1205 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1206 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1207 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1208 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1211 * Allocate mbufs early so that we don't do extra work if we are
1212 * just going to fail anyway. Make sure to pullup the header so
1213 * that other people can't step on it.
1215 rte
= (struct rtdetq
*) _MALLOC((sizeof *rte
), M_MRTABLE
, M_NOWAIT
);
1220 mb0
= m_copy(m
, 0, M_COPYALL
);
1221 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1222 mb0
= m_pullup(mb0
, hlen
);
1224 FREE(rte
, M_MRTABLE
);
1229 /* is there an upcall waiting for this packet? */
1230 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1231 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1232 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1233 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1234 (rt
->mfc_stall
!= NULL
))
1242 /* no upcall, so make a new entry */
1243 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
1245 FREE(rte
, M_MRTABLE
);
1250 /* Make a copy of the header to send to the user level process */
1251 mm
= m_copy(mb0
, 0, hlen
);
1253 FREE(rte
, M_MRTABLE
);
1255 FREE(rt
, M_MRTABLE
);
1261 * Send message to routing daemon to install
1262 * a route into the kernel table
1264 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1266 im
= mtod(mm
, struct igmpmsg
*);
1267 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1270 mrtstat
.mrts_upcalls
++;
1272 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1273 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1274 ++mrtstat
.mrts_upq_sockfull
;
1275 FREE(rte
, M_MRTABLE
);
1277 FREE(rt
, M_MRTABLE
);
1282 /* insert new entry at head of hash chain */
1283 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1284 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1285 rt
->mfc_expire
= UPCALL_EXPIRE
;
1287 for (i
= 0; i
< numvifs
; i
++)
1288 rt
->mfc_ttls
[i
] = 0;
1289 rt
->mfc_parent
= -1;
1291 /* link into table */
1292 rt
->mfc_next
= mfctable
[hash
];
1293 mfctable
[hash
] = rt
;
1294 rt
->mfc_stall
= rte
;
1297 /* determine if q has overflowed */
1301 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1304 if (npkts
> MAX_UPQ
) {
1305 mrtstat
.mrts_upq_ovflw
++;
1306 FREE(rte
, M_MRTABLE
);
1312 /* Add this entry to the end of the queue */
1329 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1330 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
1331 struct ip_moptions
*) = X_ip_mforward
;
1335 * Clean up the cache entry if upcall is not serviced
1338 expire_upcalls(void *unused
)
1341 struct mfc
*mfc
, **nptr
;
1347 for (i
= 0; i
< MFCTBLSIZ
; i
++) {
1348 if (nexpire
[i
] == 0)
1350 nptr
= &mfctable
[i
];
1351 for (mfc
= *nptr
; mfc
!= NULL
; mfc
= *nptr
) {
1353 * Skip real cache entries
1354 * Make sure it wasn't marked to not expire (shouldn't happen)
1357 if (mfc
->mfc_stall
!= NULL
&&
1358 mfc
->mfc_expire
!= 0 &&
1359 --mfc
->mfc_expire
== 0) {
1360 if (mrtdebug
& DEBUG_EXPIRE
)
1361 log(LOG_DEBUG
, "expire_upcalls: expiring (%lx %lx)\n",
1362 (u_long
)ntohl(mfc
->mfc_origin
.s_addr
),
1363 (u_long
)ntohl(mfc
->mfc_mcastgrp
.s_addr
));
1365 * drop all the packets
1366 * free the mbuf with the pkt, if, timing info
1368 for (rte
= mfc
->mfc_stall
; rte
; ) {
1369 struct rtdetq
*n
= rte
->next
;
1372 FREE(rte
, M_MRTABLE
);
1375 ++mrtstat
.mrts_cache_cleanups
;
1378 *nptr
= mfc
->mfc_next
;
1379 FREE(mfc
, M_MRTABLE
);
1381 nptr
= &mfc
->mfc_next
;
1386 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1390 * Packet forwarding routine once entry in the cache is made
1393 ip_mdq(m
, ifp
, rt
, xmt_vif
)
1394 register struct mbuf
*m
;
1395 register struct ifnet
*ifp
;
1396 register struct mfc
*rt
;
1397 register vifi_t xmt_vif
;
1399 register struct ip
*ip
= mtod(m
, struct ip
*);
1400 register vifi_t vifi
;
1401 register struct vif
*vifp
;
1402 register int plen
= ip
->ip_len
;
1405 * Macro to send packet on vif. Since RSVP packets don't get counted on
1406 * input, they shouldn't get counted on output, so statistics keeping is
1409 #define MC_SEND(ip,vifp,m) { \
1410 if ((vifp)->v_flags & VIFF_TUNNEL) \
1411 encap_send((ip), (vifp), (m)); \
1413 phyint_send((ip), (vifp), (m)); \
1417 * If xmt_vif is not -1, send on only the requested vif.
1419 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1421 if (xmt_vif
< numvifs
) {
1422 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1427 * Don't forward if it didn't arrive from the parent vif for its origin.
1429 vifi
= rt
->mfc_parent
;
1430 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1431 /* came in the wrong interface */
1432 if (mrtdebug
& DEBUG_FORWARD
)
1433 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1434 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1435 ++mrtstat
.mrts_wrong_if
;
1438 * If we are doing PIM assert processing, and we are forwarding
1439 * packets on this interface, and it is a broadcast medium
1440 * interface (and not a tunnel), send a message to the routing daemon.
1442 if (pim_assert
&& rt
->mfc_ttls
[vifi
] &&
1443 (ifp
->if_flags
& IFF_BROADCAST
) &&
1444 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
1445 struct sockaddr_in k_igmpsrc
;
1448 int hlen
= ip
->ip_hl
<< 2;
1450 register u_long delta
;
1454 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1456 if (delta
> ASSERT_MSG_TIME
) {
1457 mm
= m_copy(m
, 0, hlen
);
1458 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1459 mm
= m_pullup(mm
, hlen
);
1464 rt
->mfc_last_assert
= now
;
1466 im
= mtod(mm
, struct igmpmsg
*);
1467 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1471 k_igmpsrc
.sin_addr
= im
->im_src
;
1473 socket_send(ip_mrouter
, mm
, &k_igmpsrc
);
1479 /* If I sourced this packet, it counts as output, else it was input. */
1480 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1481 viftable
[vifi
].v_pkt_out
++;
1482 viftable
[vifi
].v_bytes_out
+= plen
;
1484 viftable
[vifi
].v_pkt_in
++;
1485 viftable
[vifi
].v_bytes_in
+= plen
;
1488 rt
->mfc_byte_cnt
+= plen
;
1491 * For each vif, decide if a copy of the packet should be forwarded.
1493 * - the ttl exceeds the vif's threshold
1494 * - there are group members downstream on interface
1496 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1497 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1498 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1500 vifp
->v_bytes_out
+= plen
;
1501 MC_SEND(ip
, vifp
, m
);
1508 * check if a vif number is legal/ok. This is used by ip_output, to export
1512 X_legal_vif_num(vif
)
1515 if (vif
>= 0 && vif
< numvifs
)
1521 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1522 int (*legal_vif_num
)(int) = X_legal_vif_num
;
1526 * Return the local address used by this vif
1529 X_ip_mcast_src(vifi
)
1532 if (vifi
>= 0 && vifi
< numvifs
)
1533 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1538 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1539 u_long (*ip_mcast_src
)(int) = X_ip_mcast_src
;
1543 phyint_send(ip
, vifp
, m
)
1548 register struct mbuf
*mb_copy
;
1549 register int hlen
= ip
->ip_hl
<< 2;
1552 * Make a new reference to the packet; make sure that
1553 * the IP header is actually copied, not just referenced,
1554 * so that ip_output() only scribbles on the copy.
1556 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1557 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1558 mb_copy
= m_pullup(mb_copy
, hlen
);
1559 if (mb_copy
== NULL
)
1562 if (vifp
->v_rate_limit
== 0)
1563 tbf_send_packet(vifp
, mb_copy
);
1565 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1569 encap_send(ip
, vifp
, m
)
1570 register struct ip
*ip
;
1571 register struct vif
*vifp
;
1572 register struct mbuf
*m
;
1574 register struct mbuf
*mb_copy
;
1575 register struct ip
*ip_copy
;
1576 register int i
, len
= ip
->ip_len
;
1579 * copy the old packet & pullup its IP header into the
1580 * new mbuf so we can modify it. Try to fill the new
1581 * mbuf since if we don't the ethernet driver will.
1583 MGETHDR(mb_copy
, M_DONTWAIT
, MT_HEADER
);
1584 if (mb_copy
== NULL
)
1586 mb_copy
->m_data
+= max_linkhdr
;
1587 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1589 if ((mb_copy
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1593 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1596 mb_copy
= m_pullup(mb_copy
, i
);
1597 if (mb_copy
== NULL
)
1599 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1602 * fill in the encapsulating IP header.
1604 ip_copy
= mtod(mb_copy
, struct ip
*);
1605 *ip_copy
= multicast_encap_iphdr
;
1607 ip_copy
->ip_id
= ip_randomid();
1609 ip_copy
->ip_id
= htons(ip_id
++);
1611 ip_copy
->ip_len
+= len
;
1612 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1613 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1616 * turn the encapsulated IP header back into a valid one.
1618 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1623 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1624 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1625 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1627 if (vifp
->v_rate_limit
== 0)
1628 tbf_send_packet(vifp
, mb_copy
);
1630 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1634 * De-encapsulate a packet and feed it back through ip input (this
1635 * routine is called whenever IP gets a packet with proto type
1636 * ENCAP_PROTO and a local destination address).
1640 X_ipip_input(m
, iphlen
)
1642 ipip_input(m
, iphlen
)
1644 register struct mbuf
*m
;
1647 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1648 register struct ip
*ip
= mtod(m
, struct ip
*);
1649 register int hlen
= ip
->ip_hl
<< 2;
1650 register struct vif
*vifp
;
1652 if (!have_encap_tunnel
) {
1653 rip_input(m
, iphlen
);
1657 * dump the packet if it's not to a multicast destination or if
1658 * we don't have an encapsulating tunnel with the source.
1659 * Note: This code assumes that the remote site IP address
1660 * uniquely identifies the tunnel (i.e., that this site has
1661 * at most one tunnel with the remote site).
1663 if (! IN_MULTICAST(ntohl(((struct ip
*)((char *)ip
+ hlen
))->ip_dst
.s_addr
))) {
1664 ++mrtstat
.mrts_bad_tunnel
;
1668 if (ip
->ip_src
.s_addr
!= last_encap_src
) {
1669 register struct vif
*vife
;
1672 vife
= vifp
+ numvifs
;
1673 last_encap_src
= ip
->ip_src
.s_addr
;
1675 for ( ; vifp
< vife
; ++vifp
)
1676 if (vifp
->v_rmt_addr
.s_addr
== ip
->ip_src
.s_addr
) {
1677 if ((vifp
->v_flags
& (VIFF_TUNNEL
|VIFF_SRCRT
))
1679 last_encap_vif
= vifp
;
1683 if ((vifp
= last_encap_vif
) == 0) {
1685 mrtstat
.mrts_cant_tunnel
++; /*XXX*/
1688 log(LOG_DEBUG
, "ip_mforward: no tunnel with %lx\n",
1689 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1694 if (hlen
> IP_HDR_LEN
)
1695 ip_stripoptions(m
, (struct mbuf
*) 0);
1696 m
->m_data
+= IP_HDR_LEN
;
1697 m
->m_len
-= IP_HDR_LEN
;
1698 m
->m_pkthdr
.len
-= IP_HDR_LEN
;
1699 m
->m_pkthdr
.rcvif
= ifp
;
1701 proto_inject(PF_INET
, m
);
1705 * Token bucket filter module
1709 tbf_control(vifp
, m
, ip
, p_len
)
1710 register struct vif
*vifp
;
1711 register struct mbuf
*m
;
1712 register struct ip
*ip
;
1713 register u_long p_len
;
1715 register struct tbf
*t
= vifp
->v_tbf
;
1717 if (p_len
> MAX_BKT_SIZE
) {
1718 /* drop if packet is too large */
1719 mrtstat
.mrts_pkt2large
++;
1724 tbf_update_tokens(vifp
);
1726 /* if there are enough tokens,
1727 * and the queue is empty,
1728 * send this packet out
1731 if (t
->tbf_q_len
== 0) {
1732 /* queue empty, send packet if enough tokens */
1733 if (p_len
<= t
->tbf_n_tok
) {
1734 t
->tbf_n_tok
-= p_len
;
1735 tbf_send_packet(vifp
, m
);
1737 /* queue packet and timeout till later */
1739 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1741 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1742 /* finite queue length, so queue pkts and process queue */
1744 tbf_process_q(vifp
);
1746 /* queue length too much, try to dq and queue and process */
1747 if (!tbf_dq_sel(vifp
, ip
)) {
1748 mrtstat
.mrts_q_overflow
++;
1753 tbf_process_q(vifp
);
1760 * adds a packet to the queue at the interface
1764 register struct vif
*vifp
;
1765 register struct mbuf
*m
;
1767 register int s
= splnet();
1768 register struct tbf
*t
= vifp
->v_tbf
;
1770 if (t
->tbf_t
== NULL
) {
1771 /* Queue was empty */
1774 /* Insert at tail */
1775 t
->tbf_t
->m_act
= m
;
1778 /* Set new tail pointer */
1782 /* Make sure we didn't get fed a bogus mbuf */
1784 panic("tbf_queue: m_act");
1795 * processes the queue at the interface
1799 register struct vif
*vifp
;
1801 register struct mbuf
*m
;
1803 register int s
= splnet();
1804 register struct tbf
*t
= vifp
->v_tbf
;
1806 /* loop through the queue at the interface and send as many packets
1809 while (t
->tbf_q_len
> 0) {
1812 len
= mtod(m
, struct ip
*)->ip_len
;
1814 /* determine if the packet can be sent */
1815 if (len
<= t
->tbf_n_tok
) {
1817 * reduce no of tokens, dequeue the packet,
1820 t
->tbf_n_tok
-= len
;
1822 t
->tbf_q
= m
->m_act
;
1823 if (--t
->tbf_q_len
== 0)
1827 tbf_send_packet(vifp
, m
);
1835 tbf_reprocess_q(xvifp
)
1838 register struct vif
*vifp
= xvifp
;
1840 if (ip_mrouter
== NULL
) {
1844 tbf_update_tokens(vifp
);
1846 tbf_process_q(vifp
);
1848 if (vifp
->v_tbf
->tbf_q_len
)
1849 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1852 /* function that will selectively discard a member of the queue
1853 * based on the precedence value and the priority
1856 tbf_dq_sel(vifp
, ip
)
1857 register struct vif
*vifp
;
1858 register struct ip
*ip
;
1860 register int s
= splnet();
1862 register struct mbuf
*m
, *last
;
1863 register struct mbuf
**np
;
1864 register struct tbf
*t
= vifp
->v_tbf
;
1866 p
= priority(vifp
, ip
);
1870 while ((m
= *np
) != NULL
) {
1871 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1873 /* If we're removing the last packet, fix the tail pointer */
1877 /* it's impossible for the queue to be empty, but
1878 * we check anyway. */
1879 if (--t
->tbf_q_len
== 0)
1882 mrtstat
.mrts_drop_sel
++;
1893 tbf_send_packet(vifp
, m
)
1894 register struct vif
*vifp
;
1895 register struct mbuf
*m
;
1897 struct ip_moptions imo
;
1899 static struct route ro
;
1902 if (vifp
->v_flags
& VIFF_TUNNEL
) {
1903 /* If tunnel options */
1904 ip_output(m
, (struct mbuf
*)0, &vifp
->v_route
,
1905 IP_FORWARDING
, (struct ip_moptions
*)0);
1907 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1908 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1909 imo
.imo_multicast_loop
= 1;
1910 imo
.imo_multicast_vif
= -1;
1913 * Re-entrancy should not be a problem here, because
1914 * the packets that we send out and are looped back at us
1915 * should get rejected because they appear to come from
1916 * the loopback interface, thus preventing looping.
1918 error
= ip_output(m
, (struct mbuf
*)0, &ro
,
1919 IP_FORWARDING
, &imo
);
1921 if (mrtdebug
& DEBUG_XMIT
)
1922 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1923 vifp
- viftable
, error
);
1928 /* determine the current time and then
1929 * the elapsed time (between the last time and time now)
1930 * in milliseconds & update the no. of tokens in the bucket
1933 tbf_update_tokens(vifp
)
1934 register struct vif
*vifp
;
1938 register int s
= splnet();
1939 register struct tbf
*t
= vifp
->v_tbf
;
1943 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1946 * This formula is actually
1947 * "time in seconds" * "bytes/second".
1949 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1951 * The (1000/1024) was introduced in add_vif to optimize
1952 * this divide into a shift.
1954 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1955 t
->tbf_last_pkt_t
= tp
;
1957 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1958 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1965 register struct vif
*vifp
;
1966 register struct ip
*ip
;
1970 /* temporary hack; may add general packet classifier some day */
1973 * The UDP port space is divided up into four priority ranges:
1974 * [0, 16384) : unclassified - lowest priority
1975 * [16384, 32768) : audio - highest priority
1976 * [32768, 49152) : whiteboard - medium priority
1977 * [49152, 65536) : video - low priority
1979 if (ip
->ip_p
== IPPROTO_UDP
) {
1980 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
1981 switch (ntohs(udp
->uh_dport
) & 0xc000) {
1996 log(LOG_DEBUG
, "port %x prio%d\n", ntohs(udp
->uh_dport
), prio
);
2004 * End of token bucket filter modifications
2008 ip_rsvp_vif_init(so
, sopt
)
2010 struct sockopt
*sopt
;
2015 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
2016 so
->so_type
, so
->so_proto
->pr_protocol
);
2018 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2022 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2027 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i
, rsvp_on
);
2032 if (!legal_vif_num(i
)) {
2034 return EADDRNOTAVAIL
;
2037 /* Check if socket is available. */
2038 if (viftable
[i
].v_rsvpd
!= NULL
) {
2043 viftable
[i
].v_rsvpd
= so
;
2044 /* This may seem silly, but we need to be sure we don't over-increment
2045 * the RSVP counter, in case something slips up.
2047 if (!viftable
[i
].v_rsvp_on
) {
2048 viftable
[i
].v_rsvp_on
= 1;
2057 ip_rsvp_vif_done(so
, sopt
)
2059 struct sockopt
*sopt
;
2064 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
2065 so
->so_type
, so
->so_proto
->pr_protocol
);
2067 if (so
->so_type
!= SOCK_RAW
||
2068 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2071 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
2078 if (!legal_vif_num(i
)) {
2080 return EADDRNOTAVAIL
;
2084 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
2085 viftable
[i
].v_rsvpd
, so
);
2087 viftable
[i
].v_rsvpd
= NULL
;
2089 * This may seem silly, but we need to be sure we don't over-decrement
2090 * the RSVP counter, in case something slips up.
2092 if (viftable
[i
].v_rsvp_on
) {
2093 viftable
[i
].v_rsvp_on
= 0;
2102 ip_rsvp_force_done(so
)
2108 /* Don't bother if it is not the right type of socket. */
2109 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2114 /* The socket may be attached to more than one vif...this
2115 * is perfectly legal.
2117 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2118 if (viftable
[vifi
].v_rsvpd
== so
) {
2119 viftable
[vifi
].v_rsvpd
= NULL
;
2120 /* This may seem silly, but we need to be sure we don't
2121 * over-decrement the RSVP counter, in case something slips up.
2123 if (viftable
[vifi
].v_rsvp_on
) {
2124 viftable
[vifi
].v_rsvp_on
= 0;
2135 rsvp_input(m
, iphlen
)
2140 register struct ip
*ip
= mtod(m
, struct ip
*);
2141 static struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
};
2146 printf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2148 /* Can still get packets with rsvp_on = 0 if there is a local member
2149 * of the group to which the RSVP packet is addressed. But in this
2150 * case we want to throw the packet away.
2160 printf("rsvp_input: check vifs\n");
2163 if (!(m
->m_flags
& M_PKTHDR
))
2164 panic("rsvp_input no hdr");
2167 ifp
= m
->m_pkthdr
.rcvif
;
2168 /* Find which vif the packet arrived on. */
2169 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2170 if (viftable
[vifi
].v_ifp
== ifp
)
2173 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2175 * If the old-style non-vif-associated socket is set,
2176 * then use it. Otherwise, drop packet since there
2177 * is no specific socket for this vif.
2179 if (ip_rsvpd
!= NULL
) {
2181 printf("rsvp_input: Sending packet up old-style socket\n");
2182 rip_input(m
, iphlen
); /* xxx */
2184 if (rsvpdebug
&& vifi
== numvifs
)
2185 printf("rsvp_input: Can't find vif for packet.\n");
2186 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2187 printf("rsvp_input: No socket defined for vif %d\n",vifi
);
2193 rsvp_src
.sin_addr
= ip
->ip_src
;
2196 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n",
2197 m
->m_len
,sbspace(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2199 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2201 printf("rsvp_input: Failed to append to socket\n");
2204 printf("rsvp_input: send packet up\n");
2211 #include <sys/conf.h>
2212 #include <sys/exec.h>
2213 #include <sys/sysent.h>
2214 #include <sys/lkm.h>
2216 MOD_MISC("ip_mroute_mod")
2219 ip_mroute_mod_handle(struct lkm_table
*lkmtp
, int cmd
)
2222 struct lkm_misc
*args
= lkmtp
->private.lkm_misc
;
2226 static int (*old_ip_mrouter_cmd
)();
2227 static int (*old_ip_mrouter_done
)();
2228 static int (*old_ip_mforward
)();
2229 static int (*old_mrt_ioctl
)();
2230 static void (*old_proto4_input
)();
2231 static int (*old_legal_vif_num
)();
2232 extern struct protosw inetsw
[];
2235 if(lkmexists(lkmtp
) || ip_mrtproto
)
2237 old_ip_mrouter_cmd
= ip_mrouter_cmd
;
2238 ip_mrouter_cmd
= X_ip_mrouter_cmd
;
2239 old_ip_mrouter_done
= ip_mrouter_done
;
2240 ip_mrouter_done
= X_ip_mrouter_done
;
2241 old_ip_mforward
= ip_mforward
;
2242 ip_mforward
= X_ip_mforward
;
2243 old_mrt_ioctl
= mrt_ioctl
;
2244 mrt_ioctl
= X_mrt_ioctl
;
2245 old_proto4_input
= ip_protox
[ENCAP_PROTO
]->pr_input
;
2246 ip_protox
[ENCAP_PROTO
]->pr_input
= X_ipip_input
;
2247 old_legal_vif_num
= legal_vif_num
;
2248 legal_vif_num
= X_legal_vif_num
;
2249 ip_mrtproto
= IGMP_DVMRP
;
2251 printf("\nIP multicast routing loaded\n");
2258 ip_mrouter_cmd
= old_ip_mrouter_cmd
;
2259 ip_mrouter_done
= old_ip_mrouter_done
;
2260 ip_mforward
= old_ip_mforward
;
2261 mrt_ioctl
= old_mrt_ioctl
;
2262 ip_protox
[ENCAP_PROTO
]->pr_input
= old_proto4_input
;
2263 legal_vif_num
= old_legal_vif_num
;
2276 ip_mroute_mod(struct lkm_table
*lkmtp
, int cmd
, int ver
) {
2277 DISPATCH(lkmtp
, cmd
, ver
, ip_mroute_mod_handle
, ip_mroute_mod_handle
,
2281 #endif /* MROUTE_LKM */
2282 #endif /* MROUTING */