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: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $
30 */
31
32 /*-
33 * Copyright (c) 1982, 1986, 1991, 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 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
61 */
62
63 #include <sys/cdefs.h>
64 __FBSDID("$FreeBSD: releng/8.4/sys/netinet6/in6_pcb.c 220489 2011-04-09 12:35:08Z bz $");
65
66 #include "opt_inet.h"
67 #include "opt_inet6.h"
68 #include "opt_ipsec.h"
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/domain.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/sockio.h>
79 #include <sys/errno.h>
80 #include <sys/time.h>
81 #include <sys/priv.h>
82 #include <sys/proc.h>
83 #include <sys/jail.h>
84
85 #include <vm/uma.h>
86
87 #include <net/if.h>
88 #include <net/if_types.h>
89 #include <net/route.h>
90
91 #include <netinet/in.h>
92 #include <netinet/in_var.h>
93 #include <netinet/in_systm.h>
94 #include <netinet/tcp_var.h>
95 #include <netinet/ip6.h>
96 #include <netinet/ip_var.h>
97
98 #include <netinet6/ip6_var.h>
99 #include <netinet6/nd6.h>
100 #include <netinet/in_pcb.h>
101 #include <netinet6/in6_pcb.h>
102 #include <netinet6/scope6_var.h>
103
104 #include <security/mac/mac_framework.h>
105
106 struct in6_addr zeroin6_addr;
107
108 int
109 in6_pcbbind(register struct inpcb *inp, struct sockaddr *nam,
110 struct ucred *cred)
111 {
112 struct socket *so = inp->inp_socket;
113 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
114 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
115 u_short lport = 0;
116 int error, wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
117
118 INP_INFO_WLOCK_ASSERT(pcbinfo);
119 INP_WLOCK_ASSERT(inp);
120
121 if (TAILQ_EMPTY(&V_in6_ifaddrhead)) /* XXX broken! */
122 return (EADDRNOTAVAIL);
123 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
124 return (EINVAL);
125 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
126 wild = INPLOOKUP_WILDCARD;
127 if (nam == NULL) {
128 if ((error = prison_local_ip6(cred, &inp->in6p_laddr,
129 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
130 return (error);
131 } else {
132 sin6 = (struct sockaddr_in6 *)nam;
133 if (nam->sa_len != sizeof(*sin6))
134 return (EINVAL);
135 /*
136 * family check.
137 */
138 if (nam->sa_family != AF_INET6)
139 return (EAFNOSUPPORT);
140
141 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
142 return(error);
143
144 if ((error = prison_local_ip6(cred, &sin6->sin6_addr,
145 ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0))) != 0)
146 return (error);
147
148 lport = sin6->sin6_port;
149 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
150 /*
151 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
152 * allow compepte duplication of binding if
153 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
154 * and a multicast address is bound on both
155 * new and duplicated sockets.
156 */
157 if (so->so_options & SO_REUSEADDR)
158 reuseport = SO_REUSEADDR|SO_REUSEPORT;
159 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
160 struct ifaddr *ifa;
161
162 sin6->sin6_port = 0; /* yech... */
163 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin6)) ==
164 NULL &&
165 (inp->inp_flags & INP_BINDANY) == 0) {
166 return (EADDRNOTAVAIL);
167 }
168
169 /*
170 * XXX: bind to an anycast address might accidentally
171 * cause sending a packet with anycast source address.
172 * We should allow to bind to a deprecated address, since
173 * the application dares to use it.
174 */
175 if (ifa != NULL &&
176 ((struct in6_ifaddr *)ifa)->ia6_flags &
177 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
178 ifa_free(ifa);
179 return (EADDRNOTAVAIL);
180 }
181 if (ifa != NULL)
182 ifa_free(ifa);
183 }
184 if (lport) {
185 struct inpcb *t;
186
187 /* GROSS */
188 if (ntohs(lport) <= V_ipport_reservedhigh &&
189 ntohs(lport) >= V_ipport_reservedlow &&
190 priv_check_cred(cred, PRIV_NETINET_RESERVEDPORT,
191 0))
192 return (EACCES);
193 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
194 priv_check_cred(inp->inp_cred,
195 PRIV_NETINET_REUSEPORT, 0) != 0) {
196 t = in6_pcblookup_local(pcbinfo,
197 &sin6->sin6_addr, lport,
198 INPLOOKUP_WILDCARD, cred);
199 if (t &&
200 ((t->inp_flags & INP_TIMEWAIT) == 0) &&
201 (so->so_type != SOCK_STREAM ||
202 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
203 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205 (t->inp_socket->so_options & SO_REUSEPORT)
206 == 0) && (inp->inp_cred->cr_uid !=
207 t->inp_cred->cr_uid))
208 return (EADDRINUSE);
209 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct sockaddr_in sin;
212
213 in6_sin6_2_sin(&sin, sin6);
214 t = in_pcblookup_local(pcbinfo,
215 sin.sin_addr, lport,
216 INPLOOKUP_WILDCARD, cred);
217 if (t &&
218 ((t->inp_flags &
219 INP_TIMEWAIT) == 0) &&
220 (so->so_type != SOCK_STREAM ||
221 ntohl(t->inp_faddr.s_addr) ==
222 INADDR_ANY) &&
223 (inp->inp_cred->cr_uid !=
224 t->inp_cred->cr_uid))
225 return (EADDRINUSE);
226 }
227 }
228 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229 lport, wild, cred);
230 if (t && (reuseport & ((t->inp_flags & INP_TIMEWAIT) ?
231 intotw(t)->tw_so_options :
232 t->inp_socket->so_options)) == 0)
233 return (EADDRINUSE);
234 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
235 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
236 struct sockaddr_in sin;
237
238 in6_sin6_2_sin(&sin, sin6);
239 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
240 lport, wild, cred);
241 if (t && t->inp_flags & INP_TIMEWAIT) {
242 if ((reuseport &
243 intotw(t)->tw_so_options) == 0 &&
244 (ntohl(t->inp_laddr.s_addr) !=
245 INADDR_ANY || ((inp->inp_vflag &
246 INP_IPV6PROTO) ==
247 (t->inp_vflag & INP_IPV6PROTO))))
248 return (EADDRINUSE);
249 }
250 else if (t &&
251 (reuseport & t->inp_socket->so_options)
252 == 0 && (ntohl(t->inp_laddr.s_addr) !=
253 INADDR_ANY || INP_SOCKAF(so) ==
254 INP_SOCKAF(t->inp_socket)))
255 return (EADDRINUSE);
256 }
257 }
258 inp->in6p_laddr = sin6->sin6_addr;
259 }
260 if (lport == 0) {
261 if ((error = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0) {
262 /* Undo an address bind that may have occurred. */
263 inp->in6p_laddr = in6addr_any;
264 return (error);
265 }
266 } else {
267 inp->inp_lport = lport;
268 if (in_pcbinshash(inp) != 0) {
269 inp->in6p_laddr = in6addr_any;
270 inp->inp_lport = 0;
271 return (EAGAIN);
272 }
273 }
274 return (0);
275 }
276
277 /*
278 * Transform old in6_pcbconnect() into an inner subroutine for new
279 * in6_pcbconnect(): Do some validity-checking on the remote
280 * address (in mbuf 'nam') and then determine local host address
281 * (i.e., which interface) to use to access that remote host.
282 *
283 * This preserves definition of in6_pcbconnect(), while supporting a
284 * slightly different version for T/TCP. (This is more than
285 * a bit of a kludge, but cleaning up the internal interfaces would
286 * have forced minor changes in every protocol).
287 */
288 int
289 in6_pcbladdr(register struct inpcb *inp, struct sockaddr *nam,
290 struct in6_addr *plocal_addr6)
291 {
292 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
293 int error = 0;
294 struct ifnet *ifp = NULL;
295 int scope_ambiguous = 0;
296 struct in6_addr in6a;
297
298 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
299 INP_WLOCK_ASSERT(inp);
300
301 if (nam->sa_len != sizeof (*sin6))
302 return (EINVAL);
303 if (sin6->sin6_family != AF_INET6)
304 return (EAFNOSUPPORT);
305 if (sin6->sin6_port == 0)
306 return (EADDRNOTAVAIL);
307
308 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
309 scope_ambiguous = 1;
310 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
311 return(error);
312
313 if (!TAILQ_EMPTY(&V_in6_ifaddrhead)) {
314 /*
315 * If the destination address is UNSPECIFIED addr,
316 * use the loopback addr, e.g ::1.
317 */
318 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
319 sin6->sin6_addr = in6addr_loopback;
320 }
321 if ((error = prison_remote_ip6(inp->inp_cred, &sin6->sin6_addr)) != 0)
322 return (error);
323
324 error = in6_selectsrc(sin6, inp->in6p_outputopts,
325 inp, NULL, inp->inp_cred, &ifp, &in6a);
326 if (error)
327 return (error);
328
329 if (ifp && scope_ambiguous &&
330 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
331 return(error);
332 }
333
334 /*
335 * Do not update this earlier, in case we return with an error.
336 *
337 * XXX: this in6_selectsrc result might replace the bound local
338 * address with the address specified by setsockopt(IPV6_PKTINFO).
339 * Is it the intended behavior?
340 */
341 *plocal_addr6 = in6a;
342
343 /*
344 * Don't do pcblookup call here; return interface in
345 * plocal_addr6
346 * and exit to caller, that will do the lookup.
347 */
348
349 return (0);
350 }
351
352 /*
353 * Outer subroutine:
354 * Connect from a socket to a specified address.
355 * Both address and port must be specified in argument sin.
356 * If don't have a local address for this socket yet,
357 * then pick one.
358 */
359 int
360 in6_pcbconnect(register struct inpcb *inp, struct sockaddr *nam,
361 struct ucred *cred)
362 {
363 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
364 struct in6_addr addr6;
365 int error;
366
367 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
368 INP_WLOCK_ASSERT(inp);
369
370 /*
371 * Call inner routine, to assign local interface address.
372 * in6_pcbladdr() may automatically fill in sin6_scope_id.
373 */
374 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
375 return (error);
376
377 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
378 sin6->sin6_port,
379 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
380 ? &addr6 : &inp->in6p_laddr,
381 inp->inp_lport, 0, NULL) != NULL) {
382 return (EADDRINUSE);
383 }
384 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
385 if (inp->inp_lport == 0) {
386 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
387 if (error)
388 return (error);
389 }
390 inp->in6p_laddr = addr6;
391 }
392 inp->in6p_faddr = sin6->sin6_addr;
393 inp->inp_fport = sin6->sin6_port;
394 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
395 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
396 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
397 inp->inp_flow |=
398 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
399
400 in_pcbrehash(inp);
401
402 return (0);
403 }
404
405 void
406 in6_pcbdisconnect(struct inpcb *inp)
407 {
408
409 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
410 INP_WLOCK_ASSERT(inp);
411
412 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
413 inp->inp_fport = 0;
414 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
415 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
416 in_pcbrehash(inp);
417 }
418
419 struct sockaddr *
420 in6_sockaddr(in_port_t port, struct in6_addr *addr_p)
421 {
422 struct sockaddr_in6 *sin6;
423
424 sin6 = malloc(sizeof *sin6, M_SONAME, M_WAITOK);
425 bzero(sin6, sizeof *sin6);
426 sin6->sin6_family = AF_INET6;
427 sin6->sin6_len = sizeof(*sin6);
428 sin6->sin6_port = port;
429 sin6->sin6_addr = *addr_p;
430 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
431
432 return (struct sockaddr *)sin6;
433 }
434
435 struct sockaddr *
436 in6_v4mapsin6_sockaddr(in_port_t port, struct in_addr *addr_p)
437 {
438 struct sockaddr_in sin;
439 struct sockaddr_in6 *sin6_p;
440
441 bzero(&sin, sizeof sin);
442 sin.sin_family = AF_INET;
443 sin.sin_len = sizeof(sin);
444 sin.sin_port = port;
445 sin.sin_addr = *addr_p;
446
447 sin6_p = malloc(sizeof *sin6_p, M_SONAME,
448 M_WAITOK);
449 in6_sin_2_v4mapsin6(&sin, sin6_p);
450
451 return (struct sockaddr *)sin6_p;
452 }
453
454 int
455 in6_getsockaddr(struct socket *so, struct sockaddr **nam)
456 {
457 register struct inpcb *inp;
458 struct in6_addr addr;
459 in_port_t port;
460
461 inp = sotoinpcb(so);
462 KASSERT(inp != NULL, ("in6_getsockaddr: inp == NULL"));
463
464 INP_RLOCK(inp);
465 port = inp->inp_lport;
466 addr = inp->in6p_laddr;
467 INP_RUNLOCK(inp);
468
469 *nam = in6_sockaddr(port, &addr);
470 return 0;
471 }
472
473 int
474 in6_getpeeraddr(struct socket *so, struct sockaddr **nam)
475 {
476 struct inpcb *inp;
477 struct in6_addr addr;
478 in_port_t port;
479
480 inp = sotoinpcb(so);
481 KASSERT(inp != NULL, ("in6_getpeeraddr: inp == NULL"));
482
483 INP_RLOCK(inp);
484 port = inp->inp_fport;
485 addr = inp->in6p_faddr;
486 INP_RUNLOCK(inp);
487
488 *nam = in6_sockaddr(port, &addr);
489 return 0;
490 }
491
492 int
493 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
494 {
495 struct inpcb *inp;
496 int error;
497
498 inp = sotoinpcb(so);
499 KASSERT(inp != NULL, ("in6_mapped_sockaddr: inp == NULL"));
500
501 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
502 error = in_getsockaddr(so, nam);
503 if (error == 0)
504 in6_sin_2_v4mapsin6_in_sock(nam);
505 } else {
506 /* scope issues will be handled in in6_getsockaddr(). */
507 error = in6_getsockaddr(so, nam);
508 }
509
510 return error;
511 }
512
513 int
514 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
515 {
516 struct inpcb *inp;
517 int error;
518
519 inp = sotoinpcb(so);
520 KASSERT(inp != NULL, ("in6_mapped_peeraddr: inp == NULL"));
521
522 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
523 error = in_getpeeraddr(so, nam);
524 if (error == 0)
525 in6_sin_2_v4mapsin6_in_sock(nam);
526 } else
527 /* scope issues will be handled in in6_getpeeraddr(). */
528 error = in6_getpeeraddr(so, nam);
529
530 return error;
531 }
532
533 /*
534 * Pass some notification to all connections of a protocol
535 * associated with address dst. The local address and/or port numbers
536 * may be specified to limit the search. The "usual action" will be
537 * taken, depending on the ctlinput cmd. The caller must filter any
538 * cmds that are uninteresting (e.g., no error in the map).
539 * Call the protocol specific routine (if any) to report
540 * any errors for each matching socket.
541 */
542 void
543 in6_pcbnotify(struct inpcbinfo *pcbinfo, struct sockaddr *dst,
544 u_int fport_arg, const struct sockaddr *src, u_int lport_arg,
545 int cmd, void *cmdarg,
546 struct inpcb *(*notify)(struct inpcb *, int))
547 {
548 struct inpcb *inp, *inp_temp;
549 struct sockaddr_in6 sa6_src, *sa6_dst;
550 u_short fport = fport_arg, lport = lport_arg;
551 u_int32_t flowinfo;
552 int errno;
553
554 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
555 return;
556
557 sa6_dst = (struct sockaddr_in6 *)dst;
558 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
559 return;
560
561 /*
562 * note that src can be NULL when we get notify by local fragmentation.
563 */
564 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
565 flowinfo = sa6_src.sin6_flowinfo;
566
567 /*
568 * Redirects go to all references to the destination,
569 * and use in6_rtchange to invalidate the route cache.
570 * Dead host indications: also use in6_rtchange to invalidate
571 * the cache, and deliver the error to all the sockets.
572 * Otherwise, if we have knowledge of the local port and address,
573 * deliver only to that socket.
574 */
575 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
576 fport = 0;
577 lport = 0;
578 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
579
580 if (cmd != PRC_HOSTDEAD)
581 notify = in6_rtchange;
582 }
583 errno = inet6ctlerrmap[cmd];
584 INP_INFO_WLOCK(pcbinfo);
585 LIST_FOREACH_SAFE(inp, pcbinfo->ipi_listhead, inp_list, inp_temp) {
586 INP_WLOCK(inp);
587 if ((inp->inp_vflag & INP_IPV6) == 0) {
588 INP_WUNLOCK(inp);
589 continue;
590 }
591
592 /*
593 * If the error designates a new path MTU for a destination
594 * and the application (associated with this socket) wanted to
595 * know the value, notify. Note that we notify for all
596 * disconnected sockets if the corresponding application
597 * wanted. This is because some UDP applications keep sending
598 * sockets disconnected.
599 * XXX: should we avoid to notify the value to TCP sockets?
600 */
601 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
602 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
603 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
604 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
605 (u_int32_t *)cmdarg);
606 }
607
608 /*
609 * Detect if we should notify the error. If no source and
610 * destination ports are specifed, but non-zero flowinfo and
611 * local address match, notify the error. This is the case
612 * when the error is delivered with an encrypted buffer
613 * by ESP. Otherwise, just compare addresses and ports
614 * as usual.
615 */
616 if (lport == 0 && fport == 0 && flowinfo &&
617 inp->inp_socket != NULL &&
618 flowinfo == (inp->inp_flow & IPV6_FLOWLABEL_MASK) &&
619 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
620 goto do_notify;
621 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
622 &sa6_dst->sin6_addr) ||
623 inp->inp_socket == 0 ||
624 (lport && inp->inp_lport != lport) ||
625 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
626 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
627 &sa6_src.sin6_addr)) ||
628 (fport && inp->inp_fport != fport)) {
629 INP_WUNLOCK(inp);
630 continue;
631 }
632
633 do_notify:
634 if (notify) {
635 if ((*notify)(inp, errno))
636 INP_WUNLOCK(inp);
637 } else
638 INP_WUNLOCK(inp);
639 }
640 INP_INFO_WUNLOCK(pcbinfo);
641 }
642
643 /*
644 * Lookup a PCB based on the local address and port.
645 */
646 struct inpcb *
647 in6_pcblookup_local(struct inpcbinfo *pcbinfo, struct in6_addr *laddr,
648 u_short lport, int wild_okay, struct ucred *cred)
649 {
650 register struct inpcb *inp;
651 int matchwild = 3, wildcard;
652
653 INP_INFO_WLOCK_ASSERT(pcbinfo);
654
655 if (!wild_okay) {
656 struct inpcbhead *head;
657 /*
658 * Look for an unconnected (wildcard foreign addr) PCB that
659 * matches the local address and port we're looking for.
660 */
661 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
662 0, pcbinfo->ipi_hashmask)];
663 LIST_FOREACH(inp, head, inp_hash) {
664 /* XXX inp locking */
665 if ((inp->inp_vflag & INP_IPV6) == 0)
666 continue;
667 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
668 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
669 inp->inp_lport == lport) {
670 /* Found. */
671 if (cred == NULL ||
672 prison_equal_ip6(cred->cr_prison,
673 inp->inp_cred->cr_prison))
674 return (inp);
675 }
676 }
677 /*
678 * Not found.
679 */
680 return (NULL);
681 } else {
682 struct inpcbporthead *porthash;
683 struct inpcbport *phd;
684 struct inpcb *match = NULL;
685 /*
686 * Best fit PCB lookup.
687 *
688 * First see if this local port is in use by looking on the
689 * port hash list.
690 */
691 porthash = &pcbinfo->ipi_porthashbase[INP_PCBPORTHASH(lport,
692 pcbinfo->ipi_porthashmask)];
693 LIST_FOREACH(phd, porthash, phd_hash) {
694 if (phd->phd_port == lport)
695 break;
696 }
697 if (phd != NULL) {
698 /*
699 * Port is in use by one or more PCBs. Look for best
700 * fit.
701 */
702 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
703 wildcard = 0;
704 if (cred != NULL &&
705 !prison_equal_ip6(cred->cr_prison,
706 inp->inp_cred->cr_prison))
707 continue;
708 /* XXX inp locking */
709 if ((inp->inp_vflag & INP_IPV6) == 0)
710 continue;
711 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
712 wildcard++;
713 if (!IN6_IS_ADDR_UNSPECIFIED(
714 &inp->in6p_laddr)) {
715 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
716 wildcard++;
717 else if (!IN6_ARE_ADDR_EQUAL(
718 &inp->in6p_laddr, laddr))
719 continue;
720 } else {
721 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
722 wildcard++;
723 }
724 if (wildcard < matchwild) {
725 match = inp;
726 matchwild = wildcard;
727 if (matchwild == 0)
728 break;
729 }
730 }
731 }
732 return (match);
733 }
734 }
735
736 void
737 in6_pcbpurgeif0(struct inpcbinfo *pcbinfo, struct ifnet *ifp)
738 {
739 struct inpcb *in6p;
740 struct ip6_moptions *im6o;
741 int i, gap;
742
743 INP_INFO_RLOCK(pcbinfo);
744 LIST_FOREACH(in6p, pcbinfo->ipi_listhead, inp_list) {
745 INP_WLOCK(in6p);
746 im6o = in6p->in6p_moptions;
747 if ((in6p->inp_vflag & INP_IPV6) && im6o != NULL) {
748 /*
749 * Unselect the outgoing ifp for multicast if it
750 * is being detached.
751 */
752 if (im6o->im6o_multicast_ifp == ifp)
753 im6o->im6o_multicast_ifp = NULL;
754 /*
755 * Drop multicast group membership if we joined
756 * through the interface being detached.
757 */
758 gap = 0;
759 for (i = 0; i < im6o->im6o_num_memberships; i++) {
760 if (im6o->im6o_membership[i]->in6m_ifp ==
761 ifp) {
762 in6_mc_leave(im6o->im6o_membership[i],
763 NULL);
764 gap++;
765 } else if (gap != 0) {
766 im6o->im6o_membership[i - gap] =
767 im6o->im6o_membership[i];
768 }
769 }
770 im6o->im6o_num_memberships -= gap;
771 }
772 INP_WUNLOCK(in6p);
773 }
774 INP_INFO_RUNLOCK(pcbinfo);
775 }
776
777 /*
778 * Check for alternatives when higher level complains
779 * about service problems. For now, invalidate cached
780 * routing information. If the route was created dynamically
781 * (by a redirect), time to try a default gateway again.
782 */
783 void
784 in6_losing(struct inpcb *in6p)
785 {
786
787 /*
788 * We don't store route pointers in the routing table anymore
789 */
790 return;
791 }
792
793 /*
794 * After a routing change, flush old routing
795 * and allocate a (hopefully) better one.
796 */
797 struct inpcb *
798 in6_rtchange(struct inpcb *inp, int errno)
799 {
800 /*
801 * We don't store route pointers in the routing table anymore
802 */
803 return inp;
804 }
805
806 /*
807 * Lookup PCB in hash list.
808 */
809 struct inpcb *
810 in6_pcblookup_hash(struct inpcbinfo *pcbinfo, struct in6_addr *faddr,
811 u_int fport_arg, struct in6_addr *laddr, u_int lport_arg, int wildcard,
812 struct ifnet *ifp)
813 {
814 struct inpcbhead *head;
815 struct inpcb *inp, *tmpinp;
816 u_short fport = fport_arg, lport = lport_arg;
817 int faith;
818
819 INP_INFO_LOCK_ASSERT(pcbinfo);
820
821 if (faithprefix_p != NULL)
822 faith = (*faithprefix_p)(laddr);
823 else
824 faith = 0;
825
826 /*
827 * First look for an exact match.
828 */
829 tmpinp = NULL;
830 head = &pcbinfo->ipi_hashbase[
831 INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, lport, fport,
832 pcbinfo->ipi_hashmask)];
833 LIST_FOREACH(inp, head, inp_hash) {
834 /* XXX inp locking */
835 if ((inp->inp_vflag & INP_IPV6) == 0)
836 continue;
837 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
838 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
839 inp->inp_fport == fport &&
840 inp->inp_lport == lport) {
841 /*
842 * XXX We should be able to directly return
843 * the inp here, without any checks.
844 * Well unless both bound with SO_REUSEPORT?
845 */
846 if (prison_flag(inp->inp_cred, PR_IP6))
847 return (inp);
848 if (tmpinp == NULL)
849 tmpinp = inp;
850 }
851 }
852 if (tmpinp != NULL)
853 return (tmpinp);
854
855 /*
856 * Then look for a wildcard match, if requested.
857 */
858 if (wildcard == INPLOOKUP_WILDCARD) {
859 struct inpcb *local_wild = NULL, *local_exact = NULL;
860 struct inpcb *jail_wild = NULL;
861 int injail;
862
863 /*
864 * Order of socket selection - we always prefer jails.
865 * 1. jailed, non-wild.
866 * 2. jailed, wild.
867 * 3. non-jailed, non-wild.
868 * 4. non-jailed, wild.
869 */
870 head = &pcbinfo->ipi_hashbase[INP_PCBHASH(INADDR_ANY, lport,
871 0, pcbinfo->ipi_hashmask)];
872 LIST_FOREACH(inp, head, inp_hash) {
873 /* XXX inp locking */
874 if ((inp->inp_vflag & INP_IPV6) == 0)
875 continue;
876
877 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
878 inp->inp_lport != lport) {
879 continue;
880 }
881
882 /* XXX inp locking */
883 if (faith && (inp->inp_flags & INP_FAITH) == 0)
884 continue;
885
886 injail = prison_flag(inp->inp_cred, PR_IP6);
887 if (injail) {
888 if (prison_check_ip6(inp->inp_cred,
889 laddr) != 0)
890 continue;
891 } else {
892 if (local_exact != NULL)
893 continue;
894 }
895
896 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr)) {
897 if (injail)
898 return (inp);
899 else
900 local_exact = inp;
901 } else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
902 if (injail)
903 jail_wild = inp;
904 else
905 local_wild = inp;
906 }
907 } /* LIST_FOREACH */
908
909 if (jail_wild != NULL)
910 return (jail_wild);
911 if (local_exact != NULL)
912 return (local_exact);
913 if (local_wild != NULL)
914 return (local_wild);
915 } /* if (wildcard == INPLOOKUP_WILDCARD) */
916
917 /*
918 * Not found.
919 */
920 return (NULL);
921 }
922
923 void
924 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
925 {
926 struct ip6_hdr *ip;
927
928 ip = mtod(m, struct ip6_hdr *);
929 bzero(sin6, sizeof(*sin6));
930 sin6->sin6_len = sizeof(*sin6);
931 sin6->sin6_family = AF_INET6;
932 sin6->sin6_addr = ip->ip6_src;
933
934 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
935
936 return;
937 }
Cache object: a8ebbfd57cbba4759eb6ec9e28b279cc
|