in6_pcb.c revision 1.30 1 /* $NetBSD: in6_pcb.c,v 1.30 2000/08/26 11:03:46 itojun Exp $ */
2 /* $KAME: in6_pcb.c,v 1.63 2000/08/26 10:00:45 itojun 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 #endif /* IPSEC */
103
104 struct in6_addr zeroin6_addr;
105
106 int ip6_anonportmin = IPV6PORT_ANONMIN;
107 int ip6_anonportmax = IPV6PORT_ANONMAX;
108 int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
109 int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
110
111 int
112 in6_pcballoc(so, head)
113 struct socket *so;
114 struct in6pcb *head;
115 {
116 struct in6pcb *in6p;
117
118 MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
119 if (in6p == NULL)
120 return(ENOBUFS);
121 bzero((caddr_t)in6p, sizeof(*in6p));
122 in6p->in6p_head = head;
123 in6p->in6p_socket = so;
124 in6p->in6p_hops = -1; /* use kernel default */
125 in6p->in6p_icmp6filt = NULL;
126 in6p->in6p_next = head->in6p_next;
127 head->in6p_next = in6p;
128 in6p->in6p_prev = head;
129 in6p->in6p_next->in6p_prev = in6p;
130 #ifndef INET6_BINDV6ONLY
131 if (ip6_bindv6only)
132 in6p->in6p_flags |= IN6P_BINDV6ONLY;
133 #else
134 in6p->in6p_flags |= IN6P_BINDV6ONLY; /*just for safety*/
135 #endif
136 so->so_pcb = (caddr_t)in6p;
137 return(0);
138 }
139
140 int
141 in6_pcbbind(in6p, nam, p)
142 register struct in6pcb *in6p;
143 struct mbuf *nam;
144 struct proc *p;
145 {
146 struct socket *so = in6p->in6p_socket;
147 struct in6pcb *head = in6p->in6p_head;
148 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
149 u_int16_t lport = 0;
150 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
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 * since we do not check port number duplicate with IPv4 space,
171 * we reject it at this moment. If we leave it, we make normal
172 * user to hijack port number from other users.
173 */
174 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
175 return(EADDRNOTAVAIL);
176
177 /* KAME hack: embed scopeid */
178 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0)
179 return EINVAL;
180 /* this must be cleared for ifa_ifwithaddr() */
181 sin6->sin6_scope_id = 0;
182
183 lport = sin6->sin6_port;
184 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
185 /*
186 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
187 * allow compepte duplication of binding if
188 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
189 * and a multicast address is bound on both
190 * new and duplicated sockets.
191 */
192 if (so->so_options & SO_REUSEADDR)
193 reuseport = SO_REUSEADDR|SO_REUSEPORT;
194 } else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
195 struct sockaddr_in sin;
196
197 bzero(&sin, sizeof(sin));
198 sin.sin_len = sizeof(sin);
199 sin.sin_family = AF_INET;
200 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
201 sizeof(sin.sin_addr));
202 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
203 return EADDRNOTAVAIL;
204 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
205 struct ifaddr *ia = NULL;
206
207 sin6->sin6_port = 0; /* yech... */
208 #if defined(NFAITH) && NFAITH > 0
209 if ((in6p->in6p_flags & IN6P_FAITH) == 0
210 && (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
211 #else
212 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
213 #endif
214 return(EADDRNOTAVAIL);
215
216 /*
217 * XXX: bind to an anycast address might accidentally
218 * cause sending a packet with anycast source address.
219 */
220 if (ia &&
221 ((struct in6_ifaddr *)ia)->ia6_flags &
222 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
223 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
224 return(EADDRNOTAVAIL);
225 }
226 }
227 if (lport) {
228 #ifndef IPNOPRIVPORTS
229 int priv;
230
231 /*
232 * NOTE: all operating systems use suser() for
233 * privilege check! do not rewrite it into SS_PRIV.
234 */
235 priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0;
236 /* GROSS */
237 if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
238 return(EACCES);
239 #endif
240
241 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
242 /* should check this but we can't ... */
243 #if 0
244 struct inpcb *t;
245
246 t = in_pcblookup_bind(&tcbtable,
247 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
248 lport);
249 if (t && (reuseport & t->inp_socket->so_options) == 0)
250 return EADDRINUSE;
251 #endif
252 } else {
253 struct in6pcb *t;
254
255 t = in6_pcblookup(head, &zeroin6_addr, 0,
256 &sin6->sin6_addr, lport, wild);
257 if (t && (reuseport & t->in6p_socket->so_options) == 0)
258 return(EADDRINUSE);
259 }
260 }
261 in6p->in6p_laddr = sin6->sin6_addr;
262 }
263
264 if (lport == 0) {
265 int e;
266 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
267 return(e);
268 }
269 else
270 in6p->in6p_lport = lport;
271
272 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
273 return(0);
274 }
275
276 /*
277 * Connect from a socket to a specified address.
278 * Both address and port must be specified in argument sin6.
279 * If don't have a local address for this socket yet,
280 * then pick one.
281 */
282 int
283 in6_pcbconnect(in6p, nam)
284 struct in6pcb *in6p;
285 struct mbuf *nam;
286 {
287 struct in6_addr *in6a = NULL;
288 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
289 struct ifnet *ifp = NULL; /* outgoing interface */
290 int error = 0;
291 struct in6_addr mapped;
292 struct sockaddr_in6 tmp;
293
294 (void)&in6a; /* XXX fool gcc */
295
296 if (nam->m_len != sizeof(*sin6))
297 return(EINVAL);
298 if (sin6->sin6_family != AF_INET6)
299 return(EAFNOSUPPORT);
300 if (sin6->sin6_port == 0)
301 return(EADDRNOTAVAIL);
302
303 /* sanity check for mapped address case */
304 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
305 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
306 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
307 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
308 return EINVAL;
309 } else {
310 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
311 return EINVAL;
312 }
313
314 /* protect *sin6 from overwrites */
315 tmp = *sin6;
316 sin6 = &tmp;
317
318 /* KAME hack: embed scopeid */
319 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0)
320 return EINVAL;
321
322 /* Source address selection. */
323 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
324 && in6p->in6p_laddr.s6_addr32[3] == 0) {
325 struct sockaddr_in sin, *sinp;
326
327 bzero(&sin, sizeof(sin));
328 sin.sin_len = sizeof(sin);
329 sin.sin_family = AF_INET;
330 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
331 sizeof(sin.sin_addr));
332 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
333 in6p->in6p_socket->so_options, NULL, &error);
334 if (sinp == 0) {
335 if (error == 0)
336 error = EADDRNOTAVAIL;
337 return(error);
338 }
339 bzero(&mapped, sizeof(mapped));
340 mapped.s6_addr16[5] = htons(0xffff);
341 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
342 in6a = &mapped;
343 } else {
344 /*
345 * XXX: in6_selectsrc might replace the bound local address
346 * with the address specified by setsockopt(IPV6_PKTINFO).
347 * Is it the intended behavior?
348 */
349 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
350 in6p->in6p_moptions,
351 &in6p->in6p_route,
352 &in6p->in6p_laddr, &error);
353 if (in6a == 0) {
354 if (error == 0)
355 error = EADDRNOTAVAIL;
356 return(error);
357 }
358 }
359 if (in6p->in6p_route.ro_rt)
360 ifp = in6p->in6p_route.ro_rt->rt_ifp;
361
362 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
363
364 if (in6_pcblookup(in6p->in6p_head,
365 &sin6->sin6_addr,
366 sin6->sin6_port,
367 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
368 in6a : &in6p->in6p_laddr,
369 in6p->in6p_lport,
370 0))
371 return(EADDRINUSE);
372 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
373 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
374 && in6p->in6p_laddr.s6_addr32[3] == 0)) {
375 if (in6p->in6p_lport == 0) {
376 #ifdef __NetBSD__
377 (void)in6_pcbbind(in6p, (struct mbuf *)0,
378 (struct proc *)0);
379 #else
380 (void)in6_pcbbind(in6p, (struct mbuf *)0);
381 #endif
382 }
383 in6p->in6p_laddr = *in6a;
384 }
385 in6p->in6p_faddr = sin6->sin6_addr;
386 in6p->in6p_fport = sin6->sin6_port;
387 /*
388 * xxx kazu flowlabel is necessary for connect?
389 * but if this line is missing, the garbage value remains.
390 */
391 in6p->in6p_flowinfo = sin6->sin6_flowinfo;
392 return(0);
393 }
394
395 void
396 in6_pcbdisconnect(in6p)
397 struct in6pcb *in6p;
398 {
399 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
400 in6p->in6p_fport = 0;
401 if (in6p->in6p_socket->so_state & SS_NOFDREF)
402 in6_pcbdetach(in6p);
403 }
404
405 void
406 in6_pcbdetach(in6p)
407 struct in6pcb *in6p;
408 {
409 struct socket *so = in6p->in6p_socket;
410
411 #ifdef IPSEC
412 ipsec6_delete_pcbpolicy(in6p);
413 #endif /* IPSEC */
414 sotoin6pcb(so) = 0;
415 sofree(so);
416 if (in6p->in6p_options)
417 m_freem(in6p->in6p_options);
418 if (in6p->in6p_outputopts) {
419 if (in6p->in6p_outputopts->ip6po_rthdr &&
420 in6p->in6p_outputopts->ip6po_route.ro_rt)
421 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
422 if (in6p->in6p_outputopts->ip6po_m)
423 (void)m_free(in6p->in6p_outputopts->ip6po_m);
424 free(in6p->in6p_outputopts, M_IP6OPT);
425 }
426 if (in6p->in6p_route.ro_rt)
427 rtfree(in6p->in6p_route.ro_rt);
428 ip6_freemoptions(in6p->in6p_moptions);
429 in6p->in6p_next->in6p_prev = in6p->in6p_prev;
430 in6p->in6p_prev->in6p_next = in6p->in6p_next;
431 in6p->in6p_prev = NULL;
432 FREE(in6p, M_PCB);
433 }
434
435 void
436 in6_setsockaddr(in6p, nam)
437 struct in6pcb *in6p;
438 struct mbuf *nam;
439 {
440 struct sockaddr_in6 *sin6;
441
442 nam->m_len = sizeof(*sin6);
443 sin6 = mtod(nam, struct sockaddr_in6 *);
444 bzero((caddr_t)sin6, sizeof(*sin6));
445 sin6->sin6_family = AF_INET6;
446 sin6->sin6_len = sizeof(struct sockaddr_in6);
447 sin6->sin6_port = in6p->in6p_lport;
448 /* KAME hack: recover scopeid */
449 (void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
450 }
451
452 void
453 in6_setpeeraddr(in6p, nam)
454 struct in6pcb *in6p;
455 struct mbuf *nam;
456 {
457 struct sockaddr_in6 *sin6;
458
459 nam->m_len = sizeof(*sin6);
460 sin6 = mtod(nam, struct sockaddr_in6 *);
461 bzero((caddr_t)sin6, sizeof(*sin6));
462 sin6->sin6_family = AF_INET6;
463 sin6->sin6_len = sizeof(struct sockaddr_in6);
464 sin6->sin6_port = in6p->in6p_fport;
465 /* KAME hack: recover scopeid */
466 (void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
467 }
468
469 /*
470 * Pass some notification to all connections of a protocol
471 * associated with address dst. The local address and/or port numbers
472 * may be specified to limit the search. The "usual action" will be
473 * taken, depending on the ctlinput cmd. The caller must filter any
474 * cmds that are uninteresting (e.g., no error in the map).
475 * Call the protocol specific routine (if any) to report
476 * any errors for each matching socket.
477 *
478 * Must be called at splsoftnet.
479 */
480 int
481 in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify)
482 struct in6pcb *head;
483 struct sockaddr *dst;
484 u_int fport_arg, lport_arg;
485 struct in6_addr *laddr6;
486 int cmd;
487 void (*notify) __P((struct in6pcb *, int));
488 {
489 struct in6pcb *in6p, *nin6p;
490 struct in6_addr faddr6;
491 u_int16_t fport = fport_arg, lport = lport_arg;
492 int errno;
493 int nmatch = 0;
494 int do_rtchange = (notify == in6_rtchange);
495
496 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
497 return 0;
498 faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
499 if (IN6_IS_ADDR_UNSPECIFIED(&faddr6))
500 return 0;
501
502 /*
503 * Redirects go to all references to the destination,
504 * and use in6_rtchange to invalidate the route cache.
505 * Dead host indications: also use in6_rtchange to invalidate
506 * the cache, and deliver the error to all the sockets.
507 * Otherwise, if we have knowledge of the local port and address,
508 * deliver only to that socket.
509 */
510 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
511 fport = 0;
512 lport = 0;
513 bzero((caddr_t)laddr6, sizeof(*laddr6));
514
515 do_rtchange = 1;
516 }
517
518 if (notify == NULL)
519 return 0;
520
521 errno = inet6ctlerrmap[cmd];
522 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
523 nin6p = in6p->in6p_next;
524
525 if (do_rtchange) {
526 /*
527 * Since a non-connected PCB might have a cached route,
528 * we always call in6_rtchange without matching
529 * the PCB to the src/dst pair.
530 *
531 * XXX: we assume in6_rtchange does not free the PCB.
532 */
533 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_route.ro_dst.sin6_addr,
534 &faddr6))
535 in6_rtchange(in6p, errno);
536
537 if (notify == in6_rtchange)
538 continue; /* there's nothing to do any more */
539 }
540
541 /* at this point, we can assume that NOTIFY is not NULL. */
542
543 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &faddr6) ||
544 in6p->in6p_socket == 0 ||
545 (lport && in6p->in6p_lport != lport) ||
546 (!IN6_IS_ADDR_UNSPECIFIED(laddr6) &&
547 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6)) ||
548 (fport && in6p->in6p_fport != fport))
549 continue;
550
551 (*notify)(in6p, errno);
552 nmatch++;
553 }
554 return nmatch;
555 }
556
557 void
558 in6_pcbpurgeif(head, ifp)
559 struct in6pcb *head;
560 struct ifnet *ifp;
561 {
562 struct in6pcb *in6p, *nin6p;
563
564 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
565 nin6p = in6p->in6p_next;
566 if (in6p->in6p_route.ro_rt != NULL &&
567 in6p->in6p_route.ro_rt->rt_ifp == ifp)
568 in6_rtchange(in6p, 0);
569 }
570 }
571
572 /*
573 * Check for alternatives when higher level complains
574 * about service problems. For now, invalidate cached
575 * routing information. If the route was created dynamically
576 * (by a redirect), time to try a default gateway again.
577 */
578 void
579 in6_losing(in6p)
580 struct in6pcb *in6p;
581 {
582 struct rtentry *rt;
583 struct rt_addrinfo info;
584
585 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
586 in6p->in6p_route.ro_rt = 0;
587 bzero((caddr_t)&info, sizeof(info));
588 info.rti_info[RTAX_DST] =
589 (struct sockaddr *)&in6p->in6p_route.ro_dst;
590 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
591 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
592 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
593 if (rt->rt_flags & RTF_DYNAMIC)
594 (void)rtrequest(RTM_DELETE, rt_key(rt),
595 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
596 (struct rtentry **)0);
597 else
598 /*
599 * A new route can be allocated
600 * the next time output is attempted.
601 */
602 rtfree(rt);
603 }
604 }
605
606 /*
607 * After a routing change, flush old routing
608 * and allocate a (hopefully) better one.
609 */
610 void
611 in6_rtchange(in6p, errno)
612 struct in6pcb *in6p;
613 int errno;
614 {
615 if (in6p->in6p_route.ro_rt) {
616 rtfree(in6p->in6p_route.ro_rt);
617 in6p->in6p_route.ro_rt = 0;
618 /*
619 * A new route can be allocated the next time
620 * output is attempted.
621 */
622 }
623 }
624
625 struct in6pcb *
626 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
627 struct in6pcb *head;
628 struct in6_addr *faddr6, *laddr6;
629 u_int fport_arg, lport_arg;
630 int flags;
631 {
632 struct in6pcb *in6p, *match = 0;
633 int matchwild = 3, wildcard;
634 u_int16_t fport = fport_arg, lport = lport_arg;
635
636 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
637 if (in6p->in6p_lport != lport)
638 continue;
639 wildcard = 0;
640 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
641 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
642 wildcard++;
643 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
644 continue;
645 }
646 #ifndef TCP6
647 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
648 && in6p->in6p_laddr.s6_addr32[3] == 0) {
649 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
650 continue;
651 if (laddr6->s6_addr32[3] == 0)
652 ;
653 else
654 wildcard++;
655 }
656 #endif
657 else {
658 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
659 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
660 if (in6p->in6p_flags & IN6P_BINDV6ONLY)
661 continue;
662 else
663 wildcard++;
664 #else
665 continue;
666 #endif
667 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
668 wildcard++;
669 }
670 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
671 if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
672 wildcard++;
673 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
674 || in6p->in6p_fport != fport)
675 continue;
676 }
677 #ifndef TCP6
678 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
679 && in6p->in6p_faddr.s6_addr32[3] == 0) {
680 if (!IN6_IS_ADDR_V4MAPPED(faddr6))
681 continue;
682 if (faddr6->s6_addr32[3] == 0)
683 ;
684 else
685 wildcard++;
686 }
687 #endif
688 else {
689 if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
690 #if !defined(TCP6) && !defined(INET6_BINDV6ONLY)
691 if (in6p->in6p_flags & IN6P_BINDV6ONLY)
692 continue;
693 else
694 wildcard++;
695 #else
696 continue;
697 #endif
698 } else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
699 wildcard++;
700 }
701
702 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
703 continue;
704 if (wildcard < matchwild) {
705 match = in6p;
706 matchwild = wildcard;
707 if (matchwild == 0)
708 break;
709 }
710 }
711 return(match);
712 }
713
714 #ifndef TCP6
715 struct rtentry *
716 in6_pcbrtentry(in6p)
717 struct in6pcb *in6p;
718 {
719 struct route_in6 *ro;
720
721 ro = &in6p->in6p_route;
722
723 if (ro->ro_rt == NULL) {
724 /*
725 * No route yet, so try to acquire one.
726 */
727 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
728 bzero(&ro->ro_dst, sizeof(ro->ro_dst));
729 ro->ro_dst.sin6_family = AF_INET6;
730 ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6);
731 satosin6(&ro->ro_dst)->sin6_addr = in6p->in6p_faddr;
732 rtalloc((struct route *)ro);
733 }
734 }
735 return (ro->ro_rt);
736 }
737
738 struct in6pcb *
739 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
740 struct in6pcb *head;
741 struct in6_addr *faddr6, *laddr6;
742 u_int fport_arg, lport_arg;
743 int faith;
744 {
745 struct in6pcb *in6p;
746 u_int16_t fport = fport_arg, lport = lport_arg;
747
748 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
749 /* find exact match on both source and dest */
750 if (in6p->in6p_fport != fport)
751 continue;
752 if (in6p->in6p_lport != lport)
753 continue;
754 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
755 continue;
756 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
757 continue;
758 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
759 continue;
760 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
761 continue;
762 return in6p;
763 }
764 return NULL;
765 }
766
767 struct in6pcb *
768 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
769 struct in6pcb *head;
770 struct in6_addr *laddr6;
771 u_int lport_arg;
772 int faith;
773 {
774 struct in6pcb *in6p, *match;
775 u_int16_t lport = lport_arg;
776
777 match = NULL;
778 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
779 /*
780 * find destination match. exact match is preferred
781 * against wildcard match.
782 */
783 #if defined(NFAITH) && NFAITH > 0
784 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
785 continue;
786 #endif
787 if (in6p->in6p_fport != 0)
788 continue;
789 if (in6p->in6p_lport != lport)
790 continue;
791 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
792 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
793 #ifndef INET6_BINDV6ONLY
794 if (in6p->in6p_flags & IN6P_BINDV6ONLY)
795 continue;
796 else
797 match = in6p;
798 #else
799 continue;
800 #endif
801 } else
802 match = in6p;
803 }
804 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
805 in6p->in6p_laddr.s6_addr32[3] == 0) {
806 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
807 laddr6->s6_addr32[3] != 0)
808 match = in6p;
809 }
810 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
811 return in6p;
812 }
813 return match;
814 }
815 #endif
816