]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/in6_pcb.c
xnu-517.tar.gz
[apple/xnu.git] / bsd / netinet6 / in6_pcb.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 */
30
31 /*
32 * Copyright (c) 1982, 1986, 1991, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/sockio.h>
75 #include <sys/errno.h>
76 #include <sys/time.h>
77 #include <sys/proc.h>
78
79 #include <net/if.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
82
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip6.h>
87 #include <netinet/ip_var.h>
88 #include <netinet6/ip6_var.h>
89 #include <netinet6/nd6.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet6/in6_pcb.h>
92 #include <net/if_types.h>
93
94 #include "faith.h"
95 #if defined(NFAITH) && NFAITH > 0
96 #include <net/if_faith.h>
97 #endif
98
99 #if IPSEC
100 #include <netinet6/ipsec.h>
101 #if INET6
102 #include <netinet6/ipsec6.h>
103 #endif
104 #include <netinet6/ah.h>
105 #if INET6
106 #include <netinet6/ah6.h>
107 #endif
108 #include <netkey/key.h>
109 #endif /* IPSEC */
110
111 struct in6_addr zeroin6_addr;
112
113 int
114 in6_pcbbind(inp, nam, p)
115 register struct inpcb *inp;
116 struct sockaddr *nam;
117 struct proc *p;
118 {
119 struct socket *so = inp->inp_socket;
120 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
121 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
122 u_short lport = 0;
123 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
124
125 if (!in6_ifaddr) /* XXX broken! */
126 return (EADDRNOTAVAIL);
127 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
128 return(EINVAL);
129 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
130 wild = 1;
131 if (nam) {
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 /* KAME hack: embed scopeid */
142 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
143 return EINVAL;
144 /* this must be cleared for ifa_ifwithaddr() */
145 sin6->sin6_scope_id = 0;
146
147 lport = sin6->sin6_port;
148 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
149 /*
150 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
151 * allow compepte duplication of binding if
152 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
153 * and a multicast address is bound on both
154 * new and duplicated sockets.
155 */
156 if (so->so_options & SO_REUSEADDR)
157 reuseport = SO_REUSEADDR|SO_REUSEPORT;
158 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
159 struct ifaddr *ia = NULL;
160
161 sin6->sin6_port = 0; /* yech... */
162 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
163 return(EADDRNOTAVAIL);
164
165 /*
166 * XXX: bind to an anycast address might accidentally
167 * cause sending a packet with anycast source address.
168 * We should allow to bind to a deprecated address, since
169 * the application dare to use it.
170 */
171 if (ia &&
172 ((struct in6_ifaddr *)ia)->ia6_flags &
173 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
174 return(EADDRNOTAVAIL);
175 }
176 }
177 if (lport) {
178 struct inpcb *t;
179
180 /* GROSS */
181 if (ntohs(lport) < IPV6PORT_RESERVED && p &&
182 #if 0
183 suser(p->p_ucred, &p->p_acflag))
184 #else
185 ((so->so_state & SS_PRIV) == 0))
186 #endif
187 return(EACCES);
188
189 if (so->so_uid &&
190 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
191 t = in6_pcblookup_local(pcbinfo,
192 &sin6->sin6_addr, lport,
193 INPLOOKUP_WILDCARD);
194 if (t &&
195 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
196 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
197 (t->inp_socket->so_options &
198 SO_REUSEPORT) == 0) &&
199 so->so_uid != t->inp_socket->so_uid)
200 return (EADDRINUSE);
201 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
202 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
203 struct sockaddr_in sin;
204
205 in6_sin6_2_sin(&sin, sin6);
206 t = in_pcblookup_local(pcbinfo,
207 sin.sin_addr, lport,
208 INPLOOKUP_WILDCARD);
209 if (t &&
210 (so->so_uid !=
211 t->inp_socket->so_uid) &&
212 (ntohl(t->inp_laddr.s_addr) !=
213 INADDR_ANY ||
214 INP_SOCKAF(so) ==
215 INP_SOCKAF(t->inp_socket)))
216 return (EADDRINUSE);
217 }
218 }
219 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
220 lport, wild);
221 if (t && (reuseport & t->inp_socket->so_options) == 0)
222 return(EADDRINUSE);
223 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
224 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
225 struct sockaddr_in sin;
226
227 in6_sin6_2_sin(&sin, sin6);
228 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
229 lport, wild);
230 if (t &&
231 (reuseport & t->inp_socket->so_options)
232 == 0 &&
233 (ntohl(t->inp_laddr.s_addr)
234 != INADDR_ANY ||
235 INP_SOCKAF(so) ==
236 INP_SOCKAF(t->inp_socket)))
237 return (EADDRINUSE);
238 }
239 }
240 inp->in6p_laddr = sin6->sin6_addr;
241 }
242 if (lport == 0) {
243 int e;
244 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, p)) != 0)
245 return(e);
246 }
247 else {
248 inp->inp_lport = lport;
249 if (in_pcbinshash(inp) != 0) {
250 inp->in6p_laddr = in6addr_any;
251 inp->inp_lport = 0;
252 return (EAGAIN);
253 }
254 }
255 return(0);
256 }
257
258 /*
259 * Transform old in6_pcbconnect() into an inner subroutine for new
260 * in6_pcbconnect(): Do some validity-checking on the remote
261 * address (in mbuf 'nam') and then determine local host address
262 * (i.e., which interface) to use to access that remote host.
263 *
264 * This preserves definition of in6_pcbconnect(), while supporting a
265 * slightly different version for T/TCP. (This is more than
266 * a bit of a kludge, but cleaning up the internal interfaces would
267 * have forced minor changes in every protocol).
268 */
269
270 int
271 in6_pcbladdr(inp, nam, plocal_addr6)
272 register struct inpcb *inp;
273 struct sockaddr *nam;
274 struct in6_addr **plocal_addr6;
275 {
276 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
277 struct ifnet *ifp = NULL;
278 int error = 0;
279
280 if (nam->sa_len != sizeof (*sin6))
281 return (EINVAL);
282 if (sin6->sin6_family != AF_INET6)
283 return (EAFNOSUPPORT);
284 if (sin6->sin6_port == 0)
285 return (EADDRNOTAVAIL);
286
287 /* KAME hack: embed scopeid */
288 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
289 return EINVAL;
290
291 if (in6_ifaddr) {
292 /*
293 * If the destination address is UNSPECIFIED addr,
294 * use the loopback addr, e.g ::1.
295 */
296 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
297 sin6->sin6_addr = in6addr_loopback;
298 }
299 {
300 /*
301 * XXX: in6_selectsrc might replace the bound local address
302 * with the address specified by setsockopt(IPV6_PKTINFO).
303 * Is it the intended behavior?
304 */
305 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
306 inp->in6p_moptions,
307 &inp->in6p_route,
308 &inp->in6p_laddr, &error);
309 if (*plocal_addr6 == 0) {
310 if (error == 0)
311 error = EADDRNOTAVAIL;
312 return(error);
313 }
314 /*
315 * Don't do pcblookup call here; return interface in
316 * plocal_addr6
317 * and exit to caller, that will do the lookup.
318 */
319 }
320
321 if (inp->in6p_route.ro_rt)
322 ifp = inp->in6p_route.ro_rt->rt_ifp;
323
324 return(0);
325 }
326
327 /*
328 * Outer subroutine:
329 * Connect from a socket to a specified address.
330 * Both address and port must be specified in argument sin.
331 * If don't have a local address for this socket yet,
332 * then pick one.
333 */
334 int
335 in6_pcbconnect(inp, nam, p)
336 register struct inpcb *inp;
337 struct sockaddr *nam;
338 struct proc *p;
339 {
340 struct in6_addr *addr6;
341 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
342 int error;
343
344 /*
345 * Call inner routine, to assign local interface address.
346 * in6_pcbladdr() may automatically fill in sin6_scope_id.
347 */
348 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
349 return(error);
350
351 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
352 sin6->sin6_port,
353 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
354 ? addr6 : &inp->in6p_laddr,
355 inp->inp_lport, 0, NULL) != NULL) {
356 return (EADDRINUSE);
357 }
358 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
359 if (inp->inp_lport == 0) {
360 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
361 if (error)
362 return (error);
363 }
364 inp->in6p_laddr = *addr6;
365 }
366 inp->in6p_faddr = sin6->sin6_addr;
367 inp->inp_fport = sin6->sin6_port;
368 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
369 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
370 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
371 inp->in6p_flowinfo |=
372 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
373
374 in_pcbrehash(inp);
375 return (0);
376 }
377
378 #if 0
379 /*
380 * Return an IPv6 address, which is the most appropriate for given
381 * destination and user specified options.
382 * If necessary, this function lookups the routing table and return
383 * an entry to the caller for later use.
384 */
385 struct in6_addr *
386 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
387 struct sockaddr_in6 *dstsock;
388 struct ip6_pktopts *opts;
389 struct ip6_moptions *mopts;
390 struct route_in6 *ro;
391 struct in6_addr *laddr;
392 int *errorp;
393 {
394 struct in6_addr *dst;
395 struct in6_ifaddr *ia6 = 0;
396 struct in6_pktinfo *pi = NULL;
397
398 dst = &dstsock->sin6_addr;
399 *errorp = 0;
400
401 /*
402 * If the source address is explicitly specified by the caller,
403 * use it.
404 */
405 if (opts && (pi = opts->ip6po_pktinfo) &&
406 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
407 return(&pi->ipi6_addr);
408
409 /*
410 * If the source address is not specified but the socket(if any)
411 * is already bound, use the bound address.
412 */
413 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
414 return(laddr);
415
416 /*
417 * If the caller doesn't specify the source address but
418 * the outgoing interface, use an address associated with
419 * the interface.
420 */
421 if (pi && pi->ipi6_ifindex) {
422 /* XXX boundary check is assumed to be already done. */
423 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
424 dst);
425 if (ia6 == 0) {
426 *errorp = EADDRNOTAVAIL;
427 return(0);
428 }
429 return(&satosin6(&ia6->ia_addr)->sin6_addr);
430 }
431
432 /*
433 * If the destination address is a link-local unicast address or
434 * a multicast address, and if the outgoing interface is specified
435 * by the sin6_scope_id filed, use an address associated with the
436 * interface.
437 * XXX: We're now trying to define more specific semantics of
438 * sin6_scope_id field, so this part will be rewritten in
439 * the near future.
440 */
441 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
442 dstsock->sin6_scope_id) {
443 /*
444 * I'm not sure if boundary check for scope_id is done
445 * somewhere...
446 */
447 if (dstsock->sin6_scope_id < 0 ||
448 if_index < dstsock->sin6_scope_id) {
449 *errorp = ENXIO; /* XXX: better error? */
450 return(0);
451 }
452 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
453 dst);
454 if (ia6 == 0) {
455 *errorp = EADDRNOTAVAIL;
456 return(0);
457 }
458 return(&satosin6(&ia6->ia_addr)->sin6_addr);
459 }
460
461 /*
462 * If the destination address is a multicast address and
463 * the outgoing interface for the address is specified
464 * by the caller, use an address associated with the interface.
465 * There is a sanity check here; if the destination has node-local
466 * scope, the outgoing interfacde should be a loopback address.
467 * Even if the outgoing interface is not specified, we also
468 * choose a loopback interface as the outgoing interface.
469 */
470 if (IN6_IS_ADDR_MULTICAST(dst)) {
471 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
472
473 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
474 ifp = &loif[0];
475 }
476
477 if (ifp) {
478 ia6 = in6_ifawithscope(ifp, dst);
479 if (ia6 == 0) {
480 *errorp = EADDRNOTAVAIL;
481 return(0);
482 }
483 return(&ia6->ia_addr.sin6_addr);
484 }
485 }
486
487 /*
488 * If the next hop address for the packet is specified
489 * by caller, use an address associated with the route
490 * to the next hop.
491 */
492 {
493 struct sockaddr_in6 *sin6_next;
494 struct rtentry *rt;
495
496 if (opts && opts->ip6po_nexthop) {
497 sin6_next = satosin6(opts->ip6po_nexthop);
498 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
499 if (rt) {
500 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
501 if (ia6 == 0)
502 ia6 = ifatoia6(rt->rt_ifa);
503 }
504 if (ia6 == 0) {
505 *errorp = EADDRNOTAVAIL;
506 return(0);
507 }
508 return(&satosin6(&ia6->ia_addr)->sin6_addr);
509 }
510 }
511
512 /*
513 * If route is known or can be allocated now,
514 * our src addr is taken from the i/f, else punt.
515 */
516 if (ro) {
517 if (ro->ro_rt &&
518 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
519 rtfree(ro->ro_rt);
520 ro->ro_rt = (struct rtentry *)0;
521 }
522 if (ro->ro_rt == (struct rtentry *)0 ||
523 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
524 struct sockaddr_in6 *dst6;
525
526 /* No route yet, so try to acquire one */
527 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
528 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
529 dst6->sin6_family = AF_INET6;
530 dst6->sin6_len = sizeof(struct sockaddr_in6);
531 dst6->sin6_addr = *dst;
532 if (IN6_IS_ADDR_MULTICAST(dst)) {
533 ro->ro_rt = rtalloc1(&((struct route *)ro)
534 ->ro_dst, 0, 0UL);
535 } else {
536 rtalloc((struct route *)ro);
537 }
538 }
539
540 /*
541 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
542 * the address. But we don't know why it does so.
543 * It is necessary to ensure the scope even for lo0
544 * so doesn't check out IFF_LOOPBACK.
545 */
546
547 if (ro->ro_rt) {
548 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
549 if (ia6 == 0) /* xxx scope error ?*/
550 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
551 }
552 if (ia6 == 0) {
553 *errorp = EHOSTUNREACH; /* no route */
554 return(0);
555 }
556 return(&satosin6(&ia6->ia_addr)->sin6_addr);
557 }
558
559 *errorp = EADDRNOTAVAIL;
560 return(0);
561 }
562
563 /*
564 * Default hop limit selection. The precedence is as follows:
565 * 1. Hoplimit valued specified via ioctl.
566 * 2. (If the outgoing interface is detected) the current
567 * hop limit of the interface specified by router advertisement.
568 * 3. The system default hoplimit.
569 */
570 int
571 in6_selecthlim(in6p, ifp)
572 struct in6pcb *in6p;
573 struct ifnet *ifp;
574 {
575 if (in6p && in6p->in6p_hops >= 0)
576 return(in6p->in6p_hops);
577 else if (ifp)
578 return(nd_ifinfo[ifp->if_index].chlim);
579 else
580 return(ip6_defhlim);
581 }
582 #endif
583
584 void
585 in6_pcbdisconnect(inp)
586 struct inpcb *inp;
587 {
588 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
589 inp->inp_fport = 0;
590 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
591 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
592 in_pcbrehash(inp);
593 if (inp->inp_socket->so_state & SS_NOFDREF)
594 in6_pcbdetach(inp);
595 }
596
597 void
598 in6_pcbdetach(inp)
599 struct inpcb *inp;
600 {
601 struct socket *so = inp->inp_socket;
602 struct inpcbinfo *ipi = inp->inp_pcbinfo;
603
604 #if IPSEC
605 if (inp->in6p_sp != NULL)
606 ipsec6_delete_pcbpolicy(inp);
607 #endif /* IPSEC */
608 inp->inp_gencnt = ++ipi->ipi_gencnt;
609 in_pcbremlists(inp);
610 sotoinpcb(so) = 0;
611 sofree(so);
612
613 if (inp->in6p_options)
614 m_freem(inp->in6p_options);
615 ip6_freepcbopts(inp->in6p_outputopts);
616 ip6_freemoptions(inp->in6p_moptions);
617 if (inp->in6p_route.ro_rt)
618 rtfree(inp->in6p_route.ro_rt);
619 /* Check and free IPv4 related resources in case of mapped addr */
620 if (inp->inp_options)
621 (void)m_free(inp->inp_options);
622 ip_freemoptions(inp->inp_moptions);
623
624 inp->inp_vflag = 0;
625 zfree(ipi->ipi_zone, inp);
626 }
627
628 struct sockaddr *
629 in6_sockaddr(port, addr_p)
630 in_port_t port;
631 struct in6_addr *addr_p;
632 {
633 struct sockaddr_in6 *sin6;
634
635 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
636 bzero(sin6, sizeof *sin6);
637 sin6->sin6_family = AF_INET6;
638 sin6->sin6_len = sizeof(*sin6);
639 sin6->sin6_port = port;
640 sin6->sin6_addr = *addr_p;
641 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
642 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
643 else
644 sin6->sin6_scope_id = 0; /*XXX*/
645 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
646 sin6->sin6_addr.s6_addr16[1] = 0;
647
648 return (struct sockaddr *)sin6;
649 }
650
651 struct sockaddr *
652 in6_v4mapsin6_sockaddr(port, addr_p)
653 in_port_t port;
654 struct in_addr *addr_p;
655 {
656 struct sockaddr_in sin;
657 struct sockaddr_in6 *sin6_p;
658
659 bzero(&sin, sizeof sin);
660 sin.sin_family = AF_INET;
661 sin.sin_len = sizeof(sin);
662 sin.sin_port = port;
663 sin.sin_addr = *addr_p;
664
665 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
666 M_WAITOK);
667 in6_sin_2_v4mapsin6(&sin, sin6_p);
668
669 return (struct sockaddr *)sin6_p;
670 }
671
672 /*
673 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
674 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
675 * in struct pr_usrreqs, so that protocols can just reference then directly
676 * without the need for a wrapper function. The socket must have a valid
677 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
678 * except through a kernel programming error, so it is acceptable to panic
679 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
680 * because there actually /is/ a programming error somewhere... XXX)
681 */
682 int
683 in6_setsockaddr(so, nam)
684 struct socket *so;
685 struct sockaddr **nam;
686 {
687 int s;
688 register struct inpcb *inp;
689 struct in6_addr addr;
690 in_port_t port;
691
692 s = splnet();
693 inp = sotoinpcb(so);
694 if (!inp) {
695 splx(s);
696 return EINVAL;
697 }
698 port = inp->inp_lport;
699 addr = inp->in6p_laddr;
700 splx(s);
701
702 *nam = in6_sockaddr(port, &addr);
703 return 0;
704 }
705
706 int
707 in6_setpeeraddr(so, nam)
708 struct socket *so;
709 struct sockaddr **nam;
710 {
711 int s;
712 struct inpcb *inp;
713 struct in6_addr addr;
714 in_port_t port;
715
716 s = splnet();
717 inp = sotoinpcb(so);
718 if (!inp) {
719 splx(s);
720 return EINVAL;
721 }
722 port = inp->inp_fport;
723 addr = inp->in6p_faddr;
724 splx(s);
725
726 *nam = in6_sockaddr(port, &addr);
727 return 0;
728 }
729
730 int
731 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
732 {
733 struct inpcb *inp = sotoinpcb(so);
734 int error;
735
736 if (inp == NULL)
737 return EINVAL;
738 if (inp->inp_vflag & INP_IPV4) {
739 error = in_setsockaddr(so, nam);
740 if (error == 0)
741 in6_sin_2_v4mapsin6_in_sock(nam);
742 } else
743 /* scope issues will be handled in in6_setsockaddr(). */
744 error = in6_setsockaddr(so, nam);
745
746 return error;
747 }
748
749 int
750 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
751 {
752 struct inpcb *inp = sotoinpcb(so);
753 int error;
754
755 if (inp == NULL)
756 return EINVAL;
757 if (inp->inp_vflag & INP_IPV4) {
758 error = in_setpeeraddr(so, nam);
759 if (error == 0)
760 in6_sin_2_v4mapsin6_in_sock(nam);
761 } else
762 /* scope issues will be handled in in6_setpeeraddr(). */
763 error = in6_setpeeraddr(so, nam);
764
765 return error;
766 }
767
768 /*
769 * Pass some notification to all connections of a protocol
770 * associated with address dst. The local address and/or port numbers
771 * may be specified to limit the search. The "usual action" will be
772 * taken, depending on the ctlinput cmd. The caller must filter any
773 * cmds that are uninteresting (e.g., no error in the map).
774 * Call the protocol specific routine (if any) to report
775 * any errors for each matching socket.
776 *
777 * Must be called at splnet.
778 */
779 void
780 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
781 struct inpcbhead *head;
782 struct sockaddr *dst;
783 const struct sockaddr *src;
784 u_int fport_arg, lport_arg;
785 int cmd;
786 // struct inpcb *(*notify) __P((struct inpcb *, int));
787 void (*notify) __P((struct inpcb *, int));
788 {
789 struct inpcb *inp, *ninp;
790 struct sockaddr_in6 sa6_src, *sa6_dst;
791 u_short fport = fport_arg, lport = lport_arg;
792 u_int32_t flowinfo;
793 int errno, s;
794
795 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
796 return;
797
798 sa6_dst = (struct sockaddr_in6 *)dst;
799 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
800 return;
801
802 /*
803 * note that src can be NULL when we get notify by local fragmentation.
804 */
805 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
806 flowinfo = sa6_src.sin6_flowinfo;
807
808 /*
809 * Redirects go to all references to the destination,
810 * and use in6_rtchange to invalidate the route cache.
811 * Dead host indications: also use in6_rtchange to invalidate
812 * the cache, and deliver the error to all the sockets.
813 * Otherwise, if we have knowledge of the local port and address,
814 * deliver only to that socket.
815 */
816 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
817 fport = 0;
818 lport = 0;
819 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
820
821 if (cmd != PRC_HOSTDEAD)
822 notify = in6_rtchange;
823 }
824 errno = inet6ctlerrmap[cmd];
825 s = splnet();
826 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
827 ninp = LIST_NEXT(inp, inp_list);
828
829 if ((inp->inp_vflag & INP_IPV6) == 0)
830 continue;
831
832 /*
833 * Detect if we should notify the error. If no source and
834 * destination ports are specifed, but non-zero flowinfo and
835 * local address match, notify the error. This is the case
836 * when the error is delivered with an encrypted buffer
837 * by ESP. Otherwise, just compare addresses and ports
838 * as usual.
839 */
840 if (lport == 0 && fport == 0 && flowinfo &&
841 inp->inp_socket != NULL &&
842 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
843 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
844 goto do_notify;
845 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
846 &sa6_dst->sin6_addr) ||
847 inp->inp_socket == 0 ||
848 (lport && inp->inp_lport != lport) ||
849 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
850 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
851 &sa6_src.sin6_addr)) ||
852 (fport && inp->inp_fport != fport))
853 continue;
854
855 do_notify:
856 if (notify)
857 (*notify)(inp, errno);
858 }
859 splx(s);
860 }
861
862 /*
863 * Lookup a PCB based on the local address and port.
864 */
865 struct inpcb *
866 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
867 struct inpcbinfo *pcbinfo;
868 struct in6_addr *laddr;
869 u_int lport_arg;
870 int wild_okay;
871 {
872 register struct inpcb *inp;
873 int matchwild = 3, wildcard;
874 u_short lport = lport_arg;
875
876 if (!wild_okay) {
877 struct inpcbhead *head;
878 /*
879 * Look for an unconnected (wildcard foreign addr) PCB that
880 * matches the local address and port we're looking for.
881 */
882 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
883 pcbinfo->hashmask)];
884 LIST_FOREACH(inp, head, inp_hash) {
885 if ((inp->inp_vflag & INP_IPV6) == 0)
886 continue;
887 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
888 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
889 inp->inp_lport == lport) {
890 /*
891 * Found.
892 */
893 return (inp);
894 }
895 }
896 /*
897 * Not found.
898 */
899 return (NULL);
900 } else {
901 struct inpcbporthead *porthash;
902 struct inpcbport *phd;
903 struct inpcb *match = NULL;
904 /*
905 * Best fit PCB lookup.
906 *
907 * First see if this local port is in use by looking on the
908 * port hash list.
909 */
910 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
911 pcbinfo->porthashmask)];
912 LIST_FOREACH(phd, porthash, phd_hash) {
913 if (phd->phd_port == lport)
914 break;
915 }
916 if (phd != NULL) {
917 /*
918 * Port is in use by one or more PCBs. Look for best
919 * fit.
920 */
921 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
922 wildcard = 0;
923 if ((inp->inp_vflag & INP_IPV6) == 0)
924 continue;
925 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
926 wildcard++;
927 if (!IN6_IS_ADDR_UNSPECIFIED(
928 &inp->in6p_laddr)) {
929 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
930 wildcard++;
931 else if (!IN6_ARE_ADDR_EQUAL(
932 &inp->in6p_laddr, laddr))
933 continue;
934 } else {
935 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
936 wildcard++;
937 }
938 if (wildcard < matchwild) {
939 match = inp;
940 matchwild = wildcard;
941 if (matchwild == 0) {
942 break;
943 }
944 }
945 }
946 }
947 return (match);
948 }
949 }
950
951 void
952 in6_pcbpurgeif0(head, ifp)
953 struct in6pcb *head;
954 struct ifnet *ifp;
955 {
956 struct in6pcb *in6p;
957 struct ip6_moptions *im6o;
958 struct in6_multi_mship *imm, *nimm;
959
960 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
961 im6o = in6p->in6p_moptions;
962 if ((in6p->inp_vflag & INP_IPV6) &&
963 im6o) {
964 /*
965 * Unselect the outgoing interface if it is being
966 * detached.
967 */
968 if (im6o->im6o_multicast_ifp == ifp)
969 im6o->im6o_multicast_ifp = NULL;
970
971 /*
972 * Drop multicast group membership if we joined
973 * through the interface being detached.
974 * XXX controversial - is it really legal for kernel
975 * to force this?
976 */
977 for (imm = im6o->im6o_memberships.lh_first;
978 imm != NULL; imm = nimm) {
979 nimm = imm->i6mm_chain.le_next;
980 if (imm->i6mm_maddr->in6m_ifp == ifp) {
981 LIST_REMOVE(imm, i6mm_chain);
982 in6_delmulti(imm->i6mm_maddr);
983 FREE(imm, M_IPMADDR);
984 }
985 }
986 }
987 }
988 }
989
990 /*
991 * Check for alternatives when higher level complains
992 * about service problems. For now, invalidate cached
993 * routing information. If the route was created dynamically
994 * (by a redirect), time to try a default gateway again.
995 */
996 void
997 in6_losing(in6p)
998 struct inpcb *in6p;
999 {
1000 struct rtentry *rt;
1001 struct rt_addrinfo info;
1002
1003 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1004 in6p->in6p_route.ro_rt = 0;
1005 bzero((caddr_t)&info, sizeof(info));
1006 info.rti_info[RTAX_DST] =
1007 (struct sockaddr *)&in6p->in6p_route.ro_dst;
1008 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1009 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1010 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1011 if (rt->rt_flags & RTF_DYNAMIC)
1012 (void)rtrequest(RTM_DELETE, rt_key(rt),
1013 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
1014 (struct rtentry **)0);
1015 else
1016 /*
1017 * A new route can be allocated
1018 * the next time output is attempted.
1019 */
1020 rtfree(rt);
1021 }
1022 }
1023
1024 /*
1025 * After a routing change, flush old routing
1026 * and allocate a (hopefully) better one.
1027 */
1028 void
1029 in6_rtchange(inp, errno)
1030 struct inpcb *inp;
1031 int errno;
1032 {
1033 if (inp->in6p_route.ro_rt) {
1034 rtfree(inp->in6p_route.ro_rt);
1035 inp->in6p_route.ro_rt = 0;
1036 /*
1037 * A new route can be allocated the next time
1038 * output is attempted.
1039 */
1040 }
1041 }
1042
1043 /*
1044 * Lookup PCB in hash list.
1045 */
1046 struct inpcb *
1047 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1048 struct inpcbinfo *pcbinfo;
1049 struct in6_addr *faddr, *laddr;
1050 u_int fport_arg, lport_arg;
1051 int wildcard;
1052 struct ifnet *ifp;
1053 {
1054 struct inpcbhead *head;
1055 register struct inpcb *inp;
1056 u_short fport = fport_arg, lport = lport_arg;
1057 int faith;
1058
1059 #if defined(NFAITH) && NFAITH > 0
1060 faith = faithprefix(laddr);
1061 #else
1062 faith = 0;
1063 #endif
1064
1065 /*
1066 * First look for an exact match.
1067 */
1068 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1069 lport, fport,
1070 pcbinfo->hashmask)];
1071 LIST_FOREACH(inp, head, inp_hash) {
1072 if ((inp->inp_vflag & INP_IPV6) == 0)
1073 continue;
1074 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1075 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1076 inp->inp_fport == fport &&
1077 inp->inp_lport == lport) {
1078 /*
1079 * Found.
1080 */
1081 return (inp);
1082 }
1083 }
1084 if (wildcard) {
1085 struct inpcb *local_wild = NULL;
1086
1087 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1088 pcbinfo->hashmask)];
1089 LIST_FOREACH(inp, head, inp_hash) {
1090 if ((inp->inp_vflag & INP_IPV6) == 0)
1091 continue;
1092 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1093 inp->inp_lport == lport) {
1094 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1095 continue;
1096 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1097 laddr))
1098 return (inp);
1099 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1100 local_wild = inp;
1101 }
1102 }
1103 return (local_wild);
1104 }
1105
1106 /*
1107 * Not found.
1108 */
1109 return (NULL);
1110 }
1111
1112 void
1113 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1114 {
1115 struct ip6_hdr *ip;
1116
1117 ip = mtod(m, struct ip6_hdr *);
1118 bzero(sin6, sizeof(*sin6));
1119 sin6->sin6_len = sizeof(*sin6);
1120 sin6->sin6_family = AF_INET6;
1121 sin6->sin6_addr = ip->ip6_src;
1122 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1123 sin6->sin6_addr.s6_addr16[1] = 0;
1124 sin6->sin6_scope_id =
1125 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1126 ? m->m_pkthdr.rcvif->if_index : 0;
1127
1128 return;
1129 }