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