in6_pcb.c revision 1.12 1 /* $NetBSD: in6_pcb.c,v 1.12 2000/01/06 15:46:09 itojun Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1991, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
65 */
66
67 #include "opt_ipsec.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/ioctl.h>
77 #include <sys/errno.h>
78 #include <sys/time.h>
79 #include <sys/proc.h>
80
81 #include <net/if.h>
82 #include <net/route.h>
83
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet6/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/in6_pcb.h>
92 #include <netinet6/nd6.h>
93
94 #include "loop.h"
95 extern struct ifnet loif[NLOOP];
96 #include "faith.h"
97
98 #ifdef IPSEC
99 #include <netinet6/ipsec.h>
100 #include <netkey/key.h>
101 #include <netkey/key_debug.h>
102 #endif /* IPSEC */
103
104 struct in6_addr zeroin6_addr;
105
106 int
107 in6_pcballoc(so, head)
108 struct socket *so;
109 struct in6pcb *head;
110 {
111 struct in6pcb *in6p;
112
113 MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
114 if (in6p == NULL)
115 return(ENOBUFS);
116 bzero((caddr_t)in6p, sizeof(*in6p));
117 in6p->in6p_head = head;
118 in6p->in6p_socket = so;
119 in6p->in6p_hops = -1; /* use kernel default */
120 in6p->in6p_icmp6filt = NULL;
121 #if 0
122 insque(in6p, head);
123 #else
124 in6p->in6p_next = head->in6p_next;
125 head->in6p_next = in6p;
126 in6p->in6p_prev = head;
127 in6p->in6p_next->in6p_prev = in6p;
128 #endif
129 #ifndef INET6_BINDV6ONLY
130 if (ip6_bindv6only)
131 in6p->in6p_flags |= IN6P_BINDV6ONLY;
132 #else
133 in6p->in6p_flags |= IN6P_BINDV6ONLY; /*just for safety*/
134 #endif
135 so->so_pcb = (caddr_t)in6p;
136 return(0);
137 }
138
139 int
140 in6_pcbbind(in6p, nam)
141 register struct in6pcb *in6p;
142 struct mbuf *nam;
143 {
144 struct socket *so = in6p->in6p_socket;
145 struct in6pcb *head = in6p->in6p_head;
146 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
147 struct proc *p = curproc; /* XXX */
148 u_short lport = 0;
149 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
150 int error;
151
152 if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
153 return(EINVAL);
154 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
155 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
156 (so->so_options & SO_ACCEPTCONN) == 0))
157 wild = IN6PLOOKUP_WILDCARD;
158 if (nam) {
159 sin6 = mtod(nam, struct sockaddr_in6 *);
160 if (nam->m_len != sizeof(*sin6))
161 return(EINVAL);
162 /*
163 * We should check the family, but old programs
164 * incorrectly fail to intialize it.
165 */
166 if (sin6->sin6_family != AF_INET6)
167 return(EAFNOSUPPORT);
168
169 /*
170 * If the scope of the destination is link-local, embed the
171 * interface index in the address.
172 */
173 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
174 /* XXX boundary check is assumed to be already done. */
175 /* XXX sin6_scope_id is weaker than advanced-api. */
176 struct in6_pktinfo *pi;
177 if (in6p->in6p_outputopts &&
178 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
179 pi->ipi6_ifindex) {
180 sin6->sin6_addr.s6_addr16[1]
181 = htons(pi->ipi6_ifindex);
182 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)
183 && in6p->in6p_moptions
184 && in6p->in6p_moptions->im6o_multicast_ifp) {
185 sin6->sin6_addr.s6_addr16[1] =
186 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
187 } else if (sin6->sin6_scope_id) {
188 /* boundary check */
189 if (sin6->sin6_scope_id < 0
190 || if_index < sin6->sin6_scope_id) {
191 return ENXIO; /* XXX EINVAL? */
192 }
193 sin6->sin6_addr.s6_addr16[1]
194 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
195 /* this must be cleared for ifa_ifwithaddr() */
196 sin6->sin6_scope_id = 0;
197 }
198 }
199
200 lport = sin6->sin6_port;
201 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
202 /*
203 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
204 * allow compepte duplication of binding if
205 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
206 * and a multicast address is bound on both
207 * new and duplicated sockets.
208 */
209 if (so->so_options & SO_REUSEADDR)
210 reuseport = SO_REUSEADDR|SO_REUSEPORT;
211 } else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
212 struct sockaddr_in sin;
213
214 bzero(&sin, sizeof(sin));
215 sin.sin_len = sizeof(sin);
216 sin.sin_family = AF_INET;
217 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
218 sizeof(sin.sin_addr));
219 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
220 return EADDRNOTAVAIL;
221 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
222 struct ifaddr *ia = NULL;
223
224 sin6->sin6_port = 0; /* yech... */
225 #if defined(NFAITH) && NFAITH > 0
226 if ((in6p->in6p_flags & IN6P_FAITH) == 0
227 && (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
228 #else
229 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
230 #endif
231 return(EADDRNOTAVAIL);
232
233 /*
234 * XXX: bind to an anycast address might accidentally
235 * cause sending a packet with anycast source address.
236 */
237 if (ia &&
238 ((struct in6_ifaddr *)ia)->ia6_flags &
239 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
240 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
241 return(EADDRNOTAVAIL);
242 }
243 }
244 if (lport) {
245 /* GROSS */
246 if (ntohs(lport) < IPV6PORT_RESERVED &&
247 (error = suser(p->p_ucred, &p->p_acflag)))
248 return(EACCES);
249
250 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
251 /* should check this but we can't ... */
252 #if 0
253 struct inpcb *t;
254
255 t = in_pcblookup_bind(&tcbtable,
256 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
257 lport);
258 if (t && (reuseport & t->inp_socket->so_options) == 0)
259 return EADDRINUSE;
260 #endif
261 } else {
262 struct in6pcb *t;
263
264 t = in6_pcblookup(head, &zeroin6_addr, 0,
265 &sin6->sin6_addr, lport, wild);
266 if (t && (reuseport & t->in6p_socket->so_options) == 0)
267 return(EADDRINUSE);
268 }
269 }
270 in6p->in6p_laddr = sin6->sin6_addr;
271 }
272
273 if (lport == 0) {
274 int e;
275 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
276 return(e);
277 }
278 else
279 in6p->in6p_lport = lport;
280
281 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
282 return(0);
283 }
284
285 /*
286 * Find an empty port and set it to the specified PCB.
287 * XXX IN6P_LOWPORT
288 */
289 int
290 in6_pcbsetport(laddr, in6p)
291 struct in6_addr *laddr;
292 struct in6pcb *in6p;
293 {
294 struct socket *so = in6p->in6p_socket;
295 struct in6pcb *head = in6p->in6p_head;
296 u_short last_port, lport = 0;
297 int wild = 0;
298 void *t;
299
300 /* XXX: this is redundant when called from in6_pcbbind */
301 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
302 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
303 (so->so_options & SO_ACCEPTCONN) == 0))
304 wild = IN6PLOOKUP_WILDCARD;
305
306 /* value out of range */
307 if (head->in6p_lport < IPV6PORT_ANONMIN)
308 head->in6p_lport = IPV6PORT_ANONMIN;
309 else if (head->in6p_lport > IPV6PORT_ANONMAX)
310 head->in6p_lport = IPV6PORT_ANONMIN;
311 last_port = head->in6p_lport;
312 goto startover; /*to randomize*/
313 for (;;) {
314 lport = htons(head->in6p_lport);
315 if (IN6_IS_ADDR_V4MAPPED(laddr)) {
316 #if 0
317 t = in_pcblookup_bind(&tcbtable,
318 (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
319 lport);
320 #else
321 t = NULL;
322 #endif
323 } else {
324 t = in6_pcblookup(head, &zeroin6_addr, 0, laddr,
325 lport, wild);
326 }
327 if (t == 0)
328 break;
329 startover:
330 if (head->in6p_lport >= IPV6PORT_ANONMAX)
331 head->in6p_lport = IPV6PORT_ANONMIN;
332 else
333 head->in6p_lport++;
334 if (head->in6p_lport == last_port)
335 return (EADDRINUSE);
336 }
337
338 in6p->in6p_lport = lport;
339 return(0); /* success */
340 }
341
342 /*
343 * Connect from a socket to a specified address.
344 * Both address and port must be specified in argument sin6.
345 * If don't have a local address for this socket yet,
346 * then pick one.
347 */
348 int
349 in6_pcbconnect(in6p, nam)
350 struct in6pcb *in6p;
351 struct mbuf *nam;
352 {
353 struct in6_addr *in6a = NULL;
354 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
355 struct in6_pktinfo *pi;
356 struct ifnet *ifp = NULL; /* outgoing interface */
357 int error = 0;
358 struct in6_addr mapped;
359
360 (void)&in6a; /* XXX fool gcc */
361
362 if (nam->m_len != sizeof(*sin6))
363 return(EINVAL);
364 if (sin6->sin6_family != AF_INET6)
365 return(EAFNOSUPPORT);
366 if (sin6->sin6_port == 0)
367 return(EADDRNOTAVAIL);
368
369 /* sanity check for mapped address case */
370 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
371 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
372 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
373 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
374 return EINVAL;
375 } else {
376 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
377 return EINVAL;
378 }
379
380 /*
381 * If the scope of the destination is link-local, embed the interface
382 * index in the address.
383 */
384 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
385 /* XXX boundary check is assumed to be already done. */
386 /* XXX sin6_scope_id is weaker than advanced-api. */
387 if (in6p->in6p_outputopts &&
388 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
389 pi->ipi6_ifindex) {
390 sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex);
391 ifp = ifindex2ifnet[pi->ipi6_ifindex];
392 }
393 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
394 in6p->in6p_moptions &&
395 in6p->in6p_moptions->im6o_multicast_ifp) {
396 sin6->sin6_addr.s6_addr16[1] =
397 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
398 ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
399 } else if (sin6->sin6_scope_id) {
400 /* boundary check */
401 if (sin6->sin6_scope_id < 0
402 || if_index < sin6->sin6_scope_id) {
403 return ENXIO; /* XXX EINVAL? */
404 }
405 sin6->sin6_addr.s6_addr16[1]
406 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
407 ifp = ifindex2ifnet[sin6->sin6_scope_id];
408 }
409 }
410
411 /* Source address selection. */
412 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
413 && in6p->in6p_laddr.s6_addr32[3] == 0) {
414 struct sockaddr_in sin, *sinp;
415
416 bzero(&sin, sizeof(sin));
417 sin.sin_len = sizeof(sin);
418 sin.sin_family = AF_INET;
419 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
420 sizeof(sin.sin_addr));
421 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
422 in6p->in6p_socket->so_options, NULL, &error);
423 if (sinp == 0) {
424 if (error == 0)
425 error = EADDRNOTAVAIL;
426 return(error);
427 }
428 bzero(&mapped, sizeof(mapped));
429 mapped.s6_addr16[5] = htons(0xffff);
430 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
431 in6a = &mapped;
432 } else {
433 /*
434 * XXX: in6_selectsrc might replace the bound local address
435 * with the address specified by setsockopt(IPV6_PKTINFO).
436 * Is it the intended behavior?
437 */
438 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
439 in6p->in6p_moptions,
440 &in6p->in6p_route,
441 &in6p->in6p_laddr, &error);
442 if (in6a == 0) {
443 if (error == 0)
444 error = EADDRNOTAVAIL;
445 return(error);
446 }
447 }
448 if (in6p->in6p_route.ro_rt)
449 ifp = in6p->in6p_route.ro_rt->rt_ifp;
450
451 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
452
453 if (in6_pcblookup(in6p->in6p_head,
454 &sin6->sin6_addr,
455 sin6->sin6_port,
456 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
457 in6a : &in6p->in6p_laddr,
458 in6p->in6p_lport,
459 0))
460 return(EADDRINUSE);
461 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
462 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
463 && in6p->in6p_laddr.s6_addr32[3] == 0)) {
464 if (in6p->in6p_lport == 0)
465 (void)in6_pcbbind(in6p, (struct mbuf *)0);
466 in6p->in6p_laddr = *in6a;
467 }
468 in6p->in6p_faddr = sin6->sin6_addr;
469 in6p->in6p_fport = sin6->sin6_port;
470 /*
471 * xxx kazu flowlabel is necessary for connect?
472 * but if this line is missing, the garbage value remains.
473 */
474 in6p->in6p_flowinfo = sin6->sin6_flowinfo;
475 return(0);
476 }
477
478 /*
479 * Return an IPv6 address, which is the most appropriate for given
480 * destination and user specified options.
481 * If necessary, this function lookups the routing table and return
482 * an entry to the caller for later use.
483 */
484 struct in6_addr *
485 in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp)
486 struct sockaddr_in6 *dstsock;
487 struct ip6_pktopts *opts;
488 struct ip6_moptions *mopts;
489 struct route_in6 *ro;
490 struct in6_addr *laddr;
491 int *errorp;
492 {
493 struct in6_addr *dst;
494 struct in6_ifaddr *ia6 = 0;
495 struct in6_pktinfo *pi = NULL;
496
497 dst = &dstsock->sin6_addr;
498 *errorp = 0;
499
500 /*
501 * If the source address is explicitly specified by the caller,
502 * use it.
503 */
504 if (opts && (pi = opts->ip6po_pktinfo) &&
505 !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr))
506 return(&pi->ipi6_addr);
507
508 /*
509 * If the source address is not specified but the socket(if any)
510 * is already bound, use the bound address.
511 */
512 if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr))
513 return(laddr);
514
515 /*
516 * If the caller doesn't specify the source address but
517 * the outgoing interface, use an address associated with
518 * the interface.
519 */
520 if (pi && pi->ipi6_ifindex) {
521 /* XXX boundary check is assumed to be already done. */
522 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
523 dst);
524 if (ia6 == 0) {
525 *errorp = EADDRNOTAVAIL;
526 return(0);
527 }
528 return(&satosin6(&ia6->ia_addr)->sin6_addr);
529 }
530
531 /*
532 * If the destination address is a link-local unicast address or
533 * a multicast address, and if the outgoing interface is specified
534 * by the sin6_scope_id filed, use an address associated with the
535 * interface.
536 * XXX: We're now trying to define more specific semantics of
537 * sin6_scope_id field, so this part will be rewritten in
538 * the near future.
539 */
540 if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) &&
541 dstsock->sin6_scope_id) {
542 /*
543 * I'm not sure if boundary check for scope_id is done
544 * somewhere...
545 */
546 if (dstsock->sin6_scope_id < 0 ||
547 if_index < dstsock->sin6_scope_id) {
548 *errorp = ENXIO; /* XXX: better error? */
549 return(0);
550 }
551 ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id],
552 dst);
553 if (ia6 == 0) {
554 *errorp = EADDRNOTAVAIL;
555 return(0);
556 }
557 return(&satosin6(&ia6->ia_addr)->sin6_addr);
558 }
559
560 /*
561 * If the destination address is a multicast address and
562 * the outgoing interface for the address is specified
563 * by the caller, use an address associated with the interface.
564 * There is a sanity check here; if the destination has node-local
565 * scope, the outgoing interfacde should be a loopback address.
566 * Even if the outgoing interface is not specified, we also
567 * choose a loopback interface as the outgoing interface.
568 */
569 if (IN6_IS_ADDR_MULTICAST(dst)) {
570 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
571
572 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
573 ifp = &loif[0];
574 }
575
576 if (ifp) {
577 ia6 = in6_ifawithscope(ifp, dst);
578 if (ia6 == 0) {
579 *errorp = EADDRNOTAVAIL;
580 return(0);
581 }
582 return(&satosin6(&ia6->ia_addr)->sin6_addr);
583 }
584 }
585
586 /*
587 * If the next hop address for the packet is specified
588 * by caller, use an address associated with the route
589 * to the next hop.
590 */
591 {
592 struct sockaddr_in6 *sin6_next;
593 struct rtentry *rt;
594
595 if (opts && opts->ip6po_nexthop) {
596 sin6_next = satosin6(opts->ip6po_nexthop);
597 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
598 if (rt) {
599 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
600 if (ia6 == 0)
601 ia6 = ifatoia6(rt->rt_ifa);
602 }
603 if (ia6 == 0) {
604 *errorp = EADDRNOTAVAIL;
605 return(0);
606 }
607 return(&satosin6(&ia6->ia_addr)->sin6_addr);
608 }
609 }
610
611 /*
612 * If route is known or can be allocated now,
613 * our src addr is taken from the i/f, else punt.
614 */
615 if (ro) {
616 if (ro->ro_rt &&
617 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
618 RTFREE(ro->ro_rt);
619 ro->ro_rt = (struct rtentry *)0;
620 }
621 if (ro->ro_rt == (struct rtentry *)0 ||
622 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
623 /* No route yet, so try to acquire one */
624 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
625 ro->ro_dst.sin6_family = AF_INET6;
626 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
627 ro->ro_dst.sin6_addr = *dst;
628 if (IN6_IS_ADDR_MULTICAST(dst)) {
629 ro->ro_rt = rtalloc1(&((struct route *)ro)
630 ->ro_dst, 0);
631 } else {
632 rtalloc((struct route *)ro);
633 }
634
635 }
636
637 /*
638 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
639 * the address. But we don't know why it does so.
640 * It is necessary to ensure the scope even for lo0
641 * so doesn't check out IFF_LOOPBACK.
642 */
643
644 if (ro->ro_rt) {
645 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
646 if (ia6 == 0) /* xxx scope error ?*/
647 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
648 }
649 #if 0
650 /*
651 * xxx The followings are necessary? (kazu)
652 * I don't think so.
653 * It's for SO_DONTROUTE option in IPv4.(jinmei)
654 */
655 if (ia6 == 0) {
656 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
657
658 sin6->sin6_addr = *dst;
659
660 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
661 if (ia6 == 0)
662 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
663 if (ia6 == 0)
664 return(0);
665 return(&satosin6(&ia6->ia_addr)->sin6_addr);
666 }
667 #endif /* 0 */
668 if (ia6 == 0) {
669 *errorp = EHOSTUNREACH; /* no route */
670 return(0);
671 }
672 return(&satosin6(&ia6->ia_addr)->sin6_addr);
673 }
674
675 *errorp = EADDRNOTAVAIL;
676 return(0);
677 }
678
679 /*
680 * Default hop limit selection. The precedence is as follows:
681 * 1. Hoplimit valued specified via ioctl.
682 * 2. (If the outgoing interface is detected) the current
683 * hop limit of the interface specified by router advertisement.
684 * 3. The system default hoplimit.
685 */
686 int
687 in6_selecthlim(in6p, ifp)
688 struct in6pcb *in6p;
689 struct ifnet *ifp;
690 {
691 if (in6p && in6p->in6p_hops >= 0)
692 return(in6p->in6p_hops);
693 else if (ifp)
694 return(nd_ifinfo[ifp->if_index].chlim);
695 else
696 return(ip6_defhlim);
697 }
698
699 void
700 in6_pcbdisconnect(in6p)
701 struct in6pcb *in6p;
702 {
703 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
704 in6p->in6p_fport = 0;
705 if (in6p->in6p_socket->so_state & SS_NOFDREF)
706 in6_pcbdetach(in6p);
707 }
708
709 void
710 in6_pcbdetach(in6p)
711 struct in6pcb *in6p;
712 {
713 struct socket *so = in6p->in6p_socket;
714
715 #ifdef IPSEC
716 if (sotoin6pcb(so) != 0)
717 key_freeso(so);
718 ipsec6_delete_pcbpolicy(in6p);
719 #endif /* IPSEC */
720 sotoin6pcb(so) = 0;
721 sofree(so);
722 if (in6p->in6p_options)
723 m_freem(in6p->in6p_options);
724 if (in6p->in6p_outputopts) {
725 if (in6p->in6p_outputopts->ip6po_rthdr &&
726 in6p->in6p_outputopts->ip6po_route.ro_rt)
727 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
728 if (in6p->in6p_outputopts->ip6po_m)
729 (void)m_free(in6p->in6p_outputopts->ip6po_m);
730 free(in6p->in6p_outputopts, M_IP6OPT);
731 }
732 if (in6p->in6p_route.ro_rt)
733 rtfree(in6p->in6p_route.ro_rt);
734 ip6_freemoptions(in6p->in6p_moptions);
735 #if 0
736 remque(in6p);
737 #else
738 in6p->in6p_next->in6p_prev = in6p->in6p_prev;
739 in6p->in6p_prev->in6p_next = in6p->in6p_next;
740 in6p->in6p_prev = NULL;
741 #endif
742 FREE(in6p, M_PCB);
743 }
744
745 void
746 in6_setsockaddr(in6p, nam)
747 struct in6pcb *in6p;
748 struct mbuf *nam;
749 {
750 struct sockaddr_in6 *sin6;
751
752 nam->m_len = sizeof(*sin6);
753 sin6 = mtod(nam, struct sockaddr_in6 *);
754 bzero((caddr_t)sin6, sizeof(*sin6));
755 sin6->sin6_family = AF_INET6;
756 sin6->sin6_len = sizeof(struct sockaddr_in6);
757 sin6->sin6_port = in6p->in6p_lport;
758 sin6->sin6_addr = in6p->in6p_laddr;
759 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
760 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
761 else
762 sin6->sin6_scope_id = 0; /*XXX*/
763 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
764 sin6->sin6_addr.s6_addr16[1] = 0;
765 }
766
767 void
768 in6_setpeeraddr(in6p, nam)
769 struct in6pcb *in6p;
770 struct mbuf *nam;
771 {
772 struct sockaddr_in6 *sin6;
773
774 nam->m_len = sizeof(*sin6);
775 sin6 = mtod(nam, struct sockaddr_in6 *);
776 bzero((caddr_t)sin6, sizeof(*sin6));
777 sin6->sin6_family = AF_INET6;
778 sin6->sin6_len = sizeof(struct sockaddr_in6);
779 sin6->sin6_port = in6p->in6p_fport;
780 sin6->sin6_addr = in6p->in6p_faddr;
781 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
782 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
783 else
784 sin6->sin6_scope_id = 0; /*XXX*/
785 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
786 sin6->sin6_addr.s6_addr16[1] = 0;
787 }
788
789 /*
790 * Pass some notification to all connections of a protocol
791 * associated with address dst. The local address and/or port numbers
792 * may be specified to limit the search. The "usual action" will be
793 * taken, depending on the ctlinput cmd. The caller must filter any
794 * cmds that are uninteresting (e.g., no error in the map).
795 * Call the protocol specific routine (if any) to report
796 * any errors for each matching socket.
797 *
798 * Must be called at splsoftnet.
799 */
800 int
801 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify)
802 struct in6pcb *head;
803 struct sockaddr *dst;
804 u_int fport_arg, lport_arg;
805 struct in6_addr *laddr6;
806 int cmd;
807 void (*notify) __P((struct in6pcb *, int));
808 {
809 struct in6pcb *in6p, *oin6p;
810 struct in6_addr faddr6;
811 u_short fport = fport_arg, lport = lport_arg;
812 int errno;
813 int nmatch = 0;
814
815 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
816 return 0;
817 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
818 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6))
819 return 0;
820
821 /*
822 * Redirects go to all references to the destination,
823 * and use in_rtchange to invalidate the route cache.
824 * Dead host indications: notify all references to the destination.
825 * Otherwise, if we have knowledge of the local port and address,
826 * deliver only to that socket.
827 */
828 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
829 fport = 0;
830 lport = 0;
831 bzero((caddr_t)laddr6, sizeof(*laddr6));
832 if (cmd != PRC_HOSTDEAD)
833 notify = in6_rtchange;
834 }
835 if (notify == NULL)
836 return 0;
837 errno = inet6ctlerrmap[cmd];
838 for (in6p = head->in6p_next; in6p != head;) {
839 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,&faddr6) ||
840 in6p->in6p_socket == 0 ||
841 (lport && in6p->in6p_lport != lport) ||
842 (!IN6_IS_ADDR_UNSPECIFIED(laddr6) &&
843 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) ||
844 (fport && in6p->in6p_fport != fport)) {
845 in6p = in6p->in6p_next;
846 continue;
847 }
848 oin6p = in6p;
849 in6p = in6p->in6p_next;
850 (*notify)(oin6p, errno);
851 nmatch++;
852 }
853 return nmatch;
854 }
855
856 /*
857 * Check for alternatives when higher level complains
858 * about service problems. For now, invalidate cached
859 * routing information. If the route was created dynamically
860 * (by a redirect), time to try a default gateway again.
861 */
862 void
863 in6_losing(in6p)
864 struct in6pcb *in6p;
865 {
866 struct rtentry *rt;
867 struct rt_addrinfo info;
868
869 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
870 in6p->in6p_route.ro_rt = 0;
871 bzero((caddr_t)&info, sizeof(info));
872 info.rti_info[RTAX_DST] =
873 (struct sockaddr *)&in6p->in6p_route.ro_dst;
874 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
875 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
876 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
877 if (rt->rt_flags & RTF_DYNAMIC)
878 (void)rtrequest(RTM_DELETE, rt_key(rt),
879 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
880 (struct rtentry **)0);
881 else
882 /*
883 * A new route can be allocated
884 * the next time output is attempted.
885 */
886 rtfree(rt);
887 }
888 }
889
890 /*
891 * After a routing change, flush old routing
892 * and allocate a (hopefully) better one.
893 */
894 void
895 in6_rtchange(in6p, errno)
896 struct in6pcb *in6p;
897 int errno;
898 {
899 if (in6p->in6p_route.ro_rt) {
900 rtfree(in6p->in6p_route.ro_rt);
901 in6p->in6p_route.ro_rt = 0;
902 /*
903 * A new route can be allocated the next time
904 * output is attempted.
905 */
906 }
907 }
908
909 struct in6pcb *
910 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
911 struct in6pcb *head;
912 struct in6_addr *faddr6, *laddr6;
913 u_int fport_arg, lport_arg;
914 int flags;
915 {
916 struct in6pcb *in6p, *match = 0;
917 int matchwild = 3, wildcard;
918 u_short fport = fport_arg, lport = lport_arg;
919
920 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
921 if (in6p->in6p_lport != lport)
922 continue;
923 wildcard = 0;
924 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
925 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
926 wildcard++;
927 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
928 continue;
929 }
930 #ifndef TCP6
931 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
932 && in6p->in6p_laddr.s6_addr32[3] == 0) {
933 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
934 continue;
935 if (laddr6->s6_addr32[3] == 0)
936 ;
937 else
938 wildcard++;
939 }
940 #endif
941 else {
942 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
943 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
944 if (in6p->in6p_flags & IN6P_BINDV6ONLY)
945 continue;
946 else
947 wildcard++;
948 #else
949 continue;
950 #endif
951 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
952 wildcard++;
953 }
954 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
955 if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
956 wildcard++;
957 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
958 || in6p->in6p_fport != fport)
959 continue;
960 }
961 #ifndef TCP6
962 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
963 && in6p->in6p_faddr.s6_addr32[3] == 0) {
964 if (!IN6_IS_ADDR_V4MAPPED(faddr6))
965 continue;
966 if (faddr6->s6_addr32[3] == 0)
967 ;
968 else
969 wildcard++;
970 }
971 #endif
972 else {
973 if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
974 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
975 if (in6p->in6p_flags & IN6P_BINDV6ONLY)
976 continue;
977 else
978 wildcard++;
979 #else
980 continue;
981 #endif
982 } else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
983 wildcard++;
984 }
985
986 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
987 continue;
988 if (wildcard < matchwild) {
989 match = in6p;
990 matchwild = wildcard;
991 if (matchwild == 0)
992 break;
993 }
994 }
995 return(match);
996 }
997
998 #ifndef TCP6
999 struct rtentry *
1000 in6_pcbrtentry(in6p)
1001 struct in6pcb *in6p;
1002 {
1003 struct route_in6 *ro;
1004
1005 ro = &in6p->in6p_route;
1006
1007 if (ro->ro_rt == NULL) {
1008 /*
1009 * No route yet, so try to acquire one.
1010 */
1011 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
1012 bzero(&ro->ro_dst, sizeof(ro->ro_dst));
1013 ro->ro_dst.sin6_family = AF_INET6;
1014 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
1015 satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
1016 rtalloc((struct route *)ro);
1017 }
1018 }
1019 return (ro->ro_rt);
1020 }
1021
1022 struct in6pcb *
1023 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
1024 struct in6pcb *head;
1025 struct in6_addr *faddr6, *laddr6;
1026 u_int fport_arg, lport_arg;
1027 int faith;
1028 {
1029 struct in6pcb *in6p;
1030 u_short fport = fport_arg, lport = lport_arg;
1031
1032 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
1033 #if defined(NFAITH) && NFAITH > 0
1034 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1035 continue;
1036 #endif
1037 /* find exact match on both source and dest */
1038 if (in6p->in6p_fport != fport)
1039 continue;
1040 if (in6p->in6p_lport != lport)
1041 continue;
1042 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
1043 continue;
1044 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
1045 continue;
1046 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
1047 continue;
1048 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1049 continue;
1050 return in6p;
1051 }
1052 return NULL;
1053 }
1054
1055 struct in6pcb *
1056 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
1057 struct in6pcb *head;
1058 struct in6_addr *laddr6;
1059 u_int lport_arg;
1060 int faith;
1061 {
1062 struct in6pcb *in6p, *match;
1063 u_short lport = lport_arg;
1064
1065 match = NULL;
1066 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
1067 /*
1068 * find destination match. exact match is preferred
1069 * against wildcard match.
1070 */
1071 #if defined(NFAITH) && NFAITH > 0
1072 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1073 continue;
1074 #endif
1075 if (in6p->in6p_fport != 0)
1076 continue;
1077 if (in6p->in6p_lport != lport)
1078 continue;
1079 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
1080 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1081 #ifndef INET6_BINDV6ONLY
1082 if (in6p->in6p_flags & IN6P_BINDV6ONLY)
1083 continue;
1084 else
1085 match = in6p;
1086 #else
1087 continue;
1088 #endif
1089 } else
1090 match = in6p;
1091 }
1092 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
1093 in6p->in6p_laddr.s6_addr32[3] == 0) {
1094 if (IN6_IS_ADDR_V4MAPPED(laddr6)
1095 && laddr6->s6_addr32[3] != 0)
1096 match = in6p;
1097 }
1098 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1099 return in6p;
1100 }
1101 return match;
1102 }
1103 #endif
1104