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