xref: /xnu-8796.121.2/bsd/net/classq/classq_util.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2007-2011 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions /*
30*c54f35caSApple OSS Distributions  * Copyright (c) 1991-1997 Regents of the University of California.
31*c54f35caSApple OSS Distributions  * All rights reserved.
32*c54f35caSApple OSS Distributions  *
33*c54f35caSApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
34*c54f35caSApple OSS Distributions  * modification, are permitted provided that the following conditions
35*c54f35caSApple OSS Distributions  * are met:
36*c54f35caSApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
37*c54f35caSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
38*c54f35caSApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
39*c54f35caSApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
40*c54f35caSApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
41*c54f35caSApple OSS Distributions  * 3. All advertising materials mentioning features or use of this software
42*c54f35caSApple OSS Distributions  *    must display the following acknowledgement:
43*c54f35caSApple OSS Distributions  *      This product includes software developed by the Network Research
44*c54f35caSApple OSS Distributions  *      Group at Lawrence Berkeley Laboratory.
45*c54f35caSApple OSS Distributions  * 4. Neither the name of the University nor of the Laboratory may be used
46*c54f35caSApple OSS Distributions  *    to endorse or promote products derived from this software without
47*c54f35caSApple OSS Distributions  *    specific prior written permission.
48*c54f35caSApple OSS Distributions  *
49*c54f35caSApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50*c54f35caSApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51*c54f35caSApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52*c54f35caSApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53*c54f35caSApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54*c54f35caSApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55*c54f35caSApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56*c54f35caSApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57*c54f35caSApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58*c54f35caSApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59*c54f35caSApple OSS Distributions  * SUCH DAMAGE.
60*c54f35caSApple OSS Distributions  */
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions #include <sys/cdefs.h>
63*c54f35caSApple OSS Distributions #include <sys/param.h>
64*c54f35caSApple OSS Distributions #include <sys/mbuf.h>
65*c54f35caSApple OSS Distributions #include <sys/errno.h>
66*c54f35caSApple OSS Distributions #include <sys/random.h>
67*c54f35caSApple OSS Distributions #include <sys/kernel_types.h>
68*c54f35caSApple OSS Distributions #include <sys/sysctl.h>
69*c54f35caSApple OSS Distributions 
70*c54f35caSApple OSS Distributions #include <net/if.h>
71*c54f35caSApple OSS Distributions #include <net/net_osdep.h>
72*c54f35caSApple OSS Distributions #include <net/classq/classq.h>
73*c54f35caSApple OSS Distributions 
74*c54f35caSApple OSS Distributions #include <netinet/in.h>
75*c54f35caSApple OSS Distributions #include <netinet/in_systm.h>
76*c54f35caSApple OSS Distributions #include <netinet/ip.h>
77*c54f35caSApple OSS Distributions #include <netinet/ip6.h>
78*c54f35caSApple OSS Distributions #include <netinet/tcp.h>
79*c54f35caSApple OSS Distributions #include <netinet/udp.h>
80*c54f35caSApple OSS Distributions 
81*c54f35caSApple OSS Distributions #include <libkern/libkern.h>
82*c54f35caSApple OSS Distributions 
83*c54f35caSApple OSS Distributions #if PF_ECN
84*c54f35caSApple OSS Distributions /*
85*c54f35caSApple OSS Distributions  * read and write diffserv field in IPv4 or IPv6 header
86*c54f35caSApple OSS Distributions  */
87*c54f35caSApple OSS Distributions u_int8_t
read_dsfield(struct mbuf * m,struct pf_mtag * t)88*c54f35caSApple OSS Distributions read_dsfield(struct mbuf *m, struct pf_mtag *t)
89*c54f35caSApple OSS Distributions {
90*c54f35caSApple OSS Distributions 	struct mbuf *m0;
91*c54f35caSApple OSS Distributions 	u_int8_t ds_field = 0;
92*c54f35caSApple OSS Distributions 
93*c54f35caSApple OSS Distributions 	if (t->pftag_hdr == NULL ||
94*c54f35caSApple OSS Distributions 	    !(t->pftag_flags & (PF_TAG_HDR_INET | PF_TAG_HDR_INET6))) {
95*c54f35caSApple OSS Distributions 		return (u_int8_t)0;
96*c54f35caSApple OSS Distributions 	}
97*c54f35caSApple OSS Distributions 
98*c54f35caSApple OSS Distributions 	/* verify that hdr is within the mbuf data */
99*c54f35caSApple OSS Distributions 	for (m0 = m; m0 != NULL; m0 = m0->m_next) {
100*c54f35caSApple OSS Distributions 		if (((caddr_t)t->pftag_hdr >= m0->m_data) &&
101*c54f35caSApple OSS Distributions 		    ((caddr_t)t->pftag_hdr < m0->m_data + m0->m_len)) {
102*c54f35caSApple OSS Distributions 			break;
103*c54f35caSApple OSS Distributions 		}
104*c54f35caSApple OSS Distributions 	}
105*c54f35caSApple OSS Distributions 	if (m0 == NULL) {
106*c54f35caSApple OSS Distributions 		/* ick, tag info is stale */
107*c54f35caSApple OSS Distributions 		printf("%s: can't locate header!\n", __func__);
108*c54f35caSApple OSS Distributions 		return (u_int8_t)0;
109*c54f35caSApple OSS Distributions 	}
110*c54f35caSApple OSS Distributions 
111*c54f35caSApple OSS Distributions 	if (t->pftag_flags & PF_TAG_HDR_INET) {
112*c54f35caSApple OSS Distributions 		struct ip *ip = (struct ip *)(void *)t->pftag_hdr;
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions 		if (((uintptr_t)ip + sizeof(*ip)) >
115*c54f35caSApple OSS Distributions 		    ((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
116*c54f35caSApple OSS Distributions 			return 0;             /* out of bounds */
117*c54f35caSApple OSS Distributions 		}
118*c54f35caSApple OSS Distributions 		if (ip->ip_v != 4) {
119*c54f35caSApple OSS Distributions 			return (u_int8_t)0;   /* version mismatch! */
120*c54f35caSApple OSS Distributions 		}
121*c54f35caSApple OSS Distributions 		ds_field = ip->ip_tos;
122*c54f35caSApple OSS Distributions 	} else if (t->pftag_flags & PF_TAG_HDR_INET6) {
123*c54f35caSApple OSS Distributions 		struct ip6_hdr *ip6 = (struct ip6_hdr *)(void *)t->pftag_hdr;
124*c54f35caSApple OSS Distributions 		u_int32_t flowlabel;
125*c54f35caSApple OSS Distributions 
126*c54f35caSApple OSS Distributions 		if (((uintptr_t)ip6 + sizeof(*ip6)) >
127*c54f35caSApple OSS Distributions 		    ((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
128*c54f35caSApple OSS Distributions 			return 0;             /* out of bounds */
129*c54f35caSApple OSS Distributions 		}
130*c54f35caSApple OSS Distributions 		flowlabel = ntohl(ip6->ip6_flow);
131*c54f35caSApple OSS Distributions 		if ((flowlabel >> 28) != 6) {
132*c54f35caSApple OSS Distributions 			return (u_int8_t)0;   /* version mismatch! */
133*c54f35caSApple OSS Distributions 		}
134*c54f35caSApple OSS Distributions 		ds_field = (flowlabel >> 20) & 0xff;
135*c54f35caSApple OSS Distributions 	}
136*c54f35caSApple OSS Distributions 	return ds_field;
137*c54f35caSApple OSS Distributions }
138*c54f35caSApple OSS Distributions 
139*c54f35caSApple OSS Distributions void
write_dsfield(struct mbuf * m,struct pf_mtag * t,u_int8_t dsfield)140*c54f35caSApple OSS Distributions write_dsfield(struct mbuf *m, struct pf_mtag *t, u_int8_t dsfield)
141*c54f35caSApple OSS Distributions {
142*c54f35caSApple OSS Distributions 	struct mbuf *m0;
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions 	if (t->pftag_hdr == NULL ||
145*c54f35caSApple OSS Distributions 	    !(t->pftag_flags & (PF_TAG_HDR_INET | PF_TAG_HDR_INET6))) {
146*c54f35caSApple OSS Distributions 		return;
147*c54f35caSApple OSS Distributions 	}
148*c54f35caSApple OSS Distributions 
149*c54f35caSApple OSS Distributions 	/* verify that hdr is within the mbuf data */
150*c54f35caSApple OSS Distributions 	for (m0 = m; m0 != NULL; m0 = m0->m_next) {
151*c54f35caSApple OSS Distributions 		if (((caddr_t)t->pftag_hdr >= m0->m_data) &&
152*c54f35caSApple OSS Distributions 		    ((caddr_t)t->pftag_hdr < m0->m_data + m0->m_len)) {
153*c54f35caSApple OSS Distributions 			break;
154*c54f35caSApple OSS Distributions 		}
155*c54f35caSApple OSS Distributions 	}
156*c54f35caSApple OSS Distributions 	if (m0 == NULL) {
157*c54f35caSApple OSS Distributions 		/* ick, tag info is stale */
158*c54f35caSApple OSS Distributions 		printf("%s: can't locate header!\n", __func__);
159*c54f35caSApple OSS Distributions 		return;
160*c54f35caSApple OSS Distributions 	}
161*c54f35caSApple OSS Distributions 
162*c54f35caSApple OSS Distributions 	if (t->pftag_flags & PF_TAG_HDR_INET) {
163*c54f35caSApple OSS Distributions 		struct ip *ip = (struct ip *)(void *)t->pftag_hdr;
164*c54f35caSApple OSS Distributions 		u_int8_t old;
165*c54f35caSApple OSS Distributions 		int32_t sum;
166*c54f35caSApple OSS Distributions 
167*c54f35caSApple OSS Distributions 		if (((uintptr_t)ip + sizeof(*ip)) >
168*c54f35caSApple OSS Distributions 		    ((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
169*c54f35caSApple OSS Distributions 			return;         /* out of bounds */
170*c54f35caSApple OSS Distributions 		}
171*c54f35caSApple OSS Distributions 		if (ip->ip_v != 4) {
172*c54f35caSApple OSS Distributions 			return;         /* version mismatch! */
173*c54f35caSApple OSS Distributions 		}
174*c54f35caSApple OSS Distributions 		old = ip->ip_tos;
175*c54f35caSApple OSS Distributions 		dsfield |= old & 3;     /* leave CU bits */
176*c54f35caSApple OSS Distributions 		if (old == dsfield) {
177*c54f35caSApple OSS Distributions 			return;
178*c54f35caSApple OSS Distributions 		}
179*c54f35caSApple OSS Distributions 		ip->ip_tos = dsfield;
180*c54f35caSApple OSS Distributions 		/*
181*c54f35caSApple OSS Distributions 		 * update checksum (from RFC1624)
182*c54f35caSApple OSS Distributions 		 *	   HC' = ~(~HC + ~m + m')
183*c54f35caSApple OSS Distributions 		 */
184*c54f35caSApple OSS Distributions 		sum = ~ntohs(ip->ip_sum) & 0xffff;
185*c54f35caSApple OSS Distributions 		sum += 0xff00 + (~old & 0xff) + dsfield;
186*c54f35caSApple OSS Distributions 		sum = (sum >> 16) + (sum & 0xffff);
187*c54f35caSApple OSS Distributions 		sum += (sum >> 16);  /* add carry */
188*c54f35caSApple OSS Distributions 
189*c54f35caSApple OSS Distributions 		ip->ip_sum = htons(~sum & 0xffff);
190*c54f35caSApple OSS Distributions 	} else if (t->pftag_flags & PF_TAG_HDR_INET6) {
191*c54f35caSApple OSS Distributions 		struct ip6_hdr *ip6 = (struct ip6_hdr *)t->pftag_hdr;
192*c54f35caSApple OSS Distributions 		u_int32_t flowlabel;
193*c54f35caSApple OSS Distributions 
194*c54f35caSApple OSS Distributions 		if (((uintptr_t)ip6 + sizeof(*ip6)) >
195*c54f35caSApple OSS Distributions 		    ((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
196*c54f35caSApple OSS Distributions 			return;         /* out of bounds */
197*c54f35caSApple OSS Distributions 		}
198*c54f35caSApple OSS Distributions 		flowlabel = ntohl(ip6->ip6_flow);
199*c54f35caSApple OSS Distributions 		if ((flowlabel >> 28) != 6) {
200*c54f35caSApple OSS Distributions 			return;         /* version mismatch! */
201*c54f35caSApple OSS Distributions 		}
202*c54f35caSApple OSS Distributions 		flowlabel = (flowlabel & 0xf03fffff) | (dsfield << 20);
203*c54f35caSApple OSS Distributions 		ip6->ip6_flow = htonl(flowlabel);
204*c54f35caSApple OSS Distributions 	}
205*c54f35caSApple OSS Distributions }
206*c54f35caSApple OSS Distributions 
207*c54f35caSApple OSS Distributions /*
208*c54f35caSApple OSS Distributions  * try to mark CE bit to the packet.
209*c54f35caSApple OSS Distributions  *    returns 1 if successfully marked, 0 otherwise.
210*c54f35caSApple OSS Distributions  */
211*c54f35caSApple OSS Distributions int
mark_ecn(struct mbuf * m,struct pf_mtag * t,int flags)212*c54f35caSApple OSS Distributions mark_ecn(struct mbuf *m, struct pf_mtag *t, int flags)
213*c54f35caSApple OSS Distributions {
214*c54f35caSApple OSS Distributions 	struct mbuf     *m0;
215*c54f35caSApple OSS Distributions 	void            *hdr;
216*c54f35caSApple OSS Distributions 	int             af;
217*c54f35caSApple OSS Distributions 
218*c54f35caSApple OSS Distributions 	if ((hdr = t->pftag_hdr) == NULL ||
219*c54f35caSApple OSS Distributions 	    !(t->pftag_flags & (PF_TAG_HDR_INET | PF_TAG_HDR_INET6))) {
220*c54f35caSApple OSS Distributions 		return 0;
221*c54f35caSApple OSS Distributions 	}
222*c54f35caSApple OSS Distributions 
223*c54f35caSApple OSS Distributions 	/* verify that hdr is within the mbuf data */
224*c54f35caSApple OSS Distributions 	for (m0 = m; m0 != NULL; m0 = m0->m_next) {
225*c54f35caSApple OSS Distributions 		if (((caddr_t)hdr >= m0->m_data) &&
226*c54f35caSApple OSS Distributions 		    ((caddr_t)hdr < m0->m_data + m0->m_len)) {
227*c54f35caSApple OSS Distributions 			break;
228*c54f35caSApple OSS Distributions 		}
229*c54f35caSApple OSS Distributions 	}
230*c54f35caSApple OSS Distributions 	if (m0 == NULL) {
231*c54f35caSApple OSS Distributions 		/* ick, tag info is stale */
232*c54f35caSApple OSS Distributions 		printf("%s: can't locate header!\n", __func__);
233*c54f35caSApple OSS Distributions 		return 0;
234*c54f35caSApple OSS Distributions 	}
235*c54f35caSApple OSS Distributions 
236*c54f35caSApple OSS Distributions 	if (t->pftag_flags & PF_TAG_HDR_INET) {
237*c54f35caSApple OSS Distributions 		af = AF_INET;
238*c54f35caSApple OSS Distributions 	} else if (t->pftag_flags & PF_TAG_HDR_INET6) {
239*c54f35caSApple OSS Distributions 		af = AF_INET6;
240*c54f35caSApple OSS Distributions 	} else {
241*c54f35caSApple OSS Distributions 		af = AF_UNSPEC;
242*c54f35caSApple OSS Distributions 	}
243*c54f35caSApple OSS Distributions 
244*c54f35caSApple OSS Distributions 	switch (af) {
245*c54f35caSApple OSS Distributions 	case AF_INET:
246*c54f35caSApple OSS Distributions 		if (flags & CLASSQF_ECN4) {     /* REDF_ECN4 == BLUEF_ECN4 */
247*c54f35caSApple OSS Distributions 			struct ip *ip = hdr;
248*c54f35caSApple OSS Distributions 			u_int8_t otos;
249*c54f35caSApple OSS Distributions 			int sum;
250*c54f35caSApple OSS Distributions 
251*c54f35caSApple OSS Distributions 			if (((uintptr_t)ip + sizeof(*ip)) >
252*c54f35caSApple OSS Distributions 			    ((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
253*c54f35caSApple OSS Distributions 				return 0;     /* out of bounds */
254*c54f35caSApple OSS Distributions 			}
255*c54f35caSApple OSS Distributions 			if (ip->ip_v != 4) {
256*c54f35caSApple OSS Distributions 				return 0;     /* version mismatch! */
257*c54f35caSApple OSS Distributions 			}
258*c54f35caSApple OSS Distributions 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_NOTECT) {
259*c54f35caSApple OSS Distributions 				return 0;     /* not-ECT */
260*c54f35caSApple OSS Distributions 			}
261*c54f35caSApple OSS Distributions 			if ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) {
262*c54f35caSApple OSS Distributions 				return 1;     /* already marked */
263*c54f35caSApple OSS Distributions 			}
264*c54f35caSApple OSS Distributions 			/*
265*c54f35caSApple OSS Distributions 			 * ecn-capable but not marked,
266*c54f35caSApple OSS Distributions 			 * mark CE and update checksum
267*c54f35caSApple OSS Distributions 			 */
268*c54f35caSApple OSS Distributions 			otos = ip->ip_tos;
269*c54f35caSApple OSS Distributions 			ip->ip_tos |= IPTOS_ECN_CE;
270*c54f35caSApple OSS Distributions 			/*
271*c54f35caSApple OSS Distributions 			 * update checksum (from RFC1624) only if hw
272*c54f35caSApple OSS Distributions 			 * checksum is not supported.
273*c54f35caSApple OSS Distributions 			 *	   HC' = ~(~HC + ~m + m')
274*c54f35caSApple OSS Distributions 			 */
275*c54f35caSApple OSS Distributions 			if (!(m->m_pkthdr.csum_flags & CSUM_DELAY_IP)) {
276*c54f35caSApple OSS Distributions 				sum = ~ntohs(ip->ip_sum) & 0xffff;
277*c54f35caSApple OSS Distributions 				sum += (~otos & 0xffff) + ip->ip_tos;
278*c54f35caSApple OSS Distributions 				sum = (sum >> 16) + (sum & 0xffff);
279*c54f35caSApple OSS Distributions 				sum += (sum >> 16);  /* add carry */
280*c54f35caSApple OSS Distributions 				ip->ip_sum = htons(~sum & 0xffff);
281*c54f35caSApple OSS Distributions 			}
282*c54f35caSApple OSS Distributions 			return 1;
283*c54f35caSApple OSS Distributions 		}
284*c54f35caSApple OSS Distributions 		break;
285*c54f35caSApple OSS Distributions 	case AF_INET6:
286*c54f35caSApple OSS Distributions 		if (flags & CLASSQF_ECN6) {     /* REDF_ECN6 == BLUEF_ECN6 */
287*c54f35caSApple OSS Distributions 			struct ip6_hdr *ip6 = hdr;
288*c54f35caSApple OSS Distributions 			u_int32_t flowlabel;
289*c54f35caSApple OSS Distributions 
290*c54f35caSApple OSS Distributions 			if (((uintptr_t)ip6 + sizeof(*ip6)) >
291*c54f35caSApple OSS Distributions 			    ((uintptr_t)mbuf_datastart(m0) + mbuf_maxlen(m0))) {
292*c54f35caSApple OSS Distributions 				return 0;     /* out of bounds */
293*c54f35caSApple OSS Distributions 			}
294*c54f35caSApple OSS Distributions 			flowlabel = ntohl(ip6->ip6_flow);
295*c54f35caSApple OSS Distributions 			if ((flowlabel >> 28) != 6) {
296*c54f35caSApple OSS Distributions 				return 0;     /* version mismatch! */
297*c54f35caSApple OSS Distributions 			}
298*c54f35caSApple OSS Distributions 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
299*c54f35caSApple OSS Distributions 			    (IPTOS_ECN_NOTECT << 20)) {
300*c54f35caSApple OSS Distributions 				return 0;     /* not-ECT */
301*c54f35caSApple OSS Distributions 			}
302*c54f35caSApple OSS Distributions 			if ((flowlabel & (IPTOS_ECN_MASK << 20)) ==
303*c54f35caSApple OSS Distributions 			    (IPTOS_ECN_CE << 20)) {
304*c54f35caSApple OSS Distributions 				return 1;     /* already marked */
305*c54f35caSApple OSS Distributions 			}
306*c54f35caSApple OSS Distributions 			/*
307*c54f35caSApple OSS Distributions 			 * ecn-capable but not marked,  mark CE
308*c54f35caSApple OSS Distributions 			 */
309*c54f35caSApple OSS Distributions 			flowlabel |= (IPTOS_ECN_CE << 20);
310*c54f35caSApple OSS Distributions 			ip6->ip6_flow = htonl(flowlabel);
311*c54f35caSApple OSS Distributions 			return 1;
312*c54f35caSApple OSS Distributions 		}
313*c54f35caSApple OSS Distributions 		break;
314*c54f35caSApple OSS Distributions 	}
315*c54f35caSApple OSS Distributions 
316*c54f35caSApple OSS Distributions 	/* not marked */
317*c54f35caSApple OSS Distributions 	return 0;
318*c54f35caSApple OSS Distributions }
319*c54f35caSApple OSS Distributions #endif /* PF_ECN */
320