frag6.c revision 1.28 1 1.28 perry /* $NetBSD: frag6.c,v 1.28 2005/12/24 20:45:09 perry 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.28 perry __KERNEL_RCSID(0, "$NetBSD: frag6.c,v 1.28 2005/12/24 20:45:09 perry 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/malloc.h>
39 1.2 itojun #include <sys/mbuf.h>
40 1.2 itojun #include <sys/domain.h>
41 1.2 itojun #include <sys/protosw.h>
42 1.2 itojun #include <sys/socket.h>
43 1.2 itojun #include <sys/errno.h>
44 1.2 itojun #include <sys/time.h>
45 1.2 itojun #include <sys/kernel.h>
46 1.2 itojun #include <sys/syslog.h>
47 1.2 itojun
48 1.2 itojun #include <net/if.h>
49 1.2 itojun #include <net/route.h>
50 1.2 itojun
51 1.2 itojun #include <netinet/in.h>
52 1.2 itojun #include <netinet/in_var.h>
53 1.10 itojun #include <netinet/ip6.h>
54 1.2 itojun #include <netinet6/in6_pcb.h>
55 1.2 itojun #include <netinet6/ip6_var.h>
56 1.10 itojun #include <netinet/icmp6.h>
57 1.2 itojun
58 1.7 itojun #include <net/net_osdep.h>
59 1.7 itojun
60 1.7 itojun /*
61 1.7 itojun * Define it to get a correct behavior on per-interface statistics.
62 1.7 itojun * You will need to perform an extra routing table lookup, per fragment,
63 1.7 itojun * to do it. This may, or may not be, a performance hit.
64 1.7 itojun */
65 1.7 itojun #define IN6_IFSTAT_STRICT
66 1.7 itojun
67 1.2 itojun static void frag6_enq __P((struct ip6asfrag *, struct ip6asfrag *));
68 1.2 itojun static void frag6_deq __P((struct ip6asfrag *));
69 1.2 itojun static void frag6_insque __P((struct ip6q *, struct ip6q *));
70 1.2 itojun static void frag6_remque __P((struct ip6q *));
71 1.2 itojun static void frag6_freef __P((struct ip6q *));
72 1.2 itojun
73 1.17 itojun static int ip6q_locked;
74 1.2 itojun u_int frag6_nfragpackets;
75 1.18 itojun u_int frag6_nfrags;
76 1.2 itojun struct ip6q ip6q; /* ip6 reassemble queue */
77 1.2 itojun
78 1.28 perry static inline int ip6q_lock_try __P((void));
79 1.28 perry static inline void ip6q_unlock __P((void));
80 1.17 itojun
81 1.28 perry static inline int
82 1.17 itojun ip6q_lock_try()
83 1.17 itojun {
84 1.17 itojun int s;
85 1.17 itojun
86 1.17 itojun /*
87 1.17 itojun * Use splvm() -- we're bloking things that would cause
88 1.17 itojun * mbuf allocation.
89 1.17 itojun */
90 1.17 itojun s = splvm();
91 1.17 itojun if (ip6q_locked) {
92 1.17 itojun splx(s);
93 1.17 itojun return (0);
94 1.17 itojun }
95 1.17 itojun ip6q_locked = 1;
96 1.17 itojun splx(s);
97 1.17 itojun return (1);
98 1.17 itojun }
99 1.17 itojun
100 1.28 perry static inline void
101 1.17 itojun ip6q_unlock()
102 1.17 itojun {
103 1.17 itojun int s;
104 1.17 itojun
105 1.17 itojun s = splvm();
106 1.17 itojun ip6q_locked = 0;
107 1.17 itojun splx(s);
108 1.17 itojun }
109 1.17 itojun
110 1.17 itojun #ifdef DIAGNOSTIC
111 1.17 itojun #define IP6Q_LOCK() \
112 1.17 itojun do { \
113 1.17 itojun if (ip6q_lock_try() == 0) { \
114 1.17 itojun printf("%s:%d: ip6q already locked\n", __FILE__, __LINE__); \
115 1.17 itojun panic("ip6q_lock"); \
116 1.17 itojun } \
117 1.23 perry } while (/*CONSTCOND*/ 0)
118 1.17 itojun #define IP6Q_LOCK_CHECK() \
119 1.17 itojun do { \
120 1.17 itojun if (ip6q_locked == 0) { \
121 1.17 itojun printf("%s:%d: ip6q lock not held\n", __FILE__, __LINE__); \
122 1.17 itojun panic("ip6q lock check"); \
123 1.17 itojun } \
124 1.23 perry } while (/*CONSTCOND*/ 0)
125 1.17 itojun #else
126 1.17 itojun #define IP6Q_LOCK() (void) ip6q_lock_try()
127 1.17 itojun #define IP6Q_LOCK_CHECK() /* nothing */
128 1.17 itojun #endif
129 1.17 itojun
130 1.17 itojun #define IP6Q_UNLOCK() ip6q_unlock()
131 1.17 itojun
132 1.9 itojun #ifndef offsetof /* XXX */
133 1.9 itojun #define offsetof(type, member) ((size_t)(&((type *)0)->member))
134 1.12 itojun #endif
135 1.9 itojun
136 1.2 itojun /*
137 1.2 itojun * Initialise reassembly queue and fragment identifier.
138 1.2 itojun */
139 1.2 itojun void
140 1.2 itojun frag6_init()
141 1.2 itojun {
142 1.6 itojun
143 1.2 itojun ip6q.ip6q_next = ip6q.ip6q_prev = &ip6q;
144 1.2 itojun }
145 1.2 itojun
146 1.2 itojun /*
147 1.9 itojun * In RFC2460, fragment and reassembly rule do not agree with each other,
148 1.9 itojun * in terms of next header field handling in fragment header.
149 1.9 itojun * While the sender will use the same value for all of the fragmented packets,
150 1.9 itojun * receiver is suggested not to check the consistency.
151 1.9 itojun *
152 1.9 itojun * fragment rule (p20):
153 1.9 itojun * (2) A Fragment header containing:
154 1.9 itojun * The Next Header value that identifies the first header of
155 1.9 itojun * the Fragmentable Part of the original packet.
156 1.9 itojun * -> next header field is same for all fragments
157 1.9 itojun *
158 1.11 itojun * reassembly rule (p21):
159 1.9 itojun * The Next Header field of the last header of the Unfragmentable
160 1.9 itojun * Part is obtained from the Next Header field of the first
161 1.9 itojun * fragment's Fragment header.
162 1.9 itojun * -> should grab it from the first fragment only
163 1.9 itojun *
164 1.9 itojun * The following note also contradicts with fragment rule - noone is going to
165 1.9 itojun * send different fragment with different next header field.
166 1.9 itojun *
167 1.9 itojun * additional note (p22):
168 1.9 itojun * The Next Header values in the Fragment headers of different
169 1.9 itojun * fragments of the same original packet may differ. Only the value
170 1.9 itojun * from the Offset zero fragment packet is used for reassembly.
171 1.9 itojun * -> should grab it from the first fragment only
172 1.9 itojun *
173 1.9 itojun * There is no explicit reason given in the RFC. Historical reason maybe?
174 1.9 itojun */
175 1.9 itojun /*
176 1.2 itojun * Fragment input
177 1.2 itojun */
178 1.2 itojun int
179 1.2 itojun frag6_input(mp, offp, proto)
180 1.2 itojun struct mbuf **mp;
181 1.2 itojun int *offp, proto;
182 1.2 itojun {
183 1.2 itojun struct mbuf *m = *mp, *t;
184 1.2 itojun struct ip6_hdr *ip6;
185 1.2 itojun struct ip6_frag *ip6f;
186 1.2 itojun struct ip6q *q6;
187 1.9 itojun struct ip6asfrag *af6, *ip6af, *af6dwn;
188 1.2 itojun int offset = *offp, nxt, i, next;
189 1.2 itojun int first_frag = 0;
190 1.9 itojun int fragoff, frgpartlen; /* must be larger than u_int16_t */
191 1.7 itojun struct ifnet *dstifp;
192 1.7 itojun #ifdef IN6_IFSTAT_STRICT
193 1.7 itojun static struct route_in6 ro;
194 1.7 itojun struct sockaddr_in6 *dst;
195 1.7 itojun #endif
196 1.2 itojun
197 1.7 itojun ip6 = mtod(m, struct ip6_hdr *);
198 1.7 itojun IP6_EXTHDR_GET(ip6f, struct ip6_frag *, m, offset, sizeof(*ip6f));
199 1.7 itojun if (ip6f == NULL)
200 1.7 itojun return IPPROTO_DONE;
201 1.2 itojun
202 1.7 itojun dstifp = NULL;
203 1.7 itojun #ifdef IN6_IFSTAT_STRICT
204 1.7 itojun /* find the destination interface of the packet. */
205 1.7 itojun dst = (struct sockaddr_in6 *)&ro.ro_dst;
206 1.7 itojun if (ro.ro_rt
207 1.7 itojun && ((ro.ro_rt->rt_flags & RTF_UP) == 0
208 1.7 itojun || !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))) {
209 1.7 itojun RTFREE(ro.ro_rt);
210 1.7 itojun ro.ro_rt = (struct rtentry *)0;
211 1.7 itojun }
212 1.7 itojun if (ro.ro_rt == NULL) {
213 1.7 itojun bzero(dst, sizeof(*dst));
214 1.7 itojun dst->sin6_family = AF_INET6;
215 1.7 itojun dst->sin6_len = sizeof(struct sockaddr_in6);
216 1.7 itojun dst->sin6_addr = ip6->ip6_dst;
217 1.7 itojun }
218 1.7 itojun rtalloc((struct route *)&ro);
219 1.7 itojun if (ro.ro_rt != NULL && ro.ro_rt->rt_ifa != NULL)
220 1.7 itojun dstifp = ((struct in6_ifaddr *)ro.ro_rt->rt_ifa)->ia_ifp;
221 1.7 itojun #else
222 1.7 itojun /* we are violating the spec, this is not the destination interface */
223 1.7 itojun if ((m->m_flags & M_PKTHDR) != 0)
224 1.7 itojun dstifp = m->m_pkthdr.rcvif;
225 1.7 itojun #endif
226 1.2 itojun
227 1.2 itojun /* jumbo payload can't contain a fragment header */
228 1.2 itojun if (ip6->ip6_plen == 0) {
229 1.2 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER, offset);
230 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_fail);
231 1.2 itojun return IPPROTO_DONE;
232 1.2 itojun }
233 1.2 itojun
234 1.2 itojun /*
235 1.2 itojun * check whether fragment packet's fragment length is
236 1.11 itojun * multiple of 8 octets.
237 1.2 itojun * sizeof(struct ip6_frag) == 8
238 1.2 itojun * sizeof(struct ip6_hdr) = 40
239 1.2 itojun */
240 1.2 itojun if ((ip6f->ip6f_offlg & IP6F_MORE_FRAG) &&
241 1.2 itojun (((ntohs(ip6->ip6_plen) - offset) & 0x7) != 0)) {
242 1.18 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
243 1.18 itojun offsetof(struct ip6_hdr, ip6_plen));
244 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_fail);
245 1.2 itojun return IPPROTO_DONE;
246 1.2 itojun }
247 1.2 itojun
248 1.2 itojun ip6stat.ip6s_fragments++;
249 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_reqd);
250 1.20 itojun
251 1.9 itojun /* offset now points to data portion */
252 1.2 itojun offset += sizeof(struct ip6_frag);
253 1.2 itojun
254 1.17 itojun IP6Q_LOCK();
255 1.12 itojun
256 1.18 itojun /*
257 1.18 itojun * Enforce upper bound on number of fragments.
258 1.18 itojun * If maxfrag is 0, never accept fragments.
259 1.18 itojun * If maxfrag is -1, accept all fragments without limitation.
260 1.18 itojun */
261 1.18 itojun if (ip6_maxfrags < 0)
262 1.18 itojun ;
263 1.18 itojun else if (frag6_nfrags >= (u_int)ip6_maxfrags)
264 1.18 itojun goto dropfrag;
265 1.18 itojun
266 1.2 itojun for (q6 = ip6q.ip6q_next; q6 != &ip6q; q6 = q6->ip6q_next)
267 1.2 itojun if (ip6f->ip6f_ident == q6->ip6q_ident &&
268 1.2 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_src, &q6->ip6q_src) &&
269 1.2 itojun IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &q6->ip6q_dst))
270 1.2 itojun break;
271 1.2 itojun
272 1.2 itojun if (q6 == &ip6q) {
273 1.2 itojun /*
274 1.2 itojun * the first fragment to arrive, create a reassembly queue.
275 1.2 itojun */
276 1.2 itojun first_frag = 1;
277 1.2 itojun
278 1.2 itojun /*
279 1.2 itojun * Enforce upper bound on number of fragmented packets
280 1.11 itojun * for which we attempt reassembly;
281 1.18 itojun * If maxfragpackets is 0, never accept fragments.
282 1.18 itojun * If maxfragpackets is -1, accept all fragments without
283 1.18 itojun * limitation.
284 1.2 itojun */
285 1.13 itojun if (ip6_maxfragpackets < 0)
286 1.13 itojun ;
287 1.13 itojun else if (frag6_nfragpackets >= (u_int)ip6_maxfragpackets)
288 1.13 itojun goto dropfrag;
289 1.13 itojun frag6_nfragpackets++;
290 1.2 itojun q6 = (struct ip6q *)malloc(sizeof(struct ip6q), M_FTABLE,
291 1.18 itojun M_DONTWAIT);
292 1.2 itojun if (q6 == NULL)
293 1.2 itojun goto dropfrag;
294 1.9 itojun bzero(q6, sizeof(*q6));
295 1.2 itojun
296 1.2 itojun frag6_insque(q6, &ip6q);
297 1.2 itojun
298 1.9 itojun /* ip6q_nxt will be filled afterwards, from 1st fragment */
299 1.2 itojun q6->ip6q_down = q6->ip6q_up = (struct ip6asfrag *)q6;
300 1.2 itojun #ifdef notyet
301 1.2 itojun q6->ip6q_nxtp = (u_char *)nxtp;
302 1.2 itojun #endif
303 1.2 itojun q6->ip6q_ident = ip6f->ip6f_ident;
304 1.2 itojun q6->ip6q_arrive = 0; /* Is it used anywhere? */
305 1.2 itojun q6->ip6q_ttl = IPV6_FRAGTTL;
306 1.2 itojun q6->ip6q_src = ip6->ip6_src;
307 1.2 itojun q6->ip6q_dst = ip6->ip6_dst;
308 1.2 itojun q6->ip6q_unfrglen = -1; /* The 1st fragment has not arrived. */
309 1.18 itojun
310 1.18 itojun q6->ip6q_nfrag = 0;
311 1.2 itojun }
312 1.2 itojun
313 1.2 itojun /*
314 1.2 itojun * If it's the 1st fragment, record the length of the
315 1.2 itojun * unfragmentable part and the next header of the fragment header.
316 1.2 itojun */
317 1.2 itojun fragoff = ntohs(ip6f->ip6f_offlg & IP6F_OFF_MASK);
318 1.2 itojun if (fragoff == 0) {
319 1.18 itojun q6->ip6q_unfrglen = offset - sizeof(struct ip6_hdr) -
320 1.18 itojun sizeof(struct ip6_frag);
321 1.2 itojun q6->ip6q_nxt = ip6f->ip6f_nxt;
322 1.2 itojun }
323 1.2 itojun
324 1.2 itojun /*
325 1.2 itojun * Check that the reassembled packet would not exceed 65535 bytes
326 1.2 itojun * in size.
327 1.2 itojun * If it would exceed, discard the fragment and return an ICMP error.
328 1.2 itojun */
329 1.9 itojun frgpartlen = sizeof(struct ip6_hdr) + ntohs(ip6->ip6_plen) - offset;
330 1.2 itojun if (q6->ip6q_unfrglen >= 0) {
331 1.2 itojun /* The 1st fragment has already arrived. */
332 1.2 itojun if (q6->ip6q_unfrglen + fragoff + frgpartlen > IPV6_MAXPACKET) {
333 1.2 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
334 1.18 itojun offset - sizeof(struct ip6_frag) +
335 1.18 itojun offsetof(struct ip6_frag, ip6f_offlg));
336 1.17 itojun IP6Q_UNLOCK();
337 1.22 itojun return (IPPROTO_DONE);
338 1.2 itojun }
339 1.18 itojun } else if (fragoff + frgpartlen > IPV6_MAXPACKET) {
340 1.2 itojun icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_HEADER,
341 1.9 itojun offset - sizeof(struct ip6_frag) +
342 1.9 itojun offsetof(struct ip6_frag, ip6f_offlg));
343 1.17 itojun IP6Q_UNLOCK();
344 1.22 itojun return (IPPROTO_DONE);
345 1.2 itojun }
346 1.2 itojun /*
347 1.2 itojun * If it's the first fragment, do the above check for each
348 1.2 itojun * fragment already stored in the reassembly queue.
349 1.2 itojun */
350 1.2 itojun if (fragoff == 0) {
351 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
352 1.2 itojun af6 = af6dwn) {
353 1.2 itojun af6dwn = af6->ip6af_down;
354 1.2 itojun
355 1.2 itojun if (q6->ip6q_unfrglen + af6->ip6af_off + af6->ip6af_frglen >
356 1.2 itojun IPV6_MAXPACKET) {
357 1.2 itojun struct mbuf *merr = IP6_REASS_MBUF(af6);
358 1.2 itojun struct ip6_hdr *ip6err;
359 1.2 itojun int erroff = af6->ip6af_offset;
360 1.2 itojun
361 1.2 itojun /* dequeue the fragment. */
362 1.2 itojun frag6_deq(af6);
363 1.9 itojun free(af6, M_FTABLE);
364 1.2 itojun
365 1.2 itojun /* adjust pointer. */
366 1.2 itojun ip6err = mtod(merr, struct ip6_hdr *);
367 1.2 itojun
368 1.2 itojun /*
369 1.2 itojun * Restore source and destination addresses
370 1.2 itojun * in the erroneous IPv6 header.
371 1.2 itojun */
372 1.2 itojun ip6err->ip6_src = q6->ip6q_src;
373 1.2 itojun ip6err->ip6_dst = q6->ip6q_dst;
374 1.2 itojun
375 1.2 itojun icmp6_error(merr, ICMP6_PARAM_PROB,
376 1.18 itojun ICMP6_PARAMPROB_HEADER,
377 1.18 itojun erroff - sizeof(struct ip6_frag) +
378 1.18 itojun offsetof(struct ip6_frag, ip6f_offlg));
379 1.2 itojun }
380 1.2 itojun }
381 1.2 itojun }
382 1.2 itojun
383 1.9 itojun ip6af = (struct ip6asfrag *)malloc(sizeof(struct ip6asfrag), M_FTABLE,
384 1.9 itojun M_DONTWAIT);
385 1.9 itojun if (ip6af == NULL)
386 1.9 itojun goto dropfrag;
387 1.9 itojun bzero(ip6af, sizeof(*ip6af));
388 1.9 itojun ip6af->ip6af_head = ip6->ip6_flow;
389 1.9 itojun ip6af->ip6af_len = ip6->ip6_plen;
390 1.9 itojun ip6af->ip6af_nxt = ip6->ip6_nxt;
391 1.9 itojun ip6af->ip6af_hlim = ip6->ip6_hlim;
392 1.2 itojun ip6af->ip6af_mff = ip6f->ip6f_offlg & IP6F_MORE_FRAG;
393 1.2 itojun ip6af->ip6af_off = fragoff;
394 1.2 itojun ip6af->ip6af_frglen = frgpartlen;
395 1.2 itojun ip6af->ip6af_offset = offset;
396 1.2 itojun IP6_REASS_MBUF(ip6af) = m;
397 1.2 itojun
398 1.2 itojun if (first_frag) {
399 1.2 itojun af6 = (struct ip6asfrag *)q6;
400 1.2 itojun goto insert;
401 1.2 itojun }
402 1.2 itojun
403 1.2 itojun /*
404 1.2 itojun * Find a segment which begins after this one does.
405 1.2 itojun */
406 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
407 1.2 itojun af6 = af6->ip6af_down)
408 1.2 itojun if (af6->ip6af_off > ip6af->ip6af_off)
409 1.2 itojun break;
410 1.2 itojun
411 1.2 itojun #if 0
412 1.2 itojun /*
413 1.2 itojun * If there is a preceding segment, it may provide some of
414 1.2 itojun * our data already. If so, drop the data from the incoming
415 1.2 itojun * segment. If it provides all of our data, drop us.
416 1.2 itojun */
417 1.2 itojun if (af6->ip6af_up != (struct ip6asfrag *)q6) {
418 1.2 itojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
419 1.2 itojun - ip6af->ip6af_off;
420 1.2 itojun if (i > 0) {
421 1.2 itojun if (i >= ip6af->ip6af_frglen)
422 1.2 itojun goto dropfrag;
423 1.2 itojun m_adj(IP6_REASS_MBUF(ip6af), i);
424 1.2 itojun ip6af->ip6af_off += i;
425 1.2 itojun ip6af->ip6af_frglen -= i;
426 1.2 itojun }
427 1.2 itojun }
428 1.2 itojun
429 1.2 itojun /*
430 1.2 itojun * While we overlap succeeding segments trim them or,
431 1.2 itojun * if they are completely covered, dequeue them.
432 1.2 itojun */
433 1.2 itojun while (af6 != (struct ip6asfrag *)q6 &&
434 1.2 itojun ip6af->ip6af_off + ip6af->ip6af_frglen > af6->ip6af_off) {
435 1.2 itojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
436 1.2 itojun if (i < af6->ip6af_frglen) {
437 1.2 itojun af6->ip6af_frglen -= i;
438 1.2 itojun af6->ip6af_off += i;
439 1.2 itojun m_adj(IP6_REASS_MBUF(af6), i);
440 1.2 itojun break;
441 1.2 itojun }
442 1.2 itojun af6 = af6->ip6af_down;
443 1.2 itojun m_freem(IP6_REASS_MBUF(af6->ip6af_up));
444 1.2 itojun frag6_deq(af6->ip6af_up);
445 1.2 itojun }
446 1.2 itojun #else
447 1.2 itojun /*
448 1.2 itojun * If the incoming framgent overlaps some existing fragments in
449 1.2 itojun * the reassembly queue, drop it, since it is dangerous to override
450 1.2 itojun * existing fragments from a security point of view.
451 1.18 itojun * We don't know which fragment is the bad guy - here we trust
452 1.18 itojun * fragment that came in earlier, with no real reason.
453 1.2 itojun */
454 1.2 itojun if (af6->ip6af_up != (struct ip6asfrag *)q6) {
455 1.2 itojun i = af6->ip6af_up->ip6af_off + af6->ip6af_up->ip6af_frglen
456 1.2 itojun - ip6af->ip6af_off;
457 1.2 itojun if (i > 0) {
458 1.14 itojun #if 0 /* suppress the noisy log */
459 1.2 itojun log(LOG_ERR, "%d bytes of a fragment from %s "
460 1.2 itojun "overlaps the previous fragment\n",
461 1.2 itojun i, ip6_sprintf(&q6->ip6q_src));
462 1.14 itojun #endif
463 1.14 itojun free(ip6af, M_FTABLE);
464 1.2 itojun goto dropfrag;
465 1.2 itojun }
466 1.2 itojun }
467 1.2 itojun if (af6 != (struct ip6asfrag *)q6) {
468 1.2 itojun i = (ip6af->ip6af_off + ip6af->ip6af_frglen) - af6->ip6af_off;
469 1.2 itojun if (i > 0) {
470 1.14 itojun #if 0 /* suppress the noisy log */
471 1.2 itojun log(LOG_ERR, "%d bytes of a fragment from %s "
472 1.2 itojun "overlaps the succeeding fragment",
473 1.2 itojun i, ip6_sprintf(&q6->ip6q_src));
474 1.14 itojun #endif
475 1.14 itojun free(ip6af, M_FTABLE);
476 1.2 itojun goto dropfrag;
477 1.2 itojun }
478 1.2 itojun }
479 1.2 itojun #endif
480 1.2 itojun
481 1.2 itojun insert:
482 1.2 itojun
483 1.2 itojun /*
484 1.2 itojun * Stick new segment in its place;
485 1.2 itojun * check for complete reassembly.
486 1.2 itojun * Move to front of packet queue, as we are
487 1.2 itojun * the most recently active fragmented packet.
488 1.2 itojun */
489 1.2 itojun frag6_enq(ip6af, af6->ip6af_up);
490 1.18 itojun frag6_nfrags++;
491 1.18 itojun q6->ip6q_nfrag++;
492 1.2 itojun #if 0 /* xxx */
493 1.2 itojun if (q6 != ip6q.ip6q_next) {
494 1.2 itojun frag6_remque(q6);
495 1.2 itojun frag6_insque(q6, &ip6q);
496 1.2 itojun }
497 1.2 itojun #endif
498 1.2 itojun next = 0;
499 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
500 1.2 itojun af6 = af6->ip6af_down) {
501 1.2 itojun if (af6->ip6af_off != next) {
502 1.17 itojun IP6Q_UNLOCK();
503 1.2 itojun return IPPROTO_DONE;
504 1.2 itojun }
505 1.2 itojun next += af6->ip6af_frglen;
506 1.2 itojun }
507 1.2 itojun if (af6->ip6af_up->ip6af_mff) {
508 1.17 itojun IP6Q_UNLOCK();
509 1.2 itojun return IPPROTO_DONE;
510 1.2 itojun }
511 1.2 itojun
512 1.2 itojun /*
513 1.2 itojun * Reassembly is complete; concatenate fragments.
514 1.2 itojun */
515 1.2 itojun ip6af = q6->ip6q_down;
516 1.2 itojun t = m = IP6_REASS_MBUF(ip6af);
517 1.2 itojun af6 = ip6af->ip6af_down;
518 1.9 itojun frag6_deq(ip6af);
519 1.2 itojun while (af6 != (struct ip6asfrag *)q6) {
520 1.9 itojun af6dwn = af6->ip6af_down;
521 1.9 itojun frag6_deq(af6);
522 1.2 itojun while (t->m_next)
523 1.2 itojun t = t->m_next;
524 1.2 itojun t->m_next = IP6_REASS_MBUF(af6);
525 1.9 itojun m_adj(t->m_next, af6->ip6af_offset);
526 1.9 itojun free(af6, M_FTABLE);
527 1.9 itojun af6 = af6dwn;
528 1.2 itojun }
529 1.2 itojun
530 1.2 itojun /* adjust offset to point where the original next header starts */
531 1.2 itojun offset = ip6af->ip6af_offset - sizeof(struct ip6_frag);
532 1.9 itojun free(ip6af, M_FTABLE);
533 1.9 itojun ip6 = mtod(m, struct ip6_hdr *);
534 1.25 itojun ip6->ip6_plen = htons(next + offset - sizeof(struct ip6_hdr));
535 1.2 itojun ip6->ip6_src = q6->ip6q_src;
536 1.2 itojun ip6->ip6_dst = q6->ip6q_dst;
537 1.2 itojun nxt = q6->ip6q_nxt;
538 1.2 itojun #ifdef notyet
539 1.2 itojun *q6->ip6q_nxtp = (u_char)(nxt & 0xff);
540 1.2 itojun #endif
541 1.2 itojun
542 1.2 itojun /*
543 1.2 itojun * Delete frag6 header with as a few cost as possible.
544 1.2 itojun */
545 1.9 itojun if (offset < m->m_len) {
546 1.7 itojun ovbcopy((caddr_t)ip6, (caddr_t)ip6 + sizeof(struct ip6_frag),
547 1.2 itojun offset);
548 1.9 itojun m->m_data += sizeof(struct ip6_frag);
549 1.9 itojun m->m_len -= sizeof(struct ip6_frag);
550 1.9 itojun } else {
551 1.9 itojun /* this comes with no copy if the boundary is on cluster */
552 1.9 itojun if ((t = m_split(m, offset, M_DONTWAIT)) == NULL) {
553 1.9 itojun frag6_remque(q6);
554 1.18 itojun frag6_nfrags -= q6->ip6q_nfrag;
555 1.9 itojun free(q6, M_FTABLE);
556 1.9 itojun frag6_nfragpackets--;
557 1.9 itojun goto dropfrag;
558 1.9 itojun }
559 1.9 itojun m_adj(t, sizeof(struct ip6_frag));
560 1.9 itojun m_cat(m, t);
561 1.2 itojun }
562 1.2 itojun
563 1.2 itojun /*
564 1.2 itojun * Store NXT to the original.
565 1.2 itojun */
566 1.2 itojun {
567 1.21 itojun u_int8_t *prvnxtp = ip6_get_prevhdr(m, offset); /* XXX */
568 1.2 itojun *prvnxtp = nxt;
569 1.2 itojun }
570 1.2 itojun
571 1.2 itojun frag6_remque(q6);
572 1.18 itojun frag6_nfrags -= q6->ip6q_nfrag;
573 1.2 itojun free(q6, M_FTABLE);
574 1.2 itojun frag6_nfragpackets--;
575 1.2 itojun
576 1.2 itojun if (m->m_flags & M_PKTHDR) { /* Isn't it always true? */
577 1.2 itojun int plen = 0;
578 1.2 itojun for (t = m; t; t = t->m_next)
579 1.2 itojun plen += t->m_len;
580 1.2 itojun m->m_pkthdr.len = plen;
581 1.2 itojun }
582 1.20 itojun
583 1.2 itojun ip6stat.ip6s_reassembled++;
584 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_ok);
585 1.2 itojun
586 1.2 itojun /*
587 1.2 itojun * Tell launch routine the next header
588 1.2 itojun */
589 1.2 itojun
590 1.2 itojun *mp = m;
591 1.2 itojun *offp = offset;
592 1.2 itojun
593 1.17 itojun IP6Q_UNLOCK();
594 1.2 itojun return nxt;
595 1.2 itojun
596 1.2 itojun dropfrag:
597 1.7 itojun in6_ifstat_inc(dstifp, ifs6_reass_fail);
598 1.2 itojun ip6stat.ip6s_fragdropped++;
599 1.2 itojun m_freem(m);
600 1.17 itojun IP6Q_UNLOCK();
601 1.2 itojun return IPPROTO_DONE;
602 1.2 itojun }
603 1.2 itojun
604 1.2 itojun /*
605 1.2 itojun * Free a fragment reassembly header and all
606 1.2 itojun * associated datagrams.
607 1.2 itojun */
608 1.2 itojun void
609 1.2 itojun frag6_freef(q6)
610 1.2 itojun struct ip6q *q6;
611 1.2 itojun {
612 1.2 itojun struct ip6asfrag *af6, *down6;
613 1.2 itojun
614 1.17 itojun IP6Q_LOCK_CHECK();
615 1.17 itojun
616 1.2 itojun for (af6 = q6->ip6q_down; af6 != (struct ip6asfrag *)q6;
617 1.2 itojun af6 = down6) {
618 1.2 itojun struct mbuf *m = IP6_REASS_MBUF(af6);
619 1.2 itojun
620 1.2 itojun down6 = af6->ip6af_down;
621 1.2 itojun frag6_deq(af6);
622 1.2 itojun
623 1.2 itojun /*
624 1.2 itojun * Return ICMP time exceeded error for the 1st fragment.
625 1.2 itojun * Just free other fragments.
626 1.2 itojun */
627 1.2 itojun if (af6->ip6af_off == 0) {
628 1.2 itojun struct ip6_hdr *ip6;
629 1.2 itojun
630 1.2 itojun /* adjust pointer */
631 1.2 itojun ip6 = mtod(m, struct ip6_hdr *);
632 1.2 itojun
633 1.2 itojun /* restoure source and destination addresses */
634 1.2 itojun ip6->ip6_src = q6->ip6q_src;
635 1.2 itojun ip6->ip6_dst = q6->ip6q_dst;
636 1.2 itojun
637 1.2 itojun icmp6_error(m, ICMP6_TIME_EXCEEDED,
638 1.2 itojun ICMP6_TIME_EXCEED_REASSEMBLY, 0);
639 1.9 itojun } else
640 1.2 itojun m_freem(m);
641 1.9 itojun free(af6, M_FTABLE);
642 1.2 itojun }
643 1.2 itojun frag6_remque(q6);
644 1.18 itojun frag6_nfrags -= q6->ip6q_nfrag;
645 1.2 itojun free(q6, M_FTABLE);
646 1.2 itojun frag6_nfragpackets--;
647 1.2 itojun }
648 1.2 itojun
649 1.2 itojun /*
650 1.2 itojun * Put an ip fragment on a reassembly chain.
651 1.2 itojun * Like insque, but pointers in middle of structure.
652 1.2 itojun */
653 1.2 itojun void
654 1.2 itojun frag6_enq(af6, up6)
655 1.2 itojun struct ip6asfrag *af6, *up6;
656 1.2 itojun {
657 1.17 itojun
658 1.17 itojun IP6Q_LOCK_CHECK();
659 1.17 itojun
660 1.2 itojun af6->ip6af_up = up6;
661 1.2 itojun af6->ip6af_down = up6->ip6af_down;
662 1.2 itojun up6->ip6af_down->ip6af_up = af6;
663 1.2 itojun up6->ip6af_down = af6;
664 1.2 itojun }
665 1.2 itojun
666 1.2 itojun /*
667 1.2 itojun * To frag6_enq as remque is to insque.
668 1.2 itojun */
669 1.2 itojun void
670 1.2 itojun frag6_deq(af6)
671 1.2 itojun struct ip6asfrag *af6;
672 1.2 itojun {
673 1.17 itojun
674 1.17 itojun IP6Q_LOCK_CHECK();
675 1.17 itojun
676 1.2 itojun af6->ip6af_up->ip6af_down = af6->ip6af_down;
677 1.2 itojun af6->ip6af_down->ip6af_up = af6->ip6af_up;
678 1.2 itojun }
679 1.2 itojun
680 1.11 itojun void
681 1.2 itojun frag6_insque(new, old)
682 1.2 itojun struct ip6q *new, *old;
683 1.2 itojun {
684 1.17 itojun
685 1.17 itojun IP6Q_LOCK_CHECK();
686 1.17 itojun
687 1.2 itojun new->ip6q_prev = old;
688 1.2 itojun new->ip6q_next = old->ip6q_next;
689 1.2 itojun old->ip6q_next->ip6q_prev= new;
690 1.2 itojun old->ip6q_next = new;
691 1.2 itojun }
692 1.2 itojun
693 1.2 itojun void
694 1.2 itojun frag6_remque(p6)
695 1.2 itojun struct ip6q *p6;
696 1.2 itojun {
697 1.17 itojun
698 1.17 itojun IP6Q_LOCK_CHECK();
699 1.17 itojun
700 1.2 itojun p6->ip6q_prev->ip6q_next = p6->ip6q_next;
701 1.2 itojun p6->ip6q_next->ip6q_prev = p6->ip6q_prev;
702 1.2 itojun }
703 1.2 itojun
704 1.2 itojun /*
705 1.11 itojun * IPv6 reassembling timer processing;
706 1.2 itojun * if a timer expires on a reassembly
707 1.2 itojun * queue, discard it.
708 1.2 itojun */
709 1.2 itojun void
710 1.2 itojun frag6_slowtimo()
711 1.2 itojun {
712 1.2 itojun struct ip6q *q6;
713 1.4 itojun int s = splsoftnet();
714 1.2 itojun
715 1.17 itojun IP6Q_LOCK();
716 1.2 itojun q6 = ip6q.ip6q_next;
717 1.2 itojun if (q6)
718 1.2 itojun while (q6 != &ip6q) {
719 1.2 itojun --q6->ip6q_ttl;
720 1.2 itojun q6 = q6->ip6q_next;
721 1.2 itojun if (q6->ip6q_prev->ip6q_ttl == 0) {
722 1.2 itojun ip6stat.ip6s_fragtimeout++;
723 1.7 itojun /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
724 1.2 itojun frag6_freef(q6->ip6q_prev);
725 1.2 itojun }
726 1.2 itojun }
727 1.2 itojun /*
728 1.2 itojun * If we are over the maximum number of fragments
729 1.2 itojun * (due to the limit being lowered), drain off
730 1.2 itojun * enough to get down to the new limit.
731 1.2 itojun */
732 1.13 itojun while (frag6_nfragpackets > (u_int)ip6_maxfragpackets &&
733 1.13 itojun ip6q.ip6q_prev) {
734 1.2 itojun ip6stat.ip6s_fragoverflow++;
735 1.7 itojun /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
736 1.2 itojun frag6_freef(ip6q.ip6q_prev);
737 1.2 itojun }
738 1.17 itojun IP6Q_UNLOCK();
739 1.2 itojun
740 1.2 itojun #if 0
741 1.2 itojun /*
742 1.2 itojun * Routing changes might produce a better route than we last used;
743 1.2 itojun * make sure we notice eventually, even if forwarding only for one
744 1.2 itojun * destination and the cache is never replaced.
745 1.2 itojun */
746 1.2 itojun if (ip6_forward_rt.ro_rt) {
747 1.2 itojun RTFREE(ip6_forward_rt.ro_rt);
748 1.2 itojun ip6_forward_rt.ro_rt = 0;
749 1.2 itojun }
750 1.2 itojun if (ipsrcchk_rt.ro_rt) {
751 1.2 itojun RTFREE(ipsrcchk_rt.ro_rt);
752 1.2 itojun ipsrcchk_rt.ro_rt = 0;
753 1.2 itojun }
754 1.2 itojun #endif
755 1.2 itojun
756 1.2 itojun splx(s);
757 1.2 itojun }
758 1.2 itojun
759 1.2 itojun /*
760 1.2 itojun * Drain off all datagram fragments.
761 1.2 itojun */
762 1.2 itojun void
763 1.2 itojun frag6_drain()
764 1.2 itojun {
765 1.17 itojun
766 1.17 itojun if (ip6q_lock_try() == 0)
767 1.2 itojun return;
768 1.2 itojun while (ip6q.ip6q_next != &ip6q) {
769 1.2 itojun ip6stat.ip6s_fragdropped++;
770 1.7 itojun /* XXX in6_ifstat_inc(ifp, ifs6_reass_fail) */
771 1.2 itojun frag6_freef(ip6q.ip6q_next);
772 1.2 itojun }
773 1.17 itojun IP6Q_UNLOCK();
774 1.2 itojun }
775