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