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