frag6.c revision 1.54 1 1.54 christos /* $NetBSD: frag6.c,v 1.54 2012/09/27 23:10:00 christos Exp $ */
2 1.18 itojun /* $KAME: frag6.c,v 1.40 2002/05/27 21:40:31 itojun Exp $ */
3 1.3 thorpej
4 1.2 itojun /*
5 1.2 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 1.2 itojun * All rights reserved.
7 1.11 itojun *
8 1.2 itojun * Redistribution and use in source and binary forms, with or without
9 1.2 itojun * modification, are permitted provided that the following conditions
10 1.2 itojun * are met:
11 1.2 itojun * 1. Redistributions of source code must retain the above copyright
12 1.2 itojun * notice, this list of conditions and the following disclaimer.
13 1.2 itojun * 2. Redistributions in binary form must reproduce the above copyright
14 1.2 itojun * notice, this list of conditions and the following disclaimer in the
15 1.2 itojun * documentation and/or other materials provided with the distribution.
16 1.2 itojun * 3. Neither the name of the project nor the names of its contributors
17 1.2 itojun * may be used to endorse or promote products derived from this software
18 1.2 itojun * without specific prior written permission.
19 1.11 itojun *
20 1.2 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 1.2 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 1.2 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 1.2 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 1.2 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 1.2 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 1.2 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 1.2 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 1.2 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 1.2 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.2 itojun * SUCH DAMAGE.
31 1.2 itojun */
32 1.16 lukem
33 1.16 lukem #include <sys/cdefs.h>
34 1.54 christos __KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.54 2012/09/27 23:10:00 christos Exp $");
35 1.2 itojun
36 1.2 itojun #include <sys/param.h>
37 1.2 itojun #include <sys/systm.h>
38 1.2 itojun #include <sys/mbuf.h>
39 1.2 itojun #include <sys/domain.h>
40 1.2 itojun #include <sys/protosw.h>
41 1.2 itojun #include <sys/socket.h>
42 1.45 ad #include <sys/socketvar.h>
43 1.2 itojun #include <sys/errno.h>
44 1.2 itojun #include <sys/time.h>
45 1.53 rmind #include <sys/kmem.h>
46 1.2 itojun #include <sys/kernel.h>
47 1.2 itojun #include <sys/syslog.h>
48 1.2 itojun
49 1.2 itojun #include <net/if.h>
50 1.2 itojun #include <net/route.h>
51 1.2 itojun
52 1.2 itojun #include <netinet/in.h>
53 1.2 itojun #include <netinet/in_var.h>
54 1.10 itojun #include <netinet/ip6.h>
55 1.2 itojun #include <netinet6/ip6_var.h>
56 1.44 thorpej #include <netinet6/ip6_private.h>
57 1.10 itojun #include <netinet/icmp6.h>
58 1.2 itojun
59 1.7 itojun #include <net/net_osdep.h>
60 1.7 itojun
61 1.39 dyoung static void frag6_enq(struct ip6asfrag *, struct ip6asfrag *);
62 1.39 dyoung static void frag6_deq(struct ip6asfrag *);
63 1.39 dyoung static void frag6_insque(struct ip6q *, struct ip6q *);
64 1.39 dyoung static void frag6_remque(struct ip6q *);
65 1.39 dyoung static void frag6_freef(struct ip6q *);
66 1.2 itojun
67 1.49 dyoung static int frag6_drainwanted;
68 1.49 dyoung
69 1.2 itojun u_int frag6_nfragpackets;
70 1.18 itojun u_int frag6_nfrags;
71 1.2 itojun struct ip6q ip6q; /* ip6 reassemble queue */
72 1.2 itojun
73 1.50 zoltan static kmutex_t frag6_lock;
74 1.17 itojun
75 1.2 itojun /*
76 1.2 itojun * Initialise reassembly queue and fragment identifier.
77 1.2 itojun */
78 1.2 itojun void
79 1.42 matt frag6_init(void)
80 1.2 itojun {
81 1.6 itojun
82 1.2 itojun ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
83 1.50 zoltan mutex_init(&frag6_lock, MUTEX_DEFAULT, IPL_NET);
84 1.2 itojun }
85 1.2 itojun
86 1.2 itojun /*
87 1.53 rmind * IPv6 fragment input.
88 1.53 rmind *
89 1.9 itojun * In RFC2460, fragment and reassembly rule do not agree with each other,
90 1.9 itojun * in terms of next header field handling in fragment header.
91 1.9 itojun * While the sender will use the same value for all of the fragmented packets,
92 1.9 itojun * receiver is suggested not to check the consistency.
93 1.9 itojun *
94 1.9 itojun * fragment rule (p20):
95 1.9 itojun * (2) A Fragment header containing:
96 1.9 itojun * The Next Header value that identifies the first header of
97 1.9 itojun * the Fragmentable Part of the original packet.
98 1.9 itojun * -> next header field is same for all fragments
99 1.9 itojun *
100 1.11 itojun * reassembly rule (p21):
101 1.9 itojun * The Next Header field of the last header of the Unfragmentable
102 1.9 itojun * Part is obtained from the Next Header field of the first
103 1.9 itojun * fragment's Fragment header.
104 1.9 itojun * -> should grab it from the first fragment only
105 1.9 itojun *
106 1.9 itojun * The following note also contradicts with fragment rule - noone is going to
107 1.9 itojun * send different fragment with different next header field.
108 1.9 itojun *
109 1.9 itojun * additional note (p22):
110 1.9 itojun * The Next Header values in the Fragment headers of different
111 1.9 itojun * fragments of the same original packet may differ. Only the value
112 1.9 itojun * from the Offset zero fragment packet is used for reassembly.
113 1.9 itojun * -> should grab it from the first fragment only
114 1.9 itojun *
115 1.9 itojun * There is no explicit reason given in the RFC. Historical reason maybe?
116 1.9 itojun */
117 1.53 rmind int
118 1.53 rmind frag6_input(struct mbuf **mp, int *offp, int proto)
119 1.2 itojun {
120 1.40 dyoung struct rtentry *rt;
121 1.2 itojun struct mbuf *m = *mp, *t;
122 1.2 itojun struct ip6_hdr *ip6;
123 1.2 itojun struct ip6_frag *ip6f;
124 1.2 itojun struct ip6q *q6;
125 1.9 itojun struct ip6asfrag *af6, *ip6af, *af6dwn;
126 1.2 itojun int offset = *offp, nxt, i, next;
127 1.2 itojun int first_frag = 0;
128 1.9 itojun int fragoff, frgpartlen; /* must be larger than u_int16_t */
129 1.7 itojun struct ifnet *dstifp;
130 1.37 dyoung static struct route ro;
131 1.37 dyoung union {
132 1.37 dyoung struct sockaddr dst;
133 1.37 dyoung struct sockaddr_in6 dst6;
134 1.37 dyoung } u;
135 1.2 itojun
136 1.7 itojun ip6 = mtod(m, struct ip6_hdr *);
137 1.7 itojun IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
138 1.7 itojun if (ip6f == NULL)
139 1.53 rmind return IPPROTO_DONE;
140 1.2 itojun
141 1.7 itojun dstifp = NULL;
142 1.7 itojun /* find the destination interface of the packet. */
143 1.37 dyoung sockaddr_in6_init(&u.dst6, &ip6->ip6_dst, 0, 0, 0);
144 1.41 dyoung if ((rt = rtcache_lookup(&ro, &u.dst)) != NULL && rt->rt_ifa != NULL)
145 1.40 dyoung dstifp = ((struct in6_ifaddr *)rt->rt_ifa)->ia_ifp;
146 1.2 itojun
147 1.2 itojun /* jumbo payload can't contain a fragment header */
148 1.2 itojun if (ip6->ip6_plen == 0) {
149 1.2 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
150 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_fail);
151 1.53 rmind return IPPROTO_DONE;
152 1.2 itojun }
153 1.2 itojun
154 1.2 itojun /*
155 1.2 itojun * check whether fragment packet's fragment length is
156 1.11 itojun * multiple of 8 octets.
157 1.2 itojun * sizeof(struct ip6_frag) == 8
158 1.2 itojun * sizeof(struct ip6_hdr) = 40
159 1.2 itojun */
160 1.2 itojun if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
161 1.2 itojun (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
162 1.18 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
163 1.18 itojun offsetof(struct ip6_hdr, ip6_plen));
164 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_fail);
165 1.53 rmind return IPPROTO_DONE;
166 1.2 itojun }
167 1.2 itojun
168 1.44 thorpej IP6_STATINC(IP6_STAT_FRAGMENTS);
169 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_reqd);
170 1.20 itojun
171 1.9 itojun /* offset now points to data portion */
172 1.2 itojun offset += sizeof(struct ip6_frag);
173 1.2 itojun
174 1.54 christos /*
175 1.54 christos * draft-gont-6man-ipv6-atomic-fragments-00: A host that receives an
176 1.54 christos * IPv6 packet which includes a Fragment Header with the "Fragment
177 1.54 christos * Offset" equal to 0 and the "M" bit equal to 0 MUST process such
178 1.54 christos * packet in isolation from any other packets/fragments.
179 1.54 christos */
180 1.54 christos fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
181 1.54 christos if (fragoff == 0 && !(ip6f->ip6f_offlg & IP6F_MORE_FRAG)) {
182 1.54 christos IP6_STATINC(IP6_STAT_REASSEMBLED);
183 1.54 christos in6_ifstat_inc(dstifp, ifs6_reass_ok);
184 1.54 christos *offp = offset;
185 1.54 christos return ip6f->ip6f_nxt;
186 1.54 christos }
187 1.54 christos
188 1.50 zoltan mutex_enter(&frag6_lock);
189 1.12 itojun
190 1.18 itojun /*
191 1.18 itojun * Enforce upper bound on number of fragments.
192 1.18 itojun * If maxfrag is 0, never accept fragments.
193 1.18 itojun * If maxfrag is -1, accept all fragments without limitation.
194 1.18 itojun */
195 1.18 itojun if (ip6_maxfrags < 0)
196 1.18 itojun ;
197 1.18 itojun else if (frag6_nfrags >= (u_int)ip6_maxfrags)
198 1.18 itojun goto dropfrag;
199 1.18 itojun
200 1.2 itojun for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
201 1.2 itojun if (ip6f->ip6f_ident == q6->ip6q_ident &&
202 1.2 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
203 1.2 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
204 1.2 itojun break;
205 1.2 itojun
206 1.2 itojun if (q6 == &ip6q) {
207 1.2 itojun /*
208 1.2 itojun * the first fragment to arrive, create a reassembly queue.
209 1.2 itojun */
210 1.2 itojun first_frag = 1;
211 1.2 itojun
212 1.2 itojun /*
213 1.2 itojun * Enforce upper bound on number of fragmented packets
214 1.11 itojun * for which we attempt reassembly;
215 1.18 itojun * If maxfragpackets is 0, never accept fragments.
216 1.18 itojun * If maxfragpackets is -1, accept all fragments without
217 1.18 itojun * limitation.
218 1.2 itojun */
219 1.13 itojun if (ip6_maxfragpackets < 0)
220 1.13 itojun ;
221 1.13 itojun else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
222 1.13 itojun goto dropfrag;
223 1.13 itojun frag6_nfragpackets++;
224 1.53 rmind
225 1.53 rmind q6 = kmem_intr_zalloc(sizeof(struct ip6q), KM_NOSLEEP);
226 1.53 rmind if (q6 == NULL) {
227 1.2 itojun goto dropfrag;
228 1.53 rmind }
229 1.2 itojun frag6_insque(q6, &ip6q);
230 1.2 itojun
231 1.9 itojun /* ip6q_nxt will be filled afterwards, from 1st fragment */
232 1.2 itojun q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
233 1.2 itojun #ifdef notyet
234 1.2 itojun q6->ip6q_nxtp = (u_char *)nxtp;
235 1.2 itojun #endif
236 1.2 itojun q6->ip6q_ident = ip6f->ip6f_ident;
237 1.2 itojun q6->ip6q_arrive = 0; /* Is it used anywhere? */
238 1.2 itojun q6->ip6q_ttl = IPV6_FRAGTTL;
239 1.2 itojun q6->ip6q_src = ip6->ip6_src;
240 1.2 itojun q6->ip6q_dst = ip6->ip6_dst;
241 1.2 itojun q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
242 1.18 itojun
243 1.18 itojun q6->ip6q_nfrag = 0;
244 1.2 itojun }
245 1.2 itojun
246 1.2 itojun /*
247 1.2 itojun * If it's the 1st fragment, record the length of the
248 1.2 itojun * unfragmentable part and the next header of the fragment header.
249 1.2 itojun */
250 1.54 christos
251 1.2 itojun if (fragoff == 0) {
252 1.18 itojun q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
253 1.18 itojun sizeof(struct ip6_frag);
254 1.2 itojun q6->ip6q_nxt = ip6f->ip6f_nxt;
255 1.2 itojun }
256 1.2 itojun
257 1.2 itojun /*
258 1.2 itojun * Check that the reassembled packet would not exceed 65535 bytes
259 1.2 itojun * in size.
260 1.2 itojun * If it would exceed, discard the fragment and return an ICMP error.
261 1.2 itojun */
262 1.9 itojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
263 1.2 itojun if (q6->ip6q_unfrglen >= 0) {
264 1.2 itojun /* The 1st fragment has already arrived. */
265 1.2 itojun if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
266 1.50 zoltan mutex_exit(&frag6_lock);
267 1.2 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
268 1.18 itojun offset - sizeof(struct ip6_frag) +
269 1.18 itojun offsetof(struct ip6_frag, ip6f_offlg));
270 1.53 rmind return IPPROTO_DONE;
271 1.2 itojun }
272 1.18 itojun } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
273 1.50 zoltan mutex_exit(&frag6_lock);
274 1.2 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
275 1.9 itojun offset - sizeof(struct ip6_frag) +
276 1.9 itojun offsetof(struct ip6_frag, ip6f_offlg));
277 1.53 rmind return IPPROTO_DONE;
278 1.2 itojun }
279 1.2 itojun /*
280 1.2 itojun * If it's the first fragment, do the above check for each
281 1.2 itojun * fragment already stored in the reassembly queue.
282 1.2 itojun */
283 1.2 itojun if (fragoff == 0) {
284 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
285 1.2 itojun af6 = af6dwn) {
286 1.2 itojun af6dwn = af6->ip6af_down;
287 1.2 itojun
288 1.2 itojun if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
289 1.2 itojun IPV6_MAXPACKET) {
290 1.2 itojun struct mbuf *merr = IP6_REASS_MBUF(af6);
291 1.2 itojun struct ip6_hdr *ip6err;
292 1.2 itojun int erroff = af6->ip6af_offset;
293 1.2 itojun
294 1.2 itojun /* dequeue the fragment. */
295 1.2 itojun frag6_deq(af6);
296 1.53 rmind kmem_intr_free(af6, sizeof(struct ip6asfrag));
297 1.2 itojun
298 1.2 itojun /* adjust pointer. */
299 1.2 itojun ip6err = mtod(merr, struct ip6_hdr *);
300 1.2 itojun
301 1.2 itojun /*
302 1.2 itojun * Restore source and destination addresses
303 1.2 itojun * in the erroneous IPv6 header.
304 1.2 itojun */
305 1.2 itojun ip6err->ip6_src = q6->ip6q_src;
306 1.2 itojun ip6err->ip6_dst = q6->ip6q_dst;
307 1.2 itojun
308 1.2 itojun icmp6_error(merr, ICMP6_PARAM_PROB,
309 1.18 itojun ICMP6_PARAMPROB_HEADER,
310 1.18 itojun erroff - sizeof(struct ip6_frag) +
311 1.18 itojun offsetof(struct ip6_frag, ip6f_offlg));
312 1.2 itojun }
313 1.2 itojun }
314 1.2 itojun }
315 1.2 itojun
316 1.53 rmind ip6af = kmem_intr_zalloc(sizeof(struct ip6asfrag), KM_NOSLEEP);
317 1.53 rmind if (ip6af == NULL) {
318 1.9 itojun goto dropfrag;
319 1.53 rmind }
320 1.9 itojun ip6af->ip6af_head = ip6->ip6_flow;
321 1.9 itojun ip6af->ip6af_len = ip6->ip6_plen;
322 1.9 itojun ip6af->ip6af_nxt = ip6->ip6_nxt;
323 1.9 itojun ip6af->ip6af_hlim = ip6->ip6_hlim;
324 1.2 itojun ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
325 1.2 itojun ip6af->ip6af_off = fragoff;
326 1.2 itojun ip6af->ip6af_frglen = frgpartlen;
327 1.2 itojun ip6af->ip6af_offset = offset;
328 1.2 itojun IP6_REASS_MBUF(ip6af) = m;
329 1.2 itojun
330 1.2 itojun if (first_frag) {
331 1.2 itojun af6 = (struct ip6asfrag *)q6;
332 1.2 itojun goto insert;
333 1.2 itojun }
334 1.2 itojun
335 1.2 itojun /*
336 1.2 itojun * Find a segment which begins after this one does.
337 1.2 itojun */
338 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
339 1.2 itojun af6 = af6->ip6af_down)
340 1.2 itojun if (af6->ip6af_off > ip6af->ip6af_off)
341 1.2 itojun break;
342 1.2 itojun
343 1.2 itojun /*
344 1.53 rmind * If the incoming fragment overlaps some existing fragments in
345 1.53 rmind * the reassembly queue - drop it as per RFC 5722.
346 1.2 itojun */
347 1.2 itojun if (af6->ip6af_up != (struct ip6asfrag *)q6) {
348 1.2 itojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
349 1.2 itojun - ip6af->ip6af_off;
350 1.2 itojun if (i > 0) {
351 1.53 rmind kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
352 1.2 itojun goto dropfrag;
353 1.2 itojun }
354 1.2 itojun }
355 1.2 itojun if (af6 != (struct ip6asfrag *)q6) {
356 1.2 itojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
357 1.2 itojun if (i > 0) {
358 1.53 rmind kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
359 1.2 itojun goto dropfrag;
360 1.2 itojun }
361 1.2 itojun }
362 1.2 itojun
363 1.2 itojun insert:
364 1.2 itojun
365 1.2 itojun /*
366 1.2 itojun * Stick new segment in its place;
367 1.2 itojun * check for complete reassembly.
368 1.2 itojun * Move to front of packet queue, as we are
369 1.2 itojun * the most recently active fragmented packet.
370 1.2 itojun */
371 1.2 itojun frag6_enq(ip6af, af6->ip6af_up);
372 1.18 itojun frag6_nfrags++;
373 1.18 itojun q6->ip6q_nfrag++;
374 1.2 itojun #if 0 /* xxx */
375 1.2 itojun if (q6 != ip6q.ip6q_next) {
376 1.2 itojun frag6_remque(q6);
377 1.2 itojun frag6_insque(q6, &ip6q);
378 1.2 itojun }
379 1.2 itojun #endif
380 1.2 itojun next = 0;
381 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
382 1.2 itojun af6 = af6->ip6af_down) {
383 1.2 itojun if (af6->ip6af_off != next) {
384 1.50 zoltan mutex_exit(&frag6_lock);
385 1.53 rmind return IPPROTO_DONE;
386 1.2 itojun }
387 1.2 itojun next += af6->ip6af_frglen;
388 1.2 itojun }
389 1.2 itojun if (af6->ip6af_up->ip6af_mff) {
390 1.50 zoltan mutex_exit(&frag6_lock);
391 1.53 rmind return IPPROTO_DONE;
392 1.2 itojun }
393 1.2 itojun
394 1.2 itojun /*
395 1.2 itojun * Reassembly is complete; concatenate fragments.
396 1.2 itojun */
397 1.2 itojun ip6af = q6->ip6q_down;
398 1.2 itojun t = m = IP6_REASS_MBUF(ip6af);
399 1.2 itojun af6 = ip6af->ip6af_down;
400 1.9 itojun frag6_deq(ip6af);
401 1.2 itojun while (af6 != (struct ip6asfrag *)q6) {
402 1.9 itojun af6dwn = af6->ip6af_down;
403 1.9 itojun frag6_deq(af6);
404 1.2 itojun while (t->m_next)
405 1.2 itojun t = t->m_next;
406 1.2 itojun t->m_next = IP6_REASS_MBUF(af6);
407 1.9 itojun m_adj(t->m_next, af6->ip6af_offset);
408 1.53 rmind kmem_intr_free(af6, sizeof(struct ip6asfrag));
409 1.9 itojun af6 = af6dwn;
410 1.2 itojun }
411 1.2 itojun
412 1.2 itojun /* adjust offset to point where the original next header starts */
413 1.2 itojun offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
414 1.53 rmind kmem_intr_free(ip6af, sizeof(struct ip6asfrag));
415 1.9 itojun ip6 = mtod(m, struct ip6_hdr *);
416 1.25 itojun ip6->ip6_plen = htons(next + offset - sizeof(struct ip6_hdr));
417 1.2 itojun ip6->ip6_src = q6->ip6q_src;
418 1.2 itojun ip6->ip6_dst = q6->ip6q_dst;
419 1.2 itojun nxt = q6->ip6q_nxt;
420 1.2 itojun #ifdef notyet
421 1.2 itojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
422 1.2 itojun #endif
423 1.2 itojun
424 1.2 itojun /*
425 1.2 itojun * Delete frag6 header with as a few cost as possible.
426 1.2 itojun */
427 1.48 mlelstv if (m->m_len >= offset + sizeof(struct ip6_frag)) {
428 1.36 christos memmove((char *)ip6 + sizeof(struct ip6_frag), ip6, offset);
429 1.9 itojun m->m_data += sizeof(struct ip6_frag);
430 1.9 itojun m->m_len -= sizeof(struct ip6_frag);
431 1.9 itojun } else {
432 1.9 itojun /* this comes with no copy if the boundary is on cluster */
433 1.9 itojun if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
434 1.9 itojun frag6_remque(q6);
435 1.18 itojun frag6_nfrags -= q6->ip6q_nfrag;
436 1.53 rmind kmem_intr_free(q6, sizeof(struct ip6q));
437 1.9 itojun frag6_nfragpackets--;
438 1.9 itojun goto dropfrag;
439 1.9 itojun }
440 1.9 itojun m_adj(t, sizeof(struct ip6_frag));
441 1.9 itojun m_cat(m, t);
442 1.2 itojun }
443 1.2 itojun
444 1.2 itojun /*
445 1.2 itojun * Store NXT to the original.
446 1.2 itojun */
447 1.2 itojun {
448 1.21 itojun u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
449 1.2 itojun *prvnxtp = nxt;
450 1.2 itojun }
451 1.2 itojun
452 1.2 itojun frag6_remque(q6);
453 1.18 itojun frag6_nfrags -= q6->ip6q_nfrag;
454 1.53 rmind kmem_intr_free(q6, sizeof(struct ip6q));
455 1.2 itojun frag6_nfragpackets--;
456 1.2 itojun
457 1.2 itojun if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
458 1.2 itojun int plen = 0;
459 1.2 itojun for (t = m; t; t = t->m_next)
460 1.2 itojun plen += t->m_len;
461 1.2 itojun m->m_pkthdr.len = plen;
462 1.2 itojun }
463 1.20 itojun
464 1.44 thorpej IP6_STATINC(IP6_STAT_REASSEMBLED);
465 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_ok);
466 1.2 itojun
467 1.2 itojun /*
468 1.2 itojun * Tell launch routine the next header
469 1.2 itojun */
470 1.2 itojun
471 1.2 itojun *mp = m;
472 1.2 itojun *offp = offset;
473 1.2 itojun
474 1.50 zoltan mutex_exit(&frag6_lock);
475 1.2 itojun return nxt;
476 1.2 itojun
477 1.2 itojun dropfrag:
478 1.50 zoltan mutex_exit(&frag6_lock);
479 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_fail);
480 1.44 thorpej IP6_STATINC(IP6_STAT_FRAGDROPPED);
481 1.2 itojun m_freem(m);
482 1.2 itojun return IPPROTO_DONE;
483 1.2 itojun }
484 1.2 itojun
485 1.50 zoltan int
486 1.50 zoltan ip6_reass_packet(struct mbuf **mp, int offset)
487 1.50 zoltan {
488 1.50 zoltan
489 1.53 rmind if (frag6_input(mp, &offset, IPPROTO_IPV6) == IPPROTO_DONE) {
490 1.50 zoltan *mp = NULL;
491 1.53 rmind return EINVAL;
492 1.50 zoltan }
493 1.53 rmind return 0;
494 1.50 zoltan }
495 1.50 zoltan
496 1.2 itojun /*
497 1.2 itojun * Free a fragment reassembly header and all
498 1.2 itojun * associated datagrams.
499 1.2 itojun */
500 1.2 itojun void
501 1.38 christos frag6_freef(struct ip6q *q6)
502 1.2 itojun {
503 1.2 itojun struct ip6asfrag *af6, *down6;
504 1.2 itojun
505 1.50 zoltan KASSERT(mutex_owned(&frag6_lock));
506 1.17 itojun
507 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
508 1.2 itojun af6 = down6) {
509 1.2 itojun struct mbuf *m = IP6_REASS_MBUF(af6);
510 1.2 itojun
511 1.2 itojun down6 = af6->ip6af_down;
512 1.2 itojun frag6_deq(af6);
513 1.2 itojun
514 1.2 itojun /*
515 1.2 itojun * Return ICMP time exceeded error for the 1st fragment.
516 1.2 itojun * Just free other fragments.
517 1.2 itojun */
518 1.2 itojun if (af6->ip6af_off == 0) {
519 1.2 itojun struct ip6_hdr *ip6;
520 1.2 itojun
521 1.2 itojun /* adjust pointer */
522 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
523 1.2 itojun
524 1.2 itojun /* restoure source and destination addresses */
525 1.2 itojun ip6->ip6_src = q6->ip6q_src;
526 1.2 itojun ip6->ip6_dst = q6->ip6q_dst;
527 1.2 itojun
528 1.2 itojun icmp6_error(m, ICMP6_TIME_EXCEEDED,
529 1.2 itojun ICMP6_TIME_EXCEED_REASSEMBLY, 0);
530 1.53 rmind } else {
531 1.2 itojun m_freem(m);
532 1.53 rmind }
533 1.53 rmind kmem_intr_free(af6, sizeof(struct ip6asfrag));
534 1.2 itojun }
535 1.2 itojun frag6_remque(q6);
536 1.18 itojun frag6_nfrags -= q6->ip6q_nfrag;
537 1.53 rmind kmem_intr_free(q6, sizeof(struct ip6q));
538 1.2 itojun frag6_nfragpackets--;
539 1.2 itojun }
540 1.2 itojun
541 1.2 itojun /*
542 1.2 itojun * Put an ip fragment on a reassembly chain.
543 1.2 itojun * Like insque, but pointers in middle of structure.
544 1.2 itojun */
545 1.2 itojun void
546 1.38 christos frag6_enq(struct ip6asfrag *af6, struct ip6asfrag *up6)
547 1.2 itojun {
548 1.17 itojun
549 1.50 zoltan KASSERT(mutex_owned(&frag6_lock));
550 1.17 itojun
551 1.2 itojun af6->ip6af_up = up6;
552 1.2 itojun af6->ip6af_down = up6->ip6af_down;
553 1.2 itojun up6->ip6af_down->ip6af_up = af6;
554 1.2 itojun up6->ip6af_down = af6;
555 1.2 itojun }
556 1.2 itojun
557 1.2 itojun /*
558 1.2 itojun * To frag6_enq as remque is to insque.
559 1.2 itojun */
560 1.2 itojun void
561 1.38 christos frag6_deq(struct ip6asfrag *af6)
562 1.2 itojun {
563 1.17 itojun
564 1.50 zoltan KASSERT(mutex_owned(&frag6_lock));
565 1.17 itojun
566 1.2 itojun af6->ip6af_up->ip6af_down = af6->ip6af_down;
567 1.2 itojun af6->ip6af_down->ip6af_up = af6->ip6af_up;
568 1.2 itojun }
569 1.2 itojun
570 1.11 itojun void
571 1.38 christos frag6_insque(struct ip6q *new, struct ip6q *old)
572 1.2 itojun {
573 1.17 itojun
574 1.50 zoltan KASSERT(mutex_owned(&frag6_lock));
575 1.17 itojun
576 1.2 itojun new->ip6q_prev = old;
577 1.2 itojun new->ip6q_next = old->ip6q_next;
578 1.2 itojun old->ip6q_next->ip6q_prev= new;
579 1.2 itojun old->ip6q_next = new;
580 1.2 itojun }
581 1.2 itojun
582 1.2 itojun void
583 1.38 christos frag6_remque(struct ip6q *p6)
584 1.2 itojun {
585 1.17 itojun
586 1.50 zoltan KASSERT(mutex_owned(&frag6_lock));
587 1.17 itojun
588 1.2 itojun p6->ip6q_prev->ip6q_next = p6->ip6q_next;
589 1.2 itojun p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
590 1.2 itojun }
591 1.2 itojun
592 1.49 dyoung void
593 1.49 dyoung frag6_fasttimo(void)
594 1.49 dyoung {
595 1.51 jakllsch mutex_enter(softnet_lock);
596 1.51 jakllsch KERNEL_LOCK(1, NULL);
597 1.51 jakllsch
598 1.49 dyoung if (frag6_drainwanted) {
599 1.49 dyoung frag6_drain();
600 1.49 dyoung frag6_drainwanted = 0;
601 1.49 dyoung }
602 1.51 jakllsch
603 1.51 jakllsch KERNEL_UNLOCK_ONE(NULL);
604 1.51 jakllsch mutex_exit(softnet_lock);
605 1.49 dyoung }
606 1.49 dyoung
607 1.2 itojun /*
608 1.11 itojun * IPv6 reassembling timer processing;
609 1.2 itojun * if a timer expires on a reassembly
610 1.2 itojun * queue, discard it.
611 1.2 itojun */
612 1.2 itojun void
613 1.42 matt frag6_slowtimo(void)
614 1.2 itojun {
615 1.2 itojun struct ip6q *q6;
616 1.45 ad
617 1.51 jakllsch mutex_enter(softnet_lock);
618 1.51 jakllsch KERNEL_LOCK(1, NULL);
619 1.51 jakllsch
620 1.50 zoltan mutex_enter(&frag6_lock);
621 1.2 itojun q6 = ip6q.ip6q_next;
622 1.2 itojun if (q6)
623 1.2 itojun while (q6 != &ip6q) {
624 1.2 itojun --q6->ip6q_ttl;
625 1.2 itojun q6 = q6->ip6q_next;
626 1.2 itojun if (q6->ip6q_prev->ip6q_ttl == 0) {
627 1.44 thorpej IP6_STATINC(IP6_STAT_FRAGTIMEOUT);
628 1.7 itojun /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
629 1.2 itojun frag6_freef(q6->ip6q_prev);
630 1.2 itojun }
631 1.2 itojun }
632 1.2 itojun /*
633 1.2 itojun * If we are over the maximum number of fragments
634 1.2 itojun * (due to the limit being lowered), drain off
635 1.2 itojun * enough to get down to the new limit.
636 1.2 itojun */
637 1.13 itojun while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
638 1.13 itojun ip6q.ip6q_prev) {
639 1.44 thorpej IP6_STATINC(IP6_STAT_FRAGOVERFLOW);
640 1.7 itojun /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
641 1.2 itojun frag6_freef(ip6q.ip6q_prev);
642 1.2 itojun }
643 1.50 zoltan mutex_exit(&frag6_lock);
644 1.2 itojun
645 1.51 jakllsch KERNEL_UNLOCK_ONE(NULL);
646 1.51 jakllsch mutex_exit(softnet_lock);
647 1.51 jakllsch
648 1.2 itojun #if 0
649 1.2 itojun /*
650 1.2 itojun * Routing changes might produce a better route than we last used;
651 1.2 itojun * make sure we notice eventually, even if forwarding only for one
652 1.2 itojun * destination and the cache is never replaced.
653 1.2 itojun */
654 1.37 dyoung rtcache_free(&ip6_forward_rt);
655 1.37 dyoung rtcache_free(&ipsrcchk_rt);
656 1.2 itojun #endif
657 1.2 itojun
658 1.2 itojun }
659 1.2 itojun
660 1.49 dyoung void
661 1.49 dyoung frag6_drainstub(void)
662 1.49 dyoung {
663 1.49 dyoung frag6_drainwanted = 1;
664 1.49 dyoung }
665 1.49 dyoung
666 1.2 itojun /*
667 1.2 itojun * Drain off all datagram fragments.
668 1.2 itojun */
669 1.2 itojun void
670 1.42 matt frag6_drain(void)
671 1.2 itojun {
672 1.17 itojun
673 1.50 zoltan if (mutex_tryenter(&frag6_lock)) {
674 1.45 ad while (ip6q.ip6q_next != &ip6q) {
675 1.45 ad IP6_STATINC(IP6_STAT_FRAGDROPPED);
676 1.45 ad /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
677 1.45 ad frag6_freef(ip6q.ip6q_next);
678 1.45 ad }
679 1.50 zoltan mutex_exit(&frag6_lock);
680 1.2 itojun }
681 1.2 itojun }
682