]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
8 | * 1. Redistributions of source code must retain the above copyright | |
9 | * notice, this list of conditions and the following disclaimer. | |
10 | * 2. Redistributions in binary form must reproduce the above copyright | |
11 | * notice, this list of conditions and the following disclaimer in the | |
12 | * documentation and/or other materials provided with the distribution. | |
13 | * 3. Neither the name of the project nor the names of its contributors | |
14 | * may be used to endorse or promote products derived from this software | |
15 | * without specific prior written permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
30 | /* | |
31 | * Copyright (c) 1982, 1986, 1991, 1993 | |
32 | * The Regents of the University of California. All rights reserved. | |
33 | * | |
34 | * Redistribution and use in source and binary forms, with or without | |
35 | * modification, are permitted provided that the following conditions | |
36 | * are met: | |
37 | * 1. Redistributions of source code must retain the above copyright | |
38 | * notice, this list of conditions and the following disclaimer. | |
39 | * 2. Redistributions in binary form must reproduce the above copyright | |
40 | * notice, this list of conditions and the following disclaimer in the | |
41 | * documentation and/or other materials provided with the distribution. | |
42 | * 3. All advertising materials mentioning features or use of this software | |
43 | * must display the following acknowledgement: | |
44 | * This product includes software developed by the University of | |
45 | * California, Berkeley and its contributors. | |
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 | #ifdef __NetBSD__ /*XXX*/ | |
66 | #include "opt_ipsec.h" | |
67 | #endif | |
68 | ||
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/malloc.h> | |
72 | #include <sys/mbuf.h> | |
73 | #include <sys/protosw.h> | |
74 | #include <sys/socket.h> | |
75 | #include <sys/socketvar.h> | |
76 | #include <sys/sockio.h> | |
77 | #include <sys/errno.h> | |
78 | #include <sys/time.h> | |
79 | #include <sys/proc.h> | |
80 | ||
81 | #include <net/if.h> | |
82 | #include <net/if_types.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/ip6.h> | |
89 | #include <netinet6/ip6_var.h> | |
90 | #include <netinet6/nd6.h> | |
91 | #include <netinet/in_pcb.h> | |
92 | #include <netinet6/in6_pcb.h> | |
93 | #include <net/if_types.h> | |
94 | ||
95 | #include "faith.h" | |
96 | ||
97 | #if IPSEC | |
98 | #include <netinet6/ipsec.h> | |
99 | #include <netkey/key.h> | |
100 | #include <netkey/key_debug.h> | |
101 | #endif /* IPSEC */ | |
102 | ||
103 | struct in6_addr zeroin6_addr; | |
104 | ||
105 | int | |
106 | in6_pcbbind(inp, nam, p) | |
107 | register struct inpcb *inp; | |
108 | struct sockaddr *nam; | |
109 | struct proc *p; | |
110 | { | |
111 | struct socket *so = inp->inp_socket; | |
112 | unsigned short *lastport; | |
113 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)NULL; | |
114 | struct inpcbinfo *pcbinfo = inp->inp_pcbinfo; | |
115 | u_short lport = 0; | |
116 | int wild = 0, reuseport = (so->so_options & SO_REUSEPORT); | |
117 | int error; | |
118 | ||
119 | if (!in6_ifaddr) /* XXX broken! */ | |
120 | return (EADDRNOTAVAIL); | |
121 | if (inp->inp_lport || !IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) | |
122 | return(EINVAL); | |
123 | if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0) | |
124 | wild = INPLOOKUP_WILDCARD; | |
125 | if (nam) { | |
126 | if (nam->sa_len != sizeof(*sin6)) | |
127 | return(EINVAL); | |
128 | /* | |
129 | * We should check the family, but old programs | |
130 | * incorrectly fail to intialize it. | |
131 | */ | |
132 | if (nam->sa_family != AF_INET6) | |
133 | return(EAFNOSUPPORT); | |
134 | sin6 = (struct sockaddr_in6 *)nam; | |
135 | ||
136 | /* | |
137 | * If the scope of the destination is link-local, embed the | |
138 | * interface index in the address. | |
139 | */ | |
140 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { | |
141 | /* XXX boundary check is assumed to be already done. */ | |
142 | /* XXX sin6_scope_id is weaker than advanced-api. */ | |
143 | struct in6_pktinfo *pi; | |
144 | if (inp->in6p_outputopts && | |
145 | (pi = inp->in6p_outputopts->ip6po_pktinfo) && | |
146 | pi->ipi6_ifindex) { | |
147 | sin6->sin6_addr.s6_addr16[1] | |
148 | = htons(pi->ipi6_ifindex); | |
149 | } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) | |
150 | && inp->in6p_moptions | |
151 | && inp->in6p_moptions->im6o_multicast_ifp) { | |
152 | sin6->sin6_addr.s6_addr16[1] = | |
153 | htons(inp->in6p_moptions->im6o_multicast_ifp->if_index); | |
154 | } else if (sin6->sin6_scope_id) { | |
155 | /* boundary check */ | |
156 | if (sin6->sin6_scope_id < 0 | |
157 | || if_index < sin6->sin6_scope_id) { | |
158 | return ENXIO; /* XXX EINVAL? */ | |
159 | } | |
160 | sin6->sin6_addr.s6_addr16[1] | |
161 | = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/ | |
162 | /* this must be cleared for ifa_ifwithaddr() */ | |
163 | sin6->sin6_scope_id = 0; | |
164 | } | |
165 | } | |
166 | ||
167 | lport = sin6->sin6_port; | |
168 | if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { | |
169 | /* | |
170 | * Treat SO_REUSEADDR as SO_REUSEPORT for multicast; | |
171 | * allow compepte duplication of binding if | |
172 | * SO_REUSEPORT is set, or if SO_REUSEADDR is set | |
173 | * and a multicast address is bound on both | |
174 | * new and duplicated sockets. | |
175 | */ | |
176 | if (so->so_options & SO_REUSEADDR) | |
177 | reuseport = SO_REUSEADDR|SO_REUSEPORT; | |
178 | } else if (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) { | |
179 | struct ifaddr *ia = NULL; | |
180 | ||
181 | sin6->sin6_port = 0; /* yech... */ | |
182 | if ((ia = ifa_ifwithaddr((struct sockaddr *)sin6)) == 0) | |
183 | return(EADDRNOTAVAIL); | |
184 | ||
185 | /* | |
186 | * XXX: bind to an anycast address might accidentally | |
187 | * cause sending a packet with anycast source address. | |
188 | */ | |
189 | if (ia && | |
190 | ((struct in6_ifaddr *)ia)->ia6_flags & | |
191 | (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY| | |
192 | IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) { | |
193 | return(EADDRNOTAVAIL); | |
194 | } | |
195 | } | |
196 | if (lport) { | |
197 | struct inpcb *t; | |
198 | ||
199 | /* GROSS */ | |
200 | if (ntohs(lport) < IPV6PORT_RESERVED && p && | |
201 | #if 0 | |
202 | suser(p->p_ucred, &p->p_acflag)) | |
203 | #else | |
204 | ((so->so_state & SS_PRIV) == 0)) | |
205 | #endif | |
206 | return(EACCES); | |
207 | ||
208 | if (so->so_uid && | |
209 | !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) { | |
210 | t = in6_pcblookup_local(inp->inp_pcbinfo, | |
211 | &sin6->sin6_addr, | |
212 | lport, | |
213 | INPLOOKUP_WILDCARD); | |
214 | if (t && | |
215 | (!IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) || | |
216 | !IN6_IS_ADDR_UNSPECIFIED(&t->in6p_laddr) || | |
217 | (t->inp_socket->so_options & | |
218 | SO_REUSEPORT) == 0) && | |
219 | so->so_uid != t->inp_socket->so_uid) | |
220 | return (EADDRINUSE); | |
221 | } | |
222 | t = in6_pcblookup_local(pcbinfo, &sin6->sin6_addr, | |
223 | lport, wild); | |
224 | if (t && (reuseport & t->inp_socket->so_options) == 0) | |
225 | return(EADDRINUSE); | |
226 | } | |
227 | inp->in6p_laddr = sin6->sin6_addr; | |
228 | } | |
229 | if (lport == 0) { | |
230 | ushort first, last; | |
231 | int count; | |
232 | ||
233 | inp->inp_flags |= INP_ANONPORT; | |
234 | ||
235 | if (inp->inp_flags & INP_HIGHPORT) { | |
236 | first = ipport_hifirstauto; /* sysctl */ | |
237 | last = ipport_hilastauto; | |
238 | lastport = &pcbinfo->lasthi; | |
239 | } else if (inp->inp_flags & INP_LOWPORT) { | |
240 | #if 0 | |
241 | if (p && (error = suser(p->p_ucred, &p->p_acflag))) | |
242 | return error; | |
243 | #else | |
244 | if ((so->so_state & SS_PRIV) == 0) | |
245 | return (EPERM); | |
246 | #endif | |
247 | first = ipport_lowfirstauto; /* 1023 */ | |
248 | last = ipport_lowlastauto; /* 600 */ | |
249 | lastport = &pcbinfo->lastlow; | |
250 | } else { | |
251 | first = ipport_firstauto; /* sysctl */ | |
252 | last = ipport_lastauto; | |
253 | lastport = &pcbinfo->lastport; | |
254 | } | |
255 | /* | |
256 | * Simple check to ensure all ports are not used up causing | |
257 | * a deadlock here. | |
258 | * | |
259 | * We split the two cases (up and down) so that the direction | |
260 | * is not being tested on each round of the loop. | |
261 | */ | |
262 | if (first > last) { | |
263 | /* | |
264 | * counting down | |
265 | */ | |
266 | count = first - last; | |
267 | ||
268 | do { | |
269 | if (count-- < 0) { /* completely used? */ | |
270 | /* | |
271 | * Undo any address bind that may have | |
272 | * occurred above. | |
273 | */ | |
274 | inp->in6p_laddr = in6addr_any; | |
275 | return (EAGAIN); | |
276 | } | |
277 | --*lastport; | |
278 | if (*lastport > first || *lastport < last) | |
279 | *lastport = first; | |
280 | lport = htons(*lastport); | |
281 | } while (in6_pcblookup_local(pcbinfo, | |
282 | &inp->in6p_laddr, lport, wild)); | |
283 | } else { | |
284 | /* | |
285 | * counting up | |
286 | */ | |
287 | count = last - first; | |
288 | ||
289 | do { | |
290 | if (count-- < 0) { /* completely used? */ | |
291 | /* | |
292 | * Undo any address bind that may have | |
293 | * occurred above. | |
294 | */ | |
295 | inp->in6p_laddr = in6addr_any; | |
296 | return (EAGAIN); | |
297 | } | |
298 | ++*lastport; | |
299 | if (*lastport < first || *lastport > last) | |
300 | *lastport = first; | |
301 | lport = htons(*lastport); | |
302 | } while (in6_pcblookup_local(pcbinfo, | |
303 | &inp->in6p_laddr, lport, wild)); | |
304 | } | |
305 | } | |
306 | inp->inp_lport = lport; | |
307 | if (in_pcbinshash(inp) != 0) { | |
308 | inp->in6p_laddr = in6addr_any; | |
309 | inp->inp_lport = 0; | |
310 | return (EAGAIN); | |
311 | } | |
312 | inp->in6p_flowinfo = sin6 ? sin6->sin6_flowinfo : 0; /*XXX*/ | |
313 | return(0); | |
314 | } | |
315 | ||
316 | /* | |
317 | * Transform old in6_pcbconnect() into an inner subroutine for new | |
318 | * in6_pcbconnect(): Do some validity-checking on the remote | |
319 | * address (in mbuf 'nam') and then determine local host address | |
320 | * (i.e., which interface) to use to access that remote host. | |
321 | * | |
322 | * This preserves definition of in6_pcbconnect(), while supporting a | |
323 | * slightly different version for T/TCP. (This is more than | |
324 | * a bit of a kludge, but cleaning up the internal interfaces would | |
325 | * have forced minor changes in every protocol). | |
326 | */ | |
327 | ||
328 | int | |
329 | in6_pcbladdr(inp, nam, plocal_addr6) | |
330 | register struct inpcb *inp; | |
331 | struct sockaddr *nam; | |
332 | struct in6_addr **plocal_addr6; | |
333 | { | |
334 | register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; | |
335 | struct in6_pktinfo *pi; | |
336 | struct ifnet *ifp = NULL; | |
337 | int error = 0; | |
338 | ||
339 | if (nam->sa_len != sizeof (*sin6)) | |
340 | return (EINVAL); | |
341 | if (sin6->sin6_family != AF_INET6) | |
342 | return (EAFNOSUPPORT); | |
343 | if (sin6->sin6_port == 0) | |
344 | return (EADDRNOTAVAIL); | |
345 | ||
346 | /* | |
347 | * If the scope of the destination is link-local, embed the interface | |
348 | * index in the address. | |
349 | */ | |
350 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) { | |
351 | /* XXX boundary check is assumed to be already done. */ | |
352 | /* XXX sin6_scope_id is weaker than advanced-api. */ | |
353 | if (inp->in6p_outputopts && | |
354 | (pi = inp->in6p_outputopts->ip6po_pktinfo) && | |
355 | pi->ipi6_ifindex) { | |
356 | sin6->sin6_addr.s6_addr16[1] = htons(pi->ipi6_ifindex); | |
357 | ifp = ifindex2ifnet[pi->ipi6_ifindex]; | |
358 | } | |
359 | else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr) && | |
360 | inp->in6p_moptions && | |
361 | inp->in6p_moptions->im6o_multicast_ifp) { | |
362 | sin6->sin6_addr.s6_addr16[1] = | |
363 | htons(inp->in6p_moptions->im6o_multicast_ifp->if_index); | |
364 | ifp = ifindex2ifnet[inp->in6p_moptions->im6o_multicast_ifp->if_index]; | |
365 | } else if (sin6->sin6_scope_id) { | |
366 | /* boundary check */ | |
367 | if (sin6->sin6_scope_id < 0 | |
368 | || if_index < sin6->sin6_scope_id) { | |
369 | return ENXIO; /* XXX EINVAL? */ | |
370 | } | |
371 | sin6->sin6_addr.s6_addr16[1] | |
372 | = htons(sin6->sin6_scope_id & 0xffff);/*XXX*/ | |
373 | ifp = ifindex2ifnet[sin6->sin6_scope_id]; | |
374 | } | |
375 | } | |
376 | ||
377 | if (in6_ifaddr) { | |
378 | /* | |
379 | * If the destination address is UNSPECIFIED addr, | |
380 | * use the loopback addr, e.g ::1. | |
381 | */ | |
382 | #define satosin6(sa) ((struct sockaddr_in6 *)(sa)) | |
383 | #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa)) | |
384 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) | |
385 | sin6->sin6_addr = in6addr_loopback; | |
386 | } | |
387 | { | |
388 | /* | |
389 | * XXX: in6_selectsrc might replace the bound local address | |
390 | * with the address specified by setsockopt(IPV6_PKTINFO). | |
391 | * Is it the intended behavior? | |
392 | */ | |
393 | *plocal_addr6 = in6_selectsrc(sin6, inp->in6p_outputopts, | |
394 | inp->in6p_moptions, | |
395 | &inp->in6p_route, | |
396 | &inp->in6p_laddr, &error); | |
397 | if (*plocal_addr6 == 0) { | |
398 | if (error == 0) | |
399 | error = EADDRNOTAVAIL; | |
400 | return(error); | |
401 | } | |
402 | /* | |
403 | * Don't do pcblookup call here; return interface in | |
404 | * plocal_addr6 | |
405 | * and exit to caller, that will do the lookup. | |
406 | */ | |
407 | } | |
408 | ||
409 | if (inp->in6p_route.ro_rt) | |
410 | ifp = inp->in6p_route.ro_rt->rt_ifp; | |
411 | ||
412 | return(0); | |
413 | } | |
414 | ||
415 | /* | |
416 | * Outer subroutine: | |
417 | * Connect from a socket to a specified address. | |
418 | * Both address and port must be specified in argument sin. | |
419 | * If don't have a local address for this socket yet, | |
420 | * then pick one. | |
421 | */ | |
422 | int | |
423 | in6_pcbconnect(inp, nam, p) | |
424 | register struct inpcb *inp; | |
425 | struct sockaddr *nam; | |
426 | struct proc *p; | |
427 | { | |
428 | struct in6_addr *addr6; | |
429 | register struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam; | |
430 | int error; | |
431 | ||
432 | /* | |
433 | * Call inner routine, to assign local interface address. | |
434 | */ | |
435 | if (error = in6_pcbladdr(inp, nam, &addr6)) | |
436 | return(error); | |
437 | ||
438 | if (in6_pcblookup_hash(inp->inp_pcbinfo, &sin6->sin6_addr, | |
439 | sin6->sin6_port, | |
440 | IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) | |
441 | ? addr6 : &inp->in6p_laddr, | |
442 | inp->inp_lport, 0, NULL) != NULL) { | |
443 | return (EADDRINUSE); | |
444 | } | |
445 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) { | |
446 | if (inp->inp_lport == 0) { | |
447 | error = in6_pcbbind(inp, (struct sockaddr *)0, p); | |
448 | if (error) | |
449 | return (error); | |
450 | } | |
451 | inp->in6p_laddr = *addr6; | |
452 | } | |
453 | inp->in6p_faddr = sin6->sin6_addr; | |
454 | inp->inp_fport = sin6->sin6_port; | |
455 | /* | |
456 | * xxx kazu flowlabel is necessary for connect? | |
457 | * but if this line is missing, the garbage value remains. | |
458 | */ | |
459 | inp->in6p_flowinfo = sin6->sin6_flowinfo; | |
460 | ||
461 | in_pcbrehash(inp); | |
462 | return (0); | |
463 | } | |
464 | ||
465 | #if 0 | |
466 | /* | |
467 | * Return an IPv6 address, which is the most appropriate for given | |
468 | * destination and user specified options. | |
469 | * If necessary, this function lookups the routing table and return | |
470 | * an entry to the caller for later use. | |
471 | */ | |
472 | struct in6_addr * | |
473 | in6_selectsrc(dstsock, opts, mopts, ro, laddr, errorp) | |
474 | struct sockaddr_in6 *dstsock; | |
475 | struct ip6_pktopts *opts; | |
476 | struct ip6_moptions *mopts; | |
477 | struct route_in6 *ro; | |
478 | struct in6_addr *laddr; | |
479 | int *errorp; | |
480 | { | |
481 | struct in6_addr *dst; | |
482 | struct in6_ifaddr *ia6 = 0; | |
483 | struct in6_pktinfo *pi = NULL; | |
484 | ||
485 | dst = &dstsock->sin6_addr; | |
486 | *errorp = 0; | |
487 | ||
488 | /* | |
489 | * If the source address is explicitly specified by the caller, | |
490 | * use it. | |
491 | */ | |
492 | if (opts && (pi = opts->ip6po_pktinfo) && | |
493 | !IN6_IS_ADDR_UNSPECIFIED(&pi->ipi6_addr)) | |
494 | return(&pi->ipi6_addr); | |
495 | ||
496 | /* | |
497 | * If the source address is not specified but the socket(if any) | |
498 | * is already bound, use the bound address. | |
499 | */ | |
500 | if (laddr && !IN6_IS_ADDR_UNSPECIFIED(laddr)) | |
501 | return(laddr); | |
502 | ||
503 | /* | |
504 | * If the caller doesn't specify the source address but | |
505 | * the outgoing interface, use an address associated with | |
506 | * the interface. | |
507 | */ | |
508 | if (pi && pi->ipi6_ifindex) { | |
509 | /* XXX boundary check is assumed to be already done. */ | |
510 | ia6 = in6_ifawithscope(ifindex2ifnet[pi->ipi6_ifindex], | |
511 | dst); | |
512 | if (ia6 == 0) { | |
513 | *errorp = EADDRNOTAVAIL; | |
514 | return(0); | |
515 | } | |
516 | return(&satosin6(&ia6->ia_addr)->sin6_addr); | |
517 | } | |
518 | ||
519 | /* | |
520 | * If the destination address is a link-local unicast address or | |
521 | * a multicast address, and if the outgoing interface is specified | |
522 | * by the sin6_scope_id filed, use an address associated with the | |
523 | * interface. | |
524 | * XXX: We're now trying to define more specific semantics of | |
525 | * sin6_scope_id field, so this part will be rewritten in | |
526 | * the near future. | |
527 | */ | |
528 | if ((IN6_IS_ADDR_LINKLOCAL(dst) || IN6_IS_ADDR_MULTICAST(dst)) && | |
529 | dstsock->sin6_scope_id) { | |
530 | /* | |
531 | * I'm not sure if boundary check for scope_id is done | |
532 | * somewhere... | |
533 | */ | |
534 | if (dstsock->sin6_scope_id < 0 || | |
535 | if_index < dstsock->sin6_scope_id) { | |
536 | *errorp = ENXIO; /* XXX: better error? */ | |
537 | return(0); | |
538 | } | |
539 | ia6 = in6_ifawithscope(ifindex2ifnet[dstsock->sin6_scope_id], | |
540 | dst); | |
541 | if (ia6 == 0) { | |
542 | *errorp = EADDRNOTAVAIL; | |
543 | return(0); | |
544 | } | |
545 | return(&satosin6(&ia6->ia_addr)->sin6_addr); | |
546 | } | |
547 | ||
548 | /* | |
549 | * If the destination address is a multicast address and | |
550 | * the outgoing interface for the address is specified | |
551 | * by the caller, use an address associated with the interface. | |
552 | * There is a sanity check here; if the destination has node-local | |
553 | * scope, the outgoing interfacde should be a loopback address. | |
554 | * Even if the outgoing interface is not specified, we also | |
555 | * choose a loopback interface as the outgoing interface. | |
556 | */ | |
557 | if (IN6_IS_ADDR_MULTICAST(dst)) { | |
558 | struct ifnet *ifp = mopts ? mopts->im6o_multicast_ifp : NULL; | |
559 | #ifdef __bsdi__ | |
560 | extern struct ifnet loif; | |
561 | #endif | |
562 | ||
563 | if (ifp == NULL && IN6_IS_ADDR_MC_NODELOCAL(dst)) { | |
564 | #ifdef __bsdi__ | |
565 | ifp = &loif; | |
566 | #else | |
567 | ifp = &loif[0]; | |
568 | #endif | |
569 | } | |
570 | ||
571 | if (ifp) { | |
572 | ia6 = in6_ifawithscope(ifp, dst); | |
573 | if (ia6 == 0) { | |
574 | *errorp = EADDRNOTAVAIL; | |
575 | return(0); | |
576 | } | |
577 | return(&ia6->ia_addr.sin6_addr); | |
578 | } | |
579 | } | |
580 | ||
581 | /* | |
582 | * If the next hop address for the packet is specified | |
583 | * by caller, use an address associated with the route | |
584 | * to the next hop. | |
585 | */ | |
586 | { | |
587 | struct sockaddr_in6 *sin6_next; | |
588 | struct rtentry *rt; | |
589 | ||
590 | if (opts && opts->ip6po_nexthop) { | |
591 | sin6_next = satosin6(opts->ip6po_nexthop); | |
592 | rt = nd6_lookup(&sin6_next->sin6_addr, 1, NULL); | |
593 | if (rt) { | |
594 | ia6 = in6_ifawithscope(rt->rt_ifp, dst); | |
595 | if (ia6 == 0) | |
596 | ia6 = ifatoia6(rt->rt_ifa); | |
597 | } | |
598 | if (ia6 == 0) { | |
599 | *errorp = EADDRNOTAVAIL; | |
600 | return(0); | |
601 | } | |
602 | return(&satosin6(&ia6->ia_addr)->sin6_addr); | |
603 | } | |
604 | } | |
605 | ||
606 | /* | |
607 | * If route is known or can be allocated now, | |
608 | * our src addr is taken from the i/f, else punt. | |
609 | */ | |
610 | if (ro) { | |
611 | if (ro->ro_rt && | |
612 | !IN6_ARE_ADDR_EQUAL(&satosin6(&ro->ro_dst)->sin6_addr, dst)) { | |
613 | RTFREE(ro->ro_rt); | |
614 | ro->ro_rt = (struct rtentry *)0; | |
615 | } | |
616 | if (ro->ro_rt == (struct rtentry *)0 || | |
617 | ro->ro_rt->rt_ifp == (struct ifnet *)0) { | |
618 | /* No route yet, so try to acquire one */ | |
619 | bzero(&ro->ro_dst, sizeof(struct sockaddr_in6)); | |
620 | ro->ro_dst.sin6_family = AF_INET6; | |
621 | ro->ro_dst.sin6_len = sizeof(struct sockaddr_in6); | |
622 | ro->ro_dst.sin6_addr = *dst; | |
623 | if (IN6_IS_ADDR_MULTICAST(dst)) { | |
624 | #if defined(__FreeBSD__) || defined (__APPLE__) | |
625 | ro->ro_rt = rtalloc1(&((struct route *)ro) | |
626 | ->ro_dst, 0, 0UL); | |
627 | #endif /*__FreeBSD__*/ | |
628 | #if defined(__bsdi__) || defined(__NetBSD__) | |
629 | ro->ro_rt = rtalloc1(&((struct route *)ro) | |
630 | ->ro_dst, 0); | |
631 | #endif /*__bsdi__*/ | |
632 | } else { | |
633 | rtalloc((struct route *)ro); | |
634 | } | |
635 | } | |
636 | ||
637 | /* | |
638 | * in_pcbconnect() checks out IFF_LOOPBACK to skip using | |
639 | * the address. But we don't know why it does so. | |
640 | * It is necessary to ensure the scope even for lo0 | |
641 | * so doesn't check out IFF_LOOPBACK. | |
642 | */ | |
643 | ||
644 | if (ro->ro_rt) { | |
645 | ia6 = in6_ifawithscope(ro->ro_rt->rt_ifa->ifa_ifp, dst); | |
646 | if (ia6 == 0) /* xxx scope error ?*/ | |
647 | ia6 = ifatoia6(ro->ro_rt->rt_ifa); | |
648 | } | |
649 | #if 0 | |
650 | /* | |
651 | * xxx The followings are necessary? (kazu) | |
652 | * I don't think so. | |
653 | * It's for SO_DONTROUTE option in IPv4.(jinmei) | |
654 | */ | |
655 | if (ia6 == 0) { | |
656 | struct sockaddr_in6 sin6 = {sizeof(sin6), AF_INET6, 0}; | |
657 | ||
658 | sin6->sin6_addr = *dst; | |
659 | ||
660 | ia6 = ifatoia6(ifa_ifwithdstaddr(sin6tosa(&sin6))); | |
661 | if (ia6 == 0) | |
662 | ia6 = ifatoia6(ifa_ifwithnet(sin6tosa(&sin6))); | |
663 | if (ia6 == 0) { | |
664 | *errorp = EHOSTUNREACH; /* no route */ | |
665 | return(0); | |
666 | } | |
667 | return(&satosin6(&ia6->ia_addr)->sin6_addr); | |
668 | } | |
669 | #endif /* 0 */ | |
670 | if (ia6 == 0) { | |
671 | *errorp = EHOSTUNREACH; /* no route */ | |
672 | return(0); | |
673 | } | |
674 | return(&satosin6(&ia6->ia_addr)->sin6_addr); | |
675 | } | |
676 | ||
677 | *errorp = EADDRNOTAVAIL; | |
678 | return(0); | |
679 | } | |
680 | ||
681 | /* | |
682 | * Default hop limit selection. The precedence is as follows: | |
683 | * 1. Hoplimit valued specified via ioctl. | |
684 | * 2. (If the outgoing interface is detected) the current | |
685 | * hop limit of the interface specified by router advertisement. | |
686 | * 3. The system default hoplimit. | |
687 | */ | |
688 | int | |
689 | in6_selecthlim(in6p, ifp) | |
690 | struct in6pcb *in6p; | |
691 | struct ifnet *ifp; | |
692 | { | |
693 | if (in6p && in6p->in6p_hops >= 0) | |
694 | return(in6p->in6p_hops); | |
695 | else if (ifp) | |
696 | return(nd_ifinfo[ifp->if_index].chlim); | |
697 | else | |
698 | return(ip6_defhlim); | |
699 | } | |
700 | #endif | |
701 | ||
702 | void | |
703 | in6_pcbdisconnect(inp) | |
704 | struct inpcb *inp; | |
705 | { | |
706 | bzero((caddr_t)&inp->in6p_faddr, sizeof(inp->in6p_faddr)); | |
707 | inp->inp_fport = 0; | |
708 | in_pcbrehash(inp); | |
709 | if (inp->inp_socket->so_state & SS_NOFDREF) | |
710 | in6_pcbdetach(inp); | |
711 | } | |
712 | ||
713 | void | |
714 | in6_pcbdetach(inp) | |
715 | struct inpcb *inp; | |
716 | { | |
717 | struct socket *so = inp->inp_socket; | |
718 | struct inpcbinfo *ipi = inp->inp_pcbinfo; | |
719 | ||
720 | #if IPSEC | |
721 | ipsec6_delete_pcbpolicy(inp); | |
722 | #endif /* IPSEC */ | |
723 | inp->inp_gencnt = ++ipi->ipi_gencnt; | |
724 | in_pcbremlists(inp); | |
725 | sotoinpcb(so) = 0; | |
726 | sofree(so); | |
727 | ||
728 | if (inp->in6p_inputopts) /* Free all received options. */ | |
729 | m_freem(inp->in6p_inputopts->head); /* this is safe */ | |
730 | ip6_freepcbopts(inp->in6p_outputopts); | |
731 | ip6_freemoptions(inp->in6p_moptions); | |
732 | ||
733 | if (inp->in6p_route.ro_rt) | |
734 | rtfree(inp->in6p_route.ro_rt); | |
735 | inp->inp_vflag = 0; | |
736 | zfree(ipi->ipi_zone, inp); | |
737 | } | |
738 | ||
739 | /* | |
740 | * The calling convention of in6_setsockaddr() and in6_setpeeraddr() was | |
741 | * modified to match the pru_sockaddr() and pru_peeraddr() entry points | |
742 | * in struct pr_usrreqs, so that protocols can just reference then directly | |
743 | * without the need for a wrapper function. The socket must have a valid | |
744 | * (i.e., non-nil) PCB, but it should be impossible to get an invalid one | |
745 | * except through a kernel programming error, so it is acceptable to panic | |
746 | * (or in this case trap) if the PCB is invalid. (Actually, we don't trap | |
747 | * because there actually /is/ a programming error somewhere... XXX) | |
748 | */ | |
749 | int | |
750 | in6_setsockaddr(so, nam) | |
751 | struct socket *so; | |
752 | struct sockaddr **nam; | |
753 | { | |
754 | int s; | |
755 | register struct inpcb *inp; | |
756 | register struct sockaddr_in6 *sin6; | |
757 | ||
758 | /* | |
759 | * Do the malloc first in case it blocks. | |
760 | */ | |
761 | MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, M_SONAME, M_WAITOK); | |
762 | bzero(sin6, sizeof *sin6); | |
763 | sin6->sin6_family = AF_INET6; | |
764 | sin6->sin6_len = sizeof(*sin6); | |
765 | ||
766 | s = splnet(); | |
767 | inp = sotoinpcb(so); | |
768 | if (!inp) { | |
769 | splx(s); | |
770 | _FREE(sin6, M_SONAME); | |
771 | return EINVAL; | |
772 | } | |
773 | sin6->sin6_port = inp->inp_lport; | |
774 | sin6->sin6_addr = inp->in6p_laddr; | |
775 | splx(s); | |
776 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) | |
777 | sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); | |
778 | else | |
779 | sin6->sin6_scope_id = 0; /*XXX*/ | |
780 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) | |
781 | sin6->sin6_addr.s6_addr16[1] = 0; | |
782 | ||
783 | *nam = (struct sockaddr *)sin6; | |
784 | return 0; | |
785 | } | |
786 | ||
787 | int | |
788 | in6_setpeeraddr(so, nam) | |
789 | struct socket *so; | |
790 | struct sockaddr **nam; | |
791 | { | |
792 | int s; | |
793 | struct inpcb *inp; | |
794 | register struct sockaddr_in6 *sin6; | |
795 | ||
796 | /* | |
797 | * Do the malloc first in case it blocks. | |
798 | */ | |
799 | MALLOC(sin6, struct sockaddr_in6 *, sizeof(*sin6), M_SONAME, M_WAITOK); | |
800 | bzero((caddr_t)sin6, sizeof (*sin6)); | |
801 | sin6->sin6_family = AF_INET6; | |
802 | sin6->sin6_len = sizeof(struct sockaddr_in6); | |
803 | ||
804 | s = splnet(); | |
805 | inp = sotoinpcb(so); | |
806 | if (!inp) { | |
807 | splx(s); | |
808 | _FREE(sin6, M_SONAME); | |
809 | return EINVAL; | |
810 | } | |
811 | sin6->sin6_port = inp->inp_fport; | |
812 | sin6->sin6_addr = inp->in6p_faddr; | |
813 | splx(s); | |
814 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) | |
815 | sin6->sin6_scope_id = ntohs(sin6->sin6_addr.s6_addr16[1]); | |
816 | else | |
817 | sin6->sin6_scope_id = 0; /*XXX*/ | |
818 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) | |
819 | sin6->sin6_addr.s6_addr16[1] = 0; | |
820 | ||
821 | *nam = (struct sockaddr *)sin6; | |
822 | return 0; | |
823 | } | |
824 | ||
825 | int | |
826 | in6_mapped_sockaddr(struct socket *so, struct sockaddr **nam) | |
827 | { | |
828 | struct inpcb *inp = sotoinpcb(so); | |
829 | int error; | |
830 | ||
831 | if (inp == NULL) | |
832 | return EINVAL; | |
833 | if (inp->inp_vflag & INP_IPV4) { | |
834 | error = in_setsockaddr(so, nam); | |
835 | if (error == 0) | |
836 | in6_sin_2_v4mapsin6_in_sock(nam); | |
837 | } else | |
838 | error = in6_setsockaddr(so, nam); | |
839 | ||
840 | return error; | |
841 | } | |
842 | ||
843 | int | |
844 | in6_mapped_peeraddr(struct socket *so, struct sockaddr **nam) | |
845 | { | |
846 | struct inpcb *inp = sotoinpcb(so); | |
847 | int error; | |
848 | ||
849 | if (inp == NULL) | |
850 | return EINVAL; | |
851 | if (inp->inp_vflag & INP_IPV4) { | |
852 | error = in_setpeeraddr(so, nam); | |
853 | if (error == 0) | |
854 | in6_sin_2_v4mapsin6_in_sock(nam); | |
855 | } else | |
856 | error = in6_setpeeraddr(so, nam); | |
857 | ||
858 | return error; | |
859 | } | |
860 | ||
861 | /* | |
862 | * Pass some notification to all connections of a protocol | |
863 | * associated with address dst. The local address and/or port numbers | |
864 | * may be specified to limit the search. The "usual action" will be | |
865 | * taken, depending on the ctlinput cmd. The caller must filter any | |
866 | * cmds that are uninteresting (e.g., no error in the map). | |
867 | * Call the protocol specific routine (if any) to report | |
868 | * any errors for each matching socket. | |
869 | * | |
870 | * Must be called at splnet. | |
871 | */ | |
872 | void | |
873 | in6_pcbnotify(head, dst, fport_arg, laddr6, lport_arg, cmd, notify) | |
874 | struct inpcbhead *head; | |
875 | struct sockaddr *dst; | |
876 | u_int fport_arg, lport_arg; | |
877 | struct in6_addr *laddr6; | |
878 | int cmd; | |
879 | void (*notify) __P((struct inpcb *, int)); | |
880 | { | |
881 | struct inpcb *inp, *ninp; | |
882 | struct in6_addr faddr6; | |
883 | u_short fport = fport_arg, lport = lport_arg; | |
884 | int errno, s; | |
885 | void (*notify2) __P((struct inpcb *, int)); | |
886 | ||
887 | notify2 = NULL; | |
888 | ||
889 | if ((unsigned)cmd > PRC_NCMDS || dst->sa_family != AF_INET6) | |
890 | return; | |
891 | faddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr; | |
892 | if (IN6_IS_ADDR_UNSPECIFIED(&faddr6)) | |
893 | return; | |
894 | ||
895 | /* | |
896 | * Redirects go to all references to the destination, | |
897 | * and use in6_rtchange to invalidate the route cache. | |
898 | * Dead host indications: also use in6_rtchange to invalidate | |
899 | * the cache, and deliver the error to all the sockets. | |
900 | * Otherwise, if we have knowledge of the local port and address, | |
901 | * deliver only to that socket. | |
902 | */ | |
903 | if (PRC_IS_REDIRECT(cmd) || cmd == PRC_HOSTDEAD) { | |
904 | fport = 0; | |
905 | lport = 0; | |
906 | bzero((caddr_t)laddr6, sizeof(*laddr6)); | |
907 | ||
908 | /* | |
909 | * Keep the old notify function to store a soft error | |
910 | * in each PCB. | |
911 | */ | |
912 | if (cmd == PRC_HOSTDEAD && notify != in6_rtchange) | |
913 | notify2 = notify; | |
914 | ||
915 | notify = in6_rtchange; | |
916 | } | |
917 | errno = inet6ctlerrmap[cmd]; | |
918 | s = splnet(); | |
919 | for (inp = LIST_FIRST(head); inp != NULL; inp = ninp) { | |
920 | ninp = LIST_NEXT(inp, inp_list); | |
921 | ||
922 | if ((inp->inp_vflag & INP_IPV6) == 0) | |
923 | continue; | |
924 | ||
925 | if (notify == in6_rtchange) { | |
926 | /* | |
927 | * Since a non-connected PCB might have a cached route, | |
928 | * we always call in6_rtchange without matching | |
929 | * the PCB to the src/dst pair. | |
930 | * | |
931 | * XXX: we assume in6_rtchange does not free the PCB. | |
932 | */ | |
933 | if (IN6_ARE_ADDR_EQUAL(&inp->in6p_route.ro_dst.sin6_addr, | |
934 | &faddr6)) | |
935 | in6_rtchange(inp, errno); | |
936 | ||
937 | if (notify2 == NULL) | |
938 | continue; | |
939 | ||
940 | notify = notify2; | |
941 | } | |
942 | ||
943 | if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &faddr6) || | |
944 | inp->inp_socket == 0 || | |
945 | (lport && inp->inp_lport != lport) || | |
946 | (!IN6_IS_ADDR_UNSPECIFIED(laddr6) && | |
947 | !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr6)) || | |
948 | (fport && inp->inp_fport != fport)) | |
949 | continue; | |
950 | ||
951 | if (notify) | |
952 | (*notify)(inp, errno); | |
953 | } | |
954 | splx(s); | |
955 | } | |
956 | ||
957 | /* | |
958 | * Lookup a PCB based on the local address and port. | |
959 | */ | |
960 | struct inpcb * | |
961 | in6_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay) | |
962 | struct inpcbinfo *pcbinfo; | |
963 | struct in6_addr *laddr; | |
964 | u_int lport_arg; | |
965 | int wild_okay; | |
966 | { | |
967 | register struct inpcb *inp; | |
968 | int matchwild = 3, wildcard; | |
969 | u_short lport = lport_arg; | |
970 | ||
971 | if (!wild_okay) { | |
972 | struct inpcbhead *head; | |
973 | /* | |
974 | * Look for an unconnected (wildcard foreign addr) PCB that | |
975 | * matches the local address and port we're looking for. | |
976 | */ | |
977 | head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, | |
978 | pcbinfo->hashmask)]; | |
979 | LIST_FOREACH(inp, head, inp_hash) { | |
980 | if ((inp->inp_vflag & INP_IPV6) == 0) | |
981 | continue; | |
982 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && | |
983 | IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && | |
984 | inp->inp_lport == lport) { | |
985 | /* | |
986 | * Found. | |
987 | */ | |
988 | return (inp); | |
989 | } | |
990 | } | |
991 | /* | |
992 | * Not found. | |
993 | */ | |
994 | return (NULL); | |
995 | } else { | |
996 | struct inpcbporthead *porthash; | |
997 | struct inpcbport *phd; | |
998 | struct inpcb *match = NULL; | |
999 | /* | |
1000 | * Best fit PCB lookup. | |
1001 | * | |
1002 | * First see if this local port is in use by looking on the | |
1003 | * port hash list. | |
1004 | */ | |
1005 | porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport, | |
1006 | pcbinfo->porthashmask)]; | |
1007 | LIST_FOREACH(phd, porthash, phd_hash) { | |
1008 | if (phd->phd_port == lport) | |
1009 | break; | |
1010 | } | |
1011 | if (phd != NULL) { | |
1012 | /* | |
1013 | * Port is in use by one or more PCBs. Look for best | |
1014 | * fit. | |
1015 | */ | |
1016 | LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) { | |
1017 | wildcard = 0; | |
1018 | if ((inp->inp_vflag & INP_IPV6) == 0) | |
1019 | continue; | |
1020 | if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) | |
1021 | wildcard++; | |
1022 | if (!IN6_IS_ADDR_UNSPECIFIED( | |
1023 | &inp->in6p_laddr)) { | |
1024 | if (IN6_IS_ADDR_UNSPECIFIED(laddr)) | |
1025 | wildcard++; | |
1026 | else if (!IN6_ARE_ADDR_EQUAL( | |
1027 | &inp->in6p_laddr, laddr)) | |
1028 | continue; | |
1029 | } else { | |
1030 | if (!IN6_IS_ADDR_UNSPECIFIED(laddr)) | |
1031 | wildcard++; | |
1032 | } | |
1033 | if (wildcard < matchwild) { | |
1034 | match = inp; | |
1035 | matchwild = wildcard; | |
1036 | if (matchwild == 0) { | |
1037 | break; | |
1038 | } | |
1039 | } | |
1040 | } | |
1041 | } | |
1042 | return (match); | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | /* | |
1047 | * Check for alternatives when higher level complains | |
1048 | * about service problems. For now, invalidate cached | |
1049 | * routing information. If the route was created dynamically | |
1050 | * (by a redirect), time to try a default gateway again. | |
1051 | */ | |
1052 | void | |
1053 | in6_losing(in6p) | |
1054 | struct inpcb *in6p; | |
1055 | { | |
1056 | struct rtentry *rt; | |
1057 | struct rt_addrinfo info; | |
1058 | ||
1059 | if ((rt = in6p->in6p_route.ro_rt) != NULL) { | |
1060 | in6p->in6p_route.ro_rt = 0; | |
1061 | bzero((caddr_t)&info, sizeof(info)); | |
1062 | info.rti_info[RTAX_DST] = | |
1063 | (struct sockaddr *)&in6p->in6p_route.ro_dst; | |
1064 | info.rti_info[RTAX_GATEWAY] = rt->rt_gateway; | |
1065 | info.rti_info[RTAX_NETMASK] = rt_mask(rt); | |
1066 | rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0); | |
1067 | if (rt->rt_flags & RTF_DYNAMIC) | |
1068 | (void)rtrequest(RTM_DELETE, rt_key(rt), | |
1069 | rt->rt_gateway, rt_mask(rt), rt->rt_flags, | |
1070 | (struct rtentry **)0); | |
1071 | else | |
1072 | /* | |
1073 | * A new route can be allocated | |
1074 | * the next time output is attempted. | |
1075 | */ | |
1076 | rtfree(rt); | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | /* | |
1081 | * After a routing change, flush old routing | |
1082 | * and allocate a (hopefully) better one. | |
1083 | */ | |
1084 | void | |
1085 | in6_rtchange(inp, errno) | |
1086 | struct inpcb *inp; | |
1087 | int errno; | |
1088 | { | |
1089 | if (inp->in6p_route.ro_rt) { | |
1090 | rtfree(inp->in6p_route.ro_rt); | |
1091 | inp->in6p_route.ro_rt = 0; | |
1092 | /* | |
1093 | * A new route can be allocated the next time | |
1094 | * output is attempted. | |
1095 | */ | |
1096 | } | |
1097 | } | |
1098 | ||
1099 | /* | |
1100 | * Lookup PCB in hash list. | |
1101 | */ | |
1102 | struct inpcb * | |
1103 | in6_pcblookup_hash(pcbinfo, faddr, fport_arg, laddr, lport_arg, wildcard, ifp) | |
1104 | struct inpcbinfo *pcbinfo; | |
1105 | struct in6_addr *faddr, *laddr; | |
1106 | u_int fport_arg, lport_arg; | |
1107 | int wildcard; | |
1108 | struct ifnet *ifp; | |
1109 | { | |
1110 | struct inpcbhead *head; | |
1111 | register struct inpcb *inp; | |
1112 | u_short fport = fport_arg, lport = lport_arg; | |
1113 | ||
1114 | /* | |
1115 | * First look for an exact match. | |
1116 | */ | |
1117 | head = &pcbinfo->hashbase[INP_PCBHASH(faddr->s6_addr32[3] /* XXX */, | |
1118 | lport, fport, | |
1119 | pcbinfo->hashmask)]; | |
1120 | for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) { | |
1121 | if ((inp->inp_vflag & INP_IPV6) == 0) | |
1122 | continue; | |
1123 | if (IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, faddr) && | |
1124 | IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, laddr) && | |
1125 | inp->inp_fport == fport && | |
1126 | inp->inp_lport == lport) { | |
1127 | /* | |
1128 | * Found. | |
1129 | */ | |
1130 | return (inp); | |
1131 | } | |
1132 | } | |
1133 | if (wildcard) { | |
1134 | struct inpcb *local_wild = NULL; | |
1135 | ||
1136 | head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, | |
1137 | pcbinfo->hashmask)]; | |
1138 | for (inp = head->lh_first; inp != NULL; | |
1139 | inp = inp->inp_hash.le_next) { | |
1140 | if ((inp->inp_vflag & INP_IPV6) == 0) | |
1141 | continue; | |
1142 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) && | |
1143 | inp->inp_lport == lport) { | |
1144 | #if defined(NFAITH) && NFAITH > 0 | |
1145 | if (ifp && ifp->if_type == IFT_FAITH && | |
1146 | (inp->inp_flags & INP_FAITH) == 0) | |
1147 | continue; | |
1148 | #endif | |
1149 | if (IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, | |
1150 | laddr)) | |
1151 | return (inp); | |
1152 | else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) | |
1153 | local_wild = inp; | |
1154 | } | |
1155 | } | |
1156 | return (local_wild); | |
1157 | } | |
1158 | ||
1159 | /* | |
1160 | * Not found. | |
1161 | */ | |
1162 | return (NULL); | |
1163 | } | |
1164 | ||
1165 | void | |
1166 | init_sin6(struct sockaddr_in6 *sin6, struct mbuf *m) | |
1167 | { | |
1168 | struct ip6_hdr *ip; | |
1169 | ||
1170 | ip = mtod(m, struct ip6_hdr *); | |
1171 | bzero(sin6, sizeof(*sin6)); | |
1172 | sin6->sin6_len = sizeof(*sin6); | |
1173 | sin6->sin6_family = AF_INET6; | |
1174 | sin6->sin6_addr = ip->ip6_src; | |
1175 | if (IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) | |
1176 | sin6->sin6_addr.s6_addr16[1] = 0; | |
1177 | sin6->sin6_scope_id = | |
1178 | (m->m_pkthdr.rcvif && IN6_IS_SCOPE_LINKLOCAL(&sin6->sin6_addr)) | |
1179 | ? m->m_pkthdr.rcvif->if_index : 0; | |
1180 | ||
1181 | return; | |
1182 | } |