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