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