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