1 /* $FreeBSD: releng/6.1/sys/netinet6/in6_pcb.c 158179 2006-04-30 16:44:43Z cvs2svn $ */
2 /* $KAME: in6_pcb.c,v 1.31 2001/05/21 05:45:10 jinmei 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 /*-
35 * Copyright (c) 1982, 1986, 1991, 1993
36 * The Regents of the University of California. All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)in_pcb.c 8.2 (Berkeley) 1/4/94
63 */
64
65 #include "opt_inet.h"
66 #include "opt_inet6.h"
67 #include "opt_ipsec.h"
68
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/domain.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sockio.h>
78 #include <sys/errno.h>
79 #include <sys/time.h>
80 #include <sys/proc.h>
81 #include <sys/jail.h>
82
83 #include <vm/uma.h>
84
85 #include <net/if.h>
86 #include <net/if_types.h>
87 #include <net/route.h>
88
89 #include <netinet/in.h>
90 #include <netinet/in_var.h>
91 #include <netinet/in_systm.h>
92 #include <netinet/tcp_var.h>
93 #include <netinet/ip6.h>
94 #include <netinet/ip_var.h>
95 #include <netinet6/ip6_var.h>
96 #include <netinet6/nd6.h>
97 #include <netinet/in_pcb.h>
98 #include <netinet6/in6_pcb.h>
99 #include <netinet6/scope6_var.h>
100
101 #ifdef IPSEC
102 #include <netinet6/ipsec.h>
103 #ifdef INET6
104 #include <netinet6/ipsec6.h>
105 #endif
106 #include <netinet6/ah.h>
107 #ifdef INET6
108 #include <netinet6/ah6.h>
109 #endif
110 #include <netkey/key.h>
111 #endif /* IPSEC */
112
113 #ifdef FAST_IPSEC
114 #include <netipsec/ipsec.h>
115 #include <netipsec/ipsec6.h>
116 #include <netipsec/key.h>
117 #endif /* FAST_IPSEC */
118
119 struct in6_addr zeroin6_addr;
120
121 int
122 in6_pcbbind(inp, nam, cred)
123 register struct inpcb *inp;
124 struct sockaddr *nam;
125 struct ucred *cred;
126 {
127 struct socket *so = inp->inp_socket;
128 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL;
129 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
130 u_short lport = 0;
131 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
132
133 INP_INFO_WLOCK_ASSERT(pcbinfo);
134 INP_LOCK_ASSERT(inp);
135
136 if (!in6_ifaddr) /* XXX broken! */
137 return (EADDRNOTAVAIL);
138 if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
139 return (EINVAL);
140 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
141 wild = 1;
142 if (nam) {
143 int error;
144
145 sin6 = (struct sockaddr_in6 *)nam;
146 if (nam->sa_len != sizeof(*sin6))
147 return (EINVAL);
148 /*
149 * family check.
150 */
151 if (nam->sa_family != AF_INET6)
152 return (EAFNOSUPPORT);
153
154 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
155 return(error);
156
157 lport = sin6->sin6_port;
158 if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
159 /*
160 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
161 * allow compepte duplication of binding if
162 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
163 * and a multicast address is bound on both
164 * new and duplicated sockets.
165 */
166 if (so->so_options & SO_REUSEADDR)
167 reuseport = SO_REUSEADDR|SO_REUSEPORT;
168 } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
169 struct ifaddr *ia = NULL;
170
171 sin6->sin6_port = 0; /* yech... */
172 if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0)
173 return (EADDRNOTAVAIL);
174
175 /*
176 * XXX: bind to an anycast address might accidentally
177 * cause sending a packet with anycast source address.
178 * We should allow to bind to a deprecated address, since
179 * the application dares to use it.
180 */
181 if (ia &&
182 ((struct in6_ifaddr *)ia)->ia6_flags &
183 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|IN6_IFF_DETACHED)) {
184 return (EADDRNOTAVAIL);
185 }
186 }
187 if (lport) {
188 struct inpcb *t;
189
190 /* GROSS */
191 if (ntohs(lport) < IPV6PORT_RESERVED &&
192 suser_cred(cred, SUSER_ALLOWJAIL))
193 return (EACCES);
194 if (so->so_cred->cr_uid != 0 &&
195 !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) {
196 t = in6_pcblookup_local(pcbinfo,
197 &sin6->sin6_addr, lport,
198 INPLOOKUP_WILDCARD);
199 if (t &&
200 ((t->inp_vflag & INP_TIMEWAIT) == 0) &&
201 (so->so_type != SOCK_STREAM ||
202 IN6_IS_ADDR_UNSPECIFIED(&t->in6p_faddr)) &&
203 (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
204 !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) ||
205 (t->inp_socket->so_options & SO_REUSEPORT)
206 == 0) && (so->so_cred->cr_uid !=
207 t->inp_socket->so_cred->cr_uid))
208 return (EADDRINUSE);
209 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
210 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
211 struct sockaddr_in sin;
212
213 in6_sin6_2_sin(&sin, sin6);
214 t = in_pcblookup_local(pcbinfo,
215 sin.sin_addr, lport,
216 INPLOOKUP_WILDCARD);
217 if (t &&
218 ((t->inp_vflag &
219 INP_TIMEWAIT) == 0) &&
220 (so->so_type != SOCK_STREAM ||
221 ntohl(t->inp_faddr.s_addr) ==
222 INADDR_ANY) &&
223 (so->so_cred->cr_uid !=
224 t->inp_socket->so_cred->cr_uid))
225 return (EADDRINUSE);
226 }
227 }
228 t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr,
229 lport, wild);
230 if (t && (reuseport & ((t->inp_vflag & INP_TIMEWAIT) ?
231 intotw(t)->tw_so_options :
232 t->inp_socket->so_options)) == 0)
233 return (EADDRINUSE);
234 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
235 IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
236 struct sockaddr_in sin;
237
238 in6_sin6_2_sin(&sin, sin6);
239 t = in_pcblookup_local(pcbinfo, sin.sin_addr,
240 lport, wild);
241 if (t && t->inp_vflag & INP_TIMEWAIT) {
242 if ((reuseport &
243 intotw(t)->tw_so_options) == 0 &&
244 (ntohl(t->inp_laddr.s_addr) !=
245 INADDR_ANY || ((inp->inp_vflag &
246 INP_IPV6PROTO) ==
247 (t->inp_vflag & INP_IPV6PROTO))))
248 return (EADDRINUSE);
249 }
250 else if (t &&
251 (reuseport & t->inp_socket->so_options)
252 == 0 && (ntohl(t->inp_laddr.s_addr) !=
253 INADDR_ANY || INP_SOCKAF(so) ==
254 INP_SOCKAF(t->inp_socket)))
255 return (EADDRINUSE);
256 }
257 }
258 inp->in6p_laddr = sin6->sin6_addr;
259 }
260 if (lport == 0) {
261 int e;
262 if ((e = in6_pcbsetport(&inp->in6p_laddr, inp, cred)) != 0)
263 return (e);
264 }
265 else {
266 inp->inp_lport = lport;
267 if (in_pcbinshash(inp) != 0) {
268 inp->in6p_laddr = in6addr_any;
269 inp->inp_lport = 0;
270 return (EAGAIN);
271 }
272 }
273 return (0);
274 }
275
276 /*
277 * Transform old in6_pcbconnect() into an inner subroutine for new
278 * in6_pcbconnect(): Do some validity-checking on the remote
279 * address (in mbuf 'nam') and then determine local host address
280 * (i.e., which interface) to use to access that remote host.
281 *
282 * This preserves definition of in6_pcbconnect(), while supporting a
283 * slightly different version for T/TCP. (This is more than
284 * a bit of a kludge, but cleaning up the internal interfaces would
285 * have forced minor changes in every protocol).
286 */
287
288 int
289 in6_pcbladdr(inp, nam, plocal_addr6)
290 register struct inpcb *inp;
291 struct sockaddr *nam;
292 struct in6_addr **plocal_addr6;
293 {
294 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
295 int error = 0;
296 struct ifnet *ifp = NULL;
297 int scope_ambiguous = 0;
298
299 if (nam->sa_len != sizeof (*sin6))
300 return (EINVAL);
301 if (sin6->sin6_family != AF_INET6)
302 return (EAFNOSUPPORT);
303 if (sin6->sin6_port == 0)
304 return (EADDRNOTAVAIL);
305
306 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
307 scope_ambiguous = 1;
308 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
309 return(error);
310
311 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
312 INP_LOCK_ASSERT(inp);
313
314 if (in6_ifaddr) {
315 /*
316 * If the destination address is UNSPECIFIED addr,
317 * use the loopback addr, e.g ::1.
318 */
319 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
320 sin6->sin6_addr = in6addr_loopback;
321 }
322
323 /*
324 * XXX: in6_selectsrc might replace the bound local address
325 * with the address specified by setsockopt(IPV6_PKTINFO).
326 * Is it the intended behavior?
327 */
328 *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts,
329 inp->in6p_moptions, NULL,
330 &inp->in6p_laddr, &ifp, &error);
331 if (ifp && scope_ambiguous &&
332 (error = in6_setscope(&sin6->sin6_addr, ifp, NULL)) != 0) {
333 return(error);
334 }
335
336 if (*plocal_addr6 == 0) {
337 if (error == 0)
338 error = EADDRNOTAVAIL;
339 return (error);
340 }
341 /*
342 * Don't do pcblookup call here; return interface in
343 * plocal_addr6
344 * and exit to caller, that will do the lookup.
345 */
346
347 return (0);
348 }
349
350 /*
351 * Outer subroutine:
352 * Connect from a socket to a specified address.
353 * Both address and port must be specified in argument sin.
354 * If don't have a local address for this socket yet,
355 * then pick one.
356 */
357 int
358 in6_pcbconnect(inp, nam, cred)
359 register struct inpcb *inp;
360 struct sockaddr *nam;
361 struct ucred *cred;
362 {
363 struct in6_addr *addr6;
364 register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
365 int error;
366
367 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
368 INP_LOCK_ASSERT(inp);
369
370 /*
371 * Call inner routine, to assign local interface address.
372 * in6_pcbladdr() may automatically fill in sin6_scope_id.
373 */
374 if ((error = in6_pcbladdr(inp, nam, &addr6)) != 0)
375 return (error);
376
377 if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr,
378 sin6->sin6_port,
379 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
380 ? addr6 : &inp->in6p_laddr,
381 inp->inp_lport, 0, NULL) != NULL) {
382 return (EADDRINUSE);
383 }
384 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
385 if (inp->inp_lport == 0) {
386 error = in6_pcbbind(inp, (struct sockaddr *)0, cred);
387 if (error)
388 return (error);
389 }
390 inp->in6p_laddr = *addr6;
391 }
392 inp->in6p_faddr = sin6->sin6_addr;
393 inp->inp_fport = sin6->sin6_port;
394 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
395 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
396 if (inp->in6p_flags & IN6P_AUTOFLOWLABEL)
397 inp->in6p_flowinfo |=
398 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
399
400 in_pcbrehash(inp);
401 #ifdef IPSEC
402 if (inp->inp_socket->so_type == SOCK_STREAM)
403 ipsec_pcbconn(inp->inp_sp);
404 #endif
405 return (0);
406 }
407
408 void
409 in6_pcbdisconnect(inp)
410 struct inpcb *inp;
411 {
412
413 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
414 INP_LOCK_ASSERT(inp);
415
416 bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr));
417 inp->inp_fport = 0;
418 /* clear flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
419 inp->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK;
420 in_pcbrehash(inp);
421 #ifdef IPSEC
422 ipsec_pcbdisconn(inp->inp_sp);
423 #endif
424 if (inp->inp_socket->so_state & SS_NOFDREF)
425 in6_pcbdetach(inp);
426 }
427
428 void
429 in6_pcbdetach(inp)
430 struct inpcb *inp;
431 {
432 struct socket *so = inp->inp_socket;
433 struct inpcbinfo *ipi = inp->inp_pcbinfo;
434
435 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
436 INP_LOCK_ASSERT(inp);
437
438 #if defined(IPSEC) || defined(FAST_IPSEC)
439 if (inp->in6p_sp != NULL)
440 ipsec6_delete_pcbpolicy(inp);
441 #endif /* IPSEC */
442 inp->inp_gencnt = ++ipi->ipi_gencnt;
443 in_pcbremlists(inp);
444
445 if (so) {
446 ACCEPT_LOCK();
447 SOCK_LOCK(so);
448 so->so_pcb = NULL;
449 sotryfree(so);
450 }
451
452 ip6_freepcbopts(inp->in6p_outputopts);
453 ip6_freemoptions(inp->in6p_moptions);
454 /* Check and free IPv4 related resources in case of mapped addr */
455 if (inp->inp_options)
456 (void)m_free(inp->inp_options);
457 ip_freemoptions(inp->inp_moptions);
458 inp->inp_vflag = 0;
459 INP_LOCK_DESTROY(inp);
460 uma_zfree(ipi->ipi_zone, inp);
461 }
462
463 struct sockaddr *
464 in6_sockaddr(port, addr_p)
465 in_port_t port;
466 struct in6_addr *addr_p;
467 {
468 struct sockaddr_in6 *sin6;
469
470 MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK);
471 bzero(sin6, sizeof *sin6);
472 sin6->sin6_family = AF_INET6;
473 sin6->sin6_len = sizeof(*sin6);
474 sin6->sin6_port = port;
475 sin6->sin6_addr = *addr_p;
476 (void)sa6_recoverscope(sin6); /* XXX: should catch errors */
477
478 return (struct sockaddr *)sin6;
479 }
480
481 struct sockaddr *
482 in6_v4mapsin6_sockaddr(port, addr_p)
483 in_port_t port;
484 struct in_addr *addr_p;
485 {
486 struct sockaddr_in sin;
487 struct sockaddr_in6 *sin6_p;
488
489 bzero(&sin, sizeof sin);
490 sin.sin_family = AF_INET;
491 sin.sin_len = sizeof(sin);
492 sin.sin_port = port;
493 sin.sin_addr = *addr_p;
494
495 MALLOC(sin6_p, struct sockaddr_in6 *, sizeof *sin6_p, M_SONAME,
496 M_WAITOK);
497 in6_sin_2_v4mapsin6(&sin, sin6_p);
498
499 return (struct sockaddr *)sin6_p;
500 }
501
502 /*
503 * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was
504 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
505 * in struct pr_usrreqs, so that protocols can just reference then directly
506 * without the need for a wrapper function. The socket must have a valid
507 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
508 * except through a kernel programming error, so it is acceptable to panic
509 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
510 * because there actually /is/ a programming error somewhere... XXX)
511 */
512 int
513 in6_setsockaddr(so, nam)
514 struct socket *so;
515 struct sockaddr **nam;
516 {
517 int s;
518 register struct inpcb *inp;
519 struct in6_addr addr;
520 in_port_t port;
521
522 s = splnet();
523 inp = sotoinpcb(so);
524 if (!inp) {
525 splx(s);
526 return EINVAL;
527 }
528 port = inp->inp_lport;
529 addr = inp->in6p_laddr;
530 splx(s);
531
532 *nam = in6_sockaddr(port, &addr);
533 return 0;
534 }
535
536 int
537 in6_setpeeraddr(so, nam)
538 struct socket *so;
539 struct sockaddr **nam;
540 {
541 int s;
542 struct inpcb *inp;
543 struct in6_addr addr;
544 in_port_t port;
545
546 s = splnet();
547 inp = sotoinpcb(so);
548 if (!inp) {
549 splx(s);
550 return EINVAL;
551 }
552 port = inp->inp_fport;
553 addr = inp->in6p_faddr;
554 splx(s);
555
556 *nam = in6_sockaddr(port, &addr);
557 return 0;
558 }
559
560 int
561 in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam)
562 {
563 struct inpcb *inp = sotoinpcb(so);
564 int error;
565
566 if (inp == NULL)
567 return EINVAL;
568 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
569 error = in_setsockaddr(so, nam, &tcbinfo);
570 if (error == 0)
571 in6_sin_2_v4mapsin6_in_sock(nam);
572 } else {
573 /* scope issues will be handled in in6_setsockaddr(). */
574 error = in6_setsockaddr(so, nam);
575 }
576
577 return error;
578 }
579
580 int
581 in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam)
582 {
583 struct inpcb *inp = sotoinpcb(so);
584 int error;
585
586 if (inp == NULL)
587 return EINVAL;
588 if ((inp->inp_vflag & (INP_IPV4 | INP_IPV6)) == INP_IPV4) {
589 error = in_setpeeraddr(so, nam, &tcbinfo);
590 if (error == 0)
591 in6_sin_2_v4mapsin6_in_sock(nam);
592 } else
593 /* scope issues will be handled in in6_setpeeraddr(). */
594 error = in6_setpeeraddr(so, nam);
595
596 return error;
597 }
598
599 /*
600 * Pass some notification to all connections of a protocol
601 * associated with address dst. The local address and/or port numbers
602 * may be specified to limit the search. The "usual action" will be
603 * taken, depending on the ctlinput cmd. The caller must filter any
604 * cmds that are uninteresting (e.g., no error in the map).
605 * Call the protocol specific routine (if any) to report
606 * any errors for each matching socket.
607 *
608 * Must be called at splnet.
609 */
610 void
611 in6_pcbnotify(pcbinfo, dst, fport_arg, src, lport_arg, cmd, cmdarg, notify)
612 struct inpcbinfo *pcbinfo;
613 struct sockaddr *dst;
614 const struct sockaddr *src;
615 u_int fport_arg, lport_arg;
616 int cmd;
617 void *cmdarg;
618 struct inpcb *(*notify) __P((struct inpcb *, int));
619 {
620 struct inpcbhead *head;
621 struct inpcb *inp, *ninp;
622 struct sockaddr_in6 sa6_src, *sa6_dst;
623 u_short fport = fport_arg, lport = lport_arg;
624 u_int32_t flowinfo;
625 int errno, s;
626
627 if ((unsigned)cmd >= PRC_NCMDS || dst->sa_family != AF_INET6)
628 return;
629
630 sa6_dst = (struct sockaddr_in6 *)dst;
631 if (IN6_IS_ADDR_UNSPECIFIED(&sa6_dst->sin6_addr))
632 return;
633
634 /*
635 * note that src can be NULL when we get notify by local fragmentation.
636 */
637 sa6_src = (src == NULL) ? sa6_any : *(const struct sockaddr_in6 *)src;
638 flowinfo = sa6_src.sin6_flowinfo;
639
640 /*
641 * Redirects go to all references to the destination,
642 * and use in6_rtchange to invalidate the route cache.
643 * Dead host indications: also use in6_rtchange to invalidate
644 * the cache, and deliver the error to all the sockets.
645 * Otherwise, if we have knowledge of the local port and address,
646 * deliver only to that socket.
647 */
648 if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) {
649 fport = 0;
650 lport = 0;
651 bzero((caddr_t)&sa6_src.sin6_addr, sizeof(sa6_src.sin6_addr));
652
653 if (cmd != PRC_HOSTDEAD)
654 notify = in6_rtchange;
655 }
656 errno = inet6ctlerrmap[cmd];
657 s = splnet();
658 head = pcbinfo->listhead;
659 INP_INFO_WLOCK(pcbinfo);
660 for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) {
661 INP_LOCK(inp);
662 ninp = LIST_NEXT(inp, inp_list);
663
664 if ((inp->inp_vflag & INP_IPV6) == 0) {
665 INP_UNLOCK(inp);
666 continue;
667 }
668
669 /*
670 * If the error designates a new path MTU for a destination
671 * and the application (associated with this socket) wanted to
672 * know the value, notify. Note that we notify for all
673 * disconnected sockets if the corresponding application
674 * wanted. This is because some UDP applications keep sending
675 * sockets disconnected.
676 * XXX: should we avoid to notify the value to TCP sockets?
677 */
678 if (cmd == PRC_MSGSIZE && (inp->inp_flags & IN6P_MTU) != 0 &&
679 (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) ||
680 IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &sa6_dst->sin6_addr))) {
681 ip6_notify_pmtu(inp, (struct sockaddr_in6 *)dst,
682 (u_int32_t *)cmdarg);
683 }
684
685 /*
686 * Detect if we should notify the error. If no source and
687 * destination ports are specifed, but non-zero flowinfo and
688 * local address match, notify the error. This is the case
689 * when the error is delivered with an encrypted buffer
690 * by ESP. Otherwise, just compare addresses and ports
691 * as usual.
692 */
693 if (lport == 0 && fport == 0 && flowinfo &&
694 inp->inp_socket != NULL &&
695 flowinfo == (inp->in6p_flowinfo & IPV6_FLOWLABEL_MASK) &&
696 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &sa6_src.sin6_addr))
697 goto do_notify;
698 else if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
699 &sa6_dst->sin6_addr) ||
700 inp->inp_socket == 0 ||
701 (lport && inp->inp_lport != lport) ||
702 (!IN6_IS_ADDR_UNSPECIFIED(&sa6_src.sin6_addr) &&
703 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
704 &sa6_src.sin6_addr)) ||
705 (fport && inp->inp_fport != fport)) {
706 INP_UNLOCK(inp);
707 continue;
708 }
709
710 do_notify:
711 if (notify) {
712 if ((*notify)(inp, errno))
713 INP_UNLOCK(inp);
714 } else
715 INP_UNLOCK(inp);
716 }
717 INP_INFO_WUNLOCK(pcbinfo);
718 splx(s);
719 }
720
721 /*
722 * Lookup a PCB based on the local address and port.
723 */
724 struct inpcb *
725 in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
726 struct inpcbinfo *pcbinfo;
727 struct in6_addr *laddr;
728 u_int lport_arg;
729 int wild_okay;
730 {
731 register struct inpcb *inp;
732 int matchwild = 3, wildcard;
733 u_short lport = lport_arg;
734
735 if (!wild_okay) {
736 struct inpcbhead *head;
737 /*
738 * Look for an unconnected (wildcard foreign addr) PCB that
739 * matches the local address and port we're looking for.
740 */
741 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
742 pcbinfo->hashmask)];
743 LIST_FOREACH(inp, head, inp_hash) {
744 if ((inp->inp_vflag & INP_IPV6) == 0)
745 continue;
746 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
747 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
748 inp->inp_lport == lport) {
749 /*
750 * Found.
751 */
752 return (inp);
753 }
754 }
755 /*
756 * Not found.
757 */
758 return (NULL);
759 } else {
760 struct inpcbporthead *porthash;
761 struct inpcbport *phd;
762 struct inpcb *match = NULL;
763 /*
764 * Best fit PCB lookup.
765 *
766 * First see if this local port is in use by looking on the
767 * port hash list.
768 */
769 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
770 pcbinfo->porthashmask)];
771 LIST_FOREACH(phd, porthash, phd_hash) {
772 if (phd->phd_port == lport)
773 break;
774 }
775 if (phd != NULL) {
776 /*
777 * Port is in use by one or more PCBs. Look for best
778 * fit.
779 */
780 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
781 wildcard = 0;
782 if ((inp->inp_vflag & INP_IPV6) == 0)
783 continue;
784 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))
785 wildcard++;
786 if (!IN6_IS_ADDR_UNSPECIFIED(
787 &inp->in6p_laddr)) {
788 if (IN6_IS_ADDR_UNSPECIFIED(laddr))
789 wildcard++;
790 else if (!IN6_ARE_ADDR_EQUAL(
791 &inp->in6p_laddr, laddr))
792 continue;
793 } else {
794 if (!IN6_IS_ADDR_UNSPECIFIED(laddr))
795 wildcard++;
796 }
797 if (wildcard < matchwild) {
798 match = inp;
799 matchwild = wildcard;
800 if (matchwild == 0) {
801 break;
802 }
803 }
804 }
805 }
806 return (match);
807 }
808 }
809
810 void
811 in6_pcbpurgeif0(head, ifp)
812 struct in6pcb *head;
813 struct ifnet *ifp;
814 {
815 struct in6pcb *in6p;
816 struct ip6_moptions *im6o;
817 struct in6_multi_mship *imm, *nimm;
818
819 for (in6p = head; in6p != NULL; in6p = LIST_NEXT(in6p, inp_list)) {
820 im6o = in6p->in6p_moptions;
821 if ((in6p->inp_vflag & INP_IPV6) &&
822 im6o) {
823 /*
824 * Unselect the outgoing interface if it is being
825 * detached.
826 */
827 if (im6o->im6o_multicast_ifp == ifp)
828 im6o->im6o_multicast_ifp = NULL;
829
830 /*
831 * Drop multicast group membership if we joined
832 * through the interface being detached.
833 * XXX controversial - is it really legal for kernel
834 * to force this?
835 */
836 for (imm = im6o->im6o_memberships.lh_first;
837 imm != NULL; imm = nimm) {
838 nimm = imm->i6mm_chain.le_next;
839 if (imm->i6mm_maddr->in6m_ifp == ifp) {
840 LIST_REMOVE(imm, i6mm_chain);
841 in6_delmulti(imm->i6mm_maddr);
842 free(imm, M_IP6MADDR);
843 }
844 }
845 }
846 }
847 }
848
849 /*
850 * Check for alternatives when higher level complains
851 * about service problems. For now, invalidate cached
852 * routing information. If the route was created dynamically
853 * (by a redirect), time to try a default gateway again.
854 */
855 void
856 in6_losing(in6p)
857 struct inpcb *in6p;
858 {
859 /*
860 * We don't store route pointers in the routing table anymore
861 */
862 return;
863 }
864
865 /*
866 * After a routing change, flush old routing
867 * and allocate a (hopefully) better one.
868 */
869 struct inpcb *
870 in6_rtchange(inp, errno)
871 struct inpcb *inp;
872 int errno;
873 {
874 /*
875 * We don't store route pointers in the routing table anymore
876 */
877 return inp;
878 }
879
880 /*
881 * Lookup PCB in hash list.
882 */
883 struct inpcb *
884 in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp)
885 struct inpcbinfo *pcbinfo;
886 struct in6_addr *faddr, *laddr;
887 u_int fport_arg, lport_arg;
888 int wildcard;
889 struct ifnet *ifp;
890 {
891 struct inpcbhead *head;
892 register struct inpcb *inp;
893 u_short fport = fport_arg, lport = lport_arg;
894 int faith;
895
896 if (faithprefix_p != NULL)
897 faith = (*faithprefix_p)(laddr);
898 else
899 faith = 0;
900
901 /*
902 * First look for an exact match.
903 */
904 head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */,
905 lport, fport,
906 pcbinfo->hashmask)];
907 LIST_FOREACH(inp, head, inp_hash) {
908 if ((inp->inp_vflag & INP_IPV6) == 0)
909 continue;
910 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) &&
911 IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) &&
912 inp->inp_fport == fport &&
913 inp->inp_lport == lport) {
914 /*
915 * Found.
916 */
917 return (inp);
918 }
919 }
920 if (wildcard) {
921 struct inpcb *local_wild = NULL;
922
923 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0,
924 pcbinfo->hashmask)];
925 LIST_FOREACH(inp, head, inp_hash) {
926 if ((inp->inp_vflag & INP_IPV6) == 0)
927 continue;
928 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
929 inp->inp_lport == lport) {
930 if (faith && (inp->inp_flags & INP_FAITH) == 0)
931 continue;
932 if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
933 laddr))
934 return (inp);
935 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
936 local_wild = inp;
937 }
938 }
939 return (local_wild);
940 }
941
942 /*
943 * Not found.
944 */
945 return (NULL);
946 }
947
948 void
949 init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m)
950 {
951 struct ip6_hdr *ip;
952
953 ip = mtod(m, struct ip6_hdr *);
954 bzero(sin6, sizeof(*sin6));
955 sin6->sin6_len = sizeof(*sin6);
956 sin6->sin6_family = AF_INET6;
957 sin6->sin6_addr = ip->ip6_src;
958
959 (void)sa6_recoverscope(sin6); /* XXX: should catch errors... */
960
961 return;
962 }
Cache object: 295f2bd333f9b44e07c2bf61b0e5cc40
|