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