]>
Commit | Line | Data |
---|---|---|
9bccf70c A |
1 | /* $FreeBSD: src/sys/netinet6/icmp6.c,v 1.6.2.6 2001/07/10 09:44:16 ume Exp $ */ |
2 | /* $KAME: icmp6.c,v 1.211 2001/04/04 05:56:20 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_icmp.c 8.2 (Berkeley) 1/4/94 | |
66 | */ | |
67 | ||
1c79356b A |
68 | |
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
91447636 | 71 | #include <sys/lock.h> |
1c79356b A |
72 | #include <sys/malloc.h> |
73 | #include <sys/mbuf.h> | |
74 | #include <sys/protosw.h> | |
75 | #include <sys/socket.h> | |
76 | #include <sys/socketvar.h> | |
77 | #include <sys/time.h> | |
78 | #include <sys/kernel.h> | |
79 | #include <sys/syslog.h> | |
80 | #include <sys/domain.h> | |
81 | ||
82 | #include <net/if.h> | |
83 | #include <net/route.h> | |
84 | #include <net/if_dl.h> | |
85 | #include <net/if_types.h> | |
86 | ||
87 | #include <netinet/in.h> | |
88 | #include <netinet/in_var.h> | |
1c79356b A |
89 | #include <netinet/ip6.h> |
90 | #include <netinet6/ip6_var.h> | |
91 | #include <netinet/icmp6.h> | |
92 | #include <netinet6/mld6_var.h> | |
1c79356b | 93 | #include <netinet/in_pcb.h> |
9bccf70c | 94 | #include <netinet6/in6_pcb.h> |
1c79356b A |
95 | #include <netinet6/nd6.h> |
96 | #include <netinet6/in6_ifattach.h> | |
97 | #include <netinet6/ip6protosw.h> | |
98 | ||
1c79356b A |
99 | #if IPSEC |
100 | #include <netinet6/ipsec.h> | |
101 | #include <netkey/key.h> | |
9bccf70c A |
102 | |
103 | extern int ipsec_bypass; | |
1c79356b A |
104 | #endif |
105 | ||
106 | #include "faith.h" | |
9bccf70c A |
107 | #if defined(NFAITH) && 0 < NFAITH |
108 | #include <net/if_faith.h> | |
109 | #endif | |
1c79356b A |
110 | |
111 | #include <net/net_osdep.h> | |
112 | ||
113 | extern struct domain inet6domain; | |
114 | extern struct ip6protosw inet6sw[]; | |
115 | extern struct ip6protosw *ip6_protox[]; | |
116 | ||
117 | struct icmp6stat icmp6stat; | |
118 | ||
1c79356b | 119 | extern struct inpcbhead ripcb; |
9bccf70c A |
120 | extern int icmp6errppslim; |
121 | static int icmp6errpps_count = 0; | |
122 | static struct timeval icmp6errppslim_last; | |
1c79356b | 123 | extern int icmp6_nodeinfo; |
91447636 A |
124 | extern struct inpcbinfo ripcbinfo; |
125 | extern lck_mtx_t *ip6_mutex; | |
126 | extern lck_mtx_t *nd6_mutex; | |
127 | ||
128 | static void icmp6_errcount(struct icmp6errstat *, int, int); | |
129 | static int icmp6_rip6_input(struct mbuf **, int); | |
130 | static int icmp6_ratelimit(const struct in6_addr *, const int, const int); | |
131 | static const char *icmp6_redirect_diag(struct in6_addr *, | |
132 | struct in6_addr *, struct in6_addr *); | |
9bccf70c | 133 | #ifndef HAVE_PPSRATECHECK |
91447636 | 134 | static int ppsratecheck(struct timeval *, int *, int); |
9bccf70c | 135 | #endif |
91447636 A |
136 | static struct mbuf *ni6_input(struct mbuf *, int); |
137 | static struct mbuf *ni6_nametodns(const char *, int, int); | |
138 | static int ni6_dnsmatch(const char *, int, const char *, int); | |
139 | static int ni6_addrs(struct icmp6_nodeinfo *, | |
140 | struct ifnet **, char *); | |
141 | static int ni6_store_addrs(struct icmp6_nodeinfo *, struct icmp6_nodeinfo *, | |
142 | struct ifnet *, int); | |
143 | static int icmp6_notify_error(struct mbuf *, int, int, int); | |
1c79356b A |
144 | |
145 | #ifdef COMPAT_RFC1885 | |
146 | static struct route_in6 icmp6_reflect_rt; | |
147 | #endif | |
1c79356b | 148 | |
1c79356b A |
149 | |
150 | void | |
151 | icmp6_init() | |
152 | { | |
153 | mld6_init(); | |
9bccf70c A |
154 | } |
155 | ||
156 | static void | |
157 | icmp6_errcount(stat, type, code) | |
158 | struct icmp6errstat *stat; | |
159 | int type, code; | |
160 | { | |
161 | switch (type) { | |
162 | case ICMP6_DST_UNREACH: | |
163 | switch (code) { | |
164 | case ICMP6_DST_UNREACH_NOROUTE: | |
165 | stat->icp6errs_dst_unreach_noroute++; | |
166 | return; | |
167 | case ICMP6_DST_UNREACH_ADMIN: | |
168 | stat->icp6errs_dst_unreach_admin++; | |
169 | return; | |
170 | case ICMP6_DST_UNREACH_BEYONDSCOPE: | |
171 | stat->icp6errs_dst_unreach_beyondscope++; | |
172 | return; | |
173 | case ICMP6_DST_UNREACH_ADDR: | |
174 | stat->icp6errs_dst_unreach_addr++; | |
175 | return; | |
176 | case ICMP6_DST_UNREACH_NOPORT: | |
177 | stat->icp6errs_dst_unreach_noport++; | |
178 | return; | |
179 | } | |
180 | break; | |
181 | case ICMP6_PACKET_TOO_BIG: | |
182 | stat->icp6errs_packet_too_big++; | |
183 | return; | |
184 | case ICMP6_TIME_EXCEEDED: | |
185 | switch (code) { | |
186 | case ICMP6_TIME_EXCEED_TRANSIT: | |
187 | stat->icp6errs_time_exceed_transit++; | |
188 | return; | |
189 | case ICMP6_TIME_EXCEED_REASSEMBLY: | |
190 | stat->icp6errs_time_exceed_reassembly++; | |
191 | return; | |
192 | } | |
193 | break; | |
194 | case ICMP6_PARAM_PROB: | |
195 | switch (code) { | |
196 | case ICMP6_PARAMPROB_HEADER: | |
197 | stat->icp6errs_paramprob_header++; | |
198 | return; | |
199 | case ICMP6_PARAMPROB_NEXTHEADER: | |
200 | stat->icp6errs_paramprob_nextheader++; | |
201 | return; | |
202 | case ICMP6_PARAMPROB_OPTION: | |
203 | stat->icp6errs_paramprob_option++; | |
204 | return; | |
205 | } | |
206 | break; | |
207 | case ND_REDIRECT: | |
208 | stat->icp6errs_redirect++; | |
209 | return; | |
210 | } | |
211 | stat->icp6errs_unknown++; | |
1c79356b A |
212 | } |
213 | ||
214 | /* | |
215 | * Generate an error packet of type error in response to bad IP6 packet. | |
216 | */ | |
217 | void | |
218 | icmp6_error(m, type, code, param) | |
219 | struct mbuf *m; | |
220 | int type, code, param; | |
221 | { | |
222 | struct ip6_hdr *oip6, *nip6; | |
223 | struct icmp6_hdr *icmp6; | |
224 | u_int preplen; | |
225 | int off; | |
226 | int nxt; | |
227 | ||
228 | icmp6stat.icp6s_error++; | |
229 | ||
91447636 | 230 | lck_mtx_assert(ip6_mutex, LCK_MTX_ASSERT_NOTOWNED); |
9bccf70c A |
231 | /* count per-type-code statistics */ |
232 | icmp6_errcount(&icmp6stat.icp6s_outerrhist, type, code); | |
233 | ||
1c79356b A |
234 | #ifdef M_DECRYPTED /*not openbsd*/ |
235 | if (m->m_flags & M_DECRYPTED) { | |
236 | icmp6stat.icp6s_canterror++; | |
237 | goto freeit; | |
238 | } | |
239 | #endif | |
240 | ||
241 | #ifndef PULLDOWN_TEST | |
91447636 | 242 | IP6_EXTHDR_CHECK(m, 0, sizeof(struct ip6_hdr), return); |
1c79356b A |
243 | #else |
244 | if (m->m_len < sizeof(struct ip6_hdr)) { | |
245 | m = m_pullup(m, sizeof(struct ip6_hdr)); | |
246 | if (m == NULL) | |
247 | return; | |
248 | } | |
249 | #endif | |
250 | oip6 = mtod(m, struct ip6_hdr *); | |
251 | ||
252 | /* | |
253 | * Multicast destination check. For unrecognized option errors, | |
254 | * this check has already done in ip6_unknown_opt(), so we can | |
255 | * check only for other errors. | |
256 | */ | |
257 | if ((m->m_flags & (M_BCAST|M_MCAST) || | |
258 | IN6_IS_ADDR_MULTICAST(&oip6->ip6_dst)) && | |
259 | (type != ICMP6_PACKET_TOO_BIG && | |
260 | (type != ICMP6_PARAM_PROB || | |
261 | code != ICMP6_PARAMPROB_OPTION))) | |
262 | goto freeit; | |
263 | ||
264 | /* Source address check. XXX: the case of anycast source? */ | |
265 | if (IN6_IS_ADDR_UNSPECIFIED(&oip6->ip6_src) || | |
266 | IN6_IS_ADDR_MULTICAST(&oip6->ip6_src)) | |
267 | goto freeit; | |
268 | ||
269 | /* | |
270 | * If we are about to send ICMPv6 against ICMPv6 error/redirect, | |
271 | * don't do it. | |
272 | */ | |
273 | nxt = -1; | |
274 | off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt); | |
275 | if (off >= 0 && nxt == IPPROTO_ICMPV6) { | |
276 | struct icmp6_hdr *icp; | |
277 | ||
278 | #ifndef PULLDOWN_TEST | |
91447636 | 279 | IP6_EXTHDR_CHECK(m, 0, off + sizeof(struct icmp6_hdr), return); |
1c79356b A |
280 | icp = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); |
281 | #else | |
282 | IP6_EXTHDR_GET(icp, struct icmp6_hdr *, m, off, | |
283 | sizeof(*icp)); | |
284 | if (icp == NULL) { | |
285 | icmp6stat.icp6s_tooshort++; | |
286 | return; | |
287 | } | |
288 | #endif | |
289 | if (icp->icmp6_type < ICMP6_ECHO_REQUEST || | |
290 | icp->icmp6_type == ND_REDIRECT) { | |
291 | /* | |
292 | * ICMPv6 error | |
293 | * Special case: for redirect (which is | |
294 | * informational) we must not send icmp6 error. | |
295 | */ | |
296 | icmp6stat.icp6s_canterror++; | |
297 | goto freeit; | |
298 | } else { | |
299 | /* ICMPv6 informational - send the error */ | |
300 | } | |
301 | } else { | |
302 | /* non-ICMPv6 - send the error */ | |
303 | } | |
304 | ||
305 | oip6 = mtod(m, struct ip6_hdr *); /* adjust pointer */ | |
306 | ||
307 | /* Finally, do rate limitation check. */ | |
308 | if (icmp6_ratelimit(&oip6->ip6_src, type, code)) { | |
309 | icmp6stat.icp6s_toofreq++; | |
310 | goto freeit; | |
311 | } | |
312 | ||
313 | /* | |
314 | * OK, ICMP6 can be generated. | |
315 | */ | |
316 | ||
317 | if (m->m_pkthdr.len >= ICMPV6_PLD_MAXLEN) | |
318 | m_adj(m, ICMPV6_PLD_MAXLEN - m->m_pkthdr.len); | |
319 | ||
320 | preplen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); | |
321 | M_PREPEND(m, preplen, M_DONTWAIT); | |
322 | if (m && m->m_len < preplen) | |
323 | m = m_pullup(m, preplen); | |
324 | if (m == NULL) { | |
9bccf70c | 325 | nd6log((LOG_DEBUG, "ENOBUFS in icmp6_error %d\n", __LINE__)); |
1c79356b A |
326 | return; |
327 | } | |
328 | ||
329 | nip6 = mtod(m, struct ip6_hdr *); | |
330 | nip6->ip6_src = oip6->ip6_src; | |
331 | nip6->ip6_dst = oip6->ip6_dst; | |
332 | ||
333 | if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_src)) | |
334 | oip6->ip6_src.s6_addr16[1] = 0; | |
335 | if (IN6_IS_SCOPE_LINKLOCAL(&oip6->ip6_dst)) | |
336 | oip6->ip6_dst.s6_addr16[1] = 0; | |
337 | ||
338 | icmp6 = (struct icmp6_hdr *)(nip6 + 1); | |
339 | icmp6->icmp6_type = type; | |
340 | icmp6->icmp6_code = code; | |
341 | icmp6->icmp6_pptr = htonl((u_int32_t)param); | |
342 | ||
9bccf70c A |
343 | /* |
344 | * icmp6_reflect() is designed to be in the input path. | |
345 | * icmp6_error() can be called from both input and outut path, | |
346 | * and if we are in output path rcvif could contain bogus value. | |
347 | * clear m->m_pkthdr.rcvif for safety, we should have enough scope | |
348 | * information in ip header (nip6). | |
349 | */ | |
350 | m->m_pkthdr.rcvif = NULL; | |
351 | ||
1c79356b | 352 | icmp6stat.icp6s_outhist[type]++; |
55e303ae | 353 | icmp6_reflect(m, sizeof(struct ip6_hdr)); /* header order: IPv6 - ICMPv6 */ |
1c79356b A |
354 | |
355 | return; | |
356 | ||
357 | freeit: | |
358 | /* | |
359 | * If we can't tell wheter or not we can generate ICMP6, free it. | |
360 | */ | |
361 | m_freem(m); | |
362 | } | |
363 | ||
364 | /* | |
365 | * Process a received ICMP6 message. | |
366 | */ | |
367 | int | |
55e303ae | 368 | icmp6_input(mp, offp) |
1c79356b | 369 | struct mbuf **mp; |
55e303ae | 370 | int *offp; |
1c79356b A |
371 | { |
372 | struct mbuf *m = *mp, *n; | |
373 | struct ip6_hdr *ip6, *nip6; | |
374 | struct icmp6_hdr *icmp6, *nicmp6; | |
375 | int off = *offp; | |
376 | int icmp6len = m->m_pkthdr.len - *offp; | |
377 | int code, sum, noff; | |
1c79356b A |
378 | |
379 | #ifndef PULLDOWN_TEST | |
91447636 | 380 | IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_hdr), return IPPROTO_DONE); |
55e303ae | 381 | /* m might change if M_LOOP. So, call mtod after this */ |
1c79356b A |
382 | #endif |
383 | ||
384 | /* | |
385 | * Locate icmp6 structure in mbuf, and check | |
386 | * that not corrupted and of at least minimum length | |
387 | */ | |
388 | ||
389 | ip6 = mtod(m, struct ip6_hdr *); | |
390 | if (icmp6len < sizeof(struct icmp6_hdr)) { | |
391 | icmp6stat.icp6s_tooshort++; | |
392 | goto freeit; | |
393 | } | |
394 | ||
395 | /* | |
396 | * calculate the checksum | |
397 | */ | |
398 | #ifndef PULLDOWN_TEST | |
399 | icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); | |
400 | #else | |
401 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); | |
402 | if (icmp6 == NULL) { | |
403 | icmp6stat.icp6s_tooshort++; | |
404 | return IPPROTO_DONE; | |
405 | } | |
406 | #endif | |
407 | code = icmp6->icmp6_code; | |
408 | ||
409 | if ((sum = in6_cksum(m, IPPROTO_ICMPV6, off, icmp6len)) != 0) { | |
9bccf70c | 410 | nd6log((LOG_ERR, |
1c79356b | 411 | "ICMP6 checksum error(%d|%x) %s\n", |
9bccf70c | 412 | icmp6->icmp6_type, sum, ip6_sprintf(&ip6->ip6_src))); |
1c79356b A |
413 | icmp6stat.icp6s_checksum++; |
414 | goto freeit; | |
415 | } | |
416 | ||
417 | #if defined(NFAITH) && 0 < NFAITH | |
9bccf70c | 418 | if (faithprefix(&ip6->ip6_dst)) { |
1c79356b A |
419 | /* |
420 | * Deliver very specific ICMP6 type only. | |
421 | * This is important to deilver TOOBIG. Otherwise PMTUD | |
422 | * will not work. | |
423 | */ | |
424 | switch (icmp6->icmp6_type) { | |
425 | case ICMP6_DST_UNREACH: | |
426 | case ICMP6_PACKET_TOO_BIG: | |
427 | case ICMP6_TIME_EXCEEDED: | |
428 | break; | |
429 | default: | |
430 | goto freeit; | |
431 | } | |
432 | } | |
433 | #endif | |
434 | ||
1c79356b A |
435 | icmp6stat.icp6s_inhist[icmp6->icmp6_type]++; |
436 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_msg); | |
437 | if (icmp6->icmp6_type < ICMP6_INFOMSG_MASK) | |
438 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_error); | |
439 | ||
1c79356b | 440 | switch (icmp6->icmp6_type) { |
1c79356b A |
441 | case ICMP6_DST_UNREACH: |
442 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_dstunreach); | |
443 | switch (code) { | |
444 | case ICMP6_DST_UNREACH_NOROUTE: | |
445 | code = PRC_UNREACH_NET; | |
446 | break; | |
447 | case ICMP6_DST_UNREACH_ADMIN: | |
448 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_adminprohib); | |
449 | code = PRC_UNREACH_PROTOCOL; /* is this a good code? */ | |
450 | break; | |
451 | case ICMP6_DST_UNREACH_ADDR: | |
452 | code = PRC_HOSTDEAD; | |
453 | break; | |
454 | #ifdef COMPAT_RFC1885 | |
455 | case ICMP6_DST_UNREACH_NOTNEIGHBOR: | |
456 | code = PRC_UNREACH_SRCFAIL; | |
457 | break; | |
458 | #else | |
459 | case ICMP6_DST_UNREACH_BEYONDSCOPE: | |
460 | /* I mean "source address was incorrect." */ | |
461 | code = PRC_PARAMPROB; | |
462 | break; | |
463 | #endif | |
464 | case ICMP6_DST_UNREACH_NOPORT: | |
465 | code = PRC_UNREACH_PORT; | |
466 | break; | |
467 | default: | |
468 | goto badcode; | |
469 | } | |
470 | goto deliver; | |
471 | break; | |
472 | ||
473 | case ICMP6_PACKET_TOO_BIG: | |
474 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_pkttoobig); | |
475 | if (code != 0) | |
476 | goto badcode; | |
477 | ||
478 | code = PRC_MSGSIZE; | |
479 | ||
480 | /* | |
481 | * Updating the path MTU will be done after examining | |
482 | * intermediate extension headers. | |
483 | */ | |
484 | goto deliver; | |
485 | break; | |
486 | ||
487 | case ICMP6_TIME_EXCEEDED: | |
488 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_timeexceed); | |
489 | switch (code) { | |
490 | case ICMP6_TIME_EXCEED_TRANSIT: | |
491 | case ICMP6_TIME_EXCEED_REASSEMBLY: | |
492 | code += PRC_TIMXCEED_INTRANS; | |
493 | break; | |
494 | default: | |
495 | goto badcode; | |
496 | } | |
497 | goto deliver; | |
498 | break; | |
499 | ||
500 | case ICMP6_PARAM_PROB: | |
501 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_paramprob); | |
502 | switch (code) { | |
503 | case ICMP6_PARAMPROB_NEXTHEADER: | |
504 | code = PRC_UNREACH_PROTOCOL; | |
505 | break; | |
506 | case ICMP6_PARAMPROB_HEADER: | |
507 | case ICMP6_PARAMPROB_OPTION: | |
508 | code = PRC_PARAMPROB; | |
509 | break; | |
510 | default: | |
511 | goto badcode; | |
512 | } | |
513 | goto deliver; | |
514 | break; | |
515 | ||
516 | case ICMP6_ECHO_REQUEST: | |
517 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echo); | |
518 | if (code != 0) | |
519 | goto badcode; | |
520 | if ((n = m_copy(m, 0, M_COPYALL)) == NULL) { | |
521 | /* Give up remote */ | |
522 | break; | |
523 | } | |
524 | if ((n->m_flags & M_EXT) != 0 | |
525 | || n->m_len < off + sizeof(struct icmp6_hdr)) { | |
526 | struct mbuf *n0 = n; | |
527 | const int maxlen = sizeof(*nip6) + sizeof(*nicmp6); | |
528 | ||
529 | /* | |
530 | * Prepare an internal mbuf. m_pullup() doesn't | |
531 | * always copy the length we specified. | |
532 | */ | |
533 | if (maxlen >= MCLBYTES) { | |
1c79356b A |
534 | /* Give up remote */ |
535 | m_freem(n0); | |
536 | break; | |
537 | } | |
538 | MGETHDR(n, M_DONTWAIT, n0->m_type); | |
539 | if (n && maxlen >= MHLEN) { | |
540 | MCLGET(n, M_DONTWAIT); | |
541 | if ((n->m_flags & M_EXT) == 0) { | |
542 | m_free(n); | |
543 | n = NULL; | |
544 | } | |
545 | } | |
546 | if (n == NULL) { | |
547 | /* Give up remote */ | |
548 | m_freem(n0); | |
549 | break; | |
550 | } | |
551 | M_COPY_PKTHDR(n, n0); | |
552 | /* | |
553 | * Copy IPv6 and ICMPv6 only. | |
554 | */ | |
555 | nip6 = mtod(n, struct ip6_hdr *); | |
556 | bcopy(ip6, nip6, sizeof(struct ip6_hdr)); | |
557 | nicmp6 = (struct icmp6_hdr *)(nip6 + 1); | |
558 | bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); | |
559 | noff = sizeof(struct ip6_hdr); | |
560 | n->m_pkthdr.len = n->m_len = | |
561 | noff + sizeof(struct icmp6_hdr); | |
562 | /* | |
563 | * Adjust mbuf. ip6_plen will be adjusted in | |
564 | * ip6_output(). | |
565 | */ | |
566 | m_adj(n0, off + sizeof(struct icmp6_hdr)); | |
567 | n->m_pkthdr.len += n0->m_pkthdr.len; | |
568 | n->m_next = n0; | |
569 | n0->m_flags &= ~M_PKTHDR; | |
570 | } else { | |
571 | nip6 = mtod(n, struct ip6_hdr *); | |
572 | nicmp6 = (struct icmp6_hdr *)((caddr_t)nip6 + off); | |
573 | noff = off; | |
574 | } | |
575 | nicmp6->icmp6_type = ICMP6_ECHO_REPLY; | |
576 | nicmp6->icmp6_code = 0; | |
577 | if (n) { | |
578 | icmp6stat.icp6s_reflect++; | |
579 | icmp6stat.icp6s_outhist[ICMP6_ECHO_REPLY]++; | |
580 | icmp6_reflect(n, noff); | |
581 | } | |
582 | break; | |
583 | ||
584 | case ICMP6_ECHO_REPLY: | |
585 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_echoreply); | |
586 | if (code != 0) | |
587 | goto badcode; | |
588 | break; | |
589 | ||
590 | case MLD6_LISTENER_QUERY: | |
591 | case MLD6_LISTENER_REPORT: | |
592 | if (icmp6len < sizeof(struct mld6_hdr)) | |
593 | goto badlen; | |
594 | if (icmp6->icmp6_type == MLD6_LISTENER_QUERY) /* XXX: ugly... */ | |
595 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldquery); | |
596 | else | |
597 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mldreport); | |
598 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { | |
599 | /* give up local */ | |
600 | mld6_input(m, off); | |
601 | m = NULL; | |
602 | goto freeit; | |
603 | } | |
604 | mld6_input(n, off); | |
605 | /* m stays. */ | |
606 | break; | |
607 | ||
608 | case MLD6_LISTENER_DONE: | |
609 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_mlddone); | |
610 | if (icmp6len < sizeof(struct mld6_hdr)) /* necessary? */ | |
611 | goto badlen; | |
612 | break; /* nothing to be done in kernel */ | |
613 | ||
614 | case MLD6_MTRACE_RESP: | |
615 | case MLD6_MTRACE: | |
616 | /* XXX: these two are experimental. not officially defind. */ | |
617 | /* XXX: per-interface statistics? */ | |
618 | break; /* just pass it to applications */ | |
619 | ||
620 | case ICMP6_WRUREQUEST: /* ICMP6_FQDN_QUERY */ | |
621 | { | |
622 | enum { WRU, FQDN } mode; | |
623 | ||
1c79356b A |
624 | if (!icmp6_nodeinfo) |
625 | break; | |
626 | ||
627 | if (icmp6len == sizeof(struct icmp6_hdr) + 4) | |
628 | mode = WRU; | |
9bccf70c | 629 | else if (icmp6len >= sizeof(struct icmp6_nodeinfo)) |
1c79356b A |
630 | mode = FQDN; |
631 | else | |
632 | goto badlen; | |
633 | ||
1c79356b | 634 | #define hostnamelen strlen(hostname) |
1c79356b A |
635 | if (mode == FQDN) { |
636 | #ifndef PULLDOWN_TEST | |
637 | IP6_EXTHDR_CHECK(m, off, sizeof(struct icmp6_nodeinfo), | |
91447636 | 638 | return IPPROTO_DONE); |
1c79356b A |
639 | #endif |
640 | n = m_copy(m, 0, M_COPYALL); | |
641 | if (n) | |
642 | n = ni6_input(n, off); | |
9bccf70c A |
643 | /* XXX meaningless if n == NULL */ |
644 | noff = sizeof(struct ip6_hdr); | |
1c79356b A |
645 | } else { |
646 | u_char *p; | |
647 | int maxlen, maxhlen; | |
648 | ||
9bccf70c A |
649 | if ((icmp6_nodeinfo & 5) != 5) |
650 | break; | |
651 | ||
652 | if (code != 0) | |
653 | goto badcode; | |
1c79356b A |
654 | maxlen = sizeof(*nip6) + sizeof(*nicmp6) + 4; |
655 | if (maxlen >= MCLBYTES) { | |
1c79356b A |
656 | /* Give up remote */ |
657 | break; | |
658 | } | |
659 | MGETHDR(n, M_DONTWAIT, m->m_type); | |
660 | if (n && maxlen > MHLEN) { | |
661 | MCLGET(n, M_DONTWAIT); | |
662 | if ((n->m_flags & M_EXT) == 0) { | |
663 | m_free(n); | |
664 | n = NULL; | |
665 | } | |
666 | } | |
667 | if (n == NULL) { | |
668 | /* Give up remote */ | |
669 | break; | |
670 | } | |
9bccf70c | 671 | n->m_pkthdr.rcvif = NULL; |
1c79356b A |
672 | n->m_len = 0; |
673 | maxhlen = M_TRAILINGSPACE(n) - maxlen; | |
674 | if (maxhlen > hostnamelen) | |
675 | maxhlen = hostnamelen; | |
676 | /* | |
677 | * Copy IPv6 and ICMPv6 only. | |
678 | */ | |
679 | nip6 = mtod(n, struct ip6_hdr *); | |
680 | bcopy(ip6, nip6, sizeof(struct ip6_hdr)); | |
681 | nicmp6 = (struct icmp6_hdr *)(nip6 + 1); | |
682 | bcopy(icmp6, nicmp6, sizeof(struct icmp6_hdr)); | |
683 | p = (u_char *)(nicmp6 + 1); | |
684 | bzero(p, 4); | |
55e303ae | 685 | bcopy(hostname, p + 4, maxhlen); /* meaningless TTL */ |
1c79356b | 686 | noff = sizeof(struct ip6_hdr); |
55e303ae | 687 | M_COPY_PKTHDR(n, m); /* just for rcvif */ |
1c79356b A |
688 | n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + |
689 | sizeof(struct icmp6_hdr) + 4 + maxhlen; | |
690 | nicmp6->icmp6_type = ICMP6_WRUREPLY; | |
691 | nicmp6->icmp6_code = 0; | |
692 | } | |
693 | #undef hostnamelen | |
694 | if (n) { | |
695 | icmp6stat.icp6s_reflect++; | |
696 | icmp6stat.icp6s_outhist[ICMP6_WRUREPLY]++; | |
697 | icmp6_reflect(n, noff); | |
698 | } | |
699 | break; | |
700 | } | |
701 | ||
702 | case ICMP6_WRUREPLY: | |
703 | if (code != 0) | |
704 | goto badcode; | |
705 | break; | |
706 | ||
707 | case ND_ROUTER_SOLICIT: | |
708 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routersolicit); | |
709 | if (code != 0) | |
710 | goto badcode; | |
711 | if (icmp6len < sizeof(struct nd_router_solicit)) | |
712 | goto badlen; | |
713 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { | |
714 | /* give up local */ | |
715 | nd6_rs_input(m, off, icmp6len); | |
716 | m = NULL; | |
717 | goto freeit; | |
718 | } | |
719 | nd6_rs_input(n, off, icmp6len); | |
720 | /* m stays. */ | |
721 | break; | |
722 | ||
723 | case ND_ROUTER_ADVERT: | |
724 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_routeradvert); | |
725 | if (code != 0) | |
726 | goto badcode; | |
727 | if (icmp6len < sizeof(struct nd_router_advert)) | |
728 | goto badlen; | |
729 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { | |
730 | /* give up local */ | |
731 | nd6_ra_input(m, off, icmp6len); | |
732 | m = NULL; | |
733 | goto freeit; | |
734 | } | |
735 | nd6_ra_input(n, off, icmp6len); | |
736 | /* m stays. */ | |
737 | break; | |
738 | ||
739 | case ND_NEIGHBOR_SOLICIT: | |
740 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighborsolicit); | |
741 | if (code != 0) | |
742 | goto badcode; | |
743 | if (icmp6len < sizeof(struct nd_neighbor_solicit)) | |
744 | goto badlen; | |
745 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { | |
746 | /* give up local */ | |
747 | nd6_ns_input(m, off, icmp6len); | |
748 | m = NULL; | |
749 | goto freeit; | |
750 | } | |
751 | nd6_ns_input(n, off, icmp6len); | |
752 | /* m stays. */ | |
753 | break; | |
754 | ||
755 | case ND_NEIGHBOR_ADVERT: | |
756 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_neighboradvert); | |
757 | if (code != 0) | |
758 | goto badcode; | |
759 | if (icmp6len < sizeof(struct nd_neighbor_advert)) | |
760 | goto badlen; | |
761 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { | |
762 | /* give up local */ | |
763 | nd6_na_input(m, off, icmp6len); | |
764 | m = NULL; | |
765 | goto freeit; | |
766 | } | |
767 | nd6_na_input(n, off, icmp6len); | |
768 | /* m stays. */ | |
769 | break; | |
770 | ||
771 | case ND_REDIRECT: | |
772 | icmp6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_redirect); | |
773 | if (code != 0) | |
774 | goto badcode; | |
775 | if (icmp6len < sizeof(struct nd_redirect)) | |
776 | goto badlen; | |
777 | if ((n = m_copym(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { | |
778 | /* give up local */ | |
779 | icmp6_redirect_input(m, off); | |
780 | m = NULL; | |
781 | goto freeit; | |
782 | } | |
783 | icmp6_redirect_input(n, off); | |
784 | /* m stays. */ | |
785 | break; | |
786 | ||
787 | case ICMP6_ROUTER_RENUMBERING: | |
788 | if (code != ICMP6_ROUTER_RENUMBERING_COMMAND && | |
789 | code != ICMP6_ROUTER_RENUMBERING_RESULT) | |
790 | goto badcode; | |
791 | if (icmp6len < sizeof(struct icmp6_router_renum)) | |
792 | goto badlen; | |
793 | break; | |
794 | ||
795 | default: | |
9bccf70c A |
796 | nd6log((LOG_DEBUG, |
797 | "icmp6_input: unknown type %d(src=%s, dst=%s, ifid=%d)\n", | |
798 | icmp6->icmp6_type, ip6_sprintf(&ip6->ip6_src), | |
799 | ip6_sprintf(&ip6->ip6_dst), | |
800 | m->m_pkthdr.rcvif ? m->m_pkthdr.rcvif->if_index : 0)); | |
1c79356b A |
801 | if (icmp6->icmp6_type < ICMP6_ECHO_REQUEST) { |
802 | /* ICMPv6 error: MUST deliver it by spec... */ | |
803 | code = PRC_NCMDS; | |
804 | /* deliver */ | |
805 | } else { | |
806 | /* ICMPv6 informational: MUST not deliver */ | |
807 | break; | |
808 | } | |
809 | deliver: | |
9bccf70c A |
810 | if (icmp6_notify_error(m, off, icmp6len, code)) { |
811 | /* In this case, m should've been freed. */ | |
812 | return(IPPROTO_DONE); | |
1c79356b | 813 | } |
9bccf70c A |
814 | break; |
815 | ||
816 | badcode: | |
817 | icmp6stat.icp6s_badcode++; | |
818 | break; | |
819 | ||
820 | badlen: | |
821 | icmp6stat.icp6s_badlen++; | |
822 | break; | |
823 | } | |
824 | ||
825 | /* deliver the packet to appropriate sockets */ | |
826 | icmp6_rip6_input(&m, *offp); | |
827 | ||
828 | return IPPROTO_DONE; | |
829 | ||
830 | freeit: | |
831 | m_freem(m); | |
832 | return IPPROTO_DONE; | |
833 | } | |
834 | ||
835 | static int | |
836 | icmp6_notify_error(m, off, icmp6len, code) | |
837 | struct mbuf *m; | |
55e303ae | 838 | int off, icmp6len, code; |
9bccf70c A |
839 | { |
840 | struct icmp6_hdr *icmp6; | |
841 | struct ip6_hdr *eip6; | |
842 | u_int32_t notifymtu; | |
843 | struct sockaddr_in6 icmp6src, icmp6dst; | |
844 | ||
845 | if (icmp6len < sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr)) { | |
846 | icmp6stat.icp6s_tooshort++; | |
847 | goto freeit; | |
848 | } | |
1c79356b | 849 | #ifndef PULLDOWN_TEST |
9bccf70c A |
850 | IP6_EXTHDR_CHECK(m, off, |
851 | sizeof(struct icmp6_hdr) + sizeof(struct ip6_hdr), | |
91447636 | 852 | return -1); |
9bccf70c | 853 | icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); |
1c79356b | 854 | #else |
9bccf70c A |
855 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, |
856 | sizeof(*icmp6) + sizeof(struct ip6_hdr)); | |
857 | if (icmp6 == NULL) { | |
858 | icmp6stat.icp6s_tooshort++; | |
859 | return(-1); | |
860 | } | |
1c79356b | 861 | #endif |
9bccf70c | 862 | eip6 = (struct ip6_hdr *)(icmp6 + 1); |
1c79356b | 863 | |
9bccf70c A |
864 | /* Detect the upper level protocol */ |
865 | { | |
91447636 | 866 | void (*ctlfunc)(int, struct sockaddr *, void *); |
1c79356b A |
867 | u_int8_t nxt = eip6->ip6_nxt; |
868 | int eoff = off + sizeof(struct icmp6_hdr) + | |
869 | sizeof(struct ip6_hdr); | |
870 | struct ip6ctlparam ip6cp; | |
871 | struct in6_addr *finaldst = NULL; | |
872 | int icmp6type = icmp6->icmp6_type; | |
873 | struct ip6_frag *fh; | |
874 | struct ip6_rthdr *rth; | |
875 | struct ip6_rthdr0 *rth0; | |
876 | int rthlen; | |
877 | ||
55e303ae | 878 | while (1) { /* XXX: should avoid infinite loop explicitly? */ |
1c79356b A |
879 | struct ip6_ext *eh; |
880 | ||
9bccf70c | 881 | switch (nxt) { |
1c79356b A |
882 | case IPPROTO_HOPOPTS: |
883 | case IPPROTO_DSTOPTS: | |
884 | case IPPROTO_AH: | |
885 | #ifndef PULLDOWN_TEST | |
886 | IP6_EXTHDR_CHECK(m, 0, eoff + | |
887 | sizeof(struct ip6_ext), | |
91447636 | 888 | return -1); |
1c79356b A |
889 | eh = (struct ip6_ext *)(mtod(m, caddr_t) |
890 | + eoff); | |
891 | #else | |
892 | IP6_EXTHDR_GET(eh, struct ip6_ext *, m, | |
9bccf70c | 893 | eoff, sizeof(*eh)); |
1c79356b A |
894 | if (eh == NULL) { |
895 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 896 | return(-1); |
1c79356b A |
897 | } |
898 | #endif | |
899 | ||
900 | if (nxt == IPPROTO_AH) | |
901 | eoff += (eh->ip6e_len + 2) << 2; | |
902 | else | |
903 | eoff += (eh->ip6e_len + 1) << 3; | |
904 | nxt = eh->ip6e_nxt; | |
905 | break; | |
906 | case IPPROTO_ROUTING: | |
907 | /* | |
908 | * When the erroneous packet contains a | |
909 | * routing header, we should examine the | |
910 | * header to determine the final destination. | |
911 | * Otherwise, we can't properly update | |
912 | * information that depends on the final | |
913 | * destination (e.g. path MTU). | |
914 | */ | |
915 | #ifndef PULLDOWN_TEST | |
916 | IP6_EXTHDR_CHECK(m, 0, eoff + sizeof(*rth), | |
91447636 | 917 | return -1); |
1c79356b A |
918 | rth = (struct ip6_rthdr *)(mtod(m, caddr_t) |
919 | + eoff); | |
920 | #else | |
921 | IP6_EXTHDR_GET(rth, struct ip6_rthdr *, m, | |
9bccf70c | 922 | eoff, sizeof(*rth)); |
1c79356b A |
923 | if (rth == NULL) { |
924 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 925 | return(-1); |
1c79356b A |
926 | } |
927 | #endif | |
928 | rthlen = (rth->ip6r_len + 1) << 3; | |
929 | /* | |
930 | * XXX: currently there is no | |
931 | * officially defined type other | |
932 | * than type-0. | |
933 | * Note that if the segment left field | |
934 | * is 0, all intermediate hops must | |
935 | * have been passed. | |
936 | */ | |
937 | if (rth->ip6r_segleft && | |
938 | rth->ip6r_type == IPV6_RTHDR_TYPE_0) { | |
939 | int hops; | |
940 | ||
941 | #ifndef PULLDOWN_TEST | |
942 | IP6_EXTHDR_CHECK(m, 0, eoff + rthlen, | |
91447636 | 943 | return -1); |
1c79356b A |
944 | rth0 = (struct ip6_rthdr0 *)(mtod(m, caddr_t) + eoff); |
945 | #else | |
946 | IP6_EXTHDR_GET(rth0, | |
947 | struct ip6_rthdr0 *, m, | |
948 | eoff, rthlen); | |
949 | if (rth0 == NULL) { | |
950 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 951 | return(-1); |
1c79356b A |
952 | } |
953 | #endif | |
954 | /* just ignore a bogus header */ | |
955 | if ((rth0->ip6r0_len % 2) == 0 && | |
956 | (hops = rth0->ip6r0_len/2)) | |
957 | finaldst = (struct in6_addr *)(rth0 + 1) + (hops - 1); | |
958 | } | |
959 | eoff += rthlen; | |
960 | nxt = rth->ip6r_nxt; | |
961 | break; | |
962 | case IPPROTO_FRAGMENT: | |
963 | #ifndef PULLDOWN_TEST | |
964 | IP6_EXTHDR_CHECK(m, 0, eoff + | |
965 | sizeof(struct ip6_frag), | |
91447636 | 966 | return -1); |
1c79356b A |
967 | fh = (struct ip6_frag *)(mtod(m, caddr_t) |
968 | + eoff); | |
969 | #else | |
970 | IP6_EXTHDR_GET(fh, struct ip6_frag *, m, | |
9bccf70c | 971 | eoff, sizeof(*fh)); |
1c79356b A |
972 | if (fh == NULL) { |
973 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 974 | return(-1); |
1c79356b A |
975 | } |
976 | #endif | |
977 | /* | |
978 | * Data after a fragment header is meaningless | |
979 | * unless it is the first fragment, but | |
980 | * we'll go to the notify label for path MTU | |
981 | * discovery. | |
982 | */ | |
983 | if (fh->ip6f_offlg & IP6F_OFF_MASK) | |
984 | goto notify; | |
985 | ||
986 | eoff += sizeof(struct ip6_frag); | |
987 | nxt = fh->ip6f_nxt; | |
988 | break; | |
989 | default: | |
990 | /* | |
991 | * This case includes ESP and the No Next | |
55e303ae | 992 | * Header. In such cases going to the notify |
1c79356b A |
993 | * label does not have any meaning |
994 | * (i.e. ctlfunc will be NULL), but we go | |
995 | * anyway since we might have to update | |
996 | * path MTU information. | |
997 | */ | |
998 | goto notify; | |
999 | } | |
1000 | } | |
9bccf70c | 1001 | notify: |
1c79356b A |
1002 | #ifndef PULLDOWN_TEST |
1003 | icmp6 = (struct icmp6_hdr *)(mtod(m, caddr_t) + off); | |
1004 | #else | |
1005 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, | |
9bccf70c | 1006 | sizeof(*icmp6) + sizeof(struct ip6_hdr)); |
1c79356b A |
1007 | if (icmp6 == NULL) { |
1008 | icmp6stat.icp6s_tooshort++; | |
9bccf70c | 1009 | return(-1); |
1c79356b A |
1010 | } |
1011 | #endif | |
9bccf70c A |
1012 | |
1013 | eip6 = (struct ip6_hdr *)(icmp6 + 1); | |
1014 | bzero(&icmp6dst, sizeof(icmp6dst)); | |
1015 | icmp6dst.sin6_len = sizeof(struct sockaddr_in6); | |
1016 | icmp6dst.sin6_family = AF_INET6; | |
1017 | if (finaldst == NULL) | |
1018 | icmp6dst.sin6_addr = eip6->ip6_dst; | |
1019 | else | |
1020 | icmp6dst.sin6_addr = *finaldst; | |
1021 | icmp6dst.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, | |
1022 | &icmp6dst.sin6_addr); | |
1023 | #ifndef SCOPEDROUTING | |
1024 | if (in6_embedscope(&icmp6dst.sin6_addr, &icmp6dst, | |
1025 | NULL, NULL)) { | |
1026 | /* should be impossbile */ | |
1027 | nd6log((LOG_DEBUG, | |
1028 | "icmp6_notify_error: in6_embedscope failed\n")); | |
1029 | goto freeit; | |
1030 | } | |
1031 | #endif | |
1032 | ||
1033 | /* | |
1034 | * retrieve parameters from the inner IPv6 header, and convert | |
1035 | * them into sockaddr structures. | |
1036 | */ | |
1037 | bzero(&icmp6src, sizeof(icmp6src)); | |
1038 | icmp6src.sin6_len = sizeof(struct sockaddr_in6); | |
1039 | icmp6src.sin6_family = AF_INET6; | |
1040 | icmp6src.sin6_addr = eip6->ip6_src; | |
1041 | icmp6src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, | |
1042 | &icmp6src.sin6_addr); | |
1043 | #ifndef SCOPEDROUTING | |
1044 | if (in6_embedscope(&icmp6src.sin6_addr, &icmp6src, | |
1045 | NULL, NULL)) { | |
1046 | /* should be impossbile */ | |
1047 | nd6log((LOG_DEBUG, | |
1048 | "icmp6_notify_error: in6_embedscope failed\n")); | |
1049 | goto freeit; | |
1050 | } | |
1051 | #endif | |
1052 | icmp6src.sin6_flowinfo = | |
1053 | (eip6->ip6_flow & IPV6_FLOWLABEL_MASK); | |
1054 | ||
1055 | if (finaldst == NULL) | |
1056 | finaldst = &eip6->ip6_dst; | |
1057 | ip6cp.ip6c_m = m; | |
1058 | ip6cp.ip6c_icmp6 = icmp6; | |
1059 | ip6cp.ip6c_ip6 = (struct ip6_hdr *)(icmp6 + 1); | |
1060 | ip6cp.ip6c_off = eoff; | |
1061 | ip6cp.ip6c_finaldst = finaldst; | |
1062 | ip6cp.ip6c_src = &icmp6src; | |
1063 | ip6cp.ip6c_nxt = nxt; | |
1064 | ||
1c79356b | 1065 | if (icmp6type == ICMP6_PACKET_TOO_BIG) { |
9bccf70c A |
1066 | notifymtu = ntohl(icmp6->icmp6_mtu); |
1067 | ip6cp.ip6c_cmdarg = (void *)¬ifymtu; | |
1068 | icmp6_mtudisc_update(&ip6cp, 1); /*XXX*/ | |
1c79356b A |
1069 | } |
1070 | ||
91447636 | 1071 | ctlfunc = (void (*)(int, struct sockaddr *, void *)) |
1c79356b A |
1072 | (ip6_protox[nxt]->pr_ctlinput); |
1073 | if (ctlfunc) { | |
9bccf70c A |
1074 | (void) (*ctlfunc)(code, (struct sockaddr *)&icmp6dst, |
1075 | &ip6cp); | |
1c79356b | 1076 | } |
1c79356b | 1077 | } |
9bccf70c | 1078 | return(0); |
1c79356b | 1079 | |
9bccf70c | 1080 | freeit: |
1c79356b | 1081 | m_freem(m); |
9bccf70c | 1082 | return(-1); |
1c79356b A |
1083 | } |
1084 | ||
9bccf70c A |
1085 | void |
1086 | icmp6_mtudisc_update(ip6cp, validated) | |
1087 | struct ip6ctlparam *ip6cp; | |
1088 | int validated; | |
1c79356b | 1089 | { |
9bccf70c A |
1090 | struct in6_addr *dst = ip6cp->ip6c_finaldst; |
1091 | struct icmp6_hdr *icmp6 = ip6cp->ip6c_icmp6; | |
1092 | struct mbuf *m = ip6cp->ip6c_m; /* will be necessary for scope issue */ | |
1c79356b A |
1093 | u_int mtu = ntohl(icmp6->icmp6_mtu); |
1094 | struct rtentry *rt = NULL; | |
1095 | struct sockaddr_in6 sin6; | |
9bccf70c A |
1096 | |
1097 | if (!validated) | |
1098 | return; | |
1c79356b A |
1099 | |
1100 | bzero(&sin6, sizeof(sin6)); | |
1101 | sin6.sin6_family = PF_INET6; | |
1102 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
1103 | sin6.sin6_addr = *dst; | |
9bccf70c A |
1104 | /* XXX normally, this won't happen */ |
1105 | if (IN6_IS_ADDR_LINKLOCAL(dst)) { | |
1106 | sin6.sin6_addr.s6_addr16[1] = | |
1107 | htons(m->m_pkthdr.rcvif->if_index); | |
1c79356b | 1108 | } |
9bccf70c | 1109 | /* sin6.sin6_scope_id = XXX: should be set if DST is a scoped addr */ |
1c79356b A |
1110 | rt = rtalloc1((struct sockaddr *)&sin6, 0, |
1111 | RTF_CLONING | RTF_PRCLONING); | |
1c79356b A |
1112 | |
1113 | if (rt && (rt->rt_flags & RTF_HOST) | |
1114 | && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { | |
1115 | if (mtu < IPV6_MMTU) { | |
1116 | /* xxx */ | |
1117 | rt->rt_rmx.rmx_locks |= RTV_MTU; | |
1118 | } else if (mtu < rt->rt_ifp->if_mtu && | |
1119 | rt->rt_rmx.rmx_mtu > mtu) { | |
9bccf70c | 1120 | icmp6stat.icp6s_pmtuchg++; |
1c79356b A |
1121 | rt->rt_rmx.rmx_mtu = mtu; |
1122 | } | |
1123 | } | |
1124 | if (rt) | |
9bccf70c | 1125 | rtfree(rt); |
1c79356b A |
1126 | } |
1127 | ||
1128 | /* | |
9bccf70c A |
1129 | * Process a Node Information Query packet, based on |
1130 | * draft-ietf-ipngwg-icmp-name-lookups-07. | |
1131 | * | |
1132 | * Spec incompatibilities: | |
1133 | * - IPv6 Subject address handling | |
1134 | * - IPv4 Subject address handling support missing | |
1135 | * - Proxy reply (answer even if it's not for me) | |
1136 | * - joins NI group address at in6_ifattach() time only, does not cope | |
1137 | * with hostname changes by sethostname(3) | |
1c79356b | 1138 | */ |
1c79356b | 1139 | #define hostnamelen strlen(hostname) |
1c79356b A |
1140 | static struct mbuf * |
1141 | ni6_input(m, off) | |
1142 | struct mbuf *m; | |
1143 | int off; | |
1144 | { | |
1145 | struct icmp6_nodeinfo *ni6, *nni6; | |
1146 | struct mbuf *n = NULL; | |
1147 | u_int16_t qtype; | |
9bccf70c | 1148 | int subjlen; |
1c79356b A |
1149 | int replylen = sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); |
1150 | struct ni_reply_fqdn *fqdn; | |
1151 | int addrs; /* for NI_QTYPE_NODEADDR */ | |
1152 | struct ifnet *ifp = NULL; /* for NI_QTYPE_NODEADDR */ | |
9bccf70c A |
1153 | struct sockaddr_in6 sin6; /* double meaning; ip6_dst and subjectaddr */ |
1154 | struct sockaddr_in6 sin6_d; /* XXX: we should retrieve this from m_aux */ | |
1155 | struct ip6_hdr *ip6; | |
1156 | int oldfqdn = 0; /* if 1, return pascal string (03 draft) */ | |
1157 | char *subj = NULL; | |
1158 | struct in6_ifaddr *ia6 = NULL; | |
1c79356b | 1159 | |
9bccf70c | 1160 | ip6 = mtod(m, struct ip6_hdr *); |
1c79356b A |
1161 | #ifndef PULLDOWN_TEST |
1162 | ni6 = (struct icmp6_nodeinfo *)(mtod(m, caddr_t) + off); | |
1163 | #else | |
1164 | IP6_EXTHDR_GET(ni6, struct icmp6_nodeinfo *, m, off, sizeof(*ni6)); | |
1165 | if (ni6 == NULL) { | |
1166 | /* m is already reclaimed */ | |
1167 | return NULL; | |
1168 | } | |
1169 | #endif | |
9bccf70c A |
1170 | |
1171 | /* | |
1172 | * Validate IPv6 destination address. | |
1173 | * | |
1174 | * The Responder must discard the Query without further processing | |
1175 | * unless it is one of the Responder's unicast or anycast addresses, or | |
1176 | * a link-local scope multicast address which the Responder has joined. | |
1177 | * [icmp-name-lookups-07, Section 4.] | |
1178 | */ | |
1179 | bzero(&sin6, sizeof(sin6)); | |
1180 | sin6.sin6_family = AF_INET6; | |
1181 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
1182 | bcopy(&ip6->ip6_dst, &sin6.sin6_addr, sizeof(sin6.sin6_addr)); | |
1183 | /* XXX scopeid */ | |
1184 | if ((ia6 = (struct in6_ifaddr *)ifa_ifwithaddr((struct sockaddr *)&sin6)) != NULL) { | |
1185 | /* unicast/anycast, fine */ | |
1186 | if ((ia6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && | |
1187 | (icmp6_nodeinfo & 4) == 0) { | |
91447636 A |
1188 | ifafree(&ia6->ia_ifa); |
1189 | ia6 = NULL; | |
9bccf70c A |
1190 | nd6log((LOG_DEBUG, "ni6_input: ignore node info to " |
1191 | "a temporary address in %s:%d", | |
1192 | __FILE__, __LINE__)); | |
1193 | goto bad; | |
1194 | } | |
91447636 A |
1195 | ifafree(&ia6->ia_ifa); |
1196 | ia6 = NULL; | |
9bccf70c A |
1197 | } else if (IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) |
1198 | ; /* link-local multicast, fine */ | |
1199 | else | |
1200 | goto bad; | |
1201 | ||
1202 | /* validate query Subject field. */ | |
1c79356b | 1203 | qtype = ntohs(ni6->ni_qtype); |
9bccf70c A |
1204 | subjlen = m->m_pkthdr.len - off - sizeof(struct icmp6_nodeinfo); |
1205 | switch (qtype) { | |
1206 | case NI_QTYPE_NOOP: | |
1207 | case NI_QTYPE_SUPTYPES: | |
1208 | /* 07 draft */ | |
1209 | if (ni6->ni_code == ICMP6_NI_SUBJ_FQDN && subjlen == 0) | |
1210 | break; | |
1211 | /* FALLTHROUGH */ | |
1212 | case NI_QTYPE_FQDN: | |
1213 | case NI_QTYPE_NODEADDR: | |
1214 | switch (ni6->ni_code) { | |
1215 | case ICMP6_NI_SUBJ_IPV6: | |
1216 | #if ICMP6_NI_SUBJ_IPV6 != 0 | |
1217 | case 0: | |
1218 | #endif | |
1219 | /* | |
1220 | * backward compatibility - try to accept 03 draft | |
1221 | * format, where no Subject is present. | |
1222 | */ | |
1223 | if (qtype == NI_QTYPE_FQDN && ni6->ni_code == 0 && | |
1224 | subjlen == 0) { | |
1225 | oldfqdn++; | |
1226 | break; | |
1227 | } | |
1228 | #if ICMP6_NI_SUBJ_IPV6 != 0 | |
1229 | if (ni6->ni_code != ICMP6_NI_SUBJ_IPV6) | |
1230 | goto bad; | |
1231 | #endif | |
1232 | ||
1233 | if (subjlen != sizeof(sin6.sin6_addr)) | |
1234 | goto bad; | |
1235 | ||
1236 | /* | |
1237 | * Validate Subject address. | |
1238 | * | |
1239 | * Not sure what exactly "address belongs to the node" | |
1240 | * means in the spec, is it just unicast, or what? | |
1241 | * | |
1242 | * At this moment we consider Subject address as | |
1243 | * "belong to the node" if the Subject address equals | |
1244 | * to the IPv6 destination address; validation for | |
1245 | * IPv6 destination address should have done enough | |
1246 | * check for us. | |
1247 | * | |
1248 | * We do not do proxy at this moment. | |
1249 | */ | |
1250 | /* m_pulldown instead of copy? */ | |
1251 | m_copydata(m, off + sizeof(struct icmp6_nodeinfo), | |
1252 | subjlen, (caddr_t)&sin6.sin6_addr); | |
1253 | sin6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, | |
1254 | &sin6.sin6_addr); | |
1255 | #ifndef SCOPEDROUTING | |
1256 | in6_embedscope(&sin6.sin6_addr, &sin6, NULL, NULL); | |
1257 | #endif | |
1258 | bzero(&sin6_d, sizeof(sin6_d)); | |
1259 | sin6_d.sin6_family = AF_INET6; /* not used, actually */ | |
1260 | sin6_d.sin6_len = sizeof(sin6_d); /* ditto */ | |
1261 | sin6_d.sin6_addr = ip6->ip6_dst; | |
1262 | sin6_d.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.rcvif, | |
1263 | &ip6->ip6_dst); | |
1264 | #ifndef SCOPEDROUTING | |
1265 | in6_embedscope(&sin6_d.sin6_addr, &sin6_d, NULL, NULL); | |
1266 | #endif | |
1267 | subj = (char *)&sin6; | |
1268 | if (SA6_ARE_ADDR_EQUAL(&sin6, &sin6_d)) | |
1269 | break; | |
1270 | ||
1271 | /* | |
1272 | * XXX if we are to allow other cases, we should really | |
1273 | * be careful about scope here. | |
1274 | * basically, we should disallow queries toward IPv6 | |
1275 | * destination X with subject Y, if scope(X) > scope(Y). | |
1276 | * if we allow scope(X) > scope(Y), it will result in | |
1277 | * information leakage across scope boundary. | |
1278 | */ | |
1279 | goto bad; | |
1c79356b | 1280 | |
9bccf70c A |
1281 | case ICMP6_NI_SUBJ_FQDN: |
1282 | /* | |
1283 | * Validate Subject name with gethostname(3). | |
1284 | * | |
1285 | * The behavior may need some debate, since: | |
1286 | * - we are not sure if the node has FQDN as | |
1287 | * hostname (returned by gethostname(3)). | |
1288 | * - the code does wildcard match for truncated names. | |
1289 | * however, we are not sure if we want to perform | |
1290 | * wildcard match, if gethostname(3) side has | |
1291 | * truncated hostname. | |
1292 | */ | |
1293 | n = ni6_nametodns(hostname, hostnamelen, 0); | |
1294 | if (!n || n->m_next || n->m_len == 0) | |
1295 | goto bad; | |
1296 | IP6_EXTHDR_GET(subj, char *, m, | |
1297 | off + sizeof(struct icmp6_nodeinfo), subjlen); | |
1298 | if (subj == NULL) | |
1299 | goto bad; | |
1300 | if (!ni6_dnsmatch(subj, subjlen, mtod(n, const char *), | |
1301 | n->m_len)) { | |
1302 | goto bad; | |
1303 | } | |
1304 | m_freem(n); | |
1305 | n = NULL; | |
1306 | break; | |
1307 | ||
1308 | case ICMP6_NI_SUBJ_IPV4: /* XXX: to be implemented? */ | |
1309 | default: | |
1310 | goto bad; | |
1311 | } | |
1312 | break; | |
1c79356b A |
1313 | } |
1314 | ||
9bccf70c A |
1315 | /* refuse based on configuration. XXX ICMP6_NI_REFUSED? */ |
1316 | switch (qtype) { | |
1317 | case NI_QTYPE_FQDN: | |
1318 | if ((icmp6_nodeinfo & 1) == 0) | |
1319 | goto bad; | |
1320 | break; | |
1321 | case NI_QTYPE_NODEADDR: | |
1322 | if ((icmp6_nodeinfo & 2) == 0) | |
1323 | goto bad; | |
1324 | break; | |
1325 | } | |
1326 | ||
1327 | /* guess reply length */ | |
1328 | switch (qtype) { | |
1329 | case NI_QTYPE_NOOP: | |
1330 | break; /* no reply data */ | |
1331 | case NI_QTYPE_SUPTYPES: | |
1332 | replylen += sizeof(u_int32_t); | |
1333 | break; | |
1334 | case NI_QTYPE_FQDN: | |
1335 | /* XXX will append an mbuf */ | |
1336 | replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); | |
1337 | break; | |
1338 | case NI_QTYPE_NODEADDR: | |
91447636 | 1339 | addrs = ni6_addrs(ni6, &ifp, subj); |
9bccf70c A |
1340 | if ((replylen += addrs * (sizeof(struct in6_addr) + |
1341 | sizeof(u_int32_t))) > MCLBYTES) | |
1342 | replylen = MCLBYTES; /* XXX: will truncate pkt later */ | |
1343 | break; | |
1344 | default: | |
1345 | /* | |
1346 | * XXX: We must return a reply with the ICMP6 code | |
1347 | * `unknown Qtype' in this case. However we regard the case | |
1348 | * as an FQDN query for backward compatibility. | |
1349 | * Older versions set a random value to this field, | |
1350 | * so it rarely varies in the defined qtypes. | |
1351 | * But the mechanism is not reliable... | |
1352 | * maybe we should obsolete older versions. | |
1353 | */ | |
1354 | qtype = NI_QTYPE_FQDN; | |
1355 | /* XXX will append an mbuf */ | |
1356 | replylen += offsetof(struct ni_reply_fqdn, ni_fqdn_namelen); | |
1357 | oldfqdn++; | |
1358 | break; | |
1359 | } | |
1360 | ||
1361 | /* allocate an mbuf to reply. */ | |
1c79356b A |
1362 | MGETHDR(n, M_DONTWAIT, m->m_type); |
1363 | if (n == NULL) { | |
1364 | m_freem(m); | |
1365 | return(NULL); | |
1366 | } | |
1367 | M_COPY_PKTHDR(n, m); /* just for recvif */ | |
1368 | if (replylen > MHLEN) { | |
9bccf70c A |
1369 | if (replylen > MCLBYTES) { |
1370 | /* | |
1371 | * XXX: should we try to allocate more? But MCLBYTES | |
1372 | * is probably much larger than IPV6_MMTU... | |
1373 | */ | |
1c79356b | 1374 | goto bad; |
9bccf70c | 1375 | } |
1c79356b A |
1376 | MCLGET(n, M_DONTWAIT); |
1377 | if ((n->m_flags & M_EXT) == 0) { | |
1378 | goto bad; | |
1379 | } | |
1380 | } | |
1381 | n->m_pkthdr.len = n->m_len = replylen; | |
1382 | ||
9bccf70c A |
1383 | /* copy mbuf header and IPv6 + Node Information base headers */ |
1384 | bcopy(mtod(m, caddr_t), mtod(n, caddr_t), sizeof(struct ip6_hdr)); | |
1385 | nni6 = (struct icmp6_nodeinfo *)(mtod(n, struct ip6_hdr *) + 1); | |
1386 | bcopy((caddr_t)ni6, (caddr_t)nni6, sizeof(struct icmp6_nodeinfo)); | |
1387 | ||
1388 | /* qtype dependent procedure */ | |
1389 | switch (qtype) { | |
1390 | case NI_QTYPE_NOOP: | |
1391 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1392 | nni6->ni_flags = 0; | |
1393 | break; | |
1394 | case NI_QTYPE_SUPTYPES: | |
1395 | { | |
1396 | u_int32_t v; | |
1397 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1398 | nni6->ni_flags = htons(0x0000); /* raw bitmap */ | |
1399 | /* supports NOOP, SUPTYPES, FQDN, and NODEADDR */ | |
1400 | v = (u_int32_t)htonl(0x0000000f); | |
1401 | bcopy(&v, nni6 + 1, sizeof(u_int32_t)); | |
1402 | break; | |
1403 | } | |
1404 | case NI_QTYPE_FQDN: | |
1405 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1406 | fqdn = (struct ni_reply_fqdn *)(mtod(n, caddr_t) + | |
1407 | sizeof(struct ip6_hdr) + | |
1408 | sizeof(struct icmp6_nodeinfo)); | |
1409 | nni6->ni_flags = 0; /* XXX: meaningless TTL */ | |
1410 | fqdn->ni_fqdn_ttl = 0; /* ditto. */ | |
1411 | /* | |
1412 | * XXX do we really have FQDN in variable "hostname"? | |
1413 | */ | |
1414 | n->m_next = ni6_nametodns(hostname, hostnamelen, oldfqdn); | |
1415 | if (n->m_next == NULL) | |
1416 | goto bad; | |
1417 | /* XXX we assume that n->m_next is not a chain */ | |
1418 | if (n->m_next->m_next != NULL) | |
1419 | goto bad; | |
1420 | n->m_pkthdr.len += n->m_next->m_len; | |
1421 | break; | |
1422 | case NI_QTYPE_NODEADDR: | |
1423 | { | |
1424 | int lenlim, copied; | |
1425 | ||
1426 | nni6->ni_code = ICMP6_NI_SUCCESS; | |
1427 | n->m_pkthdr.len = n->m_len = | |
1428 | sizeof(struct ip6_hdr) + sizeof(struct icmp6_nodeinfo); | |
1429 | lenlim = M_TRAILINGSPACE(n); | |
1430 | copied = ni6_store_addrs(ni6, nni6, ifp, lenlim); | |
1431 | /* XXX: reset mbuf length */ | |
1432 | n->m_pkthdr.len = n->m_len = sizeof(struct ip6_hdr) + | |
1433 | sizeof(struct icmp6_nodeinfo) + copied; | |
1434 | break; | |
1435 | } | |
1436 | default: | |
1437 | break; /* XXX impossible! */ | |
1438 | } | |
1439 | ||
1440 | nni6->ni_type = ICMP6_NI_REPLY; | |
1441 | m_freem(m); | |
1442 | return(n); | |
1443 | ||
1444 | bad: | |
1445 | m_freem(m); | |
1446 | if (n) | |
1447 | m_freem(n); | |
1448 | return(NULL); | |
1449 | } | |
1450 | #undef hostnamelen | |
1451 | ||
1452 | /* | |
1453 | * make a mbuf with DNS-encoded string. no compression support. | |
1454 | * | |
1455 | * XXX names with less than 2 dots (like "foo" or "foo.section") will be | |
1456 | * treated as truncated name (two \0 at the end). this is a wild guess. | |
1457 | */ | |
1458 | static struct mbuf * | |
1459 | ni6_nametodns(name, namelen, old) | |
1460 | const char *name; | |
1461 | int namelen; | |
1462 | int old; /* return pascal string if non-zero */ | |
1463 | { | |
1464 | struct mbuf *m; | |
1465 | char *cp, *ep; | |
1466 | const char *p, *q; | |
1467 | int i, len, nterm; | |
1468 | ||
1469 | if (old) | |
1470 | len = namelen + 1; | |
1471 | else | |
1472 | len = MCLBYTES; | |
1473 | ||
1474 | /* because MAXHOSTNAMELEN is usually 256, we use cluster mbuf */ | |
1475 | MGET(m, M_DONTWAIT, MT_DATA); | |
1476 | if (m && len > MLEN) { | |
1477 | MCLGET(m, M_DONTWAIT); | |
1478 | if ((m->m_flags & M_EXT) == 0) | |
1479 | goto fail; | |
1480 | } | |
1481 | if (!m) | |
1482 | goto fail; | |
1483 | m->m_next = NULL; | |
1484 | ||
1485 | if (old) { | |
1486 | m->m_len = len; | |
1487 | *mtod(m, char *) = namelen; | |
1488 | bcopy(name, mtod(m, char *) + 1, namelen); | |
1489 | return m; | |
1490 | } else { | |
1491 | m->m_len = 0; | |
1492 | cp = mtod(m, char *); | |
1493 | ep = mtod(m, char *) + M_TRAILINGSPACE(m); | |
1494 | ||
1495 | /* if not certain about my name, return empty buffer */ | |
1496 | if (namelen == 0) | |
1497 | return m; | |
1498 | ||
1499 | /* | |
1500 | * guess if it looks like shortened hostname, or FQDN. | |
1501 | * shortened hostname needs two trailing "\0". | |
1502 | */ | |
1503 | i = 0; | |
1504 | for (p = name; p < name + namelen; p++) { | |
1505 | if (*p && *p == '.') | |
1506 | i++; | |
1507 | } | |
1508 | if (i < 2) | |
1509 | nterm = 2; | |
1510 | else | |
1511 | nterm = 1; | |
1512 | ||
1513 | p = name; | |
1514 | while (cp < ep && p < name + namelen) { | |
1515 | i = 0; | |
1516 | for (q = p; q < name + namelen && *q && *q != '.'; q++) | |
1517 | i++; | |
1518 | /* result does not fit into mbuf */ | |
1519 | if (cp + i + 1 >= ep) | |
1520 | goto fail; | |
1521 | /* | |
1522 | * DNS label length restriction, RFC1035 page 8. | |
1523 | * "i == 0" case is included here to avoid returning | |
1524 | * 0-length label on "foo..bar". | |
1525 | */ | |
1526 | if (i <= 0 || i >= 64) | |
1527 | goto fail; | |
1528 | *cp++ = i; | |
1529 | bcopy(p, cp, i); | |
1530 | cp += i; | |
1531 | p = q; | |
1532 | if (p < name + namelen && *p == '.') | |
1533 | p++; | |
1534 | } | |
1535 | /* termination */ | |
1536 | if (cp + nterm >= ep) | |
1537 | goto fail; | |
1538 | while (nterm-- > 0) | |
1539 | *cp++ = '\0'; | |
1540 | m->m_len = cp - mtod(m, char *); | |
1541 | return m; | |
1542 | } | |
1543 | ||
1544 | panic("should not reach here"); | |
55e303ae | 1545 | /* NOTREACHED */ |
9bccf70c A |
1546 | |
1547 | fail: | |
1548 | if (m) | |
1549 | m_freem(m); | |
1550 | return NULL; | |
1551 | } | |
1552 | ||
1553 | /* | |
1554 | * check if two DNS-encoded string matches. takes care of truncated | |
1555 | * form (with \0\0 at the end). no compression support. | |
1556 | * XXX upper/lowercase match (see RFC2065) | |
1557 | */ | |
1558 | static int | |
1559 | ni6_dnsmatch(a, alen, b, blen) | |
1560 | const char *a; | |
1561 | int alen; | |
1562 | const char *b; | |
1563 | int blen; | |
1564 | { | |
1565 | const char *a0, *b0; | |
1566 | int l; | |
1567 | ||
1568 | /* simplest case - need validation? */ | |
1569 | if (alen == blen && bcmp(a, b, alen) == 0) | |
1570 | return 1; | |
1c79356b | 1571 | |
9bccf70c A |
1572 | a0 = a; |
1573 | b0 = b; | |
1c79356b | 1574 | |
9bccf70c A |
1575 | /* termination is mandatory */ |
1576 | if (alen < 2 || blen < 2) | |
1577 | return 0; | |
1578 | if (a0[alen - 1] != '\0' || b0[blen - 1] != '\0') | |
1579 | return 0; | |
1580 | alen--; | |
1581 | blen--; | |
1582 | ||
1583 | while (a - a0 < alen && b - b0 < blen) { | |
1584 | if (a - a0 + 1 > alen || b - b0 + 1 > blen) | |
1585 | return 0; | |
1586 | ||
1587 | if ((signed char)a[0] < 0 || (signed char)b[0] < 0) | |
1588 | return 0; | |
1589 | /* we don't support compression yet */ | |
1590 | if (a[0] >= 64 || b[0] >= 64) | |
1591 | return 0; | |
1592 | ||
1593 | /* truncated case */ | |
1594 | if (a[0] == 0 && a - a0 == alen - 1) | |
1595 | return 1; | |
1596 | if (b[0] == 0 && b - b0 == blen - 1) | |
1597 | return 1; | |
1598 | if (a[0] == 0 || b[0] == 0) | |
1599 | return 0; | |
1600 | ||
1601 | if (a[0] != b[0]) | |
1602 | return 0; | |
1603 | l = a[0]; | |
1604 | if (a - a0 + 1 + l > alen || b - b0 + 1 + l > blen) | |
1605 | return 0; | |
1606 | if (bcmp(a + 1, b + 1, l) != 0) | |
1607 | return 0; | |
1608 | ||
1609 | a += 1 + l; | |
1610 | b += 1 + l; | |
1611 | } | |
1c79356b | 1612 | |
9bccf70c A |
1613 | if (a - a0 == alen && b - b0 == blen) |
1614 | return 1; | |
1615 | else | |
1616 | return 0; | |
1c79356b | 1617 | } |
1c79356b A |
1618 | |
1619 | /* | |
1620 | * calculate the number of addresses to be returned in the node info reply. | |
1621 | */ | |
1622 | static int | |
91447636 | 1623 | ni6_addrs(ni6, ifpp, subj) |
1c79356b | 1624 | struct icmp6_nodeinfo *ni6; |
1c79356b | 1625 | struct ifnet **ifpp; |
9bccf70c | 1626 | char *subj; |
1c79356b | 1627 | { |
9bccf70c A |
1628 | struct ifnet *ifp; |
1629 | struct in6_ifaddr *ifa6; | |
1630 | struct ifaddr *ifa; | |
1631 | struct sockaddr_in6 *subj_ip6 = NULL; /* XXX pedant */ | |
1c79356b | 1632 | int addrs = 0, addrsofif, iffound = 0; |
9bccf70c A |
1633 | int niflags = ni6->ni_flags; |
1634 | ||
1635 | if ((niflags & NI_NODEADDR_FLAG_ALL) == 0) { | |
1636 | switch (ni6->ni_code) { | |
1637 | case ICMP6_NI_SUBJ_IPV6: | |
1638 | if (subj == NULL) /* must be impossible... */ | |
1639 | return(0); | |
1640 | subj_ip6 = (struct sockaddr_in6 *)subj; | |
1641 | break; | |
1642 | default: | |
1643 | /* | |
1644 | * XXX: we only support IPv6 subject address for | |
1645 | * this Qtype. | |
1646 | */ | |
1647 | return(0); | |
1648 | } | |
1649 | } | |
1c79356b | 1650 | |
91447636 A |
1651 | ifnet_head_lock_shared(); |
1652 | TAILQ_FOREACH(ifp, &ifnet_head, if_list) { | |
1c79356b | 1653 | addrsofif = 0; |
91447636 | 1654 | ifnet_lock_shared(ifp); |
9bccf70c | 1655 | TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) |
1c79356b A |
1656 | { |
1657 | if (ifa->ifa_addr->sa_family != AF_INET6) | |
1658 | continue; | |
1659 | ifa6 = (struct in6_ifaddr *)ifa; | |
1660 | ||
9bccf70c A |
1661 | if ((niflags & NI_NODEADDR_FLAG_ALL) == 0 && |
1662 | IN6_ARE_ADDR_EQUAL(&subj_ip6->sin6_addr, | |
1c79356b A |
1663 | &ifa6->ia_addr.sin6_addr)) |
1664 | iffound = 1; | |
1665 | ||
1666 | /* | |
1667 | * IPv4-mapped addresses can only be returned by a | |
1668 | * Node Information proxy, since they represent | |
1669 | * addresses of IPv4-only nodes, which perforce do | |
1670 | * not implement this protocol. | |
9bccf70c | 1671 | * [icmp-name-lookups-07, Section 5.4] |
1c79356b A |
1672 | * So we don't support NI_NODEADDR_FLAG_COMPAT in |
1673 | * this function at this moment. | |
1674 | */ | |
1675 | ||
1c79356b | 1676 | /* What do we have to do about ::1? */ |
9bccf70c A |
1677 | switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { |
1678 | case IPV6_ADDR_SCOPE_LINKLOCAL: | |
1679 | if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) | |
1680 | continue; | |
1681 | break; | |
1682 | case IPV6_ADDR_SCOPE_SITELOCAL: | |
1683 | if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) | |
1684 | continue; | |
1c79356b | 1685 | break; |
9bccf70c A |
1686 | case IPV6_ADDR_SCOPE_GLOBAL: |
1687 | if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) | |
1688 | continue; | |
1c79356b | 1689 | break; |
9bccf70c A |
1690 | default: |
1691 | continue; | |
1692 | } | |
1693 | ||
1694 | /* | |
1695 | * check if anycast is okay. | |
55e303ae | 1696 | * XXX: just experimental. not in the spec. |
9bccf70c A |
1697 | */ |
1698 | if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && | |
1699 | (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) | |
1700 | continue; /* we need only unicast addresses */ | |
1701 | if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && | |
1702 | (icmp6_nodeinfo & 4) == 0) { | |
1703 | continue; | |
1c79356b | 1704 | } |
9bccf70c | 1705 | addrsofif++; /* count the address */ |
1c79356b | 1706 | } |
91447636 | 1707 | ifnet_lock_done(ifp); |
1c79356b A |
1708 | if (iffound) { |
1709 | *ifpp = ifp; | |
91447636 | 1710 | ifnet_head_done(); |
1c79356b A |
1711 | return(addrsofif); |
1712 | } | |
1713 | ||
1714 | addrs += addrsofif; | |
1715 | } | |
91447636 | 1716 | ifnet_head_done(); |
1c79356b A |
1717 | |
1718 | return(addrs); | |
1719 | } | |
1720 | ||
1721 | static int | |
1722 | ni6_store_addrs(ni6, nni6, ifp0, resid) | |
1723 | struct icmp6_nodeinfo *ni6, *nni6; | |
1724 | struct ifnet *ifp0; | |
1725 | int resid; | |
1726 | { | |
91447636 | 1727 | struct ifnet *ifp = ifp0; |
9bccf70c A |
1728 | struct in6_ifaddr *ifa6; |
1729 | struct ifaddr *ifa; | |
1730 | struct ifnet *ifp_dep = NULL; | |
1731 | int copied = 0, allow_deprecated = 0; | |
1c79356b | 1732 | u_char *cp = (u_char *)(nni6 + 1); |
9bccf70c A |
1733 | int niflags = ni6->ni_flags; |
1734 | u_int32_t ltime; | |
91447636 A |
1735 | struct timeval timenow; |
1736 | ||
1737 | getmicrotime(&timenow); | |
1c79356b | 1738 | |
9bccf70c | 1739 | if (ifp0 == NULL && !(niflags & NI_NODEADDR_FLAG_ALL)) |
1c79356b A |
1740 | return(0); /* needless to copy */ |
1741 | ||
9bccf70c A |
1742 | again: |
1743 | ||
91447636 A |
1744 | ifnet_head_lock_shared(); |
1745 | if (ifp == NULL) ifp = TAILQ_FIRST(&ifnet_head); | |
1746 | ||
1747 | for (; ifp; ifp = TAILQ_NEXT(ifp, if_list)) { | |
1748 | ifnet_lock_shared(ifp); | |
1c79356b A |
1749 | for (ifa = ifp->if_addrlist.tqh_first; ifa; |
1750 | ifa = ifa->ifa_list.tqe_next) | |
1c79356b | 1751 | { |
1c79356b A |
1752 | if (ifa->ifa_addr->sa_family != AF_INET6) |
1753 | continue; | |
1754 | ifa6 = (struct in6_ifaddr *)ifa; | |
1755 | ||
9bccf70c A |
1756 | if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) != 0 && |
1757 | allow_deprecated == 0) { | |
1758 | /* | |
1759 | * prefererred address should be put before | |
1760 | * deprecated addresses. | |
1761 | */ | |
1762 | ||
1763 | /* record the interface for later search */ | |
1764 | if (ifp_dep == NULL) | |
1765 | ifp_dep = ifp; | |
1766 | ||
1767 | continue; | |
1c79356b | 1768 | } |
9bccf70c A |
1769 | else if ((ifa6->ia6_flags & IN6_IFF_DEPRECATED) == 0 && |
1770 | allow_deprecated != 0) | |
1771 | continue; /* we now collect deprecated addrs */ | |
1c79356b A |
1772 | |
1773 | /* What do we have to do about ::1? */ | |
9bccf70c A |
1774 | switch (in6_addrscope(&ifa6->ia_addr.sin6_addr)) { |
1775 | case IPV6_ADDR_SCOPE_LINKLOCAL: | |
1776 | if ((niflags & NI_NODEADDR_FLAG_LINKLOCAL) == 0) | |
1777 | continue; | |
1778 | break; | |
1779 | case IPV6_ADDR_SCOPE_SITELOCAL: | |
1780 | if ((niflags & NI_NODEADDR_FLAG_SITELOCAL) == 0) | |
1781 | continue; | |
1c79356b | 1782 | break; |
9bccf70c A |
1783 | case IPV6_ADDR_SCOPE_GLOBAL: |
1784 | if ((niflags & NI_NODEADDR_FLAG_GLOBAL) == 0) | |
1785 | continue; | |
1c79356b | 1786 | break; |
9bccf70c A |
1787 | default: |
1788 | continue; | |
1c79356b A |
1789 | } |
1790 | ||
9bccf70c A |
1791 | /* |
1792 | * check if anycast is okay. | |
1793 | * XXX: just experimental. not in the spec. | |
1794 | */ | |
1795 | if ((ifa6->ia6_flags & IN6_IFF_ANYCAST) != 0 && | |
1796 | (niflags & NI_NODEADDR_FLAG_ANYCAST) == 0) | |
1797 | continue; | |
1798 | if ((ifa6->ia6_flags & IN6_IFF_TEMPORARY) != 0 && | |
1799 | (icmp6_nodeinfo & 4) == 0) { | |
1800 | continue; | |
1801 | } | |
1802 | ||
1803 | /* now we can copy the address */ | |
1804 | if (resid < sizeof(struct in6_addr) + | |
1805 | sizeof(u_int32_t)) { | |
1806 | /* | |
1807 | * We give up much more copy. | |
1808 | * Set the truncate flag and return. | |
1809 | */ | |
1810 | nni6->ni_flags |= | |
1811 | NI_NODEADDR_FLAG_TRUNCATE; | |
91447636 A |
1812 | ifnet_lock_done(ifp); |
1813 | ifnet_head_done(); | |
9bccf70c A |
1814 | return(copied); |
1815 | } | |
1816 | ||
1817 | /* | |
1818 | * Set the TTL of the address. | |
1819 | * The TTL value should be one of the following | |
1820 | * according to the specification: | |
1821 | * | |
1822 | * 1. The remaining lifetime of a DHCP lease on the | |
1823 | * address, or | |
1824 | * 2. The remaining Valid Lifetime of a prefix from | |
1825 | * which the address was derived through Stateless | |
1826 | * Autoconfiguration. | |
1827 | * | |
1828 | * Note that we currently do not support stateful | |
1829 | * address configuration by DHCPv6, so the former | |
1830 | * case can't happen. | |
1831 | */ | |
1832 | if (ifa6->ia6_lifetime.ia6t_expire == 0) | |
1833 | ltime = ND6_INFINITE_LIFETIME; | |
1834 | else { | |
1835 | if (ifa6->ia6_lifetime.ia6t_expire > | |
91447636 A |
1836 | timenow.tv_sec) |
1837 | ltime = htonl(ifa6->ia6_lifetime.ia6t_expire - timenow.tv_sec); | |
9bccf70c A |
1838 | else |
1839 | ltime = 0; | |
1c79356b | 1840 | } |
9bccf70c A |
1841 | |
1842 | bcopy(<ime, cp, sizeof(u_int32_t)); | |
1843 | cp += sizeof(u_int32_t); | |
1844 | ||
1845 | /* copy the address itself */ | |
1846 | bcopy(&ifa6->ia_addr.sin6_addr, cp, | |
1847 | sizeof(struct in6_addr)); | |
1848 | /* XXX: KAME link-local hack; remove ifindex */ | |
1849 | if (IN6_IS_ADDR_LINKLOCAL(&ifa6->ia_addr.sin6_addr)) | |
1850 | ((struct in6_addr *)cp)->s6_addr16[1] = 0; | |
1851 | cp += sizeof(struct in6_addr); | |
1852 | ||
1853 | resid -= (sizeof(struct in6_addr) + sizeof(u_int32_t)); | |
1854 | copied += (sizeof(struct in6_addr) + | |
1855 | sizeof(u_int32_t)); | |
1c79356b | 1856 | } |
91447636 | 1857 | ifnet_lock_done(ifp); |
1c79356b A |
1858 | if (ifp0) /* we need search only on the specified IF */ |
1859 | break; | |
1860 | } | |
91447636 | 1861 | ifnet_head_done(); |
1c79356b | 1862 | |
9bccf70c A |
1863 | if (allow_deprecated == 0 && ifp_dep != NULL) { |
1864 | ifp = ifp_dep; | |
1865 | allow_deprecated = 1; | |
1866 | ||
1867 | goto again; | |
1868 | } | |
1869 | ||
1c79356b A |
1870 | return(copied); |
1871 | } | |
1872 | ||
1c79356b A |
1873 | /* |
1874 | * XXX almost dup'ed code with rip6_input. | |
1875 | */ | |
1876 | static int | |
1877 | icmp6_rip6_input(mp, off) | |
1878 | struct mbuf **mp; | |
1879 | int off; | |
1880 | { | |
1881 | struct mbuf *m = *mp; | |
9bccf70c A |
1882 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); |
1883 | struct in6pcb *in6p; | |
1c79356b A |
1884 | struct in6pcb *last = NULL; |
1885 | struct sockaddr_in6 rip6src; | |
1886 | struct icmp6_hdr *icmp6; | |
9bccf70c | 1887 | struct mbuf *opts = NULL; |
1c79356b A |
1888 | |
1889 | #ifndef PULLDOWN_TEST | |
1890 | /* this is assumed to be safe. */ | |
1891 | icmp6 = (struct icmp6_hdr *)((caddr_t)ip6 + off); | |
1892 | #else | |
1893 | IP6_EXTHDR_GET(icmp6, struct icmp6_hdr *, m, off, sizeof(*icmp6)); | |
1894 | if (icmp6 == NULL) { | |
1895 | /* m is already reclaimed */ | |
1896 | return IPPROTO_DONE; | |
1897 | } | |
1898 | #endif | |
1899 | ||
1c79356b A |
1900 | bzero(&rip6src, sizeof(rip6src)); |
1901 | rip6src.sin6_len = sizeof(struct sockaddr_in6); | |
1902 | rip6src.sin6_family = AF_INET6; | |
9bccf70c A |
1903 | /* KAME hack: recover scopeid */ |
1904 | (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif); | |
1c79356b | 1905 | |
91447636 | 1906 | lck_rw_lock_shared(ripcbinfo.mtx); |
1c79356b | 1907 | LIST_FOREACH(in6p, &ripcb, inp_list) |
1c79356b | 1908 | { |
9bccf70c A |
1909 | if ((in6p->inp_vflag & INP_IPV6) == 0) |
1910 | continue; | |
1911 | #if HAVE_NRL_INPCB | |
1912 | if (!(in6p->in6p_flags & INP_IPV6)) | |
1c79356b A |
1913 | continue; |
1914 | #endif | |
1915 | if (in6p->in6p_ip6_nxt != IPPROTO_ICMPV6) | |
1916 | continue; | |
1917 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) && | |
1918 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) | |
1919 | continue; | |
1920 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && | |
1921 | !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) | |
1922 | continue; | |
1923 | if (in6p->in6p_icmp6filt | |
1924 | && ICMP6_FILTER_WILLBLOCK(icmp6->icmp6_type, | |
1925 | in6p->in6p_icmp6filt)) | |
1926 | continue; | |
1927 | if (last) { | |
1928 | struct mbuf *n; | |
1929 | if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) { | |
1930 | if (last->in6p_flags & IN6P_CONTROLOPTS) | |
9bccf70c | 1931 | ip6_savecontrol(last, &opts, ip6, n); |
1c79356b A |
1932 | /* strip intermediate headers */ |
1933 | m_adj(n, off); | |
1934 | if (sbappendaddr(&last->in6p_socket->so_rcv, | |
1935 | (struct sockaddr *)&rip6src, | |
91447636 | 1936 | n, opts, NULL) != 0) { |
1c79356b | 1937 | sorwakeup(last->in6p_socket); |
91447636 | 1938 | } |
9bccf70c | 1939 | opts = NULL; |
1c79356b A |
1940 | } |
1941 | } | |
1942 | last = in6p; | |
1943 | } | |
91447636 | 1944 | lck_rw_done(ripcbinfo.mtx); |
1c79356b A |
1945 | if (last) { |
1946 | if (last->in6p_flags & IN6P_CONTROLOPTS) | |
9bccf70c | 1947 | ip6_savecontrol(last, &opts, ip6, m); |
1c79356b A |
1948 | /* strip intermediate headers */ |
1949 | m_adj(m, off); | |
1950 | if (sbappendaddr(&last->in6p_socket->so_rcv, | |
91447636 | 1951 | (struct sockaddr *)&rip6src, m, opts, NULL) != 0) { |
1c79356b | 1952 | sorwakeup(last->in6p_socket); |
91447636 | 1953 | } |
1c79356b A |
1954 | } else { |
1955 | m_freem(m); | |
1956 | ip6stat.ip6s_delivered--; | |
1957 | } | |
1958 | return IPPROTO_DONE; | |
1959 | } | |
1c79356b A |
1960 | |
1961 | /* | |
1962 | * Reflect the ip6 packet back to the source. | |
1963 | * OFF points to the icmp6 header, counted from the top of the mbuf. | |
1964 | */ | |
1965 | void | |
1966 | icmp6_reflect(m, off) | |
1967 | struct mbuf *m; | |
1968 | size_t off; | |
1969 | { | |
1970 | struct ip6_hdr *ip6; | |
1971 | struct icmp6_hdr *icmp6; | |
1972 | struct in6_ifaddr *ia; | |
91447636 | 1973 | struct in6_addr t, src_storage, *src = 0; |
1c79356b A |
1974 | int plen; |
1975 | int type, code; | |
1976 | struct ifnet *outif = NULL; | |
9bccf70c | 1977 | struct sockaddr_in6 sa6_src, sa6_dst; |
1c79356b A |
1978 | #ifdef COMPAT_RFC1885 |
1979 | int mtu = IPV6_MMTU; | |
1980 | struct sockaddr_in6 *sin6 = &icmp6_reflect_rt.ro_dst; | |
1981 | #endif | |
1982 | ||
1983 | /* too short to reflect */ | |
1984 | if (off < sizeof(struct ip6_hdr)) { | |
9bccf70c A |
1985 | nd6log((LOG_DEBUG, |
1986 | "sanity fail: off=%lx, sizeof(ip6)=%lx in %s:%d\n", | |
1987 | (u_long)off, (u_long)sizeof(struct ip6_hdr), | |
1988 | __FILE__, __LINE__)); | |
1c79356b A |
1989 | goto bad; |
1990 | } | |
1991 | ||
1992 | /* | |
1993 | * If there are extra headers between IPv6 and ICMPv6, strip | |
1994 | * off that header first. | |
1995 | */ | |
9bccf70c A |
1996 | #if DIAGNOSTIC |
1997 | if (sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) > MHLEN) | |
1998 | panic("assumption failed in icmp6_reflect"); | |
1999 | #endif | |
1c79356b A |
2000 | if (off > sizeof(struct ip6_hdr)) { |
2001 | size_t l; | |
2002 | struct ip6_hdr nip6; | |
2003 | ||
2004 | l = off - sizeof(struct ip6_hdr); | |
2005 | m_copydata(m, 0, sizeof(nip6), (caddr_t)&nip6); | |
2006 | m_adj(m, l); | |
2007 | l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); | |
2008 | if (m->m_len < l) { | |
2009 | if ((m = m_pullup(m, l)) == NULL) | |
2010 | return; | |
2011 | } | |
2012 | bcopy((caddr_t)&nip6, mtod(m, caddr_t), sizeof(nip6)); | |
2013 | } else /* off == sizeof(struct ip6_hdr) */ { | |
2014 | size_t l; | |
2015 | l = sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr); | |
2016 | if (m->m_len < l) { | |
2017 | if ((m = m_pullup(m, l)) == NULL) | |
2018 | return; | |
2019 | } | |
2020 | } | |
2021 | plen = m->m_pkthdr.len - sizeof(struct ip6_hdr); | |
2022 | ip6 = mtod(m, struct ip6_hdr *); | |
2023 | ip6->ip6_nxt = IPPROTO_ICMPV6; | |
2024 | icmp6 = (struct icmp6_hdr *)(ip6 + 1); | |
2025 | type = icmp6->icmp6_type; /* keep type for statistics */ | |
2026 | code = icmp6->icmp6_code; /* ditto. */ | |
2027 | ||
2028 | t = ip6->ip6_dst; | |
2029 | /* | |
2030 | * ip6_input() drops a packet if its src is multicast. | |
2031 | * So, the src is never multicast. | |
2032 | */ | |
2033 | ip6->ip6_dst = ip6->ip6_src; | |
2034 | ||
9bccf70c A |
2035 | /* |
2036 | * XXX: make sure to embed scope zone information, using | |
2037 | * already embedded IDs or the received interface (if any). | |
2038 | * Note that rcvif may be NULL. | |
2039 | * TODO: scoped routing case (XXX). | |
2040 | */ | |
2041 | bzero(&sa6_src, sizeof(sa6_src)); | |
2042 | sa6_src.sin6_family = AF_INET6; | |
2043 | sa6_src.sin6_len = sizeof(sa6_src); | |
2044 | sa6_src.sin6_addr = ip6->ip6_dst; | |
2045 | in6_recoverscope(&sa6_src, &ip6->ip6_dst, m->m_pkthdr.rcvif); | |
2046 | in6_embedscope(&ip6->ip6_dst, &sa6_src, NULL, NULL); | |
2047 | bzero(&sa6_dst, sizeof(sa6_dst)); | |
2048 | sa6_dst.sin6_family = AF_INET6; | |
2049 | sa6_dst.sin6_len = sizeof(sa6_dst); | |
2050 | sa6_dst.sin6_addr = t; | |
2051 | in6_recoverscope(&sa6_dst, &t, m->m_pkthdr.rcvif); | |
2052 | in6_embedscope(&t, &sa6_dst, NULL, NULL); | |
1c79356b A |
2053 | |
2054 | #ifdef COMPAT_RFC1885 | |
2055 | /* | |
2056 | * xxx guess MTU | |
2057 | * RFC 1885 requires that echo reply should be truncated if it | |
2058 | * does not fit in with (return) path MTU, but the description was | |
2059 | * removed in the new spec. | |
2060 | */ | |
2061 | if (icmp6_reflect_rt.ro_rt == 0 || | |
2062 | ! (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr, &ip6->ip6_dst))) { | |
2063 | if (icmp6_reflect_rt.ro_rt) { | |
1c79356b | 2064 | rtfree(icmp6_reflect_rt.ro_rt); |
1c79356b A |
2065 | icmp6_reflect_rt.ro_rt = 0; |
2066 | } | |
2067 | bzero(sin6, sizeof(*sin6)); | |
2068 | sin6->sin6_family = PF_INET6; | |
2069 | sin6->sin6_len = sizeof(struct sockaddr_in6); | |
2070 | sin6->sin6_addr = ip6->ip6_dst; | |
2071 | ||
1c79356b A |
2072 | rtalloc_ign((struct route *)&icmp6_reflect_rt.ro_rt, |
2073 | RTF_PRCLONING); | |
1c79356b A |
2074 | } |
2075 | ||
2076 | if (icmp6_reflect_rt.ro_rt == 0) | |
2077 | goto bad; | |
2078 | ||
2079 | if ((icmp6_reflect_rt.ro_rt->rt_flags & RTF_HOST) | |
2080 | && mtu < icmp6_reflect_rt.ro_rt->rt_ifp->if_mtu) | |
2081 | mtu = icmp6_reflect_rt.ro_rt->rt_rmx.rmx_mtu; | |
2082 | ||
2083 | if (mtu < m->m_pkthdr.len) { | |
2084 | plen -= (m->m_pkthdr.len - mtu); | |
2085 | m_adj(m, mtu - m->m_pkthdr.len); | |
2086 | } | |
2087 | #endif | |
2088 | /* | |
2089 | * If the incoming packet was addressed directly to us(i.e. unicast), | |
2090 | * use dst as the src for the reply. | |
9bccf70c | 2091 | * The IN6_IFF_NOTREADY case would be VERY rare, but is possible |
1c79356b A |
2092 | * (for example) when we encounter an error while forwarding procedure |
2093 | * destined to a duplicated address of ours. | |
2094 | */ | |
91447636 A |
2095 | lck_mtx_lock(nd6_mutex); |
2096 | for (ia = in6_ifaddrs; ia; ia = ia->ia_next) | |
1c79356b A |
2097 | if (IN6_ARE_ADDR_EQUAL(&t, &ia->ia_addr.sin6_addr) && |
2098 | (ia->ia6_flags & (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY)) == 0) { | |
2099 | src = &t; | |
2100 | break; | |
2101 | } | |
91447636 | 2102 | lck_mtx_unlock(nd6_mutex); |
1c79356b A |
2103 | if (ia == NULL && IN6_IS_ADDR_LINKLOCAL(&t) && (m->m_flags & M_LOOP)) { |
2104 | /* | |
2105 | * This is the case if the dst is our link-local address | |
55e303ae | 2106 | * and the sender is also ourselves. |
1c79356b A |
2107 | */ |
2108 | src = &t; | |
2109 | } | |
2110 | ||
9bccf70c A |
2111 | if (src == 0) { |
2112 | int e; | |
2113 | struct route_in6 ro; | |
2114 | ||
1c79356b A |
2115 | /* |
2116 | * This case matches to multicasts, our anycast, or unicasts | |
55e303ae | 2117 | * that we do not own. Select a source address based on the |
9bccf70c | 2118 | * source address of the erroneous packet. |
1c79356b | 2119 | */ |
9bccf70c | 2120 | bzero(&ro, sizeof(ro)); |
91447636 | 2121 | src = in6_selectsrc(&sa6_src, NULL, NULL, &ro, NULL, &src_storage, &e); |
9bccf70c A |
2122 | if (ro.ro_rt) |
2123 | rtfree(ro.ro_rt); /* XXX: we could use this */ | |
2124 | if (src == NULL) { | |
2125 | nd6log((LOG_DEBUG, | |
2126 | "icmp6_reflect: source can't be determined: " | |
2127 | "dst=%s, error=%d\n", | |
2128 | ip6_sprintf(&sa6_src.sin6_addr), e)); | |
2129 | goto bad; | |
2130 | } | |
2131 | } | |
1c79356b A |
2132 | |
2133 | ip6->ip6_src = *src; | |
2134 | ||
2135 | ip6->ip6_flow = 0; | |
2136 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
2137 | ip6->ip6_vfc |= IPV6_VERSION; | |
2138 | ip6->ip6_nxt = IPPROTO_ICMPV6; | |
2139 | if (m->m_pkthdr.rcvif) { | |
2140 | /* XXX: This may not be the outgoing interface */ | |
2141 | ip6->ip6_hlim = nd_ifinfo[m->m_pkthdr.rcvif->if_index].chlim; | |
9bccf70c A |
2142 | } else |
2143 | ip6->ip6_hlim = ip6_defhlim; | |
1c79356b A |
2144 | |
2145 | icmp6->icmp6_cksum = 0; | |
2146 | icmp6->icmp6_cksum = in6_cksum(m, IPPROTO_ICMPV6, | |
2147 | sizeof(struct ip6_hdr), plen); | |
2148 | ||
2149 | /* | |
9bccf70c | 2150 | * XXX option handling |
1c79356b A |
2151 | */ |
2152 | ||
2153 | m->m_flags &= ~(M_BCAST|M_MCAST); | |
2154 | #if IPSEC | |
2155 | /* Don't lookup socket */ | |
9bccf70c A |
2156 | if (ipsec_bypass == 0) |
2157 | (void)ipsec_setsocket(m, NULL); | |
1c79356b A |
2158 | #endif /*IPSEC*/ |
2159 | ||
2160 | #ifdef COMPAT_RFC1885 | |
91447636 | 2161 | ip6_output(m, NULL, &icmp6_reflect_rt, 0, NULL, &outif, 0); |
1c79356b | 2162 | #else |
91447636 | 2163 | ip6_output(m, NULL, NULL, 0, NULL, &outif, 0); |
1c79356b A |
2164 | #endif |
2165 | if (outif) | |
2166 | icmp6_ifoutstat_inc(outif, type, code); | |
2167 | ||
2168 | return; | |
2169 | ||
2170 | bad: | |
2171 | m_freem(m); | |
2172 | return; | |
2173 | } | |
2174 | ||
2175 | void | |
2176 | icmp6_fasttimo() | |
2177 | { | |
9bccf70c | 2178 | |
1c79356b A |
2179 | mld6_fasttimeo(); |
2180 | } | |
2181 | ||
2182 | static const char * | |
2183 | icmp6_redirect_diag(src6, dst6, tgt6) | |
2184 | struct in6_addr *src6; | |
2185 | struct in6_addr *dst6; | |
2186 | struct in6_addr *tgt6; | |
2187 | { | |
2188 | static char buf[1024]; | |
1c79356b A |
2189 | snprintf(buf, sizeof(buf), "(src=%s dst=%s tgt=%s)", |
2190 | ip6_sprintf(src6), ip6_sprintf(dst6), ip6_sprintf(tgt6)); | |
1c79356b A |
2191 | return buf; |
2192 | } | |
2193 | ||
2194 | void | |
2195 | icmp6_redirect_input(m, off) | |
9bccf70c | 2196 | struct mbuf *m; |
1c79356b A |
2197 | int off; |
2198 | { | |
2199 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
2200 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
2201 | struct nd_redirect *nd_rd; | |
2202 | int icmp6len = ntohs(ip6->ip6_plen); | |
2203 | char *lladdr = NULL; | |
2204 | int lladdrlen = 0; | |
2205 | u_char *redirhdr = NULL; | |
2206 | int redirhdrlen = 0; | |
2207 | struct rtentry *rt = NULL; | |
2208 | int is_router; | |
2209 | int is_onlink; | |
2210 | struct in6_addr src6 = ip6->ip6_src; | |
2211 | struct in6_addr redtgt6; | |
2212 | struct in6_addr reddst6; | |
2213 | union nd_opts ndopts; | |
2214 | ||
2215 | if (!m || !ifp) | |
2216 | return; | |
2217 | ||
2218 | /* XXX if we are router, we don't update route by icmp6 redirect */ | |
2219 | if (ip6_forwarding) | |
2220 | goto freeit; | |
2221 | if (!icmp6_rediraccept) | |
2222 | goto freeit; | |
2223 | ||
2224 | #ifndef PULLDOWN_TEST | |
91447636 | 2225 | IP6_EXTHDR_CHECK(m, off, icmp6len, return); |
1c79356b A |
2226 | nd_rd = (struct nd_redirect *)((caddr_t)ip6 + off); |
2227 | #else | |
2228 | IP6_EXTHDR_GET(nd_rd, struct nd_redirect *, m, off, icmp6len); | |
2229 | if (nd_rd == NULL) { | |
2230 | icmp6stat.icp6s_tooshort++; | |
2231 | return; | |
2232 | } | |
2233 | #endif | |
2234 | redtgt6 = nd_rd->nd_rd_target; | |
2235 | reddst6 = nd_rd->nd_rd_dst; | |
2236 | ||
2237 | if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) | |
2238 | redtgt6.s6_addr16[1] = htons(ifp->if_index); | |
2239 | if (IN6_IS_ADDR_LINKLOCAL(&reddst6)) | |
2240 | reddst6.s6_addr16[1] = htons(ifp->if_index); | |
2241 | ||
2242 | /* validation */ | |
2243 | if (!IN6_IS_ADDR_LINKLOCAL(&src6)) { | |
9bccf70c | 2244 | nd6log((LOG_ERR, |
1c79356b | 2245 | "ICMP6 redirect sent from %s rejected; " |
9bccf70c A |
2246 | "must be from linklocal\n", ip6_sprintf(&src6))); |
2247 | goto bad; | |
1c79356b A |
2248 | } |
2249 | if (ip6->ip6_hlim != 255) { | |
9bccf70c | 2250 | nd6log((LOG_ERR, |
1c79356b A |
2251 | "ICMP6 redirect sent from %s rejected; " |
2252 | "hlim=%d (must be 255)\n", | |
9bccf70c A |
2253 | ip6_sprintf(&src6), ip6->ip6_hlim)); |
2254 | goto bad; | |
1c79356b A |
2255 | } |
2256 | { | |
2257 | /* ip6->ip6_src must be equal to gw for icmp6->icmp6_reddst */ | |
2258 | struct sockaddr_in6 sin6; | |
2259 | struct in6_addr *gw6; | |
2260 | ||
2261 | bzero(&sin6, sizeof(sin6)); | |
2262 | sin6.sin6_family = AF_INET6; | |
2263 | sin6.sin6_len = sizeof(struct sockaddr_in6); | |
2264 | bcopy(&reddst6, &sin6.sin6_addr, sizeof(reddst6)); | |
9bccf70c | 2265 | rt = rtalloc1((struct sockaddr *)&sin6, 0, 0UL); |
1c79356b | 2266 | if (rt) { |
9bccf70c A |
2267 | if (rt->rt_gateway == NULL || |
2268 | rt->rt_gateway->sa_family != AF_INET6) { | |
2269 | nd6log((LOG_ERR, | |
2270 | "ICMP6 redirect rejected; no route " | |
2271 | "with inet6 gateway found for redirect dst: %s\n", | |
2272 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); | |
2273 | rtfree(rt); | |
2274 | goto bad; | |
2275 | } | |
2276 | ||
1c79356b A |
2277 | gw6 = &(((struct sockaddr_in6 *)rt->rt_gateway)->sin6_addr); |
2278 | if (bcmp(&src6, gw6, sizeof(struct in6_addr)) != 0) { | |
9bccf70c | 2279 | nd6log((LOG_ERR, |
1c79356b A |
2280 | "ICMP6 redirect rejected; " |
2281 | "not equal to gw-for-src=%s (must be same): " | |
2282 | "%s\n", | |
2283 | ip6_sprintf(gw6), | |
9bccf70c A |
2284 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2285 | rtfree(rt); | |
2286 | goto bad; | |
1c79356b A |
2287 | } |
2288 | } else { | |
9bccf70c | 2289 | nd6log((LOG_ERR, |
1c79356b A |
2290 | "ICMP6 redirect rejected; " |
2291 | "no route found for redirect dst: %s\n", | |
9bccf70c A |
2292 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2293 | goto bad; | |
1c79356b | 2294 | } |
9bccf70c | 2295 | rtfree(rt); |
1c79356b A |
2296 | rt = NULL; |
2297 | } | |
2298 | if (IN6_IS_ADDR_MULTICAST(&reddst6)) { | |
9bccf70c | 2299 | nd6log((LOG_ERR, |
1c79356b A |
2300 | "ICMP6 redirect rejected; " |
2301 | "redirect dst must be unicast: %s\n", | |
9bccf70c A |
2302 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2303 | goto bad; | |
1c79356b A |
2304 | } |
2305 | ||
2306 | is_router = is_onlink = 0; | |
2307 | if (IN6_IS_ADDR_LINKLOCAL(&redtgt6)) | |
2308 | is_router = 1; /* router case */ | |
2309 | if (bcmp(&redtgt6, &reddst6, sizeof(redtgt6)) == 0) | |
2310 | is_onlink = 1; /* on-link destination case */ | |
2311 | if (!is_router && !is_onlink) { | |
9bccf70c | 2312 | nd6log((LOG_ERR, |
1c79356b A |
2313 | "ICMP6 redirect rejected; " |
2314 | "neither router case nor onlink case: %s\n", | |
9bccf70c A |
2315 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2316 | goto bad; | |
1c79356b A |
2317 | } |
2318 | /* validation passed */ | |
2319 | ||
2320 | icmp6len -= sizeof(*nd_rd); | |
2321 | nd6_option_init(nd_rd + 1, icmp6len, &ndopts); | |
2322 | if (nd6_options(&ndopts) < 0) { | |
9bccf70c | 2323 | nd6log((LOG_INFO, "icmp6_redirect_input: " |
1c79356b | 2324 | "invalid ND option, rejected: %s\n", |
9bccf70c A |
2325 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2326 | /* nd6_options have incremented stats */ | |
1c79356b A |
2327 | goto freeit; |
2328 | } | |
2329 | ||
2330 | if (ndopts.nd_opts_tgt_lladdr) { | |
2331 | lladdr = (char *)(ndopts.nd_opts_tgt_lladdr + 1); | |
2332 | lladdrlen = ndopts.nd_opts_tgt_lladdr->nd_opt_len << 3; | |
2333 | } | |
2334 | ||
2335 | if (ndopts.nd_opts_rh) { | |
2336 | redirhdrlen = ndopts.nd_opts_rh->nd_opt_rh_len; | |
2337 | redirhdr = (u_char *)(ndopts.nd_opts_rh + 1); /* xxx */ | |
2338 | } | |
2339 | ||
2340 | if (lladdr && ((ifp->if_addrlen + 2 + 7) & ~7) != lladdrlen) { | |
9bccf70c | 2341 | nd6log((LOG_INFO, |
1c79356b A |
2342 | "icmp6_redirect_input: lladdrlen mismatch for %s " |
2343 | "(if %d, icmp6 packet %d): %s\n", | |
2344 | ip6_sprintf(&redtgt6), ifp->if_addrlen, lladdrlen - 2, | |
9bccf70c A |
2345 | icmp6_redirect_diag(&src6, &reddst6, &redtgt6))); |
2346 | goto bad; | |
1c79356b A |
2347 | } |
2348 | ||
2349 | /* RFC 2461 8.3 */ | |
2350 | nd6_cache_lladdr(ifp, &redtgt6, lladdr, lladdrlen, ND_REDIRECT, | |
2351 | is_onlink ? ND_REDIRECT_ONLINK : ND_REDIRECT_ROUTER); | |
2352 | ||
55e303ae | 2353 | if (!is_onlink) { /* better router case. perform rtredirect. */ |
1c79356b A |
2354 | /* perform rtredirect */ |
2355 | struct sockaddr_in6 sdst; | |
2356 | struct sockaddr_in6 sgw; | |
2357 | struct sockaddr_in6 ssrc; | |
1c79356b A |
2358 | |
2359 | bzero(&sdst, sizeof(sdst)); | |
2360 | bzero(&sgw, sizeof(sgw)); | |
2361 | bzero(&ssrc, sizeof(ssrc)); | |
2362 | sdst.sin6_family = sgw.sin6_family = ssrc.sin6_family = AF_INET6; | |
2363 | sdst.sin6_len = sgw.sin6_len = ssrc.sin6_len = | |
2364 | sizeof(struct sockaddr_in6); | |
2365 | bcopy(&redtgt6, &sgw.sin6_addr, sizeof(struct in6_addr)); | |
2366 | bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); | |
2367 | bcopy(&src6, &ssrc.sin6_addr, sizeof(struct in6_addr)); | |
2368 | rtredirect((struct sockaddr *)&sdst, (struct sockaddr *)&sgw, | |
2369 | (struct sockaddr *)NULL, RTF_GATEWAY | RTF_HOST, | |
2370 | (struct sockaddr *)&ssrc, | |
9bccf70c | 2371 | (struct rtentry **)NULL); |
1c79356b A |
2372 | } |
2373 | /* finally update cached route in each socket via pfctlinput */ | |
2374 | { | |
2375 | struct sockaddr_in6 sdst; | |
1c79356b A |
2376 | |
2377 | bzero(&sdst, sizeof(sdst)); | |
2378 | sdst.sin6_family = AF_INET6; | |
2379 | sdst.sin6_len = sizeof(struct sockaddr_in6); | |
2380 | bcopy(&reddst6, &sdst.sin6_addr, sizeof(struct in6_addr)); | |
1c79356b | 2381 | pfctlinput(PRC_REDIRECT_HOST, (struct sockaddr *)&sdst); |
1c79356b A |
2382 | #if IPSEC |
2383 | key_sa_routechange((struct sockaddr *)&sdst); | |
2384 | #endif | |
2385 | } | |
2386 | ||
2387 | freeit: | |
2388 | m_freem(m); | |
9bccf70c A |
2389 | return; |
2390 | ||
2391 | bad: | |
2392 | icmp6stat.icp6s_badredirect++; | |
2393 | m_freem(m); | |
1c79356b A |
2394 | } |
2395 | ||
2396 | void | |
2397 | icmp6_redirect_output(m0, rt) | |
2398 | struct mbuf *m0; | |
2399 | struct rtentry *rt; | |
2400 | { | |
2401 | struct ifnet *ifp; /* my outgoing interface */ | |
91447636 | 2402 | struct in6_addr ifp_ll6; |
1c79356b A |
2403 | struct in6_addr *router_ll6; |
2404 | struct ip6_hdr *sip6; /* m0 as struct ip6_hdr */ | |
2405 | struct mbuf *m = NULL; /* newly allocated one */ | |
2406 | struct ip6_hdr *ip6; /* m as struct ip6_hdr */ | |
2407 | struct nd_redirect *nd_rd; | |
2408 | size_t maxlen; | |
2409 | u_char *p; | |
2410 | struct ifnet *outif = NULL; | |
9bccf70c A |
2411 | struct sockaddr_in6 src_sa; |
2412 | ||
2413 | icmp6_errcount(&icmp6stat.icp6s_outerrhist, ND_REDIRECT, 0); | |
1c79356b | 2414 | |
1c79356b A |
2415 | /* sanity check */ |
2416 | if (!m0 || !rt || !(rt->rt_flags & RTF_UP) || !(ifp = rt->rt_ifp)) | |
2417 | goto fail; | |
2418 | ||
55e303ae A |
2419 | /* if we are not router, we don't send icmp6 redirect */ |
2420 | if (!ip6_forwarding || ip6_accept_rtadv || (ifp->if_eflags & IFEF_ACCEPT_RTADVD)) | |
2421 | goto fail; | |
2422 | ||
1c79356b A |
2423 | /* |
2424 | * Address check: | |
2425 | * the source address must identify a neighbor, and | |
2426 | * the destination address must not be a multicast address | |
2427 | * [RFC 2461, sec 8.2] | |
2428 | */ | |
2429 | sip6 = mtod(m0, struct ip6_hdr *); | |
9bccf70c A |
2430 | bzero(&src_sa, sizeof(src_sa)); |
2431 | src_sa.sin6_family = AF_INET6; | |
2432 | src_sa.sin6_len = sizeof(src_sa); | |
2433 | src_sa.sin6_addr = sip6->ip6_src; | |
2434 | /* we don't currently use sin6_scope_id, but eventually use it */ | |
2435 | src_sa.sin6_scope_id = in6_addr2scopeid(ifp, &sip6->ip6_src); | |
91447636 | 2436 | if (nd6_is_addr_neighbor(&src_sa, ifp, 0) == 0) |
1c79356b A |
2437 | goto fail; |
2438 | if (IN6_IS_ADDR_MULTICAST(&sip6->ip6_dst)) | |
2439 | goto fail; /* what should we do here? */ | |
2440 | ||
2441 | /* rate limit */ | |
2442 | if (icmp6_ratelimit(&sip6->ip6_src, ND_REDIRECT, 0)) | |
2443 | goto fail; | |
2444 | ||
2445 | /* | |
2446 | * Since we are going to append up to 1280 bytes (= IPV6_MMTU), | |
2447 | * we almost always ask for an mbuf cluster for simplicity. | |
2448 | * (MHLEN < IPV6_MMTU is almost always true) | |
2449 | */ | |
2450 | #if IPV6_MMTU >= MCLBYTES | |
2451 | # error assumption failed about IPV6_MMTU and MCLBYTES | |
2452 | #endif | |
2453 | MGETHDR(m, M_DONTWAIT, MT_HEADER); | |
2454 | if (m && IPV6_MMTU >= MHLEN) | |
2455 | MCLGET(m, M_DONTWAIT); | |
2456 | if (!m) | |
2457 | goto fail; | |
9bccf70c A |
2458 | m->m_pkthdr.rcvif = NULL; |
2459 | m->m_len = 0; | |
2460 | maxlen = M_TRAILINGSPACE(m); | |
1c79356b A |
2461 | maxlen = min(IPV6_MMTU, maxlen); |
2462 | /* just for safety */ | |
2463 | if (maxlen < sizeof(struct ip6_hdr) + sizeof(struct icmp6_hdr) + | |
2464 | ((sizeof(struct nd_opt_hdr) + ifp->if_addrlen + 7) & ~7)) { | |
2465 | goto fail; | |
2466 | } | |
2467 | ||
2468 | { | |
2469 | /* get ip6 linklocal address for ifp(my outgoing interface). */ | |
2470 | struct in6_ifaddr *ia; | |
2471 | if ((ia = in6ifa_ifpforlinklocal(ifp, | |
2472 | IN6_IFF_NOTREADY| | |
2473 | IN6_IFF_ANYCAST)) == NULL) | |
2474 | goto fail; | |
91447636 A |
2475 | ifp_ll6 = ia->ia_addr.sin6_addr; |
2476 | ifafree(&ia->ia_ifa); | |
1c79356b A |
2477 | } |
2478 | ||
2479 | /* get ip6 linklocal address for the router. */ | |
2480 | if (rt->rt_gateway && (rt->rt_flags & RTF_GATEWAY)) { | |
2481 | struct sockaddr_in6 *sin6; | |
2482 | sin6 = (struct sockaddr_in6 *)rt->rt_gateway; | |
2483 | router_ll6 = &sin6->sin6_addr; | |
2484 | if (!IN6_IS_ADDR_LINKLOCAL(router_ll6)) | |
2485 | router_ll6 = (struct in6_addr *)NULL; | |
2486 | } else | |
2487 | router_ll6 = (struct in6_addr *)NULL; | |
2488 | ||
2489 | /* ip6 */ | |
2490 | ip6 = mtod(m, struct ip6_hdr *); | |
2491 | ip6->ip6_flow = 0; | |
2492 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; | |
2493 | ip6->ip6_vfc |= IPV6_VERSION; | |
2494 | /* ip6->ip6_plen will be set later */ | |
2495 | ip6->ip6_nxt = IPPROTO_ICMPV6; | |
2496 | ip6->ip6_hlim = 255; | |
2497 | /* ip6->ip6_src must be linklocal addr for my outgoing if. */ | |
91447636 | 2498 | bcopy(&ifp_ll6, &ip6->ip6_src, sizeof(struct in6_addr)); |
1c79356b A |
2499 | bcopy(&sip6->ip6_src, &ip6->ip6_dst, sizeof(struct in6_addr)); |
2500 | ||
2501 | /* ND Redirect */ | |
2502 | nd_rd = (struct nd_redirect *)(ip6 + 1); | |
2503 | nd_rd->nd_rd_type = ND_REDIRECT; | |
2504 | nd_rd->nd_rd_code = 0; | |
2505 | nd_rd->nd_rd_reserved = 0; | |
2506 | if (rt->rt_flags & RTF_GATEWAY) { | |
2507 | /* | |
2508 | * nd_rd->nd_rd_target must be a link-local address in | |
2509 | * better router cases. | |
2510 | */ | |
2511 | if (!router_ll6) | |
2512 | goto fail; | |
2513 | bcopy(router_ll6, &nd_rd->nd_rd_target, | |
2514 | sizeof(nd_rd->nd_rd_target)); | |
2515 | bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, | |
2516 | sizeof(nd_rd->nd_rd_dst)); | |
2517 | } else { | |
2518 | /* make sure redtgt == reddst */ | |
2519 | bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_target, | |
2520 | sizeof(nd_rd->nd_rd_target)); | |
2521 | bcopy(&sip6->ip6_dst, &nd_rd->nd_rd_dst, | |
2522 | sizeof(nd_rd->nd_rd_dst)); | |
2523 | } | |
2524 | ||
2525 | p = (u_char *)(nd_rd + 1); | |
2526 | ||
2527 | if (!router_ll6) | |
2528 | goto nolladdropt; | |
2529 | ||
2530 | { | |
2531 | /* target lladdr option */ | |
2532 | struct rtentry *rt_router = NULL; | |
2533 | int len; | |
2534 | struct sockaddr_dl *sdl; | |
2535 | struct nd_opt_hdr *nd_opt; | |
2536 | char *lladdr; | |
2537 | ||
91447636 | 2538 | rt_router = nd6_lookup(router_ll6, 0, ifp, 0); |
1c79356b A |
2539 | if (!rt_router) |
2540 | goto nolladdropt; | |
2541 | len = sizeof(*nd_opt) + ifp->if_addrlen; | |
55e303ae | 2542 | len = (len + 7) & ~7; /* round by 8 */ |
1c79356b A |
2543 | /* safety check */ |
2544 | if (len + (p - (u_char *)ip6) > maxlen) | |
2545 | goto nolladdropt; | |
2546 | if (!(rt_router->rt_flags & RTF_GATEWAY) && | |
2547 | (rt_router->rt_flags & RTF_LLINFO) && | |
2548 | (rt_router->rt_gateway->sa_family == AF_LINK) && | |
2549 | (sdl = (struct sockaddr_dl *)rt_router->rt_gateway) && | |
2550 | sdl->sdl_alen) { | |
2551 | nd_opt = (struct nd_opt_hdr *)p; | |
2552 | nd_opt->nd_opt_type = ND_OPT_TARGET_LINKADDR; | |
2553 | nd_opt->nd_opt_len = len >> 3; | |
2554 | lladdr = (char *)(nd_opt + 1); | |
2555 | bcopy(LLADDR(sdl), lladdr, ifp->if_addrlen); | |
2556 | p += len; | |
2557 | } | |
2558 | } | |
2559 | nolladdropt:; | |
2560 | ||
2561 | m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; | |
2562 | ||
2563 | /* just to be safe */ | |
9bccf70c | 2564 | #ifdef M_DECRYPTED /*not openbsd*/ |
1c79356b A |
2565 | if (m0->m_flags & M_DECRYPTED) |
2566 | goto noredhdropt; | |
2567 | #endif | |
2568 | if (p - (u_char *)ip6 > maxlen) | |
2569 | goto noredhdropt; | |
2570 | ||
2571 | { | |
2572 | /* redirected header option */ | |
2573 | int len; | |
2574 | struct nd_opt_rd_hdr *nd_opt_rh; | |
2575 | ||
2576 | /* | |
2577 | * compute the maximum size for icmp6 redirect header option. | |
2578 | * XXX room for auth header? | |
2579 | */ | |
2580 | len = maxlen - (p - (u_char *)ip6); | |
2581 | len &= ~7; | |
2582 | ||
2583 | /* This is just for simplicity. */ | |
2584 | if (m0->m_pkthdr.len != m0->m_len) { | |
2585 | if (m0->m_next) { | |
2586 | m_freem(m0->m_next); | |
2587 | m0->m_next = NULL; | |
2588 | } | |
2589 | m0->m_pkthdr.len = m0->m_len; | |
2590 | } | |
2591 | ||
2592 | /* | |
2593 | * Redirected header option spec (RFC2461 4.6.3) talks nothing | |
2594 | * about padding/truncate rule for the original IP packet. | |
2595 | * From the discussion on IPv6imp in Feb 1999, the consensus was: | |
2596 | * - "attach as much as possible" is the goal | |
2597 | * - pad if not aligned (original size can be guessed by original | |
2598 | * ip6 header) | |
2599 | * Following code adds the padding if it is simple enough, | |
2600 | * and truncates if not. | |
2601 | */ | |
2602 | if (m0->m_next || m0->m_pkthdr.len != m0->m_len) | |
2603 | panic("assumption failed in %s:%d\n", __FILE__, __LINE__); | |
2604 | ||
2605 | if (len - sizeof(*nd_opt_rh) < m0->m_pkthdr.len) { | |
2606 | /* not enough room, truncate */ | |
2607 | m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); | |
2608 | } else { | |
2609 | /* enough room, pad or truncate */ | |
2610 | size_t extra; | |
2611 | ||
2612 | extra = m0->m_pkthdr.len % 8; | |
2613 | if (extra) { | |
2614 | /* pad if easy enough, truncate if not */ | |
2615 | if (8 - extra <= M_TRAILINGSPACE(m0)) { | |
2616 | /* pad */ | |
2617 | m0->m_len += (8 - extra); | |
2618 | m0->m_pkthdr.len += (8 - extra); | |
2619 | } else { | |
2620 | /* truncate */ | |
2621 | m0->m_pkthdr.len -= extra; | |
2622 | m0->m_len -= extra; | |
2623 | } | |
2624 | } | |
2625 | len = m0->m_pkthdr.len + sizeof(*nd_opt_rh); | |
2626 | m0->m_pkthdr.len = m0->m_len = len - sizeof(*nd_opt_rh); | |
2627 | } | |
2628 | ||
2629 | nd_opt_rh = (struct nd_opt_rd_hdr *)p; | |
2630 | bzero(nd_opt_rh, sizeof(*nd_opt_rh)); | |
2631 | nd_opt_rh->nd_opt_rh_type = ND_OPT_REDIRECTED_HEADER; | |
2632 | nd_opt_rh->nd_opt_rh_len = len >> 3; | |
2633 | p += sizeof(*nd_opt_rh); | |
2634 | m->m_pkthdr.len = m->m_len = p - (u_char *)ip6; | |
2635 | ||
2636 | /* connect m0 to m */ | |
2637 | m->m_next = m0; | |
2638 | m->m_pkthdr.len = m->m_len + m0->m_len; | |
2639 | } | |
2640 | noredhdropt:; | |
2641 | ||
2642 | if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_src)) | |
2643 | sip6->ip6_src.s6_addr16[1] = 0; | |
2644 | if (IN6_IS_ADDR_LINKLOCAL(&sip6->ip6_dst)) | |
2645 | sip6->ip6_dst.s6_addr16[1] = 0; | |
2646 | #if 0 | |
2647 | if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) | |
2648 | ip6->ip6_src.s6_addr16[1] = 0; | |
2649 | if (IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_dst)) | |
2650 | ip6->ip6_dst.s6_addr16[1] = 0; | |
2651 | #endif | |
2652 | if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_target)) | |
2653 | nd_rd->nd_rd_target.s6_addr16[1] = 0; | |
2654 | if (IN6_IS_ADDR_LINKLOCAL(&nd_rd->nd_rd_dst)) | |
2655 | nd_rd->nd_rd_dst.s6_addr16[1] = 0; | |
2656 | ||
2657 | ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr)); | |
2658 | ||
2659 | nd_rd->nd_rd_cksum = 0; | |
2660 | nd_rd->nd_rd_cksum | |
2661 | = in6_cksum(m, IPPROTO_ICMPV6, sizeof(*ip6), ntohs(ip6->ip6_plen)); | |
2662 | ||
2663 | /* send the packet to outside... */ | |
2664 | #if IPSEC | |
2665 | /* Don't lookup socket */ | |
9bccf70c A |
2666 | if (ipsec_bypass == 0) |
2667 | (void)ipsec_setsocket(m, NULL); | |
1c79356b | 2668 | #endif /*IPSEC*/ |
91447636 | 2669 | ip6_output(m, NULL, NULL, 0, NULL, &outif, 0); |
1c79356b A |
2670 | if (outif) { |
2671 | icmp6_ifstat_inc(outif, ifs6_out_msg); | |
2672 | icmp6_ifstat_inc(outif, ifs6_out_redirect); | |
2673 | } | |
2674 | icmp6stat.icp6s_outhist[ND_REDIRECT]++; | |
2675 | ||
2676 | return; | |
2677 | ||
2678 | fail: | |
2679 | if (m) | |
2680 | m_freem(m); | |
2681 | if (m0) | |
2682 | m_freem(m0); | |
2683 | } | |
2684 | ||
9bccf70c A |
2685 | #if HAVE_NRL_INPCB |
2686 | #define sotoin6pcb sotoinpcb | |
2687 | #define in6pcb inpcb | |
2688 | #define in6p_icmp6filt inp_icmp6filt | |
2689 | #endif | |
1c79356b A |
2690 | /* |
2691 | * ICMPv6 socket option processing. | |
1c79356b A |
2692 | */ |
2693 | int | |
1c79356b A |
2694 | icmp6_ctloutput(so, sopt) |
2695 | struct socket *so; | |
2696 | struct sockopt *sopt; | |
1c79356b A |
2697 | { |
2698 | int error = 0; | |
2699 | int optlen; | |
9bccf70c | 2700 | struct inpcb *inp = sotoinpcb(so); |
1c79356b A |
2701 | int level, op, optname; |
2702 | ||
2703 | if (sopt) { | |
2704 | level = sopt->sopt_level; | |
2705 | op = sopt->sopt_dir; | |
2706 | optname = sopt->sopt_name; | |
2707 | optlen = sopt->sopt_valsize; | |
2708 | } else | |
2709 | level = op = optname = optlen = 0; | |
1c79356b A |
2710 | |
2711 | if (level != IPPROTO_ICMPV6) { | |
1c79356b A |
2712 | return EINVAL; |
2713 | } | |
2714 | ||
9bccf70c | 2715 | switch (op) { |
1c79356b A |
2716 | case PRCO_SETOPT: |
2717 | switch (optname) { | |
2718 | case ICMP6_FILTER: | |
2719 | { | |
2720 | struct icmp6_filter *p; | |
2721 | ||
2722 | if (optlen != sizeof(*p)) { | |
2723 | error = EMSGSIZE; | |
2724 | break; | |
2725 | } | |
1c79356b A |
2726 | if (inp->in6p_icmp6filt == NULL) { |
2727 | error = EINVAL; | |
2728 | break; | |
2729 | } | |
2730 | error = sooptcopyin(sopt, inp->in6p_icmp6filt, optlen, | |
2731 | optlen); | |
1c79356b A |
2732 | break; |
2733 | } | |
2734 | ||
2735 | default: | |
2736 | error = ENOPROTOOPT; | |
2737 | break; | |
2738 | } | |
1c79356b A |
2739 | break; |
2740 | ||
2741 | case PRCO_GETOPT: | |
2742 | switch (optname) { | |
2743 | case ICMP6_FILTER: | |
2744 | { | |
1c79356b A |
2745 | if (inp->in6p_icmp6filt == NULL) { |
2746 | error = EINVAL; | |
2747 | break; | |
2748 | } | |
2749 | error = sooptcopyout(sopt, inp->in6p_icmp6filt, | |
2750 | sizeof(struct icmp6_filter)); | |
1c79356b A |
2751 | break; |
2752 | } | |
2753 | ||
2754 | default: | |
2755 | error = ENOPROTOOPT; | |
2756 | break; | |
2757 | } | |
2758 | break; | |
2759 | } | |
2760 | ||
2761 | return(error); | |
2762 | } | |
9bccf70c A |
2763 | #if HAVE_NRL_INPCB |
2764 | #undef sotoin6pcb | |
2765 | #undef in6pcb | |
2766 | #undef in6p_icmp6filt | |
2767 | #endif | |
2768 | ||
2769 | #ifndef HAVE_PPSRATECHECK | |
2770 | #ifndef timersub | |
2771 | #define timersub(tvp, uvp, vvp) \ | |
2772 | do { \ | |
2773 | (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ | |
2774 | (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ | |
2775 | if ((vvp)->tv_usec < 0) { \ | |
2776 | (vvp)->tv_sec--; \ | |
2777 | (vvp)->tv_usec += 1000000; \ | |
2778 | } \ | |
2779 | } while (0) | |
2780 | #endif | |
2781 | ||
2782 | /* | |
2783 | * ppsratecheck(): packets (or events) per second limitation. | |
2784 | */ | |
2785 | static int | |
2786 | ppsratecheck(lasttime, curpps, maxpps) | |
2787 | struct timeval *lasttime; | |
2788 | int *curpps; | |
2789 | int maxpps; /* maximum pps allowed */ | |
2790 | { | |
2791 | struct timeval tv, delta; | |
2792 | int s, rv; | |
2793 | ||
2794 | s = splclock(); | |
2795 | microtime(&tv); | |
2796 | splx(s); | |
2797 | ||
2798 | timersub(&tv, lasttime, &delta); | |
2799 | ||
2800 | /* | |
55e303ae A |
2801 | * Check for 0,0 so that the message will be seen at least once. |
2802 | * If more than one second has passed since the last update of | |
9bccf70c A |
2803 | * lasttime, reset the counter. |
2804 | * | |
2805 | * we do increment *curpps even in *curpps < maxpps case, as some may | |
2806 | * try to use *curpps for stat purposes as well. | |
2807 | */ | |
2808 | if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || | |
2809 | delta.tv_sec >= 1) { | |
2810 | *lasttime = tv; | |
2811 | *curpps = 0; | |
2812 | rv = 1; | |
2813 | } else if (maxpps < 0) | |
2814 | rv = 1; | |
2815 | else if (*curpps < maxpps) | |
2816 | rv = 1; | |
2817 | else | |
2818 | rv = 0; | |
2819 | ||
55e303ae | 2820 | #if 1 /* DIAGNOSTIC? */ |
9bccf70c A |
2821 | /* be careful about wrap-around */ |
2822 | if (*curpps + 1 > *curpps) | |
2823 | *curpps = *curpps + 1; | |
2824 | #else | |
2825 | /* | |
2826 | * assume that there's not too many calls to this function. | |
2827 | * not sure if the assumption holds, as it depends on *caller's* | |
2828 | * behavior, not the behavior of this function. | |
2829 | * IMHO it is wrong to make assumption on the caller's behavior, | |
2830 | * so the above #if is #if 1, not #ifdef DIAGNOSTIC. | |
2831 | */ | |
2832 | *curpps = *curpps + 1; | |
2833 | #endif | |
2834 | ||
2835 | return (rv); | |
2836 | } | |
2837 | #endif | |
1c79356b A |
2838 | |
2839 | /* | |
2840 | * Perform rate limit check. | |
2841 | * Returns 0 if it is okay to send the icmp6 packet. | |
2842 | * Returns 1 if the router SHOULD NOT send this icmp6 packet due to rate | |
2843 | * limitation. | |
2844 | * | |
2845 | * XXX per-destination/type check necessary? | |
2846 | */ | |
2847 | static int | |
2848 | icmp6_ratelimit(dst, type, code) | |
2849 | const struct in6_addr *dst; /* not used at this moment */ | |
2850 | const int type; /* not used at this moment */ | |
2851 | const int code; /* not used at this moment */ | |
2852 | { | |
9bccf70c | 2853 | int ret; |
1c79356b | 2854 | |
55e303ae | 2855 | ret = 0; /* okay to send */ |
1c79356b | 2856 | |
9bccf70c A |
2857 | /* PPS limit */ |
2858 | if (!ppsratecheck(&icmp6errppslim_last, &icmp6errpps_count, | |
2859 | icmp6errppslim)) { | |
1c79356b | 2860 | /* The packet is subject to rate limit */ |
9bccf70c | 2861 | ret++; |
1c79356b | 2862 | } |
1c79356b | 2863 | |
9bccf70c | 2864 | return ret; |
1c79356b | 2865 | } |