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