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