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