in6_pcb.c revision 1.44 1 /* $NetBSD: in6_pcb.c,v 1.44 2001/11/13 00:56:59 lukem 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.44 2001/11/13 00:56:59 lukem 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 * XXX: bind to an anycast address might accidentally
226 * cause sending a packet with anycast source address.
227 */
228 if (ia &&
229 ((struct in6_ifaddr *)ia)->ia6_flags &
230 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
231 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
232 return(EADDRNOTAVAIL);
233 }
234 }
235 if (lport) {
236 #ifndef IPNOPRIVPORTS
237 int priv;
238
239 /*
240 * NOTE: all operating systems use suser() for
241 * privilege check! do not rewrite it into SS_PRIV.
242 */
243 priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0;
244 /* GROSS */
245 if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
246 return(EACCES);
247 #endif
248
249 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
250 /* should check this but we can't ... */
251 #if 0
252 struct inpcb *t;
253
254 t = in_pcblookup_bind(&tcbtable,
255 (struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
256 lport);
257 if (t && (reuseport & t->inp_socket->so_options) == 0)
258 return EADDRINUSE;
259 #endif
260 } else
261 {
262 struct in6pcb *t;
263
264 t = in6_pcblookup(head, &zeroin6_addr, 0,
265 &sin6->sin6_addr, lport, wild);
266 if (t && (reuseport & t->in6p_socket->so_options) == 0)
267 return(EADDRINUSE);
268 }
269 }
270 in6p->in6p_laddr = sin6->sin6_addr;
271 }
272
273 if (lport == 0) {
274 int e;
275 if ((e = in6_pcbsetport(&in6p->in6p_laddr, in6p)) != 0)
276 return(e);
277 }
278 else
279 in6p->in6p_lport = lport;
280
281 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
282 return(0);
283 }
284
285 /*
286 * Connect from a socket to a specified address.
287 * Both address and port must be specified in argument sin6.
288 * If don't have a local address for this socket yet,
289 * then pick one.
290 */
291 int
292 in6_pcbconnect(in6p, nam)
293 struct in6pcb *in6p;
294 struct mbuf *nam;
295 {
296 struct in6_addr *in6a = NULL;
297 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
298 struct ifnet *ifp = NULL; /* outgoing interface */
299 int error = 0;
300 #ifdef INET
301 struct in6_addr mapped;
302 #endif
303 struct sockaddr_in6 tmp;
304
305 (void)&in6a; /* XXX fool gcc */
306
307 if (nam->m_len != sizeof(*sin6))
308 return(EINVAL);
309 if (sin6->sin6_family != AF_INET6)
310 return(EAFNOSUPPORT);
311 if (sin6->sin6_port == 0)
312 return(EADDRNOTAVAIL);
313
314 /* sanity check for mapped address case */
315 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
316 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
317 return EINVAL;
318 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
319 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
320 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
321 return EINVAL;
322 } else
323 {
324 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
325 return EINVAL;
326 }
327
328 /* protect *sin6 from overwrites */
329 tmp = *sin6;
330 sin6 = &tmp;
331
332 /* KAME hack: embed scopeid */
333 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0)
334 return EINVAL;
335
336 /* Source address selection. */
337 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
338 && in6p->in6p_laddr.s6_addr32[3] == 0) {
339 #ifdef INET
340 struct sockaddr_in sin, *sinp;
341
342 bzero(&sin, sizeof(sin));
343 sin.sin_len = sizeof(sin);
344 sin.sin_family = AF_INET;
345 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
346 sizeof(sin.sin_addr));
347 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
348 in6p->in6p_socket->so_options, NULL, &error);
349 if (sinp == 0) {
350 if (error == 0)
351 error = EADDRNOTAVAIL;
352 return(error);
353 }
354 bzero(&mapped, sizeof(mapped));
355 mapped.s6_addr16[5] = htons(0xffff);
356 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
357 in6a = &mapped;
358 #else
359 return EADDRNOTAVAIL;
360 #endif
361 } else
362 {
363 /*
364 * XXX: in6_selectsrc might replace the bound local address
365 * with the address specified by setsockopt(IPV6_PKTINFO).
366 * Is it the intended behavior?
367 */
368 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
369 in6p->in6p_moptions,
370 &in6p->in6p_route,
371 &in6p->in6p_laddr, &error);
372 if (in6a == 0) {
373 if (error == 0)
374 error = EADDRNOTAVAIL;
375 return(error);
376 }
377 }
378 if (in6p->in6p_route.ro_rt)
379 ifp = in6p->in6p_route.ro_rt->rt_ifp;
380
381 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
382
383 if (in6_pcblookup(in6p->in6p_head,
384 &sin6->sin6_addr,
385 sin6->sin6_port,
386 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ?
387 in6a : &in6p->in6p_laddr,
388 in6p->in6p_lport,
389 0))
390 return(EADDRINUSE);
391 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)
392 || (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)
393 && in6p->in6p_laddr.s6_addr32[3] == 0))
394 {
395 /*
396 * XXX in IPv4 mapped address case, we should grab fresh
397 * local port number by in_pcbbind, not in6_pcbbind.
398 * if we are in bad luck, we may assign conflicting port number
399 * between IPv4 and IPv6 unwillingly.
400 */
401 if (in6p->in6p_lport == 0) {
402 (void)in6_pcbbind(in6p, (struct mbuf *)0,
403 (struct proc *)0);
404 }
405 in6p->in6p_laddr = *in6a;
406 }
407 in6p->in6p_faddr = sin6->sin6_addr;
408 in6p->in6p_fport = sin6->sin6_port;
409 /*
410 * xxx kazu flowlabel is necessary for connect?
411 * but if this line is missing, the garbage value remains.
412 */
413 in6p->in6p_flowinfo = sin6->sin6_flowinfo;
414 #ifdef IPSEC
415 if (in6p->in6p_socket->so_type == SOCK_STREAM)
416 ipsec_pcbconn(in6p->in6p_sp);
417 #endif
418 return(0);
419 }
420
421 void
422 in6_pcbdisconnect(in6p)
423 struct in6pcb *in6p;
424 {
425 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
426 in6p->in6p_fport = 0;
427 if (in6p->in6p_socket->so_state & SS_NOFDREF)
428 in6_pcbdetach(in6p);
429 #ifdef IPSEC
430 ipsec_pcbdisconn(in6p->in6p_sp);
431 #endif
432 }
433
434 void
435 in6_pcbdetach(in6p)
436 struct in6pcb *in6p;
437 {
438 struct socket *so = in6p->in6p_socket;
439
440 #ifdef IPSEC
441 ipsec6_delete_pcbpolicy(in6p);
442 #endif /* IPSEC */
443 sotoin6pcb(so) = 0;
444 sofree(so);
445 if (in6p->in6p_options)
446 m_freem(in6p->in6p_options);
447 if (in6p->in6p_outputopts) {
448 if (in6p->in6p_outputopts->ip6po_rthdr &&
449 in6p->in6p_outputopts->ip6po_route.ro_rt)
450 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
451 if (in6p->in6p_outputopts->ip6po_m)
452 (void)m_free(in6p->in6p_outputopts->ip6po_m);
453 free(in6p->in6p_outputopts, M_IP6OPT);
454 }
455 if (in6p->in6p_route.ro_rt)
456 rtfree(in6p->in6p_route.ro_rt);
457 ip6_freemoptions(in6p->in6p_moptions);
458 in6p->in6p_next->in6p_prev = in6p->in6p_prev;
459 in6p->in6p_prev->in6p_next = in6p->in6p_next;
460 in6p->in6p_prev = NULL;
461 FREE(in6p, M_PCB);
462 }
463
464 void
465 in6_setsockaddr(in6p, nam)
466 struct in6pcb *in6p;
467 struct mbuf *nam;
468 {
469 struct sockaddr_in6 *sin6;
470
471 nam->m_len = sizeof(*sin6);
472 sin6 = mtod(nam, struct sockaddr_in6 *);
473 bzero((caddr_t)sin6, sizeof(*sin6));
474 sin6->sin6_family = AF_INET6;
475 sin6->sin6_len = sizeof(struct sockaddr_in6);
476 sin6->sin6_port = in6p->in6p_lport;
477 /* KAME hack: recover scopeid */
478 (void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
479 }
480
481 void
482 in6_setpeeraddr(in6p, nam)
483 struct in6pcb *in6p;
484 struct mbuf *nam;
485 {
486 struct sockaddr_in6 *sin6;
487
488 nam->m_len = sizeof(*sin6);
489 sin6 = mtod(nam, struct sockaddr_in6 *);
490 bzero((caddr_t)sin6, sizeof(*sin6));
491 sin6->sin6_family = AF_INET6;
492 sin6->sin6_len = sizeof(struct sockaddr_in6);
493 sin6->sin6_port = in6p->in6p_fport;
494 /* KAME hack: recover scopeid */
495 (void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
496 }
497
498 /*
499 * Pass some notification to all connections of a protocol
500 * associated with address dst. The local address and/or port numbers
501 * may be specified to limit the search. The "usual action" will be
502 * taken, depending on the ctlinput cmd. The caller must filter any
503 * cmds that are uninteresting (e.g., no error in the map).
504 * Call the protocol specific routine (if any) to report
505 * any errors for each matching socket.
506 *
507 * Must be called at splsoftnet.
508 *
509 * Note: src (4th arg) carries the flowlabel value on the original IPv6
510 * header, in sin6_flowinfo member.
511 */
512 int
513 in6_pcbnotify(head, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
514 struct in6pcb *head;
515 struct sockaddr *dst, *src;
516 u_int fport_arg, lport_arg;
517 int cmd;
518 void *cmdarg;
519 void (*notify) __P((struct in6pcb *, int));
520 {
521 struct in6pcb *in6p, *nin6p;
522 struct sockaddr_in6 sa6_src, *sa6_dst;
523 u_int16_t fport = fport_arg, lport = lport_arg;
524 int errno;
525 int nmatch = 0;
526 u_int32_t flowinfo;
527
528 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
529 return 0;
530
531 sa6_dst = (struct sockaddr_in6 *)dst;
532 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
533 return 0;
534
535 /*
536 * note that src can be NULL when we get notify by local fragmentation.
537 */
538 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
539 flowinfo = sa6_src.sin6_flowinfo;
540
541 /*
542 * Redirects go to all references to the destination,
543 * and use in6_rtchange to invalidate the route cache.
544 * Dead host indications: also use in6_rtchange to invalidate
545 * the cache, and deliver the error to all the sockets.
546 * Otherwise, if we have knowledge of the local port and address,
547 * deliver only to that socket.
548 */
549 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
550 fport = 0;
551 lport = 0;
552 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
553
554 if (cmd != PRC_HOSTDEAD)
555 notify = in6_rtchange;
556 }
557
558 errno = inet6ctlerrmap[cmd];
559 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
560 nin6p = in6p->in6p_next;
561
562 /*
563 * Under the following condition, notify of redirects
564 * to the pcb, without making address matches against inpcb.
565 * - redirect notification is arrived.
566 * - the inpcb is unconnected.
567 * - the inpcb is caching !RTF_HOST routing entry.
568 * - the ICMPv6 notification is from the gateway cached in the
569 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
570 * the inpcb used very recently.
571 *
572 * This is to improve interaction between netbsd/openbsd
573 * redirect handling code, and inpcb route cache code.
574 * without the clause, !RTF_HOST routing entry (which carries
575 * gateway used by inpcb right before the ICMPv6 redirect)
576 * will be cached forever in unconnected inpcb.
577 *
578 * There still is a question regarding to what is TRT:
579 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
580 * generated on packet output. inpcb will always cache
581 * RTF_HOST routing entry so there's no need for the clause
582 * (ICMPv6 redirect will update RTF_HOST routing entry,
583 * and inpcb is caching it already).
584 * However, bsdi/freebsd are vulnerable to local DoS attacks
585 * due to the cloned routing entries.
586 * - Specwise, "destination cache" is mentioned in RFC2461.
587 * Jinmei says that it implies bsdi/freebsd behavior, itojun
588 * is not really convinced.
589 * - Having hiwat/lowat on # of cloned host route (redirect/
590 * pmtud) may be a good idea. netbsd/openbsd has it. see
591 * icmp6_mtudisc_update().
592 */
593 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
594 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
595 in6p->in6p_route.ro_rt &&
596 !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) {
597 struct sockaddr_in6 *dst6;
598
599 dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst;
600 if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
601 &sa6_dst->sin6_addr))
602 goto do_notify;
603 }
604
605 /*
606 * Detect if we should notify the error. If no source and
607 * destination ports are specified, but non-zero flowinfo and
608 * local address match, notify the error. This is the case
609 * when the error is delivered with an encrypted buffer
610 * by ESP. Otherwise, just compare addresses and ports
611 * as usual.
612 */
613 if (lport == 0 && fport == 0 && flowinfo &&
614 in6p->in6p_socket != NULL &&
615 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
616 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
617 goto do_notify;
618 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
619 &sa6_dst->sin6_addr) ||
620 in6p->in6p_socket == 0 ||
621 (lport && in6p->in6p_lport != lport) ||
622 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
623 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
624 &sa6_src.sin6_addr)) ||
625 (fport && in6p->in6p_fport != fport))
626 continue;
627
628 do_notify:
629 if (notify)
630 (*notify)(in6p, errno);
631 nmatch++;
632 }
633 return nmatch;
634 }
635
636 void
637 in6_pcbpurgeif0(head, ifp)
638 struct in6pcb *head;
639 struct ifnet *ifp;
640 {
641 struct in6pcb *in6p, *nin6p;
642 struct ip6_moptions *im6o;
643 struct in6_multi_mship *imm, *nimm;
644
645 for (in6p = head->in6p_next; in6p != head; in6p = nin6p) {
646 nin6p = in6p->in6p_next;
647 im6o = in6p->in6p_moptions;
648 if (im6o) {
649 /*
650 * Unselect the outgoing interface if it is being
651 * detached.
652 */
653 if (im6o->im6o_multicast_ifp == ifp)
654 im6o->im6o_multicast_ifp = NULL;
655
656 /*
657 * Drop multicast group membership if we joined
658 * through the interface being detached.
659 * XXX controversial - is it really legal for kernel
660 * to force this?
661 */
662 for (imm = im6o->im6o_memberships.lh_first;
663 imm != NULL; imm = nimm) {
664 nimm = imm->i6mm_chain.le_next;
665 if (imm->i6mm_maddr->in6m_ifp == ifp) {
666 LIST_REMOVE(imm, i6mm_chain);
667 in6_delmulti(imm->i6mm_maddr);
668 free(imm, M_IPMADDR);
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