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