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