]>
git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/ip_mroute.c
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
30 * support for mandatory and extensible security protections. This notice
31 * is included in support of clause 2.2 (b) of the Apple Public License,
35 * IP multicast forwarding procedures
37 * Written by David Waitzman, BBN Labs, August 1988.
38 * Modified by Steve Deering, Stanford, February 1989.
39 * Modified by Mark J. Steiglitz, Stanford, May, 1991
40 * Modified by Van Jacobson, LBL, January 1993
41 * Modified by Ajit Thyagarajan, PARC, August 1993
42 * Modified by Bill Fenner, PARC, April 1995
44 * MROUTING Revision: 3.5
45 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.2 2001/07/19 06:37:26 kris Exp $
49 #include <sys/param.h>
50 #include <sys/systm.h>
51 #include <sys/malloc.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
55 #include <sys/protosw.h>
57 #include <sys/kernel.h>
58 #include <sys/sockio.h>
59 #include <sys/syslog.h>
61 #include <net/route.h>
62 #include <net/kpi_protocol.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/in_var.h>
68 #include <netinet/igmp.h>
69 #include <netinet/ip_mroute.h>
70 #include <netinet/udp.h>
73 #include <security/mac_framework.h>
77 #if BYTE_ORDER != BIG_ENDIAN
78 #define NTOHL(d) ((d) = ntohl((d)))
79 #define NTOHS(d) ((d) = ntohs((u_short)(d)))
80 #define HTONL(d) ((d) = htonl((d)))
81 #define HTONS(d) ((d) = htons((u_short)(d)))
91 extern u_long
_ip_mcast_src(int vifi
);
92 extern int _ip_mforward(struct ip
*ip
, struct ifnet
*ifp
,
93 struct mbuf
*m
, struct ip_moptions
*imo
);
94 extern int _ip_mrouter_done(void);
95 extern int _ip_mrouter_get(struct socket
*so
, struct sockopt
*sopt
);
96 extern int _ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
);
97 extern int _mrt_ioctl(int req
, caddr_t data
, struct proc
*p
);
100 * Dummy routines and globals used when multicast routing is not compiled in.
103 struct socket
*ip_mrouter
= NULL
;
107 _ip_mrouter_set(__unused
struct socket
*so
,
108 __unused
struct sockopt
*sopt
)
113 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = _ip_mrouter_set
;
117 _ip_mrouter_get(__unused
struct socket
*so
,
118 __unused sockopt
*sopt
)
123 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = _ip_mrouter_get
;
126 _ip_mrouter_done(void)
131 int (*ip_mrouter_done
)(void) = _ip_mrouter_done
;
134 _ip_mforward(__unused
struct ip
*ip
, __unused
struct ifnet
*ifp
,
135 __unused
struct mbuf
*m
, __unused ip_moptions
*imo
)
140 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
141 struct ip_moptions
*) = _ip_mforward
;
144 _mrt_ioctl(__unused
int req
, __unused caddr_t data
, __unused
struct proc
*p
)
149 int (*mrt_ioctl
)(int, caddr_t
, struct proc
*) = _mrt_ioctl
;
152 rsvp_input(struct mbuf
*m
, int iphlen
) /* XXX must fixup manually */
154 /* Can still get packets with rsvp_on = 0 if there is a local member
155 * of the group to which the RSVP packet is addressed. But in this
156 * case we want to throw the packet away.
163 if (ip_rsvpd
!= NULL
) {
165 printf("rsvp_input: Sending packet up old-style socket\n");
166 rip_input(m
, iphlen
);
169 /* Drop the packet */
173 void ipip_input(struct mbuf
*m
, int iphlen
) { /* XXX must fixup manually */
174 rip_input(m
, iphlen
);
177 int (*legal_vif_num
)(int) = 0;
180 * This should never be called, since IP_MULTICAST_VIF should fail, but
181 * just in case it does get called, the code a little lower in ip_output
182 * will assign the packet a local address.
185 _ip_mcast_src(int vifi
) { return INADDR_ANY
; }
186 u_long (*ip_mcast_src
)(int) = _ip_mcast_src
;
189 ip_rsvp_vif_init(so
, sopt
)
191 struct sockopt
*sopt
;
197 ip_rsvp_vif_done(so
, sopt
)
199 struct sockopt
*sopt
;
205 ip_rsvp_force_done(so
)
213 #define M_HASCL(m) ((m)->m_flags & M_EXT)
215 #define INSIZ sizeof(struct in_addr)
216 #define same(a1, a2) \
217 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0)
221 * Globals. All but ip_mrouter and ip_mrtproto could be static,
222 * except for netstat or debugging purposes.
225 struct socket
*ip_mrouter
= NULL
;
226 static struct mrtstat mrtstat
;
227 #else /* MROUTE_LKM */
228 extern void X_ipip_input(struct mbuf
*m
, int iphlen
);
229 extern struct mrtstat mrtstat
;
230 static int ip_mrtproto
;
233 #define NO_RTE_FOUND 0x1
234 #define RTE_FOUND 0x2
236 static struct mfc
*mfctable
[CONFIG_MFCTBLSIZ
];
237 static u_char nexpire
[CONFIG_MFCTBLSIZ
];
238 static struct vif viftable
[CONFIG_MAXVIFS
];
239 static u_int mrtdebug
= 0; /* debug level */
240 #define DEBUG_MFC 0x02
241 #define DEBUG_FORWARD 0x04
242 #define DEBUG_EXPIRE 0x08
243 #define DEBUG_XMIT 0x10
244 static u_int tbfdebug
= 0; /* tbf debug level */
245 static u_int rsvpdebug
= 0; /* rsvp debug level */
247 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */
248 #define UPCALL_EXPIRE 6 /* number of timeouts */
251 * Define the token bucket filter structures
252 * tbftable -> each vif has one of these for storing info
255 static struct tbf tbftable
[CONFIG_MAXVIFS
];
256 #define TBF_REPROCESS (hz / 100) /* 100x / second */
259 * 'Interfaces' associated with decapsulator (so we can tell
260 * packets that went through it from ones that get reflected
261 * by a broken gateway). These interfaces are never linked into
262 * the system ifnet list & no routes point to them. I.e., packets
263 * can't be sent this way. They only exist as a placeholder for
264 * multicast source verification.
266 static struct ifnet multicast_decap_if
[CONFIG_MAXVIFS
];
269 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */
271 /* prototype IP hdr for encapsulated packets */
272 static struct ip multicast_encap_iphdr
= {
273 #if BYTE_ORDER == LITTLE_ENDIAN
274 sizeof(struct ip
) >> 2, IPVERSION
,
276 IPVERSION
, sizeof(struct ip
) >> 2,
279 sizeof(struct ip
), /* total length */
282 ENCAP_TTL
, ENCAP_PROTO
,
290 static vifi_t numvifs
= 0;
291 static int have_encap_tunnel
= 0;
294 * one-back cache used by ipip_input to locate a tunnel's vif
295 * given a datagram's src ip address.
297 static u_long last_encap_src
;
298 static struct vif
*last_encap_vif
;
300 static u_long
X_ip_mcast_src(int vifi
);
301 static int X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
, struct ip_moptions
*imo
);
302 static int X_ip_mrouter_done(void);
303 static int X_ip_mrouter_get(struct socket
*so
, struct sockopt
*m
);
304 static int X_ip_mrouter_set(struct socket
*so
, struct sockopt
*m
);
305 static int X_legal_vif_num(int vif
);
306 static int X_mrt_ioctl(int cmd
, caddr_t data
);
308 static int get_sg_cnt(struct sioc_sg_req
*);
309 static int get_vif_cnt(struct sioc_vif_req
*);
310 static int ip_mrouter_init(struct socket
*, int);
311 static int add_vif(struct vifctl
*);
312 static int del_vif(vifi_t
);
313 static int add_mfc(struct mfcctl
*);
314 static int del_mfc(struct mfcctl
*);
315 static int socket_send(struct socket
*, struct mbuf
*, struct sockaddr_in
*);
316 static int set_assert(int);
317 static void expire_upcalls(void *);
318 static int ip_mdq(struct mbuf
*, struct ifnet
*, struct mfc
*,
320 static void phyint_send(struct ip
*, struct vif
*, struct mbuf
*);
321 static void encap_send(struct ip
*, struct vif
*, struct mbuf
*);
322 static void tbf_control(struct vif
*, struct mbuf
*, struct ip
*, u_long
);
323 static void tbf_queue(struct vif
*, struct mbuf
*);
324 static void tbf_process_q(struct vif
*);
325 static void tbf_reprocess_q(void *);
326 static int tbf_dq_sel(struct vif
*, struct ip
*);
327 static void tbf_send_packet(struct vif
*, struct mbuf
*);
328 static void tbf_update_tokens(struct vif
*);
329 static int priority(struct vif
*, struct ip
*);
330 void multiencap_decap(struct mbuf
*);
333 * whether or not special PIM assert processing is enabled.
335 static int pim_assert
;
337 * Rate limit for assert notification messages, in usec
339 #define ASSERT_MSG_TIME 3000000
342 * Hash function for a source, group entry
344 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \
345 ((g) >> 20) ^ ((g) >> 10) ^ (g))
348 * Find a route for a given origin IP address and Multicast group address
349 * Type of service parameter to be added in the future!!!
352 #define MFCFIND(o, g, rt) { \
353 struct mfc *_rt = mfctable[MFCHASH(o,g)]; \
355 ++mrtstat.mrts_mfc_lookups; \
357 if ((_rt->mfc_origin.s_addr == o) && \
358 (_rt->mfc_mcastgrp.s_addr == g) && \
359 (_rt->mfc_stall == NULL)) { \
363 _rt = _rt->mfc_next; \
366 ++mrtstat.mrts_mfc_misses; \
372 * Macros to compute elapsed time efficiently
373 * Borrowed from Van Jacobson's scheduling code
375 #define TV_DELTA(a, b, delta) { \
378 delta = (a).tv_usec - (b).tv_usec; \
379 if ((xxs = (a).tv_sec - (b).tv_sec)) { \
388 delta += (1000000 * xxs); \
393 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \
394 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec)
397 u_long upcall_data
[51];
398 static void collate(struct timeval
*);
399 #endif /* UPCALL_TIMING */
403 * Handle MRT setsockopt commands to modify the multicast routing tables.
406 X_ip_mrouter_set(struct socket
*so
, struct sockopt
*sopt
)
413 if (so
!= ip_mrouter
&& sopt
->sopt_name
!= MRT_INIT
)
417 switch (sopt
->sopt_name
) {
419 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
423 error
= ip_mrouter_init(so
, optval
);
427 error
= ip_mrouter_done();
431 error
= sooptcopyin(sopt
, &vifc
, sizeof vifc
, sizeof vifc
);
434 error
= add_vif(&vifc
);
438 error
= sooptcopyin(sopt
, &vifi
, sizeof vifi
, sizeof vifi
);
441 error
= del_vif(vifi
);
446 error
= sooptcopyin(sopt
, &mfc
, sizeof mfc
, sizeof mfc
);
449 if (sopt
->sopt_name
== MRT_ADD_MFC
)
450 error
= add_mfc(&mfc
);
452 error
= del_mfc(&mfc
);
456 error
= sooptcopyin(sopt
, &optval
, sizeof optval
,
470 #if !defined(MROUTE_LKM) || !MROUTE_LKM
471 int (*ip_mrouter_set
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_set
;
475 * Handle MRT getsockopt commands
478 X_ip_mrouter_get(__unused
struct socket
*so
, struct sockopt
*sopt
)
481 static int vers
= 0x0305; /* !!! why is this here? XXX */
483 switch (sopt
->sopt_name
) {
485 error
= sooptcopyout(sopt
, &vers
, sizeof vers
);
489 error
= sooptcopyout(sopt
, &pim_assert
, sizeof pim_assert
);
498 #if !defined(MROUTE_LKM) || !MROUTE_LKM
499 int (*ip_mrouter_get
)(struct socket
*, struct sockopt
*) = X_ip_mrouter_get
;
503 * Handle ioctl commands to obtain information from the cache
506 X_mrt_ioctl(int cmd
, caddr_t data
)
511 case (SIOCGETVIFCNT
):
512 return (get_vif_cnt((struct sioc_vif_req
*)data
));
515 return (get_sg_cnt((struct sioc_sg_req
*)data
));
524 #if !defined(MROUTE_LKM) || !MROUTE_LKM
525 int (*mrt_ioctl
)(int, caddr_t
) = X_mrt_ioctl
;
529 * returns the packet, byte, rpf-failure count for the source group provided
532 get_sg_cnt(struct sioc_sg_req
*req
)
536 MFCFIND(req
->src
.s_addr
, req
->grp
.s_addr
, rt
);
538 req
->pktcnt
= rt
->mfc_pkt_cnt
;
539 req
->bytecnt
= rt
->mfc_byte_cnt
;
540 req
->wrong_if
= rt
->mfc_wrong_if
;
542 req
->pktcnt
= req
->bytecnt
= req
->wrong_if
= 0xffffffff;
548 * returns the input and output packet and byte counts on the vif provided
551 get_vif_cnt(struct sioc_vif_req
*req
)
553 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(struct socket
*so
, int vers
)
572 log(LOG_DEBUG
,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n",
573 so
->so_type
, so
->so_proto
->pr_protocol
);
575 if (so
->so_type
!= SOCK_RAW
||
576 so
->so_proto
->pr_protocol
!= IPPROTO_IGMP
) return EOPNOTSUPP
;
581 if (ip_mrouter
!= NULL
) return EADDRINUSE
;
585 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
586 bzero((caddr_t
)nexpire
, sizeof(nexpire
));
590 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
593 log(LOG_DEBUG
, "ip_mrouter_init\n");
599 * Disable multicast routing
602 X_ip_mrouter_done(void)
612 * For each phyint in use, disable promiscuous reception of all IP
615 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
616 if (viftable
[vifi
].v_lcl_addr
.s_addr
!= 0 &&
617 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
618 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
619 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
621 ifp
= viftable
[vifi
].v_ifp
;
625 bzero((caddr_t
)tbftable
, sizeof(tbftable
));
626 bzero((caddr_t
)viftable
, sizeof(viftable
));
630 untimeout(expire_upcalls
, (caddr_t
)NULL
);
633 * Free all multicast forwarding cache entries.
635 for (i
= 0; i
< CONFIG_MFCTBLSIZ
; i
++) {
636 for (rt
= mfctable
[i
]; rt
!= NULL
; ) {
637 struct mfc
*nr
= rt
->mfc_next
;
639 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
640 struct rtdetq
*n
= rte
->next
;
643 FREE(rte
, M_MRTABLE
);
651 bzero((caddr_t
)mfctable
, sizeof(mfctable
));
654 * Reset de-encapsulation cache
657 last_encap_vif
= NULL
;
658 have_encap_tunnel
= 0;
663 log(LOG_DEBUG
, "ip_mrouter_done\n");
668 #if !defined(MROUTE_LKM) || !MROUTE_LKM
669 int (*ip_mrouter_done
)(void) = X_ip_mrouter_done
;
673 * Set PIM assert processing global
678 if ((i
!= 1) && (i
!= 0))
687 * Add a vif to the vif table
690 add_vif(struct vifctl
*vifcp
)
692 struct vif
*vifp
= viftable
+ vifcp
->vifc_vifi
;
693 static struct sockaddr_in sin
= { sizeof sin
, AF_INET
,
694 0 , {0}, {0,0,0,0,0,0,0,0,} };
698 struct tbf
*v_tbf
= tbftable
+ vifcp
->vifc_vifi
;
700 if (vifcp
->vifc_vifi
>= CONFIG_MAXVIFS
) return EINVAL
;
701 if (vifp
->v_lcl_addr
.s_addr
!= 0) return EADDRINUSE
;
703 /* Find the interface with an address in AF_INET family */
704 sin
.sin_addr
= vifcp
->vifc_lcl_addr
;
705 ifa
= ifa_ifwithaddr((struct sockaddr
*)&sin
);
706 if (ifa
== 0) return EADDRNOTAVAIL
;
711 if (vifcp
->vifc_flags
& VIFF_TUNNEL
) {
712 if ((vifcp
->vifc_flags
& VIFF_SRCRT
) == 0) {
714 * An encapsulating tunnel is wanted. Tell ipip_input() to
715 * start paying attention to encapsulated packets.
717 if (have_encap_tunnel
== 0) {
718 have_encap_tunnel
= 1;
719 for (s
= 0; s
< CONFIG_MAXVIFS
; ++s
) {
720 multicast_decap_if
[s
].if_name
= "mdecap";
721 multicast_decap_if
[s
].if_unit
= s
;
722 multicast_decap_if
[s
].if_family
= APPLE_IF_FAM_MDECAP
;
726 * Set interface to fake encapsulator interface
728 ifp
= &multicast_decap_if
[vifcp
->vifc_vifi
];
730 * Prepare cached route entry
732 bzero(&vifp
->v_route
, sizeof(vifp
->v_route
));
734 log(LOG_ERR
, "source routed tunnels not supported\n");
738 /* Make sure the interface supports multicast */
739 if ((ifp
->if_flags
& IFF_MULTICAST
) == 0)
742 /* Enable promiscuous reception of all IP multicasts from the if */
743 error
= if_allmulti(ifp
, 1);
748 /* define parameters for the tbf structure */
750 GET_TIME(vifp
->v_tbf
->tbf_last_pkt_t
);
751 vifp
->v_tbf
->tbf_n_tok
= 0;
752 vifp
->v_tbf
->tbf_q_len
= 0;
753 vifp
->v_tbf
->tbf_max_q_len
= MAXQSIZE
;
754 vifp
->v_tbf
->tbf_q
= vifp
->v_tbf
->tbf_t
= NULL
;
756 vifp
->v_flags
= vifcp
->vifc_flags
;
757 vifp
->v_threshold
= vifcp
->vifc_threshold
;
758 vifp
->v_lcl_addr
= vifcp
->vifc_lcl_addr
;
759 vifp
->v_rmt_addr
= vifcp
->vifc_rmt_addr
;
761 /* scaling up here allows division by 1024 in critical code */
762 vifp
->v_rate_limit
= vifcp
->vifc_rate_limit
* 1024 / 1000;
764 vifp
->v_rsvpd
= NULL
;
765 /* initialize per vif pkt counters */
768 vifp
->v_bytes_in
= 0;
769 vifp
->v_bytes_out
= 0;
771 /* Adjust numvifs up if the vifi is higher than numvifs */
772 if (numvifs
<= vifcp
->vifc_vifi
) numvifs
= vifcp
->vifc_vifi
+ 1;
775 log(LOG_DEBUG
, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n",
777 (u_long
)ntohl(vifcp
->vifc_lcl_addr
.s_addr
),
778 (vifcp
->vifc_flags
& VIFF_TUNNEL
) ? "rmtaddr" : "mask",
779 (u_long
)ntohl(vifcp
->vifc_rmt_addr
.s_addr
),
780 vifcp
->vifc_threshold
,
781 vifcp
->vifc_rate_limit
);
787 * Delete a vif from the vif table
792 struct vif
*vifp
= &viftable
[vifi
];
797 if (vifi
>= numvifs
) return EINVAL
;
798 if (vifp
->v_lcl_addr
.s_addr
== 0) return EADDRNOTAVAIL
;
800 if (!(vifp
->v_flags
& VIFF_TUNNEL
)) {
801 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_family
= AF_INET
;
802 ((struct sockaddr_in
*)&(ifr
.ifr_addr
))->sin_addr
.s_addr
= INADDR_ANY
;
807 if (vifp
== last_encap_vif
) {
813 * Free packets queued at the interface
815 while (vifp
->v_tbf
->tbf_q
) {
816 m
= vifp
->v_tbf
->tbf_q
;
817 vifp
->v_tbf
->tbf_q
= m
->m_act
;
821 bzero((caddr_t
)vifp
->v_tbf
, sizeof(*(vifp
->v_tbf
)));
822 bzero((caddr_t
)vifp
, sizeof (*vifp
));
825 log(LOG_DEBUG
, "del_vif %d, numvifs %d\n", vifi
, numvifs
);
827 /* Adjust numvifs down */
828 for (vifi
= numvifs
; vifi
> 0; vifi
--)
829 if (viftable
[vifi
-1].v_lcl_addr
.s_addr
!= 0) break;
839 add_mfc(struct mfcctl
*mfccp
)
847 MFCFIND(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
, rt
);
849 /* If an entry already exists, just update the fields */
851 if (mrtdebug
& DEBUG_MFC
)
852 log(LOG_DEBUG
,"add_mfc update o %lx g %lx p %x\n",
853 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
854 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
857 rt
->mfc_parent
= mfccp
->mfcc_parent
;
858 for (i
= 0; i
< numvifs
; i
++)
859 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
864 * Find the entry for which the upcall was made and update
866 hash
= MFCHASH(mfccp
->mfcc_origin
.s_addr
, mfccp
->mfcc_mcastgrp
.s_addr
);
867 for (rt
= mfctable
[hash
], nstl
= 0; rt
; rt
= rt
->mfc_next
) {
869 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
870 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
) &&
871 (rt
->mfc_stall
!= NULL
)) {
874 log(LOG_ERR
, "add_mfc %s o %lx g %lx p %x dbx %p\n",
875 "multiple kernel entries",
876 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
877 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
878 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
880 if (mrtdebug
& DEBUG_MFC
)
881 log(LOG_DEBUG
,"add_mfc o %lx g %lx p %x dbg %p\n",
882 (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
883 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
884 mfccp
->mfcc_parent
, (void *)rt
->mfc_stall
);
886 rt
->mfc_origin
= mfccp
->mfcc_origin
;
887 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
888 rt
->mfc_parent
= mfccp
->mfcc_parent
;
889 for (i
= 0; i
< numvifs
; i
++)
890 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
891 /* initialize pkt counters per src-grp */
893 rt
->mfc_byte_cnt
= 0;
894 rt
->mfc_wrong_if
= 0;
895 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
897 rt
->mfc_expire
= 0; /* Don't clean this guy up */
900 /* free packets Qed at the end of this entry */
901 for (rte
= rt
->mfc_stall
; rte
!= NULL
; ) {
902 struct rtdetq
*n
= rte
->next
;
904 ip_mdq(rte
->m
, rte
->ifp
, rt
, -1);
908 #endif /* UPCALL_TIMING */
909 FREE(rte
, M_MRTABLE
);
912 rt
->mfc_stall
= NULL
;
917 * It is possible that an entry is being inserted without an upcall
920 if (mrtdebug
& DEBUG_MFC
)
921 log(LOG_DEBUG
,"add_mfc no upcall h %lu o %lx g %lx p %x\n",
922 hash
, (u_long
)ntohl(mfccp
->mfcc_origin
.s_addr
),
923 (u_long
)ntohl(mfccp
->mfcc_mcastgrp
.s_addr
),
926 for (rt
= mfctable
[hash
]; rt
!= NULL
; rt
= rt
->mfc_next
) {
928 if ((rt
->mfc_origin
.s_addr
== mfccp
->mfcc_origin
.s_addr
) &&
929 (rt
->mfc_mcastgrp
.s_addr
== mfccp
->mfcc_mcastgrp
.s_addr
)) {
931 rt
->mfc_origin
= mfccp
->mfcc_origin
;
932 rt
->mfc_mcastgrp
= mfccp
->mfcc_mcastgrp
;
933 rt
->mfc_parent
= mfccp
->mfcc_parent
;
934 for (i
= 0; i
< numvifs
; i
++)
935 rt
->mfc_ttls
[i
] = mfccp
->mfcc_ttls
[i
];
936 /* initialize pkt counters per src-grp */
938 rt
->mfc_byte_cnt
= 0;
939 rt
->mfc_wrong_if
= 0;
940 rt
->mfc_last_assert
.tv_sec
= rt
->mfc_last_assert
.tv_usec
= 0;
947 /* no upcall, so make a new entry */
948 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
953 /* insert new entry at head of hash chain */
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;
965 rt
->mfc_stall
= NULL
;
967 /* link into table */
968 rt
->mfc_next
= mfctable
[hash
];
977 * collect delay statistics on the upcalls
980 collate(struct timeval
*t
)
990 TV_DELTA(tp
, *t
, delta
);
999 #endif /* UPCALL_TIMING */
1002 * Delete an mfc entry
1005 del_mfc(struct mfcctl
*mfccp
)
1007 struct in_addr origin
;
1008 struct in_addr mcastgrp
;
1013 origin
= mfccp
->mfcc_origin
;
1014 mcastgrp
= mfccp
->mfcc_mcastgrp
;
1015 hash
= MFCHASH(origin
.s_addr
, mcastgrp
.s_addr
);
1017 if (mrtdebug
& DEBUG_MFC
)
1018 log(LOG_DEBUG
,"del_mfc orig %lx mcastgrp %lx\n",
1019 (u_long
)ntohl(origin
.s_addr
), (u_long
)ntohl(mcastgrp
.s_addr
));
1021 nptr
= &mfctable
[hash
];
1022 while ((rt
= *nptr
) != NULL
) {
1023 if (origin
.s_addr
== rt
->mfc_origin
.s_addr
&&
1024 mcastgrp
.s_addr
== rt
->mfc_mcastgrp
.s_addr
&&
1025 rt
->mfc_stall
== NULL
)
1028 nptr
= &rt
->mfc_next
;
1031 return EADDRNOTAVAIL
;
1034 *nptr
= rt
->mfc_next
;
1035 FREE(rt
, M_MRTABLE
);
1041 * Send a message to mrouted on the multicast routing socket
1044 socket_send(struct socket
*s
, struct mbuf
*mm
, struct sockaddr_in
*src
)
1048 if (sbappendaddr(&s
->so_rcv
,
1049 (struct sockaddr
*)src
,
1050 mm
, (struct mbuf
*)0, NULL
) != 0) {
1052 socket_unlock(s
, 1);
1056 socket_unlock(s
, 1);
1062 * IP multicast forwarding function. This function assumes that the packet
1063 * pointed to by "ip" has arrived on (or is about to be sent to) the interface
1064 * pointed to by "ifp", and the packet is to be relayed to other networks
1065 * that have members of the packet's destination IP multicast group.
1067 * The packet is returned unscathed to the caller, unless it is
1068 * erroneous, in which case a non-zero return value tells the caller to
1072 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */
1073 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */
1076 X_ip_mforward(struct ip
*ip
, struct ifnet
*ifp
, struct mbuf
*m
,
1077 struct ip_moptions
*imo
)
1081 static struct sockaddr_in k_igmpsrc
= { sizeof k_igmpsrc
, AF_INET
,
1082 0 , {0}, {0,0,0,0,0,0,0,0,} };
1083 static int srctun
= 0;
1088 if (mrtdebug
& DEBUG_FORWARD
)
1089 log(LOG_DEBUG
, "ip_mforward: src %lx, dst %lx, ifp %p\n",
1090 (u_long
)ntohl(ip
->ip_src
.s_addr
), (u_long
)ntohl(ip
->ip_dst
.s_addr
),
1093 if (ip
->ip_hl
< (IP_HDR_LEN
+ TUNNEL_LEN
) >> 2 ||
1094 (ipoptions
= (u_char
*)(ip
+ 1))[1] != IPOPT_LSRR
) {
1096 * Packet arrived via a physical interface or
1097 * an encapsulated tunnel.
1101 * Packet arrived through a source-route tunnel.
1102 * Source-route tunnels are no longer supported.
1104 if ((srctun
++ % 1000) == 0)
1106 "ip_mforward: received source-routed packet from %lx\n",
1107 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1112 if ((imo
) && ((vifi
= imo
->imo_multicast_vif
) < numvifs
)) {
1113 if (ip
->ip_ttl
< 255)
1114 ip
->ip_ttl
++; /* compensate for -1 in *_send routines */
1115 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1116 vifp
= viftable
+ vifi
;
1117 printf("Sending IPPROTO_RSVP from %x to %x on vif %d (%s%s%d)\n",
1118 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
), vifi
,
1119 (vifp
->v_flags
& VIFF_TUNNEL
) ? "tunnel on " : "",
1120 vifp
->v_ifp
->if_name
, vifp
->v_ifp
->if_unit
);
1122 return (ip_mdq(m
, ifp
, NULL
, vifi
));
1124 if (rsvpdebug
&& ip
->ip_p
== IPPROTO_RSVP
) {
1125 printf("Warning: IPPROTO_RSVP from %x to %x without vif option\n",
1126 ntohl(ip
->ip_src
.s_addr
), ntohl(ip
->ip_dst
.s_addr
));
1128 printf("In fact, no options were specified at all\n");
1132 * Don't forward a packet with time-to-live of zero or one,
1133 * or a packet destined to a local-only group.
1135 if (ip
->ip_ttl
<= 1 ||
1136 ntohl(ip
->ip_dst
.s_addr
) <= INADDR_MAX_LOCAL_GROUP
)
1140 * Determine forwarding vifs from the forwarding cache table
1142 MFCFIND(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, rt
);
1144 /* Entry exists, so forward if necessary */
1146 return (ip_mdq(m
, ifp
, rt
, -1));
1149 * If we don't have a route for packet's origin,
1150 * Make a copy of the packet &
1151 * send message to routing daemon
1157 int hlen
= ip
->ip_hl
<< 2;
1164 mrtstat
.mrts_no_route
++;
1165 if (mrtdebug
& (DEBUG_FORWARD
| DEBUG_MFC
))
1166 log(LOG_DEBUG
, "ip_mforward: no rte s %lx g %lx\n",
1167 (u_long
)ntohl(ip
->ip_src
.s_addr
),
1168 (u_long
)ntohl(ip
->ip_dst
.s_addr
));
1171 * Allocate mbufs early so that we don't do extra work if we are
1172 * just going to fail anyway. Make sure to pullup the header so
1173 * that other people can't step on it.
1175 rte
= (struct rtdetq
*) _MALLOC((sizeof *rte
), M_MRTABLE
, M_NOWAIT
);
1179 mb0
= m_copy(m
, 0, M_COPYALL
);
1180 if (mb0
&& (M_HASCL(mb0
) || mb0
->m_len
< hlen
))
1181 mb0
= m_pullup(mb0
, hlen
);
1183 FREE(rte
, M_MRTABLE
);
1187 /* is there an upcall waiting for this packet? */
1188 hash
= MFCHASH(ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
);
1189 for (rt
= mfctable
[hash
]; rt
; rt
= rt
->mfc_next
) {
1190 if ((ip
->ip_src
.s_addr
== rt
->mfc_origin
.s_addr
) &&
1191 (ip
->ip_dst
.s_addr
== rt
->mfc_mcastgrp
.s_addr
) &&
1192 (rt
->mfc_stall
!= NULL
))
1200 /* no upcall, so make a new entry */
1201 rt
= (struct mfc
*) _MALLOC(sizeof(*rt
), M_MRTABLE
, M_NOWAIT
);
1203 FREE(rte
, M_MRTABLE
);
1207 /* Make a copy of the header to send to the user level process */
1208 mm
= m_copy(mb0
, 0, hlen
);
1210 FREE(rte
, M_MRTABLE
);
1212 FREE(rt
, M_MRTABLE
);
1217 * Send message to routing daemon to install
1218 * a route into the kernel table
1220 k_igmpsrc
.sin_addr
= ip
->ip_src
;
1222 im
= mtod(mm
, struct igmpmsg
*);
1223 im
->im_msgtype
= IGMPMSG_NOCACHE
;
1226 mrtstat
.mrts_upcalls
++;
1228 if (socket_send(ip_mrouter
, mm
, &k_igmpsrc
) < 0) {
1229 log(LOG_WARNING
, "ip_mforward: ip_mrouter socket queue full\n");
1230 ++mrtstat
.mrts_upq_sockfull
;
1231 FREE(rte
, M_MRTABLE
);
1233 FREE(rt
, M_MRTABLE
);
1237 /* insert new entry at head of hash chain */
1238 rt
->mfc_origin
.s_addr
= ip
->ip_src
.s_addr
;
1239 rt
->mfc_mcastgrp
.s_addr
= ip
->ip_dst
.s_addr
;
1240 rt
->mfc_expire
= UPCALL_EXPIRE
;
1242 for (i
= 0; i
< numvifs
; i
++)
1243 rt
->mfc_ttls
[i
] = 0;
1244 rt
->mfc_parent
= -1;
1246 /* link into table */
1247 rt
->mfc_next
= mfctable
[hash
];
1248 mfctable
[hash
] = rt
;
1249 rt
->mfc_stall
= rte
;
1252 /* determine if q has overflowed */
1256 for (p
= &rt
->mfc_stall
; *p
!= NULL
; p
= &(*p
)->next
)
1259 if (npkts
> MAX_UPQ
) {
1260 mrtstat
.mrts_upq_ovflw
++;
1261 FREE(rte
, M_MRTABLE
);
1266 /* Add this entry to the end of the queue */
1281 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1282 int (*ip_mforward
)(struct ip
*, struct ifnet
*, struct mbuf
*,
1283 struct ip_moptions
*) = X_ip_mforward
;
1287 * Clean up the cache entry if upcall is not serviced
1290 expire_upcalls(__unused
void *unused
)
1293 struct mfc
*mfc
, **nptr
;
1296 for (i
= 0; i
< CONFIG_MFCTBLSIZ
; i
++) {
1297 if (nexpire
[i
] == 0)
1299 nptr
= &mfctable
[i
];
1300 for (mfc
= *nptr
; mfc
!= NULL
; mfc
= *nptr
) {
1302 * Skip real cache entries
1303 * Make sure it wasn't marked to not expire (shouldn't happen)
1306 if (mfc
->mfc_stall
!= NULL
&&
1307 mfc
->mfc_expire
!= 0 &&
1308 --mfc
->mfc_expire
== 0) {
1309 if (mrtdebug
& DEBUG_EXPIRE
)
1310 log(LOG_DEBUG
, "expire_upcalls: expiring (%lx %lx)\n",
1311 (u_long
)ntohl(mfc
->mfc_origin
.s_addr
),
1312 (u_long
)ntohl(mfc
->mfc_mcastgrp
.s_addr
));
1314 * drop all the packets
1315 * free the mbuf with the pkt, if, timing info
1317 for (rte
= mfc
->mfc_stall
; rte
; ) {
1318 struct rtdetq
*n
= rte
->next
;
1321 FREE(rte
, M_MRTABLE
);
1324 ++mrtstat
.mrts_cache_cleanups
;
1327 *nptr
= mfc
->mfc_next
;
1328 FREE(mfc
, M_MRTABLE
);
1330 nptr
= &mfc
->mfc_next
;
1334 timeout(expire_upcalls
, (caddr_t
)NULL
, EXPIRE_TIMEOUT
);
1338 * Packet forwarding routine once entry in the cache is made
1341 ip_mdq(struct mbuf
*m
, struct ifnet
*ifp
, struct mfc
*rt
,
1344 struct ip
*ip
= mtod(m
, struct ip
*);
1347 int plen
= ip
->ip_len
;
1350 * Macro to send packet on vif. Since RSVP packets don't get counted on
1351 * input, they shouldn't get counted on output, so statistics keeping is
1354 #define MC_SEND(ip,vifp,m) { \
1355 if ((vifp)->v_flags & VIFF_TUNNEL) \
1356 encap_send((ip), (vifp), (m)); \
1358 phyint_send((ip), (vifp), (m)); \
1362 * If xmt_vif is not -1, send on only the requested vif.
1364 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.)
1366 if (xmt_vif
< numvifs
) {
1367 MC_SEND(ip
, viftable
+ xmt_vif
, m
);
1372 * Don't forward if it didn't arrive from the parent vif for its origin.
1374 vifi
= rt
->mfc_parent
;
1375 if ((vifi
>= numvifs
) || (viftable
[vifi
].v_ifp
!= ifp
)) {
1376 /* came in the wrong interface */
1377 if (mrtdebug
& DEBUG_FORWARD
)
1378 log(LOG_DEBUG
, "wrong if: ifp %p vifi %d vififp %p\n",
1379 (void *)ifp
, vifi
, (void *)viftable
[vifi
].v_ifp
);
1380 ++mrtstat
.mrts_wrong_if
;
1383 * If we are doing PIM assert processing, and we are forwarding
1384 * packets on this interface, and it is a broadcast medium
1385 * interface (and not a tunnel), send a message to the routing daemon.
1387 if (pim_assert
&& rt
->mfc_ttls
[vifi
] &&
1388 (ifp
->if_flags
& IFF_BROADCAST
) &&
1389 !(viftable
[vifi
].v_flags
& VIFF_TUNNEL
)) {
1390 struct sockaddr_in k_igmpsrc
;
1393 int hlen
= ip
->ip_hl
<< 2;
1399 TV_DELTA(rt
->mfc_last_assert
, now
, delta
);
1401 if (delta
> ASSERT_MSG_TIME
) {
1402 mm
= m_copy(m
, 0, hlen
);
1403 if (mm
&& (M_HASCL(mm
) || mm
->m_len
< hlen
))
1404 mm
= m_pullup(mm
, hlen
);
1409 rt
->mfc_last_assert
= now
;
1411 im
= mtod(mm
, struct igmpmsg
*);
1412 im
->im_msgtype
= IGMPMSG_WRONGVIF
;
1416 k_igmpsrc
.sin_addr
= im
->im_src
;
1418 socket_send(ip_mrouter
, mm
, &k_igmpsrc
);
1424 /* If I sourced this packet, it counts as output, else it was input. */
1425 if (ip
->ip_src
.s_addr
== viftable
[vifi
].v_lcl_addr
.s_addr
) {
1426 viftable
[vifi
].v_pkt_out
++;
1427 viftable
[vifi
].v_bytes_out
+= plen
;
1429 viftable
[vifi
].v_pkt_in
++;
1430 viftable
[vifi
].v_bytes_in
+= plen
;
1433 rt
->mfc_byte_cnt
+= plen
;
1436 * For each vif, decide if a copy of the packet should be forwarded.
1438 * - the ttl exceeds the vif's threshold
1439 * - there are group members downstream on interface
1441 for (vifp
= viftable
, vifi
= 0; vifi
< numvifs
; vifp
++, vifi
++)
1442 if ((rt
->mfc_ttls
[vifi
] > 0) &&
1443 (ip
->ip_ttl
> rt
->mfc_ttls
[vifi
])) {
1445 vifp
->v_bytes_out
+= plen
;
1446 MC_SEND(ip
, vifp
, m
);
1453 * check if a vif number is legal/ok. This is used by ip_output, to export
1457 X_legal_vif_num(int vif
)
1459 if (vif
>= 0 && vif
< numvifs
)
1465 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1466 int (*legal_vif_num
)(int) = X_legal_vif_num
;
1470 * Return the local address used by this vif
1473 X_ip_mcast_src(int vifi
)
1475 if (vifi
>= 0 && vifi
< numvifs
)
1476 return viftable
[vifi
].v_lcl_addr
.s_addr
;
1481 #if !defined(MROUTE_LKM) || !MROUTE_LKM
1482 u_long (*ip_mcast_src
)(int) = X_ip_mcast_src
;
1486 phyint_send(struct ip
*ip
, struct vif
*vifp
, struct mbuf
*m
)
1488 struct mbuf
*mb_copy
;
1489 int hlen
= ip
->ip_hl
<< 2;
1492 * Make a new reference to the packet; make sure that
1493 * the IP header is actually copied, not just referenced,
1494 * so that ip_output() only scribbles on the copy.
1496 mb_copy
= m_copy(m
, 0, M_COPYALL
);
1497 if (mb_copy
&& (M_HASCL(mb_copy
) || mb_copy
->m_len
< hlen
))
1498 mb_copy
= m_pullup(mb_copy
, hlen
);
1499 if (mb_copy
== NULL
)
1502 if (vifp
->v_rate_limit
== 0)
1503 tbf_send_packet(vifp
, mb_copy
);
1505 tbf_control(vifp
, mb_copy
, mtod(mb_copy
, struct ip
*), ip
->ip_len
);
1509 encap_send(struct ip
*ip
, struct vif
*vifp
, struct mbuf
*m
)
1511 struct mbuf
*mb_copy
;
1513 int i
, len
= ip
->ip_len
;
1516 * copy the old packet & pullup its IP header into the
1517 * new mbuf so we can modify it. Try to fill the new
1518 * mbuf since if we don't the ethernet driver will.
1520 MGETHDR(mb_copy
, M_DONTWAIT
, MT_HEADER
);
1521 if (mb_copy
== NULL
)
1524 mac_mbuf_label_associate_multicast_encap(m
, vifp
->v_ifp
, mb_copy
);
1526 mb_copy
->m_data
+= max_linkhdr
;
1527 mb_copy
->m_len
= sizeof(multicast_encap_iphdr
);
1529 if ((mb_copy
->m_next
= m_copy(m
, 0, M_COPYALL
)) == NULL
) {
1533 i
= MHLEN
- M_LEADINGSPACE(mb_copy
);
1536 mb_copy
= m_pullup(mb_copy
, i
);
1537 if (mb_copy
== NULL
)
1539 mb_copy
->m_pkthdr
.len
= len
+ sizeof(multicast_encap_iphdr
);
1542 * fill in the encapsulating IP header.
1544 ip_copy
= mtod(mb_copy
, struct ip
*);
1545 *ip_copy
= multicast_encap_iphdr
;
1547 ip_copy
->ip_id
= ip_randomid();
1549 ip_copy
->ip_id
= htons(ip_id
++);
1551 ip_copy
->ip_len
+= len
;
1552 ip_copy
->ip_src
= vifp
->v_lcl_addr
;
1553 ip_copy
->ip_dst
= vifp
->v_rmt_addr
;
1556 * turn the encapsulated IP header back into a valid one.
1558 ip
= (struct ip
*)((caddr_t
)ip_copy
+ sizeof(multicast_encap_iphdr
));
1563 mb_copy
->m_data
+= sizeof(multicast_encap_iphdr
);
1564 ip
->ip_sum
= in_cksum(mb_copy
, ip
->ip_hl
<< 2);
1565 mb_copy
->m_data
-= sizeof(multicast_encap_iphdr
);
1567 if (vifp
->v_rate_limit
== 0)
1568 tbf_send_packet(vifp
, mb_copy
);
1570 tbf_control(vifp
, mb_copy
, ip
, ip_copy
->ip_len
);
1574 * De-encapsulate a packet and feed it back through ip input (this
1575 * routine is called whenever IP gets a packet with proto type
1576 * ENCAP_PROTO and a local destination address).
1580 X_ipip_input(struct mbuf
*m
, int iphlen
)
1582 ipip_input(struct mbuf
*m
, int iphlen
)
1585 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
1586 struct ip
*ip
= mtod(m
, struct ip
*);
1587 int hlen
= ip
->ip_hl
<< 2;
1590 if (!have_encap_tunnel
) {
1591 rip_input(m
, iphlen
);
1595 * dump the packet if it's not to a multicast destination or if
1596 * we don't have an encapsulating tunnel with the source.
1597 * Note: This code assumes that the remote site IP address
1598 * uniquely identifies the tunnel (i.e., that this site has
1599 * at most one tunnel with the remote site).
1601 if (! IN_MULTICAST(ntohl(((struct ip
*)((char *)ip
+ hlen
))->ip_dst
.s_addr
))) {
1602 ++mrtstat
.mrts_bad_tunnel
;
1606 if (ip
->ip_src
.s_addr
!= last_encap_src
) {
1610 vife
= vifp
+ numvifs
;
1611 last_encap_src
= ip
->ip_src
.s_addr
;
1613 for ( ; vifp
< vife
; ++vifp
)
1614 if (vifp
->v_rmt_addr
.s_addr
== ip
->ip_src
.s_addr
) {
1615 if ((vifp
->v_flags
& (VIFF_TUNNEL
|VIFF_SRCRT
))
1617 last_encap_vif
= vifp
;
1621 if ((vifp
= last_encap_vif
) == 0) {
1623 mrtstat
.mrts_cant_tunnel
++; /*XXX*/
1626 log(LOG_DEBUG
, "ip_mforward: no tunnel with %lx\n",
1627 (u_long
)ntohl(ip
->ip_src
.s_addr
));
1632 if (hlen
> IP_HDR_LEN
)
1633 ip_stripoptions(m
, (struct mbuf
*) 0);
1634 m
->m_data
+= IP_HDR_LEN
;
1635 m
->m_len
-= IP_HDR_LEN
;
1636 m
->m_pkthdr
.len
-= IP_HDR_LEN
;
1637 m
->m_pkthdr
.rcvif
= ifp
;
1639 proto_inject(PF_INET
, m
);
1643 * Token bucket filter module
1647 tbf_control(struct vif
*vifp
, struct mbuf
*m
, struct ip
*ip
,
1650 struct tbf
*t
= vifp
->v_tbf
;
1652 if (p_len
> MAX_BKT_SIZE
) {
1653 /* drop if packet is too large */
1654 mrtstat
.mrts_pkt2large
++;
1659 tbf_update_tokens(vifp
);
1661 /* if there are enough tokens,
1662 * and the queue is empty,
1663 * send this packet out
1666 if (t
->tbf_q_len
== 0) {
1667 /* queue empty, send packet if enough tokens */
1668 if (p_len
<= t
->tbf_n_tok
) {
1669 t
->tbf_n_tok
-= p_len
;
1670 tbf_send_packet(vifp
, m
);
1672 /* queue packet and timeout till later */
1674 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1676 } else if (t
->tbf_q_len
< t
->tbf_max_q_len
) {
1677 /* finite queue length, so queue pkts and process queue */
1679 tbf_process_q(vifp
);
1681 /* queue length too much, try to dq and queue and process */
1682 if (!tbf_dq_sel(vifp
, ip
)) {
1683 mrtstat
.mrts_q_overflow
++;
1688 tbf_process_q(vifp
);
1695 * adds a packet to the queue at the interface
1698 tbf_queue(struct vif
*vifp
, struct mbuf
*m
)
1700 struct tbf
*t
= vifp
->v_tbf
;
1702 if (t
->tbf_t
== NULL
) {
1703 /* Queue was empty */
1706 /* Insert at tail */
1707 t
->tbf_t
->m_act
= m
;
1710 /* Set new tail pointer */
1714 /* Make sure we didn't get fed a bogus mbuf */
1716 panic("tbf_queue: m_act");
1725 * processes the queue at the interface
1728 tbf_process_q(struct vif
*vifp
)
1732 struct tbf
*t
= vifp
->v_tbf
;
1734 /* loop through the queue at the interface and send as many packets
1737 while (t
->tbf_q_len
> 0) {
1740 len
= mtod(m
, struct ip
*)->ip_len
;
1742 /* determine if the packet can be sent */
1743 if (len
<= t
->tbf_n_tok
) {
1745 * reduce no of tokens, dequeue the packet,
1748 t
->tbf_n_tok
-= len
;
1750 t
->tbf_q
= m
->m_act
;
1751 if (--t
->tbf_q_len
== 0)
1755 tbf_send_packet(vifp
, m
);
1762 tbf_reprocess_q(void *xvifp
)
1764 struct vif
*vifp
= xvifp
;
1766 if (ip_mrouter
== NULL
) {
1770 tbf_update_tokens(vifp
);
1772 tbf_process_q(vifp
);
1774 if (vifp
->v_tbf
->tbf_q_len
)
1775 timeout(tbf_reprocess_q
, (caddr_t
)vifp
, TBF_REPROCESS
);
1778 /* function that will selectively discard a member of the queue
1779 * based on the precedence value and the priority
1782 tbf_dq_sel(struct vif
*vifp
, struct ip
*ip
)
1785 struct mbuf
*m
, *last
;
1787 struct tbf
*t
= vifp
->v_tbf
;
1789 p
= priority(vifp
, ip
);
1793 while ((m
= *np
) != NULL
) {
1794 if (p
> priority(vifp
, mtod(m
, struct ip
*))) {
1796 /* If we're removing the last packet, fix the tail pointer */
1800 /* it's impossible for the queue to be empty, but
1801 * we check anyway. */
1802 if (--t
->tbf_q_len
== 0)
1804 mrtstat
.mrts_drop_sel
++;
1814 tbf_send_packet(struct vif
*vifp
, struct mbuf
*m
)
1816 struct ip_moptions imo
;
1818 static struct route ro
;
1820 if (vifp
->v_flags
& VIFF_TUNNEL
) {
1821 /* If tunnel options */
1822 ip_output(m
, (struct mbuf
*)0, &vifp
->v_route
,
1823 IP_FORWARDING
, (struct ip_moptions
*)0, NULL
);
1825 imo
.imo_multicast_ifp
= vifp
->v_ifp
;
1826 imo
.imo_multicast_ttl
= mtod(m
, struct ip
*)->ip_ttl
- 1;
1827 imo
.imo_multicast_loop
= 1;
1828 imo
.imo_multicast_vif
= -1;
1831 * Re-entrancy should not be a problem here, because
1832 * the packets that we send out and are looped back at us
1833 * should get rejected because they appear to come from
1834 * the loopback interface, thus preventing looping.
1836 error
= ip_output(m
, (struct mbuf
*)0, &ro
,
1837 IP_FORWARDING
, &imo
, NULL
);
1839 if (mrtdebug
& DEBUG_XMIT
)
1840 log(LOG_DEBUG
, "phyint_send on vif %d err %d\n",
1841 vifp
- viftable
, error
);
1845 /* determine the current time and then
1846 * the elapsed time (between the last time and time now)
1847 * in milliseconds & update the no. of tokens in the bucket
1850 tbf_update_tokens(struct vif
*vifp
)
1854 struct tbf
*t
= vifp
->v_tbf
;
1858 TV_DELTA(tp
, t
->tbf_last_pkt_t
, tm
);
1861 * This formula is actually
1862 * "time in seconds" * "bytes/second".
1864 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8)
1866 * The (1000/1024) was introduced in add_vif to optimize
1867 * this divide into a shift.
1869 t
->tbf_n_tok
+= tm
* vifp
->v_rate_limit
/ 1024 / 8;
1870 t
->tbf_last_pkt_t
= tp
;
1872 if (t
->tbf_n_tok
> MAX_BKT_SIZE
)
1873 t
->tbf_n_tok
= MAX_BKT_SIZE
;
1877 priority(__unused
struct vif
*vifp
, struct ip
*ip
)
1881 /* temporary hack; may add general packet classifier some day */
1884 * The UDP port space is divided up into four priority ranges:
1885 * [0, 16384) : unclassified - lowest priority
1886 * [16384, 32768) : audio - highest priority
1887 * [32768, 49152) : whiteboard - medium priority
1888 * [49152, 65536) : video - low priority
1890 if (ip
->ip_p
== IPPROTO_UDP
) {
1891 struct udphdr
*udp
= (struct udphdr
*)(((char *)ip
) + (ip
->ip_hl
<< 2));
1892 switch (ntohs(udp
->uh_dport
) & 0xc000) {
1907 log(LOG_DEBUG
, "port %x prio%d\n", ntohs(udp
->uh_dport
), prio
);
1915 * End of token bucket filter modifications
1919 ip_rsvp_vif_init(struct socket
*so
, struct sockopt
*sopt
)
1924 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n",
1925 so
->so_type
, so
->so_proto
->pr_protocol
);
1927 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
1931 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
1936 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i
, rsvp_on
);
1939 if (!legal_vif_num(i
)) {
1940 return EADDRNOTAVAIL
;
1943 /* Check if socket is available. */
1944 if (viftable
[i
].v_rsvpd
!= NULL
) {
1948 viftable
[i
].v_rsvpd
= so
;
1949 /* This may seem silly, but we need to be sure we don't over-increment
1950 * the RSVP counter, in case something slips up.
1952 if (!viftable
[i
].v_rsvp_on
) {
1953 viftable
[i
].v_rsvp_on
= 1;
1961 ip_rsvp_vif_done(struct socket
*so
, struct sockopt
*sopt
)
1966 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n",
1967 so
->so_type
, so
->so_proto
->pr_protocol
);
1969 if (so
->so_type
!= SOCK_RAW
||
1970 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
1973 error
= sooptcopyin(sopt
, &i
, sizeof i
, sizeof i
);
1978 if (!legal_vif_num(i
)) {
1979 return EADDRNOTAVAIL
;
1983 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n",
1984 viftable
[i
].v_rsvpd
, so
);
1986 viftable
[i
].v_rsvpd
= NULL
;
1988 * This may seem silly, but we need to be sure we don't over-decrement
1989 * the RSVP counter, in case something slips up.
1991 if (viftable
[i
].v_rsvp_on
) {
1992 viftable
[i
].v_rsvp_on
= 0;
2000 ip_rsvp_force_done(struct socket
*so
)
2004 /* Don't bother if it is not the right type of socket. */
2005 if (so
->so_type
!= SOCK_RAW
|| so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2008 /* The socket may be attached to more than one vif...this
2009 * is perfectly legal.
2011 for (vifi
= 0; vifi
< numvifs
; vifi
++) {
2012 if (viftable
[vifi
].v_rsvpd
== so
) {
2013 viftable
[vifi
].v_rsvpd
= NULL
;
2014 /* This may seem silly, but we need to be sure we don't
2015 * over-decrement the RSVP counter, in case something slips up.
2017 if (viftable
[vifi
].v_rsvp_on
) {
2018 viftable
[vifi
].v_rsvp_on
= 0;
2028 rsvp_input(struct mbuf
*m
, int iphlen
)
2031 struct ip
*ip
= mtod(m
, struct ip
*);
2032 static struct sockaddr_in rsvp_src
= { sizeof rsvp_src
, AF_INET
,
2033 0 , {0}, {0,0,0,0,0,0,0,0,} };
2037 printf("rsvp_input: rsvp_on %d\n",rsvp_on
);
2039 /* Can still get packets with rsvp_on = 0 if there is a local member
2040 * of the group to which the RSVP packet is addressed. But in this
2041 * case we want to throw the packet away.
2049 printf("rsvp_input: check vifs\n");
2052 if (!(m
->m_flags
& M_PKTHDR
))
2053 panic("rsvp_input no hdr");
2056 ifp
= m
->m_pkthdr
.rcvif
;
2057 /* Find which vif the packet arrived on. */
2058 for (vifi
= 0; vifi
< numvifs
; vifi
++)
2059 if (viftable
[vifi
].v_ifp
== ifp
)
2062 if (vifi
== numvifs
|| viftable
[vifi
].v_rsvpd
== NULL
) {
2064 * If the old-style non-vif-associated socket is set,
2065 * then use it. Otherwise, drop packet since there
2066 * is no specific socket for this vif.
2068 if (ip_rsvpd
!= NULL
) {
2070 printf("rsvp_input: Sending packet up old-style socket\n");
2071 rip_input(m
, iphlen
); /* xxx */
2073 if (rsvpdebug
&& vifi
== numvifs
)
2074 printf("rsvp_input: Can't find vif for packet.\n");
2075 else if (rsvpdebug
&& viftable
[vifi
].v_rsvpd
== NULL
)
2076 printf("rsvp_input: No socket defined for vif %d\n",vifi
);
2081 rsvp_src
.sin_addr
= ip
->ip_src
;
2084 printf("rsvp_input: m->m_len = %ld, sbspace() = %ld\n",
2085 m
->m_len
,sbspace(&(viftable
[vifi
].v_rsvpd
->so_rcv
)));
2087 if (socket_send(viftable
[vifi
].v_rsvpd
, m
, &rsvp_src
) < 0) {
2089 printf("rsvp_input: Failed to append to socket\n");
2092 printf("rsvp_input: send packet up\n");
2098 #include <sys/conf.h>
2099 #include <sys/exec.h>
2100 #include <sys/sysent.h>
2101 #include <sys/lkm.h>
2103 MOD_MISC("ip_mroute_mod")
2106 ip_mroute_mod_handle(struct lkm_table
*lkmtp
, int cmd
)
2109 struct lkm_misc
*args
= lkmtp
->private.lkm_misc
;
2113 static int (*old_ip_mrouter_cmd
)();
2114 static int (*old_ip_mrouter_done
)();
2115 static int (*old_ip_mforward
)();
2116 static int (*old_mrt_ioctl
)();
2117 static void (*old_proto4_input
)();
2118 static int (*old_legal_vif_num
)();
2119 extern struct protosw inetsw
[];
2122 if(lkmexists(lkmtp
) || ip_mrtproto
)
2124 old_ip_mrouter_cmd
= ip_mrouter_cmd
;
2125 ip_mrouter_cmd
= X_ip_mrouter_cmd
;
2126 old_ip_mrouter_done
= ip_mrouter_done
;
2127 ip_mrouter_done
= X_ip_mrouter_done
;
2128 old_ip_mforward
= ip_mforward
;
2129 ip_mforward
= X_ip_mforward
;
2130 old_mrt_ioctl
= mrt_ioctl
;
2131 mrt_ioctl
= X_mrt_ioctl
;
2132 old_proto4_input
= ip_protox
[ENCAP_PROTO
]->pr_input
;
2133 ip_protox
[ENCAP_PROTO
]->pr_input
= X_ipip_input
;
2134 old_legal_vif_num
= legal_vif_num
;
2135 legal_vif_num
= X_legal_vif_num
;
2136 ip_mrtproto
= IGMP_DVMRP
;
2138 printf("\nIP multicast routing loaded\n");
2145 ip_mrouter_cmd
= old_ip_mrouter_cmd
;
2146 ip_mrouter_done
= old_ip_mrouter_done
;
2147 ip_mforward
= old_ip_mforward
;
2148 mrt_ioctl
= old_mrt_ioctl
;
2149 ip_protox
[ENCAP_PROTO
]->pr_input
= old_proto4_input
;
2150 legal_vif_num
= old_legal_vif_num
;
2163 ip_mroute_mod(struct lkm_table
*lkmtp
, int cmd
, int ver
) {
2164 DISPATCH(lkmtp
, cmd
, ver
, ip_mroute_mod_handle
, ip_mroute_mod_handle
,
2168 #endif /* MROUTE_LKM */
2169 #endif /* MROUTING */