in6_pcb.c revision 1.38.2.3 1 /* $NetBSD: in6_pcb.c,v 1.38.2.3 2002/01/10 20:03:16 thorpej Exp $ */
2 /* $KAME: in6_pcb.c,v 1.84 2001/02/08 18:02:08 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 <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.38.2.3 2002/01/10 20:03:16 thorpej Exp $");
70
71 #include "opt_ipsec.h"
72
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/malloc.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/ioctl.h>
81 #include <sys/errno.h>
82 #include <sys/time.h>
83 #include <sys/proc.h>
84
85 #include <net/if.h>
86 #include <net/route.h>
87
88 #include <netinet/in.h>
89 #include <netinet/in_var.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/ip.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/ip6.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/nd6.h>
97
98 #include "loop.h"
99 extern struct ifnet loif[NLOOP];
100 #include "faith.h"
101
102 #ifdef IPSEC
103 #include <netinet6/ipsec.h>
104 #include <netkey/key.h>
105 #endif /* IPSEC */
106
107 struct in6_addr zeroin6_addr;
108
109 int ip6_anonportmin = IPV6PORT_ANONMIN;
110 int ip6_anonportmax = IPV6PORT_ANONMAX;
111 int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
112 int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
113
114 int
115 in6_pcballoc(so, head)
116 struct socket *so;
117 struct in6pcb *head;
118 {
119 struct in6pcb *in6p;
120 #ifdef IPSEC
121 int error;
122 #endif
123
124 MALLOC(in6p, struct in6pcb *, sizeof(*in6p), M_PCB, M_NOWAIT);
125 if (in6p == NULL)
126 return(ENOBUFS);
127 bzero((caddr_t)in6p, sizeof(*in6p));
128 in6p->in6p_head = head;
129 in6p->in6p_socket = so;
130 in6p->in6p_hops = -1; /* use kernel default */
131 in6p->in6p_icmp6filt = NULL;
132 #ifdef IPSEC
133 error = ipsec_init_policy(so, &in6p->in6p_sp);
134 if (error != 0) {
135 FREE(in6p, M_PCB);
136 return error;
137 }
138 #endif /* IPSEC */
139 in6p->in6p_next = head->in6p_next;
140 head->in6p_next = in6p;
141 in6p->in6p_prev = head;
142 in6p->in6p_next->in6p_prev = in6p;
143 if (ip6_v6only)
144 in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
145 so->so_pcb = (caddr_t)in6p;
146 return(0);
147 }
148
149 int
150 in6_pcbbind(in6p, nam, p)
151 struct in6pcb *in6p;
152 struct mbuf *nam;
153 struct proc *p;
154 {
155 struct socket *so = in6p->in6p_socket;
156 struct in6pcb *head = in6p->in6p_head;
157 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
158 u_int16_t lport = 0;
159 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
160
161 if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
162 return(EINVAL);
163 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
164 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
165 (so->so_options & SO_ACCEPTCONN) == 0))
166 wild = IN6PLOOKUP_WILDCARD;
167 if (nam) {
168 sin6 = mtod(nam, struct sockaddr_in6 *);
169 if (nam->m_len != sizeof(*sin6))
170 return(EINVAL);
171 /*
172 * We should check the family, but old programs
173 * incorrectly fail to intialize it.
174 */
175 if (sin6->sin6_family != AF_INET6)
176 return(EAFNOSUPPORT);
177
178 /*
179 * since we do not check port number duplicate with IPv4 space,
180 * we reject it at this moment. If we leave it, we would
181 * mistakenly allow normal users to hijack tcp/udp ports from
182 * other users.
183 */
184 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
185 return(EADDRNOTAVAIL);
186
187 /* KAME hack: embed scopeid */
188 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0)
189 return EINVAL;
190 /* this must be cleared for ifa_ifwithaddr() */
191 sin6->sin6_scope_id = 0;
192
193 lport = sin6->sin6_port;
194 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
195 /*
196 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
197 * allow compepte duplication of binding if
198 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
199 * and a multicast address is bound on both
200 * new and duplicated sockets.
201 */
202 if (so->so_options & SO_REUSEADDR)
203 reuseport = SO_REUSEADDR|SO_REUSEPORT;
204 }
205 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
206 struct sockaddr_in sin;
207
208 bzero(&sin, sizeof(sin));
209 sin.sin_len = sizeof(sin);
210 sin.sin_family = AF_INET;
211 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
212 sizeof(sin.sin_addr));
213 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
214 return EADDRNOTAVAIL;
215 }
216 else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
217 struct ifaddr *ia = NULL;
218
219 sin6->sin6_port = 0; /* yech... */
220 if ((in6p->in6p_flags & IN6P_FAITH) == 0 &&
221 (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
222 return(EADDRNOTAVAIL);
223
224 /*
225 * bind to an anycast address might accidentally
226 * cause sending a packet with an anycast source
227 * address, so we forbid it.
228 */
229 if (ia &&
230 ((struct in6_ifaddr *)ia)->ia6_flags &
231 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
232 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
233 return(EADDRNOTAVAIL);
234 }
235 }
236 if (lport) {
237 #ifndef IPNOPRIVPORTS
238 int priv;
239
240 /*
241 * NOTE: all operating systems use suser() for
242 * privilege check! do not rewrite it into SS_PRIV.
243 */
244 priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0;
245 /* GROSS */
246 if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
247 return(EACCES);
248 #endif
249
250 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
251 /* should check this but we can't ... */
252 #if 0
253 struct inpcb *t;
254
255 t = in_pcblookup_bind(&tcbtable,
256 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
257 lport);
258 if (t && (reuseport & t->inp_socket->so_options) == 0)
259 return EADDRINUSE;
260 #endif
261 } else
262 {
263 struct in6pcb *t;
264
265 t = in6_pcblookup(head, &zeroin6_addr, 0,
266 &sin6->sin6_addr, lport, wild);
267 if (t && (reuseport & t->in6p_socket->so_options) == 0)
268 return(EADDRINUSE);
269 }
270 }
271 in6p->in6p_laddr = sin6->sin6_addr;
272 }
273
274 if (lport == 0) {
275 int e;
276 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
277 return(e);
278 }
279 else
280 in6p->in6p_lport = lport;
281
282 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
283 return(0);
284 }
285
286 /*
287 * Connect from a socket to a specified address.
288 * Both address and port must be specified in argument sin6.
289 * If don't have a local address for this socket yet,
290 * then pick one.
291 */
292 int
293 in6_pcbconnect(in6p, nam)
294 struct in6pcb *in6p;
295 struct mbuf *nam;
296 {
297 struct in6_addr *in6a = NULL;
298 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
299 struct ifnet *ifp = NULL; /* outgoing interface */
300 int error = 0;
301 #ifdef INET
302 struct in6_addr mapped;
303 #endif
304 struct sockaddr_in6 tmp;
305
306 (void)&in6a; /* XXX fool gcc */
307
308 if (nam->m_len != sizeof(*sin6))
309 return(EINVAL);
310 if (sin6->sin6_family != AF_INET6)
311 return(EAFNOSUPPORT);
312 if (sin6->sin6_port == 0)
313 return(EADDRNOTAVAIL);
314
315 /* sanity check for mapped address case */
316 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
317 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
318 return EINVAL;
319 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
320 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
321 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
322 return EINVAL;
323 } else
324 {
325 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
326 return EINVAL;
327 }
328
329 /* protect *sin6 from overwrites */
330 tmp = *sin6;
331 sin6 = &tmp;
332
333 /* KAME hack: embed scopeid */
334 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0)
335 return EINVAL;
336
337 /* Source address selection. */
338 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
339 && in6p->in6p_laddr.s6_addr32[3] == 0) {
340 #ifdef INET
341 struct sockaddr_in sin, *sinp;
342
343 bzero(&sin, sizeof(sin));
344 sin.sin_len = sizeof(sin);
345 sin.sin_family = AF_INET;
346 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
347 sizeof(sin.sin_addr));
348 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
349 in6p->in6p_socket->so_options, NULL, &error);
350 if (sinp == 0) {
351 if (error == 0)
352 error = EADDRNOTAVAIL;
353 return(error);
354 }
355 bzero(&mapped, sizeof(mapped));
356 mapped.s6_addr16[5] = htons(0xffff);
357 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
358 in6a = &mapped;
359 #else
360 return EADDRNOTAVAIL;
361 #endif
362 } else
363 {
364 /*
365 * XXX: in6_selectsrc might replace the bound local address
366 * with the address specified by setsockopt(IPV6_PKTINFO).
367 * Is it the intended behavior?
368 */
369 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
370 in6p->in6p_moptions,
371 &in6p->in6p_route,
372 &in6p->in6p_laddr, &error);
373 if (in6a == 0) {
374 if (error == 0)
375 error = EADDRNOTAVAIL;
376 return(error);
377 }
378 }
379 if (in6p->in6p_route.ro_rt)
380 ifp = in6p->in6p_route.ro_rt->rt_ifp;
381
382 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
383
384 if (in6_pcblookup(in6p->in6p_head,
385 &sin6->sin6_addr,
386 sin6->sin6_port,
387 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
388 in6a : &in6p->in6p_laddr,
389 in6p->in6p_lport,
390 0))
391 return(EADDRINUSE);
392 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
393 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
394 && in6p->in6p_laddr.s6_addr32[3] == 0))
395 {
396 /*
397 * XXX in IPv4 mapped address case, we should grab fresh
398 * local port number by in_pcbbind, not in6_pcbbind.
399 * if we are in bad luck, we may assign conflicting port number
400 * between IPv4 and IPv6 unwillingly.
401 */
402 if (in6p->in6p_lport == 0) {
403 (void)in6_pcbbind(in6p, (struct mbuf *)0,
404 (struct proc *)0);
405 }
406 in6p->in6p_laddr = *in6a;
407 }
408 in6p->in6p_faddr = sin6->sin6_addr;
409 in6p->in6p_fport = sin6->sin6_port;
410 /*
411 * xxx kazu flowlabel is necessary for connect?
412 * but if this line is missing, the garbage value remains.
413 */
414 in6p->in6p_flowinfo = sin6->sin6_flowinfo;
415 #ifdef IPSEC
416 if (in6p->in6p_socket->so_type == SOCK_STREAM)
417 ipsec_pcbconn(in6p->in6p_sp);
418 #endif
419 return(0);
420 }
421
422 void
423 in6_pcbdisconnect(in6p)
424 struct in6pcb *in6p;
425 {
426 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
427 in6p->in6p_fport = 0;
428 if (in6p->in6p_socket->so_state & SS_NOFDREF)
429 in6_pcbdetach(in6p);
430 #ifdef IPSEC
431 ipsec_pcbdisconn(in6p->in6p_sp);
432 #endif
433 }
434
435 void
436 in6_pcbdetach(in6p)
437 struct in6pcb *in6p;
438 {
439 struct socket *so = in6p->in6p_socket;
440
441 #ifdef IPSEC
442 ipsec6_delete_pcbpolicy(in6p);
443 #endif /* IPSEC */
444 sotoin6pcb(so) = 0;
445 sofree(so);
446 if (in6p->in6p_options)
447 m_freem(in6p->in6p_options);
448 if (in6p->in6p_outputopts) {
449 if (in6p->in6p_outputopts->ip6po_rthdr &&
450 in6p->in6p_outputopts->ip6po_route.ro_rt)
451 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
452 if (in6p->in6p_outputopts->ip6po_m)
453 (void)m_free(in6p->in6p_outputopts->ip6po_m);
454 free(in6p->in6p_outputopts, M_IP6OPT);
455 }
456 if (in6p->in6p_route.ro_rt)
457 rtfree(in6p->in6p_route.ro_rt);
458 ip6_freemoptions(in6p->in6p_moptions);
459 in6p->in6p_next->in6p_prev = in6p->in6p_prev;
460 in6p->in6p_prev->in6p_next = in6p->in6p_next;
461 in6p->in6p_prev = NULL;
462 FREE(in6p, M_PCB);
463 }
464
465 void
466 in6_setsockaddr(in6p, nam)
467 struct in6pcb *in6p;
468 struct mbuf *nam;
469 {
470 struct sockaddr_in6 *sin6;
471
472 nam->m_len = sizeof(*sin6);
473 sin6 = mtod(nam, struct sockaddr_in6 *);
474 bzero((caddr_t)sin6, sizeof(*sin6));
475 sin6->sin6_family = AF_INET6;
476 sin6->sin6_len = sizeof(struct sockaddr_in6);
477 sin6->sin6_port = in6p->in6p_lport;
478 /* KAME hack: recover scopeid */
479 (void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
480 }
481
482 void
483 in6_setpeeraddr(in6p, nam)
484 struct in6pcb *in6p;
485 struct mbuf *nam;
486 {
487 struct sockaddr_in6 *sin6;
488
489 nam->m_len = sizeof(*sin6);
490 sin6 = mtod(nam, struct sockaddr_in6 *);
491 bzero((caddr_t)sin6, sizeof(*sin6));
492 sin6->sin6_family = AF_INET6;
493 sin6->sin6_len = sizeof(struct sockaddr_in6);
494 sin6->sin6_port = in6p->in6p_fport;
495 /* KAME hack: recover scopeid */
496 (void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
497 }
498
499 /*
500 * Pass some notification to all connections of a protocol
501 * associated with address dst. The local address and/or port numbers
502 * may be specified to limit the search. The "usual action" will be
503 * taken, depending on the ctlinput cmd. The caller must filter any
504 * cmds that are uninteresting (e.g., no error in the map).
505 * Call the protocol specific routine (if any) to report
506 * any errors for each matching socket.
507 *
508 * Must be called at splsoftnet.
509 *
510 * Note: src (4th arg) carries the flowlabel value on the original IPv6
511 * header, in sin6_flowinfo member.
512 */
513 int
514 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
515 struct in6pcb *head;
516 struct sockaddr *dst, *src;
517 u_int fport_arg, lport_arg;
518 int cmd;
519 void *cmdarg;
520 void (*notify) __P((struct in6pcb *, int));
521 {
522 struct in6pcb *in6p, *nin6p;
523 struct sockaddr_in6 sa6_src, *sa6_dst;
524 u_int16_t fport = fport_arg, lport = lport_arg;
525 int errno;
526 int nmatch = 0;
527 u_int32_t flowinfo;
528
529 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
530 return 0;
531
532 sa6_dst = (struct sockaddr_in6 *)dst;
533 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
534 return 0;
535
536 /*
537 * note that src can be NULL when we get notify by local fragmentation.
538 */
539 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
540 flowinfo = sa6_src.sin6_flowinfo;
541
542 /*
543 * Redirects go to all references to the destination,
544 * and use in6_rtchange to invalidate the route cache.
545 * Dead host indications: also use in6_rtchange to invalidate
546 * the cache, and deliver the error to all the sockets.
547 * Otherwise, if we have knowledge of the local port and address,
548 * deliver only to that socket.
549 */
550 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
551 fport = 0;
552 lport = 0;
553 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
554
555 if (cmd != PRC_HOSTDEAD)
556 notify = in6_rtchange;
557 }
558
559 errno = inet6ctlerrmap[cmd];
560 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
561 nin6p = in6p->in6p_next;
562
563 /*
564 * Under the following condition, notify of redirects
565 * to the pcb, without making address matches against inpcb.
566 * - redirect notification is arrived.
567 * - the inpcb is unconnected.
568 * - the inpcb is caching !RTF_HOST routing entry.
569 * - the ICMPv6 notification is from the gateway cached in the
570 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
571 * the inpcb used very recently.
572 *
573 * This is to improve interaction between netbsd/openbsd
574 * redirect handling code, and inpcb route cache code.
575 * without the clause, !RTF_HOST routing entry (which carries
576 * gateway used by inpcb right before the ICMPv6 redirect)
577 * will be cached forever in unconnected inpcb.
578 *
579 * There still is a question regarding to what is TRT:
580 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
581 * generated on packet output. inpcb will always cache
582 * RTF_HOST routing entry so there's no need for the clause
583 * (ICMPv6 redirect will update RTF_HOST routing entry,
584 * and inpcb is caching it already).
585 * However, bsdi/freebsd are vulnerable to local DoS attacks
586 * due to the cloned routing entries.
587 * - Specwise, "destination cache" is mentioned in RFC2461.
588 * Jinmei says that it implies bsdi/freebsd behavior, itojun
589 * is not really convinced.
590 * - Having hiwat/lowat on # of cloned host route (redirect/
591 * pmtud) may be a good idea. netbsd/openbsd has it. see
592 * icmp6_mtudisc_update().
593 */
594 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
595 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
596 in6p->in6p_route.ro_rt &&
597 !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) {
598 struct sockaddr_in6 *dst6;
599
600 dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst;
601 if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
602 &sa6_dst->sin6_addr))
603 goto do_notify;
604 }
605
606 /*
607 * Detect if we should notify the error. If no source and
608 * destination ports are specified, but non-zero flowinfo and
609 * local address match, notify the error. This is the case
610 * when the error is delivered with an encrypted buffer
611 * by ESP. Otherwise, just compare addresses and ports
612 * as usual.
613 */
614 if (lport == 0 && fport == 0 && flowinfo &&
615 in6p->in6p_socket != NULL &&
616 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
617 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
618 goto do_notify;
619 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
620 &sa6_dst->sin6_addr) ||
621 in6p->in6p_socket == 0 ||
622 (lport && in6p->in6p_lport != lport) ||
623 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
624 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
625 &sa6_src.sin6_addr)) ||
626 (fport && in6p->in6p_fport != fport))
627 continue;
628
629 do_notify:
630 if (notify)
631 (*notify)(in6p, errno);
632 nmatch++;
633 }
634 return nmatch;
635 }
636
637 void
638 in6_pcbpurgeif0(head, ifp)
639 struct in6pcb *head;
640 struct ifnet *ifp;
641 {
642 struct in6pcb *in6p, *nin6p;
643 struct ip6_moptions *im6o;
644 struct in6_multi_mship *imm, *nimm;
645
646 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
647 nin6p = in6p->in6p_next;
648 im6o = in6p->in6p_moptions;
649 if (im6o) {
650 /*
651 * Unselect the outgoing interface if it is being
652 * detached.
653 */
654 if (im6o->im6o_multicast_ifp == ifp)
655 im6o->im6o_multicast_ifp = NULL;
656
657 /*
658 * Drop multicast group membership if we joined
659 * through the interface being detached.
660 * XXX controversial - is it really legal for kernel
661 * to force this?
662 */
663 for (imm = im6o->im6o_memberships.lh_first;
664 imm != NULL; imm = nimm) {
665 nimm = imm->i6mm_chain.le_next;
666 if (imm->i6mm_maddr->in6m_ifp == ifp) {
667 LIST_REMOVE(imm, i6mm_chain);
668 in6_leavegroup(imm);
669 }
670 }
671 }
672 }
673 }
674
675 void
676 in6_pcbpurgeif(head, ifp)
677 struct in6pcb *head;
678 struct ifnet *ifp;
679 {
680 struct in6pcb *in6p, *nin6p;
681
682 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
683 nin6p = in6p->in6p_next;
684 if (in6p->in6p_route.ro_rt != NULL &&
685 in6p->in6p_route.ro_rt->rt_ifp == ifp)
686 in6_rtchange(in6p, 0);
687 }
688 }
689
690 /*
691 * Check for alternatives when higher level complains
692 * about service problems. For now, invalidate cached
693 * routing information. If the route was created dynamically
694 * (by a redirect), time to try a default gateway again.
695 */
696 void
697 in6_losing(in6p)
698 struct in6pcb *in6p;
699 {
700 struct rtentry *rt;
701 struct rt_addrinfo info;
702
703 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
704 in6p->in6p_route.ro_rt = 0;
705 bzero((caddr_t)&info, sizeof(info));
706 info.rti_info[RTAX_DST] =
707 (struct sockaddr *)&in6p->in6p_route.ro_dst;
708 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
709 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
710 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
711 if (rt->rt_flags & RTF_DYNAMIC) {
712 (void)rtrequest(RTM_DELETE, rt_key(rt),
713 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
714 (struct rtentry **)0);
715 } else {
716 /*
717 * A new route can be allocated
718 * the next time output is attempted.
719 */
720 rtfree(rt);
721 }
722 }
723 }
724
725 /*
726 * After a routing change, flush old routing
727 * and allocate a (hopefully) better one.
728 */
729 void
730 in6_rtchange(in6p, errno)
731 struct in6pcb *in6p;
732 int errno;
733 {
734 if (in6p->in6p_route.ro_rt) {
735 rtfree(in6p->in6p_route.ro_rt);
736 in6p->in6p_route.ro_rt = 0;
737 /*
738 * A new route can be allocated the next time
739 * output is attempted.
740 */
741 }
742 }
743
744 struct in6pcb *
745 in6_pcblookup(head, faddr6, fport_arg, laddr6, lport_arg, flags)
746 struct in6pcb *head;
747 struct in6_addr *faddr6, *laddr6;
748 u_int fport_arg, lport_arg;
749 int flags;
750 {
751 struct in6pcb *in6p, *match = 0;
752 int matchwild = 3, wildcard;
753 u_int16_t fport = fport_arg, lport = lport_arg;
754
755 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
756 if (in6p->in6p_lport != lport)
757 continue;
758 wildcard = 0;
759 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
760 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
761 wildcard++;
762 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
763 continue;
764 }
765 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
766 && in6p->in6p_laddr.s6_addr32[3] == 0) {
767 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
768 continue;
769 if (laddr6->s6_addr32[3] == 0)
770 ;
771 else
772 wildcard++;
773 }
774 else {
775 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
776 if (in6p->in6p_flags & IN6P_IPV6_V6ONLY)
777 continue;
778 else
779 wildcard++;
780 } else if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
781 wildcard++;
782 }
783 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
784 if (IN6_IS_ADDR_UNSPECIFIED(faddr6))
785 wildcard++;
786 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6)
787 || in6p->in6p_fport != fport)
788 continue;
789 }
790 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)
791 && in6p->in6p_faddr.s6_addr32[3] == 0) {
792 if (!IN6_IS_ADDR_V4MAPPED(faddr6))
793 continue;
794 if (faddr6->s6_addr32[3] == 0)
795 ;
796 else
797 wildcard++;
798 }
799 else {
800 if (IN6_IS_ADDR_V4MAPPED(faddr6)) {
801 if (in6p->in6p_flags & IN6P_IPV6_V6ONLY)
802 continue;
803 else
804 wildcard++;
805 } else if (!IN6_IS_ADDR_UNSPECIFIED(faddr6))
806 wildcard++;
807 }
808
809 if (wildcard && (flags & IN6PLOOKUP_WILDCARD) == 0)
810 continue;
811 if (wildcard < matchwild) {
812 match = in6p;
813 matchwild = wildcard;
814 if (matchwild == 0)
815 break;
816 }
817 }
818 return(match);
819 }
820
821 struct rtentry *
822 in6_pcbrtentry(in6p)
823 struct in6pcb *in6p;
824 {
825 struct route_in6 *ro;
826 struct sockaddr_in6 *dst6;
827
828 ro = &in6p->in6p_route;
829 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
830
831 if (ro->ro_rt == NULL) {
832 /*
833 * No route yet, so try to acquire one.
834 */
835 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
836 bzero(dst6, sizeof(*dst6));
837 dst6->sin6_family = AF_INET6;
838 dst6->sin6_len = sizeof(struct sockaddr_in6);
839 dst6->sin6_addr = in6p->in6p_faddr;
840 rtalloc((struct route *)ro);
841 }
842 }
843 return (ro->ro_rt);
844 }
845
846 struct in6pcb *
847 in6_pcblookup_connect(head, faddr6, fport_arg, laddr6, lport_arg, faith)
848 struct in6pcb *head;
849 struct in6_addr *faddr6, *laddr6;
850 u_int fport_arg, lport_arg;
851 int faith;
852 {
853 struct in6pcb *in6p;
854 u_int16_t fport = fport_arg, lport = lport_arg;
855
856 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
857 /* find exact match on both source and dest */
858 if (in6p->in6p_fport != fport)
859 continue;
860 if (in6p->in6p_lport != lport)
861 continue;
862 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
863 continue;
864 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
865 continue;
866 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
867 continue;
868 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
869 continue;
870 if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
871 IN6_IS_ADDR_V4MAPPED(faddr6)) &&
872 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
873 continue;
874 return in6p;
875 }
876 return NULL;
877 }
878
879 struct in6pcb *
880 in6_pcblookup_bind(head, laddr6, lport_arg, faith)
881 struct in6pcb *head;
882 struct in6_addr *laddr6;
883 u_int lport_arg;
884 int faith;
885 {
886 struct in6pcb *in6p, *match;
887 u_int16_t lport = lport_arg;
888
889 match = NULL;
890 for (in6p = head->in6p_next; in6p != head; in6p = in6p->in6p_next) {
891 /*
892 * find destination match. exact match is preferred
893 * against wildcard match.
894 */
895 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
896 continue;
897 if (in6p->in6p_fport != 0)
898 continue;
899 if (in6p->in6p_lport != lport)
900 continue;
901 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
902 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
903 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY))
904 continue;
905 else
906 match = in6p;
907 } else
908 match = in6p;
909 }
910 else if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
911 !(in6p->in6p_flags & IN6P_IPV6_V6ONLY) &&
912 in6p->in6p_laddr.s6_addr32[3] == 0) {
913 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
914 laddr6->s6_addr32[3] != 0)
915 match = in6p;
916 }
917 else if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
918 return in6p;
919 }
920 return match;
921 }
922