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