in6_pcb.c revision 1.9 1 /* $NetBSD: in6_pcb.c,v 1.9 1999/07/31 18:41:16 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 #ifdef __NetBSD__ /*XXX*/
68 #include "opt_ipsec.h"
69 #endif
70
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/malloc.h>
74 #include <sys/mbuf.h>
75 #include <sys/protosw.h>
76 #include <sys/socket.h>
77 #include <sys/socketvar.h>
78 #include <sys/ioctl.h>
79 #include <sys/errno.h>
80 #include <sys/time.h>
81 #include <sys/proc.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet/in_systm.h>
89 #include <netinet/ip.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet6/ip6.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/nd6.h>
95
96 #include "loop.h"
97 #ifdef __NetBSD__
98 extern struct ifnet loif[NLOOP];
99 #endif
100 #include "faith.h"
101
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
105 #include <netkey/key_debug.h>
106 #endif /* IPSEC */
107
108 struct in6_addr zeroin6_addr;
109
110 int
111 in6_pcballoc(so, head)
112 struct socket *so;
113 struct in6pcb *head;
114 {
115 struct in6pcb *in6p;
116
117 MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
118 if (in6p == NULL)
119 return(ENOBUFS);
120 bzero((caddr_t)in6p, sizeof(*in6p));
121 in6p->in6p_head = head;
122 in6p->in6p_socket = so;
123 in6p->in6p_hops = -1; /* use kernel default */
124 in6p->in6p_icmp6filt = NULL;
125 #if 0
126 insque(in6p, head);
127 #else
128 in6p->in6p_next = head->in6p_next;
129 head->in6p_next = in6p;
130 in6p->in6p_prev = head;
131 in6p->in6p_next->in6p_prev = in6p;
132 #endif
133 so->so_pcb = (caddr_t)in6p;
134 return(0);
135 }
136
137 int
138 in6_pcbbind(in6p, nam)
139 register struct in6pcb *in6p;
140 struct mbuf *nam;
141 {
142 struct socket *so = in6p->in6p_socket;
143 struct in6pcb *head = in6p->in6p_head;
144 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
145 struct proc *p = curproc; /* XXX */
146 u_short lport = 0;
147 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
148 int error;
149
150 if (in6p->in6p_lport || !IN6_IS_ADDR_ANY(&in6p->in6p_laddr))
151 return(EINVAL);
152 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
153 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
154 (so->so_options & SO_ACCEPTCONN) == 0))
155 wild = IN6PLOOKUP_WILDCARD;
156 if (nam) {
157 sin6 = mtod(nam, struct sockaddr_in6 *);
158 if (nam->m_len != sizeof(*sin6))
159 return(EINVAL);
160 /*
161 * We should check the family, but old programs
162 * incorrectly fail to intialize it.
163 */
164 if (sin6->sin6_family != AF_INET6)
165 return(EAFNOSUPPORT);
166
167 /*
168 * If the scope of the destination is link-local, embed the
169 * interface index in the address.
170 */
171 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
172 /* XXX boundary check is assumed to be already done. */
173 /* XXX sin6_scope_id is weaker than advanced-api. */
174 struct in6_pktinfo *pi;
175 if (in6p->in6p_outputopts &&
176 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
177 pi->ipi6_ifindex) {
178 sin6->sin6_addr.s6_addr16[1]
179 = htons(pi->ipi6_ifindex);
180 } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)
181 && in6p->in6p_moptions
182 && in6p->in6p_moptions->im6o_multicast_ifp) {
183 sin6->sin6_addr.s6_addr16[1] =
184 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
185 } else if (sin6->sin6_scope_id) {
186 /* boundary check */
187 if (sin6->sin6_scope_id < 0
188 || if_index < sin6->sin6_scope_id) {
189 return ENXIO; /* XXX EINVAL? */
190 }
191 sin6->sin6_addr.s6_addr16[1]
192 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
193 /* this must be cleared for ifa_ifwithaddr() */
194 sin6->sin6_scope_id = 0;
195 }
196 }
197
198 lport = sin6->sin6_port;
199 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
200 /*
201 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
202 * allow compepte duplication of binding if
203 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
204 * and a multicast address is bound on both
205 * new and duplicated sockets.
206 */
207 if (so->so_options & SO_REUSEADDR)
208 reuseport = SO_REUSEADDR|SO_REUSEPORT;
209 } else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
210 struct sockaddr_in sin;
211
212 bzero(&sin, sizeof(sin));
213 sin.sin_len = sizeof(sin);
214 sin.sin_family = AF_INET;
215 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
216 sizeof(sin.sin_addr));
217 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
218 return EADDRNOTAVAIL;
219 } else if (!IN6_IS_ADDR_ANY(&sin6->sin6_addr)) {
220 struct ifaddr *ia = NULL;
221
222 sin6->sin6_port = 0; /* yech... */
223 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
224 return(EADDRNOTAVAIL);
225
226 /*
227 * XXX: bind to an anycast address might accidentally
228 * cause sending a packet with anycast source address.
229 */
230 if (ia &&
231 ((struct in6_ifaddr *)ia)->ia6_flags &
232 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
233 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
234 return(EADDRNOTAVAIL);
235 }
236 }
237 if (lport) {
238 /* GROSS */
239 if (ntohs(lport) < IPV6PORT_RESERVED &&
240 (error = suser(p->p_ucred, &p->p_acflag)))
241 return(EACCES);
242
243 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
244 /* should check this but we can't ... */
245 #if 0
246 struct inpcb *t;
247
248 t = in_pcblookup_bind(&tcbtable,
249 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
250 lport);
251 if (t && (reuseport & t->inp_socket->so_options) == 0)
252 return EADDRINUSE;
253 #endif
254 } else {
255 struct in6pcb *t;
256
257 t = in6_pcblookup(head, &zeroin6_addr, 0,
258 &sin6->sin6_addr, lport, wild);
259 if (t && (reuseport & t->in6p_socket->so_options) == 0)
260 return(EADDRINUSE);
261 }
262 }
263 in6p->in6p_laddr = sin6->sin6_addr;
264 }
265 if (lport == 0) {
266 u_short last_port;
267 void *t;
268
269 /* XXX IN6P_LOWPORT */
270
271 /* value out of range */
272 if (head->in6p_lport < IPV6PORT_ANONMIN)
273 head->in6p_lport = IPV6PORT_ANONMIN;
274 else if (head->in6p_lport > IPV6PORT_ANONMAX)
275 head->in6p_lport = IPV6PORT_ANONMIN;
276 last_port = head->in6p_lport;
277 goto startover; /*to randomize*/
278 for (;;) {
279 lport = htons(head->in6p_lport);
280 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
281 #if 0
282 t = in_pcblookup_bind(&tcbtable,
283 (struct in_addr *)&in6p->in6p_laddr.s6_addr32[3],
284 lport);
285 #else
286 t = NULL;
287 #endif
288 } else {
289 t = in6_pcblookup(head, &zeroin6_addr, 0,
290 &in6p->in6p_laddr, lport, wild);
291 }
292 if (t == 0)
293 break;
294 startover:
295 if (head->in6p_lport >= IPV6PORT_ANONMAX)
296 head->in6p_lport = IPV6PORT_ANONMIN;
297 else
298 head->in6p_lport++;
299 if (head->in6p_lport == last_port)
300 return (EADDRINUSE);
301 }
302 }
303 in6p->in6p_lport = lport;
304 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
305 return(0);
306 }
307
308 /*
309 * Connect from a socket to a specified address.
310 * Both address and port must be specified in argument sin6.
311 * If don't have a local address for this socket yet,
312 * then pick one.
313 */
314 int
315 in6_pcbconnect(in6p, nam)
316 struct in6pcb *in6p;
317 struct mbuf *nam;
318 {
319 struct in6_addr *in6a = NULL;
320 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
321 struct in6_pktinfo *pi;
322 struct ifnet *ifp = NULL; /* outgoing interface */
323 int error = 0;
324 struct in6_addr mapped;
325
326 (void)&in6a; /* XXX fool gcc */
327
328 if (nam->m_len != sizeof(*sin6))
329 return(EINVAL);
330 if (sin6->sin6_family != AF_INET6)
331 return(EAFNOSUPPORT);
332 if (sin6->sin6_port == 0)
333 return(EADDRNOTAVAIL);
334
335 /* sanity check for mapped address case */
336 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
337 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
338 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
339 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
340 return EINVAL;
341 } else {
342 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
343 return EINVAL;
344 }
345
346 /*
347 * If the scope of the destination is link-local, embed the interface
348 * index in the address.
349 */
350 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) {
351 /* XXX boundary check is assumed to be already done. */
352 /* XXX sin6_scope_id is weaker than advanced-api. */
353 if (in6p->in6p_outputopts &&
354 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
355 pi->ipi6_ifindex) {
356 sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex);
357 ifp = ifindex2ifnet[pi->ipi6_ifindex];
358 }
359 else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) &&
360 in6p->in6p_moptions &&
361 in6p->in6p_moptions->im6o_multicast_ifp) {
362 sin6->sin6_addr.s6_addr16[1] =
363 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
364 ifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
365 } else if (sin6->sin6_scope_id) {
366 /* boundary check */
367 if (sin6->sin6_scope_id < 0
368 || if_index < sin6->sin6_scope_id) {
369 return ENXIO; /* XXX EINVAL? */
370 }
371 sin6->sin6_addr.s6_addr16[1]
372 = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/
373 ifp = ifindex2ifnet[sin6->sin6_scope_id];
374 }
375 }
376
377 /* Source address selection. */
378 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
379 && in6p->in6p_laddr.s6_addr32[3] == 0) {
380 struct sockaddr_in sin, *sinp;
381
382 bzero(&sin, sizeof(sin));
383 sin.sin_len = sizeof(sin);
384 sin.sin_family = AF_INET;
385 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
386 sizeof(sin.sin_addr));
387 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
388 in6p->in6p_socket->so_options, NULL, &error);
389 if (sinp == 0) {
390 if (error == 0)
391 error = EADDRNOTAVAIL;
392 return(error);
393 }
394 bzero(&mapped, sizeof(mapped));
395 mapped.s6_addr16[5] = htons(0xffff);
396 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
397 in6a = &mapped;
398 } else if (IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
399 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
400 in6p->in6p_moptions, &in6p->in6p_route,
401 &error);
402 if (in6a == 0) {
403 if (error == 0)
404 error = EADDRNOTAVAIL;
405 return(error);
406 }
407 }
408 if (in6p->in6p_route.ro_rt)
409 ifp = in6p->in6p_route.ro_rt->rt_ifp;
410
411 /*
412 * Default hop limit selection. If a hoplimit was specified via ioctl,
413 * use it. Else if the outgoing interface is detected and the current
414 * hop limit of the interface was specified by router advertisement,
415 * use the value.
416 * Otherwise, use the system default hoplimit.
417 */
418 if (in6p->in6p_hops >= 0)
419 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6p->in6p_hops;
420 else if (ifp)
421 in6p->in6p_ip6.ip6_hlim = nd_ifinfo[ifp->if_index].chlim;
422 else
423 in6p->in6p_ip6.ip6_hlim = ip6_defhlim;
424
425 if (in6_pcblookup(in6p->in6p_head,
426 &sin6->sin6_addr,
427 sin6->sin6_port,
428 IN6_IS_ADDR_ANY(&in6p->in6p_laddr) ?
429 in6a : &in6p->in6p_laddr,
430 in6p->in6p_lport,
431 0))
432 return(EADDRINUSE);
433 if (IN6_IS_ADDR_ANY(&in6p->in6p_laddr)
434 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
435 && in6p->in6p_laddr.s6_addr32[3] == 0)) {
436 if (in6p->in6p_lport == 0)
437 (void)in6_pcbbind(in6p, (struct mbuf *)0);
438 in6p->in6p_laddr = *in6a;
439 }
440 in6p->in6p_faddr = sin6->sin6_addr;
441 in6p->in6p_fport = sin6->sin6_port;
442 /*
443 * xxx kazu flowlabel is necessary for connect?
444 * but if this line is missing, the garbage value remains.
445 */
446 in6p->in6p_flowinfo = sin6->sin6_flowinfo;
447 return(0);
448 }
449
450 /*
451 * Return an IPv6 address, which is the most appropriate for given
452 * destination and user specified options.
453 * If necessary, this function lookups the routing table and return
454 * an entry to the caller for later use.
455 */
456 struct in6_addr *
457 in6_selectsrc(dstsock, opts, mopts, ro, errorp)
458 struct sockaddr_in6 *dstsock;
459 struct ip6_pktopts *opts;
460 struct ip6_moptions *mopts;
461 struct route_in6 *ro;
462 int *errorp;
463 {
464 struct in6_addr *dst;
465 struct in6_ifaddr *ia6 = 0;
466 struct in6_pktinfo *pi;
467
468 dst = &dstsock->sin6_addr;
469 *errorp = 0;
470
471 /*
472 * If the source address is explicitly specified by the caller,
473 * use it.
474 * If the caller doesn't specify the source address but
475 * the outgoing interface, use an address associated with
476 * the interface.
477 */
478 if (opts && (pi = opts->ip6po_pktinfo)) {
479 if (!IN6_IS_ADDR_ANY(&pi->ipi6_addr))
480 return(&pi->ipi6_addr);
481 else if (pi->ipi6_ifindex) {
482 /* XXX boundary check is assumed to be already done. */
483 ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex],
484 dst);
485 if (ia6 == 0) {
486 *errorp = EADDRNOTAVAIL;
487 return(0);
488 }
489 return(&satosin6(&ia6->ia_addr)->sin6_addr);
490 }
491 }
492
493 /*
494 * If the destination address is a multicast address and
495 * the outgoing interface for the address is specified
496 * by the caller, use an address associated with the interface.
497 * There is a sanity check here; if the destination has node-local
498 * scope, the outgoing interfacde should be a loopback address.
499 * Even if the outgoing interface is not specified, we also
500 * choose a loopback interface as the outgoing interface.
501 */
502 if (IN6_IS_ADDR_MULTICAST(dst)) {
503 struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL;
504 #ifdef __bsdi__
505 extern struct ifnet loif;
506 #endif
507
508 if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) {
509 #ifdef __bsdi__
510 ifp = &loif;
511 #else
512 ifp = &loif[0];
513 #endif
514 }
515
516 if (ifp) {
517 ia6 = in6_ifawithscope(ifp, dst);
518 if (ia6 == 0) {
519 *errorp = EADDRNOTAVAIL;
520 return(0);
521 }
522 return(&satosin6(&ia6->ia_addr)->sin6_addr);
523 }
524 }
525
526 /*
527 * XXX How should we use sin6_scope_id???
528 */
529
530 /*
531 * If the next hop address for the packet is specified
532 * by caller, use an address associated with the route
533 * to the next hop.
534 */
535 {
536 struct sockaddr_in6 *sin6_next;
537 struct rtentry *rt;
538
539 if (opts && opts->ip6po_nexthop) {
540 sin6_next = satosin6(opts->ip6po_nexthop);
541 rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL);
542 if (rt) {
543 ia6 = in6_ifawithscope(rt->rt_ifp, dst);
544 if (ia6 == 0)
545 ia6 = ifatoia6(rt->rt_ifa);
546 }
547 if (ia6 == 0) {
548 *errorp = EADDRNOTAVAIL;
549 return(0);
550 }
551 return(&satosin6(&ia6->ia_addr)->sin6_addr);
552 }
553 }
554
555 /*
556 * If route is known or can be allocated now,
557 * our src addr is taken from the i/f, else punt.
558 */
559 if (ro) {
560 if (ro->ro_rt &&
561 !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) {
562 RTFREE(ro->ro_rt);
563 ro->ro_rt = (struct rtentry *)0;
564 }
565 if (ro->ro_rt == (struct rtentry *)0 ||
566 ro->ro_rt->rt_ifp == (struct ifnet *)0) {
567 /* No route yet, so try to acquire one */
568 bzero(&ro->ro_dst, sizeof(struct sockaddr_in6));
569 ro->ro_dst.sin6_family = AF_INET6;
570 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
571 ro->ro_dst.sin6_addr = *dst;
572 if (IN6_IS_ADDR_MULTICAST(dst)) {
573 #ifdef __FreeBSD__
574 ro->ro_rt = rtalloc1(&((struct route *)ro)
575 ->ro_dst, 0, 0UL);
576 #endif /*__FreeBSD__*/
577 #if defined(__bsdi__) || defined(__NetBSD__)
578 ro->ro_rt = rtalloc1(&((struct route *)ro)
579 ->ro_dst, 0);
580 #endif /*__bsdi__*/
581 } else {
582 #if 0 /* XXX Is this correct? */
583 rtcalloc((struct route *)ro);
584 #else
585 rtalloc((struct route *)ro);
586 #endif
587 }
588 }
589
590 /*
591 * in_pcbconnect() checks out IFF_LOOPBACK to skip using
592 * the address. But we don't know why it does so.
593 * It is necessary to ensure the scope even for lo0
594 * so doesn't check out IFF_LOOPBACK.
595 */
596
597 if (ro->ro_rt) {
598 ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst);
599 if (ia6 == 0) /* xxx scope error ?*/
600 ia6 = ifatoia6(ro->ro_rt->rt_ifa);
601 }
602 #if 0
603 /*
604 * xxx The followings are necessary? (kazu)
605 * I don't think so.
606 * It's for SO_DONTROUTE option in IPv4.(jinmei)
607 */
608 if (ia6 == 0) {
609 struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0};
610
611 sin6->sin6_addr = *dst;
612
613 ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6)));
614 if (ia6 == 0)
615 ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6)));
616 if (ia6 == 0)
617 return(0);
618 return(&satosin6(&ia6->ia_addr)->sin6_addr);
619 }
620 #endif /* 0 */
621 if (ia6 == 0) {
622 *errorp = EHOSTUNREACH; /* no route */
623 return(0);
624 }
625 return(&satosin6(&ia6->ia_addr)->sin6_addr);
626 }
627
628 *errorp = EADDRNOTAVAIL;
629 return(0);
630 }
631
632 void
633 in6_pcbdisconnect(in6p)
634 struct in6pcb *in6p;
635 {
636 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
637 in6p->in6p_fport = 0;
638 if (in6p->in6p_socket->so_state & SS_NOFDREF)
639 in6_pcbdetach(in6p);
640 }
641
642 void
643 in6_pcbdetach(in6p)
644 struct in6pcb *in6p;
645 {
646 struct socket *so = in6p->in6p_socket;
647
648 #ifdef IPSEC
649 if (sotoin6pcb(so) != 0)
650 key_freeso(so);
651 ipsec6_delete_pcbpolicy(in6p);
652 #endif /* IPSEC */
653 sotoin6pcb(so) = 0;
654 sofree(so);
655 if (in6p->in6p_options)
656 m_freem(in6p->in6p_options);
657 if (in6p->in6p_outputopts) {
658 if (in6p->in6p_outputopts->ip6po_rthdr &&
659 in6p->in6p_outputopts->ip6po_route.ro_rt)
660 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
661 if (in6p->in6p_outputopts->ip6po_m)
662 (void)m_free(in6p->in6p_outputopts->ip6po_m);
663 free(in6p->in6p_outputopts, M_IP6OPT);
664 }
665 if (in6p->in6p_route.ro_rt)
666 rtfree(in6p->in6p_route.ro_rt);
667 ip6_freemoptions(in6p->in6p_moptions);
668 #if 0
669 remque(in6p);
670 #else
671 in6p->in6p_next->in6p_prev = in6p->in6p_prev;
672 in6p->in6p_prev->in6p_next = in6p->in6p_next;
673 in6p->in6p_prev = NULL;
674 #endif
675 FREE(in6p, M_PCB);
676 }
677
678 void
679 in6_setsockaddr(in6p, nam)
680 struct in6pcb *in6p;
681 struct mbuf *nam;
682 {
683 struct sockaddr_in6 *sin6;
684
685 nam->m_len = sizeof(*sin6);
686 sin6 = mtod(nam, struct sockaddr_in6 *);
687 bzero((caddr_t)sin6, sizeof(*sin6));
688 sin6->sin6_family = AF_INET6;
689 sin6->sin6_len = sizeof(struct sockaddr_in6);
690 sin6->sin6_port = in6p->in6p_lport;
691 sin6->sin6_addr = in6p->in6p_laddr;
692 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
693 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
694 else
695 sin6->sin6_scope_id = 0; /*XXX*/
696 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
697 sin6->sin6_addr.s6_addr16[1] = 0;
698 }
699
700 void
701 in6_setpeeraddr(in6p, nam)
702 struct in6pcb *in6p;
703 struct mbuf *nam;
704 {
705 struct sockaddr_in6 *sin6;
706
707 nam->m_len = sizeof(*sin6);
708 sin6 = mtod(nam, struct sockaddr_in6 *);
709 bzero((caddr_t)sin6, sizeof(*sin6));
710 sin6->sin6_family = AF_INET6;
711 sin6->sin6_len = sizeof(struct sockaddr_in6);
712 sin6->sin6_port = in6p->in6p_fport;
713 sin6->sin6_addr = in6p->in6p_faddr;
714 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
715 sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]);
716 else
717 sin6->sin6_scope_id = 0; /*XXX*/
718 if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr))
719 sin6->sin6_addr.s6_addr16[1] = 0;
720 }
721
722 /*
723 * Pass some notification to all connections of a protocol
724 * associated with address dst. The local address and/or port numbers
725 * may be specified to limit the search. The "usual action" will be
726 * taken, depending on the ctlinput cmd. The caller must filter any
727 * cmds that are uninteresting (e.g., no error in the map).
728 * Call the protocol specific routine (if any) to report
729 * any errors for each matching socket.
730 *
731 * Must be called at splsoftnet.
732 */
733 int
734 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify)
735 struct in6pcb *head;
736 struct sockaddr *dst;
737 u_int fport_arg, lport_arg;
738 struct in6_addr *laddr6;
739 int cmd;
740 void (*notify) __P((struct in6pcb *, int));
741 {
742 struct in6pcb *in6p, *oin6p;
743 struct in6_addr faddr6;
744 u_short fport = fport_arg, lport = lport_arg;
745 int errno;
746 int nmatch = 0;
747
748 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
749 return 0;
750 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
751 if (IN6_IS_ADDR_ANY(&faddr6))
752 return 0;
753
754 /*
755 * Redirects go to all references to the destination,
756 * and use in_rtchange to invalidate the route cache.
757 * Dead host indications: notify all references to the destination.
758 * Otherwise, if we have knowledge of the local port and address,
759 * deliver only to that socket.
760 */
761 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
762 fport = 0;
763 lport = 0;
764 bzero((caddr_t)laddr6, sizeof(*laddr6));
765 if (cmd != PRC_HOSTDEAD)
766 notify = in6_rtchange;
767 }
768 if (notify == NULL)
769 return 0;
770 errno = inet6ctlerrmap[cmd];
771 for (in6p = head->in6p_next; in6p != head;) {
772 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,&faddr6) ||
773 in6p->in6p_socket == 0 ||
774 (lport && in6p->in6p_lport != lport) ||
775 (!IN6_IS_ADDR_ANY(laddr6) &&
776 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) ||
777 (fport && in6p->in6p_fport != fport)) {
778 in6p = in6p->in6p_next;
779 continue;
780 }
781 oin6p = in6p;
782 in6p = in6p->in6p_next;
783 (*notify)(oin6p, errno);
784 nmatch++;
785 }
786 return nmatch;
787 }
788
789 /*
790 * Check for alternatives when higher level complains
791 * about service problems. For now, invalidate cached
792 * routing information. If the route was created dynamically
793 * (by a redirect), time to try a default gateway again.
794 */
795 void
796 in6_losing(in6p)
797 struct in6pcb *in6p;
798 {
799 struct rtentry *rt;
800 struct rt_addrinfo info;
801
802 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
803 in6p->in6p_route.ro_rt = 0;
804 bzero((caddr_t)&info, sizeof(info));
805 info.rti_info[RTAX_DST] =
806 (struct sockaddr *)&in6p->in6p_route.ro_dst;
807 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
808 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
809 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
810 if (rt->rt_flags & RTF_DYNAMIC)
811 (void)rtrequest(RTM_DELETE, rt_key(rt),
812 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
813 (struct rtentry **)0);
814 else
815 /*
816 * A new route can be allocated
817 * the next time output is attempted.
818 */
819 rtfree(rt);
820 }
821 }
822
823 /*
824 * After a routing change, flush old routing
825 * and allocate a (hopefully) better one.
826 */
827 void
828 in6_rtchange(in6p, errno)
829 struct in6pcb *in6p;
830 int errno;
831 {
832 if (in6p->in6p_route.ro_rt) {
833 rtfree(in6p->in6p_route.ro_rt);
834 in6p->in6p_route.ro_rt = 0;
835 /*
836 * A new route can be allocated the next time
837 * output is attempted.
838 */
839 }
840 }
841
842 struct in6pcb *
843 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
844 struct in6pcb *head;
845 struct in6_addr *faddr6, *laddr6;
846 u_int fport_arg, lport_arg;
847 int flags;
848 {
849 struct in6pcb *in6p, *match = 0;
850 int matchwild = 3, wildcard;
851 u_short fport = fport_arg, lport = lport_arg;
852
853 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
854 if (in6p->in6p_lport != lport)
855 continue;
856 wildcard = 0;
857 if (!IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
858 if (IN6_IS_ADDR_ANY(laddr6))
859 wildcard++;
860 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
861 continue;
862 } else {
863 if (!IN6_IS_ADDR_ANY(laddr6))
864 wildcard++;
865 }
866 if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr)) {
867 if (IN6_IS_ADDR_ANY(faddr6))
868 wildcard++;
869 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
870 || in6p->in6p_fport != fport)
871 continue;
872 } else {
873 if (!IN6_IS_ADDR_ANY(faddr6))
874 wildcard++;
875 }
876 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
877 continue;
878 if (wildcard < matchwild) {
879 match = in6p;
880 matchwild = wildcard;
881 if (matchwild == 0)
882 break;
883 }
884 }
885 return(match);
886 }
887
888 #ifndef TCP6
889 struct rtentry *
890 in6_pcbrtentry(in6p)
891 struct in6pcb *in6p;
892 {
893 struct route_in6 *ro;
894
895 ro = &in6p->in6p_route;
896
897 if (ro->ro_rt == NULL) {
898 /*
899 * No route yet, so try to acquire one.
900 */
901 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
902 bzero(&ro->ro_dst, sizeof(ro->ro_dst));
903 ro->ro_dst.sin6_family = AF_INET6;
904 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
905 satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
906 rtalloc((struct route *)ro);
907 }
908 }
909 return (ro->ro_rt);
910 }
911
912 struct in6pcb *
913 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
914 struct in6pcb *head;
915 struct in6_addr *faddr6, *laddr6;
916 u_int fport_arg, lport_arg;
917 int faith;
918 {
919 struct in6pcb *in6p;
920 u_short fport = fport_arg, lport = lport_arg;
921
922 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
923 #if defined(NFAITH) && NFAITH > 0
924 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
925 continue;
926 #endif
927 /* find exact match on both source and dest */
928 if (in6p->in6p_fport != fport)
929 continue;
930 if (in6p->in6p_lport != lport)
931 continue;
932 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
933 continue;
934 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
935 continue;
936 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
937 continue;
938 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
939 continue;
940 return in6p;
941 }
942 return NULL;
943 }
944
945 struct in6pcb *
946 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
947 struct in6pcb *head;
948 struct in6_addr *laddr6;
949 u_int lport_arg;
950 int faith;
951 {
952 struct in6pcb *in6p, *match;
953 u_short lport = lport_arg;
954
955 match = NULL;
956 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
957 /*
958 * find destination match. exact match is preferred
959 * against wildcard match.
960 */
961 #if defined(NFAITH) && NFAITH > 0
962 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
963 continue;
964 #endif
965 if (in6p->in6p_fport != 0)
966 continue;
967 if (in6p->in6p_lport != lport)
968 continue;
969 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
970 match = in6p;
971 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
972 return in6p;
973 }
974 return match;
975 }
976 #endif
977