]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/in6_pcb.c
Notify functions can destroy the pcb, so they have to return an
[FreeBSD/FreeBSD.git] / sys / netinet6 / in6_pcb.c
1 /*      $FreeBSD$       */
2 /*      $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei Exp $        */
3   
4 /*
5  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the project nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  */
33
34 /*
35  * Copyright (c) 1982, 1986, 1991, 1993
36  *      The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  * 3. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *      This product includes software developed by the University of
49  *      California, Berkeley and its contributors.
50  * 4. Neither the name of the University 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  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *      @(#)in_pcb.c    8.2 (Berkeley) 1/4/94
67  */
68
69 #include "opt_inet.h"
70 #include "opt_inet6.h"
71 #include "opt_ipsec.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/domain.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/sockio.h>
82 #include <sys/errno.h>
83 #include <sys/time.h>
84 #include <sys/proc.h>
85 #include <sys/jail.h>
86
87 #include <vm/uma.h>
88
89 #include <net/if.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/tcp_var.h>
97 #include <netinet/ip6.h>
98 #include <netinet/ip_var.h>
99 #include <netinet6/ip6_var.h>
100 #include <netinet6/nd6.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet6/in6_pcb.h>
103
104 #ifdef IPSEC
105 #include <netinet6/ipsec.h>
106 #ifdef INET6
107 #include <netinet6/ipsec6.h>
108 #endif
109 #include <netinet6/ah.h>
110 #ifdef INET6
111 #include <netinet6/ah6.h>
112 #endif
113 #include <netkey/key.h>
114 #endif /* IPSEC */
115
116 struct  in6_addr zeroin6_addr;
117
118 int
119 in6_pcbbind(inp, nam, td)
120         register struct inpcb *inp;
121         struct sockaddr *nam;
122         struct thread *td;
123 {
124         struct socket *so = inp->inp_socket;
125         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
126         struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
127         u_short lport = 0;
128         int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
129
130         if (!in6_ifaddr) /* XXX broken! */
131                 return (EADDRNOTAVAIL);
132         if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
133                 return(EINVAL);
134         if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
135                 wild = 1;
136         if (nam) {
137                 sin6 = (struct sockaddr_in6 *)nam;
138                 if (nam->sa_len != sizeof(*sin6))
139                         return(EINVAL);
140                 /*
141                  * family check.
142                  */
143                 if (nam->sa_family != AF_INET6)
144                         return(EAFNOSUPPORT);
145
146                 /* KAME hack: embed scopeid */
147                 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
148                         return EINVAL;
149                 /* this must be cleared for ifa_ifwithaddr() */
150                 sin6->sin6_scope_id = 0;
151
152                 lport = sin6->sin6_port;
153                 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
154                         /*
155                          * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
156                          * allow compepte duplication of binding if
157                          * SO_REUSEPORT is set, or if SO_REUSEADDR is set
158                          * and a multicast address is bound on both
159                          * new and duplicated sockets.
160                          */
161                         if (so->so_options & SO_REUSEADDR)
162                                 reuseport = SO_REUSEADDR|SO_REUSEPORT;
163                 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
164                         struct ifaddr *ia = NULL;
165
166                         sin6->sin6_port = 0;            /* yech... */
167                         if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
168                                 return(EADDRNOTAVAIL);
169
170                         /*
171                          * XXX: bind to an anycast address might accidentally
172                          * cause sending a packet with anycast source address.
173                          * We should allow to bind to a deprecated address, since
174                          * the application dare to use it.
175                          */
176                         if (ia &&
177                             ((struct in6_ifaddr *)ia)->ia6_flags &
178                             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
179                                 return(EADDRNOTAVAIL);
180                         }
181                 }
182                 if (lport) {
183                         struct inpcb *t;
184
185                         /* GROSS */
186                         if (ntohs(lport) < IPV6PORT_RESERVED && td &&
187                             suser_cred(td->td_ucred, PRISON_ROOT))
188                                 return(EACCES);
189                         if (so->so_cred->cr_uid != 0 &&
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_cred->cr_uid !=
200                                      t->inp_socket->so_cred->cr_uid))
201                                         return (EADDRINUSE);
202                                 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
203                                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
204                                         struct sockaddr_in sin;
205
206                                         in6_sin6_2_sin(&sin, sin6);
207                                         t = in_pcblookup_local(pcbinfo,
208                                                 sin.sin_addr, lport,
209                                                 INPLOOKUP_WILDCARD);
210                                         if (t &&
211                                             (so->so_cred->cr_uid !=
212                                              t->inp_socket->so_cred->cr_uid) &&
213                                             (ntohl(t->inp_laddr.s_addr) !=
214                                              INADDR_ANY ||
215                                              INP_SOCKAF(so) ==
216                                              INP_SOCKAF(t->inp_socket)))
217                                                 return (EADDRINUSE);
218                                 }
219                         }
220                         t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
221                                                 lport, wild);
222                         if (t && (reuseport & t->inp_socket->so_options) == 0)
223                                 return(EADDRINUSE);
224                         if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
225                             IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
226                                 struct sockaddr_in sin;
227
228                                 in6_sin6_2_sin(&sin, sin6);
229                                 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
230                                                        lport, wild);
231                                 if (t &&
232                                     (reuseport & t->inp_socket->so_options)
233                                     == 0 &&
234                                     (ntohl(t->inp_laddr.s_addr)
235                                      != INADDR_ANY ||
236                                      INP_SOCKAF(so) ==
237                                      INP_SOCKAF(t->inp_socket)))
238                                         return (EADDRINUSE);
239                         }
240                 }
241                 inp->in6p_laddr = sin6->sin6_addr;
242         }
243         if (lport == 0) {
244                 int e;
245                 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, td)) != 0)
246                         return(e);
247         }
248         else {
249                 inp->inp_lport = lport;
250                 if (in_pcbinshash(inp) != 0) {
251                         inp->in6p_laddr = in6addr_any;
252                         inp->inp_lport = 0;
253                         return (EAGAIN);
254                 }
255         }
256         return(0);
257 }
258
259 /*
260  *   Transform old in6_pcbconnect() into an inner subroutine for new
261  *   in6_pcbconnect(): Do some validity-checking on the remote
262  *   address (in mbuf 'nam') and then determine local host address
263  *   (i.e., which interface) to use to access that remote host.
264  *
265  *   This preserves definition of in6_pcbconnect(), while supporting a
266  *   slightly different version for T/TCP.  (This is more than
267  *   a bit of a kludge, but cleaning up the internal interfaces would
268  *   have forced minor changes in every protocol).
269  */
270
271 int
272 in6_pcbladdr(inp, nam, plocal_addr6)
273         register struct inpcb *inp;
274         struct sockaddr *nam;
275         struct in6_addr **plocal_addr6;
276 {
277         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
278         struct ifnet *ifp = NULL;
279         int error = 0;
280
281         if (nam->sa_len != sizeof (*sin6))
282                 return (EINVAL);
283         if (sin6->sin6_family != AF_INET6)
284                 return (EAFNOSUPPORT);
285         if (sin6->sin6_port == 0)
286                 return (EADDRNOTAVAIL);
287
288         /* KAME hack: embed scopeid */
289         if (in6_embedscope(&sin6->sin6_addr, sin6, inp, &ifp) != 0)
290                 return EINVAL;
291
292         if (in6_ifaddr) {
293                 /*
294                  * If the destination address is UNSPECIFIED addr,
295                  * use the loopback addr, e.g ::1.
296                  */
297                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
298                         sin6->sin6_addr = in6addr_loopback;
299         }
300         {
301                 /*
302                  * XXX: in6_selectsrc might replace the bound local address
303                  * with the address specified by setsockopt(IPV6_PKTINFO).
304                  * Is it the intended behavior?
305                  */
306                 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
307                                               inp->in6p_moptions,
308                                               &inp->in6p_route,
309                                               &inp->in6p_laddr, &error);
310                 if (*plocal_addr6 == 0) {
311                         if (error == 0)
312                                 error = EADDRNOTAVAIL;
313                         return(error);
314                 }
315                 /*
316                  * Don't do pcblookup call here; return interface in
317                  * plocal_addr6
318                  * and exit to caller, that will do the lookup.
319                  */
320         }
321
322         if (inp->in6p_route.ro_rt)
323                 ifp = inp->in6p_route.ro_rt->rt_ifp;
324
325         return(0);
326 }
327
328 /*
329  * Outer subroutine:
330  * Connect from a socket to a specified address.
331  * Both address and port must be specified in argument sin.
332  * If don't have a local address for this socket yet,
333  * then pick one.
334  */
335 int
336 in6_pcbconnect(inp, nam, td)
337         register struct inpcb *inp;
338         struct sockaddr *nam;
339         struct thread *td;
340 {
341         struct in6_addr *addr6;
342         register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
343         int error;
344
345         /*
346          * Call inner routine, to assign local interface address.
347          * in6_pcbladdr() may automatically fill in sin6_scope_id.
348          */
349         if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
350                 return(error);
351
352         if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
353                                sin6->sin6_port,
354                               IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
355                               ? addr6 : &inp->in6p_laddr,
356                               inp->inp_lport, 0, NULL) != NULL) {
357                 return (EADDRINUSE);
358         }
359         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
360                 if (inp->inp_lport == 0) {
361                         error = in6_pcbbind(inp, (struct sockaddr *)0, td);
362                         if (error)
363                                 return (error);
364                 }
365                 inp->in6p_laddr = *addr6;
366         }
367         inp->in6p_faddr = sin6->sin6_addr;
368         inp->inp_fport = sin6->sin6_port;
369         /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
370         inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
371         if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
372                 inp->in6p_flowinfo |=
373                     (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK);
374
375         in_pcbrehash(inp);
376         return (0);
377 }
378
379 #if 0
380 /*
381  * Return an IPv6 address, which is the most appropriate for given
382  * destination and user specified options.
383  * If necessary, this function lookups the routing table and return
384  * an entry to the caller for later use.
385  */
386 struct in6_addr *
387 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
388         struct sockaddr_in6 *dstsock;
389         struct ip6_pktopts *opts;
390         struct ip6_moptions *mopts;
391         struct route_in6 *ro;
392         struct in6_addr *laddr;
393         int *errorp;
394 {
395         struct in6_addr *dst;
396         struct in6_ifaddr *ia6 = 0;
397         struct in6_pktinfo *pi = NULL;
398
399         dst = &dstsock->sin6_addr;
400         *errorp = 0;
401
402         /*
403          * If the source address is explicitly specified by the caller,
404          * use it.
405          */
406         if (opts && (pi = opts->ip6po_pktinfo) &&
407             !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
408                 return(&pi->ipi6_addr);
409
410         /*
411          * If the source address is not specified but the socket(if any)
412          * is already bound, use the bound address.
413          */
414         if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
415                 return(laddr);
416
417         /*
418          * If the caller doesn't specify the source address but
419          * the outgoing interface, use an address associated with
420          * the interface.
421          */
422         if (pi && pi->ipi6_ifindex) {
423                 /* XXX boundary check is assumed to be already done. */
424                 ia6 = in6_ifawithscope(ifnet_byindex(pi->ipi6_ifindex), 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(ifnet_byindex(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 #ifdef 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         sotryfree(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         INP_LOCK_DESTROY(inp);
626         uma_zfree(ipi->ipi_zone, inp);
627 }
628
629 /*
630  * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
631  * modified to match the pru_sockaddr() and pru_peeraddr() entry points
632  * in struct pr_usrreqs, so that protocols can just reference then directly
633  * without the need for a wrapper function.  The socket must have a valid
634  * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
635  * except through a kernel programming error, so it is acceptable to panic
636  * (or in this case trap) if the PCB is invalid.  (Actually, we don't trap
637  * because there actually /is/ a programming error somewhere... XXX)
638  */
639 int
640 in6_setsockaddr(so, nam)
641         struct socket *so;
642         struct sockaddr **nam;
643 {
644         int s;
645         register struct inpcb *inp;
646         register struct sockaddr_in6 *sin6;
647
648         /*
649          * Do the malloc first in case it blocks.
650          */
651         MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
652         bzero(sin6, sizeof *sin6);
653         sin6->sin6_family = AF_INET6;
654         sin6->sin6_len = sizeof(*sin6);
655
656         s = splnet();
657         inp = sotoinpcb(so);
658         if (!inp) {
659                 splx(s);
660                 free(sin6, M_SONAME);
661                 return EINVAL;
662         }
663         sin6->sin6_port = inp->inp_lport;
664         sin6->sin6_addr = inp->in6p_laddr;
665         splx(s);
666         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
667                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
668         else
669                 sin6->sin6_scope_id = 0;        /*XXX*/
670         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
671                 sin6->sin6_addr.s6_addr16[1] = 0;
672
673         *nam = (struct sockaddr *)sin6;
674         return 0;
675 }
676
677 int
678 in6_setpeeraddr(so, nam)
679         struct socket *so;
680         struct sockaddr **nam;
681 {
682         int s;
683         struct inpcb *inp;
684         register struct sockaddr_in6 *sin6;
685
686         /*
687          * Do the malloc first in case it blocks.
688          */
689         MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK);
690         bzero((caddr_t)sin6, sizeof (*sin6));
691         sin6->sin6_family = AF_INET6;
692         sin6->sin6_len = sizeof(struct sockaddr_in6);
693
694         s = splnet();
695         inp = sotoinpcb(so);
696         if (!inp) {
697                 splx(s);
698                 free(sin6, M_SONAME);
699                 return EINVAL;
700         }
701         sin6->sin6_port = inp->inp_fport;
702         sin6->sin6_addr = inp->in6p_faddr;
703         splx(s);
704         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
705                 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
706         else
707                 sin6->sin6_scope_id = 0;        /*XXX*/
708         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
709                 sin6->sin6_addr.s6_addr16[1] = 0;
710
711         *nam = (struct sockaddr *)sin6;
712         return 0;
713 }
714
715 int
716 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
717 {
718         struct  inpcb *inp = sotoinpcb(so);
719         int     error;
720
721         if (inp == NULL)
722                 return EINVAL;
723         if (inp->inp_vflag & INP_IPV4) {
724                 error = in_setsockaddr(so, nam, &tcbinfo);
725                 if (error == 0)
726                         in6_sin_2_v4mapsin6_in_sock(nam);
727         } else
728         /* scope issues will be handled in in6_setsockaddr(). */
729         error = in6_setsockaddr(so, nam);
730
731         return error;
732 }
733
734 int
735 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
736 {
737         struct  inpcb *inp = sotoinpcb(so);
738         int     error;
739
740         if (inp == NULL)
741                 return EINVAL;
742         if (inp->inp_vflag & INP_IPV4) {
743                 error = in_setpeeraddr(so, nam, &tcbinfo);
744                 if (error == 0)
745                         in6_sin_2_v4mapsin6_in_sock(nam);
746         } else
747         /* scope issues will be handled in in6_setpeeraddr(). */
748         error = in6_setpeeraddr(so, nam);
749
750         return error;
751 }
752
753 /*
754  * Pass some notification to all connections of a protocol
755  * associated with address dst.  The local address and/or port numbers
756  * may be specified to limit the search.  The "usual action" will be
757  * taken, depending on the ctlinput cmd.  The caller must filter any
758  * cmds that are uninteresting (e.g., no error in the map).
759  * Call the protocol specific routine (if any) to report
760  * any errors for each matching socket.
761  *
762  * Must be called at splnet.
763  */
764 void
765 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
766         struct inpcbhead *head;
767         struct sockaddr *dst;
768         const struct sockaddr *src;
769         u_int fport_arg, lport_arg;
770         int cmd;
771         struct inpcb *(*notify) __P((struct inpcb *, int));
772 {
773         struct inpcb *inp, *ninp;
774         struct sockaddr_in6 sa6_src, *sa6_dst;
775         u_short fport = fport_arg, lport = lport_arg;
776         u_int32_t flowinfo;
777         int errno, s;
778
779         if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
780                 return;
781
782         sa6_dst = (struct sockaddr_in6 *)dst;
783         if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
784                 return;
785
786         /*
787          * note that src can be NULL when we get notify by local fragmentation.
788          */
789         sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
790         flowinfo = sa6_src.sin6_flowinfo;
791
792         /*
793          * Redirects go to all references to the destination,
794          * and use in6_rtchange to invalidate the route cache.
795          * Dead host indications: also use in6_rtchange to invalidate
796          * the cache, and deliver the error to all the sockets.
797          * Otherwise, if we have knowledge of the local port and address,
798          * deliver only to that socket.
799          */
800         if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
801                 fport = 0;
802                 lport = 0;
803                 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
804
805                 if (cmd != PRC_HOSTDEAD)
806                         notify = in6_rtchange;
807         }
808         errno = inet6ctlerrmap[cmd];
809         s = splnet();
810         for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
811                 ninp = LIST_NEXT(inp, inp_list);
812
813                 if ((inp->inp_vflag & INP_IPV6) == 0)
814                         continue;
815
816                 /*
817                  * Detect if we should notify the error. If no source and
818                  * destination ports are specifed, but non-zero flowinfo and
819                  * local address match, notify the error. This is the case
820                  * when the error is delivered with an encrypted buffer
821                  * by ESP. Otherwise, just compare addresses and ports
822                  * as usual.
823                  */
824                 if (lport == 0 && fport == 0 && flowinfo &&
825                     inp->inp_socket != NULL &&
826                     flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
827                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
828                         goto do_notify;
829                 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
830                                              &sa6_dst->sin6_addr) ||
831                          inp->inp_socket == 0 ||
832                          (lport && inp->inp_lport != lport) ||
833                          (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
834                           !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
835                                               &sa6_src.sin6_addr)) ||
836                          (fport && inp->inp_fport != fport))
837                         continue;
838
839           do_notify:
840                 if (notify)
841                         (*notify)(inp, errno);
842         }
843         splx(s);
844 }
845
846 /*
847  * Lookup a PCB based on the local address and port.
848  */
849 struct inpcb *
850 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
851         struct inpcbinfo *pcbinfo;
852         struct in6_addr *laddr;
853         u_int lport_arg;
854         int wild_okay;
855 {
856         register struct inpcb *inp;
857         int matchwild = 3, wildcard;
858         u_short lport = lport_arg;
859
860         if (!wild_okay) {
861                 struct inpcbhead *head;
862                 /*
863                  * Look for an unconnected (wildcard foreign addr) PCB that
864                  * matches the local address and port we're looking for.
865                  */
866                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
867                                                       pcbinfo->hashmask)];
868                 LIST_FOREACH(inp, head, inp_hash) {
869                         if ((inp->inp_vflag & INP_IPV6) == 0)
870                                 continue;
871                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
872                             IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
873                             inp->inp_lport == lport) {
874                                 /*
875                                  * Found.
876                                  */
877                                 return (inp);
878                         }
879                 }
880                 /*
881                  * Not found.
882                  */
883                 return (NULL);
884         } else {
885                 struct inpcbporthead *porthash;
886                 struct inpcbport *phd;
887                 struct inpcb *match = NULL;
888                 /*
889                  * Best fit PCB lookup.
890                  *
891                  * First see if this local port is in use by looking on the
892                  * port hash list.
893                  */
894                 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
895                     pcbinfo->porthashmask)];
896                 LIST_FOREACH(phd, porthash, phd_hash) {
897                         if (phd->phd_port == lport)
898                                 break;
899                 }
900                 if (phd != NULL) {
901                         /*
902                          * Port is in use by one or more PCBs. Look for best
903                          * fit.
904                          */
905                         LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
906                                 wildcard = 0;
907                                 if ((inp->inp_vflag & INP_IPV6) == 0)
908                                         continue;
909                                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
910                                         wildcard++;
911                                 if (!IN6_IS_ADDR_UNSPECIFIED(
912                                         &inp->in6p_laddr)) {
913                                         if (IN6_IS_ADDR_UNSPECIFIED(laddr))
914                                                 wildcard++;
915                                         else if (!IN6_ARE_ADDR_EQUAL(
916                                                 &inp->in6p_laddr, laddr))
917                                                 continue;
918                                 } else {
919                                         if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
920                                                 wildcard++;
921                                 }
922                                 if (wildcard < matchwild) {
923                                         match = inp;
924                                         matchwild = wildcard;
925                                         if (matchwild == 0) {
926                                                 break;
927                                         }
928                                 }
929                         }
930                 }
931                 return (match);
932         }
933 }
934
935 void
936 in6_pcbpurgeif0(head, ifp)
937         struct in6pcb *head;
938         struct ifnet *ifp;
939 {
940         struct in6pcb *in6p;
941         struct ip6_moptions *im6o;
942         struct in6_multi_mship *imm, *nimm;
943
944         for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
945                 im6o = in6p->in6p_moptions;
946                 if ((in6p->inp_vflag & INP_IPV6) &&
947                     im6o) {
948                         /*
949                          * Unselect the outgoing interface if it is being
950                          * detached.
951                          */
952                         if (im6o->im6o_multicast_ifp == ifp)
953                                 im6o->im6o_multicast_ifp = NULL;
954
955                         /*
956                          * Drop multicast group membership if we joined
957                          * through the interface being detached.
958                          * XXX controversial - is it really legal for kernel
959                          * to force this?
960                          */
961                         for (imm = im6o->im6o_memberships.lh_first;
962                              imm != NULL; imm = nimm) {
963                                 nimm = imm->i6mm_chain.le_next;
964                                 if (imm->i6mm_maddr->in6m_ifp == ifp) {
965                                         LIST_REMOVE(imm, i6mm_chain);
966                                         in6_delmulti(imm->i6mm_maddr);
967                                         free(imm, M_IPMADDR);
968                                 }
969                         }
970                 }
971         }
972 }
973
974 /*
975  * Check for alternatives when higher level complains
976  * about service problems.  For now, invalidate cached
977  * routing information.  If the route was created dynamically
978  * (by a redirect), time to try a default gateway again.
979  */
980 void
981 in6_losing(in6p)
982         struct inpcb *in6p;
983 {
984         struct rtentry *rt;
985         struct rt_addrinfo info;
986
987         if ((rt = in6p->in6p_route.ro_rt) != NULL) {
988                 bzero((caddr_t)&info, sizeof(info));
989                 info.rti_flags = rt->rt_flags;
990                 info.rti_info[RTAX_DST] = rt_key(rt);
991                 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
992                 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
993                 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
994                 if (rt->rt_flags & RTF_DYNAMIC)
995                         (void)rtrequest1(RTM_DELETE, &info, NULL);
996                 in6p->in6p_route.ro_rt = NULL;
997                 rtfree(rt);
998                 /*
999                  * A new route can be allocated
1000                  * the next time output is attempted.
1001                  */
1002         }
1003 }
1004
1005 /*
1006  * After a routing change, flush old routing
1007  * and allocate a (hopefully) better one.
1008  */
1009 struct inpcb *
1010 in6_rtchange(inp, errno)
1011         struct inpcb *inp;
1012         int errno;
1013 {
1014         if (inp->in6p_route.ro_rt) {
1015                 rtfree(inp->in6p_route.ro_rt);
1016                 inp->in6p_route.ro_rt = 0;
1017                 /*
1018                  * A new route can be allocated the next time
1019                  * output is attempted.
1020                  */
1021         }
1022         return inp;
1023 }
1024
1025 /*
1026  * Lookup PCB in hash list.
1027  */
1028 struct inpcb *
1029 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1030         struct inpcbinfo *pcbinfo;
1031         struct in6_addr *faddr, *laddr;
1032         u_int fport_arg, lport_arg;
1033         int wildcard;
1034         struct ifnet *ifp;
1035 {
1036         struct inpcbhead *head;
1037         register struct inpcb *inp;
1038         u_short fport = fport_arg, lport = lport_arg;
1039         int faith;
1040
1041         if (faithprefix_p != NULL)
1042                 faith = (*faithprefix_p)(laddr);
1043         else
1044                 faith = 0;
1045
1046         /*
1047          * First look for an exact match.
1048          */
1049         head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1050                                               lport, fport,
1051                                               pcbinfo->hashmask)];
1052         LIST_FOREACH(inp, head, inp_hash) {
1053                 if ((inp->inp_vflag & INP_IPV6) == 0)
1054                         continue;
1055                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1056                     IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1057                     inp->inp_fport == fport &&
1058                     inp->inp_lport == lport) {
1059                         /*
1060                          * Found.
1061                          */
1062                         return (inp);
1063                 }
1064         }
1065         if (wildcard) {
1066                 struct inpcb *local_wild = NULL;
1067
1068                 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1069                                                       pcbinfo->hashmask)];
1070                 LIST_FOREACH(inp, head, inp_hash) {
1071                         if ((inp->inp_vflag & INP_IPV6) == 0)
1072                                 continue;
1073                         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1074                             inp->inp_lport == lport) {
1075                                 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1076                                         continue;
1077                                 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1078                                                        laddr))
1079                                         return (inp);
1080                                 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1081                                         local_wild = inp;
1082                         }
1083                 }
1084                 return (local_wild);
1085         }
1086
1087         /*
1088          * Not found.
1089          */
1090         return (NULL);
1091 }
1092
1093 void
1094 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1095 {
1096         struct ip6_hdr *ip;
1097
1098         ip = mtod(m, struct ip6_hdr *);
1099         bzero(sin6, sizeof(*sin6));
1100         sin6->sin6_len = sizeof(*sin6);
1101         sin6->sin6_family = AF_INET6;
1102         sin6->sin6_addr = ip->ip6_src;
1103         if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1104                 sin6->sin6_addr.s6_addr16[1] = 0;
1105         sin6->sin6_scope_id =
1106                 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1107                 ? m->m_pkthdr.rcvif->if_index : 0;
1108
1109         return;
1110 }