xref: /openbsd-src/sys/netinet6/in6_pcb.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: in6_pcb.c,v 1.96 2016/08/04 20:46:24 vgross 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/mbuf.h>
105 #include <sys/domain.h>
106 #include <sys/protosw.h>
107 #include <sys/socket.h>
108 #include <sys/socketvar.h>
109 #include <sys/errno.h>
110 #include <sys/time.h>
111 #include <sys/proc.h>
112 #include <sys/pledge.h>
113 
114 #include <net/if.h>
115 #include <net/if_var.h>
116 #include <net/route.h>
117 
118 #include <netinet/in.h>
119 #include <netinet/ip.h>
120 #include <netinet/in_pcb.h>
121 #include <netinet/ip_var.h>
122 
123 #include <netinet6/in6_var.h>
124 #include <netinet/ip6.h>
125 #include <netinet6/ip6_var.h>
126 
127 /*
128  * External globals
129  */
130 
131 /*
132  * Globals
133  */
134 
135 struct in6_addr zeroin6_addr;
136 
137 /*
138  * Keep separate inet6ctlerrmap, because I may remap some of these.
139  * I also put it here, because, quite frankly, it belongs here, not in
140  * ip{v6,}_input().
141  */
142 #if 0
143 u_char inet6ctlerrmap[PRC_NCMDS] = {
144 	0,		0,		0,		0,
145 	0,		EMSGSIZE,	EHOSTDOWN,	EHOSTUNREACH,
146 	EHOSTUNREACH,	EHOSTUNREACH,	ECONNREFUSED,	ECONNREFUSED,
147 	EMSGSIZE,	EHOSTUNREACH,	0,		0,
148 	0,		0,		0,		0,
149 	ENOPROTOOPT
150 };
151 #endif
152 
153 int
154 in6_pcbaddrisavail(struct inpcb *inp, struct sockaddr_in6 *sin6, int wild,
155     struct proc *p)
156 {
157 	struct socket *so = inp->inp_socket;
158 	struct inpcbtable *table = inp->inp_table;
159 	u_short lport = sin6->sin6_port;
160 	int reuseport = (so->so_options & SO_REUSEPORT);
161 
162 	wild |= INPLOOKUP_IPV6;
163 	/* KAME hack: embed scopeid */
164 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp) != 0)
165 		return (EINVAL);
166 	/* this must be cleared for ifa_ifwithaddr() */
167 	sin6->sin6_scope_id = 0;
168 	/* reject IPv4 mapped address, we have no support for it */
169 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
170 		return (EADDRNOTAVAIL);
171 
172 	if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
173 		/*
174 		 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
175 		 * allow complete duplication of binding if
176 		 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
177 		 * and a multicast address is bound on both
178 		 * new and duplicated sockets.
179 		 */
180 		if (so->so_options & (SO_REUSEADDR|SO_REUSEPORT))
181 			reuseport = SO_REUSEADDR | SO_REUSEPORT;
182 	} else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
183 		struct ifaddr *ifa = NULL;
184 
185 		sin6->sin6_port = 0;  /*
186 				       * Yechhhh, because of upcoming
187 				       * call to ifa_ifwithaddr(), which
188 				       * does bcmp's over the PORTS as
189 				       * well.  (What about flow?)
190 				       */
191 		sin6->sin6_flowinfo = 0;
192 		if (!(so->so_options & SO_BINDANY) &&
193 		    (ifa = ifa_ifwithaddr(sin6tosa(sin6),
194 		    inp->inp_rtableid)) == NULL)
195 			return (EADDRNOTAVAIL);
196 		sin6->sin6_port = lport;
197 
198 		/*
199 		 * bind to an anycast address might accidentally
200 		 * cause sending a packet with an anycast source
201 		 * address, so we forbid it.
202 		 *
203 		 * We should allow to bind to a deprecated address,
204 		 * since the application dare to use it.
205 		 * But, can we assume that they are careful enough
206 		 * to check if the address is deprecated or not?
207 		 * Maybe, as a safeguard, we should have a setsockopt
208 		 * flag to control the bind(2) behavior against
209 		 * deprecated addresses (default: forbid bind(2)).
210 		 */
211 		if (ifa && ifatoia6(ifa)->ia6_flags & (IN6_IFF_ANYCAST|
212 		    IN6_IFF_TENTATIVE|IN6_IFF_DUPLICATED|IN6_IFF_DETACHED))
213 			return (EADDRNOTAVAIL);
214 	}
215 	if (lport) {
216 		struct inpcb *t;
217 
218 		if (so->so_euid) {
219 			t = in_pcblookup_local(table,
220 			    (struct in_addr *)&sin6->sin6_addr, lport,
221 			    INPLOOKUP_WILDCARD | INPLOOKUP_IPV6,
222 			    inp->inp_rtableid);
223 			if (t && (so->so_euid != t->inp_socket->so_euid))
224 				return (EADDRINUSE);
225 		}
226 		t = in_pcblookup_local(table,
227 		    (struct in_addr *)&sin6->sin6_addr, lport,
228 		    wild, inp->inp_rtableid);
229 		if (t && (reuseport & t->inp_socket->so_options) == 0)
230 			return (EADDRINUSE);
231 	}
232 	return (0);
233 }
234 
235 /*
236  * Connect from a socket to a specified address.
237  * Both address and port must be specified in argument sin6.
238  * Eventually, flow labels will have to be dealt with here, as well.
239  *
240  * If don't have a local address for this socket yet,
241  * then pick one.
242  */
243 int
244 in6_pcbconnect(struct inpcb *inp, struct mbuf *nam)
245 {
246 	struct in6_addr *in6a = NULL;
247 	struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
248 	int error = 0;
249 	struct sockaddr_in6 tmp;
250 
251 	(void)&in6a;				/* XXX fool gcc */
252 
253 	if (nam->m_len != sizeof(*sin6))
254 		return (EINVAL);
255 	if (sin6->sin6_family != AF_INET6)
256 		return (EAFNOSUPPORT);
257 	if (sin6->sin6_port == 0)
258 		return (EADDRNOTAVAIL);
259 
260 	/* reject IPv4 mapped address, we have no support for it */
261 	if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
262 		return EADDRNOTAVAIL;
263 
264 	/* sanity check for mapped address case */
265 	if (IN6_IS_ADDR_V4MAPPED(&inp->inp_laddr6))
266 		return EINVAL;
267 
268 	/* protect *sin6 from overwrites */
269 	tmp = *sin6;
270 	sin6 = &tmp;
271 
272 	/* KAME hack: embed scopeid */
273 	if (in6_embedscope(&sin6->sin6_addr, sin6, inp) != 0)
274 		return EINVAL;
275 	/* this must be cleared for ifa_ifwithaddr() */
276 	sin6->sin6_scope_id = 0;
277 
278 	/* Source address selection. */
279 	/*
280 	 * XXX: in6_selectsrc might replace the bound local address
281 	 * with the address specified by setsockopt(IPV6_PKTINFO).
282 	 * Is it the intended behavior?
283 	 */
284 	error = in6_pcbselsrc(&in6a, sin6, inp, inp->inp_outputopts6);
285 	if (error)
286 		return (error);
287 
288 	inp->inp_ipv6.ip6_hlim = (u_int8_t)in6_selecthlim(inp);
289 
290 	if (in6_pcbhashlookup(inp->inp_table, &sin6->sin6_addr, sin6->sin6_port,
291 	    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) ? in6a : &inp->inp_laddr6,
292 	    inp->inp_lport, inp->inp_rtableid)) {
293 		return (EADDRINUSE);
294 	}
295 
296 	KASSERT(IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) || inp->inp_lport);
297 
298 	if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6)) {
299 		if (inp->inp_lport == 0 &&
300 		    in_pcbbind(inp, NULL, curproc) == EADDRNOTAVAIL)
301 			return (EADDRNOTAVAIL);
302 		inp->inp_laddr6 = *in6a;
303 	}
304 	inp->inp_faddr6 = sin6->sin6_addr;
305 	inp->inp_fport = sin6->sin6_port;
306 	inp->inp_flowinfo &= ~IPV6_FLOWLABEL_MASK;
307 	if (ip6_auto_flowlabel)
308 		inp->inp_flowinfo |=
309 		    (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
310 	in_pcbrehash(inp);
311 	return (0);
312 }
313 
314 /*
315  * Pass some notification to all connections of a protocol
316  * associated with address dst.  The local address and/or port numbers
317  * may be specified to limit the search.  The "usual action" will be
318  * taken, depending on the ctlinput cmd.  The caller must filter any
319  * cmds that are uninteresting (e.g., no error in the map).
320  * Call the protocol specific routine (if any) to report
321  * any errors for each matching socket.
322  *
323  * Also perform input-side security policy check
324  *    once PCB to be notified has been located.
325  */
326 int
327 in6_pcbnotify(struct inpcbtable *head, struct sockaddr_in6 *dst,
328     uint fport_arg, const struct sockaddr_in6 *src, uint lport_arg,
329     u_int rdomain, int cmd, void *cmdarg, void (*notify)(struct inpcb *, int))
330 {
331 	struct inpcb *inp, *ninp;
332 	u_short fport = fport_arg, lport = lport_arg;
333 	struct sockaddr_in6 sa6_src;
334 	int errno, nmatch = 0;
335 	u_int32_t flowinfo;
336 
337 	if ((unsigned)cmd >= PRC_NCMDS)
338 		return (0);
339 
340 	if (IN6_IS_ADDR_UNSPECIFIED(&dst->sin6_addr))
341 		return (0);
342 	if (IN6_IS_ADDR_V4MAPPED(&dst->sin6_addr)) {
343 #ifdef DIAGNOSTIC
344 		printf("Huh?  Thought in6_pcbnotify() never got "
345 		       "called with mapped!\n");
346 #endif
347 		return (0);
348 	}
349 
350 	rdomain = rtable_l2(rdomain);
351 	/*
352 	 * note that src can be NULL when we get notify by local fragmentation.
353 	 */
354 	sa6_src = (src == NULL) ? sa6_any : *src;
355 	flowinfo = sa6_src.sin6_flowinfo;
356 
357 	/*
358 	 * Redirects go to all references to the destination,
359 	 * and use in_rtchange to invalidate the route cache.
360 	 * Dead host indications: also use in_rtchange to invalidate
361 	 * the cache, and deliver the error to all the sockets.
362 	 * Otherwise, if we have knowledge of the local port and address,
363 	 * deliver only to that socket.
364 	 */
365 	if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
366 		fport = 0;
367 		lport = 0;
368 		sa6_src.sin6_addr = in6addr_any;
369 
370 		if (cmd != PRC_HOSTDEAD)
371 			notify = in_rtchange;
372 	}
373 	errno = inet6ctlerrmap[cmd];
374 
375 	TAILQ_FOREACH_SAFE(inp, &head->inpt_queue, inp_queue, ninp) {
376 		if ((inp->inp_flags & INP_IPV6) == 0)
377 			continue;
378 
379 		/*
380 		 * Under the following condition, notify of redirects
381 		 * to the pcb, without making address matches against inpcb.
382 		 * - redirect notification is arrived.
383 		 * - the inpcb is unconnected.
384 		 * - the inpcb is caching !RTF_HOST routing entry.
385 		 * - the ICMPv6 notification is from the gateway cached in the
386 		 *   inpcb.  i.e. ICMPv6 notification is from nexthop gateway
387 		 *   the inpcb used very recently.
388 		 *
389 		 * This is to improve interaction between netbsd/openbsd
390 		 * redirect handling code, and inpcb route cache code.
391 		 * without the clause, !RTF_HOST routing entry (which carries
392 		 * gateway used by inpcb right before the ICMPv6 redirect)
393 		 * will be cached forever in unconnected inpcb.
394 		 *
395 		 * There still is a question regarding to what is TRT:
396 		 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
397 		 *   generated on packet output.  inpcb will always cache
398 		 *   RTF_HOST routing entry so there's no need for the clause
399 		 *   (ICMPv6 redirect will update RTF_HOST routing entry,
400 		 *   and inpcb is caching it already).
401 		 *   However, bsdi/freebsd are vulnerable to local DoS attacks
402 		 *   due to the cloned routing entries.
403 		 * - Specwise, "destination cache" is mentioned in RFC2461.
404 		 *   Jinmei says that it implies bsdi/freebsd behavior, itojun
405 		 *   is not really convinced.
406 		 * - Having hiwat/lowat on # of cloned host route (redirect/
407 		 *   pmtud) may be a good idea.  netbsd/openbsd has it.  see
408 		 *   icmp6_mtudisc_update().
409 		 */
410 		if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
411 		    IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6) &&
412 		    inp->inp_route.ro_rt &&
413 		    !(inp->inp_route.ro_rt->rt_flags & RTF_HOST)) {
414 			struct sockaddr_in6 *dst6;
415 
416 			dst6 = satosin6(&inp->inp_route.ro_dst);
417 			if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
418 			    &dst->sin6_addr))
419 				goto do_notify;
420 		}
421 
422 		/*
423 		 * Detect if we should notify the error. If no source and
424 		 * destination ports are specified, but non-zero flowinfo and
425 		 * local address match, notify the error. This is the case
426 		 * when the error is delivered with an encrypted buffer
427 		 * by ESP. Otherwise, just compare addresses and ports
428 		 * as usual.
429 		 */
430 		if (lport == 0 && fport == 0 && flowinfo &&
431 		    inp->inp_socket != NULL &&
432 		    flowinfo == (inp->inp_flowinfo & IPV6_FLOWLABEL_MASK) &&
433 		    IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6, &sa6_src.sin6_addr))
434 			goto do_notify;
435 		else if (!IN6_ARE_ADDR_EQUAL(&inp->inp_faddr6,
436 					     &dst->sin6_addr) ||
437 			 rtable_l2(inp->inp_rtableid) != rdomain ||
438 			 inp->inp_socket == 0 ||
439 			 (lport && inp->inp_lport != lport) ||
440 			 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
441 			  !IN6_ARE_ADDR_EQUAL(&inp->inp_laddr6,
442 					      &sa6_src.sin6_addr)) ||
443 			 (fport && inp->inp_fport != fport)) {
444 			continue;
445 		}
446 	  do_notify:
447 		nmatch++;
448 		if (notify)
449 			(*notify)(inp, errno);
450 	}
451 	return (nmatch);
452 }
453 
454 /*
455  * Get the local address/port, and put it in a sockaddr_in6.
456  * This services the getsockname(2) call.
457  */
458 int
459 in6_setsockaddr(struct inpcb *inp, struct mbuf *nam)
460 {
461 	struct sockaddr_in6 *sin6;
462 
463 	nam->m_len = sizeof(struct sockaddr_in6);
464 	sin6 = mtod(nam,struct sockaddr_in6 *);
465 
466 	bzero ((caddr_t)sin6,sizeof(struct sockaddr_in6));
467 	sin6->sin6_family = AF_INET6;
468 	sin6->sin6_len = sizeof(struct sockaddr_in6);
469 	sin6->sin6_port = inp->inp_lport;
470 	sin6->sin6_addr = inp->inp_laddr6;
471 	/* KAME hack: recover scopeid */
472 	in6_recoverscope(sin6, &inp->inp_laddr6);
473 
474 	return 0;
475 }
476 
477 /*
478  * Get the foreign address/port, and put it in a sockaddr_in6.
479  * This services the getpeername(2) call.
480  */
481 int
482 in6_setpeeraddr(struct inpcb *inp, struct mbuf *nam)
483 {
484 	struct sockaddr_in6 *sin6;
485 
486 	nam->m_len = sizeof(struct sockaddr_in6);
487 	sin6 = mtod(nam,struct sockaddr_in6 *);
488 
489 	bzero ((caddr_t)sin6,sizeof(struct sockaddr_in6));
490 	sin6->sin6_family = AF_INET6;
491 	sin6->sin6_len = sizeof(struct sockaddr_in6);
492 	sin6->sin6_port = inp->inp_fport;
493 	sin6->sin6_addr = inp->inp_faddr6;
494 	/* KAME hack: recover scopeid */
495 	in6_recoverscope(sin6, &inp->inp_faddr6);
496 
497 	return 0;
498 }
499