]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/in_pcb.c
669076b3a8781cee947730e13981c5aedf5bf053
[apple/xnu.git] / bsd / netinet / in_pcb.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /*
31 * Copyright (c) 1982, 1986, 1991, 1993, 1995
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.4 (Berkeley) 5/24/95
63 * $FreeBSD: src/sys/netinet/in_pcb.c,v 1.59.2.17 2001/08/13 16:26:17 ume Exp $
64 */
65
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
69 #include <sys/mbuf.h>
70 #include <sys/domain.h>
71 #include <sys/protosw.h>
72 #include <sys/socket.h>
73 #include <sys/socketvar.h>
74 #include <sys/proc.h>
75 #ifndef __APPLE__
76 #include <sys/jail.h>
77 #endif
78 #include <sys/kernel.h>
79 #include <sys/sysctl.h>
80 #include <libkern/OSAtomic.h>
81
82 #include <machine/limits.h>
83
84 #ifdef __APPLE__
85 #include <kern/zalloc.h>
86 #endif
87
88 #include <net/if.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91
92 #include <netinet/in.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_var.h>
95 #include <netinet/ip_var.h>
96 #if INET6
97 #include <netinet/ip6.h>
98 #include <netinet6/ip6_var.h>
99 #endif /* INET6 */
100
101 #include "faith.h"
102
103 #if IPSEC
104 #include <netinet6/ipsec.h>
105 #include <netkey/key.h>
106 #endif /* IPSEC */
107
108 #include <sys/kdebug.h>
109
110 #if IPSEC
111 extern int ipsec_bypass;
112 extern lck_mtx_t *sadb_mutex;
113 #endif
114
115 extern u_long route_generation;
116
117 #define DBG_FNC_PCB_LOOKUP NETDBG_CODE(DBG_NETTCP, (6 << 8))
118 #define DBG_FNC_PCB_HLOOKUP NETDBG_CODE(DBG_NETTCP, ((6 << 8) | 1))
119
120 struct in_addr zeroin_addr;
121
122 /*
123 * These configure the range of local port addresses assigned to
124 * "unspecified" outgoing connections/packets/whatever.
125 */
126 int ipport_lowfirstauto = IPPORT_RESERVED - 1; /* 1023 */
127 int ipport_lowlastauto = IPPORT_RESERVEDSTART; /* 600 */
128 #ifndef __APPLE__
129 int ipport_firstauto = IPPORT_RESERVED; /* 1024 */
130 int ipport_lastauto = IPPORT_USERRESERVED; /* 5000 */
131 #else
132 int ipport_firstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
133 int ipport_lastauto = IPPORT_HILASTAUTO; /* 65535 */
134 #endif
135 int ipport_hifirstauto = IPPORT_HIFIRSTAUTO; /* 49152 */
136 int ipport_hilastauto = IPPORT_HILASTAUTO; /* 65535 */
137
138 #define RANGECHK(var, min, max) \
139 if ((var) < (min)) { (var) = (min); } \
140 else if ((var) > (max)) { (var) = (max); }
141
142 static int
143 sysctl_net_ipport_check SYSCTL_HANDLER_ARGS
144 {
145 int error = sysctl_handle_int(oidp,
146 oidp->oid_arg1, oidp->oid_arg2, req);
147 if (!error) {
148 RANGECHK(ipport_lowfirstauto, 1, IPPORT_RESERVED - 1);
149 RANGECHK(ipport_lowlastauto, 1, IPPORT_RESERVED - 1);
150 RANGECHK(ipport_firstauto, IPPORT_RESERVED, USHRT_MAX);
151 RANGECHK(ipport_lastauto, IPPORT_RESERVED, USHRT_MAX);
152 RANGECHK(ipport_hifirstauto, IPPORT_RESERVED, USHRT_MAX);
153 RANGECHK(ipport_hilastauto, IPPORT_RESERVED, USHRT_MAX);
154 }
155 return error;
156 }
157
158 #undef RANGECHK
159
160 SYSCTL_NODE(_net_inet_ip, IPPROTO_IP, portrange, CTLFLAG_RW, 0, "IP Ports");
161
162 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowfirst, CTLTYPE_INT|CTLFLAG_RW,
163 &ipport_lowfirstauto, 0, &sysctl_net_ipport_check, "I", "");
164 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, lowlast, CTLTYPE_INT|CTLFLAG_RW,
165 &ipport_lowlastauto, 0, &sysctl_net_ipport_check, "I", "");
166 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, first, CTLTYPE_INT|CTLFLAG_RW,
167 &ipport_firstauto, 0, &sysctl_net_ipport_check, "I", "");
168 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, last, CTLTYPE_INT|CTLFLAG_RW,
169 &ipport_lastauto, 0, &sysctl_net_ipport_check, "I", "");
170 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hifirst, CTLTYPE_INT|CTLFLAG_RW,
171 &ipport_hifirstauto, 0, &sysctl_net_ipport_check, "I", "");
172 SYSCTL_PROC(_net_inet_ip_portrange, OID_AUTO, hilast, CTLTYPE_INT|CTLFLAG_RW,
173 &ipport_hilastauto, 0, &sysctl_net_ipport_check, "I", "");
174
175 /*
176 * in_pcb.c: manage the Protocol Control Blocks.
177 *
178 * NOTE: It is assumed that most of these functions will be called at
179 * splnet(). XXX - There are, unfortunately, a few exceptions to this
180 * rule that should be fixed.
181 */
182
183 /*
184 * Allocate a PCB and associate it with the socket.
185 */
186 int
187 in_pcballoc(so, pcbinfo, p)
188 struct socket *so;
189 struct inpcbinfo *pcbinfo;
190 struct proc *p;
191 {
192 register struct inpcb *inp;
193 caddr_t temp;
194 #if IPSEC
195 #ifndef __APPLE__
196 int error;
197 #endif
198 #endif
199
200 if (so->cached_in_sock_layer == 0) {
201 #if TEMPDEBUG
202 printf("PCBALLOC calling zalloc for socket %x\n", so);
203 #endif
204 inp = (struct inpcb *) zalloc(pcbinfo->ipi_zone);
205 if (inp == NULL)
206 return (ENOBUFS);
207 bzero((caddr_t)inp, sizeof(*inp));
208 }
209 else {
210 #if TEMPDEBUG
211 printf("PCBALLOC reusing PCB for socket %x\n", so);
212 #endif
213 inp = (struct inpcb *) so->so_saved_pcb;
214 temp = inp->inp_saved_ppcb;
215 bzero((caddr_t) inp, sizeof(*inp));
216 inp->inp_saved_ppcb = temp;
217 }
218
219 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
220 inp->inp_pcbinfo = pcbinfo;
221 inp->inp_socket = so;
222 so->so_pcb = (caddr_t)inp;
223
224 if (so->so_proto->pr_flags & PR_PCBLOCK) {
225 inp->inpcb_mtx = lck_mtx_alloc_init(pcbinfo->mtx_grp, pcbinfo->mtx_attr);
226 if (inp->inpcb_mtx == NULL) {
227 printf("in_pcballoc: can't alloc mutex! so=%x\n", so);
228 return(ENOMEM);
229 }
230 }
231
232 #if IPSEC
233 #ifndef __APPLE__
234 if (ipsec_bypass == 0) {
235 lck_mtx_lock(sadb_mutex);
236 error = ipsec_init_policy(so, &inp->inp_sp);
237 lck_mtx_unlock(sadb_mutex);
238 if (error != 0) {
239 zfree(pcbinfo->ipi_zone, inp);
240 return error;
241 }
242 }
243 #endif
244 #endif /*IPSEC*/
245 #if defined(INET6)
246 if (INP_SOCKAF(so) == AF_INET6 && !ip6_mapped_addr_on)
247 inp->inp_flags |= IN6P_IPV6_V6ONLY;
248 #endif
249
250 #if INET6
251 if (ip6_auto_flowlabel)
252 inp->inp_flags |= IN6P_AUTOFLOWLABEL;
253 #endif
254 lck_rw_lock_exclusive(pcbinfo->mtx);
255 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
256 LIST_INSERT_HEAD(pcbinfo->listhead, inp, inp_list);
257 pcbinfo->ipi_count++;
258 lck_rw_done(pcbinfo->mtx);
259 return (0);
260 }
261
262 int
263 in_pcbbind(inp, nam, p)
264 register struct inpcb *inp;
265 struct sockaddr *nam;
266 struct proc *p;
267 {
268 register struct socket *so = inp->inp_socket;
269 unsigned short *lastport;
270 struct sockaddr_in *sin;
271 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
272 u_short lport = 0;
273 int wild = 0, reuseport = (so->so_options & SO_REUSEPORT);
274 int error;
275
276 if (TAILQ_EMPTY(&in_ifaddrhead)) /* XXX broken! */
277 return (EADDRNOTAVAIL);
278 if (inp->inp_lport || inp->inp_laddr.s_addr != INADDR_ANY)
279 return (EINVAL);
280 if ((so->so_options & (SO_REUSEADDR|SO_REUSEPORT)) == 0)
281 wild = 1;
282 socket_unlock(so, 0); /* keep reference on socket */
283 lck_rw_lock_exclusive(pcbinfo->mtx);
284 if (nam) {
285 sin = (struct sockaddr_in *)nam;
286 if (nam->sa_len != sizeof (*sin)) {
287 lck_rw_done(pcbinfo->mtx);
288 socket_lock(so, 0);
289 return (EINVAL);
290 }
291 #ifdef notdef
292 /*
293 * We should check the family, but old programs
294 * incorrectly fail to initialize it.
295 */
296 if (sin->sin_family != AF_INET) {
297 lck_rw_done(pcbinfo->mtx);
298 socket_lock(so, 0);
299 return (EAFNOSUPPORT);
300 }
301 #endif
302 lport = sin->sin_port;
303 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
304 /*
305 * Treat SO_REUSEADDR as SO_REUSEPORT for multicast;
306 * allow complete duplication of binding if
307 * SO_REUSEPORT is set, or if SO_REUSEADDR is set
308 * and a multicast address is bound on both
309 * new and duplicated sockets.
310 */
311 if (so->so_options & SO_REUSEADDR)
312 reuseport = SO_REUSEADDR|SO_REUSEPORT;
313 } else if (sin->sin_addr.s_addr != INADDR_ANY) {
314 struct ifaddr *ifa;
315 sin->sin_port = 0; /* yech... */
316 if ((ifa = ifa_ifwithaddr((struct sockaddr *)sin)) == 0) {
317 lck_rw_done(pcbinfo->mtx);
318 socket_lock(so, 0);
319 return (EADDRNOTAVAIL);
320 }
321 else {
322 ifafree(ifa);
323 }
324 }
325 if (lport) {
326 struct inpcb *t;
327
328 /* GROSS */
329 if (ntohs(lport) < IPPORT_RESERVED && p &&
330 proc_suser(p)) {
331 lck_rw_done(pcbinfo->mtx);
332 socket_lock(so, 0);
333 return (EACCES);
334 }
335 if (so->so_uid &&
336 !IN_MULTICAST(ntohl(sin->sin_addr.s_addr))) {
337 t = in_pcblookup_local(inp->inp_pcbinfo,
338 sin->sin_addr, lport, INPLOOKUP_WILDCARD);
339 if (t &&
340 (ntohl(sin->sin_addr.s_addr) != INADDR_ANY ||
341 ntohl(t->inp_laddr.s_addr) != INADDR_ANY ||
342 (t->inp_socket->so_options &
343 SO_REUSEPORT) == 0) &&
344 (so->so_uid != t->inp_socket->so_uid)) {
345 #if INET6
346 if (ntohl(sin->sin_addr.s_addr) !=
347 INADDR_ANY ||
348 ntohl(t->inp_laddr.s_addr) !=
349 INADDR_ANY ||
350 INP_SOCKAF(so) ==
351 INP_SOCKAF(t->inp_socket)) {
352 #endif /* defined(INET6) */
353 lck_rw_done(pcbinfo->mtx);
354 socket_lock(so, 0);
355 return (EADDRINUSE);
356 }
357 }
358 }
359 t = in_pcblookup_local(pcbinfo, sin->sin_addr,
360 lport, wild);
361 if (t &&
362 (reuseport & t->inp_socket->so_options) == 0) {
363 #if INET6
364 if (ip6_mapped_addr_on == 0 ||
365 ntohl(sin->sin_addr.s_addr) !=
366 INADDR_ANY ||
367 ntohl(t->inp_laddr.s_addr) !=
368 INADDR_ANY ||
369 INP_SOCKAF(so) ==
370 INP_SOCKAF(t->inp_socket)) {
371 #endif /* defined(INET6) */
372 lck_rw_done(pcbinfo->mtx);
373 socket_lock(so, 0);
374 return (EADDRINUSE);
375 }
376 }
377 }
378 inp->inp_laddr = sin->sin_addr;
379 }
380 if (lport == 0) {
381 u_short first, last;
382 int count;
383
384 inp->inp_flags |= INP_ANONPORT;
385
386 if (inp->inp_flags & INP_HIGHPORT) {
387 first = ipport_hifirstauto; /* sysctl */
388 last = ipport_hilastauto;
389 lastport = &pcbinfo->lasthi;
390 } else if (inp->inp_flags & INP_LOWPORT) {
391 if (p && (error = proc_suser(p))) {
392 lck_rw_done(pcbinfo->mtx);
393 socket_lock(so, 0);
394 return error;
395 }
396 first = ipport_lowfirstauto; /* 1023 */
397 last = ipport_lowlastauto; /* 600 */
398 lastport = &pcbinfo->lastlow;
399 } else {
400 first = ipport_firstauto; /* sysctl */
401 last = ipport_lastauto;
402 lastport = &pcbinfo->lastport;
403 }
404 /*
405 * Simple check to ensure all ports are not used up causing
406 * a deadlock here.
407 *
408 * We split the two cases (up and down) so that the direction
409 * is not being tested on each round of the loop.
410 */
411 if (first > last) {
412 /*
413 * counting down
414 */
415 count = first - last;
416
417 do {
418 if (count-- < 0) { /* completely used? */
419 lck_rw_done(pcbinfo->mtx);
420 socket_lock(so, 0);
421 inp->inp_laddr.s_addr = INADDR_ANY;
422 return (EADDRNOTAVAIL);
423 }
424 --*lastport;
425 if (*lastport > first || *lastport < last)
426 *lastport = first;
427 lport = htons(*lastport);
428 } while (in_pcblookup_local(pcbinfo,
429 inp->inp_laddr, lport, wild));
430 } else {
431 /*
432 * counting up
433 */
434 count = last - first;
435
436 do {
437 if (count-- < 0) { /* completely used? */
438 lck_rw_done(pcbinfo->mtx);
439 socket_lock(so, 0);
440 inp->inp_laddr.s_addr = INADDR_ANY;
441 return (EADDRNOTAVAIL);
442 }
443 ++*lastport;
444 if (*lastport < first || *lastport > last)
445 *lastport = first;
446 lport = htons(*lastport);
447 } while (in_pcblookup_local(pcbinfo,
448 inp->inp_laddr, lport, wild));
449 }
450 }
451 socket_lock(so, 0);
452 inp->inp_lport = lport;
453 if (in_pcbinshash(inp, 1) != 0) {
454 inp->inp_laddr.s_addr = INADDR_ANY;
455 inp->inp_lport = 0;
456 lck_rw_done(pcbinfo->mtx);
457 return (EAGAIN);
458 }
459 lck_rw_done(pcbinfo->mtx);
460 return (0);
461 }
462
463 /*
464 * Transform old in_pcbconnect() into an inner subroutine for new
465 * in_pcbconnect(): Do some validity-checking on the remote
466 * address (in mbuf 'nam') and then determine local host address
467 * (i.e., which interface) to use to access that remote host.
468 *
469 * This preserves definition of in_pcbconnect(), while supporting a
470 * slightly different version for T/TCP. (This is more than
471 * a bit of a kludge, but cleaning up the internal interfaces would
472 * have forced minor changes in every protocol).
473 */
474
475 int
476 in_pcbladdr(inp, nam, plocal_sin)
477 register struct inpcb *inp;
478 struct sockaddr *nam;
479 struct sockaddr_in **plocal_sin;
480 {
481 struct in_ifaddr *ia;
482 register struct sockaddr_in *sin = (struct sockaddr_in *)nam;
483
484 if (nam->sa_len != sizeof (*sin))
485 return (EINVAL);
486 if (sin->sin_family != AF_INET)
487 return (EAFNOSUPPORT);
488 if (sin->sin_port == 0)
489 return (EADDRNOTAVAIL);
490 lck_mtx_lock(rt_mtx);
491 if (!TAILQ_EMPTY(&in_ifaddrhead)) {
492 /*
493 * If the destination address is INADDR_ANY,
494 * use the primary local address.
495 * If the supplied address is INADDR_BROADCAST,
496 * and the primary interface supports broadcast,
497 * choose the broadcast address for that interface.
498 */
499 #define satosin(sa) ((struct sockaddr_in *)(sa))
500 #define sintosa(sin) ((struct sockaddr *)(sin))
501 #define ifatoia(ifa) ((struct in_ifaddr *)(ifa))
502 if (sin->sin_addr.s_addr == INADDR_ANY)
503 sin->sin_addr = IA_SIN(TAILQ_FIRST(&in_ifaddrhead))->sin_addr;
504 else if (sin->sin_addr.s_addr == (u_long)INADDR_BROADCAST &&
505 (TAILQ_FIRST(&in_ifaddrhead)->ia_ifp->if_flags & IFF_BROADCAST))
506 sin->sin_addr = satosin(&TAILQ_FIRST(&in_ifaddrhead)->ia_broadaddr)->sin_addr;
507 }
508 if (inp->inp_laddr.s_addr == INADDR_ANY) {
509 register struct route *ro;
510
511 ia = (struct in_ifaddr *)0;
512 /*
513 * If route is known or can be allocated now,
514 * our src addr is taken from the i/f, else punt.
515 * Note that we should check the address family of the cached
516 * destination, in case of sharing the cache with IPv6.
517 */
518 ro = &inp->inp_route;
519 if (ro->ro_rt &&
520 (ro->ro_dst.sa_family != AF_INET ||
521 satosin(&ro->ro_dst)->sin_addr.s_addr !=
522 sin->sin_addr.s_addr ||
523 inp->inp_socket->so_options & SO_DONTROUTE ||
524 ro->ro_rt->generation_id != route_generation)) {
525 rtfree_locked(ro->ro_rt);
526 ro->ro_rt = (struct rtentry *)0;
527 }
528 if ((inp->inp_socket->so_options & SO_DONTROUTE) == 0 && /*XXX*/
529 (ro->ro_rt == (struct rtentry *)0 ||
530 ro->ro_rt->rt_ifp == 0)) {
531 /* No route yet, so try to acquire one */
532 bzero(&ro->ro_dst, sizeof(struct sockaddr_in));
533 ro->ro_dst.sa_family = AF_INET;
534 ro->ro_dst.sa_len = sizeof(struct sockaddr_in);
535 ((struct sockaddr_in *) &ro->ro_dst)->sin_addr =
536 sin->sin_addr;
537 rtalloc_ign_locked(ro, 0UL);
538 }
539 /*
540 * If we found a route, use the address
541 * corresponding to the outgoing interface
542 * unless it is the loopback (in case a route
543 * to our address on another net goes to loopback).
544 */
545 if (ro->ro_rt && !(ro->ro_rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
546 ia = ifatoia(ro->ro_rt->rt_ifa);
547 if (ia)
548 ifaref(&ia->ia_ifa);
549 }
550 if (ia == 0) {
551 u_short fport = sin->sin_port;
552
553 sin->sin_port = 0;
554 ia = ifatoia(ifa_ifwithdstaddr(sintosa(sin)));
555 if (ia == 0) {
556 ia = ifatoia(ifa_ifwithnet(sintosa(sin)));
557 }
558 sin->sin_port = fport;
559 if (ia == 0) {
560 ia = TAILQ_FIRST(&in_ifaddrhead);
561 if (ia)
562 ifaref(&ia->ia_ifa);
563 }
564 if (ia == 0) {
565 lck_mtx_unlock(rt_mtx);
566 return (EADDRNOTAVAIL);
567 }
568 }
569 /*
570 * If the destination address is multicast and an outgoing
571 * interface has been set as a multicast option, use the
572 * address of that interface as our source address.
573 */
574 if (IN_MULTICAST(ntohl(sin->sin_addr.s_addr)) &&
575 inp->inp_moptions != NULL) {
576 struct ip_moptions *imo;
577 struct ifnet *ifp;
578
579 imo = inp->inp_moptions;
580 if (imo->imo_multicast_ifp != NULL && (ia == NULL ||
581 ia->ia_ifp != imo->imo_multicast_ifp)) {
582 ifp = imo->imo_multicast_ifp;
583 if (ia)
584 ifafree(&ia->ia_ifa);
585 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
586 if (ia->ia_ifp == ifp)
587 break;
588 if (ia == 0) {
589 lck_mtx_unlock(rt_mtx);
590 return (EADDRNOTAVAIL);
591 }
592 ifaref(ia);
593 }
594 }
595 /*
596 * Don't do pcblookup call here; return interface in plocal_sin
597 * and exit to caller, that will do the lookup.
598 */
599 *plocal_sin = &ia->ia_addr;
600 ifafree(&ia->ia_ifa);
601 }
602 lck_mtx_unlock(rt_mtx);
603 return(0);
604 }
605
606 /*
607 * Outer subroutine:
608 * Connect from a socket to a specified address.
609 * Both address and port must be specified in argument sin.
610 * If don't have a local address for this socket yet,
611 * then pick one.
612 */
613 int
614 in_pcbconnect(inp, nam, p)
615 register struct inpcb *inp;
616 struct sockaddr *nam;
617 struct proc *p;
618 {
619 struct sockaddr_in *ifaddr;
620 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
621 struct inpcb *pcb;
622 int error;
623
624 /*
625 * Call inner routine, to assign local interface address.
626 */
627 if ((error = in_pcbladdr(inp, nam, &ifaddr)) != 0)
628 return(error);
629
630 socket_unlock(inp->inp_socket, 0);
631 pcb = in_pcblookup_hash(inp->inp_pcbinfo, sin->sin_addr, sin->sin_port,
632 inp->inp_laddr.s_addr ? inp->inp_laddr : ifaddr->sin_addr,
633 inp->inp_lport, 0, NULL);
634 socket_lock(inp->inp_socket, 0);
635 if (pcb != NULL) {
636 in_pcb_checkstate(pcb, WNT_RELEASE, 0);
637 return (EADDRINUSE);
638 }
639 if (inp->inp_laddr.s_addr == INADDR_ANY) {
640 if (inp->inp_lport == 0) {
641 error = in_pcbbind(inp, (struct sockaddr *)0, p);
642 if (error)
643 return (error);
644 }
645 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
646 /*lock inversion issue, mostly with udp multicast packets */
647 socket_unlock(inp->inp_socket, 0);
648 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
649 socket_lock(inp->inp_socket, 0);
650 }
651 inp->inp_laddr = ifaddr->sin_addr;
652 inp->inp_flags |= INP_INADDR_ANY;
653 }
654 else {
655 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
656 /*lock inversion issue, mostly with udp multicast packets */
657 socket_unlock(inp->inp_socket, 0);
658 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
659 socket_lock(inp->inp_socket, 0);
660 }
661 }
662 inp->inp_faddr = sin->sin_addr;
663 inp->inp_fport = sin->sin_port;
664 in_pcbrehash(inp);
665 lck_rw_done(inp->inp_pcbinfo->mtx);
666 return (0);
667 }
668
669 void
670 in_pcbdisconnect(inp)
671 struct inpcb *inp;
672 {
673
674 inp->inp_faddr.s_addr = INADDR_ANY;
675 inp->inp_fport = 0;
676
677 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
678 /*lock inversion issue, mostly with udp multicast packets */
679 socket_unlock(inp->inp_socket, 0);
680 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
681 socket_lock(inp->inp_socket, 0);
682 }
683
684 in_pcbrehash(inp);
685 lck_rw_done(inp->inp_pcbinfo->mtx);
686
687 if (inp->inp_socket->so_state & SS_NOFDREF)
688 in_pcbdetach(inp);
689 }
690
691 void
692 in_pcbdetach(inp)
693 struct inpcb *inp;
694 {
695 struct socket *so = inp->inp_socket;
696 struct rtentry *rt = inp->inp_route.ro_rt;
697
698 if (so->so_pcb == 0) { /* we've been called twice */
699 panic("in_pcbdetach: inp=%x so=%x proto=%x so_pcb is null!\n",
700 inp, so, so->so_proto->pr_protocol);
701 }
702
703 #if IPSEC
704 if (ipsec_bypass == 0) {
705 lck_mtx_lock(sadb_mutex);
706 ipsec4_delete_pcbpolicy(inp);
707 lck_mtx_unlock(sadb_mutex);
708 }
709 #endif /*IPSEC*/
710
711 /* mark socket state as dead */
712 if (in_pcb_checkstate(inp, WNT_STOPUSING, 1) != WNT_STOPUSING)
713 panic("in_pcbdetach so=%x prot=%x couldn't set to STOPUSING\n", so, so->so_proto->pr_protocol);
714
715 #if TEMPDEBUG
716 if (so->cached_in_sock_layer)
717 printf("in_pcbdetach for cached socket %x flags=%x\n", so, so->so_flags);
718 else
719 printf("in_pcbdetach for allocated socket %x flags=%x\n", so, so->so_flags);
720 #endif
721 if ((so->so_flags & SOF_PCBCLEARING) == 0) {
722 inp->inp_vflag = 0;
723 if (inp->inp_options)
724 (void)m_free(inp->inp_options);
725 if (rt) {
726 /*
727 * route deletion requires reference count to be <= zero
728 */
729 lck_mtx_lock(rt_mtx);
730 if ((rt->rt_flags & RTF_DELCLONE) &&
731 (rt->rt_flags & RTF_WASCLONED) &&
732 (rt->rt_refcnt <= 1)) {
733 rtunref(rt);
734 rt->rt_flags &= ~RTF_UP;
735 rtrequest_locked(RTM_DELETE, rt_key(rt),
736 rt->rt_gateway, rt_mask(rt),
737 rt->rt_flags, (struct rtentry **)0);
738 }
739 else {
740 rtfree_locked(rt);
741 inp->inp_route.ro_rt = 0;
742 }
743 lck_mtx_unlock(rt_mtx);
744 }
745 ip_freemoptions(inp->inp_moptions);
746 inp->inp_moptions = NULL;
747 sofreelastref(so, 0);
748 inp->inp_state = INPCB_STATE_DEAD;
749 so->so_flags |= SOF_PCBCLEARING; /* makes sure we're not called twice from so_close */
750 }
751 }
752
753
754 void
755 in_pcbdispose(inp)
756 struct inpcb *inp;
757 {
758 struct socket *so = inp->inp_socket;
759 struct inpcbinfo *ipi = inp->inp_pcbinfo;
760
761 #if TEMPDEBUG
762 if (inp->inp_state != INPCB_STATE_DEAD) {
763 printf("in_pcbdispose: not dead yet? so=%x\n", so);
764 }
765 #endif
766
767 if (so && so->so_usecount != 0)
768 panic("in_pcbdispose: use count=%x so=%x\n", so->so_usecount, so);
769
770
771 inp->inp_gencnt = ++ipi->ipi_gencnt;
772 /*### access ipi in in_pcbremlists */
773 in_pcbremlists(inp);
774
775 if (so) {
776 if (so->so_proto->pr_flags & PR_PCBLOCK) {
777 sofreelastref(so, 0);
778 if (so->so_rcv.sb_cc || so->so_snd.sb_cc) {
779 #if TEMPDEBUG
780 printf("in_pcbdispose sb not cleaned up so=%x rc_cci=%x snd_cc=%x\n",
781 so, so->so_rcv.sb_cc, so->so_snd.sb_cc);
782 #endif
783 sbrelease(&so->so_rcv);
784 sbrelease(&so->so_snd);
785 }
786 if (so->so_head != NULL)
787 panic("in_pcbdispose, so=%x head still exist\n", so);
788 lck_mtx_unlock(inp->inpcb_mtx);
789 lck_mtx_free(inp->inpcb_mtx, ipi->mtx_grp);
790 }
791 so->so_flags |= SOF_PCBCLEARING; /* makes sure we're not called twice from so_close */
792 so->so_saved_pcb = (caddr_t) inp;
793 so->so_pcb = 0;
794 inp->inp_socket = 0;
795 inp->reserved[0] = so;
796 if (so->cached_in_sock_layer == 0) {
797 zfree(ipi->ipi_zone, inp);
798 }
799 sodealloc(so);
800 }
801 #if TEMPDEBUG
802 else
803 printf("in_pcbdispose: no socket for inp=%x\n", inp);
804 #endif
805 }
806
807 /*
808 * The calling convention of in_setsockaddr() and in_setpeeraddr() was
809 * modified to match the pru_sockaddr() and pru_peeraddr() entry points
810 * in struct pr_usrreqs, so that protocols can just reference then directly
811 * without the need for a wrapper function. The socket must have a valid
812 * (i.e., non-nil) PCB, but it should be impossible to get an invalid one
813 * except through a kernel programming error, so it is acceptable to panic
814 * (or in this case trap) if the PCB is invalid. (Actually, we don't trap
815 * because there actually /is/ a programming error somewhere... XXX)
816 */
817 int
818 in_setsockaddr(so, nam)
819 struct socket *so;
820 struct sockaddr **nam;
821 {
822 register struct inpcb *inp;
823 register struct sockaddr_in *sin;
824
825 /*
826 * Do the malloc first in case it blocks.
827 */
828 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK);
829 if (sin == NULL)
830 return ENOBUFS;
831 bzero(sin, sizeof *sin);
832 sin->sin_family = AF_INET;
833 sin->sin_len = sizeof(*sin);
834
835 inp = sotoinpcb(so);
836 if (!inp) {
837 FREE(sin, M_SONAME);
838 return ECONNRESET;
839 }
840 sin->sin_port = inp->inp_lport;
841 sin->sin_addr = inp->inp_laddr;
842
843 *nam = (struct sockaddr *)sin;
844 return 0;
845 }
846
847 int
848 in_setpeeraddr(so, nam)
849 struct socket *so;
850 struct sockaddr **nam;
851 {
852 struct inpcb *inp;
853 register struct sockaddr_in *sin;
854
855 /*
856 * Do the malloc first in case it blocks.
857 */
858 MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, M_WAITOK);
859 if (sin == NULL)
860 return ENOBUFS;
861 bzero((caddr_t)sin, sizeof (*sin));
862 sin->sin_family = AF_INET;
863 sin->sin_len = sizeof(*sin);
864
865 inp = sotoinpcb(so);
866 if (!inp) {
867 FREE(sin, M_SONAME);
868 return ECONNRESET;
869 }
870 sin->sin_port = inp->inp_fport;
871 sin->sin_addr = inp->inp_faddr;
872
873 *nam = (struct sockaddr *)sin;
874 return 0;
875 }
876
877 void
878 in_pcbnotifyall(pcbinfo, faddr, errno, notify)
879 struct inpcbinfo *pcbinfo;
880 struct in_addr faddr;
881 void (*notify) (struct inpcb *, int);
882 {
883 struct inpcb *inp;
884
885 lck_rw_lock_shared(pcbinfo->mtx);
886
887 LIST_FOREACH(inp, pcbinfo->listhead, inp_list) {
888 #if INET6
889 if ((inp->inp_vflag & INP_IPV4) == 0)
890 continue;
891 #endif
892 if (inp->inp_faddr.s_addr != faddr.s_addr ||
893 inp->inp_socket == NULL)
894 continue;
895 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING)
896 continue;
897 socket_lock(inp->inp_socket, 1);
898 (*notify)(inp, errno);
899 (void)in_pcb_checkstate(inp, WNT_RELEASE, 1);
900 socket_unlock(inp->inp_socket, 1);
901 }
902 lck_rw_done(pcbinfo->mtx);
903 }
904
905 void
906 in_pcbpurgeif0(
907 struct inpcb *head,
908 struct ifnet *ifp)
909 {
910 struct inpcb *inp;
911 struct ip_moptions *imo;
912 int i, gap;
913
914 for (inp = head; inp != NULL; inp = LIST_NEXT(inp, inp_list)) {
915 imo = inp->inp_moptions;
916 if ((inp->inp_vflag & INP_IPV4) &&
917 imo != NULL) {
918 /*
919 * Unselect the outgoing interface if it is being
920 * detached.
921 */
922 if (imo->imo_multicast_ifp == ifp)
923 imo->imo_multicast_ifp = NULL;
924
925 /*
926 * Drop multicast group membership if we joined
927 * through the interface being detached.
928 */
929 for (i = 0, gap = 0; i < imo->imo_num_memberships;
930 i++) {
931 if (imo->imo_membership[i]->inm_ifp == ifp) {
932 in_delmulti(&imo->imo_membership[i]);
933 gap++;
934 } else if (gap != 0)
935 imo->imo_membership[i - gap] =
936 imo->imo_membership[i];
937 }
938 imo->imo_num_memberships -= gap;
939 }
940 }
941 }
942
943 /*
944 * Check for alternatives when higher level complains
945 * about service problems. For now, invalidate cached
946 * routing information. If the route was created dynamically
947 * (by a redirect), time to try a default gateway again.
948 */
949 void
950 in_losing(inp)
951 struct inpcb *inp;
952 {
953 register struct rtentry *rt;
954 struct rt_addrinfo info;
955
956 if ((rt = inp->inp_route.ro_rt)) {
957 lck_mtx_lock(rt_mtx);
958 bzero((caddr_t)&info, sizeof(info));
959 info.rti_info[RTAX_DST] =
960 (struct sockaddr *)&inp->inp_route.ro_dst;
961 info.rti_info[RTAX_GATEWAY] = rt->rt_gateway;
962 info.rti_info[RTAX_NETMASK] = rt_mask(rt);
963 rt_missmsg(RTM_LOSING, &info, rt->rt_flags, 0);
964 if (rt->rt_flags & RTF_DYNAMIC)
965 (void) rtrequest_locked(RTM_DELETE, rt_key(rt),
966 rt->rt_gateway, rt_mask(rt), rt->rt_flags,
967 (struct rtentry **)0);
968 inp->inp_route.ro_rt = 0;
969 rtfree_locked(rt);
970 lck_mtx_unlock(rt_mtx);
971 /*
972 * A new route can be allocated
973 * the next time output is attempted.
974 */
975 }
976 }
977
978 /*
979 * After a routing change, flush old routing
980 * and allocate a (hopefully) better one.
981 */
982 void
983 in_rtchange(inp, errno)
984 register struct inpcb *inp;
985 int errno;
986 {
987 if (inp->inp_route.ro_rt) {
988 if ((ifa_foraddr(inp->inp_laddr.s_addr)) == 0)
989 return; /* we can't remove the route now. not sure if still ok to use src */
990 rtfree(inp->inp_route.ro_rt);
991 inp->inp_route.ro_rt = 0;
992 /*
993 * A new route can be allocated the next time
994 * output is attempted.
995 */
996 }
997 }
998
999 /*
1000 * Lookup a PCB based on the local address and port.
1001 */
1002 struct inpcb *
1003 in_pcblookup_local(pcbinfo, laddr, lport_arg, wild_okay)
1004 struct inpcbinfo *pcbinfo;
1005 struct in_addr laddr;
1006 u_int lport_arg;
1007 int wild_okay;
1008 {
1009 register struct inpcb *inp;
1010 int matchwild = 3, wildcard;
1011 u_short lport = lport_arg;
1012
1013 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP | DBG_FUNC_START, 0,0,0,0,0);
1014
1015 if (!wild_okay) {
1016 struct inpcbhead *head;
1017 /*
1018 * Look for an unconnected (wildcard foreign addr) PCB that
1019 * matches the local address and port we're looking for.
1020 */
1021 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1022 LIST_FOREACH(inp, head, inp_hash) {
1023 #if INET6
1024 if ((inp->inp_vflag & INP_IPV4) == 0)
1025 continue;
1026 #endif
1027 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1028 inp->inp_laddr.s_addr == laddr.s_addr &&
1029 inp->inp_lport == lport) {
1030 /*
1031 * Found.
1032 */
1033 return (inp);
1034 }
1035 }
1036 /*
1037 * Not found.
1038 */
1039 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP | DBG_FUNC_END, 0,0,0,0,0);
1040 return (NULL);
1041 } else {
1042 struct inpcbporthead *porthash;
1043 struct inpcbport *phd;
1044 struct inpcb *match = NULL;
1045 /*
1046 * Best fit PCB lookup.
1047 *
1048 * First see if this local port is in use by looking on the
1049 * port hash list.
1050 */
1051 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
1052 pcbinfo->porthashmask)];
1053 LIST_FOREACH(phd, porthash, phd_hash) {
1054 if (phd->phd_port == lport)
1055 break;
1056 }
1057 if (phd != NULL) {
1058 /*
1059 * Port is in use by one or more PCBs. Look for best
1060 * fit.
1061 */
1062 LIST_FOREACH(inp, &phd->phd_pcblist, inp_portlist) {
1063 wildcard = 0;
1064 #if INET6
1065 if ((inp->inp_vflag & INP_IPV4) == 0)
1066 continue;
1067 #endif
1068 if (inp->inp_faddr.s_addr != INADDR_ANY)
1069 wildcard++;
1070 if (inp->inp_laddr.s_addr != INADDR_ANY) {
1071 if (laddr.s_addr == INADDR_ANY)
1072 wildcard++;
1073 else if (inp->inp_laddr.s_addr != laddr.s_addr)
1074 continue;
1075 } else {
1076 if (laddr.s_addr != INADDR_ANY)
1077 wildcard++;
1078 }
1079 if (wildcard < matchwild) {
1080 match = inp;
1081 matchwild = wildcard;
1082 if (matchwild == 0) {
1083 break;
1084 }
1085 }
1086 }
1087 }
1088 KERNEL_DEBUG(DBG_FNC_PCB_LOOKUP | DBG_FUNC_END, match,0,0,0,0);
1089 return (match);
1090 }
1091 }
1092
1093 /*
1094 * Lookup PCB in hash list.
1095 */
1096 struct inpcb *
1097 in_pcblookup_hash(
1098 struct inpcbinfo *pcbinfo,
1099 struct in_addr faddr,
1100 u_int fport_arg,
1101 struct in_addr laddr,
1102 u_int lport_arg,
1103 int wildcard,
1104 struct ifnet *ifp)
1105 {
1106 struct inpcbhead *head;
1107 register struct inpcb *inp;
1108 u_short fport = fport_arg, lport = lport_arg;
1109
1110 /*
1111 * We may have found the pcb in the last lookup - check this first.
1112 */
1113
1114 lck_rw_lock_shared(pcbinfo->mtx);
1115
1116 /*
1117 * First look for an exact match.
1118 */
1119 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1120 LIST_FOREACH(inp, head, inp_hash) {
1121 #if INET6
1122 if ((inp->inp_vflag & INP_IPV4) == 0)
1123 continue;
1124 #endif
1125 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1126 inp->inp_laddr.s_addr == laddr.s_addr &&
1127 inp->inp_fport == fport &&
1128 inp->inp_lport == lport) {
1129 /*
1130 * Found.
1131 */
1132 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1133 lck_rw_done(pcbinfo->mtx);
1134 return (inp);
1135 }
1136 else { /* it's there but dead, say it isn't found */
1137 lck_rw_done(pcbinfo->mtx);
1138 return(NULL);
1139 }
1140 }
1141 }
1142 if (wildcard) {
1143 struct inpcb *local_wild = NULL;
1144 #if INET6
1145 struct inpcb *local_wild_mapped = NULL;
1146 #endif
1147
1148 head = &pcbinfo->hashbase[INP_PCBHASH(INADDR_ANY, lport, 0, pcbinfo->hashmask)];
1149 LIST_FOREACH(inp, head, inp_hash) {
1150 #if INET6
1151 if ((inp->inp_vflag & INP_IPV4) == 0)
1152 continue;
1153 #endif
1154 if (inp->inp_faddr.s_addr == INADDR_ANY &&
1155 inp->inp_lport == lport) {
1156 #if defined(NFAITH) && NFAITH > 0
1157 if (ifp && ifp->if_type == IFT_FAITH &&
1158 (inp->inp_flags & INP_FAITH) == 0)
1159 continue;
1160 #endif
1161 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1162 if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1163 lck_rw_done(pcbinfo->mtx);
1164 return (inp);
1165 }
1166 else { /* it's there but dead, say it isn't found */
1167 lck_rw_done(pcbinfo->mtx);
1168 return(NULL);
1169 }
1170 }
1171 else if (inp->inp_laddr.s_addr == INADDR_ANY) {
1172 #if defined(INET6)
1173 if (INP_CHECK_SOCKAF(inp->inp_socket,
1174 AF_INET6))
1175 local_wild_mapped = inp;
1176 else
1177 #endif /* defined(INET6) */
1178 local_wild = inp;
1179 }
1180 }
1181 }
1182 #if defined(INET6)
1183 if (local_wild == NULL) {
1184 if (local_wild_mapped != NULL) {
1185 if (in_pcb_checkstate(local_wild_mapped, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1186 lck_rw_done(pcbinfo->mtx);
1187 return (local_wild_mapped);
1188 }
1189 else { /* it's there but dead, say it isn't found */
1190 lck_rw_done(pcbinfo->mtx);
1191 return(NULL);
1192 }
1193 }
1194 lck_rw_done(pcbinfo->mtx);
1195 return (NULL);
1196 }
1197 #endif /* defined(INET6) */
1198 if (in_pcb_checkstate(local_wild, WNT_ACQUIRE, 0) != WNT_STOPUSING) {
1199 lck_rw_done(pcbinfo->mtx);
1200 return (local_wild);
1201 }
1202 else { /* it's there but dead, say it isn't found */
1203 lck_rw_done(pcbinfo->mtx);
1204 return(NULL);
1205 }
1206 }
1207
1208 /*
1209 * Not found.
1210 */
1211 lck_rw_done(pcbinfo->mtx);
1212 return (NULL);
1213 }
1214
1215 /*
1216 * Insert PCB onto various hash lists.
1217 */
1218 int
1219 in_pcbinshash(inp, locked)
1220 struct inpcb *inp;
1221 int locked; /* list already locked exclusive */
1222 {
1223 struct inpcbhead *pcbhash;
1224 struct inpcbporthead *pcbporthash;
1225 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1226 struct inpcbport *phd;
1227 u_int32_t hashkey_faddr;
1228
1229 #if INET6
1230 if (inp->inp_vflag & INP_IPV6)
1231 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1232 else
1233 #endif /* INET6 */
1234 hashkey_faddr = inp->inp_faddr.s_addr;
1235
1236 inp->hash_element = INP_PCBHASH(hashkey_faddr, inp->inp_lport, inp->inp_fport, pcbinfo->hashmask);
1237
1238 if (!locked) {
1239 if (!lck_rw_try_lock_exclusive(pcbinfo->mtx)) {
1240 /*lock inversion issue, mostly with udp multicast packets */
1241 socket_unlock(inp->inp_socket, 0);
1242 lck_rw_lock_exclusive(pcbinfo->mtx);
1243 socket_lock(inp->inp_socket, 0);
1244 }
1245 }
1246
1247 pcbhash = &pcbinfo->hashbase[inp->hash_element];
1248
1249 pcbporthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(inp->inp_lport,
1250 pcbinfo->porthashmask)];
1251
1252 /*
1253 * Go through port list and look for a head for this lport.
1254 */
1255 LIST_FOREACH(phd, pcbporthash, phd_hash) {
1256 if (phd->phd_port == inp->inp_lport)
1257 break;
1258 }
1259 /*
1260 * If none exists, malloc one and tack it on.
1261 */
1262 if (phd == NULL) {
1263 MALLOC(phd, struct inpcbport *, sizeof(struct inpcbport), M_PCB, M_WAITOK);
1264 if (phd == NULL) {
1265 if (!locked)
1266 lck_rw_done(pcbinfo->mtx);
1267 return (ENOBUFS); /* XXX */
1268 }
1269 phd->phd_port = inp->inp_lport;
1270 LIST_INIT(&phd->phd_pcblist);
1271 LIST_INSERT_HEAD(pcbporthash, phd, phd_hash);
1272 }
1273 inp->inp_phd = phd;
1274 LIST_INSERT_HEAD(&phd->phd_pcblist, inp, inp_portlist);
1275 LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
1276 if (!locked)
1277 lck_rw_done(pcbinfo->mtx);
1278 return (0);
1279 }
1280
1281 /*
1282 * Move PCB to the proper hash bucket when { faddr, fport } have been
1283 * changed. NOTE: This does not handle the case of the lport changing (the
1284 * hashed port list would have to be updated as well), so the lport must
1285 * not change after in_pcbinshash() has been called.
1286 */
1287 void
1288 in_pcbrehash(inp)
1289 struct inpcb *inp;
1290 {
1291 struct inpcbhead *head;
1292 u_int32_t hashkey_faddr;
1293
1294 #if INET6
1295 if (inp->inp_vflag & INP_IPV6)
1296 hashkey_faddr = inp->in6p_faddr.s6_addr32[3] /* XXX */;
1297 else
1298 #endif /* INET6 */
1299 hashkey_faddr = inp->inp_faddr.s_addr;
1300 inp->hash_element = INP_PCBHASH(hashkey_faddr, inp->inp_lport,
1301 inp->inp_fport, inp->inp_pcbinfo->hashmask);
1302 head = &inp->inp_pcbinfo->hashbase[inp->hash_element];
1303
1304 LIST_REMOVE(inp, inp_hash);
1305 LIST_INSERT_HEAD(head, inp, inp_hash);
1306 }
1307
1308 /*
1309 * Remove PCB from various lists.
1310 */
1311 //###LOCK must be called with list lock held
1312 void
1313 in_pcbremlists(inp)
1314 struct inpcb *inp;
1315 {
1316 inp->inp_gencnt = ++inp->inp_pcbinfo->ipi_gencnt;
1317
1318 if (inp->inp_lport) {
1319 struct inpcbport *phd = inp->inp_phd;
1320
1321 LIST_REMOVE(inp, inp_hash);
1322 LIST_REMOVE(inp, inp_portlist);
1323 if (phd != NULL && (LIST_FIRST(&phd->phd_pcblist) == NULL)) {
1324 LIST_REMOVE(phd, phd_hash);
1325 FREE(phd, M_PCB);
1326 }
1327 }
1328 LIST_REMOVE(inp, inp_list);
1329 inp->inp_pcbinfo->ipi_count--;
1330 }
1331
1332 static void in_pcb_detach_port( struct inpcb *inp);
1333 int
1334 in_pcb_grab_port (struct inpcbinfo *pcbinfo,
1335 u_short options,
1336 struct in_addr laddr,
1337 u_short *lport,
1338 struct in_addr faddr,
1339 u_short fport,
1340 u_int cookie,
1341 u_char owner_id)
1342 {
1343 struct inpcb *inp, *pcb;
1344 struct sockaddr_in sin;
1345 struct proc *p = current_proc();
1346 int stat;
1347
1348
1349 pcbinfo->nat_dummy_socket.so_pcb = 0;
1350 pcbinfo->nat_dummy_socket.so_options = 0;
1351 if (*lport) {
1352 /* The grabber wants a particular port */
1353
1354 if (faddr.s_addr || fport) {
1355 /*
1356 * This is either the second half of an active connect, or
1357 * it's from the acceptance of an incoming connection.
1358 */
1359 if (laddr.s_addr == 0) {
1360 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb;
1361 return EINVAL;
1362 }
1363
1364 inp = in_pcblookup_hash(pcbinfo, faddr, fport, laddr, *lport, 0, NULL);
1365 if (inp) {
1366 /* pcb was found, its count was upped. need to decrease it here */
1367 in_pcb_checkstate(inp, WNT_RELEASE, 0);
1368 if (!(IN_MULTICAST(ntohl(laddr.s_addr)))) {
1369 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb;
1370 return (EADDRINUSE);
1371 }
1372 }
1373
1374 stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
1375 if (stat) {
1376 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb;
1377 return stat;
1378 }
1379 pcb = sotoinpcb(&pcbinfo->nat_dummy_socket);
1380 pcb->inp_vflag |= INP_IPV4;
1381
1382 pcb->inp_lport = *lport;
1383 pcb->inp_laddr.s_addr = laddr.s_addr;
1384
1385 pcb->inp_faddr = faddr;
1386 pcb->inp_fport = fport;
1387
1388 lck_rw_lock_exclusive(pcbinfo->mtx);
1389 in_pcbinshash(pcb, 1);
1390 lck_rw_done(pcbinfo->mtx);
1391 }
1392 else {
1393 /*
1394 * This is either a bind for a passive socket, or it's the
1395 * first part of bind-connect sequence (not likely since an
1396 * ephemeral port is usually used in this case). Or, it's
1397 * the result of a connection acceptance when the foreign
1398 * address/port cannot be provided (which requires the SO_REUSEADDR
1399 * flag if laddr is not multicast).
1400 */
1401
1402 stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
1403 if (stat) {
1404 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb;
1405 return stat;
1406 }
1407 pcb = sotoinpcb(&pcbinfo->nat_dummy_socket);
1408 pcb->inp_vflag |= INP_IPV4;
1409
1410 pcbinfo->nat_dummy_socket.so_options = options;
1411 bzero(&sin, sizeof(struct sockaddr_in));
1412 sin.sin_len = sizeof(struct sockaddr_in);
1413 sin.sin_family = AF_INET;
1414 sin.sin_addr.s_addr = laddr.s_addr;
1415 sin.sin_port = *lport;
1416
1417 socket_lock(&pcbinfo->nat_dummy_socket, 1);
1418 stat = in_pcbbind((struct inpcb *) pcbinfo->nat_dummy_socket.so_pcb,
1419 (struct sockaddr *) &sin, p);
1420 if (stat) {
1421 socket_unlock(&pcbinfo->nat_dummy_socket, 1); /*detach first */
1422 in_pcb_detach_port(pcb); /* will restore dummy pcb */
1423 return stat;
1424 }
1425 socket_unlock(&pcbinfo->nat_dummy_socket, 1);
1426 }
1427 }
1428 else {
1429 /* The grabber wants an ephemeral port */
1430
1431 stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
1432 if (stat) {
1433 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb;
1434 return stat;
1435 }
1436 pcb = sotoinpcb(&pcbinfo->nat_dummy_socket);
1437 pcb->inp_vflag |= INP_IPV4;
1438
1439 bzero(&sin, sizeof(struct sockaddr_in));
1440 sin.sin_len = sizeof(struct sockaddr_in);
1441 sin.sin_family = AF_INET;
1442 sin.sin_addr.s_addr = laddr.s_addr;
1443 sin.sin_port = 0;
1444
1445 if (faddr.s_addr || fport) {
1446 /*
1447 * Not sure if this case will be used - could occur when connect
1448 * is called, skipping the bind.
1449 */
1450
1451 if (laddr.s_addr == 0) {
1452 in_pcb_detach_port(pcb); /* restores dummy pcb */
1453 return EINVAL;
1454 }
1455
1456 socket_lock(&pcbinfo->nat_dummy_socket, 1);
1457 stat = in_pcbbind((struct inpcb *) pcbinfo->nat_dummy_socket.so_pcb,
1458 (struct sockaddr *) &sin, p);
1459 if (stat) {
1460 socket_unlock(&pcbinfo->nat_dummy_socket, 1);
1461 in_pcb_detach_port(pcb); /* restores dummy pcb */
1462 return stat;
1463 }
1464
1465 socket_unlock(&pcbinfo->nat_dummy_socket, 1);
1466 inp = in_pcblookup_hash(pcbinfo, faddr, fport,
1467 pcb->inp_laddr, pcb->inp_lport, 0, NULL);
1468 if (inp) {
1469 /* pcb was found, its count was upped. need to decrease it here */
1470 in_pcb_checkstate(inp, WNT_RELEASE, 0);
1471 in_pcb_detach_port(pcb);
1472 return (EADDRINUSE);
1473 }
1474
1475 lck_rw_lock_exclusive(pcbinfo->mtx);
1476 pcb->inp_faddr = faddr;
1477 pcb->inp_fport = fport;
1478 in_pcbrehash(pcb);
1479 lck_rw_done(pcbinfo->mtx);
1480 }
1481 else {
1482 /*
1483 * This is a simple bind of an ephemeral port. The local addr
1484 * may or may not be defined.
1485 */
1486
1487 socket_lock(&pcbinfo->nat_dummy_socket, 1);
1488 stat = in_pcbbind((struct inpcb *) pcbinfo->nat_dummy_socket.so_pcb,
1489 (struct sockaddr *) &sin, p);
1490 if (stat) {
1491 socket_unlock(&pcbinfo->nat_dummy_socket, 1);
1492 in_pcb_detach_port(pcb);
1493 return stat;
1494 }
1495 socket_unlock(&pcbinfo->nat_dummy_socket, 1);
1496 }
1497 *lport = pcb->inp_lport;
1498 }
1499
1500
1501 pcb->nat_owner = owner_id;
1502 pcb->nat_cookie = cookie;
1503 pcb->inp_ppcb = (caddr_t) pcbinfo->dummy_cb;
1504 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb; /* restores dummypcb */
1505 return 0;
1506 }
1507
1508 /* 3962035 - in_pcb_letgo_port needs a special case function for detaching */
1509 static void
1510 in_pcb_detach_port(
1511 struct inpcb *inp)
1512 {
1513 struct socket *so = inp->inp_socket;
1514 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
1515
1516 if (so != &pcbinfo->nat_dummy_socket)
1517 panic("in_pcb_detach_port: not a dummy_sock: so=%x, inp=%x\n", so, inp);
1518 inp->inp_gencnt = ++pcbinfo->ipi_gencnt;
1519 /*### access ipi in in_pcbremlists */
1520 in_pcbremlists(inp);
1521
1522 inp->inp_socket = 0;
1523 inp->reserved[0] = so;
1524 zfree(pcbinfo->ipi_zone, inp);
1525 pcbinfo->nat_dummy_socket.so_pcb = (caddr_t)pcbinfo->nat_dummy_pcb; /* restores dummypcb */
1526 }
1527
1528 int
1529 in_pcb_letgo_port(struct inpcbinfo *pcbinfo, struct in_addr laddr, u_short lport,
1530 struct in_addr faddr, u_short fport, u_char owner_id)
1531 {
1532 struct inpcbhead *head;
1533 register struct inpcb *inp;
1534
1535
1536 /*
1537 * First look for an exact match.
1538 */
1539
1540 lck_rw_lock_exclusive(pcbinfo->mtx);
1541 head = &pcbinfo->hashbase[INP_PCBHASH(faddr.s_addr, lport, fport, pcbinfo->hashmask)];
1542 for (inp = head->lh_first; inp != NULL; inp = inp->inp_hash.le_next) {
1543 if (inp->inp_faddr.s_addr == faddr.s_addr &&
1544 inp->inp_laddr.s_addr == laddr.s_addr &&
1545 inp->inp_fport == fport &&
1546 inp->inp_lport == lport &&
1547 inp->nat_owner == owner_id) {
1548 /*
1549 * Found.
1550 */
1551 in_pcb_detach_port(inp);
1552 lck_rw_done(pcbinfo->mtx);
1553 return 0;
1554 }
1555 }
1556
1557 lck_rw_done(pcbinfo->mtx);
1558 return ENOENT;
1559 }
1560
1561 u_char
1562 in_pcb_get_owner(struct inpcbinfo *pcbinfo,
1563 struct in_addr laddr, u_short lport,
1564 struct in_addr faddr, u_short fport,
1565 u_int *cookie)
1566
1567 {
1568 struct inpcb *inp;
1569 u_char owner_id = INPCB_NO_OWNER;
1570 struct inpcbport *phd;
1571 struct inpcbporthead *porthash;
1572
1573
1574 if (IN_MULTICAST(laddr.s_addr)) {
1575 /*
1576 * Walk through PCB's looking for registered
1577 * owners.
1578 */
1579
1580 lck_rw_lock_shared(pcbinfo->mtx);
1581 porthash = &pcbinfo->porthashbase[INP_PCBPORTHASH(lport,
1582 pcbinfo->porthashmask)];
1583 for (phd = porthash->lh_first; phd != NULL; phd = phd->phd_hash.le_next) {
1584 if (phd->phd_port == lport)
1585 break;
1586 }
1587
1588 if (phd == 0) {
1589 lck_rw_done(pcbinfo->mtx);
1590 return INPCB_NO_OWNER;
1591 }
1592
1593 owner_id = INPCB_NO_OWNER;
1594 for (inp = phd->phd_pcblist.lh_first; inp != NULL;
1595 inp = inp->inp_portlist.le_next) {
1596
1597 if (inp->inp_laddr.s_addr == laddr.s_addr) {
1598 if (inp->nat_owner == 0)
1599 owner_id |= INPCB_OWNED_BY_X;
1600 else
1601 owner_id |= inp->nat_owner;
1602 }
1603 }
1604
1605 lck_rw_done(pcbinfo->mtx);
1606 return owner_id;
1607 }
1608 else {
1609 inp = in_pcblookup_hash(pcbinfo, faddr, fport,
1610 laddr, lport, 1, NULL);
1611 if (inp) {
1612 /* pcb was found, its count was upped. need to decrease it here */
1613 /* if we found it, that pcb is already locked by the caller */
1614 if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING)
1615 return(INPCB_NO_OWNER);
1616
1617 if (inp->nat_owner) {
1618 owner_id = inp->nat_owner;
1619 *cookie = inp->nat_cookie;
1620 }
1621 else {
1622 owner_id = INPCB_OWNED_BY_X;
1623 }
1624 }
1625 else
1626 owner_id = INPCB_NO_OWNER;
1627
1628 return owner_id;
1629 }
1630 }
1631
1632 int
1633 in_pcb_new_share_client(struct inpcbinfo *pcbinfo, u_char *owner_id)
1634 {
1635
1636 int i;
1637
1638
1639 for (i=0; i < INPCB_MAX_IDS; i++) {
1640 if ((pcbinfo->all_owners & (1 << i)) == 0) {
1641 pcbinfo->all_owners |= (1 << i);
1642 *owner_id = (1 << i);
1643 return 0;
1644 }
1645 }
1646
1647 return ENOSPC;
1648 }
1649
1650 int
1651 in_pcb_rem_share_client(struct inpcbinfo *pcbinfo, u_char owner_id)
1652 {
1653 struct inpcb *inp;
1654
1655
1656 lck_rw_lock_exclusive(pcbinfo->mtx);
1657 if (pcbinfo->all_owners & owner_id) {
1658 pcbinfo->all_owners &= ~owner_id;
1659 for (inp = pcbinfo->listhead->lh_first; inp != NULL; inp = inp->inp_list.le_next) {
1660 if (inp->nat_owner & owner_id) {
1661 if (inp->nat_owner == owner_id)
1662 /*
1663 * Deallocate the pcb
1664 */
1665 in_pcb_detach_port(inp);
1666 else
1667 inp->nat_owner &= ~owner_id;
1668 }
1669 }
1670 }
1671 else {
1672 lck_rw_done(pcbinfo->mtx);
1673 return ENOENT;
1674 }
1675
1676 lck_rw_done(pcbinfo->mtx);
1677 return 0;
1678 }
1679
1680
1681
1682 void in_pcb_nat_init(struct inpcbinfo *pcbinfo, int afamily,
1683 int pfamily, int protocol)
1684 {
1685 int stat;
1686 struct proc *p = current_proc();
1687
1688 bzero(&pcbinfo->nat_dummy_socket, sizeof(struct socket));
1689 pcbinfo->nat_dummy_socket.so_proto = pffindproto_locked(afamily, pfamily, protocol);
1690 pcbinfo->all_owners = 0;
1691 stat = in_pcballoc(&pcbinfo->nat_dummy_socket, pcbinfo, p);
1692 if (stat)
1693 panic("in_pcb_nat_init: can't alloc fakepcb err=%\n", stat);
1694 pcbinfo->nat_dummy_pcb = pcbinfo->nat_dummy_socket.so_pcb;
1695 }
1696
1697 /* Mechanism used to defer the memory release of PCBs
1698 * The pcb list will contain the pcb until the ripper can clean it up if
1699 * the following conditions are met: 1) state "DEAD", 2) wantcnt is STOPUSING
1700 * 3) usecount is null
1701 * This function will be called to either mark the pcb as
1702 */
1703 int
1704 in_pcb_checkstate(struct inpcb *pcb, int mode, int locked)
1705
1706 {
1707
1708 volatile UInt32 *wantcnt = (volatile UInt32 *)&pcb->inp_wantcnt;
1709 UInt32 origwant;
1710 UInt32 newwant;
1711
1712 switch (mode) {
1713
1714 case WNT_STOPUSING: /* try to mark the pcb as ready for recycling */
1715
1716 /* compareswap with STOPUSING, if success we're good, if it's in use, will be marked later */
1717
1718 if (locked == 0)
1719 socket_lock(pcb->inp_socket, 1);
1720 pcb->inp_state = INPCB_STATE_DEAD;
1721 stopusing:
1722 if (pcb->inp_socket->so_usecount < 0)
1723 panic("in_pcb_checkstate STOP pcb=%x so=%x usecount is negative\n", pcb, pcb->inp_socket);
1724 if (locked == 0)
1725 socket_unlock(pcb->inp_socket, 1);
1726
1727 origwant = *wantcnt;
1728 if ((UInt16) origwant == 0xffff ) /* should stop using */
1729 return (WNT_STOPUSING);
1730 newwant = 0xffff;
1731 if ((UInt16) origwant == 0) {/* try to mark it as unsuable now */
1732 OSCompareAndSwap(origwant, newwant, (UInt32 *) wantcnt) ;
1733 }
1734 return (WNT_STOPUSING);
1735 break;
1736
1737 case WNT_ACQUIRE: /* try to increase reference to pcb */
1738 /* if WNT_STOPUSING should bail out */
1739 /*
1740 * if socket state DEAD, try to set count to STOPUSING, return failed
1741 * otherwise increase cnt
1742 */
1743 do {
1744 origwant = *wantcnt;
1745 if ((UInt16) origwant == 0xffff ) {/* should stop using */
1746 // printf("in_pcb_checkstate: ACQ PCB was STOPUSING while release. odd pcb=%x\n", pcb);
1747 return (WNT_STOPUSING);
1748 }
1749 newwant = origwant + 1;
1750 } while (!OSCompareAndSwap(origwant, newwant, (UInt32 *) wantcnt));
1751 return (WNT_ACQUIRE);
1752 break;
1753
1754 case WNT_RELEASE: /* release reference. if result is null and pcb state is DEAD,
1755 set wanted bit to STOPUSING
1756 */
1757
1758 if (locked == 0)
1759 socket_lock(pcb->inp_socket, 1);
1760
1761 do {
1762 origwant = *wantcnt;
1763 if ((UInt16) origwant == 0x0 )
1764 panic("in_pcb_checkstate pcb=%x release with zero count", pcb);
1765 if ((UInt16) origwant == 0xffff ) {/* should stop using */
1766 #if TEMPDEBUG
1767 printf("in_pcb_checkstate: REL PCB was STOPUSING while release. odd pcb=%x\n", pcb);
1768 #endif
1769 if (locked == 0)
1770 socket_unlock(pcb->inp_socket, 1);
1771 return (WNT_STOPUSING);
1772 }
1773 newwant = origwant - 1;
1774 } while (!OSCompareAndSwap(origwant, newwant, (UInt32 *) wantcnt));
1775
1776 if (pcb->inp_state == INPCB_STATE_DEAD)
1777 goto stopusing;
1778 if (pcb->inp_socket->so_usecount < 0)
1779 panic("in_pcb_checkstate RELEASE pcb=%x so=%x usecount is negative\n", pcb, pcb->inp_socket);
1780
1781 if (locked == 0)
1782 socket_unlock(pcb->inp_socket, 1);
1783 return (WNT_RELEASE);
1784 break;
1785
1786 default:
1787
1788 panic("in_pcb_checkstate: so=%x not a valid state =%x\n", pcb->inp_socket, mode);
1789 }
1790
1791 /* NOTREACHED */
1792 return (mode);
1793 }
1794
1795 /*
1796 * inpcb_to_compat copies specific bits of an inpcb to a inpcb_compat.
1797 * The inpcb_compat data structure is passed to user space and must
1798 * not change. We intentionally avoid copying pointers. The socket is
1799 * the one exception, though we probably shouldn't copy that either.
1800 */
1801 void
1802 inpcb_to_compat(
1803 struct inpcb *inp,
1804 struct inpcb_compat *inp_compat)
1805 {
1806 bzero(inp_compat, sizeof(*inp_compat));
1807 inp_compat->inp_fport = inp->inp_fport;
1808 inp_compat->inp_lport = inp->inp_lport;
1809 inp_compat->inp_socket = inp->inp_socket;
1810 inp_compat->nat_owner = inp->nat_owner;
1811 inp_compat->nat_cookie = inp->nat_cookie;
1812 inp_compat->inp_gencnt = inp->inp_gencnt;
1813 inp_compat->inp_flags = inp->inp_flags;
1814 inp_compat->inp_flow = inp->inp_flow;
1815 inp_compat->inp_vflag = inp->inp_vflag;
1816 inp_compat->inp_ip_ttl = inp->inp_ip_ttl;
1817 inp_compat->inp_ip_p = inp->inp_ip_p;
1818 inp_compat->inp_dependfaddr.inp6_foreign = inp->inp_dependfaddr.inp6_foreign;
1819 inp_compat->inp_dependladdr.inp6_local = inp->inp_dependladdr.inp6_local;
1820 inp_compat->inp_depend4.inp4_ip_tos = inp->inp_depend4.inp4_ip_tos;
1821 inp_compat->inp_depend6.inp6_hlim = inp->inp_depend6.inp6_hlim;
1822 inp_compat->inp_depend6.inp6_cksum = inp->inp_depend6.inp6_cksum;
1823 inp_compat->inp6_ifindex = inp->inp6_ifindex;
1824 inp_compat->inp_depend6.inp6_hops = inp->inp_depend6.inp6_hops;
1825 }
1826
1827 #ifndef __APPLE__
1828 prison_xinpcb(struct proc *p, struct inpcb *inp)
1829 {
1830 if (!p->p_prison)
1831 return (0);
1832 if (ntohl(inp->inp_laddr.s_addr) == p->p_prison->pr_ip)
1833 return (0);
1834 return (1);
1835 }
1836 #endif