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