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