]>
Commit | Line | Data |
---|---|---|
1 | /* $FreeBSD: src/sys/netinet6/ip6_input.c,v 1.11.2.10 2001/07/24 19:10:18 brooks Exp $ */ | |
2 | /* $KAME: ip6_input.c,v 1.194 2001/05/27 13:28:35 itojun Exp $ */ | |
3 | ||
4 | /* | |
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the name of the project nor the names of its contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | /* | |
34 | * Copyright (c) 1982, 1986, 1988, 1993 | |
35 | * The Regents of the University of California. All rights reserved. | |
36 | * | |
37 | * Redistribution and use in source and binary forms, with or without | |
38 | * modification, are permitted provided that the following conditions | |
39 | * are met: | |
40 | * 1. Redistributions of source code must retain the above copyright | |
41 | * notice, this list of conditions and the following disclaimer. | |
42 | * 2. Redistributions in binary form must reproduce the above copyright | |
43 | * notice, this list of conditions and the following disclaimer in the | |
44 | * documentation and/or other materials provided with the distribution. | |
45 | * 3. All advertising materials mentioning features or use of this software | |
46 | * must display the following acknowledgement: | |
47 | * This product includes software developed by the University of | |
48 | * California, Berkeley and its contributors. | |
49 | * 4. Neither the name of the University nor the names of its contributors | |
50 | * may be used to endorse or promote products derived from this software | |
51 | * without specific prior written permission. | |
52 | * | |
53 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
54 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
55 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
56 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
57 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
58 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
59 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
60 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
61 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
62 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
63 | * SUCH DAMAGE. | |
64 | * | |
65 | * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 | |
66 | */ | |
67 | ||
68 | ||
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/malloc.h> | |
72 | #include <sys/mbuf.h> | |
73 | #include <sys/domain.h> | |
74 | #include <sys/protosw.h> | |
75 | #include <sys/socket.h> | |
76 | #include <sys/socketvar.h> | |
77 | #include <sys/errno.h> | |
78 | #include <sys/time.h> | |
79 | #include <sys/kernel.h> | |
80 | #include <sys/syslog.h> | |
81 | #include <sys/proc.h> | |
82 | #include <sys/kauth.h> | |
83 | ||
84 | #include <net/if.h> | |
85 | #include <net/if_var.h> | |
86 | #include <net/if_types.h> | |
87 | #include <net/if_dl.h> | |
88 | #include <net/route.h> | |
89 | ||
90 | #include <netinet/in.h> | |
91 | #include <netinet/in_systm.h> | |
92 | #if INET | |
93 | #include <netinet/ip.h> | |
94 | #include <netinet/ip_icmp.h> | |
95 | #endif /*INET*/ | |
96 | #include <netinet/ip6.h> | |
97 | #include <netinet6/in6_var.h> | |
98 | #include <netinet6/ip6_var.h> | |
99 | #include <netinet/in_pcb.h> | |
100 | #include <netinet/icmp6.h> | |
101 | #include <netinet6/in6_ifattach.h> | |
102 | #include <netinet6/nd6.h> | |
103 | #include <netinet6/in6_prefix.h> | |
104 | ||
105 | #if IPSEC | |
106 | #include <netinet6/ipsec.h> | |
107 | #if INET6 | |
108 | #include <netinet6/ipsec6.h> | |
109 | #endif | |
110 | extern int ipsec_bypass; | |
111 | extern lck_mtx_t *sadb_mutex; | |
112 | #endif | |
113 | ||
114 | #include <netinet6/ip6_fw.h> | |
115 | ||
116 | #include <netinet/kpi_ipfilter_var.h> | |
117 | ||
118 | #include <netinet6/ip6protosw.h> | |
119 | ||
120 | /* we need it for NLOOP. */ | |
121 | #include "loop.h" | |
122 | #include "faith.h" | |
123 | ||
124 | #include <net/net_osdep.h> | |
125 | ||
126 | extern struct domain inet6domain; | |
127 | extern struct ip6protosw inet6sw[]; | |
128 | ||
129 | struct ip6protosw * ip6_protox[IPPROTO_MAX]; | |
130 | static int ip6qmaxlen = IFQ_MAXLEN; | |
131 | struct in6_ifaddr *in6_ifaddrs; | |
132 | ||
133 | int ip6_forward_srcrt; /* XXX */ | |
134 | int ip6_sourcecheck; /* XXX */ | |
135 | int ip6_sourcecheck_interval; /* XXX */ | |
136 | const int int6intrq_present = 1; | |
137 | ||
138 | int ip6_ours_check_algorithm; | |
139 | int in6_init2done = 0; | |
140 | ||
141 | ||
142 | /* firewall hooks */ | |
143 | ip6_fw_chk_t *ip6_fw_chk_ptr; | |
144 | ip6_fw_ctl_t *ip6_fw_ctl_ptr; | |
145 | int ip6_fw_enable = 1; | |
146 | ||
147 | struct ip6stat ip6stat; | |
148 | ||
149 | #ifdef __APPLE__ | |
150 | struct ifqueue ip6intrq; | |
151 | lck_mtx_t *ip6_mutex; | |
152 | lck_mtx_t *dad6_mutex; | |
153 | lck_mtx_t *nd6_mutex; | |
154 | lck_mtx_t *prefix6_mutex; | |
155 | lck_attr_t *ip6_mutex_attr; | |
156 | lck_grp_t *ip6_mutex_grp; | |
157 | lck_grp_attr_t *ip6_mutex_grp_attr; | |
158 | extern lck_mtx_t *inet6_domain_mutex; | |
159 | #endif | |
160 | extern int loopattach_done; | |
161 | ||
162 | static void ip6_init2(void *); | |
163 | static struct mbuf *ip6_setdstifaddr(struct mbuf *, struct in6_ifaddr *); | |
164 | ||
165 | static int ip6_hopopts_input(u_int32_t *, u_int32_t *, struct mbuf **, int *); | |
166 | #if PULLDOWN_TEST | |
167 | static struct mbuf *ip6_pullexthdr(struct mbuf *, size_t, int); | |
168 | #endif | |
169 | ||
170 | #ifdef __APPLE__ | |
171 | void gifattach(void); | |
172 | void faithattach(void); | |
173 | void stfattach(void); | |
174 | #endif | |
175 | ||
176 | static void | |
177 | ip6_proto_input( | |
178 | protocol_family_t protocol, | |
179 | mbuf_t packet) | |
180 | { | |
181 | ip6_input(packet); | |
182 | } | |
183 | ||
184 | /* | |
185 | * IP6 initialization: fill in IP6 protocol switch table. | |
186 | * All protocols not implemented in kernel go to raw IP6 protocol handler. | |
187 | */ | |
188 | void | |
189 | ip6_init() | |
190 | { | |
191 | struct ip6protosw *pr; | |
192 | int i; | |
193 | struct timeval tv; | |
194 | extern lck_mtx_t *domain_proto_mtx; | |
195 | ||
196 | #if DIAGNOSTIC | |
197 | if (sizeof(struct protosw) != sizeof(struct ip6protosw)) | |
198 | panic("sizeof(protosw) != sizeof(ip6protosw)"); | |
199 | #endif | |
200 | pr = (struct ip6protosw *)pffindproto_locked(PF_INET6, IPPROTO_RAW, SOCK_RAW); | |
201 | if (pr == 0) | |
202 | panic("ip6_init"); | |
203 | for (i = 0; i < IPPROTO_MAX; i++) | |
204 | ip6_protox[i] = pr; | |
205 | for (pr = (struct ip6protosw*)inet6domain.dom_protosw; pr; pr = pr->pr_next) { | |
206 | if(!((unsigned int)pr->pr_domain)) continue; /* If uninitialized, skip */ | |
207 | if (pr->pr_domain->dom_family == PF_INET6 && | |
208 | pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) { | |
209 | ip6_protox[pr->pr_protocol] = pr; | |
210 | } | |
211 | } | |
212 | ||
213 | ip6_mutex_grp_attr = lck_grp_attr_alloc_init(); | |
214 | lck_grp_attr_setdefault(ip6_mutex_grp_attr); | |
215 | ||
216 | ip6_mutex_grp = lck_grp_alloc_init("ip6", ip6_mutex_grp_attr); | |
217 | ip6_mutex_attr = lck_attr_alloc_init(); | |
218 | lck_attr_setdefault(ip6_mutex_attr); | |
219 | ||
220 | if ((ip6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) { | |
221 | printf("ip6_init: can't alloc ip6_mutex\n"); | |
222 | return; | |
223 | } | |
224 | if ((dad6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) { | |
225 | printf("ip6_init: can't alloc dad6_mutex\n"); | |
226 | return; | |
227 | } | |
228 | if ((nd6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) { | |
229 | printf("ip6_init: can't alloc nd6_mutex\n"); | |
230 | return; | |
231 | } | |
232 | ||
233 | if ((prefix6_mutex = lck_mtx_alloc_init(ip6_mutex_grp, ip6_mutex_attr)) == NULL) { | |
234 | printf("ip6_init: can't alloc prefix6_mutex\n"); | |
235 | return; | |
236 | } | |
237 | ||
238 | inet6domain.dom_flags = DOM_REENTRANT; | |
239 | ||
240 | ip6intrq.ifq_maxlen = ip6qmaxlen; | |
241 | nd6_init(); | |
242 | frag6_init(); | |
243 | icmp6_init(); | |
244 | /* | |
245 | * in many cases, random() here does NOT return random number | |
246 | * as initialization during bootstrap time occur in fixed order. | |
247 | */ | |
248 | microtime(&tv); | |
249 | ip6_flow_seq = random() ^ tv.tv_usec; | |
250 | microtime(&tv); | |
251 | ip6_desync_factor = (random() ^ tv.tv_usec) % MAX_TEMP_DESYNC_FACTOR; | |
252 | timeout(ip6_init2, (caddr_t)0, 1 * hz); | |
253 | ||
254 | lck_mtx_unlock(domain_proto_mtx); | |
255 | proto_register_input(PF_INET6, ip6_proto_input, NULL); | |
256 | lck_mtx_lock(domain_proto_mtx); | |
257 | } | |
258 | ||
259 | static void | |
260 | ip6_init2(dummy) | |
261 | void *dummy; | |
262 | { | |
263 | /* | |
264 | * to route local address of p2p link to loopback, | |
265 | * assign loopback address first. | |
266 | */ | |
267 | if (loopattach_done == 0) { | |
268 | timeout(ip6_init2, (caddr_t)0, 1 * hz); | |
269 | return; | |
270 | } | |
271 | in6_ifattach(&loif[0], NULL, NULL); | |
272 | ||
273 | #ifdef __APPLE__ | |
274 | /* nd6_timer_init */ | |
275 | timeout(nd6_timer, (caddr_t)0, hz); | |
276 | ||
277 | /* router renumbering prefix list maintenance */ | |
278 | timeout(in6_rr_timer, (caddr_t)0, hz); | |
279 | ||
280 | /* timer for regeneranation of temporary addresses randomize ID */ | |
281 | timeout(in6_tmpaddrtimer, (caddr_t)0, | |
282 | (ip6_temp_preferred_lifetime - ip6_desync_factor - | |
283 | ip6_temp_regen_advance) * hz); | |
284 | ||
285 | #if NGIF | |
286 | gifattach(); | |
287 | #endif | |
288 | #if NFAITH | |
289 | faithattach(); | |
290 | #endif | |
291 | #if NSTF | |
292 | stfattach(); | |
293 | #endif | |
294 | #else | |
295 | /* nd6_timer_init */ | |
296 | ||
297 | callout_init(&nd6_timer_ch); | |
298 | callout_reset(&nd6_timer_ch, hz, nd6_timer, NULL); | |
299 | ||
300 | /* router renumbering prefix list maintenance */ | |
301 | callout_init(&in6_rr_timer_ch); | |
302 | callout_reset(&in6_rr_timer_ch, hz, in6_rr_timer, NULL); | |
303 | ||
304 | /* timer for regeneranation of temporary addresses randomize ID */ | |
305 | callout_reset(&in6_tmpaddrtimer_ch, | |
306 | (ip6_temp_preferred_lifetime - ip6_desync_factor - | |
307 | ip6_temp_regen_advance) * hz, | |
308 | in6_tmpaddrtimer, NULL); | |
309 | #endif | |
310 | ||
311 | in6_init2done = 1; | |
312 | } | |
313 | ||
314 | #if __FreeBSD__ | |
315 | /* cheat */ | |
316 | /* This must be after route_init(), which is now SI_ORDER_THIRD */ | |
317 | SYSINIT(netinet6init2, SI_SUB_PROTO_DOMAIN, SI_ORDER_MIDDLE, ip6_init2, NULL); | |
318 | #endif | |
319 | ||
320 | extern struct route_in6 ip6_forward_rt; | |
321 | ||
322 | void | |
323 | ip6_input(m) | |
324 | struct mbuf *m; | |
325 | { | |
326 | struct ip6_hdr *ip6; | |
327 | int off = sizeof(struct ip6_hdr), nest; | |
328 | u_int32_t plen; | |
329 | u_int32_t rtalert = ~0; | |
330 | int nxt = 0, ours = 0; | |
331 | struct ifnet *deliverifp = NULL; | |
332 | ipfilter_t inject_ipfref = 0; | |
333 | int seen; | |
334 | ||
335 | /* | |
336 | * No need to proccess packet twice if we've | |
337 | * already seen it | |
338 | */ | |
339 | inject_ipfref = ipf_get_inject_filter(m); | |
340 | if (inject_ipfref != 0) { | |
341 | ip6 = mtod(m, struct ip6_hdr *); | |
342 | nxt = ip6->ip6_nxt; | |
343 | seen = 0; | |
344 | goto injectit; | |
345 | } else | |
346 | seen = 1; | |
347 | ||
348 | #if IPSEC | |
349 | /* | |
350 | * should the inner packet be considered authentic? | |
351 | * see comment in ah4_input(). | |
352 | */ | |
353 | if (m) { | |
354 | m->m_flags &= ~M_AUTHIPHDR; | |
355 | m->m_flags &= ~M_AUTHIPDGM; | |
356 | } | |
357 | #endif | |
358 | ||
359 | /* | |
360 | * make sure we don't have onion peering information into m_aux. | |
361 | */ | |
362 | ip6_delaux(m); | |
363 | ||
364 | lck_mtx_lock(ip6_mutex); | |
365 | /* | |
366 | * mbuf statistics | |
367 | */ | |
368 | if (m->m_flags & M_EXT) { | |
369 | if (m->m_next) | |
370 | ip6stat.ip6s_mext2m++; | |
371 | else | |
372 | ip6stat.ip6s_mext1++; | |
373 | } else { | |
374 | #define M2MMAX (sizeof(ip6stat.ip6s_m2m)/sizeof(ip6stat.ip6s_m2m[0])) | |
375 | if (m->m_next) { | |
376 | if (m->m_flags & M_LOOP) { | |
377 | ip6stat.ip6s_m2m[loif[0].if_index]++; /* XXX */ | |
378 | } else if (m->m_pkthdr.rcvif->if_index < M2MMAX) | |
379 | ip6stat.ip6s_m2m[m->m_pkthdr.rcvif->if_index]++; | |
380 | else | |
381 | ip6stat.ip6s_m2m[0]++; | |
382 | } else | |
383 | ip6stat.ip6s_m1++; | |
384 | #undef M2MMAX | |
385 | } | |
386 | ||
387 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_receive); | |
388 | ip6stat.ip6s_total++; | |
389 | ||
390 | #ifndef PULLDOWN_TEST | |
391 | /* | |
392 | * L2 bridge code and some other code can return mbuf chain | |
393 | * that does not conform to KAME requirement. too bad. | |
394 | * XXX: fails to join if interface MTU > MCLBYTES. jumbogram? | |
395 | */ | |
396 | if (m && m->m_next != NULL && m->m_pkthdr.len < MCLBYTES) { | |
397 | struct mbuf *n; | |
398 | ||
399 | MGETHDR(n, M_DONTWAIT, MT_HEADER); | |
400 | if (n) | |
401 | M_COPY_PKTHDR(n, m); | |
402 | if (n && m->m_pkthdr.len > MHLEN) { | |
403 | MCLGET(n, M_DONTWAIT); | |
404 | if ((n->m_flags & M_EXT) == 0) { | |
405 | m_freem(n); | |
406 | n = NULL; | |
407 | } | |
408 | } | |
409 | if (n == NULL) { | |
410 | m_freem(m); | |
411 | lck_mtx_unlock(ip6_mutex); | |
412 | return; /*ENOBUFS*/ | |
413 | } | |
414 | ||
415 | m_copydata(m, 0, m->m_pkthdr.len, mtod(n, caddr_t)); | |
416 | n->m_len = m->m_pkthdr.len; | |
417 | m_freem(m); | |
418 | m = n; | |
419 | } | |
420 | IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), | |
421 | {lck_mtx_unlock(ip6_mutex); return;}); | |
422 | #endif | |
423 | ||
424 | if (m->m_len < sizeof(struct ip6_hdr)) { | |
425 | struct ifnet *inifp; | |
426 | inifp = m->m_pkthdr.rcvif; | |
427 | if ((m = m_pullup(m, sizeof(struct ip6_hdr))) == 0) { | |
428 | ip6stat.ip6s_toosmall++; | |
429 | in6_ifstat_inc(inifp, ifs6_in_hdrerr); | |
430 | lck_mtx_unlock(ip6_mutex); | |
431 | return; | |
432 | } | |
433 | } | |
434 | ||
435 | ip6 = mtod(m, struct ip6_hdr *); | |
436 | ||
437 | if ((ip6->ip6_vfc & IPV6_VERSION_MASK) != IPV6_VERSION) { | |
438 | ip6stat.ip6s_badvers++; | |
439 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); | |
440 | goto bad; | |
441 | } | |
442 | ||
443 | ip6stat.ip6s_nxthist[ip6->ip6_nxt]++; | |
444 | ||
445 | /* | |
446 | * Check with the firewall... | |
447 | */ | |
448 | if (ip6_fw_enable && ip6_fw_chk_ptr) { | |
449 | u_short port = 0; | |
450 | /* If ipfw says divert, we have to just drop packet */ | |
451 | /* use port as a dummy argument */ | |
452 | if ((*ip6_fw_chk_ptr)(&ip6, NULL, &port, &m)) { | |
453 | m_freem(m); | |
454 | m = NULL; | |
455 | } | |
456 | if (!m) { | |
457 | lck_mtx_unlock(ip6_mutex); | |
458 | return; | |
459 | } | |
460 | } | |
461 | ||
462 | /* | |
463 | * Check against address spoofing/corruption. | |
464 | */ | |
465 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src) || | |
466 | IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_dst)) { | |
467 | /* | |
468 | * XXX: "badscope" is not very suitable for a multicast source. | |
469 | */ | |
470 | ip6stat.ip6s_badscope++; | |
471 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); | |
472 | goto bad; | |
473 | } | |
474 | if ((IN6_IS_ADDR_LOOPBACK(&ip6->ip6_src) || | |
475 | IN6_IS_ADDR_LOOPBACK(&ip6->ip6_dst)) && | |
476 | (m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { | |
477 | ip6stat.ip6s_badscope++; | |
478 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); | |
479 | goto bad; | |
480 | } | |
481 | ||
482 | /* | |
483 | * The following check is not documented in specs. A malicious | |
484 | * party may be able to use IPv4 mapped addr to confuse tcp/udp stack | |
485 | * and bypass security checks (act as if it was from 127.0.0.1 by using | |
486 | * IPv6 src ::ffff:127.0.0.1). Be cautious. | |
487 | * | |
488 | * This check chokes if we are in an SIIT cloud. As none of BSDs | |
489 | * support IPv4-less kernel compilation, we cannot support SIIT | |
490 | * environment at all. So, it makes more sense for us to reject any | |
491 | * malicious packets for non-SIIT environment, than try to do a | |
492 | * partical support for SIIT environment. | |
493 | */ | |
494 | if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || | |
495 | IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { | |
496 | ip6stat.ip6s_badscope++; | |
497 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); | |
498 | goto bad; | |
499 | } | |
500 | #if 0 | |
501 | /* | |
502 | * Reject packets with IPv4 compatible addresses (auto tunnel). | |
503 | * | |
504 | * The code forbids auto tunnel relay case in RFC1933 (the check is | |
505 | * stronger than RFC1933). We may want to re-enable it if mech-xx | |
506 | * is revised to forbid relaying case. | |
507 | */ | |
508 | if (IN6_IS_ADDR_V4COMPAT(&ip6->ip6_src) || | |
509 | IN6_IS_ADDR_V4COMPAT(&ip6->ip6_dst)) { | |
510 | ip6stat.ip6s_badscope++; | |
511 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); | |
512 | goto bad; | |
513 | } | |
514 | #endif | |
515 | ||
516 | /* drop packets if interface ID portion is already filled */ | |
517 | if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { | |
518 | if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src) && | |
519 | ip6->ip6_src.s6_addr16[1]) { | |
520 | ip6stat.ip6s_badscope++; | |
521 | goto bad; | |
522 | } | |
523 | if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) && | |
524 | ip6->ip6_dst.s6_addr16[1]) { | |
525 | ip6stat.ip6s_badscope++; | |
526 | goto bad; | |
527 | } | |
528 | } | |
529 | ||
530 | if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) | |
531 | ip6->ip6_src.s6_addr16[1] | |
532 | = htons(m->m_pkthdr.rcvif->if_index); | |
533 | if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) | |
534 | ip6->ip6_dst.s6_addr16[1] | |
535 | = htons(m->m_pkthdr.rcvif->if_index); | |
536 | ||
537 | #if 0 /* this case seems to be unnecessary. (jinmei, 20010401) */ | |
538 | /* | |
539 | * We use rt->rt_ifp to determine if the address is ours or not. | |
540 | * If rt_ifp is lo0, the address is ours. | |
541 | * The problem here is, rt->rt_ifp for fe80::%lo0/64 is set to lo0, | |
542 | * so any address under fe80::%lo0/64 will be mistakenly considered | |
543 | * local. The special case is supplied to handle the case properly | |
544 | * by actually looking at interface addresses | |
545 | * (using in6ifa_ifpwithaddr). | |
546 | */ | |
547 | if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) != 0 && | |
548 | IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) { | |
549 | struct in6_ifaddr *ia6; | |
550 | if (!(ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif, &ip6->ip6_dst))) { | |
551 | lck_mtx_unlock(ip6_mutex); | |
552 | icmp6_error(m, ICMP6_DST_UNREACH, | |
553 | ICMP6_DST_UNREACH_ADDR, 0); | |
554 | /* m is already freed */ | |
555 | return; | |
556 | } | |
557 | ifafree(&ia6->ia_ifa); | |
558 | ||
559 | ours = 1; | |
560 | deliverifp = m->m_pkthdr.rcvif; | |
561 | goto hbhcheck; | |
562 | } | |
563 | #endif | |
564 | ||
565 | /* | |
566 | * Multicast check | |
567 | */ | |
568 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
569 | struct in6_multi *in6m = 0; | |
570 | ||
571 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mcast); | |
572 | /* | |
573 | * See if we belong to the destination multicast group on the | |
574 | * arrival interface. | |
575 | */ | |
576 | IN6_LOOKUP_MULTI(ip6->ip6_dst, m->m_pkthdr.rcvif, in6m); | |
577 | if (in6m) | |
578 | ours = 1; | |
579 | else if (!ip6_mrouter) { | |
580 | ip6stat.ip6s_notmember++; | |
581 | ip6stat.ip6s_cantforward++; | |
582 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); | |
583 | goto bad; | |
584 | } | |
585 | deliverifp = m->m_pkthdr.rcvif; | |
586 | goto hbhcheck; | |
587 | } | |
588 | ||
589 | /* | |
590 | * Unicast check | |
591 | */ | |
592 | switch (ip6_ours_check_algorithm) { | |
593 | default: | |
594 | /* | |
595 | * XXX: I intentionally broke our indentation rule here, | |
596 | * since this switch-case is just for measurement and | |
597 | * therefore should soon be removed. | |
598 | */ | |
599 | if (ip6_forward_rt.ro_rt != NULL && | |
600 | (ip6_forward_rt.ro_rt->rt_flags & RTF_UP) != 0 && | |
601 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, | |
602 | &((struct sockaddr_in6 *)(&ip6_forward_rt.ro_dst))->sin6_addr)) | |
603 | ip6stat.ip6s_forward_cachehit++; | |
604 | else { | |
605 | struct sockaddr_in6 *dst6; | |
606 | ||
607 | if (ip6_forward_rt.ro_rt) { | |
608 | /* route is down or destination is different */ | |
609 | ip6stat.ip6s_forward_cachemiss++; | |
610 | rtfree(ip6_forward_rt.ro_rt); | |
611 | ip6_forward_rt.ro_rt = 0; | |
612 | } | |
613 | ||
614 | bzero(&ip6_forward_rt.ro_dst, sizeof(struct sockaddr_in6)); | |
615 | dst6 = (struct sockaddr_in6 *)&ip6_forward_rt.ro_dst; | |
616 | dst6->sin6_len = sizeof(struct sockaddr_in6); | |
617 | dst6->sin6_family = AF_INET6; | |
618 | dst6->sin6_addr = ip6->ip6_dst; | |
619 | #if SCOPEDROUTING | |
620 | ip6_forward_rt.ro_dst.sin6_scope_id = | |
621 | in6_addr2scopeid(m->m_pkthdr.rcvif, &ip6->ip6_dst); | |
622 | #endif | |
623 | ||
624 | rtalloc_ign((struct route *)&ip6_forward_rt, RTF_PRCLONING); | |
625 | } | |
626 | ||
627 | #define rt6_key(r) ((struct sockaddr_in6 *)((r)->rt_nodes->rn_key)) | |
628 | ||
629 | /* | |
630 | * Accept the packet if the forwarding interface to the destination | |
631 | * according to the routing table is the loopback interface, | |
632 | * unless the associated route has a gateway. | |
633 | * Note that this approach causes to accept a packet if there is a | |
634 | * route to the loopback interface for the destination of the packet. | |
635 | * But we think it's even useful in some situations, e.g. when using | |
636 | * a special daemon which wants to intercept the packet. | |
637 | * | |
638 | * XXX: some OSes automatically make a cloned route for the destination | |
639 | * of an outgoing packet. If the outgoing interface of the packet | |
640 | * is a loopback one, the kernel would consider the packet to be | |
641 | * accepted, even if we have no such address assinged on the interface. | |
642 | * We check the cloned flag of the route entry to reject such cases, | |
643 | * assuming that route entries for our own addresses are not made by | |
644 | * cloning (it should be true because in6_addloop explicitly installs | |
645 | * the host route). However, we might have to do an explicit check | |
646 | * while it would be less efficient. Or, should we rather install a | |
647 | * reject route for such a case? | |
648 | */ | |
649 | if (ip6_forward_rt.ro_rt && | |
650 | (ip6_forward_rt.ro_rt->rt_flags & | |
651 | (RTF_HOST|RTF_GATEWAY)) == RTF_HOST && | |
652 | #if RTF_WASCLONED | |
653 | !(ip6_forward_rt.ro_rt->rt_flags & RTF_WASCLONED) && | |
654 | #endif | |
655 | #if RTF_CLONED | |
656 | !(ip6_forward_rt.ro_rt->rt_flags & RTF_CLONED) && | |
657 | #endif | |
658 | #if 0 | |
659 | /* | |
660 | * The check below is redundant since the comparison of | |
661 | * the destination and the key of the rtentry has | |
662 | * already done through looking up the routing table. | |
663 | */ | |
664 | IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, | |
665 | &rt6_key(ip6_forward_rt.ro_rt)->sin6_addr) | |
666 | #endif | |
667 | ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_LOOP) { | |
668 | struct in6_ifaddr *ia6 = | |
669 | (struct in6_ifaddr *)ip6_forward_rt.ro_rt->rt_ifa; | |
670 | ||
671 | /* | |
672 | * record address information into m_aux. | |
673 | */ | |
674 | (void)ip6_setdstifaddr(m, ia6); | |
675 | ||
676 | /* | |
677 | * packets to a tentative, duplicated, or somehow invalid | |
678 | * address must not be accepted. | |
679 | */ | |
680 | if (!(ia6->ia6_flags & IN6_IFF_NOTREADY)) { | |
681 | /* this address is ready */ | |
682 | ours = 1; | |
683 | deliverifp = ia6->ia_ifp; /* correct? */ | |
684 | /* Count the packet in the ip address stats */ | |
685 | #ifndef __APPLE__ | |
686 | ||
687 | ia6->ia_ifa.if_ipackets++; | |
688 | ia6->ia_ifa.if_ibytes += m->m_pkthdr.len; | |
689 | #endif | |
690 | goto hbhcheck; | |
691 | } else { | |
692 | /* address is not ready, so discard the packet. */ | |
693 | nd6log((LOG_INFO, | |
694 | "ip6_input: packet to an unready address %s->%s\n", | |
695 | ip6_sprintf(&ip6->ip6_src), | |
696 | ip6_sprintf(&ip6->ip6_dst))); | |
697 | goto bad; | |
698 | } | |
699 | } | |
700 | } /* XXX indentation (see above) */ | |
701 | ||
702 | /* | |
703 | * FAITH(Firewall Aided Internet Translator) | |
704 | */ | |
705 | #if defined(NFAITH) && 0 < NFAITH | |
706 | if (ip6_keepfaith) { | |
707 | if (ip6_forward_rt.ro_rt && ip6_forward_rt.ro_rt->rt_ifp | |
708 | && ip6_forward_rt.ro_rt->rt_ifp->if_type == IFT_FAITH) { | |
709 | /* XXX do we need more sanity checks? */ | |
710 | ours = 1; | |
711 | deliverifp = ip6_forward_rt.ro_rt->rt_ifp; /* faith */ | |
712 | goto hbhcheck; | |
713 | } | |
714 | } | |
715 | #endif | |
716 | ||
717 | /* | |
718 | * Now there is no reason to process the packet if it's not our own | |
719 | * and we're not a router. | |
720 | */ | |
721 | if (!ip6_forwarding) { | |
722 | ip6stat.ip6s_cantforward++; | |
723 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); | |
724 | goto bad; | |
725 | } | |
726 | ||
727 | hbhcheck: | |
728 | /* | |
729 | * record address information into m_aux, if we don't have one yet. | |
730 | * note that we are unable to record it, if the address is not listed | |
731 | * as our interface address (e.g. multicast addresses, addresses | |
732 | * within FAITH prefixes and such). | |
733 | */ | |
734 | if (deliverifp && !ip6_getdstifaddr(m)) { | |
735 | struct in6_ifaddr *ia6; | |
736 | ||
737 | ia6 = in6_ifawithifp(deliverifp, &ip6->ip6_dst); | |
738 | if (ia6) { | |
739 | if (!ip6_setdstifaddr(m, ia6)) { | |
740 | /* | |
741 | * XXX maybe we should drop the packet here, | |
742 | * as we could not provide enough information | |
743 | * to the upper layers. | |
744 | */ | |
745 | } | |
746 | } | |
747 | } | |
748 | ||
749 | /* | |
750 | * Process Hop-by-Hop options header if it's contained. | |
751 | * m may be modified in ip6_hopopts_input(). | |
752 | * If a JumboPayload option is included, plen will also be modified. | |
753 | */ | |
754 | plen = (u_int32_t)ntohs(ip6->ip6_plen); | |
755 | if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { | |
756 | struct ip6_hbh *hbh; | |
757 | ||
758 | if (ip6_hopopts_input(&plen, &rtalert, &m, &off)) { | |
759 | #if 0 /*touches NULL pointer*/ | |
760 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); | |
761 | #endif | |
762 | lck_mtx_unlock(ip6_mutex); | |
763 | return; /* m have already been freed */ | |
764 | } | |
765 | ||
766 | /* adjust pointer */ | |
767 | ip6 = mtod(m, struct ip6_hdr *); | |
768 | ||
769 | /* | |
770 | * if the payload length field is 0 and the next header field | |
771 | * indicates Hop-by-Hop Options header, then a Jumbo Payload | |
772 | * option MUST be included. | |
773 | */ | |
774 | if (ip6->ip6_plen == 0 && plen == 0) { | |
775 | /* | |
776 | * Note that if a valid jumbo payload option is | |
777 | * contained, ip6_hoptops_input() must set a valid | |
778 | * (non-zero) payload length to the variable plen. | |
779 | */ | |
780 | ip6stat.ip6s_badoptions++; | |
781 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_discard); | |
782 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_hdrerr); | |
783 | lck_mtx_unlock(ip6_mutex); | |
784 | icmp6_error(m, ICMP6_PARAM_PROB, | |
785 | ICMP6_PARAMPROB_HEADER, | |
786 | (caddr_t)&ip6->ip6_plen - (caddr_t)ip6); | |
787 | return; | |
788 | } | |
789 | #ifndef PULLDOWN_TEST | |
790 | /* ip6_hopopts_input() ensures that mbuf is contiguous */ | |
791 | hbh = (struct ip6_hbh *)(ip6 + 1); | |
792 | #else | |
793 | IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), | |
794 | sizeof(struct ip6_hbh)); | |
795 | if (hbh == NULL) { | |
796 | ip6stat.ip6s_tooshort++; | |
797 | lck_mtx_unlock(ip6_mutex); | |
798 | return; | |
799 | } | |
800 | #endif | |
801 | nxt = hbh->ip6h_nxt; | |
802 | ||
803 | /* | |
804 | * accept the packet if a router alert option is included | |
805 | * and we act as an IPv6 router. | |
806 | */ | |
807 | if (rtalert != ~0 && ip6_forwarding) | |
808 | ours = 1; | |
809 | } else | |
810 | nxt = ip6->ip6_nxt; | |
811 | ||
812 | /* | |
813 | * Check that the amount of data in the buffers | |
814 | * is as at least much as the IPv6 header would have us expect. | |
815 | * Trim mbufs if longer than we expect. | |
816 | * Drop packet if shorter than we expect. | |
817 | */ | |
818 | if (m->m_pkthdr.len - sizeof(struct ip6_hdr) < plen) { | |
819 | ip6stat.ip6s_tooshort++; | |
820 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); | |
821 | goto bad; | |
822 | } | |
823 | if (m->m_pkthdr.len > sizeof(struct ip6_hdr) + plen) { | |
824 | if (m->m_len == m->m_pkthdr.len) { | |
825 | m->m_len = sizeof(struct ip6_hdr) + plen; | |
826 | m->m_pkthdr.len = sizeof(struct ip6_hdr) + plen; | |
827 | } else | |
828 | m_adj(m, sizeof(struct ip6_hdr) + plen - m->m_pkthdr.len); | |
829 | } | |
830 | ||
831 | /* | |
832 | * Forward if desirable. | |
833 | */ | |
834 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
835 | /* | |
836 | * If we are acting as a multicast router, all | |
837 | * incoming multicast packets are passed to the | |
838 | * kernel-level multicast forwarding function. | |
839 | * The packet is returned (relatively) intact; if | |
840 | * ip6_mforward() returns a non-zero value, the packet | |
841 | * must be discarded, else it may be accepted below. | |
842 | */ | |
843 | if (ip6_mrouter && ip6_mforward(ip6, m->m_pkthdr.rcvif, m)) { | |
844 | ip6stat.ip6s_cantforward++; | |
845 | m_freem(m); | |
846 | lck_mtx_unlock(ip6_mutex); | |
847 | return; | |
848 | } | |
849 | if (!ours) { | |
850 | m_freem(m); | |
851 | lck_mtx_unlock(ip6_mutex); | |
852 | return; | |
853 | } | |
854 | } else if (!ours) { | |
855 | ip6_forward(m, 0, 1); | |
856 | lck_mtx_unlock(ip6_mutex); | |
857 | return; | |
858 | } | |
859 | ||
860 | ip6 = mtod(m, struct ip6_hdr *); | |
861 | ||
862 | /* | |
863 | * Malicious party may be able to use IPv4 mapped addr to confuse | |
864 | * tcp/udp stack and bypass security checks (act as if it was from | |
865 | * 127.0.0.1 by using IPv6 src ::ffff:127.0.0.1). Be cautious. | |
866 | * | |
867 | * For SIIT end node behavior, you may want to disable the check. | |
868 | * However, you will become vulnerable to attacks using IPv4 mapped | |
869 | * source. | |
870 | */ | |
871 | if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || | |
872 | IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { | |
873 | ip6stat.ip6s_badscope++; | |
874 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_addrerr); | |
875 | goto bad; | |
876 | } | |
877 | ||
878 | /* | |
879 | * Tell launch routine the next header | |
880 | */ | |
881 | ip6stat.ip6s_delivered++; | |
882 | in6_ifstat_inc(deliverifp, ifs6_in_deliver); | |
883 | ||
884 | lck_mtx_unlock(ip6_mutex); | |
885 | injectit: | |
886 | nest = 0; | |
887 | ||
888 | while (nxt != IPPROTO_DONE) { | |
889 | struct ipfilter *filter; | |
890 | ||
891 | if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) { | |
892 | ip6stat.ip6s_toomanyhdr++; | |
893 | goto badunlocked; | |
894 | } | |
895 | ||
896 | /* | |
897 | * protection against faulty packet - there should be | |
898 | * more sanity checks in header chain processing. | |
899 | */ | |
900 | if (m->m_pkthdr.len < off) { | |
901 | ip6stat.ip6s_tooshort++; | |
902 | in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_truncated); | |
903 | goto badunlocked; | |
904 | } | |
905 | ||
906 | #if 0 | |
907 | /* | |
908 | * do we need to do it for every header? yeah, other | |
909 | * functions can play with it (like re-allocate and copy). | |
910 | */ | |
911 | mhist = ip6_addaux(m); | |
912 | if (mhist && M_TRAILINGSPACE(mhist) >= sizeof(nxt)) { | |
913 | hist = mtod(mhist, caddr_t) + mhist->m_len; | |
914 | bcopy(&nxt, hist, sizeof(nxt)); | |
915 | mhist->m_len += sizeof(nxt); | |
916 | } else { | |
917 | ip6stat.ip6s_toomanyhdr++; | |
918 | goto bad; | |
919 | } | |
920 | #endif | |
921 | ||
922 | #if IPSEC | |
923 | /* | |
924 | * enforce IPsec policy checking if we are seeing last header. | |
925 | * note that we do not visit this with protocols with pcb layer | |
926 | * code - like udp/tcp/raw ip. | |
927 | */ | |
928 | if ((ipsec_bypass == 0) && (ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0) { | |
929 | lck_mtx_lock(sadb_mutex); | |
930 | if (ipsec6_in_reject(m, NULL)) { | |
931 | ipsec6stat.in_polvio++; | |
932 | lck_mtx_unlock(sadb_mutex); | |
933 | goto badunlocked; | |
934 | } | |
935 | lck_mtx_unlock(sadb_mutex); | |
936 | } | |
937 | #endif | |
938 | ||
939 | /* | |
940 | * Call IP filter on last header only | |
941 | */ | |
942 | if ((ip6_protox[nxt]->pr_flags & PR_LASTHDR) != 0 && !TAILQ_EMPTY(&ipv6_filters)) { | |
943 | ipf_ref(); | |
944 | TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) { | |
945 | if (seen == 0) { | |
946 | if ((struct ipfilter *)inject_ipfref == filter) | |
947 | seen = 1; | |
948 | } else if (filter->ipf_filter.ipf_input) { | |
949 | errno_t result; | |
950 | ||
951 | result = filter->ipf_filter.ipf_input( | |
952 | filter->ipf_filter.cookie, (mbuf_t*)&m, off, nxt); | |
953 | if (result == EJUSTRETURN) { | |
954 | ipf_unref(); | |
955 | return; | |
956 | } | |
957 | if (result != 0) { | |
958 | ipf_unref(); | |
959 | m_freem(m); | |
960 | return; | |
961 | } | |
962 | } | |
963 | } | |
964 | ipf_unref(); | |
965 | } | |
966 | if (!(ip6_protox[nxt]->pr_flags & PR_PROTOLOCK)) { | |
967 | lck_mtx_lock(inet6_domain_mutex); | |
968 | nxt = (*ip6_protox[nxt]->pr_input)(&m, &off); | |
969 | lck_mtx_unlock(inet6_domain_mutex); | |
970 | } | |
971 | else | |
972 | nxt = (*ip6_protox[nxt]->pr_input)(&m, &off); | |
973 | } | |
974 | return; | |
975 | bad: | |
976 | lck_mtx_unlock(ip6_mutex); | |
977 | badunlocked: | |
978 | m_freem(m); | |
979 | return; | |
980 | } | |
981 | ||
982 | /* | |
983 | * set/grab in6_ifaddr correspond to IPv6 destination address. | |
984 | * XXX backward compatibility wrapper | |
985 | */ | |
986 | static struct mbuf * | |
987 | ip6_setdstifaddr(m, ia6) | |
988 | struct mbuf *m; | |
989 | struct in6_ifaddr *ia6; | |
990 | { | |
991 | struct mbuf *n; | |
992 | ||
993 | n = ip6_addaux(m); | |
994 | if (n) | |
995 | mtod(n, struct ip6aux *)->ip6a_dstia6 = ia6; | |
996 | return n; /* NULL if failed to set */ | |
997 | } | |
998 | ||
999 | struct in6_ifaddr * | |
1000 | ip6_getdstifaddr(m) | |
1001 | struct mbuf *m; | |
1002 | { | |
1003 | struct mbuf *n; | |
1004 | ||
1005 | n = ip6_findaux(m); | |
1006 | if (n) | |
1007 | return mtod(n, struct ip6aux *)->ip6a_dstia6; | |
1008 | else | |
1009 | return NULL; | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * Hop-by-Hop options header processing. If a valid jumbo payload option is | |
1014 | * included, the real payload length will be stored in plenp. | |
1015 | */ | |
1016 | static int | |
1017 | ip6_hopopts_input(plenp, rtalertp, mp, offp) | |
1018 | u_int32_t *plenp; | |
1019 | u_int32_t *rtalertp; /* XXX: should be stored more smart way */ | |
1020 | struct mbuf **mp; | |
1021 | int *offp; | |
1022 | { | |
1023 | struct mbuf *m = *mp; | |
1024 | int off = *offp, hbhlen; | |
1025 | struct ip6_hbh *hbh; | |
1026 | u_int8_t *opt; | |
1027 | ||
1028 | /* validation of the length of the header */ | |
1029 | #ifndef PULLDOWN_TEST | |
1030 | IP6_EXTHDR_CHECK(m, off, sizeof(*hbh), return -1); | |
1031 | hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); | |
1032 | hbhlen = (hbh->ip6h_len + 1) << 3; | |
1033 | ||
1034 | IP6_EXTHDR_CHECK(m, off, hbhlen, return -1); | |
1035 | hbh = (struct ip6_hbh *)(mtod(m, caddr_t) + off); | |
1036 | #else | |
1037 | IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, | |
1038 | sizeof(struct ip6_hdr), sizeof(struct ip6_hbh)); | |
1039 | if (hbh == NULL) { | |
1040 | ip6stat.ip6s_tooshort++; | |
1041 | return -1; | |
1042 | } | |
1043 | hbhlen = (hbh->ip6h_len + 1) << 3; | |
1044 | IP6_EXTHDR_GET(hbh, struct ip6_hbh *, m, sizeof(struct ip6_hdr), | |
1045 | hbhlen); | |
1046 | if (hbh == NULL) { | |
1047 | ip6stat.ip6s_tooshort++; | |
1048 | return -1; | |
1049 | } | |
1050 | #endif | |
1051 | off += hbhlen; | |
1052 | hbhlen -= sizeof(struct ip6_hbh); | |
1053 | opt = (u_int8_t *)hbh + sizeof(struct ip6_hbh); | |
1054 | ||
1055 | if (ip6_process_hopopts(m, (u_int8_t *)hbh + sizeof(struct ip6_hbh), | |
1056 | hbhlen, rtalertp, plenp) < 0) | |
1057 | return(-1); | |
1058 | ||
1059 | *offp = off; | |
1060 | *mp = m; | |
1061 | return(0); | |
1062 | } | |
1063 | ||
1064 | /* | |
1065 | * Search header for all Hop-by-hop options and process each option. | |
1066 | * This function is separate from ip6_hopopts_input() in order to | |
1067 | * handle a case where the sending node itself process its hop-by-hop | |
1068 | * options header. In such a case, the function is called from ip6_output(). | |
1069 | * | |
1070 | * The function assumes that hbh header is located right after the IPv6 header | |
1071 | * (RFC2460 p7), opthead is pointer into data content in m, and opthead to | |
1072 | * opthead + hbhlen is located in continuous memory region. | |
1073 | */ | |
1074 | int | |
1075 | ip6_process_hopopts(m, opthead, hbhlen, rtalertp, plenp) | |
1076 | struct mbuf *m; | |
1077 | u_int8_t *opthead; | |
1078 | int hbhlen; | |
1079 | u_int32_t *rtalertp; | |
1080 | u_int32_t *plenp; | |
1081 | { | |
1082 | struct ip6_hdr *ip6; | |
1083 | int optlen = 0; | |
1084 | u_int8_t *opt = opthead; | |
1085 | u_int16_t rtalert_val; | |
1086 | u_int32_t jumboplen; | |
1087 | const int erroff = sizeof(struct ip6_hdr) + sizeof(struct ip6_hbh); | |
1088 | ||
1089 | for (; hbhlen > 0; hbhlen -= optlen, opt += optlen) { | |
1090 | switch (*opt) { | |
1091 | case IP6OPT_PAD1: | |
1092 | optlen = 1; | |
1093 | break; | |
1094 | case IP6OPT_PADN: | |
1095 | if (hbhlen < IP6OPT_MINLEN) { | |
1096 | ip6stat.ip6s_toosmall++; | |
1097 | goto bad; | |
1098 | } | |
1099 | optlen = *(opt + 1) + 2; | |
1100 | break; | |
1101 | case IP6OPT_RTALERT: | |
1102 | /* XXX may need check for alignment */ | |
1103 | if (hbhlen < IP6OPT_RTALERT_LEN) { | |
1104 | ip6stat.ip6s_toosmall++; | |
1105 | goto bad; | |
1106 | } | |
1107 | if (*(opt + 1) != IP6OPT_RTALERT_LEN - 2) { | |
1108 | /* XXX stat */ | |
1109 | lck_mtx_unlock(ip6_mutex); | |
1110 | icmp6_error(m, ICMP6_PARAM_PROB, | |
1111 | ICMP6_PARAMPROB_HEADER, | |
1112 | erroff + opt + 1 - opthead); | |
1113 | lck_mtx_lock(ip6_mutex); | |
1114 | return(-1); | |
1115 | } | |
1116 | optlen = IP6OPT_RTALERT_LEN; | |
1117 | bcopy((caddr_t)(opt + 2), (caddr_t)&rtalert_val, 2); | |
1118 | *rtalertp = ntohs(rtalert_val); | |
1119 | break; | |
1120 | case IP6OPT_JUMBO: | |
1121 | /* XXX may need check for alignment */ | |
1122 | if (hbhlen < IP6OPT_JUMBO_LEN) { | |
1123 | ip6stat.ip6s_toosmall++; | |
1124 | goto bad; | |
1125 | } | |
1126 | if (*(opt + 1) != IP6OPT_JUMBO_LEN - 2) { | |
1127 | /* XXX stat */ | |
1128 | lck_mtx_unlock(ip6_mutex); | |
1129 | icmp6_error(m, ICMP6_PARAM_PROB, | |
1130 | ICMP6_PARAMPROB_HEADER, | |
1131 | erroff + opt + 1 - opthead); | |
1132 | lck_mtx_lock(ip6_mutex); | |
1133 | return(-1); | |
1134 | } | |
1135 | optlen = IP6OPT_JUMBO_LEN; | |
1136 | ||
1137 | /* | |
1138 | * IPv6 packets that have non 0 payload length | |
1139 | * must not contain a jumbo payload option. | |
1140 | */ | |
1141 | ip6 = mtod(m, struct ip6_hdr *); | |
1142 | if (ip6->ip6_plen) { | |
1143 | ip6stat.ip6s_badoptions++; | |
1144 | lck_mtx_unlock(ip6_mutex); | |
1145 | icmp6_error(m, ICMP6_PARAM_PROB, | |
1146 | ICMP6_PARAMPROB_HEADER, | |
1147 | erroff + opt - opthead); | |
1148 | lck_mtx_lock(ip6_mutex); | |
1149 | return(-1); | |
1150 | } | |
1151 | ||
1152 | /* | |
1153 | * We may see jumbolen in unaligned location, so | |
1154 | * we'd need to perform bcopy(). | |
1155 | */ | |
1156 | bcopy(opt + 2, &jumboplen, sizeof(jumboplen)); | |
1157 | jumboplen = (u_int32_t)htonl(jumboplen); | |
1158 | ||
1159 | #if 1 | |
1160 | /* | |
1161 | * if there are multiple jumbo payload options, | |
1162 | * *plenp will be non-zero and the packet will be | |
1163 | * rejected. | |
1164 | * the behavior may need some debate in ipngwg - | |
1165 | * multiple options does not make sense, however, | |
1166 | * there's no explicit mention in specification. | |
1167 | */ | |
1168 | if (*plenp != 0) { | |
1169 | ip6stat.ip6s_badoptions++; | |
1170 | lck_mtx_unlock(ip6_mutex); | |
1171 | icmp6_error(m, ICMP6_PARAM_PROB, | |
1172 | ICMP6_PARAMPROB_HEADER, | |
1173 | erroff + opt + 2 - opthead); | |
1174 | lck_mtx_lock(ip6_mutex); | |
1175 | return(-1); | |
1176 | } | |
1177 | #endif | |
1178 | ||
1179 | /* | |
1180 | * jumbo payload length must be larger than 65535. | |
1181 | */ | |
1182 | if (jumboplen <= IPV6_MAXPACKET) { | |
1183 | ip6stat.ip6s_badoptions++; | |
1184 | lck_mtx_unlock(ip6_mutex); | |
1185 | icmp6_error(m, ICMP6_PARAM_PROB, | |
1186 | ICMP6_PARAMPROB_HEADER, | |
1187 | erroff + opt + 2 - opthead); | |
1188 | lck_mtx_lock(ip6_mutex); | |
1189 | return(-1); | |
1190 | } | |
1191 | *plenp = jumboplen; | |
1192 | ||
1193 | break; | |
1194 | default: /* unknown option */ | |
1195 | if (hbhlen < IP6OPT_MINLEN) { | |
1196 | ip6stat.ip6s_toosmall++; | |
1197 | goto bad; | |
1198 | } | |
1199 | optlen = ip6_unknown_opt(opt, m, | |
1200 | erroff + opt - opthead, 1); | |
1201 | if (optlen == -1) { | |
1202 | /* ip6_unknown opt unlocked ip6_mutex */ | |
1203 | return(-1); | |
1204 | } | |
1205 | optlen += 2; | |
1206 | break; | |
1207 | } | |
1208 | } | |
1209 | ||
1210 | return(0); | |
1211 | ||
1212 | bad: | |
1213 | m_freem(m); | |
1214 | return(-1); | |
1215 | } | |
1216 | ||
1217 | /* | |
1218 | * Unknown option processing. | |
1219 | * The third argument `off' is the offset from the IPv6 header to the option, | |
1220 | * which is necessary if the IPv6 header the and option header and IPv6 header | |
1221 | * is not continuous in order to return an ICMPv6 error. | |
1222 | */ | |
1223 | int | |
1224 | ip6_unknown_opt(optp, m, off, locked) | |
1225 | u_int8_t *optp; | |
1226 | struct mbuf *m; | |
1227 | int off; | |
1228 | int locked; | |
1229 | { | |
1230 | struct ip6_hdr *ip6; | |
1231 | ||
1232 | switch (IP6OPT_TYPE(*optp)) { | |
1233 | case IP6OPT_TYPE_SKIP: /* ignore the option */ | |
1234 | return((int)*(optp + 1)); | |
1235 | case IP6OPT_TYPE_DISCARD: /* silently discard */ | |
1236 | m_freem(m); | |
1237 | return(-1); | |
1238 | case IP6OPT_TYPE_FORCEICMP: /* send ICMP even if multicasted */ | |
1239 | ip6stat.ip6s_badoptions++; | |
1240 | if (locked) | |
1241 | lck_mtx_unlock(ip6_mutex); | |
1242 | icmp6_error(m, ICMP6_PARAM_PROB, ICMP6_PARAMPROB_OPTION, off); | |
1243 | if (locked) | |
1244 | lck_mtx_lock(ip6_mutex); | |
1245 | return(-1); | |
1246 | case IP6OPT_TYPE_ICMP: /* send ICMP if not multicasted */ | |
1247 | ip6stat.ip6s_badoptions++; | |
1248 | ip6 = mtod(m, struct ip6_hdr *); | |
1249 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) || | |
1250 | (m->m_flags & (M_BCAST|M_MCAST))) | |
1251 | m_freem(m); | |
1252 | else { | |
1253 | if (locked) | |
1254 | lck_mtx_unlock(ip6_mutex); | |
1255 | icmp6_error(m, ICMP6_PARAM_PROB, | |
1256 | ICMP6_PARAMPROB_OPTION, off); | |
1257 | if (locked) | |
1258 | lck_mtx_lock(ip6_mutex); | |
1259 | } | |
1260 | return(-1); | |
1261 | } | |
1262 | ||
1263 | m_freem(m); /* XXX: NOTREACHED */ | |
1264 | return(-1); | |
1265 | } | |
1266 | ||
1267 | /* | |
1268 | * Create the "control" list for this pcb. | |
1269 | * The function will not modify mbuf chain at all. | |
1270 | * | |
1271 | * with KAME mbuf chain restriction: | |
1272 | * The routine will be called from upper layer handlers like tcp6_input(). | |
1273 | * Thus the routine assumes that the caller (tcp6_input) have already | |
1274 | * called IP6_EXTHDR_CHECK() and all the extension headers are located in the | |
1275 | * very first mbuf on the mbuf chain. | |
1276 | */ | |
1277 | void | |
1278 | ip6_savecontrol(in6p, mp, ip6, m) | |
1279 | struct inpcb *in6p; | |
1280 | struct mbuf **mp; | |
1281 | struct ip6_hdr *ip6; | |
1282 | struct mbuf *m; | |
1283 | { | |
1284 | int rthdr_exist = 0; | |
1285 | ||
1286 | #if SO_TIMESTAMP | |
1287 | if ((in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0) { | |
1288 | struct timeval tv; | |
1289 | ||
1290 | microtime(&tv); | |
1291 | *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), | |
1292 | SCM_TIMESTAMP, SOL_SOCKET); | |
1293 | if (*mp) { | |
1294 | mp = &(*mp)->m_next; | |
1295 | } | |
1296 | } | |
1297 | #endif | |
1298 | ||
1299 | /* RFC 2292 sec. 5 */ | |
1300 | if ((in6p->in6p_flags & IN6P_PKTINFO) != 0) { | |
1301 | struct in6_pktinfo pi6; | |
1302 | bcopy(&ip6->ip6_dst, &pi6.ipi6_addr, sizeof(struct in6_addr)); | |
1303 | if (IN6_IS_SCOPE_LINKLOCAL(&pi6.ipi6_addr)) | |
1304 | pi6.ipi6_addr.s6_addr16[1] = 0; | |
1305 | pi6.ipi6_ifindex = (m && m->m_pkthdr.rcvif) | |
1306 | ? m->m_pkthdr.rcvif->if_index | |
1307 | : 0; | |
1308 | *mp = sbcreatecontrol((caddr_t) &pi6, | |
1309 | sizeof(struct in6_pktinfo), IPV6_PKTINFO, | |
1310 | IPPROTO_IPV6); | |
1311 | if (*mp) | |
1312 | mp = &(*mp)->m_next; | |
1313 | } | |
1314 | ||
1315 | if ((in6p->in6p_flags & IN6P_HOPLIMIT) != 0) { | |
1316 | int hlim = ip6->ip6_hlim & 0xff; | |
1317 | *mp = sbcreatecontrol((caddr_t) &hlim, | |
1318 | sizeof(int), IPV6_HOPLIMIT, IPPROTO_IPV6); | |
1319 | if (*mp) | |
1320 | mp = &(*mp)->m_next; | |
1321 | } | |
1322 | ||
1323 | /* | |
1324 | * IPV6_HOPOPTS socket option. Recall that we required super-user | |
1325 | * privilege for the option (see ip6_ctloutput), but it might be too | |
1326 | * strict, since there might be some hop-by-hop options which can be | |
1327 | * returned to normal user. | |
1328 | * See RFC 2292 section 6. | |
1329 | */ | |
1330 | if ((in6p->in6p_flags & IN6P_HOPOPTS) != 0) { | |
1331 | /* | |
1332 | * Check if a hop-by-hop options header is contatined in the | |
1333 | * received packet, and if so, store the options as ancillary | |
1334 | * data. Note that a hop-by-hop options header must be | |
1335 | * just after the IPv6 header, which fact is assured through | |
1336 | * the IPv6 input processing. | |
1337 | */ | |
1338 | ip6 = mtod(m, struct ip6_hdr *); | |
1339 | if (ip6->ip6_nxt == IPPROTO_HOPOPTS) { | |
1340 | struct ip6_hbh *hbh; | |
1341 | int hbhlen = 0; | |
1342 | #if PULLDOWN_TEST | |
1343 | struct mbuf *ext; | |
1344 | #endif | |
1345 | ||
1346 | #ifndef PULLDOWN_TEST | |
1347 | hbh = (struct ip6_hbh *)(ip6 + 1); | |
1348 | hbhlen = (hbh->ip6h_len + 1) << 3; | |
1349 | #else | |
1350 | ext = ip6_pullexthdr(m, sizeof(struct ip6_hdr), | |
1351 | ip6->ip6_nxt); | |
1352 | if (ext == NULL) { | |
1353 | ip6stat.ip6s_tooshort++; | |
1354 | return; | |
1355 | } | |
1356 | hbh = mtod(ext, struct ip6_hbh *); | |
1357 | hbhlen = (hbh->ip6h_len + 1) << 3; | |
1358 | if (hbhlen != ext->m_len) { | |
1359 | m_freem(ext); | |
1360 | ip6stat.ip6s_tooshort++; | |
1361 | return; | |
1362 | } | |
1363 | #endif | |
1364 | ||
1365 | /* | |
1366 | * XXX: We copy whole the header even if a jumbo | |
1367 | * payload option is included, which option is to | |
1368 | * be removed before returning in the RFC 2292. | |
1369 | * Note: this constraint is removed in 2292bis. | |
1370 | */ | |
1371 | *mp = sbcreatecontrol((caddr_t)hbh, hbhlen, | |
1372 | IPV6_HOPOPTS, IPPROTO_IPV6); | |
1373 | if (*mp) | |
1374 | mp = &(*mp)->m_next; | |
1375 | #if PULLDOWN_TEST | |
1376 | m_freem(ext); | |
1377 | #endif | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | /* IPV6_DSTOPTS and IPV6_RTHDR socket options */ | |
1382 | if ((in6p->in6p_flags & (IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { | |
1383 | int proto, off, nxt; | |
1384 | ||
1385 | /* | |
1386 | * go through the header chain to see if a routing header is | |
1387 | * contained in the packet. We need this information to store | |
1388 | * destination options headers (if any) properly. | |
1389 | * XXX: performance issue. We should record this info when | |
1390 | * processing extension headers in incoming routine. | |
1391 | * (todo) use m_aux? | |
1392 | */ | |
1393 | proto = IPPROTO_IPV6; | |
1394 | off = 0; | |
1395 | nxt = -1; | |
1396 | while (1) { | |
1397 | int newoff; | |
1398 | ||
1399 | newoff = ip6_nexthdr(m, off, proto, &nxt); | |
1400 | if (newoff < 0) | |
1401 | break; | |
1402 | if (newoff < off) /* invalid, check for safety */ | |
1403 | break; | |
1404 | if ((proto = nxt) == IPPROTO_ROUTING) { | |
1405 | rthdr_exist = 1; | |
1406 | break; | |
1407 | } | |
1408 | off = newoff; | |
1409 | } | |
1410 | } | |
1411 | ||
1412 | if ((in6p->in6p_flags & | |
1413 | (IN6P_RTHDR | IN6P_DSTOPTS | IN6P_RTHDRDSTOPTS)) != 0) { | |
1414 | ip6 = mtod(m, struct ip6_hdr *); | |
1415 | int nxt = ip6->ip6_nxt, off = sizeof(struct ip6_hdr); | |
1416 | ||
1417 | /* | |
1418 | * Search for destination options headers or routing | |
1419 | * header(s) through the header chain, and stores each | |
1420 | * header as ancillary data. | |
1421 | * Note that the order of the headers remains in | |
1422 | * the chain of ancillary data. | |
1423 | */ | |
1424 | while (1) { /* is explicit loop prevention necessary? */ | |
1425 | struct ip6_ext *ip6e = NULL; | |
1426 | int elen; | |
1427 | #if PULLDOWN_TEST | |
1428 | struct mbuf *ext = NULL; | |
1429 | #endif | |
1430 | ||
1431 | /* | |
1432 | * if it is not an extension header, don't try to | |
1433 | * pull it from the chain. | |
1434 | */ | |
1435 | switch (nxt) { | |
1436 | case IPPROTO_DSTOPTS: | |
1437 | case IPPROTO_ROUTING: | |
1438 | case IPPROTO_HOPOPTS: | |
1439 | case IPPROTO_AH: /* is it possible? */ | |
1440 | break; | |
1441 | default: | |
1442 | goto loopend; | |
1443 | } | |
1444 | ||
1445 | #ifndef PULLDOWN_TEST | |
1446 | if (off + sizeof(*ip6e) > m->m_len) | |
1447 | goto loopend; | |
1448 | ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + off); | |
1449 | if (nxt == IPPROTO_AH) | |
1450 | elen = (ip6e->ip6e_len + 2) << 2; | |
1451 | else | |
1452 | elen = (ip6e->ip6e_len + 1) << 3; | |
1453 | if (off + elen > m->m_len) | |
1454 | goto loopend; | |
1455 | #else | |
1456 | ext = ip6_pullexthdr(m, off, nxt); | |
1457 | if (ext == NULL) { | |
1458 | ip6stat.ip6s_tooshort++; | |
1459 | return; | |
1460 | } | |
1461 | ip6e = mtod(ext, struct ip6_ext *); | |
1462 | if (nxt == IPPROTO_AH) | |
1463 | elen = (ip6e->ip6e_len + 2) << 2; | |
1464 | else | |
1465 | elen = (ip6e->ip6e_len + 1) << 3; | |
1466 | if (elen != ext->m_len) { | |
1467 | m_freem(ext); | |
1468 | ip6stat.ip6s_tooshort++; | |
1469 | return; | |
1470 | } | |
1471 | #endif | |
1472 | ||
1473 | switch (nxt) { | |
1474 | case IPPROTO_DSTOPTS: | |
1475 | if ((in6p->in6p_flags & IN6P_DSTOPTS) == 0) | |
1476 | break; | |
1477 | ||
1478 | *mp = sbcreatecontrol((caddr_t)ip6e, elen, | |
1479 | IPV6_DSTOPTS, | |
1480 | IPPROTO_IPV6); | |
1481 | if (*mp) | |
1482 | mp = &(*mp)->m_next; | |
1483 | break; | |
1484 | case IPPROTO_ROUTING: | |
1485 | if (!in6p->in6p_flags & IN6P_RTHDR) | |
1486 | break; | |
1487 | ||
1488 | *mp = sbcreatecontrol((caddr_t)ip6e, elen, | |
1489 | IPV6_RTHDR, | |
1490 | IPPROTO_IPV6); | |
1491 | if (*mp) | |
1492 | mp = &(*mp)->m_next; | |
1493 | break; | |
1494 | case IPPROTO_HOPOPTS: | |
1495 | case IPPROTO_AH: /* is it possible? */ | |
1496 | break; | |
1497 | ||
1498 | default: | |
1499 | /* | |
1500 | * other cases have been filtered in the above. | |
1501 | * none will visit this case. here we supply | |
1502 | * the code just in case (nxt overwritten or | |
1503 | * other cases). | |
1504 | */ | |
1505 | #if PULLDOWN_TEST | |
1506 | m_freem(ext); | |
1507 | #endif | |
1508 | goto loopend; | |
1509 | ||
1510 | } | |
1511 | ||
1512 | /* proceed with the next header. */ | |
1513 | off += elen; | |
1514 | nxt = ip6e->ip6e_nxt; | |
1515 | ip6e = NULL; | |
1516 | #if PULLDOWN_TEST | |
1517 | m_freem(ext); | |
1518 | ext = NULL; | |
1519 | #endif | |
1520 | } | |
1521 | loopend: | |
1522 | ; | |
1523 | } | |
1524 | ||
1525 | } | |
1526 | ||
1527 | #if PULLDOWN_TEST | |
1528 | /* | |
1529 | * pull single extension header from mbuf chain. returns single mbuf that | |
1530 | * contains the result, or NULL on error. | |
1531 | */ | |
1532 | static struct mbuf * | |
1533 | ip6_pullexthdr(m, off, nxt) | |
1534 | struct mbuf *m; | |
1535 | size_t off; | |
1536 | int nxt; | |
1537 | { | |
1538 | struct ip6_ext ip6e; | |
1539 | size_t elen; | |
1540 | struct mbuf *n; | |
1541 | ||
1542 | #if DIAGNOSTIC | |
1543 | switch (nxt) { | |
1544 | case IPPROTO_DSTOPTS: | |
1545 | case IPPROTO_ROUTING: | |
1546 | case IPPROTO_HOPOPTS: | |
1547 | case IPPROTO_AH: /* is it possible? */ | |
1548 | break; | |
1549 | default: | |
1550 | printf("ip6_pullexthdr: invalid nxt=%d\n", nxt); | |
1551 | } | |
1552 | #endif | |
1553 | ||
1554 | m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); | |
1555 | if (nxt == IPPROTO_AH) | |
1556 | elen = (ip6e.ip6e_len + 2) << 2; | |
1557 | else | |
1558 | elen = (ip6e.ip6e_len + 1) << 3; | |
1559 | ||
1560 | MGET(n, M_DONTWAIT, MT_DATA); | |
1561 | if (n && elen >= MLEN) { | |
1562 | MCLGET(n, M_DONTWAIT); | |
1563 | if ((n->m_flags & M_EXT) == 0) { | |
1564 | m_free(n); | |
1565 | n = NULL; | |
1566 | } | |
1567 | } | |
1568 | if (!n) | |
1569 | return NULL; | |
1570 | ||
1571 | n->m_len = 0; | |
1572 | if (elen >= M_TRAILINGSPACE(n)) { | |
1573 | m_free(n); | |
1574 | return NULL; | |
1575 | } | |
1576 | ||
1577 | m_copydata(m, off, elen, mtod(n, caddr_t)); | |
1578 | n->m_len = elen; | |
1579 | return n; | |
1580 | } | |
1581 | #endif | |
1582 | ||
1583 | /* | |
1584 | * Get pointer to the previous header followed by the header | |
1585 | * currently processed. | |
1586 | * XXX: This function supposes that | |
1587 | * M includes all headers, | |
1588 | * the next header field and the header length field of each header | |
1589 | * are valid, and | |
1590 | * the sum of each header length equals to OFF. | |
1591 | * Because of these assumptions, this function must be called very | |
1592 | * carefully. Moreover, it will not be used in the near future when | |
1593 | * we develop `neater' mechanism to process extension headers. | |
1594 | */ | |
1595 | char * | |
1596 | ip6_get_prevhdr(m, off) | |
1597 | struct mbuf *m; | |
1598 | int off; | |
1599 | { | |
1600 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
1601 | ||
1602 | if (off == sizeof(struct ip6_hdr)) | |
1603 | return(&ip6->ip6_nxt); | |
1604 | else { | |
1605 | int len, nxt; | |
1606 | struct ip6_ext *ip6e = NULL; | |
1607 | ||
1608 | nxt = ip6->ip6_nxt; | |
1609 | len = sizeof(struct ip6_hdr); | |
1610 | while (len < off) { | |
1611 | ip6e = (struct ip6_ext *)(mtod(m, caddr_t) + len); | |
1612 | ||
1613 | switch (nxt) { | |
1614 | case IPPROTO_FRAGMENT: | |
1615 | len += sizeof(struct ip6_frag); | |
1616 | break; | |
1617 | case IPPROTO_AH: | |
1618 | len += (ip6e->ip6e_len + 2) << 2; | |
1619 | break; | |
1620 | default: | |
1621 | len += (ip6e->ip6e_len + 1) << 3; | |
1622 | break; | |
1623 | } | |
1624 | nxt = ip6e->ip6e_nxt; | |
1625 | } | |
1626 | if (ip6e) | |
1627 | return(&ip6e->ip6e_nxt); | |
1628 | else | |
1629 | return NULL; | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | /* | |
1634 | * get next header offset. m will be retained. | |
1635 | */ | |
1636 | int | |
1637 | ip6_nexthdr(m, off, proto, nxtp) | |
1638 | struct mbuf *m; | |
1639 | int off; | |
1640 | int proto; | |
1641 | int *nxtp; | |
1642 | { | |
1643 | struct ip6_hdr ip6; | |
1644 | struct ip6_ext ip6e; | |
1645 | struct ip6_frag fh; | |
1646 | ||
1647 | /* just in case */ | |
1648 | if (m == NULL) | |
1649 | panic("ip6_nexthdr: m == NULL"); | |
1650 | if ((m->m_flags & M_PKTHDR) == 0 || m->m_pkthdr.len < off) | |
1651 | return -1; | |
1652 | ||
1653 | switch (proto) { | |
1654 | case IPPROTO_IPV6: | |
1655 | if (m->m_pkthdr.len < off + sizeof(ip6)) | |
1656 | return -1; | |
1657 | m_copydata(m, off, sizeof(ip6), (caddr_t)&ip6); | |
1658 | if (nxtp) | |
1659 | *nxtp = ip6.ip6_nxt; | |
1660 | off += sizeof(ip6); | |
1661 | return off; | |
1662 | ||
1663 | case IPPROTO_FRAGMENT: | |
1664 | /* | |
1665 | * terminate parsing if it is not the first fragment, | |
1666 | * it does not make sense to parse through it. | |
1667 | */ | |
1668 | if (m->m_pkthdr.len < off + sizeof(fh)) | |
1669 | return -1; | |
1670 | m_copydata(m, off, sizeof(fh), (caddr_t)&fh); | |
1671 | /* IP6F_OFF_MASK = 0xfff8(BigEndian), 0xf8ff(LittleEndian) */ | |
1672 | if (fh.ip6f_offlg & IP6F_OFF_MASK) | |
1673 | return -1; | |
1674 | if (nxtp) | |
1675 | *nxtp = fh.ip6f_nxt; | |
1676 | off += sizeof(struct ip6_frag); | |
1677 | return off; | |
1678 | ||
1679 | case IPPROTO_AH: | |
1680 | if (m->m_pkthdr.len < off + sizeof(ip6e)) | |
1681 | return -1; | |
1682 | m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); | |
1683 | if (nxtp) | |
1684 | *nxtp = ip6e.ip6e_nxt; | |
1685 | off += (ip6e.ip6e_len + 2) << 2; | |
1686 | return off; | |
1687 | ||
1688 | case IPPROTO_HOPOPTS: | |
1689 | case IPPROTO_ROUTING: | |
1690 | case IPPROTO_DSTOPTS: | |
1691 | if (m->m_pkthdr.len < off + sizeof(ip6e)) | |
1692 | return -1; | |
1693 | m_copydata(m, off, sizeof(ip6e), (caddr_t)&ip6e); | |
1694 | if (nxtp) | |
1695 | *nxtp = ip6e.ip6e_nxt; | |
1696 | off += (ip6e.ip6e_len + 1) << 3; | |
1697 | return off; | |
1698 | ||
1699 | case IPPROTO_NONE: | |
1700 | case IPPROTO_ESP: | |
1701 | case IPPROTO_IPCOMP: | |
1702 | /* give up */ | |
1703 | return -1; | |
1704 | ||
1705 | default: | |
1706 | return -1; | |
1707 | } | |
1708 | ||
1709 | return -1; | |
1710 | } | |
1711 | ||
1712 | /* | |
1713 | * get offset for the last header in the chain. m will be kept untainted. | |
1714 | */ | |
1715 | int | |
1716 | ip6_lasthdr(m, off, proto, nxtp) | |
1717 | struct mbuf *m; | |
1718 | int off; | |
1719 | int proto; | |
1720 | int *nxtp; | |
1721 | { | |
1722 | int newoff; | |
1723 | int nxt; | |
1724 | ||
1725 | if (!nxtp) { | |
1726 | nxt = -1; | |
1727 | nxtp = &nxt; | |
1728 | } | |
1729 | while (1) { | |
1730 | newoff = ip6_nexthdr(m, off, proto, nxtp); | |
1731 | if (newoff < 0) | |
1732 | return off; | |
1733 | else if (newoff < off) | |
1734 | return -1; /* invalid */ | |
1735 | else if (newoff == off) | |
1736 | return newoff; | |
1737 | ||
1738 | off = newoff; | |
1739 | proto = *nxtp; | |
1740 | } | |
1741 | } | |
1742 | ||
1743 | struct mbuf * | |
1744 | ip6_addaux(m) | |
1745 | struct mbuf *m; | |
1746 | { | |
1747 | struct mbuf *n; | |
1748 | ||
1749 | #if DIAGNOSTIC | |
1750 | if (sizeof(struct ip6aux) > MHLEN) | |
1751 | panic("assumption failed on sizeof(ip6aux)"); | |
1752 | #endif | |
1753 | n = m_aux_find(m, AF_INET6, -1); | |
1754 | if (n) { | |
1755 | if (n->m_len < sizeof(struct ip6aux)) { | |
1756 | printf("conflicting use of ip6aux"); | |
1757 | return NULL; | |
1758 | } | |
1759 | } else { | |
1760 | n = m_aux_add(m, AF_INET6, -1); | |
1761 | if (n) { | |
1762 | n->m_len = sizeof(struct ip6aux); | |
1763 | bzero(mtod(n, caddr_t), n->m_len); | |
1764 | } | |
1765 | } | |
1766 | return n; | |
1767 | } | |
1768 | ||
1769 | struct mbuf * | |
1770 | ip6_findaux(m) | |
1771 | struct mbuf *m; | |
1772 | { | |
1773 | struct mbuf *n; | |
1774 | ||
1775 | n = m_aux_find(m, AF_INET6, -1); | |
1776 | if (n && n->m_len < sizeof(struct ip6aux)) { | |
1777 | printf("conflicting use of ip6aux"); | |
1778 | n = NULL; | |
1779 | } | |
1780 | return n; | |
1781 | } | |
1782 | ||
1783 | void | |
1784 | ip6_delaux(m) | |
1785 | struct mbuf *m; | |
1786 | { | |
1787 | struct mbuf *n; | |
1788 | ||
1789 | n = m_aux_find(m, AF_INET6, -1); | |
1790 | if (n) | |
1791 | m_aux_delete(m, n); | |
1792 | } | |
1793 | ||
1794 | /* | |
1795 | * System control for IP6 | |
1796 | */ | |
1797 | ||
1798 | u_char inet6ctlerrmap[PRC_NCMDS] = { | |
1799 | 0, 0, 0, 0, | |
1800 | 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, | |
1801 | EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, | |
1802 | EMSGSIZE, EHOSTUNREACH, 0, 0, | |
1803 | 0, 0, 0, 0, | |
1804 | ENOPROTOOPT | |
1805 | }; |