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