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