1 /* $FreeBSD: releng/5.1/sys/netinet6/in6_pcb.c 111145 2003-02-19 22:32:43Z jlemon $ */
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 #ifdef FAST_IPSEC
117 #include <netipsec/ipsec.h>
118 #include <netipsec/ipsec6.h>
119 #include <netipsec/key.h>
120 #define IPSEC
121 #endif /* FAST_IPSEC */
122
123 struct in6_addr zeroin6_addr;
124
125 int
126 in6_pcbbind(inp, nam, td)
127 register struct inpcb *inp;
128 struct sockaddr *nam;
129 struct thread *td;
130 {
131 struct socket *so = inp->inp_socket;
132 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
133 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
134 u_short lport = 0;
135 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
136
137 if (!in6_ifaddr) /* XXX broken! */
138 return (EADDRNOTAVAIL);
139 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
140 return(EINVAL);
141 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
142 wild = 1;
143 if (nam) {
144 sin6 = (struct sockaddr_in6 *)nam;
145 if (nam->sa_len != sizeof(*sin6))
146 return(EINVAL);
147 /*
148 * family check.
149 */
150 if (nam->sa_family != AF_INET6)
151 return(EAFNOSUPPORT);
152
153 /* KAME hack: embed scopeid */
154 if (in6_embedscope(&sin6->sin6_addr, sin6, inp, NULL) != 0)
155 return EINVAL;
156 /* this must be cleared for ifa_ifwithaddr() */
157 sin6->sin6_scope_id = 0;
158
159 lport = sin6->sin6_port;
160 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
161 /*
162 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
163 * allow compepte duplication of binding if
164 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
165 * and a multicast address is bound on both
166 * new and duplicated sockets.
167 */
168 if (so->so_options & SO_REUSEADDR)
169 reuseport = SO_REUSEADDR|SO_REUSEPORT;
170 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
171 struct ifaddr *ia = NULL;
172
173 sin6->sin6_port = 0; /* yech... */
174 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
175 return(EADDRNOTAVAIL);
176
177 /*
178 * XXX: bind to an anycast address might accidentally
179 * cause sending a packet with anycast source address.
180 * We should allow to bind to a deprecated address, since
181 * the application dare to use it.
182 */
183 if (ia &&
184 ((struct in6_ifaddr *)ia)->ia6_flags &
185 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
186 return(EADDRNOTAVAIL);
187 }
188 }
189 if (lport) {
190 struct inpcb *t;
191
192 /* GROSS */
193 if (ntohs(lport) < IPV6PORT_RESERVED && td &&
194 suser_cred(td->td_ucred, PRISON_ROOT))
195 return(EACCES);
196 if (so->so_cred->cr_uid != 0 &&
197 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
198 t = in6_pcblookup_local(pcbinfo,
199 &sin6->sin6_addr, lport,
200 INPLOOKUP_WILDCARD);
201 if (t &&
202 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
203 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
204 (t->inp_socket->so_options &
205 SO_REUSEPORT) == 0) &&
206 (so->so_cred->cr_uid !=
207 t->inp_socket->so_cred->cr_uid))
208 return (EADDRINUSE);
209 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct sockaddr_in sin;
212
213 in6_sin6_2_sin(&sin, sin6);
214 t = in_pcblookup_local(pcbinfo,
215 sin.sin_addr, lport,
216 INPLOOKUP_WILDCARD);
217 if (t &&
218 (so->so_cred->cr_uid !=
219 t->inp_socket->so_cred->cr_uid) &&
220 (ntohl(t->inp_laddr.s_addr) !=
221 INADDR_ANY ||
222 INP_SOCKAF(so) ==
223 INP_SOCKAF(t->inp_socket)))
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, td)) != 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, td)
344 register struct inpcb *inp;
345 struct sockaddr *nam;
346 struct thread *td;
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, td);
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(ifnet_byindex(pi->ipi6_ifindex), dst);
432 if (ia6 == 0) {
433 *errorp = EADDRNOTAVAIL;
434 return(0);
435 }
436 return(&satosin6(&ia6->ia_addr)->sin6_addr);
437 }
438
439 /*
440 * If the destination address is a link-local unicast address or
441 * a multicast address, and if the outgoing interface is specified
442 * by the sin6_scope_id filed, use an address associated with the
443 * interface.
444 * XXX: We're now trying to define more specific semantics of
445 * sin6_scope_id field, so this part will be rewritten in
446 * the near future.
447 */
448 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
449 dstsock->sin6_scope_id) {
450 /*
451 * I'm not sure if boundary check for scope_id is done
452 * somewhere...
453 */
454 if (dstsock->sin6_scope_id < 0 ||
455 if_index < dstsock->sin6_scope_id) {
456 *errorp = ENXIO; /* XXX: better error? */
457 return(0);
458 }
459 ia6 = in6_ifawithscope(ifnet_byindex(dstsock->sin6_scope_id),
460 dst);
461 if (ia6 == 0) {
462 *errorp = EADDRNOTAVAIL;
463 return(0);
464 }
465 return(&satosin6(&ia6->ia_addr)->sin6_addr);
466 }
467
468 /*
469 * If the destination address is a multicast address and
470 * the outgoing interface for the address is specified
471 * by the caller, use an address associated with the interface.
472 * There is a sanity check here; if the destination has node-local
473 * scope, the outgoing interfacde should be a loopback address.
474 * Even if the outgoing interface is not specified, we also
475 * choose a loopback interface as the outgoing interface.
476 */
477 if (IN6_IS_ADDR_MULTICAST(dst)) {
478 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
479
480 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
481 ifp = &loif[0];
482 }
483
484 if (ifp) {
485 ia6 = in6_ifawithscope(ifp, dst);
486 if (ia6 == 0) {
487 *errorp = EADDRNOTAVAIL;
488 return(0);
489 }
490 return(&ia6->ia_addr.sin6_addr);
491 }
492 }
493
494 /*
495 * If the next hop address for the packet is specified
496 * by caller, use an address associated with the route
497 * to the next hop.
498 */
499 {
500 struct sockaddr_in6 *sin6_next;
501 struct rtentry *rt;
502
503 if (opts && opts->ip6po_nexthop) {
504 sin6_next = satosin6(opts->ip6po_nexthop);
505 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
506 if (rt) {
507 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
508 if (ia6 == 0)
509 ia6 = ifatoia6(rt->rt_ifa);
510 }
511 if (ia6 == 0) {
512 *errorp = EADDRNOTAVAIL;
513 return(0);
514 }
515 return(&satosin6(&ia6->ia_addr)->sin6_addr);
516 }
517 }
518
519 /*
520 * If route is known or can be allocated now,
521 * our src addr is taken from the i/f, else punt.
522 */
523 if (ro) {
524 if (ro->ro_rt &&
525 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
526 RTFREE(ro->ro_rt);
527 ro->ro_rt = (struct rtentry *)0;
528 }
529 if (ro->ro_rt == (struct rtentry *)0 ||
530 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
531 struct sockaddr_in6 *dst6;
532
533 /* No route yet, so try to acquire one */
534 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
535 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
536 dst6->sin6_family = AF_INET6;
537 dst6->sin6_len = sizeof(struct sockaddr_in6);
538 dst6->sin6_addr = *dst;
539 if (IN6_IS_ADDR_MULTICAST(dst)) {
540 ro->ro_rt = rtalloc1(&((struct route *)ro)
541 ->ro_dst, 0, 0UL);
542 } else {
543 rtalloc((struct route *)ro);
544 }
545 }
546
547 /*
548 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
549 * the address. But we don't know why it does so.
550 * It is necessary to ensure the scope even for lo0
551 * so doesn't check out IFF_LOOPBACK.
552 */
553
554 if (ro->ro_rt) {
555 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
556 if (ia6 == 0) /* xxx scope error ?*/
557 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
558 }
559 if (ia6 == 0) {
560 *errorp = EHOSTUNREACH; /* no route */
561 return(0);
562 }
563 return(&satosin6(&ia6->ia_addr)->sin6_addr);
564 }
565
566 *errorp = EADDRNOTAVAIL;
567 return(0);
568 }
569
570 /*
571 * Default hop limit selection. The precedence is as follows:
572 * 1. Hoplimit valued specified via ioctl.
573 * 2. (If the outgoing interface is detected) the current
574 * hop limit of the interface specified by router advertisement.
575 * 3. The system default hoplimit.
576 */
577 int
578 in6_selecthlim(in6p, ifp)
579 struct in6pcb *in6p;
580 struct ifnet *ifp;
581 {
582 if (in6p && in6p->in6p_hops >= 0)
583 return(in6p->in6p_hops);
584 else if (ifp)
585 return(nd_ifinfo[ifp->if_index].chlim);
586 else
587 return(ip6_defhlim);
588 }
589 #endif
590
591 void
592 in6_pcbdisconnect(inp)
593 struct inpcb *inp;
594 {
595 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
596 inp->inp_fport = 0;
597 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
598 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
599 in_pcbrehash(inp);
600 if (inp->inp_socket->so_state & SS_NOFDREF)
601 in6_pcbdetach(inp);
602 }
603
604 void
605 in6_pcbdetach(inp)
606 struct inpcb *inp;
607 {
608 struct socket *so = inp->inp_socket;
609 struct inpcbinfo *ipi = inp->inp_pcbinfo;
610
611 #ifdef IPSEC
612 if (inp->in6p_sp != NULL)
613 ipsec6_delete_pcbpolicy(inp);
614 #endif /* IPSEC */
615 inp->inp_gencnt = ++ipi->ipi_gencnt;
616 in_pcbremlists(inp);
617 if (so) {
618 so->so_pcb = NULL;
619 sotryfree(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 inp->inp_vflag = 0;
632 INP_LOCK_DESTROY(inp);
633 uma_zfree(ipi->ipi_zone, inp);
634 }
635
636 struct sockaddr *
637 in6_sockaddr(port, addr_p)
638 in_port_t port;
639 struct in6_addr *addr_p;
640 {
641 struct sockaddr_in6 *sin6;
642
643 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
644 bzero(sin6, sizeof *sin6);
645 sin6->sin6_family = AF_INET6;
646 sin6->sin6_len = sizeof(*sin6);
647 sin6->sin6_port = port;
648 sin6->sin6_addr = *addr_p;
649 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
650 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
651 else
652 sin6->sin6_scope_id = 0; /*XXX*/
653 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
654 sin6->sin6_addr.s6_addr16[1] = 0;
655
656 return (struct sockaddr *)sin6;
657 }
658
659 struct sockaddr *
660 in6_v4mapsin6_sockaddr(port, addr_p)
661 in_port_t port;
662 struct in_addr *addr_p;
663 {
664 struct sockaddr_in sin;
665 struct sockaddr_in6 *sin6_p;
666
667 bzero(&sin, sizeof sin);
668 sin.sin_family = AF_INET;
669 sin.sin_len = sizeof(sin);
670 sin.sin_port = port;
671 sin.sin_addr = *addr_p;
672
673 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
674 M_WAITOK);
675 in6_sin_2_v4mapsin6(&sin, sin6_p);
676
677 return (struct sockaddr *)sin6_p;
678 }
679
680 /*
681 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
682 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
683 * in struct pr_usrreqs, so that protocols can just reference then directly
684 * without the need for a wrapper function. The socket must have a valid
685 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
686 * except through a kernel programming error, so it is acceptable to panic
687 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
688 * because there actually /is/ a programming error somewhere... XXX)
689 */
690 int
691 in6_setsockaddr(so, nam)
692 struct socket *so;
693 struct sockaddr **nam;
694 {
695 int s;
696 register struct inpcb *inp;
697 struct in6_addr addr;
698 in_port_t port;
699
700 s = splnet();
701 inp = sotoinpcb(so);
702 if (!inp) {
703 splx(s);
704 return EINVAL;
705 }
706 port = inp->inp_lport;
707 addr = inp->in6p_laddr;
708 splx(s);
709
710 *nam = in6_sockaddr(port, &addr);
711 return 0;
712 }
713
714 int
715 in6_setpeeraddr(so, nam)
716 struct socket *so;
717 struct sockaddr **nam;
718 {
719 int s;
720 struct inpcb *inp;
721 struct in6_addr addr;
722 in_port_t port;
723
724 s = splnet();
725 inp = sotoinpcb(so);
726 if (!inp) {
727 splx(s);
728 return EINVAL;
729 }
730 port = inp->inp_fport;
731 addr = inp->in6p_faddr;
732 splx(s);
733
734 *nam = in6_sockaddr(port, &addr);
735 return 0;
736 }
737
738 int
739 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
740 {
741 struct inpcb *inp = sotoinpcb(so);
742 int error;
743
744 if (inp == NULL)
745 return EINVAL;
746 if (inp->inp_vflag & INP_IPV4) {
747 error = in_setsockaddr(so, nam, &tcbinfo);
748 if (error == 0)
749 in6_sin_2_v4mapsin6_in_sock(nam);
750 } else
751 /* scope issues will be handled in in6_setsockaddr(). */
752 error = in6_setsockaddr(so, nam);
753
754 return error;
755 }
756
757 int
758 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
759 {
760 struct inpcb *inp = sotoinpcb(so);
761 int error;
762
763 if (inp == NULL)
764 return EINVAL;
765 if (inp->inp_vflag & INP_IPV4) {
766 error = in_setpeeraddr(so, nam, &tcbinfo);
767 if (error == 0)
768 in6_sin_2_v4mapsin6_in_sock(nam);
769 } else
770 /* scope issues will be handled in in6_setpeeraddr(). */
771 error = in6_setpeeraddr(so, nam);
772
773 return error;
774 }
775
776 /*
777 * Pass some notification to all connections of a protocol
778 * associated with address dst. The local address and/or port numbers
779 * may be specified to limit the search. The "usual action" will be
780 * taken, depending on the ctlinput cmd. The caller must filter any
781 * cmds that are uninteresting (e.g., no error in the map).
782 * Call the protocol specific routine (if any) to report
783 * any errors for each matching socket.
784 *
785 * Must be called at splnet.
786 */
787 void
788 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, notify)
789 struct inpcbhead *head;
790 struct sockaddr *dst;
791 const struct sockaddr *src;
792 u_int fport_arg, lport_arg;
793 int cmd;
794 struct inpcb *(*notify) __P((struct inpcb *, int));
795 {
796 struct inpcb *inp, *ninp;
797 struct sockaddr_in6 sa6_src, *sa6_dst;
798 u_short fport = fport_arg, lport = lport_arg;
799 u_int32_t flowinfo;
800 int errno, s;
801
802 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
803 return;
804
805 sa6_dst = (struct sockaddr_in6 *)dst;
806 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
807 return;
808
809 /*
810 * note that src can be NULL when we get notify by local fragmentation.
811 */
812 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
813 flowinfo = sa6_src.sin6_flowinfo;
814
815 /*
816 * Redirects go to all references to the destination,
817 * and use in6_rtchange to invalidate the route cache.
818 * Dead host indications: also use in6_rtchange to invalidate
819 * the cache, and deliver the error to all the sockets.
820 * Otherwise, if we have knowledge of the local port and address,
821 * deliver only to that socket.
822 */
823 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
824 fport = 0;
825 lport = 0;
826 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
827
828 if (cmd != PRC_HOSTDEAD)
829 notify = in6_rtchange;
830 }
831 errno = inet6ctlerrmap[cmd];
832 s = splnet();
833 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
834 ninp = LIST_NEXT(inp, inp_list);
835
836 if ((inp->inp_vflag & INP_IPV6) == 0)
837 continue;
838
839 /*
840 * Detect if we should notify the error. If no source and
841 * destination ports are specifed, but non-zero flowinfo and
842 * local address match, notify the error. This is the case
843 * when the error is delivered with an encrypted buffer
844 * by ESP. Otherwise, just compare addresses and ports
845 * as usual.
846 */
847 if (lport == 0 && fport == 0 && flowinfo &&
848 inp->inp_socket != NULL &&
849 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
850 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
851 goto do_notify;
852 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
853 &sa6_dst->sin6_addr) ||
854 inp->inp_socket == 0 ||
855 (lport && inp->inp_lport != lport) ||
856 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
857 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
858 &sa6_src.sin6_addr)) ||
859 (fport && inp->inp_fport != fport))
860 continue;
861
862 do_notify:
863 if (notify)
864 (*notify)(inp, errno);
865 }
866 splx(s);
867 }
868
869 /*
870 * Lookup a PCB based on the local address and port.
871 */
872 struct inpcb *
873 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
874 struct inpcbinfo *pcbinfo;
875 struct in6_addr *laddr;
876 u_int lport_arg;
877 int wild_okay;
878 {
879 register struct inpcb *inp;
880 int matchwild = 3, wildcard;
881 u_short lport = lport_arg;
882
883 if (!wild_okay) {
884 struct inpcbhead *head;
885 /*
886 * Look for an unconnected (wildcard foreign addr) PCB that
887 * matches the local address and port we're looking for.
888 */
889 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
890 pcbinfo->hashmask)];
891 LIST_FOREACH(inp, head, inp_hash) {
892 if ((inp->inp_vflag & INP_IPV6) == 0)
893 continue;
894 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
895 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
896 inp->inp_lport == lport) {
897 /*
898 * Found.
899 */
900 return (inp);
901 }
902 }
903 /*
904 * Not found.
905 */
906 return (NULL);
907 } else {
908 struct inpcbporthead *porthash;
909 struct inpcbport *phd;
910 struct inpcb *match = NULL;
911 /*
912 * Best fit PCB lookup.
913 *
914 * First see if this local port is in use by looking on the
915 * port hash list.
916 */
917 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
918 pcbinfo->porthashmask)];
919 LIST_FOREACH(phd, porthash, phd_hash) {
920 if (phd->phd_port == lport)
921 break;
922 }
923 if (phd != NULL) {
924 /*
925 * Port is in use by one or more PCBs. Look for best
926 * fit.
927 */
928 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
929 wildcard = 0;
930 if ((inp->inp_vflag & INP_IPV6) == 0)
931 continue;
932 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
933 wildcard++;
934 if (!IN6_IS_ADDR_UNSPECIFIED(
935 &inp->in6p_laddr)) {
936 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
937 wildcard++;
938 else if (!IN6_ARE_ADDR_EQUAL(
939 &inp->in6p_laddr, laddr))
940 continue;
941 } else {
942 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
943 wildcard++;
944 }
945 if (wildcard < matchwild) {
946 match = inp;
947 matchwild = wildcard;
948 if (matchwild == 0) {
949 break;
950 }
951 }
952 }
953 }
954 return (match);
955 }
956 }
957
958 void
959 in6_pcbpurgeif0(head, ifp)
960 struct in6pcb *head;
961 struct ifnet *ifp;
962 {
963 struct in6pcb *in6p;
964 struct ip6_moptions *im6o;
965 struct in6_multi_mship *imm, *nimm;
966
967 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
968 im6o = in6p->in6p_moptions;
969 if ((in6p->inp_vflag & INP_IPV6) &&
970 im6o) {
971 /*
972 * Unselect the outgoing interface if it is being
973 * detached.
974 */
975 if (im6o->im6o_multicast_ifp == ifp)
976 im6o->im6o_multicast_ifp = NULL;
977
978 /*
979 * Drop multicast group membership if we joined
980 * through the interface being detached.
981 * XXX controversial - is it really legal for kernel
982 * to force this?
983 */
984 for (imm = im6o->im6o_memberships.lh_first;
985 imm != NULL; imm = nimm) {
986 nimm = imm->i6mm_chain.le_next;
987 if (imm->i6mm_maddr->in6m_ifp == ifp) {
988 LIST_REMOVE(imm, i6mm_chain);
989 in6_delmulti(imm->i6mm_maddr);
990 free(imm, M_IPMADDR);
991 }
992 }
993 }
994 }
995 }
996
997 /*
998 * Check for alternatives when higher level complains
999 * about service problems. For now, invalidate cached
1000 * routing information. If the route was created dynamically
1001 * (by a redirect), time to try a default gateway again.
1002 */
1003 void
1004 in6_losing(in6p)
1005 struct inpcb *in6p;
1006 {
1007 struct rtentry *rt;
1008 struct rt_addrinfo info;
1009
1010 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
1011 bzero((caddr_t)&info, sizeof(info));
1012 info.rti_flags = rt->rt_flags;
1013 info.rti_info[RTAX_DST] = rt_key(rt);
1014 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
1015 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
1016 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
1017 if (rt->rt_flags & RTF_DYNAMIC)
1018 (void)rtrequest1(RTM_DELETE, &info, NULL);
1019 in6p->in6p_route.ro_rt = NULL;
1020 rtfree(rt);
1021 /*
1022 * A new route can be allocated
1023 * the next time output is attempted.
1024 */
1025 }
1026 }
1027
1028 /*
1029 * After a routing change, flush old routing
1030 * and allocate a (hopefully) better one.
1031 */
1032 struct inpcb *
1033 in6_rtchange(inp, errno)
1034 struct inpcb *inp;
1035 int errno;
1036 {
1037 if (inp->in6p_route.ro_rt) {
1038 rtfree(inp->in6p_route.ro_rt);
1039 inp->in6p_route.ro_rt = 0;
1040 /*
1041 * A new route can be allocated the next time
1042 * output is attempted.
1043 */
1044 }
1045 return inp;
1046 }
1047
1048 /*
1049 * Lookup PCB in hash list.
1050 */
1051 struct inpcb *
1052 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
1053 struct inpcbinfo *pcbinfo;
1054 struct in6_addr *faddr, *laddr;
1055 u_int fport_arg, lport_arg;
1056 int wildcard;
1057 struct ifnet *ifp;
1058 {
1059 struct inpcbhead *head;
1060 register struct inpcb *inp;
1061 u_short fport = fport_arg, lport = lport_arg;
1062 int faith;
1063
1064 if (faithprefix_p != NULL)
1065 faith = (*faithprefix_p)(laddr);
1066 else
1067 faith = 0;
1068
1069 /*
1070 * First look for an exact match.
1071 */
1072 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
1073 lport, fport,
1074 pcbinfo->hashmask)];
1075 LIST_FOREACH(inp, head, inp_hash) {
1076 if ((inp->inp_vflag & INP_IPV6) == 0)
1077 continue;
1078 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
1079 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
1080 inp->inp_fport == fport &&
1081 inp->inp_lport == lport) {
1082 /*
1083 * Found.
1084 */
1085 return (inp);
1086 }
1087 }
1088 if (wildcard) {
1089 struct inpcb *local_wild = NULL;
1090
1091 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
1092 pcbinfo->hashmask)];
1093 LIST_FOREACH(inp, head, inp_hash) {
1094 if ((inp->inp_vflag & INP_IPV6) == 0)
1095 continue;
1096 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
1097 inp->inp_lport == lport) {
1098 if (faith && (inp->inp_flags & INP_FAITH) == 0)
1099 continue;
1100 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
1101 laddr))
1102 return (inp);
1103 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1104 local_wild = inp;
1105 }
1106 }
1107 return (local_wild);
1108 }
1109
1110 /*
1111 * Not found.
1112 */
1113 return (NULL);
1114 }
1115
1116 void
1117 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
1118 {
1119 struct ip6_hdr *ip;
1120
1121 ip = mtod(m, struct ip6_hdr *);
1122 bzero(sin6, sizeof(*sin6));
1123 sin6->sin6_len = sizeof(*sin6);
1124 sin6->sin6_family = AF_INET6;
1125 sin6->sin6_addr = ip->ip6_src;
1126 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1127 sin6->sin6_addr.s6_addr16[1] = 0;
1128 sin6->sin6_scope_id =
1129 (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
1130 ? m->m_pkthdr.rcvif->if_index : 0;
1131
1132 return;
1133 }
Cache object: f7d95f13ec7c23c151c883a232786681
|