]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
d194a867feb498ce3a8b1d2ccb54ace82b73b556
[apple/xnu.git] / bsd / netinet / tcp_usrreq.c
1 /*
2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (c) 1982, 1986, 1988, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
61 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.51.2.9 2001/08/22 00:59:12 silby Exp $
62 */
63
64
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/sysctl.h>
69 #include <sys/mbuf.h>
70 #if INET6
71 #include <sys/domain.h>
72 #endif /* INET6 */
73 #include <sys/socket.h>
74 #include <sys/socketvar.h>
75 #include <sys/protosw.h>
76
77 #include <net/if.h>
78 #include <net/route.h>
79
80 #include <netinet/in.h>
81 #include <netinet/in_systm.h>
82 #if INET6
83 #include <netinet/ip6.h>
84 #endif
85 #include <netinet/in_pcb.h>
86 #if INET6
87 #include <netinet6/in6_pcb.h>
88 #endif
89 #include <netinet/in_var.h>
90 #include <netinet/ip_var.h>
91 #if INET6
92 #include <netinet6/ip6_var.h>
93 #endif
94 #include <netinet/tcp.h>
95 #include <netinet/tcp_fsm.h>
96 #include <netinet/tcp_seq.h>
97 #include <netinet/tcp_timer.h>
98 #include <netinet/tcp_var.h>
99 #include <netinet/tcpip.h>
100 #if TCPDEBUG
101 #include <netinet/tcp_debug.h>
102 #endif
103
104 #if IPSEC
105 #include <netinet6/ipsec.h>
106 #endif /*IPSEC*/
107
108 /*
109 * TCP protocol interface to socket abstraction.
110 */
111 extern char *tcpstates[]; /* XXX ??? */
112
113 static int tcp_attach(struct socket *, struct proc *);
114 static int tcp_connect(struct tcpcb *, struct sockaddr *, struct proc *);
115 #if INET6
116 static int tcp6_connect(struct tcpcb *, struct sockaddr *, struct proc *);
117 #endif /* INET6 */
118 static struct tcpcb *
119 tcp_disconnect(struct tcpcb *);
120 static struct tcpcb *
121 tcp_usrclosed(struct tcpcb *);
122
123 __private_extern__ int tcp_win_scale = 3;
124 SYSCTL_INT(_net_inet_tcp, OID_AUTO, win_scale_factor, CTLFLAG_RW,
125 &tcp_win_scale, 0, "Window scaling factor");
126
127 static u_int32_t tcps_in_sw_cksum;
128 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, in_sw_cksum, CTLFLAG_RD,
129 &tcps_in_sw_cksum, 0,
130 "Number of received packets checksummed in software");
131
132 static u_int64_t tcps_in_sw_cksum_bytes;
133 SYSCTL_QUAD(_net_inet_tcp, OID_AUTO, in_sw_cksum_bytes, CTLFLAG_RD,
134 &tcps_in_sw_cksum_bytes,
135 "Amount of received data checksummed in software");
136
137 static u_int32_t tcps_out_sw_cksum;
138 SYSCTL_UINT(_net_inet_tcp, OID_AUTO, out_sw_cksum, CTLFLAG_RD,
139 &tcps_out_sw_cksum, 0,
140 "Number of transmitted packets checksummed in software");
141
142 static u_int64_t tcps_out_sw_cksum_bytes;
143 SYSCTL_QUAD(_net_inet_tcp, OID_AUTO, out_sw_cksum_bytes, CTLFLAG_RD,
144 &tcps_out_sw_cksum_bytes,
145 "Amount of transmitted data checksummed in software");
146
147 #if TCPDEBUG
148 #define TCPDEBUG0 int ostate = 0
149 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
150 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
151 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
152 #else
153 #define TCPDEBUG0
154 #define TCPDEBUG1()
155 #define TCPDEBUG2(req)
156 #endif
157
158 #if CONFIG_USESOCKTHRESHOLD
159 __private_extern__ unsigned int tcp_sockthreshold = 64;
160 #else
161 __private_extern__ unsigned int tcp_sockthreshold = 0;
162 #endif
163 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sockthreshold, CTLFLAG_RW,
164 &tcp_sockthreshold , 0, "TCP Socket size increased if less than threshold");
165
166 /*
167 * TCP attaches to socket via pru_attach(), reserving space,
168 * and an internet control block.
169 *
170 * Returns: 0 Success
171 * EISCONN
172 * tcp_attach:ENOBUFS
173 * tcp_attach:ENOMEM
174 * tcp_attach:??? [IPSEC specific]
175 */
176 static int
177 tcp_usr_attach(struct socket *so, __unused int proto, struct proc *p)
178 {
179 int error;
180 struct inpcb *inp = sotoinpcb(so);
181 struct tcpcb *tp = 0;
182 TCPDEBUG0;
183
184 TCPDEBUG1();
185 if (inp) {
186 error = EISCONN;
187 goto out;
188 }
189
190 error = tcp_attach(so, p);
191 if (error)
192 goto out;
193
194 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
195 so->so_linger = TCP_LINGERTIME * hz;
196 tp = sototcpcb(so);
197 out:
198 TCPDEBUG2(PRU_ATTACH);
199 return error;
200 }
201
202 /*
203 * pru_detach() detaches the TCP protocol from the socket.
204 * If the protocol state is non-embryonic, then can't
205 * do this directly: have to initiate a pru_disconnect(),
206 * which may finish later; embryonic TCB's can just
207 * be discarded here.
208 */
209 static int
210 tcp_usr_detach(struct socket *so)
211 {
212 int error = 0;
213 struct inpcb *inp = sotoinpcb(so);
214 struct tcpcb *tp;
215 TCPDEBUG0;
216
217 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) {
218 return EINVAL; /* XXX */
219 }
220 #if 1
221 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
222 #endif
223 tp = intotcpcb(inp);
224 /* In case we got disconnected from the peer */
225 if (tp == 0)
226 goto out;
227 TCPDEBUG1();
228 tp = tcp_disconnect(tp);
229 out:
230 TCPDEBUG2(PRU_DETACH);
231 return error;
232 }
233
234 #define COMMON_START() TCPDEBUG0; \
235 do { \
236 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) { \
237 return EINVAL; \
238 } \
239 tp = intotcpcb(inp); \
240 TCPDEBUG1(); \
241 } while(0)
242
243 #define COMMON_END(req) out: TCPDEBUG2(req); return error; goto out
244
245
246 /*
247 * Give the socket an address.
248 *
249 * Returns: 0 Success
250 * EINVAL Invalid argument [COMMON_START]
251 * EAFNOSUPPORT Address family not supported
252 * in_pcbbind:EADDRNOTAVAIL Address not available.
253 * in_pcbbind:EINVAL Invalid argument
254 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
255 * in_pcbbind:EACCES Permission denied
256 * in_pcbbind:EADDRINUSE Address in use
257 * in_pcbbind:EAGAIN Resource unavailable, try again
258 * in_pcbbind:EPERM Operation not permitted
259 */
260 static int
261 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
262 {
263 int error = 0;
264 struct inpcb *inp = sotoinpcb(so);
265 struct tcpcb *tp;
266 struct sockaddr_in *sinp;
267
268 COMMON_START();
269
270 if (nam->sa_family != 0 && nam->sa_family != AF_INET) {
271 error = EAFNOSUPPORT;
272 goto out;
273 }
274
275 /*
276 * Must check for multicast addresses and disallow binding
277 * to them.
278 */
279 sinp = (struct sockaddr_in *)nam;
280 if (sinp->sin_family == AF_INET &&
281 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
282 error = EAFNOSUPPORT;
283 goto out;
284 }
285 error = in_pcbbind(inp, nam, p);
286 if (error)
287 goto out;
288 COMMON_END(PRU_BIND);
289
290 }
291
292 #if INET6
293 static int
294 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
295 {
296 int error = 0;
297 struct inpcb *inp = sotoinpcb(so);
298 struct tcpcb *tp;
299 struct sockaddr_in6 *sin6p;
300
301 COMMON_START();
302
303 if (nam->sa_family != 0 && nam->sa_family != AF_INET6) {
304 error = EAFNOSUPPORT;
305 goto out;
306 }
307
308 /*
309 * Must check for multicast addresses and disallow binding
310 * to them.
311 */
312 sin6p = (struct sockaddr_in6 *)nam;
313 if (sin6p->sin6_family == AF_INET6 &&
314 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
315 error = EAFNOSUPPORT;
316 goto out;
317 }
318 inp->inp_vflag &= ~INP_IPV4;
319 inp->inp_vflag |= INP_IPV6;
320 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
321 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
322 inp->inp_vflag |= INP_IPV4;
323 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
324 struct sockaddr_in sin;
325
326 in6_sin6_2_sin(&sin, sin6p);
327 inp->inp_vflag |= INP_IPV4;
328 inp->inp_vflag &= ~INP_IPV6;
329 error = in_pcbbind(inp, (struct sockaddr *)&sin, p);
330 goto out;
331 }
332 }
333 error = in6_pcbbind(inp, nam, p);
334 if (error)
335 goto out;
336 COMMON_END(PRU_BIND);
337 }
338 #endif /* INET6 */
339
340 /*
341 * Prepare to accept connections.
342 *
343 * Returns: 0 Success
344 * EINVAL [COMMON_START]
345 * in_pcbbind:EADDRNOTAVAIL Address not available.
346 * in_pcbbind:EINVAL Invalid argument
347 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
348 * in_pcbbind:EACCES Permission denied
349 * in_pcbbind:EADDRINUSE Address in use
350 * in_pcbbind:EAGAIN Resource unavailable, try again
351 * in_pcbbind:EPERM Operation not permitted
352 */
353 static int
354 tcp_usr_listen(struct socket *so, struct proc *p)
355 {
356 int error = 0;
357 struct inpcb *inp = sotoinpcb(so);
358 struct tcpcb *tp;
359
360 COMMON_START();
361 if (inp->inp_lport == 0)
362 error = in_pcbbind(inp, (struct sockaddr *)0, p);
363 if (error == 0)
364 tp->t_state = TCPS_LISTEN;
365 COMMON_END(PRU_LISTEN);
366 }
367
368 #if INET6
369 static int
370 tcp6_usr_listen(struct socket *so, struct proc *p)
371 {
372 int error = 0;
373 struct inpcb *inp = sotoinpcb(so);
374 struct tcpcb *tp;
375
376 COMMON_START();
377 if (inp->inp_lport == 0) {
378 inp->inp_vflag &= ~INP_IPV4;
379 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
380 inp->inp_vflag |= INP_IPV4;
381 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
382 }
383 if (error == 0)
384 tp->t_state = TCPS_LISTEN;
385 COMMON_END(PRU_LISTEN);
386 }
387 #endif /* INET6 */
388
389 /*
390 * Initiate connection to peer.
391 * Create a template for use in transmissions on this connection.
392 * Enter SYN_SENT state, and mark socket as connecting.
393 * Start keep-alive timer, and seed output sequence space.
394 * Send initial segment on connection.
395 */
396 static int
397 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
398 {
399 int error = 0;
400 struct inpcb *inp = sotoinpcb(so);
401 struct tcpcb *tp;
402 struct sockaddr_in *sinp;
403
404 TCPDEBUG0;
405 if (inp == 0)
406 return EINVAL;
407 else if (inp->inp_state == INPCB_STATE_DEAD) {
408 if (so->so_error) {
409 error = so->so_error;
410 so->so_error = 0;
411 return error;
412 } else
413 return EINVAL;
414 }
415 tp = intotcpcb(inp);
416 TCPDEBUG1();
417
418 if (nam->sa_family != 0 && nam->sa_family != AF_INET) {
419 error = EAFNOSUPPORT;
420 goto out;
421 }
422 /*
423 * Must disallow TCP ``connections'' to multicast addresses.
424 */
425 sinp = (struct sockaddr_in *)nam;
426 if (sinp->sin_family == AF_INET
427 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
428 error = EAFNOSUPPORT;
429 goto out;
430 }
431
432
433 if ((error = tcp_connect(tp, nam, p)) != 0)
434 goto out;
435 error = tcp_output(tp);
436 COMMON_END(PRU_CONNECT);
437 }
438
439 #if INET6
440 static int
441 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
442 {
443 int error = 0;
444 struct inpcb *inp = sotoinpcb(so);
445 struct tcpcb *tp;
446 struct sockaddr_in6 *sin6p;
447
448 COMMON_START();
449
450 if (nam->sa_family != 0 && nam->sa_family != AF_INET6) {
451 error = EAFNOSUPPORT;
452 goto out;
453 }
454
455 /*
456 * Must disallow TCP ``connections'' to multicast addresses.
457 */
458 sin6p = (struct sockaddr_in6 *)nam;
459 if (sin6p->sin6_family == AF_INET6
460 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
461 error = EAFNOSUPPORT;
462 goto out;
463 }
464
465 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
466 struct sockaddr_in sin;
467
468 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0)
469 return (EINVAL);
470
471 in6_sin6_2_sin(&sin, sin6p);
472 inp->inp_vflag |= INP_IPV4;
473 inp->inp_vflag &= ~INP_IPV6;
474 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, p)) != 0)
475 goto out;
476 error = tcp_output(tp);
477 goto out;
478 }
479 inp->inp_vflag &= ~INP_IPV4;
480 inp->inp_vflag |= INP_IPV6;
481 if ((error = tcp6_connect(tp, nam, p)) != 0)
482 goto out;
483 error = tcp_output(tp);
484 if (error)
485 goto out;
486 COMMON_END(PRU_CONNECT);
487 }
488 #endif /* INET6 */
489
490 /*
491 * Initiate disconnect from peer.
492 * If connection never passed embryonic stage, just drop;
493 * else if don't need to let data drain, then can just drop anyways,
494 * else have to begin TCP shutdown process: mark socket disconnecting,
495 * drain unread data, state switch to reflect user close, and
496 * send segment (e.g. FIN) to peer. Socket will be really disconnected
497 * when peer sends FIN and acks ours.
498 *
499 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
500 */
501 static int
502 tcp_usr_disconnect(struct socket *so)
503 {
504 int error = 0;
505 struct inpcb *inp = sotoinpcb(so);
506 struct tcpcb *tp;
507
508 #if 1
509 lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED);
510 #endif
511 COMMON_START();
512 /* In case we got disconnected from the peer */
513 if (tp == 0)
514 goto out;
515 tp = tcp_disconnect(tp);
516 COMMON_END(PRU_DISCONNECT);
517 }
518
519 /*
520 * Accept a connection. Essentially all the work is
521 * done at higher levels; just return the address
522 * of the peer, storing through addr.
523 */
524 static int
525 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
526 {
527 int error = 0;
528 struct inpcb *inp = sotoinpcb(so);
529 struct tcpcb *tp = NULL;
530 TCPDEBUG0;
531
532 if (so->so_state & SS_ISDISCONNECTED) {
533 error = ECONNABORTED;
534 goto out;
535 }
536 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) {
537 return (EINVAL);
538 }
539 tp = intotcpcb(inp);
540 TCPDEBUG1();
541 in_setpeeraddr(so, nam);
542 COMMON_END(PRU_ACCEPT);
543 }
544
545 #if INET6
546 static int
547 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
548 {
549 int error = 0;
550 struct inpcb *inp = sotoinpcb(so);
551 struct tcpcb *tp = NULL;
552 TCPDEBUG0;
553
554 if (so->so_state & SS_ISDISCONNECTED) {
555 error = ECONNABORTED;
556 goto out;
557 }
558 if (inp == 0 || (inp->inp_state == INPCB_STATE_DEAD)) {
559 return (EINVAL);
560 }
561 tp = intotcpcb(inp);
562 TCPDEBUG1();
563 in6_mapped_peeraddr(so, nam);
564 COMMON_END(PRU_ACCEPT);
565 }
566 #endif /* INET6 */
567
568 /*
569 * Mark the connection as being incapable of further output.
570 *
571 * Returns: 0 Success
572 * EINVAL [COMMON_START]
573 * tcp_output:EADDRNOTAVAIL
574 * tcp_output:ENOBUFS
575 * tcp_output:EMSGSIZE
576 * tcp_output:EHOSTUNREACH
577 * tcp_output:ENETUNREACH
578 * tcp_output:ENETDOWN
579 * tcp_output:ENOMEM
580 * tcp_output:EACCES
581 * tcp_output:EMSGSIZE
582 * tcp_output:ENOBUFS
583 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
584 */
585 static int
586 tcp_usr_shutdown(struct socket *so)
587 {
588 int error = 0;
589 struct inpcb *inp = sotoinpcb(so);
590 struct tcpcb *tp;
591
592 COMMON_START();
593 socantsendmore(so);
594 /* In case we got disconnected from the peer */
595 if (tp == 0)
596 goto out;
597 tp = tcp_usrclosed(tp);
598 if (tp)
599 error = tcp_output(tp);
600 COMMON_END(PRU_SHUTDOWN);
601 }
602
603 /*
604 * After a receive, possibly send window update to peer.
605 */
606 static int
607 tcp_usr_rcvd(struct socket *so, __unused int flags)
608 {
609 int error = 0;
610 struct inpcb *inp = sotoinpcb(so);
611 struct tcpcb *tp;
612
613 COMMON_START();
614 /* In case we got disconnected from the peer */
615 if (tp == 0)
616 goto out;
617 tcp_output(tp);
618 COMMON_END(PRU_RCVD);
619 }
620
621 /*
622 * Do a send by putting data in output queue and updating urgent
623 * marker if URG set. Possibly send more data. Unlike the other
624 * pru_*() routines, the mbuf chains are our responsibility. We
625 * must either enqueue them or free them. The other pru_* routines
626 * generally are caller-frees.
627 *
628 * Returns: 0 Success
629 * ECONNRESET
630 * EINVAL
631 * ENOBUFS
632 * tcp_connect:EADDRINUSE Address in use
633 * tcp_connect:EADDRNOTAVAIL Address not available.
634 * tcp_connect:EINVAL Invalid argument
635 * tcp_connect:EAFNOSUPPORT Address family not supported [notdef]
636 * tcp_connect:EACCES Permission denied
637 * tcp_connect:EAGAIN Resource unavailable, try again
638 * tcp_connect:EPERM Operation not permitted
639 * tcp_output:EADDRNOTAVAIL
640 * tcp_output:ENOBUFS
641 * tcp_output:EMSGSIZE
642 * tcp_output:EHOSTUNREACH
643 * tcp_output:ENETUNREACH
644 * tcp_output:ENETDOWN
645 * tcp_output:ENOMEM
646 * tcp_output:EACCES
647 * tcp_output:EMSGSIZE
648 * tcp_output:ENOBUFS
649 * tcp_output:??? [ignorable: mostly IPSEC/firewall/DLIL]
650 * tcp6_connect:??? [IPV6 only]
651 */
652 static int
653 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
654 struct sockaddr *nam, struct mbuf *control, struct proc *p)
655 {
656 int error = 0;
657 struct inpcb *inp = sotoinpcb(so);
658 struct tcpcb *tp;
659 #if INET6
660 int isipv6;
661 #endif
662 TCPDEBUG0;
663
664 if (inp == NULL || inp->inp_state == INPCB_STATE_DEAD) {
665 /*
666 * OOPS! we lost a race, the TCP session got reset after
667 * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
668 * network interrupt in the non-splnet() section of sosend().
669 */
670 if (m)
671 m_freem(m);
672 if (control)
673 m_freem(control);
674 error = ECONNRESET; /* XXX EPIPE? */
675 tp = NULL;
676 TCPDEBUG1();
677 goto out;
678 }
679 #if INET6
680 isipv6 = nam && nam->sa_family == AF_INET6;
681 #endif /* INET6 */
682 tp = intotcpcb(inp);
683 TCPDEBUG1();
684 if (control) {
685 /* TCP doesn't do control messages (rights, creds, etc) */
686 if (control->m_len) {
687 m_freem(control);
688 if (m)
689 m_freem(m);
690 error = EINVAL;
691 goto out;
692 }
693 m_freem(control); /* empty control, just free it */
694 }
695 if(!(flags & PRUS_OOB)) {
696 sbappendstream(&so->so_snd, m);
697 if (nam && tp->t_state < TCPS_SYN_SENT) {
698 /*
699 * Do implied connect if not yet connected,
700 * initialize window to default value, and
701 * initialize maxseg/maxopd using peer's cached
702 * MSS.
703 */
704 #if INET6
705 if (isipv6)
706 error = tcp6_connect(tp, nam, p);
707 else
708 #endif /* INET6 */
709 error = tcp_connect(tp, nam, p);
710 if (error)
711 goto out;
712 tp->snd_wnd = TTCP_CLIENT_SND_WND;
713 tcp_mss(tp, -1);
714 }
715
716 if (flags & PRUS_EOF) {
717 /*
718 * Close the send side of the connection after
719 * the data is sent.
720 */
721 socantsendmore(so);
722 tp = tcp_usrclosed(tp);
723 }
724 if (tp != NULL) {
725 if (flags & PRUS_MORETOCOME)
726 tp->t_flags |= TF_MORETOCOME;
727 error = tcp_output(tp);
728 if (flags & PRUS_MORETOCOME)
729 tp->t_flags &= ~TF_MORETOCOME;
730 }
731 } else {
732 if (sbspace(&so->so_snd) < -512) {
733 m_freem(m);
734 error = ENOBUFS;
735 goto out;
736 }
737 /*
738 * According to RFC961 (Assigned Protocols),
739 * the urgent pointer points to the last octet
740 * of urgent data. We continue, however,
741 * to consider it to indicate the first octet
742 * of data past the urgent section.
743 * Otherwise, snd_up should be one lower.
744 */
745 sbappendstream(&so->so_snd, m);
746 if (nam && tp->t_state < TCPS_SYN_SENT) {
747 /*
748 * Do implied connect if not yet connected,
749 * initialize window to default value, and
750 * initialize maxseg/maxopd using peer's cached
751 * MSS.
752 */
753 #if INET6
754 if (isipv6)
755 error = tcp6_connect(tp, nam, p);
756 else
757 #endif /* INET6 */
758 error = tcp_connect(tp, nam, p);
759 if (error)
760 goto out;
761 tp->snd_wnd = TTCP_CLIENT_SND_WND;
762 tcp_mss(tp, -1);
763 }
764 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
765 tp->t_force = 1;
766 error = tcp_output(tp);
767 tp->t_force = 0;
768 }
769 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
770 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
771 }
772
773 /*
774 * Abort the TCP.
775 */
776 static int
777 tcp_usr_abort(struct socket *so)
778 {
779 int error = 0;
780 struct inpcb *inp = sotoinpcb(so);
781 struct tcpcb *tp;
782
783 COMMON_START();
784 /* In case we got disconnected from the peer */
785 if (tp == 0)
786 goto out;
787 tp = tcp_drop(tp, ECONNABORTED);
788 so->so_usecount--;
789 COMMON_END(PRU_ABORT);
790 }
791
792 /*
793 * Receive out-of-band data.
794 *
795 * Returns: 0 Success
796 * EINVAL [COMMON_START]
797 * EINVAL
798 * EWOULDBLOCK
799 */
800 static int
801 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
802 {
803 int error = 0;
804 struct inpcb *inp = sotoinpcb(so);
805 struct tcpcb *tp;
806
807 COMMON_START();
808 if ((so->so_oobmark == 0 &&
809 (so->so_state & SS_RCVATMARK) == 0) ||
810 so->so_options & SO_OOBINLINE ||
811 tp->t_oobflags & TCPOOB_HADDATA) {
812 error = EINVAL;
813 goto out;
814 }
815 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
816 error = EWOULDBLOCK;
817 goto out;
818 }
819 m->m_len = 1;
820 *mtod(m, caddr_t) = tp->t_iobc;
821 if ((flags & MSG_PEEK) == 0)
822 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
823 COMMON_END(PRU_RCVOOB);
824 }
825
826 /* xxx - should be const */
827 struct pr_usrreqs tcp_usrreqs = {
828 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
829 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
830 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
831 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
832 in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
833 };
834
835 #if INET6
836 struct pr_usrreqs tcp6_usrreqs = {
837 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
838 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
839 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
840 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
841 in6_mapped_sockaddr, sosend, soreceive, pru_sopoll_notsupp
842 };
843 #endif /* INET6 */
844
845 /*
846 * Common subroutine to open a TCP connection to remote host specified
847 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
848 * port number if needed. Call in_pcbladdr to do the routing and to choose
849 * a local host address (interface). If there is an existing incarnation
850 * of the same connection in TIME-WAIT state and if the remote host was
851 * sending CC options and if the connection duration was < MSL, then
852 * truncate the previous TIME-WAIT state and proceed.
853 * Initialize connection parameters and enter SYN-SENT state.
854 *
855 * Returns: 0 Success
856 * EADDRINUSE
857 * EINVAL
858 * in_pcbbind:EADDRNOTAVAIL Address not available.
859 * in_pcbbind:EINVAL Invalid argument
860 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
861 * in_pcbbind:EACCES Permission denied
862 * in_pcbbind:EADDRINUSE Address in use
863 * in_pcbbind:EAGAIN Resource unavailable, try again
864 * in_pcbbind:EPERM Operation not permitted
865 * in_pcbladdr:EINVAL Invalid argument
866 * in_pcbladdr:EAFNOSUPPORT Address family not supported
867 * in_pcbladdr:EADDRNOTAVAIL Address not available
868 */
869 static int
870 tcp_connect(tp, nam, p)
871 register struct tcpcb *tp;
872 struct sockaddr *nam;
873 struct proc *p;
874 {
875 struct inpcb *inp = tp->t_inpcb, *oinp;
876 struct socket *so = inp->inp_socket;
877 struct tcpcb *otp;
878 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
879 struct sockaddr_in *ifaddr;
880 struct rmxp_tao *taop;
881 struct rmxp_tao tao_noncached;
882 int error;
883
884 if (inp->inp_lport == 0) {
885 error = in_pcbbind(inp, (struct sockaddr *)0, p);
886 if (error)
887 return error;
888 }
889
890 /*
891 * Cannot simply call in_pcbconnect, because there might be an
892 * earlier incarnation of this same connection still in
893 * TIME_WAIT state, creating an ADDRINUSE error.
894 */
895 error = in_pcbladdr(inp, nam, &ifaddr);
896 if (error)
897 return error;
898
899 tcp_unlock(inp->inp_socket, 0, 0);
900 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
901 sin->sin_addr, sin->sin_port,
902 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
903 : ifaddr->sin_addr,
904 inp->inp_lport, 0, NULL);
905
906 tcp_lock(inp->inp_socket, 0, 0);
907 if (oinp) {
908 if (oinp != inp) /* 4143933: avoid deadlock if inp == oinp */
909 tcp_lock(oinp->inp_socket, 1, 0);
910 if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) {
911 if (oinp != inp)
912 tcp_unlock(oinp->inp_socket, 1, 0);
913 goto skip_oinp;
914 }
915
916 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
917 otp->t_state == TCPS_TIME_WAIT &&
918 otp->t_starttime < (u_long)tcp_msl &&
919 (otp->t_flags & TF_RCVD_CC))
920 otp = tcp_close(otp);
921 else {
922 printf("tcp_connect: inp=%p err=EADDRINUSE\n", inp);
923 if (oinp != inp)
924 tcp_unlock(oinp->inp_socket, 1, 0);
925 return EADDRINUSE;
926 }
927 if (oinp != inp)
928 tcp_unlock(oinp->inp_socket, 1, 0);
929 }
930 skip_oinp:
931 if ((inp->inp_laddr.s_addr == INADDR_ANY ? ifaddr->sin_addr.s_addr :
932 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
933 inp->inp_lport == sin->sin_port)
934 return EINVAL;
935 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
936 /*lock inversion issue, mostly with udp multicast packets */
937 socket_unlock(inp->inp_socket, 0);
938 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
939 socket_lock(inp->inp_socket, 0);
940 }
941 if (inp->inp_laddr.s_addr == INADDR_ANY)
942 inp->inp_laddr = ifaddr->sin_addr;
943 inp->inp_faddr = sin->sin_addr;
944 inp->inp_fport = sin->sin_port;
945 in_pcbrehash(inp);
946 lck_rw_done(inp->inp_pcbinfo->mtx);
947
948 /* Compute window scaling to requesti according to sb_hiwat
949 * or leave us some room to increase potentially increase the window size depending
950 * on the default win scale
951 */
952 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
953 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
954 tp->request_r_scale++;
955
956 /*
957 * Inflate window size only if no setsockopt was performed on the recv sockbuf and
958 * if we're not over our number of active pcbs.
959 */
960
961 if (((so->so_rcv.sb_flags & SB_USRSIZE) == 0) && (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold)) {
962 tp->request_r_scale = max(tcp_win_scale, tp->request_r_scale);
963 so->so_rcv.sb_hiwat = min(TCP_MAXWIN << tp->request_r_scale, (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES);
964 }
965
966 soisconnecting(so);
967 tcpstat.tcps_connattempt++;
968 tp->t_state = TCPS_SYN_SENT;
969 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
970 tp->iss = tcp_new_isn(tp);
971 tcp_sendseqinit(tp);
972
973 /*
974 * Generate a CC value for this connection and
975 * check whether CC or CCnew should be used.
976 */
977 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
978 taop = &tao_noncached;
979 bzero(taop, sizeof(*taop));
980 }
981
982 tp->cc_send = CC_INC(tcp_ccgen);
983 if (taop->tao_ccsent != 0 &&
984 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
985 taop->tao_ccsent = tp->cc_send;
986 } else {
987 taop->tao_ccsent = 0;
988 tp->t_flags |= TF_SENDCCNEW;
989 }
990
991 return 0;
992 }
993
994 #if INET6
995 static int
996 tcp6_connect(tp, nam, p)
997 register struct tcpcb *tp;
998 struct sockaddr *nam;
999 struct proc *p;
1000 {
1001 struct inpcb *inp = tp->t_inpcb, *oinp;
1002 struct socket *so = inp->inp_socket;
1003 struct tcpcb *otp;
1004 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1005 struct in6_addr addr6;
1006 struct rmxp_tao *taop;
1007 struct rmxp_tao tao_noncached;
1008 int error;
1009
1010 if (inp->inp_lport == 0) {
1011 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
1012 if (error)
1013 return error;
1014 }
1015
1016 /*
1017 * Cannot simply call in_pcbconnect, because there might be an
1018 * earlier incarnation of this same connection still in
1019 * TIME_WAIT state, creating an ADDRINUSE error.
1020 */
1021 error = in6_pcbladdr(inp, nam, &addr6);
1022 if (error)
1023 return error;
1024 tcp_unlock(inp->inp_socket, 0, 0);
1025 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1026 &sin6->sin6_addr, sin6->sin6_port,
1027 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1028 ? &addr6
1029 : &inp->in6p_laddr,
1030 inp->inp_lport, 0, NULL);
1031 tcp_lock(inp->inp_socket, 0, 0);
1032 if (oinp) {
1033 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1034 otp->t_state == TCPS_TIME_WAIT &&
1035 otp->t_starttime < (u_long)tcp_msl &&
1036 (otp->t_flags & TF_RCVD_CC))
1037 otp = tcp_close(otp);
1038 else
1039 return EADDRINUSE;
1040 }
1041 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
1042 /*lock inversion issue, mostly with udp multicast packets */
1043 socket_unlock(inp->inp_socket, 0);
1044 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
1045 socket_lock(inp->inp_socket, 0);
1046 }
1047 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1048 inp->in6p_laddr = addr6;
1049 inp->in6p_faddr = sin6->sin6_addr;
1050 inp->inp_fport = sin6->sin6_port;
1051 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1052 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1053 in_pcbrehash(inp);
1054 lck_rw_done(inp->inp_pcbinfo->mtx);
1055
1056 /* Compute window scaling to request. */
1057 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1058 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1059 tp->request_r_scale++;
1060
1061 soisconnecting(so);
1062 tcpstat.tcps_connattempt++;
1063 tp->t_state = TCPS_SYN_SENT;
1064 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
1065 tp->iss = tcp_new_isn(tp);
1066 tcp_sendseqinit(tp);
1067
1068 /*
1069 * Generate a CC value for this connection and
1070 * check whether CC or CCnew should be used.
1071 */
1072 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
1073 taop = &tao_noncached;
1074 bzero(taop, sizeof(*taop));
1075 }
1076
1077 tp->cc_send = CC_INC(tcp_ccgen);
1078 if (taop->tao_ccsent != 0 &&
1079 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1080 taop->tao_ccsent = tp->cc_send;
1081 } else {
1082 taop->tao_ccsent = 0;
1083 tp->t_flags |= TF_SENDCCNEW;
1084 }
1085
1086 return 0;
1087 }
1088 #endif /* INET6 */
1089
1090 /*
1091 * The new sockopt interface makes it possible for us to block in the
1092 * copyin/out step (if we take a page fault). Taking a page fault at
1093 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1094 * use TSM, there probably isn't any need for this function to run at
1095 * splnet() any more. This needs more examination.)
1096 */
1097 int
1098 tcp_ctloutput(so, sopt)
1099 struct socket *so;
1100 struct sockopt *sopt;
1101 {
1102 int error, opt, optval;
1103 struct inpcb *inp;
1104 struct tcpcb *tp;
1105
1106 error = 0;
1107 inp = sotoinpcb(so);
1108 if (inp == NULL) {
1109 return (ECONNRESET);
1110 }
1111 if (sopt->sopt_level != IPPROTO_TCP) {
1112 #if INET6
1113 if (INP_CHECK_SOCKAF(so, AF_INET6))
1114 error = ip6_ctloutput(so, sopt);
1115 else
1116 #endif /* INET6 */
1117 error = ip_ctloutput(so, sopt);
1118 return (error);
1119 }
1120 tp = intotcpcb(inp);
1121 if (tp == NULL) {
1122 return (ECONNRESET);
1123 }
1124
1125 switch (sopt->sopt_dir) {
1126 case SOPT_SET:
1127 switch (sopt->sopt_name) {
1128 case TCP_NODELAY:
1129 case TCP_NOOPT:
1130 case TCP_NOPUSH:
1131 error = sooptcopyin(sopt, &optval, sizeof optval,
1132 sizeof optval);
1133 if (error)
1134 break;
1135
1136 switch (sopt->sopt_name) {
1137 case TCP_NODELAY:
1138 opt = TF_NODELAY;
1139 break;
1140 case TCP_NOOPT:
1141 opt = TF_NOOPT;
1142 break;
1143 case TCP_NOPUSH:
1144 opt = TF_NOPUSH;
1145 break;
1146 default:
1147 opt = 0; /* dead code to fool gcc */
1148 break;
1149 }
1150
1151 if (optval)
1152 tp->t_flags |= opt;
1153 else
1154 tp->t_flags &= ~opt;
1155 break;
1156
1157 case TCP_MAXSEG:
1158 error = sooptcopyin(sopt, &optval, sizeof optval,
1159 sizeof optval);
1160 if (error)
1161 break;
1162
1163 if (optval > 0 && optval <= tp->t_maxseg &&
1164 optval + 40 >= tcp_minmss)
1165 tp->t_maxseg = optval;
1166 else
1167 error = EINVAL;
1168 break;
1169
1170 case TCP_KEEPALIVE:
1171 error = sooptcopyin(sopt, &optval, sizeof optval,
1172 sizeof optval);
1173 if (error)
1174 break;
1175 if (optval < 0)
1176 error = EINVAL;
1177 else {
1178 tp->t_keepidle = optval * TCP_RETRANSHZ;
1179 tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp); /* reset the timer to new value */
1180 }
1181 break;
1182
1183 default:
1184 error = ENOPROTOOPT;
1185 break;
1186 }
1187 break;
1188
1189 case SOPT_GET:
1190 switch (sopt->sopt_name) {
1191 case TCP_NODELAY:
1192 optval = tp->t_flags & TF_NODELAY;
1193 break;
1194 case TCP_MAXSEG:
1195 optval = tp->t_maxseg;
1196 break;
1197 case TCP_KEEPALIVE:
1198 optval = tp->t_keepidle / TCP_RETRANSHZ;
1199 break;
1200 case TCP_NOOPT:
1201 optval = tp->t_flags & TF_NOOPT;
1202 break;
1203 case TCP_NOPUSH:
1204 optval = tp->t_flags & TF_NOPUSH;
1205 break;
1206 default:
1207 error = ENOPROTOOPT;
1208 break;
1209 }
1210 if (error == 0)
1211 error = sooptcopyout(sopt, &optval, sizeof optval);
1212 break;
1213 }
1214 return (error);
1215 }
1216
1217 /*
1218 * tcp_sendspace and tcp_recvspace are the default send and receive window
1219 * sizes, respectively. These are obsolescent (this information should
1220 * be set by the route).
1221 */
1222 u_long tcp_sendspace = 1448*256;
1223 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1224 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1225 u_long tcp_recvspace = 1448*384;
1226 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1227 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1228
1229
1230 /*
1231 * Attach TCP protocol to socket, allocating
1232 * internet protocol control block, tcp control block,
1233 * bufer space, and entering LISTEN state if to accept connections.
1234 *
1235 * Returns: 0 Success
1236 * in_pcballoc:ENOBUFS
1237 * in_pcballoc:ENOMEM
1238 * in_pcballoc:??? [IPSEC specific]
1239 * soreserve:ENOBUFS
1240 */
1241 static int
1242 tcp_attach(so, p)
1243 struct socket *so;
1244 struct proc *p;
1245 {
1246 register struct tcpcb *tp;
1247 struct inpcb *inp;
1248 int error;
1249 u_long sb_effective_max;
1250 #if INET6
1251 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1252 #endif
1253
1254 error = in_pcballoc(so, &tcbinfo, p);
1255 if (error)
1256 return (error);
1257
1258 inp = sotoinpcb(so);
1259
1260 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1261 /*
1262 * The goal is to let clients machines use large send/rcv default windows to compensate for link
1263 * latency and make sure the receiver is not constraining the sender window.
1264 * But we doon't want to have a few connections use all our mbuf space for servers.
1265 * This is done by watching a threshold of tcpcbs in use and bumping the default send and rcvspace
1266 * only if that threshold isn't reached.
1267 * We're also advertising a much bigger window size (tuneable by sysctl) in correlation with * the max socket buffer size if
1268 * we consider that we have enough ressources for it. This window will be adjusted depending on the
1269 * global socket layer buffer use with the use of tcp_sbpace
1270 */
1271
1272 if (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold) {
1273 sb_effective_max = (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES;
1274 error = soreserve(so, max(min((TCP_MAXWIN << tcp_win_scale)/4, sb_effective_max), tcp_sendspace),
1275 max(min((TCP_MAXWIN << tcp_win_scale)/2, sb_effective_max), tcp_recvspace));
1276 }
1277 else
1278 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1279 if (error)
1280 return (error);
1281 }
1282
1283 #if INET6
1284 if (isipv6) {
1285 inp->inp_vflag |= INP_IPV6;
1286 inp->in6p_hops = -1; /* use kernel default */
1287 }
1288 else
1289 #endif /* INET6 */
1290 inp->inp_vflag |= INP_IPV4;
1291 tp = tcp_newtcpcb(inp);
1292 if (tp == 0) {
1293 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1294
1295 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1296 #if INET6
1297 if (isipv6)
1298 in6_pcbdetach(inp);
1299 else
1300 #endif /* INET6 */
1301 in_pcbdetach(inp);
1302 so->so_state |= nofd;
1303 return (ENOBUFS);
1304 }
1305 tp->t_state = TCPS_CLOSED;
1306 return (0);
1307 }
1308
1309 /*
1310 * Initiate (or continue) disconnect.
1311 * If embryonic state, just send reset (once).
1312 * If in ``let data drain'' option and linger null, just drop.
1313 * Otherwise (hard), mark socket disconnecting and drop
1314 * current input data; switch states based on user close, and
1315 * send segment to peer (with FIN).
1316 */
1317 static struct tcpcb *
1318 tcp_disconnect(tp)
1319 register struct tcpcb *tp;
1320 {
1321 struct socket *so = tp->t_inpcb->inp_socket;
1322
1323 if (tp->t_state < TCPS_ESTABLISHED)
1324 tp = tcp_close(tp);
1325 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1326 tp = tcp_drop(tp, 0);
1327 else {
1328 soisdisconnecting(so);
1329 sbflush(&so->so_rcv);
1330 tp = tcp_usrclosed(tp);
1331 if (tp)
1332 (void) tcp_output(tp);
1333 }
1334 return (tp);
1335 }
1336
1337 /*
1338 * User issued close, and wish to trail through shutdown states:
1339 * if never received SYN, just forget it. If got a SYN from peer,
1340 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1341 * If already got a FIN from peer, then almost done; go to LAST_ACK
1342 * state. In all other cases, have already sent FIN to peer (e.g.
1343 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1344 * for peer to send FIN or not respond to keep-alives, etc.
1345 * We can let the user exit from the close as soon as the FIN is acked.
1346 */
1347 static struct tcpcb *
1348 tcp_usrclosed(tp)
1349 register struct tcpcb *tp;
1350 {
1351
1352 switch (tp->t_state) {
1353
1354 case TCPS_CLOSED:
1355 case TCPS_LISTEN:
1356 tp->t_state = TCPS_CLOSED;
1357 tp = tcp_close(tp);
1358 break;
1359
1360 case TCPS_SYN_SENT:
1361 case TCPS_SYN_RECEIVED:
1362 tp->t_flags |= TF_NEEDFIN;
1363 break;
1364
1365 case TCPS_ESTABLISHED:
1366 tp->t_state = TCPS_FIN_WAIT_1;
1367 break;
1368
1369 case TCPS_CLOSE_WAIT:
1370 tp->t_state = TCPS_LAST_ACK;
1371 break;
1372 }
1373 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1374 soisdisconnected(tp->t_inpcb->inp_socket);
1375 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1376 if (tp->t_state == TCPS_FIN_WAIT_2)
1377 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1378 }
1379 return (tp);
1380 }
1381
1382 void
1383 tcp_in_cksum_stats(u_int32_t len)
1384 {
1385 tcps_in_sw_cksum++;
1386 tcps_in_sw_cksum_bytes += len;
1387 }
1388
1389 void
1390 tcp_out_cksum_stats(u_int32_t len)
1391 {
1392 tcps_out_sw_cksum++;
1393 tcps_out_sw_cksum_bytes += len;
1394 }