xref: /openbsd-src/sys/netinet6/in6_pcb.c (revision 8500990981f885cbe5e6a4958549cacc238b5ae6)
1 /*	$OpenBSD: in6_pcb.c,v 1.38 2003/11/04 22:39:38 markus 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	1.1 (NRL) 17 January 1995
34  *
35  * NRL grants permission for redistribution and use in source and binary
36  * forms, with or without modification, of the software and documentation
37  * created at NRL provided that the following conditions are met:
38  *
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 acknowledgements:
46  * 	This product includes software developed by the University of
47  * 	California, Berkeley and its contributors.
48  * 	This product includes software developed at the Information
49  * 	Technology Division, US Naval Research Laboratory.
50  * 4. Neither the name of the NRL nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
58  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  *
66  * The views and conclusions contained in the software and documentation
67  * are those of the authors and should not be interpreted as representing
68  * official policies, either expressed or implied, of the US Naval
69  * Research Laboratory (NRL).
70  */
71 
72 /*
73  * Copyright (c) 1982, 1986, 1990, 1993, 1995
74  *	Regents of the University of California.  All rights reserved.
75  *
76  * Redistribution and use in source and binary forms, with or without
77  * modification, are permitted provided that the following conditions
78  * are met:
79  * 1. Redistributions of source code must retain the above copyright
80  *    notice, this list of conditions and the following disclaimer.
81  * 2. Redistributions in binary form must reproduce the above copyright
82  *    notice, this list of conditions and the following disclaimer in the
83  *    documentation and/or other materials provided with the distribution.
84  * 3. Neither the name of the University nor the names of its contributors
85  *    may be used to endorse or promote products derived from this software
86  *    without specific prior written permission.
87  *
88  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98  * SUCH DAMAGE.
99  *
100  */
101 
102 #include <sys/param.h>
103 #include <sys/systm.h>
104 #include <sys/malloc.h>
105 #include <sys/mbuf.h>
106 #include <sys/domain.h>
107 #include <sys/protosw.h>
108 #include <sys/socket.h>
109 #include <sys/socketvar.h>
110 #include <sys/errno.h>
111 #include <sys/time.h>
112 #include <sys/proc.h>
113 
114 #include <net/if.h>
115 #include <net/route.h>
116 
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/ip.h>
120 #include <netinet/in_pcb.h>
121 
122 #include <netinet6/in6_var.h>
123 #include <netinet/ip6.h>
124 #include <netinet6/ip6_var.h>
125 
126 /*
127  * External globals
128  */
129 
130 #include <dev/rndvar.h>
131 
132 extern struct in6_ifaddr *in6_ifaddr;
133 extern struct in_ifaddr *in_ifaddr;
134 
135 /*
136  * Globals
137  */
138 
139 struct in6_addr zeroin6_addr;
140 
141 extern int ipport_firstauto;
142 extern int ipport_lastauto;
143 extern int ipport_hifirstauto;
144 extern int ipport_hilastauto;
145 
146 /*
147  * Keep separate inet6ctlerrmap, because I may remap some of these.
148  * I also put it here, because, quite frankly, it belongs here, not in
149  * ip{v6,}_input().
150  */
151 #if 0
152 u_char inet6ctlerrmap[PRC_NCMDS] = {
153 	0,		0,		0,		0,
154 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
155 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
156 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
157 	0,		0,		0,		0,
158 	ENOPROTOOPT
159 };
160 #endif
161 
162 /*
163  * Bind an address (or at least a port) to an PF_INET6 socket.
164  */
165 int
166 in6_pcbbind(inp, nam)
167 	struct inpcb *inp;
168 	struct mbuf *nam;
169 {
170 	struct socket *so = inp->inp_socket;
171 
172 	struct inpcbtable *head = inp->inp_table;
173 	struct sockaddr_in6 *sin6;
174 	struct proc *p = curproc;		/* XXX */
175 	u_short lport = 0;
176 	int wild = INPLOOKUP_IPV6, reuseport = (so->so_options & SO_REUSEPORT);
177 	int error;
178 
179 	/*
180 	 * REMINDER:  Once up to speed, flow label processing should go here,
181 	 * too.  (Same with in6_pcbconnect.)
182 	 */
183 	if (in6_ifaddr == 0 || in_ifaddr == 0)
184 		return EADDRNOTAVAIL;
185 
186 	if (inp->inp_lport != 0 || !IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6))
187 		return EINVAL;	/* If already bound, EINVAL! */
188 
189 	if ((so->so_options & (SO_REUSEADDR | SO_REUSEPORT)) == 0 &&
190 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
191 	     (so->so_options & SO_ACCEPTCONN) == 0))
192 		wild |= INPLOOKUP_WILDCARD;
193 
194 	/*
195 	 * If I did get a sockaddr passed in...
196 	 */
197 	if (nam) {
198 		sin6 = mtod(nam, struct sockaddr_in6 *);
199 		if (nam->m_len != sizeof (*sin6))
200 			return EINVAL;
201 
202 		/*
203 		 * Unlike v4, I have no qualms about EAFNOSUPPORT if the
204 		 * wretched family is not filled in!
205 		 */
206 		if (sin6->sin6_family != AF_INET6)
207 			return EAFNOSUPPORT;
208 
209 		/* KAME hack: embed scopeid */
210 		if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
211 			return EINVAL;
212 		/* this must be cleared for ifa_ifwithaddr() */
213 		sin6->sin6_scope_id = 0;
214 
215 		lport = sin6->sin6_port;
216 
217 		/* reject IPv4 mapped address, we have no support for it */
218 		if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
219 			return EADDRNOTAVAIL;
220 
221 		if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
222 			/*
223 			 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
224 			 * allow complete duplication of binding if
225 			 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
226 			 * and a multicast address is bound on both
227 			 * new and duplicated sockets.
228 			 */
229 			if (so->so_options & SO_REUSEADDR)
230 				reuseport = SO_REUSEADDR | SO_REUSEPORT;
231 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
232 			struct ifaddr *ia = NULL;
233 
234 			sin6->sin6_port = 0;  /*
235 					       * Yechhhh, because of upcoming
236 					       * call to ifa_ifwithaddr(), which
237 					       * does bcmp's over the PORTS as
238 					       * well.  (What about flow?)
239 					       */
240 			sin6->sin6_flowinfo = 0;
241 			if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6))
242 			    == NULL)
243 				return EADDRNOTAVAIL;
244 
245 			/*
246 			 * bind to an anycast address might accidentally
247 			 * cause sending a packet with an anycast source
248 			 * address, so we forbid it.
249 			 *
250 			 * We should allow to bind to a deprecated address,
251 			 * since the application dare to use it.
252 			 * But, can we assume that they are careful enough
253 			 * to check if the address is deprecated or not?
254 			 * Maybe, as a safeguard, we should have a setsockopt
255 			 * flag to control the bind(2) behavior against
256 			 * deprecated addresses (default: forbid bind(2)).
257 			 */
258 			if (ia &&
259 			    ((struct in6_ifaddr *)ia)->ia6_flags &
260 			    (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED))
261 				return (EADDRNOTAVAIL);
262 		}
263 		if (lport) {
264 			struct inpcb *t;
265 
266 			/*
267 			 * Question:  Do we wish to continue the Berkeley
268 			 * tradition of ports < IPPORT_RESERVED be only for
269 			 * root?
270 			 * Answer: For now yes, but IMHO, it should be REMOVED!
271 			 * OUCH: One other thing, is there no better way of
272 			 * finding a process for a socket instead of using
273 			 * curproc?  (Marked with BSD's {in,}famous XXX ?
274 			 */
275 			if (ntohs(lport) < IPPORT_RESERVED &&
276 			    (error = suser(p, 0)))
277 				return error;
278 
279 			t = in_pcblookup(head,
280 			    (struct in_addr *)&zeroin6_addr, 0,
281 			    (struct in_addr *)&sin6->sin6_addr, lport,
282 			    wild);
283 
284 			if (t && (reuseport & t->inp_socket->so_options) == 0)
285 				return EADDRINUSE;
286 		}
287 		inp->inp_laddr6 = sin6->sin6_addr;
288 	}
289 
290 	if (lport == 0) {
291 		error = in6_pcbsetport(&inp->inp_laddr6, inp, p);
292 		if (error != 0)
293 			return error;
294 	} else {
295 		inp->inp_lport = lport;
296 		in_pcbrehash(inp);
297 	}
298 
299 	return 0;
300 }
301 
302 int
303 in6_pcbsetport(laddr, inp, p)
304 	struct in6_addr *laddr;
305 	struct inpcb *inp;
306 	struct proc *p;
307 {
308 	struct socket *so = inp->inp_socket;
309 	struct inpcbtable *table = inp->inp_table;
310 	u_int16_t first, last, old = 0;
311 	u_int16_t *lastport = &inp->inp_table->inpt_lastport;
312 	u_int16_t lport = 0;
313 	int count;
314 	int loopcount = 0;
315 	int wild = INPLOOKUP_IPV6;
316 	int error;
317 
318 	/* XXX we no longer support IPv4 mapped address, so no tweaks here */
319 
320 	if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
321 	    ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
322 	     (so->so_options & SO_ACCEPTCONN) == 0))
323 		wild |= INPLOOKUP_WILDCARD;
324 
325 	if (inp->inp_flags & INP_HIGHPORT) {
326 		first = ipport_hifirstauto;	/* sysctl */
327 		last = ipport_hilastauto;
328 	} else if (inp->inp_flags & INP_LOWPORT) {
329 		if ((error = suser(p, 0)))
330 			return (EACCES);
331 		first = IPPORT_RESERVED-1; /* 1023 */
332 		last = 600;		   /* not IPPORT_RESERVED/2 */
333 	} else {
334 		first = ipport_firstauto;	/* sysctl */
335 		last  = ipport_lastauto;
336 	}
337 
338 	/*
339 	 * Simple check to ensure all ports are not used up causing
340 	 * a deadlock here.
341 	 *
342 	 * We split the two cases (up and down) so that the direction
343 	 * is not being tested on each round of the loop.
344 	 */
345 
346 portloop:
347 	if (first > last) {
348 		/*
349 		 * counting down
350 		 */
351 		if (loopcount == 0) {	/* only do this once. */
352 			old = first;
353 			first -= (arc4random() % (first - last));
354 		}
355 		count = first - last;
356 		*lastport = first;		/* restart each time */
357 
358 		do {
359 			if (count-- <= 0) {	/* completely used? */
360 				if (loopcount == 0) {
361 					last = old;
362 					loopcount++;
363 					goto portloop;
364 				}
365 				return (EADDRNOTAVAIL);
366 			}
367 			--*lastport;
368 			if (*lastport > first || *lastport < last)
369 				*lastport = first;
370 			lport = htons(*lastport);
371 		} while (in_baddynamic(*lastport, so->so_proto->pr_protocol) ||
372 		    in_pcblookup(table, &zeroin6_addr, 0,
373 		    &inp->inp_laddr6, lport, wild));
374 	} else {
375 		/*
376 		 * counting up
377 		 */
378 		if (loopcount == 0) {	/* only do this once. */
379 			old = first;
380 			first += (arc4random() % (last - first));
381 		}
382 		count = last - first;
383 		*lastport = first;		/* restart each time */
384 
385 		do {
386 			if (count-- <= 0) {	/* completely used? */
387 				if (loopcount == 0) {
388 					first = old;
389 					loopcount++;
390 					goto portloop;
391 				}
392 				return (EADDRNOTAVAIL);
393 			}
394 			++*lastport;
395 			if (*lastport < first || *lastport > last)
396 				*lastport = first;
397 			lport = htons(*lastport);
398 		} while (in_baddynamic(*lastport, so->so_proto->pr_protocol) ||
399 		    in_pcblookup(table, &zeroin6_addr, 0,
400 		    &inp->inp_laddr6, lport, wild));
401 	}
402 
403 	inp->inp_lport = lport;
404 	in_pcbrehash(inp);
405 
406 #if 0
407 	inp->inp_flowinfo = 0;	/* XXX */
408 #endif
409 
410 	return 0;
411 }
412 
413 /*
414  * Connect from a socket to a specified address.
415  * Both address and port must be specified in argument sin6.
416  * Eventually, flow labels will have to be dealt with here, as well.
417  *
418  * If don't have a local address for this socket yet,
419  * then pick one.
420  *
421  * I believe this has to be called at splnet().
422  */
423 int
424 in6_pcbconnect(inp, nam)
425 	struct inpcb *inp;
426 	struct mbuf *nam;
427 {
428 	struct in6_addr *in6a = NULL;
429 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
430 	struct ifnet *ifp = NULL;	/* outgoing interface */
431 	int error = 0;
432 	struct in6_addr mapped;
433 	struct sockaddr_in6 tmp;
434 
435 	(void)&in6a;				/* XXX fool gcc */
436 
437 	if (nam->m_len != sizeof(*sin6))
438 		return (EINVAL);
439 	if (sin6->sin6_family != AF_INET6)
440 		return (EAFNOSUPPORT);
441 	if (sin6->sin6_port == 0)
442 		return (EADDRNOTAVAIL);
443 
444 	/* reject IPv4 mapped address, we have no support for it */
445 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
446 		return EADDRNOTAVAIL;
447 
448 	/* sanity check for mapped address case */
449 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
450 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6))
451 			inp->inp_laddr6.s6_addr16[5] = htons(0xffff);
452 		if (!IN6_IS_ADDR_V4MAPPED(&inp->inp_laddr6))
453 			return EINVAL;
454 	} else {
455 		if (IN6_IS_ADDR_V4MAPPED(&inp->inp_laddr6))
456 			return EINVAL;
457 	}
458 
459 	/* protect *sin6 from overwrites */
460 	tmp = *sin6;
461 	sin6 = &tmp;
462 
463 	/* KAME hack: embed scopeid */
464 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
465 		return EINVAL;
466 	/* this must be cleared for ifa_ifwithaddr() */
467 	sin6->sin6_scope_id = 0;
468 
469 	/* Source address selection. */
470 	if (IN6_IS_ADDR_V4MAPPED(&inp->inp_laddr6) &&
471 	    inp->inp_laddr6.s6_addr32[3] == 0) {
472 		struct sockaddr_in sin, *sinp;
473 
474 		bzero(&sin, sizeof(sin));
475 		sin.sin_len = sizeof(sin);
476 		sin.sin_family = AF_INET;
477 		bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
478 		    sizeof(sin.sin_addr));
479 		sinp = in_selectsrc(&sin, (struct route *)&inp->inp_route6,
480 		    inp->inp_socket->so_options, NULL, &error);
481 		if (sinp == 0) {
482 			if (error == 0)
483 				error = EADDRNOTAVAIL;
484 			return (error);
485 		}
486 		bzero(&mapped, sizeof(mapped));
487 		mapped.s6_addr16[5] = htons(0xffff);
488 		bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
489 		in6a = &mapped;
490 	} else {
491 		/*
492 		 * XXX: in6_selectsrc might replace the bound local address
493 		 * with the address specified by setsockopt(IPV6_PKTINFO).
494 		 * Is it the intended behavior?
495 		 */
496 		in6a = in6_selectsrc(sin6, inp->inp_outputopts6,
497 		    inp->inp_moptions6, &inp->inp_route6, &inp->inp_laddr6,
498 		    &error);
499 		if (in6a == 0) {
500 			if (error == 0)
501 				error = EADDRNOTAVAIL;
502 			return (error);
503 		}
504 	}
505 	if (inp->inp_route6.ro_rt)
506 		ifp = inp->inp_route6.ro_rt->rt_ifp;
507 
508 	inp->inp_ipv6.ip6_hlim = (u_int8_t)in6_selecthlim(inp, ifp);
509 
510 	if (in_pcblookup(inp->inp_table, &sin6->sin6_addr, sin6->sin6_port,
511 	    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ? in6a : &inp->inp_laddr6,
512 	    inp->inp_lport, INPLOOKUP_IPV6)) {
513 		return (EADDRINUSE);
514 	}
515 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ||
516 	    (IN6_IS_ADDR_V4MAPPED(&inp->inp_laddr6) &&
517 	     inp->inp_laddr6.s6_addr32[3] == 0)) {
518 		if (inp->inp_lport == 0)
519 			(void)in6_pcbbind(inp, (struct mbuf *)0);
520 		inp->inp_laddr6 = *in6a;
521 	}
522 	inp->inp_faddr6 = sin6->sin6_addr;
523 	inp->inp_fport = sin6->sin6_port;
524 	inp->inp_flowinfo &= ~IPV6_FLOWLABEL_MASK;
525 	if (ip6_auto_flowlabel)
526 		inp->inp_flowinfo |=
527 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
528 	in_pcbrehash(inp);
529 	return (0);
530 }
531 
532 /*
533  * Pass some notification to all connections of a protocol
534  * associated with address dst.  The local address and/or port numbers
535  * may be specified to limit the search.  The "usual action" will be
536  * taken, depending on the ctlinput cmd.  The caller must filter any
537  * cmds that are uninteresting (e.g., no error in the map).
538  * Call the protocol specific routine (if any) to report
539  * any errors for each matching socket.
540  *
541  * Also perform input-side security policy check
542  *    once PCB to be notified has been located.
543  *
544  * Must be called at splnet.
545  */
546 int
547 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
548 	struct inpcbtable *head;
549 	struct sockaddr *dst, *src;
550 	uint fport_arg;
551 	uint lport_arg;
552 	int cmd;
553 	void *cmdarg;
554 	void (*notify)(struct inpcb *, int);
555 {
556 	struct inpcb *inp, *ninp;
557 	u_short fport = fport_arg, lport = lport_arg;
558 	struct sockaddr_in6 sa6_src, *sa6_dst;
559 	int errno, nmatch = 0;
560 	u_int32_t flowinfo;
561 
562 	if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
563 		return 1;
564 
565 	sa6_dst = (struct sockaddr_in6 *)dst;
566 	if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
567 		return 1;
568 	if (IN6_IS_ADDR_V4MAPPED(&sa6_dst->sin6_addr))
569 		printf("Huh?  Thought in6_pcbnotify() never got "
570 		       "called with mapped!\n");
571 
572 	/*
573 	 * note that src can be NULL when we get notify by local fragmentation.
574 	 */
575 	sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
576 	flowinfo = sa6_src.sin6_flowinfo;
577 
578 	/*
579 	 * Redirects go to all references to the destination,
580 	 * and use in_rtchange to invalidate the route cache.
581 	 * Dead host indications: also use in_rtchange to invalidate
582 	 * the cache, and deliver the error to all the sockets.
583 	 * Otherwise, if we have knowledge of the local port and address,
584 	 * deliver only to that socket.
585 	 */
586 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
587 		fport = 0;
588 		lport = 0;
589 		sa6_src.sin6_addr = in6addr_any;
590 
591 		if (cmd != PRC_HOSTDEAD)
592 			notify = in_rtchange;
593 	}
594 	errno = inet6ctlerrmap[cmd];
595 
596 	for (inp = head->inpt_queue.cqh_first;
597 	     inp != (struct inpcb *)&head->inpt_queue; inp = ninp) {
598 		ninp = inp->inp_queue.cqe_next;
599 
600 		if ((inp->inp_flags & INP_IPV6) == 0)
601 			continue;
602 
603 		/*
604 		 * Under the following condition, notify of redirects
605 		 * to the pcb, without making address matches against inpcb.
606 		 * - redirect notification is arrived.
607 		 * - the inpcb is unconnected.
608 		 * - the inpcb is caching !RTF_HOST routing entry.
609 		 * - the ICMPv6 notification is from the gateway cached in the
610 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
611 		 *   the inpcb used very recently.
612 		 *
613 		 * This is to improve interaction between netbsd/openbsd
614 		 * redirect handling code, and inpcb route cache code.
615 		 * without the clause, !RTF_HOST routing entry (which carries
616 		 * gateway used by inpcb right before the ICMPv6 redirect)
617 		 * will be cached forever in unconnected inpcb.
618 		 *
619 		 * There still is a question regarding to what is TRT:
620 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
621 		 *   generated on packet output.  inpcb will always cache
622 		 *   RTF_HOST routing entry so there's no need for the clause
623 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
624 		 *   and inpcb is caching it already).
625 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
626 		 *   due to the cloned routing entries.
627 		 * - Specwise, "destination cache" is mentioned in RFC2461.
628 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
629 		 *   is not really convinced.
630 		 * - Having hiwat/lowat on # of cloned host route (redirect/
631 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
632 		 *   icmp6_mtudisc_update().
633 		 */
634 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
635 		    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) &&
636 		    inp->inp_route.ro_rt &&
637 		    !(inp->inp_route.ro_rt->rt_flags & RTF_HOST)) {
638 			struct sockaddr_in6 *dst6;
639 
640 			dst6 = (struct sockaddr_in6 *)&inp->inp_route.ro_dst;
641 			if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
642 			    &sa6_dst->sin6_addr))
643 				goto do_notify;
644 		}
645 
646 		/*
647 		 * Detect if we should notify the error. If no source and
648 		 * destination ports are specified, but non-zero flowinfo and
649 		 * local address match, notify the error. This is the case
650 		 * when the error is delivered with an encrypted buffer
651 		 * by ESP. Otherwise, just compare addresses and ports
652 		 * as usual.
653 		 */
654 		if (lport == 0 && fport == 0 && flowinfo &&
655 		    inp->inp_socket != NULL &&
656 		    flowinfo == (inp->inp_flowinfo & IPV6_FLOWLABEL_MASK) &&
657 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, &sa6_src.sin6_addr))
658 			goto do_notify;
659 		else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6,
660 					     &sa6_dst->sin6_addr) ||
661 			 inp->inp_socket == 0 ||
662 			 (lport && inp->inp_lport != lport) ||
663 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
664 			  !IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6,
665 					      &sa6_src.sin6_addr)) ||
666 			 (fport && inp->inp_fport != fport)) {
667 			continue;
668 		}
669 	  do_notify:
670 		nmatch++;
671 		if (notify)
672 			(*notify)(inp, errno);
673 	}
674 	return 0;
675 }
676 
677 /*
678  * Get the local address/port, and put it in a sockaddr_in6.
679  * This services the getsockname(2) call.
680  */
681 int
682 in6_setsockaddr(inp, nam)
683 	struct inpcb *inp;
684 	struct mbuf *nam;
685 {
686 	struct sockaddr_in6 *sin6;
687 
688 	nam->m_len = sizeof(struct sockaddr_in6);
689 	sin6 = mtod(nam,struct sockaddr_in6 *);
690 
691 	bzero ((caddr_t)sin6,sizeof(struct sockaddr_in6));
692 	sin6->sin6_family = AF_INET6;
693 	sin6->sin6_len = sizeof(struct sockaddr_in6);
694 	sin6->sin6_port = inp->inp_lport;
695 	sin6->sin6_addr = inp->inp_laddr6;
696 	/* KAME hack: recover scopeid */
697 	(void)in6_recoverscope(sin6, &inp->inp_laddr6, NULL);
698 
699 	return 0;
700 }
701 
702 /*
703  * Get the foreign address/port, and put it in a sockaddr_in6.
704  * This services the getpeername(2) call.
705  */
706 int
707 in6_setpeeraddr(inp, nam)
708 	struct inpcb *inp;
709 	struct mbuf *nam;
710 {
711 	struct sockaddr_in6 *sin6;
712 
713 	nam->m_len = sizeof(struct sockaddr_in6);
714 	sin6 = mtod(nam,struct sockaddr_in6 *);
715 
716 	bzero ((caddr_t)sin6,sizeof(struct sockaddr_in6));
717 	sin6->sin6_family = AF_INET6;
718 	sin6->sin6_len = sizeof(struct sockaddr_in6);
719 	sin6->sin6_port = inp->inp_fport;
720 	sin6->sin6_addr = inp->inp_faddr6;
721 	/* KAME hack: recover scopeid */
722 	(void)in6_recoverscope(sin6, &inp->inp_faddr6, NULL);
723 
724 	return 0;
725 }
726