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