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