]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet/tcp_usrreq.c
xnu-792.6.61.tar.gz
[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 if (oinp != inp) /* 4143933: avoid deadlock if inp == oinp */
758 tcp_lock(oinp->inp_socket, 1, 0);
759 if (in_pcb_checkstate(oinp, WNT_RELEASE, 1) == WNT_STOPUSING) {
760 if (oinp != inp)
761 tcp_unlock(oinp->inp_socket, 1, 0);
762 goto skip_oinp;
763 }
764
765 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
766 otp->t_state == TCPS_TIME_WAIT &&
767 otp->t_starttime < tcp_msl &&
768 (otp->t_flags & TF_RCVD_CC))
769 otp = tcp_close(otp);
770 else {
771 printf("tcp_connect: inp=%x err=EADDRINUSE\n", inp);
772 if (oinp != inp)
773 tcp_unlock(oinp->inp_socket, 1, 0);
774 return EADDRINUSE;
775 }
776 if (oinp != inp)
777 tcp_unlock(oinp->inp_socket, 1, 0);
778 }
779 skip_oinp:
780 if ((inp->inp_laddr.s_addr == INADDR_ANY ? ifaddr->sin_addr.s_addr :
781 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
782 inp->inp_lport == sin->sin_port)
783 return EINVAL;
784 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
785 /*lock inversion issue, mostly with udp multicast packets */
786 socket_unlock(inp->inp_socket, 0);
787 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
788 socket_lock(inp->inp_socket, 0);
789 }
790 if (inp->inp_laddr.s_addr == INADDR_ANY)
791 inp->inp_laddr = ifaddr->sin_addr;
792 inp->inp_faddr = sin->sin_addr;
793 inp->inp_fport = sin->sin_port;
794 in_pcbrehash(inp);
795 lck_rw_done(inp->inp_pcbinfo->mtx);
796
797 /* Compute window scaling to request. */
798 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
799 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
800 tp->request_r_scale++;
801
802 soisconnecting(so);
803 tcpstat.tcps_connattempt++;
804 tp->t_state = TCPS_SYN_SENT;
805 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
806 tp->iss = tcp_new_isn(tp);
807 tcp_sendseqinit(tp);
808
809 /*
810 * Generate a CC value for this connection and
811 * check whether CC or CCnew should be used.
812 */
813 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
814 taop = &tao_noncached;
815 bzero(taop, sizeof(*taop));
816 }
817
818 tp->cc_send = CC_INC(tcp_ccgen);
819 if (taop->tao_ccsent != 0 &&
820 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
821 taop->tao_ccsent = tp->cc_send;
822 } else {
823 taop->tao_ccsent = 0;
824 tp->t_flags |= TF_SENDCCNEW;
825 }
826
827 return 0;
828 }
829
830 #if INET6
831 static int
832 tcp6_connect(tp, nam, p)
833 register struct tcpcb *tp;
834 struct sockaddr *nam;
835 struct proc *p;
836 {
837 struct inpcb *inp = tp->t_inpcb, *oinp;
838 struct socket *so = inp->inp_socket;
839 struct tcpcb *otp;
840 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
841 struct in6_addr addr6;
842 struct rmxp_tao *taop;
843 struct rmxp_tao tao_noncached;
844 int error;
845
846 if (inp->inp_lport == 0) {
847 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
848 if (error)
849 return error;
850 }
851
852 /*
853 * Cannot simply call in_pcbconnect, because there might be an
854 * earlier incarnation of this same connection still in
855 * TIME_WAIT state, creating an ADDRINUSE error.
856 */
857 error = in6_pcbladdr(inp, nam, &addr6);
858 if (error)
859 return error;
860 tcp_unlock(inp->inp_socket, 0, 0);
861 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
862 &sin6->sin6_addr, sin6->sin6_port,
863 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
864 ? &addr6
865 : &inp->in6p_laddr,
866 inp->inp_lport, 0, NULL);
867 tcp_lock(inp->inp_socket, 0, 0);
868 if (oinp) {
869 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
870 otp->t_state == TCPS_TIME_WAIT &&
871 otp->t_starttime < tcp_msl &&
872 (otp->t_flags & TF_RCVD_CC))
873 otp = tcp_close(otp);
874 else
875 return EADDRINUSE;
876 }
877 if (!lck_rw_try_lock_exclusive(inp->inp_pcbinfo->mtx)) {
878 /*lock inversion issue, mostly with udp multicast packets */
879 socket_unlock(inp->inp_socket, 0);
880 lck_rw_lock_exclusive(inp->inp_pcbinfo->mtx);
881 socket_lock(inp->inp_socket, 0);
882 }
883 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
884 inp->in6p_laddr = addr6;
885 inp->in6p_faddr = sin6->sin6_addr;
886 inp->inp_fport = sin6->sin6_port;
887 if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
888 inp->in6p_flowinfo = sin6->sin6_flowinfo;
889 in_pcbrehash(inp);
890 lck_rw_done(inp->inp_pcbinfo->mtx);
891
892 /* Compute window scaling to request. */
893 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
894 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
895 tp->request_r_scale++;
896
897 soisconnecting(so);
898 tcpstat.tcps_connattempt++;
899 tp->t_state = TCPS_SYN_SENT;
900 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
901 tp->iss = tcp_new_isn(tp);
902 tcp_sendseqinit(tp);
903
904 /*
905 * Generate a CC value for this connection and
906 * check whether CC or CCnew should be used.
907 */
908 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
909 taop = &tao_noncached;
910 bzero(taop, sizeof(*taop));
911 }
912
913 tp->cc_send = CC_INC(tcp_ccgen);
914 if (taop->tao_ccsent != 0 &&
915 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
916 taop->tao_ccsent = tp->cc_send;
917 } else {
918 taop->tao_ccsent = 0;
919 tp->t_flags |= TF_SENDCCNEW;
920 }
921
922 return 0;
923 }
924 #endif /* INET6 */
925
926 /*
927 * The new sockopt interface makes it possible for us to block in the
928 * copyin/out step (if we take a page fault). Taking a page fault at
929 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
930 * use TSM, there probably isn't any need for this function to run at
931 * splnet() any more. This needs more examination.)
932 */
933 int
934 tcp_ctloutput(so, sopt)
935 struct socket *so;
936 struct sockopt *sopt;
937 {
938 int error, opt, optval;
939 struct inpcb *inp;
940 struct tcpcb *tp;
941
942 error = 0;
943 inp = sotoinpcb(so);
944 if (inp == NULL) {
945 return (ECONNRESET);
946 }
947 if (sopt->sopt_level != IPPROTO_TCP) {
948 #if INET6
949 if (INP_CHECK_SOCKAF(so, AF_INET6))
950 error = ip6_ctloutput(so, sopt);
951 else
952 #endif /* INET6 */
953 error = ip_ctloutput(so, sopt);
954 return (error);
955 }
956 tp = intotcpcb(inp);
957 if (tp == NULL) {
958 return (ECONNRESET);
959 }
960
961 switch (sopt->sopt_dir) {
962 case SOPT_SET:
963 switch (sopt->sopt_name) {
964 case TCP_NODELAY:
965 case TCP_NOOPT:
966 case TCP_NOPUSH:
967 error = sooptcopyin(sopt, &optval, sizeof optval,
968 sizeof optval);
969 if (error)
970 break;
971
972 switch (sopt->sopt_name) {
973 case TCP_NODELAY:
974 opt = TF_NODELAY;
975 break;
976 case TCP_NOOPT:
977 opt = TF_NOOPT;
978 break;
979 case TCP_NOPUSH:
980 opt = TF_NOPUSH;
981 break;
982 default:
983 opt = 0; /* dead code to fool gcc */
984 break;
985 }
986
987 if (optval)
988 tp->t_flags |= opt;
989 else
990 tp->t_flags &= ~opt;
991 break;
992
993 case TCP_MAXSEG:
994 error = sooptcopyin(sopt, &optval, sizeof optval,
995 sizeof optval);
996 if (error)
997 break;
998
999 if (optval > 0 && optval <= tp->t_maxseg &&
1000 optval + 40 >= tcp_minmss)
1001 tp->t_maxseg = optval;
1002 else
1003 error = EINVAL;
1004 break;
1005
1006 case TCP_KEEPALIVE:
1007 error = sooptcopyin(sopt, &optval, sizeof optval,
1008 sizeof optval);
1009 if (error)
1010 break;
1011 if (optval < 0)
1012 error = EINVAL;
1013 else
1014 tp->t_keepidle = optval * PR_SLOWHZ;
1015 break;
1016
1017 default:
1018 error = ENOPROTOOPT;
1019 break;
1020 }
1021 break;
1022
1023 case SOPT_GET:
1024 switch (sopt->sopt_name) {
1025 case TCP_NODELAY:
1026 optval = tp->t_flags & TF_NODELAY;
1027 break;
1028 case TCP_MAXSEG:
1029 optval = tp->t_maxseg;
1030 break;
1031 case TCP_KEEPALIVE:
1032 optval = tp->t_keepidle / PR_SLOWHZ;
1033 break;
1034 case TCP_NOOPT:
1035 optval = tp->t_flags & TF_NOOPT;
1036 break;
1037 case TCP_NOPUSH:
1038 optval = tp->t_flags & TF_NOPUSH;
1039 break;
1040 default:
1041 error = ENOPROTOOPT;
1042 break;
1043 }
1044 if (error == 0)
1045 error = sooptcopyout(sopt, &optval, sizeof optval);
1046 break;
1047 }
1048 return (error);
1049 }
1050
1051 /*
1052 * tcp_sendspace and tcp_recvspace are the default send and receive window
1053 * sizes, respectively. These are obsolescent (this information should
1054 * be set by the route).
1055 */
1056 u_long tcp_sendspace = 1024*16;
1057 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1058 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1059 u_long tcp_recvspace = 1024*16;
1060 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1061 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1062
1063 __private_extern__ int tcp_sockthreshold = 256;
1064 SYSCTL_INT(_net_inet_tcp, OID_AUTO, sockthreshold, CTLFLAG_RW,
1065 &tcp_sockthreshold , 0, "TCP Socket size increased if less than threshold");
1066
1067 #define TCP_INCREASED_SPACE 65535 /* Automatically increase tcp send/rcv space to this value */
1068 /*
1069 * Attach TCP protocol to socket, allocating
1070 * internet protocol control block, tcp control block,
1071 * bufer space, and entering LISTEN state if to accept connections.
1072 */
1073 static int
1074 tcp_attach(so, p)
1075 struct socket *so;
1076 struct proc *p;
1077 {
1078 register struct tcpcb *tp;
1079 struct inpcb *inp;
1080 int error;
1081 #if INET6
1082 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1083 #endif
1084
1085 error = in_pcballoc(so, &tcbinfo, p);
1086 if (error)
1087 return (error);
1088
1089 inp = sotoinpcb(so);
1090
1091 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1092 /*
1093 * The goal is to let clients have large send/rcv default windows (TCP_INCREASED_SPACE)
1094 * while not hogging mbuf space for servers. This is done by watching a threshold
1095 * of tcpcbs in use and bumping the default send and rcvspace only if under that threshold.
1096 * The theory being that busy servers have a lot more active tcpcbs and don't want the potential
1097 * memory penalty of having much larger sockbuffs. The sysctl allows to fine tune that threshold value. */
1098
1099 if (inp->inp_pcbinfo->ipi_count < tcp_sockthreshold)
1100 error = soreserve(so, MAX(TCP_INCREASED_SPACE, tcp_sendspace), MAX(TCP_INCREASED_SPACE,tcp_recvspace));
1101 else
1102 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1103 if (error)
1104 return (error);
1105 }
1106
1107 #if INET6
1108 if (isipv6) {
1109 inp->inp_vflag |= INP_IPV6;
1110 inp->in6p_hops = -1; /* use kernel default */
1111 }
1112 else
1113 #endif /* INET6 */
1114 inp->inp_vflag |= INP_IPV4;
1115 tp = tcp_newtcpcb(inp);
1116 if (tp == 0) {
1117 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1118
1119 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1120 #if INET6
1121 if (isipv6)
1122 in6_pcbdetach(inp);
1123 else
1124 #endif /* INET6 */
1125 in_pcbdetach(inp);
1126 so->so_state |= nofd;
1127 return (ENOBUFS);
1128 }
1129 tp->t_state = TCPS_CLOSED;
1130 return (0);
1131 }
1132
1133 /*
1134 * Initiate (or continue) disconnect.
1135 * If embryonic state, just send reset (once).
1136 * If in ``let data drain'' option and linger null, just drop.
1137 * Otherwise (hard), mark socket disconnecting and drop
1138 * current input data; switch states based on user close, and
1139 * send segment to peer (with FIN).
1140 */
1141 static struct tcpcb *
1142 tcp_disconnect(tp)
1143 register struct tcpcb *tp;
1144 {
1145 struct socket *so = tp->t_inpcb->inp_socket;
1146
1147 if (tp->t_state < TCPS_ESTABLISHED)
1148 tp = tcp_close(tp);
1149 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1150 tp = tcp_drop(tp, 0);
1151 else {
1152 soisdisconnecting(so);
1153 sbflush(&so->so_rcv);
1154 tp = tcp_usrclosed(tp);
1155 if (tp)
1156 (void) tcp_output(tp);
1157 }
1158 return (tp);
1159 }
1160
1161 /*
1162 * User issued close, and wish to trail through shutdown states:
1163 * if never received SYN, just forget it. If got a SYN from peer,
1164 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1165 * If already got a FIN from peer, then almost done; go to LAST_ACK
1166 * state. In all other cases, have already sent FIN to peer (e.g.
1167 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1168 * for peer to send FIN or not respond to keep-alives, etc.
1169 * We can let the user exit from the close as soon as the FIN is acked.
1170 */
1171 static struct tcpcb *
1172 tcp_usrclosed(tp)
1173 register struct tcpcb *tp;
1174 {
1175
1176 switch (tp->t_state) {
1177
1178 case TCPS_CLOSED:
1179 case TCPS_LISTEN:
1180 tp->t_state = TCPS_CLOSED;
1181 tp = tcp_close(tp);
1182 break;
1183
1184 case TCPS_SYN_SENT:
1185 case TCPS_SYN_RECEIVED:
1186 tp->t_flags |= TF_NEEDFIN;
1187 break;
1188
1189 case TCPS_ESTABLISHED:
1190 tp->t_state = TCPS_FIN_WAIT_1;
1191 break;
1192
1193 case TCPS_CLOSE_WAIT:
1194 tp->t_state = TCPS_LAST_ACK;
1195 break;
1196 }
1197 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1198 soisdisconnected(tp->t_inpcb->inp_socket);
1199 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1200 if (tp->t_state == TCPS_FIN_WAIT_2)
1201 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1202 }
1203 return (tp);
1204 }
1205