]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
xnu-1504.15.3.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_usrreq.c
1 /*
2 * Copyright (c) 2000-2008 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, IFSCOPE_NONE);
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) == 0) {
733 /* if no space is left in sockbuf,
734 * do not try to squeeze in OOB traffic */
735 m_freem(m);
736 error = ENOBUFS;
737 goto out;
738 }
739 /*
740 * According to RFC961 (Assigned Protocols),
741 * the urgent pointer points to the last octet
742 * of urgent data. We continue, however,
743 * to consider it to indicate the first octet
744 * of data past the urgent section.
745 * Otherwise, snd_up should be one lower.
746 */
747 sbappendstream(&so->so_snd, m);
748 if (nam && tp->t_state < TCPS_SYN_SENT) {
749 /*
750 * Do implied connect if not yet connected,
751 * initialize window to default value, and
752 * initialize maxseg/maxopd using peer's cached
753 * MSS.
754 */
755 #if INET6
756 if (isipv6)
757 error = tcp6_connect(tp, nam, p);
758 else
759 #endif /* INET6 */
760 error = tcp_connect(tp, nam, p);
761 if (error)
762 goto out;
763 tp->snd_wnd = TTCP_CLIENT_SND_WND;
764 tcp_mss(tp, -1, IFSCOPE_NONE);
765 }
766 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
767 tp->t_force = 1;
768 error = tcp_output(tp);
769 tp->t_force = 0;
770 }
771 COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
772 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
773 }
774
775 /*
776 * Abort the TCP.
777 */
778 static int
779 tcp_usr_abort(struct socket *so)
780 {
781 int error = 0;
782 struct inpcb *inp = sotoinpcb(so);
783 struct tcpcb *tp;
784
785 COMMON_START();
786 /* In case we got disconnected from the peer */
787 if (tp == 0)
788 goto out;
789 tp = tcp_drop(tp, ECONNABORTED);
790 so->so_usecount--;
791 COMMON_END(PRU_ABORT);
792 }
793
794 /*
795 * Receive out-of-band data.
796 *
797 * Returns: 0 Success
798 * EINVAL [COMMON_START]
799 * EINVAL
800 * EWOULDBLOCK
801 */
802 static int
803 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
804 {
805 int error = 0;
806 struct inpcb *inp = sotoinpcb(so);
807 struct tcpcb *tp;
808
809 COMMON_START();
810 if ((so->so_oobmark == 0 &&
811 (so->so_state & SS_RCVATMARK) == 0) ||
812 so->so_options & SO_OOBINLINE ||
813 tp->t_oobflags & TCPOOB_HADDATA) {
814 error = EINVAL;
815 goto out;
816 }
817 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
818 error = EWOULDBLOCK;
819 goto out;
820 }
821 m->m_len = 1;
822 *mtod(m, caddr_t) = tp->t_iobc;
823 if ((flags & MSG_PEEK) == 0)
824 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
825 COMMON_END(PRU_RCVOOB);
826 }
827
828 /* xxx - should be const */
829 struct pr_usrreqs tcp_usrreqs = {
830 tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
831 tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
832 tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
833 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
834 in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
835 };
836
837 #if INET6
838 struct pr_usrreqs tcp6_usrreqs = {
839 tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
840 tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
841 tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
842 tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
843 in6_mapped_sockaddr, sosend, soreceive, pru_sopoll_notsupp
844 };
845 #endif /* INET6 */
846
847 /*
848 * Common subroutine to open a TCP connection to remote host specified
849 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
850 * port number if needed. Call in_pcbladdr to do the routing and to choose
851 * a local host address (interface). If there is an existing incarnation
852 * of the same connection in TIME-WAIT state and if the remote host was
853 * sending CC options and if the connection duration was < MSL, then
854 * truncate the previous TIME-WAIT state and proceed.
855 * Initialize connection parameters and enter SYN-SENT state.
856 *
857 * Returns: 0 Success
858 * EADDRINUSE
859 * EINVAL
860 * in_pcbbind:EADDRNOTAVAIL Address not available.
861 * in_pcbbind:EINVAL Invalid argument
862 * in_pcbbind:EAFNOSUPPORT Address family not supported [notdef]
863 * in_pcbbind:EACCES Permission denied
864 * in_pcbbind:EADDRINUSE Address in use
865 * in_pcbbind:EAGAIN Resource unavailable, try again
866 * in_pcbbind:EPERM Operation not permitted
867 * in_pcbladdr:EINVAL Invalid argument
868 * in_pcbladdr:EAFNOSUPPORT Address family not supported
869 * in_pcbladdr:EADDRNOTAVAIL Address not available
870 */
871 static int
872 tcp_connect(tp, nam, p)
873 register struct tcpcb *tp;
874 struct sockaddr *nam;
875 struct proc *p;
876 {
877 struct inpcb *inp = tp->t_inpcb, *oinp;
878 struct socket *so = inp->inp_socket;
879 struct tcpcb *otp;
880 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
881 struct sockaddr_in *ifaddr;
882 struct rmxp_tao *taop;
883 struct rmxp_tao tao_noncached;
884 int error;
885
886 if (inp->inp_lport == 0) {
887 error = in_pcbbind(inp, (struct sockaddr *)0, p);
888 if (error)
889 return error;
890 }
891
892 /*
893 * Cannot simply call in_pcbconnect, because there might be an
894 * earlier incarnation of this same connection still in
895 * TIME_WAIT state, creating an ADDRINUSE error.
896 */
897 error = in_pcbladdr(inp, nam, &ifaddr);
898 if (error)
899 return error;
900
901 tcp_unlock(inp->inp_socket, 0, 0);
902 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
903 sin->sin_addr, sin->sin_port,
904 inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
905 : ifaddr->sin_addr,
906 inp->inp_lport, 0, NULL);
907
908 tcp_lock(inp->inp_socket, 0, 0);
909 if (oinp) {
910 if (oinp != inp) /* 4143933: avoid deadlock if inp == oinp */
911 tcp_lock(oinp->inp_socket, 1, 0);
912 if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) {
913 if (oinp != inp)
914 tcp_unlock(oinp->inp_socket, 1, 0);
915 goto skip_oinp;
916 }
917
918 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
919 otp->t_state == TCPS_TIME_WAIT &&
920 otp->t_starttime < (u_int32_t)tcp_msl &&
921 (otp->t_flags & TF_RCVD_CC))
922 otp = tcp_close(otp);
923 else {
924 printf("tcp_connect: inp=%p err=EADDRINUSE\n", inp);
925 if (oinp != inp)
926 tcp_unlock(oinp->inp_socket, 1, 0);
927 return EADDRINUSE;
928 }
929 if (oinp != inp)
930 tcp_unlock(oinp->inp_socket, 1, 0);
931 }
932 skip_oinp:
933 if ((inp->inp_laddr.s_addr == INADDR_ANY ? ifaddr->sin_addr.s_addr :
934 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
935 inp->inp_lport == sin->sin_port)
936 return EINVAL;
937 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
938 /*lock inversion issue, mostly with udp multicast packets */
939 socket_unlock(inp->inp_socket, 0);
940 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
941 socket_lock(inp->inp_socket, 0);
942 }
943 if (inp->inp_laddr.s_addr == INADDR_ANY)
944 inp->inp_laddr = ifaddr->sin_addr;
945 inp->inp_faddr = sin->sin_addr;
946 inp->inp_fport = sin->sin_port;
947 in_pcbrehash(inp);
948 lck_rw_done(inp->inp_pcbinfo->mtx);
949
950 /* Compute window scaling to requesti according to sb_hiwat
951 * or leave us some room to increase potentially increase the window size depending
952 * on the default win scale
953 */
954 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
955 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
956 tp->request_r_scale++;
957
958 /*
959 * Inflate window size only if no setsockopt was performed on the recv sockbuf and
960 * if we're not over our number of active pcbs.
961 */
962
963 if (((so->so_rcv.sb_flags & SB_USRSIZE) == 0) && (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold)) {
964 tp->request_r_scale = max(tcp_win_scale, tp->request_r_scale);
965 so->so_rcv.sb_hiwat = min(TCP_MAXWIN << tp->request_r_scale, (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES);
966 }
967
968 soisconnecting(so);
969 tcpstat.tcps_connattempt++;
970 tp->t_state = TCPS_SYN_SENT;
971 tp->t_timer[TCPT_KEEP] = tp->t_keepinit ? tp->t_keepinit : tcp_keepinit;
972 tp->iss = tcp_new_isn(tp);
973 tcp_sendseqinit(tp);
974
975 /*
976 * Generate a CC value for this connection and
977 * check whether CC or CCnew should be used.
978 */
979 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
980 taop = &tao_noncached;
981 bzero(taop, sizeof(*taop));
982 }
983
984 tp->cc_send = CC_INC(tcp_ccgen);
985 if (taop->tao_ccsent != 0 &&
986 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
987 taop->tao_ccsent = tp->cc_send;
988 } else {
989 taop->tao_ccsent = 0;
990 tp->t_flags |= TF_SENDCCNEW;
991 }
992
993 return 0;
994 }
995
996 #if INET6
997 static int
998 tcp6_connect(tp, nam, p)
999 register struct tcpcb *tp;
1000 struct sockaddr *nam;
1001 struct proc *p;
1002 {
1003 struct inpcb *inp = tp->t_inpcb, *oinp;
1004 struct socket *so = inp->inp_socket;
1005 struct tcpcb *otp;
1006 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1007 struct in6_addr addr6;
1008 struct rmxp_tao *taop;
1009 struct rmxp_tao tao_noncached;
1010 int error;
1011
1012 if (inp->inp_lport == 0) {
1013 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
1014 if (error)
1015 return error;
1016 }
1017
1018 /*
1019 * Cannot simply call in_pcbconnect, because there might be an
1020 * earlier incarnation of this same connection still in
1021 * TIME_WAIT state, creating an ADDRINUSE error.
1022 */
1023 error = in6_pcbladdr(inp, nam, &addr6);
1024 if (error)
1025 return error;
1026 tcp_unlock(inp->inp_socket, 0, 0);
1027 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1028 &sin6->sin6_addr, sin6->sin6_port,
1029 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1030 ? &addr6
1031 : &inp->in6p_laddr,
1032 inp->inp_lport, 0, NULL);
1033 tcp_lock(inp->inp_socket, 0, 0);
1034 if (oinp) {
1035 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
1036 otp->t_state == TCPS_TIME_WAIT &&
1037 otp->t_starttime < (u_int32_t)tcp_msl &&
1038 (otp->t_flags & TF_RCVD_CC))
1039 otp = tcp_close(otp);
1040 else
1041 return EADDRINUSE;
1042 }
1043 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
1044 /*lock inversion issue, mostly with udp multicast packets */
1045 socket_unlock(inp->inp_socket, 0);
1046 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
1047 socket_lock(inp->inp_socket, 0);
1048 }
1049 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1050 inp->in6p_laddr = addr6;
1051 inp->in6p_faddr = sin6->sin6_addr;
1052 inp->inp_fport = sin6->sin6_port;
1053 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != 0)
1054 inp->in6p_flowinfo = sin6->sin6_flowinfo;
1055 in_pcbrehash(inp);
1056 lck_rw_done(inp->inp_pcbinfo->mtx);
1057
1058 /* Compute window scaling to request. */
1059 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1060 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
1061 tp->request_r_scale++;
1062
1063 soisconnecting(so);
1064 tcpstat.tcps_connattempt++;
1065 tp->t_state = TCPS_SYN_SENT;
1066 tp->t_timer[TCPT_KEEP] = tp->t_keepinit ? tp->t_keepinit : tcp_keepinit;
1067 tp->iss = tcp_new_isn(tp);
1068 tcp_sendseqinit(tp);
1069
1070 /*
1071 * Generate a CC value for this connection and
1072 * check whether CC or CCnew should be used.
1073 */
1074 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
1075 taop = &tao_noncached;
1076 bzero(taop, sizeof(*taop));
1077 }
1078
1079 tp->cc_send = CC_INC(tcp_ccgen);
1080 if (taop->tao_ccsent != 0 &&
1081 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
1082 taop->tao_ccsent = tp->cc_send;
1083 } else {
1084 taop->tao_ccsent = 0;
1085 tp->t_flags |= TF_SENDCCNEW;
1086 }
1087
1088 return 0;
1089 }
1090 #endif /* INET6 */
1091
1092 /*
1093 * The new sockopt interface makes it possible for us to block in the
1094 * copyin/out step (if we take a page fault). Taking a page fault at
1095 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
1096 * use TSM, there probably isn't any need for this function to run at
1097 * splnet() any more. This needs more examination.)
1098 */
1099 int
1100 tcp_ctloutput(so, sopt)
1101 struct socket *so;
1102 struct sockopt *sopt;
1103 {
1104 int error, opt, optval;
1105 struct inpcb *inp;
1106 struct tcpcb *tp;
1107
1108 error = 0;
1109 inp = sotoinpcb(so);
1110 if (inp == NULL) {
1111 return (ECONNRESET);
1112 }
1113 if (sopt->sopt_level != IPPROTO_TCP) {
1114 #if INET6
1115 if (INP_CHECK_SOCKAF(so, AF_INET6))
1116 error = ip6_ctloutput(so, sopt);
1117 else
1118 #endif /* INET6 */
1119 error = ip_ctloutput(so, sopt);
1120 return (error);
1121 }
1122 tp = intotcpcb(inp);
1123 if (tp == NULL) {
1124 return (ECONNRESET);
1125 }
1126
1127 switch (sopt->sopt_dir) {
1128 case SOPT_SET:
1129 switch (sopt->sopt_name) {
1130 case TCP_NODELAY:
1131 case TCP_NOOPT:
1132 case TCP_NOPUSH:
1133 error = sooptcopyin(sopt, &optval, sizeof optval,
1134 sizeof optval);
1135 if (error)
1136 break;
1137
1138 switch (sopt->sopt_name) {
1139 case TCP_NODELAY:
1140 opt = TF_NODELAY;
1141 break;
1142 case TCP_NOOPT:
1143 opt = TF_NOOPT;
1144 break;
1145 case TCP_NOPUSH:
1146 opt = TF_NOPUSH;
1147 break;
1148 default:
1149 opt = 0; /* dead code to fool gcc */
1150 break;
1151 }
1152
1153 if (optval)
1154 tp->t_flags |= opt;
1155 else
1156 tp->t_flags &= ~opt;
1157 break;
1158
1159 case TCP_MAXSEG:
1160 error = sooptcopyin(sopt, &optval, sizeof optval,
1161 sizeof optval);
1162 if (error)
1163 break;
1164
1165 if (optval > 0 && optval <= tp->t_maxseg &&
1166 optval + 40 >= tcp_minmss)
1167 tp->t_maxseg = optval;
1168 else
1169 error = EINVAL;
1170 break;
1171
1172 case TCP_KEEPALIVE:
1173 error = sooptcopyin(sopt, &optval, sizeof optval,
1174 sizeof optval);
1175 if (error)
1176 break;
1177 if (optval < 0)
1178 error = EINVAL;
1179 else {
1180 tp->t_keepidle = optval * TCP_RETRANSHZ;
1181 tp->t_timer[TCPT_KEEP] = TCP_KEEPIDLE(tp); /* reset the timer to new value */
1182 }
1183 break;
1184
1185 case TCP_CONNECTIONTIMEOUT:
1186 error = sooptcopyin(sopt, &optval, sizeof optval,
1187 sizeof optval);
1188 if (error)
1189 break;
1190 if (optval < 0)
1191 error = EINVAL;
1192 else
1193 tp->t_keepinit = optval * TCP_RETRANSHZ;
1194 break;
1195
1196 default:
1197 error = ENOPROTOOPT;
1198 break;
1199 }
1200 break;
1201
1202 case SOPT_GET:
1203 switch (sopt->sopt_name) {
1204 case TCP_NODELAY:
1205 optval = tp->t_flags & TF_NODELAY;
1206 break;
1207 case TCP_MAXSEG:
1208 optval = tp->t_maxseg;
1209 break;
1210 case TCP_KEEPALIVE:
1211 optval = tp->t_keepidle / TCP_RETRANSHZ;
1212 break;
1213 case TCP_NOOPT:
1214 optval = tp->t_flags & TF_NOOPT;
1215 break;
1216 case TCP_NOPUSH:
1217 optval = tp->t_flags & TF_NOPUSH;
1218 break;
1219 case TCP_CONNECTIONTIMEOUT:
1220 optval = tp->t_keepinit / TCP_RETRANSHZ;
1221 break;
1222 default:
1223 error = ENOPROTOOPT;
1224 break;
1225 }
1226 if (error == 0)
1227 error = sooptcopyout(sopt, &optval, sizeof optval);
1228 break;
1229 }
1230 return (error);
1231 }
1232
1233 /*
1234 * tcp_sendspace and tcp_recvspace are the default send and receive window
1235 * sizes, respectively. These are obsolescent (this information should
1236 * be set by the route).
1237 */
1238 u_int32_t tcp_sendspace = 1448*256;
1239 u_int32_t tcp_recvspace = 1448*384;
1240
1241 /* During attach, the size of socket buffer allocated is limited to
1242 * sb_max in sbreserve. Disallow setting the tcp send and recv space
1243 * to be more than sb_max because that will cause tcp_attach to fail
1244 * (see radar 5713060)
1245 */
1246 static int
1247 sysctl_tcp_sospace(struct sysctl_oid *oidp, __unused void *arg1,
1248 __unused int arg2, struct sysctl_req *req) {
1249 u_int32_t new_value = 0, *space_p = NULL;
1250 int changed = 0, error = 0;
1251 u_quad_t sb_effective_max = (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES;
1252
1253 switch (oidp->oid_number) {
1254 case TCPCTL_SENDSPACE:
1255 space_p = &tcp_sendspace;
1256 break;
1257 case TCPCTL_RECVSPACE:
1258 space_p = &tcp_recvspace;
1259 break;
1260 default:
1261 return EINVAL;
1262 }
1263 error = sysctl_io_number(req, *space_p, sizeof(u_int32_t),
1264 &new_value, &changed);
1265 if (changed) {
1266 if (new_value > 0 && new_value <= sb_effective_max) {
1267 *space_p = new_value;
1268 } else {
1269 error = ERANGE;
1270 }
1271 }
1272 return error;
1273 }
1274
1275 SYSCTL_PROC(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLTYPE_INT | CTLFLAG_RW,
1276 &tcp_sendspace , 0, &sysctl_tcp_sospace, "IU", "Maximum outgoing TCP datagram size");
1277 SYSCTL_PROC(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLTYPE_INT | CTLFLAG_RW,
1278 &tcp_recvspace , 0, &sysctl_tcp_sospace, "IU", "Maximum incoming TCP datagram size");
1279
1280
1281 /*
1282 * Attach TCP protocol to socket, allocating
1283 * internet protocol control block, tcp control block,
1284 * bufer space, and entering LISTEN state if to accept connections.
1285 *
1286 * Returns: 0 Success
1287 * in_pcballoc:ENOBUFS
1288 * in_pcballoc:ENOMEM
1289 * in_pcballoc:??? [IPSEC specific]
1290 * soreserve:ENOBUFS
1291 */
1292 static int
1293 tcp_attach(so, p)
1294 struct socket *so;
1295 struct proc *p;
1296 {
1297 register struct tcpcb *tp;
1298 struct inpcb *inp;
1299 int error;
1300 u_long sb_effective_max;
1301 #if INET6
1302 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != 0;
1303 #endif
1304
1305 error = in_pcballoc(so, &tcbinfo, p);
1306 if (error)
1307 return (error);
1308
1309 inp = sotoinpcb(so);
1310
1311 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1312 /*
1313 * The goal is to let clients machines use large send/rcv default windows to compensate for link
1314 * latency and make sure the receiver is not constraining the sender window.
1315 * But we doon't want to have a few connections use all our mbuf space for servers.
1316 * This is done by watching a threshold of tcpcbs in use and bumping the default send and rcvspace
1317 * only if that threshold isn't reached.
1318 * We're also advertising a much bigger window size (tuneable by sysctl) in correlation with * the max socket buffer size if
1319 * we consider that we have enough ressources for it. This window will be adjusted depending on the
1320 * global socket layer buffer use with the use of tcp_sbpace
1321 */
1322
1323 if (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold) {
1324 sb_effective_max = (sb_max / (MSIZE+MCLBYTES)) * MCLBYTES;
1325 error = soreserve(so, max(min((TCP_MAXWIN << tcp_win_scale)/4, sb_effective_max), tcp_sendspace),
1326 max(min((TCP_MAXWIN << tcp_win_scale)/2, sb_effective_max), tcp_recvspace));
1327 }
1328 else
1329 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1330 if (error)
1331 return (error);
1332 }
1333
1334 #if INET6
1335 if (isipv6) {
1336 inp->inp_vflag |= INP_IPV6;
1337 inp->in6p_hops = -1; /* use kernel default */
1338 }
1339 else
1340 #endif /* INET6 */
1341 inp->inp_vflag |= INP_IPV4;
1342 tp = tcp_newtcpcb(inp);
1343 if (tp == 0) {
1344 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1345
1346 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1347 #if INET6
1348 if (isipv6)
1349 in6_pcbdetach(inp);
1350 else
1351 #endif /* INET6 */
1352 in_pcbdetach(inp);
1353 so->so_state |= nofd;
1354 return (ENOBUFS);
1355 }
1356 tp->t_state = TCPS_CLOSED;
1357 return (0);
1358 }
1359
1360 /*
1361 * Initiate (or continue) disconnect.
1362 * If embryonic state, just send reset (once).
1363 * If in ``let data drain'' option and linger null, just drop.
1364 * Otherwise (hard), mark socket disconnecting and drop
1365 * current input data; switch states based on user close, and
1366 * send segment to peer (with FIN).
1367 */
1368 static struct tcpcb *
1369 tcp_disconnect(tp)
1370 register struct tcpcb *tp;
1371 {
1372 struct socket *so = tp->t_inpcb->inp_socket;
1373
1374 if (tp->t_state < TCPS_ESTABLISHED)
1375 tp = tcp_close(tp);
1376 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1377 tp = tcp_drop(tp, 0);
1378 else {
1379 soisdisconnecting(so);
1380 sbflush(&so->so_rcv);
1381 tp = tcp_usrclosed(tp);
1382 if (tp)
1383 (void) tcp_output(tp);
1384 }
1385 return (tp);
1386 }
1387
1388 /*
1389 * User issued close, and wish to trail through shutdown states:
1390 * if never received SYN, just forget it. If got a SYN from peer,
1391 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1392 * If already got a FIN from peer, then almost done; go to LAST_ACK
1393 * state. In all other cases, have already sent FIN to peer (e.g.
1394 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1395 * for peer to send FIN or not respond to keep-alives, etc.
1396 * We can let the user exit from the close as soon as the FIN is acked.
1397 */
1398 static struct tcpcb *
1399 tcp_usrclosed(tp)
1400 register struct tcpcb *tp;
1401 {
1402
1403 switch (tp->t_state) {
1404
1405 case TCPS_CLOSED:
1406 case TCPS_LISTEN:
1407 tp->t_state = TCPS_CLOSED;
1408 tp = tcp_close(tp);
1409 break;
1410
1411 case TCPS_SYN_SENT:
1412 case TCPS_SYN_RECEIVED:
1413 tp->t_flags |= TF_NEEDFIN;
1414 break;
1415
1416 case TCPS_ESTABLISHED:
1417 tp->t_state = TCPS_FIN_WAIT_1;
1418 break;
1419
1420 case TCPS_CLOSE_WAIT:
1421 tp->t_state = TCPS_LAST_ACK;
1422 break;
1423 }
1424 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1425 soisdisconnected(tp->t_inpcb->inp_socket);
1426 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1427 if (tp->t_state == TCPS_FIN_WAIT_2)
1428 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1429 }
1430 return (tp);
1431 }
1432
1433 void
1434 tcp_in_cksum_stats(u_int32_t len)
1435 {
1436 tcps_in_sw_cksum++;
1437 tcps_in_sw_cksum_bytes += len;
1438 }
1439
1440 void
1441 tcp_out_cksum_stats(u_int32_t len)
1442 {
1443 tcps_out_sw_cksum++;
1444 tcps_out_sw_cksum_bytes += len;
1445 }