in6_pcb.c revision 1.56 1 /* $NetBSD: in6_pcb.c,v 1.56 2003/09/04 09:17:06 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. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
62 */
63
64 #include <sys/cdefs.h>
65 __KERNEL_RCSID(0, "$NetBSD: in6_pcb.c,v 1.56 2003/09/04 09:17:06 itojun Exp $");
66
67 #include "opt_inet.h"
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 #define IN6PCBHASH_BIND(table, laddr, lport) \
107 &(table)->inpt_bindhashtbl[ \
108 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
109 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + ntohs(lport)) & \
110 (table)->inpt_bindhash]
111 #define IN6PCBHASH_CONNECT(table, faddr, fport, laddr, lport) \
112 &(table)->inpt_bindhashtbl[ \
113 ((((faddr)->s6_addr32[0] ^ (faddr)->s6_addr32[1] ^ \
114 (faddr)->s6_addr32[2] ^ (faddr)->s6_addr32[3]) + ntohs(fport)) + \
115 (((laddr)->s6_addr32[0] ^ (laddr)->s6_addr32[1] ^ \
116 (laddr)->s6_addr32[2] ^ (laddr)->s6_addr32[3]) + \
117 ntohs(lport))) & (table)->inpt_bindhash]
118
119 int ip6_anonportmin = IPV6PORT_ANONMIN;
120 int ip6_anonportmax = IPV6PORT_ANONMAX;
121 int ip6_lowportmin = IPV6PORT_RESERVEDMIN;
122 int ip6_lowportmax = IPV6PORT_RESERVEDMAX;
123
124 struct pool in6pcb_pool;
125
126 void
127 in6_pcbinit(table, bindhashsize, connecthashsize)
128 struct inpcbtable *table;
129 int bindhashsize, connecthashsize;
130 {
131 static int in6pcb_pool_initialized;
132
133 if (in6pcb_pool_initialized == 0) {
134 pool_init(&in6pcb_pool, sizeof(struct in6pcb), 0, 0, 0,
135 "in6pcbpl", NULL);
136 in6pcb_pool_initialized = 1;
137 }
138
139 in_pcbinit(table, bindhashsize, connecthashsize);
140 table->inpt_lastport = (u_int16_t)ip6_anonportmax;
141 }
142
143 int
144 in6_pcballoc(so, v)
145 struct socket *so;
146 void *v;
147 {
148 struct inpcbtable *table = v;
149 struct in6pcb *in6p;
150 int s;
151 #ifdef IPSEC
152 int error;
153 #endif
154
155 in6p = pool_get(&in6pcb_pool, PR_NOWAIT);
156 if (in6p == NULL)
157 return (ENOBUFS);
158 bzero((caddr_t)in6p, sizeof(*in6p));
159 in6p->in6p_af = AF_INET6;
160 in6p->in6p_table = table;
161 in6p->in6p_socket = so;
162 in6p->in6p_hops = -1; /* use kernel default */
163 in6p->in6p_icmp6filt = NULL;
164 #ifdef IPSEC
165 error = ipsec_init_pcbpolicy(so, &in6p->in6p_sp);
166 if (error != 0) {
167 pool_put(&in6pcb_pool, in6p);
168 return error;
169 }
170 #endif /* IPSEC */
171 s = splnet();
172 CIRCLEQ_INSERT_HEAD(&table->inpt_queue, (struct inpcb_hdr*)in6p,
173 inph_queue);
174 in6_pcbstate(in6p, IN6P_ATTACHED);
175 splx(s);
176 if (ip6_v6only)
177 in6p->in6p_flags |= IN6P_IPV6_V6ONLY;
178 so->so_pcb = (caddr_t)in6p;
179 return (0);
180 }
181
182 int
183 in6_pcbbind(v, nam, p)
184 void *v;
185 struct mbuf *nam;
186 struct proc *p;
187 {
188 struct in6pcb *in6p = v;
189 struct socket *so = in6p->in6p_socket;
190 struct inpcbtable *table = in6p->in6p_table;
191 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
192 u_int16_t lport = 0;
193 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
194
195 if (in6p->in6p_af != AF_INET6)
196 return (EINVAL);
197
198 if (in6p->in6p_lport || !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
199 return (EINVAL);
200 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0 &&
201 ((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0 ||
202 (so->so_options & SO_ACCEPTCONN) == 0))
203 wild = 1;
204 if (nam) {
205 sin6 = mtod(nam, struct sockaddr_in6 *);
206 if (nam->m_len != sizeof(*sin6))
207 return (EINVAL);
208 /*
209 * We should check the family, but old programs
210 * incorrectly fail to intialize it.
211 */
212 if (sin6->sin6_family != AF_INET6)
213 return (EAFNOSUPPORT);
214
215 #ifndef INET
216 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
217 return (EADDRNOTAVAIL);
218 #endif
219
220 /* KAME hack: embed scopeid */
221 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, NULL) != 0)
222 return EINVAL;
223 /* this must be cleared for ifa_ifwithaddr() */
224 sin6->sin6_scope_id = 0;
225
226 lport = sin6->sin6_port;
227 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
228 /*
229 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
230 * allow compepte duplication of binding if
231 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
232 * and a multicast address is bound on both
233 * new and duplicated sockets.
234 */
235 if (so->so_options & SO_REUSEADDR)
236 reuseport = SO_REUSEADDR|SO_REUSEPORT;
237 }
238 else if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
239 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
240 return (EINVAL);
241 if (sin6->sin6_addr.s6_addr32[3]) {
242 struct sockaddr_in sin;
243
244 bzero(&sin, sizeof(sin));
245 sin.sin_len = sizeof(sin);
246 sin.sin_family = AF_INET;
247 bcopy(&sin6->sin6_addr.s6_addr32[3],
248 &sin.sin_addr, sizeof(sin.sin_addr));
249 if (ifa_ifwithaddr((struct sockaddr *)&sin) == 0)
250 return EADDRNOTAVAIL;
251 }
252 }
253 else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
254 struct ifaddr *ia = NULL;
255
256 sin6->sin6_port = 0; /* yech... */
257 if ((in6p->in6p_flags & IN6P_FAITH) == 0 &&
258 (ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
259 return (EADDRNOTAVAIL);
260
261 /*
262 * bind to an anycast address might accidentally
263 * cause sending a packet with an anycast source
264 * address, so we forbid it.
265 *
266 * We should allow to bind to a deprecated address,
267 * since the application dare to use it.
268 * But, can we assume that they are careful enough
269 * to check if the address is deprecated or not?
270 * Maybe, as a safeguard, we should have a setsockopt
271 * flag to control the bind(2) behavior against
272 * deprecated addresses (default: forbid bind(2)).
273 */
274 if (ia &&
275 ((struct in6_ifaddr *)ia)->ia6_flags &
276 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED))
277 return (EADDRNOTAVAIL);
278 }
279 if (lport) {
280 #ifndef IPNOPRIVPORTS
281 int priv;
282
283 /*
284 * NOTE: all operating systems use suser() for
285 * privilege check! do not rewrite it into SS_PRIV.
286 */
287 priv = (p && !suser(p->p_ucred, &p->p_acflag)) ? 1 : 0;
288 /* GROSS */
289 if (ntohs(lport) < IPV6PORT_RESERVED && !priv)
290 return (EACCES);
291 #endif
292
293 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
294 #ifdef INET
295 struct inpcb *t;
296
297 t = in_pcblookup_port(table,
298 *(struct in_addr *)&sin6->sin6_addr.s6_addr32[3],
299 lport, wild);
300 if (t && (reuseport & t->inp_socket->so_options) == 0)
301 return (EADDRINUSE);
302 #else
303 return (EADDRNOTAVAIL);
304 #endif
305 }
306
307 {
308 struct in6pcb *t;
309
310 t = in6_pcblookup_port(table, &sin6->sin6_addr,
311 lport, wild);
312 if (t && (reuseport & t->in6p_socket->so_options) == 0)
313 return (EADDRINUSE);
314 }
315 }
316 in6p->in6p_laddr = sin6->sin6_addr;
317 }
318
319 if (lport == 0) {
320 int e;
321 e = in6_pcbsetport(&in6p->in6p_laddr, in6p, p);
322 if (e != 0)
323 return (e);
324 } else {
325 in6p->in6p_lport = lport;
326 in6_pcbstate(in6p, IN6P_BOUND);
327 }
328
329 in6p->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/
330 return (0);
331 }
332
333 /*
334 * Connect from a socket to a specified address.
335 * Both address and port must be specified in argument sin6.
336 * If don't have a local address for this socket yet,
337 * then pick one.
338 */
339 int
340 in6_pcbconnect(v, nam)
341 void *v;
342 struct mbuf *nam;
343 {
344 struct in6pcb *in6p = v;
345 struct in6_addr *in6a = NULL;
346 struct sockaddr_in6 *sin6 = mtod(nam, struct sockaddr_in6 *);
347 struct ifnet *ifp = NULL; /* outgoing interface */
348 int error = 0;
349 #ifdef INET
350 struct in6_addr mapped;
351 #endif
352 struct sockaddr_in6 tmp;
353
354 (void)&in6a; /* XXX fool gcc */
355
356 if (in6p->in6p_af != AF_INET6)
357 return (EINVAL);
358
359 if (nam->m_len != sizeof(*sin6))
360 return (EINVAL);
361 if (sin6->sin6_family != AF_INET6)
362 return (EAFNOSUPPORT);
363 if (sin6->sin6_port == 0)
364 return (EADDRNOTAVAIL);
365
366 /* sanity check for mapped address case */
367 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
368 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
369 return EINVAL;
370 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
371 in6p->in6p_laddr.s6_addr16[5] = htons(0xffff);
372 if (!IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
373 return EINVAL;
374 } else
375 {
376 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
377 return EINVAL;
378 }
379
380 /* protect *sin6 from overwrites */
381 tmp = *sin6;
382 sin6 = &tmp;
383
384 /* KAME hack: embed scopeid */
385 if (in6_embedscope(&sin6->sin6_addr, sin6, in6p, &ifp) != 0)
386 return EINVAL;
387
388 /* Source address selection. */
389 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
390 in6p->in6p_laddr.s6_addr32[3] == 0) {
391 #ifdef INET
392 struct sockaddr_in sin, *sinp;
393
394 bzero(&sin, sizeof(sin));
395 sin.sin_len = sizeof(sin);
396 sin.sin_family = AF_INET;
397 bcopy(&sin6->sin6_addr.s6_addr32[3], &sin.sin_addr,
398 sizeof(sin.sin_addr));
399 sinp = in_selectsrc(&sin, (struct route *)&in6p->in6p_route,
400 in6p->in6p_socket->so_options, NULL, &error);
401 if (sinp == 0) {
402 if (error == 0)
403 error = EADDRNOTAVAIL;
404 return (error);
405 }
406 bzero(&mapped, sizeof(mapped));
407 mapped.s6_addr16[5] = htons(0xffff);
408 bcopy(&sinp->sin_addr, &mapped.s6_addr32[3], sizeof(sinp->sin_addr));
409 in6a = &mapped;
410 #else
411 return EADDRNOTAVAIL;
412 #endif
413 } else
414 {
415 /*
416 * XXX: in6_selectsrc might replace the bound local address
417 * with the address specified by setsockopt(IPV6_PKTINFO).
418 * Is it the intended behavior?
419 */
420 in6a = in6_selectsrc(sin6, in6p->in6p_outputopts,
421 in6p->in6p_moptions,
422 &in6p->in6p_route,
423 &in6p->in6p_laddr, &error);
424 if (in6a == 0) {
425 if (error == 0)
426 error = EADDRNOTAVAIL;
427 return (error);
428 }
429 }
430 if (in6p->in6p_route.ro_rt)
431 ifp = in6p->in6p_route.ro_rt->rt_ifp;
432
433 in6p->in6p_ip6.ip6_hlim = (u_int8_t)in6_selecthlim(in6p, ifp);
434
435 if (in6_pcblookup_connect(in6p->in6p_table, &sin6->sin6_addr,
436 sin6->sin6_port,
437 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ? in6a : &in6p->in6p_laddr,
438 in6p->in6p_lport, 0))
439 return (EADDRINUSE);
440 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) ||
441 (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr) &&
442 in6p->in6p_laddr.s6_addr32[3] == 0))
443 {
444 if (in6p->in6p_lport == 0) {
445 (void)in6_pcbbind(in6p, (struct mbuf *)0,
446 (struct proc *)0);
447 }
448 in6p->in6p_laddr = *in6a;
449 }
450 in6p->in6p_faddr = sin6->sin6_addr;
451 in6p->in6p_fport = sin6->sin6_port;
452 in6_pcbstate(in6p, IN6P_CONNECTED);
453 /*
454 * xxx kazu flowlabel is necessary for connect?
455 * but if this line is missing, the garbage value remains.
456 */
457 in6p->in6p_flowinfo = sin6->sin6_flowinfo;
458 #ifdef IPSEC
459 if (in6p->in6p_socket->so_type == SOCK_STREAM)
460 ipsec_pcbconn(in6p->in6p_sp);
461 #endif
462 return (0);
463 }
464
465 void
466 in6_pcbdisconnect(in6p)
467 struct in6pcb *in6p;
468 {
469 bzero((caddr_t)&in6p->in6p_faddr, sizeof(in6p->in6p_faddr));
470 in6p->in6p_fport = 0;
471 in6_pcbstate(in6p, IN6P_BOUND);
472 if (in6p->in6p_socket->so_state & SS_NOFDREF)
473 in6_pcbdetach(in6p);
474 #ifdef IPSEC
475 ipsec_pcbdisconn(in6p->in6p_sp);
476 #endif
477 }
478
479 void
480 in6_pcbdetach(in6p)
481 struct in6pcb *in6p;
482 {
483 struct socket *so = in6p->in6p_socket;
484 int s;
485
486 if (in6p->in6p_af != AF_INET6)
487 return;
488
489 #ifdef IPSEC
490 ipsec6_delete_pcbpolicy(in6p);
491 #endif /* IPSEC */
492 sotoin6pcb(so) = 0;
493 sofree(so);
494 if (in6p->in6p_options)
495 m_freem(in6p->in6p_options);
496 if (in6p->in6p_outputopts) {
497 if (in6p->in6p_outputopts->ip6po_rthdr &&
498 in6p->in6p_outputopts->ip6po_route.ro_rt)
499 RTFREE(in6p->in6p_outputopts->ip6po_route.ro_rt);
500 if (in6p->in6p_outputopts->ip6po_m)
501 (void)m_free(in6p->in6p_outputopts->ip6po_m);
502 free(in6p->in6p_outputopts, M_IP6OPT);
503 }
504 if (in6p->in6p_route.ro_rt)
505 rtfree(in6p->in6p_route.ro_rt);
506 ip6_freemoptions(in6p->in6p_moptions);
507 s = splnet();
508 in6_pcbstate(in6p, IN6P_ATTACHED);
509 CIRCLEQ_REMOVE(&in6p->in6p_table->inpt_queue, &in6p->in6p_head,
510 inph_queue);
511 splx(s);
512 pool_put(&in6pcb_pool, in6p);
513 }
514
515 void
516 in6_setsockaddr(in6p, nam)
517 struct in6pcb *in6p;
518 struct mbuf *nam;
519 {
520 struct sockaddr_in6 *sin6;
521
522 if (in6p->in6p_af != AF_INET6)
523 return;
524
525 nam->m_len = sizeof(*sin6);
526 sin6 = mtod(nam, struct sockaddr_in6 *);
527 bzero((caddr_t)sin6, sizeof(*sin6));
528 sin6->sin6_family = AF_INET6;
529 sin6->sin6_len = sizeof(struct sockaddr_in6);
530 sin6->sin6_port = in6p->in6p_lport;
531 /* KAME hack: recover scopeid */
532 (void)in6_recoverscope(sin6, &in6p->in6p_laddr, NULL);
533 }
534
535 void
536 in6_setpeeraddr(in6p, nam)
537 struct in6pcb *in6p;
538 struct mbuf *nam;
539 {
540 struct sockaddr_in6 *sin6;
541
542 if (in6p->in6p_af != AF_INET6)
543 return;
544
545 nam->m_len = sizeof(*sin6);
546 sin6 = mtod(nam, struct sockaddr_in6 *);
547 bzero((caddr_t)sin6, sizeof(*sin6));
548 sin6->sin6_family = AF_INET6;
549 sin6->sin6_len = sizeof(struct sockaddr_in6);
550 sin6->sin6_port = in6p->in6p_fport;
551 /* KAME hack: recover scopeid */
552 (void)in6_recoverscope(sin6, &in6p->in6p_faddr, NULL);
553 }
554
555 /*
556 * Pass some notification to all connections of a protocol
557 * associated with address dst. The local address and/or port numbers
558 * may be specified to limit the search. The "usual action" will be
559 * taken, depending on the ctlinput cmd. The caller must filter any
560 * cmds that are uninteresting (e.g., no error in the map).
561 * Call the protocol specific routine (if any) to report
562 * any errors for each matching socket.
563 *
564 * Must be called at splsoftnet.
565 *
566 * Note: src (4th arg) carries the flowlabel value on the original IPv6
567 * header, in sin6_flowinfo member.
568 */
569 int
570 in6_pcbnotify(table, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
571 struct inpcbtable *table;
572 struct sockaddr *dst, *src;
573 u_int fport_arg, lport_arg;
574 int cmd;
575 void *cmdarg;
576 void (*notify) __P((struct in6pcb *, int));
577 {
578 struct in6pcb *in6p, *nin6p;
579 struct sockaddr_in6 sa6_src, *sa6_dst;
580 u_int16_t fport = fport_arg, lport = lport_arg;
581 int errno;
582 int nmatch = 0;
583 u_int32_t flowinfo;
584
585 if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6)
586 return 0;
587
588 sa6_dst = (struct sockaddr_in6 *)dst;
589 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
590 return 0;
591
592 /*
593 * note that src can be NULL when we get notify by local fragmentation.
594 */
595 sa6_src = (src == NULL) ? sa6_any : *(struct sockaddr_in6 *)src;
596 flowinfo = sa6_src.sin6_flowinfo;
597
598 /*
599 * Redirects go to all references to the destination,
600 * and use in6_rtchange to invalidate the route cache.
601 * Dead host indications: also use in6_rtchange to invalidate
602 * the cache, and deliver the error to all the sockets.
603 * Otherwise, if we have knowledge of the local port and address,
604 * deliver only to that socket.
605 */
606 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
607 fport = 0;
608 lport = 0;
609 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
610
611 if (cmd != PRC_HOSTDEAD)
612 notify = in6_rtchange;
613 }
614
615 errno = inet6ctlerrmap[cmd];
616 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue);
617 in6p != (void *)&table->inpt_queue;
618 in6p = nin6p) {
619 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue);
620
621 if (in6p->in6p_af != AF_INET6)
622 continue;
623
624 /*
625 * Under the following condition, notify of redirects
626 * to the pcb, without making address matches against inpcb.
627 * - redirect notification is arrived.
628 * - the inpcb is unconnected.
629 * - the inpcb is caching !RTF_HOST routing entry.
630 * - the ICMPv6 notification is from the gateway cached in the
631 * inpcb. i.e. ICMPv6 notification is from nexthop gateway
632 * the inpcb used very recently.
633 *
634 * This is to improve interaction between netbsd/openbsd
635 * redirect handling code, and inpcb route cache code.
636 * without the clause, !RTF_HOST routing entry (which carries
637 * gateway used by inpcb right before the ICMPv6 redirect)
638 * will be cached forever in unconnected inpcb.
639 *
640 * There still is a question regarding to what is TRT:
641 * - On bsdi/freebsd, RTF_HOST (cloned) routing entry will be
642 * generated on packet output. inpcb will always cache
643 * RTF_HOST routing entry so there's no need for the clause
644 * (ICMPv6 redirect will update RTF_HOST routing entry,
645 * and inpcb is caching it already).
646 * However, bsdi/freebsd are vulnerable to local DoS attacks
647 * due to the cloned routing entries.
648 * - Specwise, "destination cache" is mentioned in RFC2461.
649 * Jinmei says that it implies bsdi/freebsd behavior, itojun
650 * is not really convinced.
651 * - Having hiwat/lowat on # of cloned host route (redirect/
652 * pmtud) may be a good idea. netbsd/openbsd has it. see
653 * icmp6_mtudisc_update().
654 */
655 if ((PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) &&
656 IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
657 in6p->in6p_route.ro_rt &&
658 !(in6p->in6p_route.ro_rt->rt_flags & RTF_HOST)) {
659 struct sockaddr_in6 *dst6;
660
661 dst6 = (struct sockaddr_in6 *)&in6p->in6p_route.ro_dst;
662 if (IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr,
663 &sa6_dst->sin6_addr))
664 goto do_notify;
665 }
666
667 /*
668 * Detect if we should notify the error. If no source and
669 * destination ports are specified, but non-zero flowinfo and
670 * local address match, notify the error. This is the case
671 * when the error is delivered with an encrypted buffer
672 * by ESP. Otherwise, just compare addresses and ports
673 * as usual.
674 */
675 if (lport == 0 && fport == 0 && flowinfo &&
676 in6p->in6p_socket != NULL &&
677 flowinfo == (in6p->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
678 IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &sa6_src.sin6_addr))
679 goto do_notify;
680 else if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
681 &sa6_dst->sin6_addr) ||
682 in6p->in6p_socket == 0 ||
683 (lport && in6p->in6p_lport != lport) ||
684 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
685 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
686 &sa6_src.sin6_addr)) ||
687 (fport && in6p->in6p_fport != fport))
688 continue;
689
690 do_notify:
691 if (notify)
692 (*notify)(in6p, errno);
693 nmatch++;
694 }
695 return nmatch;
696 }
697
698 void
699 in6_pcbpurgeif0(table, ifp)
700 struct inpcbtable *table;
701 struct ifnet *ifp;
702 {
703 struct in6pcb *in6p, *nin6p;
704 struct ip6_moptions *im6o;
705 struct in6_multi_mship *imm, *nimm;
706
707 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue);
708 in6p != (void *)&table->inpt_queue;
709 in6p = nin6p) {
710 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue);
711 if (in6p->in6p_af != AF_INET6)
712 continue;
713
714 im6o = in6p->in6p_moptions;
715 if (im6o) {
716 /*
717 * Unselect the outgoing interface if it is being
718 * detached.
719 */
720 if (im6o->im6o_multicast_ifp == ifp)
721 im6o->im6o_multicast_ifp = NULL;
722
723 /*
724 * Drop multicast group membership if we joined
725 * through the interface being detached.
726 * XXX controversial - is it really legal for kernel
727 * to force this?
728 */
729 for (imm = im6o->im6o_memberships.lh_first;
730 imm != NULL; imm = nimm) {
731 nimm = imm->i6mm_chain.le_next;
732 if (imm->i6mm_maddr->in6m_ifp == ifp) {
733 LIST_REMOVE(imm, i6mm_chain);
734 in6_leavegroup(imm);
735 }
736 }
737 }
738 }
739 }
740
741 void
742 in6_pcbpurgeif(table, ifp)
743 struct inpcbtable *table;
744 struct ifnet *ifp;
745 {
746 struct in6pcb *in6p, *nin6p;
747
748 for (in6p = (struct in6pcb *)CIRCLEQ_FIRST(&table->inpt_queue);
749 in6p != (void *)&table->inpt_queue;
750 in6p = nin6p) {
751 nin6p = (struct in6pcb *)CIRCLEQ_NEXT(in6p, in6p_queue);
752 if (in6p->in6p_af != AF_INET6)
753 continue;
754 if (in6p->in6p_route.ro_rt != NULL &&
755 in6p->in6p_route.ro_rt->rt_ifp == ifp)
756 in6_rtchange(in6p, 0);
757 }
758 }
759
760 /*
761 * Check for alternatives when higher level complains
762 * about service problems. For now, invalidate cached
763 * routing information. If the route was created dynamically
764 * (by a redirect), time to try a default gateway again.
765 */
766 void
767 in6_losing(in6p)
768 struct in6pcb *in6p;
769 {
770 struct rtentry *rt;
771 struct rt_addrinfo info;
772
773 if (in6p->in6p_af != AF_INET6)
774 return;
775
776 if ((rt = in6p->in6p_route.ro_rt) != NULL) {
777 in6p->in6p_route.ro_rt = 0;
778 bzero((caddr_t)&info, sizeof(info));
779 info.rti_info[RTAX_DST] =
780 (struct sockaddr *)&in6p->in6p_route.ro_dst;
781 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
782 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
783 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
784 if (rt->rt_flags & RTF_DYNAMIC) {
785 (void)rtrequest(RTM_DELETE, rt_key(rt),
786 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
787 (struct rtentry **)0);
788 } else {
789 /*
790 * A new route can be allocated
791 * the next time output is attempted.
792 */
793 rtfree(rt);
794 }
795 }
796 }
797
798 /*
799 * After a routing change, flush old routing
800 * and allocate a (hopefully) better one.
801 */
802 void
803 in6_rtchange(in6p, errno)
804 struct in6pcb *in6p;
805 int errno;
806 {
807 if (in6p->in6p_af != AF_INET6)
808 return;
809
810 if (in6p->in6p_route.ro_rt) {
811 rtfree(in6p->in6p_route.ro_rt);
812 in6p->in6p_route.ro_rt = 0;
813 /*
814 * A new route can be allocated the next time
815 * output is attempted.
816 */
817 }
818 }
819
820 struct in6pcb *
821 in6_pcblookup_port(table, laddr6, lport_arg, lookup_wildcard)
822 struct inpcbtable *table;
823 struct in6_addr *laddr6;
824 u_int lport_arg;
825 int lookup_wildcard;
826 {
827 struct inpcb_hdr *inph;
828 struct in6pcb *in6p, *match = 0;
829 int matchwild = 3, wildcard;
830 u_int16_t lport = lport_arg;
831
832 CIRCLEQ_FOREACH(inph, &table->inpt_queue, inph_queue) {
833 in6p = (struct in6pcb *)inph;
834 if (in6p->in6p_af != AF_INET6)
835 continue;
836
837 if (in6p->in6p_lport != lport)
838 continue;
839 wildcard = 0;
840 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
841 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
842 continue;
843 }
844 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
845 wildcard++;
846 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr)) {
847 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
848 continue;
849 if (!IN6_IS_ADDR_V4MAPPED(laddr6))
850 continue;
851
852 /* duplicate of IPv4 logic */
853 wildcard = 0;
854 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr) &&
855 in6p->in6p_faddr.s6_addr32[3])
856 wildcard++;
857 if (!in6p->in6p_laddr.s6_addr32[3]) {
858 if (laddr6->s6_addr32[3])
859 wildcard++;
860 } else {
861 if (!laddr6->s6_addr32[3])
862 wildcard++;
863 else {
864 if (in6p->in6p_laddr.s6_addr32[3] !=
865 laddr6->s6_addr32[3])
866 continue;
867 }
868 }
869 } else if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
870 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
871 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
872 continue;
873 }
874 if (!IN6_IS_ADDR_UNSPECIFIED(laddr6))
875 wildcard++;
876 } else {
877 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
878 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
879 continue;
880 }
881 if (IN6_IS_ADDR_UNSPECIFIED(laddr6))
882 wildcard++;
883 else {
884 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr,
885 laddr6))
886 continue;
887 }
888 }
889 if (wildcard && !lookup_wildcard)
890 continue;
891 if (wildcard < matchwild) {
892 match = in6p;
893 matchwild = wildcard;
894 if (matchwild == 0)
895 break;
896 }
897 }
898 return (match);
899 }
900 #undef continue
901
902 /*
903 * WARNING: return value (rtentry) could be IPv4 one if in6pcb is connected to
904 * IPv4 mapped address.
905 */
906 struct rtentry *
907 in6_pcbrtentry(in6p)
908 struct in6pcb *in6p;
909 {
910 struct route_in6 *ro;
911 struct sockaddr_in6 *dst6;
912
913 ro = &in6p->in6p_route;
914 dst6 = (struct sockaddr_in6 *)&ro->ro_dst;
915
916 if (in6p->in6p_af != AF_INET6)
917 return (NULL);
918
919 if (ro->ro_rt && ((ro->ro_rt->rt_flags & RTF_UP) == 0 ||
920 !IN6_ARE_ADDR_EQUAL(&dst6->sin6_addr, &in6p->in6p_faddr))) {
921 RTFREE(ro->ro_rt);
922 ro->ro_rt = (struct rtentry *)NULL;
923 }
924 #ifdef INET
925 if (ro->ro_rt == (struct rtentry *)NULL &&
926 IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
927 struct sockaddr_in *dst = (struct sockaddr_in *)&ro->ro_dst;
928
929 bzero(dst, sizeof(*dst));
930 dst->sin_family = AF_INET;
931 dst->sin_len = sizeof(struct sockaddr_in);
932 bcopy(&in6p->in6p_faddr.s6_addr32[3], &dst->sin_addr,
933 sizeof(dst->sin_addr));
934 rtalloc((struct route *)ro);
935 } else
936 #endif
937 if (ro->ro_rt == (struct rtentry *)NULL &&
938 !IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
939 bzero(dst6, sizeof(*dst6));
940 dst6->sin6_family = AF_INET6;
941 dst6->sin6_len = sizeof(struct sockaddr_in6);
942 dst6->sin6_addr = in6p->in6p_faddr;
943 rtalloc((struct route *)ro);
944 }
945 return (ro->ro_rt);
946 }
947
948 struct in6pcb *
949 in6_pcblookup_connect(table, faddr6, fport_arg, laddr6, lport_arg, faith)
950 struct inpcbtable *table;
951 struct in6_addr *faddr6, *laddr6;
952 u_int fport_arg, lport_arg;
953 int faith;
954 {
955 struct inpcbhead *head;
956 struct inpcb_hdr *inph;
957 struct in6pcb *in6p;
958 u_int16_t fport = fport_arg, lport = lport_arg;
959
960 head = IN6PCBHASH_CONNECT(table, faddr6, fport, laddr6, lport);
961 LIST_FOREACH(inph, head, inph_hash) {
962 in6p = (struct in6pcb *)inph;
963 if (in6p->in6p_af != AF_INET6)
964 continue;
965
966 /* find exact match on both source and dest */
967 if (in6p->in6p_fport != fport)
968 continue;
969 if (in6p->in6p_lport != lport)
970 continue;
971 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr))
972 continue;
973 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, faddr6))
974 continue;
975 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr))
976 continue;
977 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
978 continue;
979 if ((IN6_IS_ADDR_V4MAPPED(laddr6) ||
980 IN6_IS_ADDR_V4MAPPED(faddr6)) &&
981 (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
982 continue;
983 return in6p;
984 }
985 return NULL;
986 }
987
988 struct in6pcb *
989 in6_pcblookup_bind(table, laddr6, lport_arg, faith)
990 struct inpcbtable *table;
991 struct in6_addr *laddr6;
992 u_int lport_arg;
993 int faith;
994 {
995 struct inpcbhead *head;
996 struct inpcb_hdr *inph;
997 struct in6pcb *in6p;
998 u_int16_t lport = lport_arg;
999 #ifdef INET6
1000 struct in6_addr zero_mapped;
1001 #endif
1002
1003 head = IN6PCBHASH_BIND(table, laddr6, lport);
1004 LIST_FOREACH(inph, head, inph_hash) {
1005 in6p = (struct in6pcb *)inph;
1006 if (in6p->in6p_af != AF_INET6)
1007 continue;
1008
1009 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1010 continue;
1011 if (in6p->in6p_fport != 0)
1012 continue;
1013 if (in6p->in6p_lport != lport)
1014 continue;
1015 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1016 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1017 continue;
1018 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, laddr6))
1019 goto out;
1020 }
1021 #ifdef INET
1022 if (IN6_IS_ADDR_V4MAPPED(laddr6)) {
1023 memset(&zero_mapped, 0, sizeof(zero_mapped));
1024 zero_mapped.s6_addr16[5] = 0xffff;
1025 head = IN6PCBHASH_BIND(table, &zero_mapped, lport);
1026 LIST_FOREACH(inph, head, inph_hash) {
1027 in6p = (struct in6pcb *)inph;
1028 if (in6p->in6p_af != AF_INET6)
1029 continue;
1030
1031 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1032 continue;
1033 if (in6p->in6p_fport != 0)
1034 continue;
1035 if (in6p->in6p_lport != lport)
1036 continue;
1037 if ((in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1038 continue;
1039 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zero_mapped))
1040 goto out;
1041 }
1042 }
1043 #endif
1044 head = IN6PCBHASH_BIND(table, &zeroin6_addr, lport);
1045 LIST_FOREACH(inph, head, inph_hash) {
1046 in6p = (struct in6pcb *)inph;
1047 if (in6p->in6p_af != AF_INET6)
1048 continue;
1049
1050 if (faith && (in6p->in6p_flags & IN6P_FAITH) == 0)
1051 continue;
1052 if (in6p->in6p_fport != 0)
1053 continue;
1054 if (in6p->in6p_lport != lport)
1055 continue;
1056 if (IN6_IS_ADDR_V4MAPPED(laddr6) &&
1057 (in6p->in6p_flags & IN6P_IPV6_V6ONLY) != 0)
1058 continue;
1059 if (IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &zeroin6_addr))
1060 goto out;
1061 }
1062 return (NULL);
1063
1064 out:
1065 inph = &in6p->in6p_head;
1066 if (inph != LIST_FIRST(head)) {
1067 LIST_REMOVE(inph, inph_hash);
1068 LIST_INSERT_HEAD(head, inph, inph_hash);
1069 }
1070 return in6p;
1071 }
1072
1073 void
1074 in6_pcbstate(in6p, state)
1075 struct in6pcb *in6p;
1076 int state;
1077 {
1078
1079 if (in6p->in6p_af != AF_INET6)
1080 return;
1081
1082 if (in6p->in6p_state > IN6P_ATTACHED)
1083 LIST_REMOVE(&in6p->in6p_head, inph_hash);
1084
1085 switch (state) {
1086 case IN6P_BOUND:
1087 LIST_INSERT_HEAD(IN6PCBHASH_BIND(in6p->in6p_table,
1088 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1089 inph_hash);
1090 break;
1091 case IN6P_CONNECTED:
1092 LIST_INSERT_HEAD(IN6PCBHASH_CONNECT(in6p->in6p_table,
1093 &in6p->in6p_faddr, in6p->in6p_fport,
1094 &in6p->in6p_laddr, in6p->in6p_lport), &in6p->in6p_head,
1095 inph_hash);
1096 break;
1097 }
1098
1099 in6p->in6p_state = state;
1100 }
1101