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