]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_usrreq.c
xnu-201.42.3.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_usrreq.c
CommitLineData
1c79356b
A
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 }
0b4e3aa0
A
731 if ((inp->inp_laddr.s_addr == INADDR_ANY ? ifaddr->sin_addr.s_addr :
732 inp->inp_laddr.s_addr) == sin->sin_addr.s_addr &&
733 inp->inp_lport == sin->sin_port)
734 return EINVAL;
1c79356b
A
735 if (inp->inp_laddr.s_addr == INADDR_ANY)
736 inp->inp_laddr = ifaddr->sin_addr;
737 inp->inp_faddr = sin->sin_addr;
738 inp->inp_fport = sin->sin_port;
739 in_pcbrehash(inp);
740
741 tp->t_template = tcp_template(tp);
742 if (tp->t_template == 0) {
743 in_pcbdisconnect(inp);
744 return ENOBUFS;
745 }
746
747 /* Compute window scaling to request. */
748 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
749 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
750 tp->request_r_scale++;
751
752 soisconnecting(so);
753 tcpstat.tcps_connattempt++;
754 tp->t_state = TCPS_SYN_SENT;
755 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
0b4e3aa0
A
756#ifdef TCP_COMPAT_42
757 tp->iss = tcp_iss;
758 tcp_iss =+ TCP_ISSINCR/2;
759#else /* TCP_COMPAT_42 */
760 tp->iss = tcp_rndiss_next();
761#endif /* !TCP_COMPAT_42 */
762
1c79356b
A
763 tcp_sendseqinit(tp);
764
765 /*
766 * Generate a CC value for this connection and
767 * check whether CC or CCnew should be used.
768 */
769 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
770 taop = &tao_noncached;
771 bzero(taop, sizeof(*taop));
772 }
773
774 tp->cc_send = CC_INC(tcp_ccgen);
775 if (taop->tao_ccsent != 0 &&
776 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
777 taop->tao_ccsent = tp->cc_send;
778 } else {
779 taop->tao_ccsent = 0;
780 tp->t_flags |= TF_SENDCCNEW;
781 }
782
783 return 0;
784}
785
786#if INET6
787static int
788tcp6_connect(tp, nam, p)
789 register struct tcpcb *tp;
790 struct sockaddr *nam;
791 struct proc *p;
792{
793 struct inpcb *inp = tp->t_inpcb, *oinp;
794 struct socket *so = inp->inp_socket;
795 struct tcpcb *otp;
796 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
797 struct in6_addr *addr6;
798 struct rmxp_tao *taop;
799 struct rmxp_tao tao_noncached;
800 int error;
801
802 if (inp->inp_lport == 0) {
803 error = in6_pcbbind(inp, (struct sockaddr *)0, p);
804 if (error)
805 return error;
806 }
807
808 /*
809 * Cannot simply call in_pcbconnect, because there might be an
810 * earlier incarnation of this same connection still in
811 * TIME_WAIT state, creating an ADDRINUSE error.
812 */
813 error = in6_pcbladdr(inp, nam, &addr6);
814 if (error)
815 return error;
816 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
817 &sin6->sin6_addr, sin6->sin6_port,
818 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
819 ? addr6
820 : &inp->in6p_laddr,
821 inp->inp_lport, 0, NULL);
822 if (oinp) {
823 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
824 otp->t_state == TCPS_TIME_WAIT &&
825 otp->t_duration < TCPTV_MSL &&
826 (otp->t_flags & TF_RCVD_CC))
827 otp = tcp_close(otp);
828 else
829 return EADDRINUSE;
830 }
831 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
832 inp->in6p_laddr = *addr6;
833 inp->in6p_faddr = sin6->sin6_addr;
834 inp->inp_fport = sin6->sin6_port;
835 /*
836 * xxx kazu flowlabel is necessary for connect?
837 * but if this line is missing, the garbage value remains.
838 */
839 inp->in6p_flowinfo = sin6->sin6_flowinfo;
840
841 in_pcbrehash(inp);
842
843 tp->t_template = tcp_template(tp);
844 if (tp->t_template == 0) {
845 in6_pcbdisconnect(inp);
846 return ENOBUFS;
847 }
848
849 /* Compute window scaling to request. */
850 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
851 (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
852 tp->request_r_scale++;
853
854 soisconnecting(so);
855 tcpstat.tcps_connattempt++;
856 tp->t_state = TCPS_SYN_SENT;
857 tp->t_timer[TCPT_KEEP] = tcp_keepinit;
0b4e3aa0 858#ifdef TCP_COMPAT_42
1c79356b 859 tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
0b4e3aa0
A
860#else
861 tp->iss = tcp_rndiss_next();
862#endif /* TCP_COMPAT_42 */
1c79356b
A
863 tcp_sendseqinit(tp);
864
865 /*
866 * Generate a CC value for this connection and
867 * check whether CC or CCnew should be used.
868 */
869 if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
870 taop = &tao_noncached;
871 bzero(taop, sizeof(*taop));
872 }
873
874 tp->cc_send = CC_INC(tcp_ccgen);
875 if (taop->tao_ccsent != 0 &&
876 CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
877 taop->tao_ccsent = tp->cc_send;
878 } else {
879 taop->tao_ccsent = 0;
880 tp->t_flags |= TF_SENDCCNEW;
881 }
882
883 return 0;
884}
885#endif /* INET6 */
886
887/*
888 * The new sockopt interface makes it possible for us to block in the
889 * copyin/out step (if we take a page fault). Taking a page fault at
890 * splnet() is probably a Bad Thing. (Since sockets and pcbs both now
891 * use TSM, there probably isn't any need for this function to run at
892 * splnet() any more. This needs more examination.)
893 */
894int
895tcp_ctloutput(so, sopt)
896 struct socket *so;
897 struct sockopt *sopt;
898{
899 int error, opt, optval, s;
900 struct inpcb *inp;
901 struct tcpcb *tp;
902
903 error = 0;
904 s = splnet(); /* XXX */
905 inp = sotoinpcb(so);
906 if (inp == NULL) {
907 splx(s);
908 return (ECONNRESET);
909 }
910 if (sopt->sopt_level != IPPROTO_TCP) {
911#if INET6
912 if (INP_CHECK_SOCKAF(so, AF_INET6))
913 error = ip6_ctloutput(so, sopt);
914 else
915#endif /* INET6 */
916 error = ip_ctloutput(so, sopt);
917 splx(s);
918 return (error);
919 }
920 tp = intotcpcb(inp);
921 if (tp == NULL) {
922 splx(s);
923 return (ECONNRESET);
924 }
925
926 switch (sopt->sopt_dir) {
927 case SOPT_SET:
928 switch (sopt->sopt_name) {
929 case TCP_NODELAY:
930 case TCP_NOOPT:
931 case TCP_NOPUSH:
932 error = sooptcopyin(sopt, &optval, sizeof optval,
933 sizeof optval);
934 if (error)
935 break;
936
937 switch (sopt->sopt_name) {
938 case TCP_NODELAY:
939 opt = TF_NODELAY;
940 break;
941 case TCP_NOOPT:
942 opt = TF_NOOPT;
943 break;
944 case TCP_NOPUSH:
945 opt = TF_NOPUSH;
946 break;
947 default:
948 opt = 0; /* dead code to fool gcc */
949 break;
950 }
951
952 if (optval)
953 tp->t_flags |= opt;
954 else
955 tp->t_flags &= ~opt;
956 break;
957
958 case TCP_MAXSEG:
959 error = sooptcopyin(sopt, &optval, sizeof optval,
960 sizeof optval);
961 if (error)
962 break;
963
964 if (optval > 0 && optval <= tp->t_maxseg)
965 tp->t_maxseg = optval;
966 else
967 error = EINVAL;
968 break;
969
970 default:
971 error = ENOPROTOOPT;
972 break;
973 }
974 break;
975
976 case SOPT_GET:
977 switch (sopt->sopt_name) {
978 case TCP_NODELAY:
979 optval = tp->t_flags & TF_NODELAY;
980 break;
981 case TCP_MAXSEG:
982 optval = tp->t_maxseg;
983 break;
984 case TCP_NOOPT:
985 optval = tp->t_flags & TF_NOOPT;
986 break;
987 case TCP_NOPUSH:
988 optval = tp->t_flags & TF_NOPUSH;
989 break;
990 default:
991 error = ENOPROTOOPT;
992 break;
993 }
994 if (error == 0)
995 error = sooptcopyout(sopt, &optval, sizeof optval);
996 break;
997 }
998 splx(s);
999 return (error);
1000}
1001
1002/*
1003 * tcp_sendspace and tcp_recvspace are the default send and receive window
1004 * sizes, respectively. These are obsolescent (this information should
1005 * be set by the route).
1006 */
1007u_long tcp_sendspace = 1024*16;
1008SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
1009 CTLFLAG_RW, &tcp_sendspace , 0, "");
1010u_long tcp_recvspace = 1024*16;
1011SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
1012 CTLFLAG_RW, &tcp_recvspace , 0, "");
1013
1014/*
1015 * Attach TCP protocol to socket, allocating
1016 * internet protocol control block, tcp control block,
1017 * bufer space, and entering LISTEN state if to accept connections.
1018 */
1019static int
1020tcp_attach(so, p)
1021 struct socket *so;
1022 struct proc *p;
1023{
1024 register struct tcpcb *tp;
1025 struct inpcb *inp;
1026 int error;
1027#if INET6
1028 int isipv6 = INP_CHECK_SOCKAF(so, AF_INET) == NULL;
1029#endif /* INET6 */
1030
1031 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1032 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1033 if (error)
1034 return (error);
1035 }
1036 error = in_pcballoc(so, &tcbinfo, p);
1037 if (error)
1038 return (error);
1039 inp = sotoinpcb(so);
1040#if IPSEC
1041 error = ipsec_init_policy(so, &inp->inp_sp);
1042 if (error) {
1043#if INET6
1044 if (isipv6)
1045 in6_pcbdetach(inp);
1046 else
1047#endif /* INET6 */
1048 in_pcbdetach(inp);
1049 return (error);
1050 }
1051#endif /*IPSEC*/
1052#if INET6
1053 if (isipv6) {
1054 inp->inp_vflag |= INP_IPV6;
1055 inp->in6p_hops = -1; /* use kernel default */
1056 }
1057 else
1058#endif /* INET6 */
1059 inp->inp_vflag |= INP_IPV4;
1060 tp = tcp_newtcpcb(inp);
1061 if (tp == 0) {
1062 int nofd = so->so_state & SS_NOFDREF; /* XXX */
1063
1064 so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
1065#if INET6
1066 if (isipv6)
1067 in6_pcbdetach(inp);
1068 else
1069#endif /* INET6 */
1070 in_pcbdetach(inp);
1071 so->so_state |= nofd;
1072 return (ENOBUFS);
1073 }
1074 tp->t_state = TCPS_CLOSED;
1075 return (0);
1076}
1077
1078/*
1079 * Initiate (or continue) disconnect.
1080 * If embryonic state, just send reset (once).
1081 * If in ``let data drain'' option and linger null, just drop.
1082 * Otherwise (hard), mark socket disconnecting and drop
1083 * current input data; switch states based on user close, and
1084 * send segment to peer (with FIN).
1085 */
1086static struct tcpcb *
1087tcp_disconnect(tp)
1088 register struct tcpcb *tp;
1089{
1090 struct socket *so = tp->t_inpcb->inp_socket;
1091
1092 if (tp->t_state < TCPS_ESTABLISHED)
1093 tp = tcp_close(tp);
1094 else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
1095 tp = tcp_drop(tp, 0);
1096 else {
1097 soisdisconnecting(so);
1098 sbflush(&so->so_rcv);
1099 tp = tcp_usrclosed(tp);
1100 if (tp)
1101 (void) tcp_output(tp);
1102 }
1103 return (tp);
1104}
1105
1106/*
1107 * User issued close, and wish to trail through shutdown states:
1108 * if never received SYN, just forget it. If got a SYN from peer,
1109 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1110 * If already got a FIN from peer, then almost done; go to LAST_ACK
1111 * state. In all other cases, have already sent FIN to peer (e.g.
1112 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1113 * for peer to send FIN or not respond to keep-alives, etc.
1114 * We can let the user exit from the close as soon as the FIN is acked.
1115 */
1116static struct tcpcb *
1117tcp_usrclosed(tp)
1118 register struct tcpcb *tp;
1119{
1120
1121 switch (tp->t_state) {
1122
1123 case TCPS_CLOSED:
1124 case TCPS_LISTEN:
1125 tp->t_state = TCPS_CLOSED;
1126 tp = tcp_close(tp);
1127 break;
1128
1129 case TCPS_SYN_SENT:
1130 case TCPS_SYN_RECEIVED:
1131 tp->t_flags |= TF_NEEDFIN;
1132 break;
1133
1134 case TCPS_ESTABLISHED:
1135 tp->t_state = TCPS_FIN_WAIT_1;
1136 break;
1137
1138 case TCPS_CLOSE_WAIT:
1139 tp->t_state = TCPS_LAST_ACK;
1140 break;
1141 }
1142 if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1143 soisdisconnected(tp->t_inpcb->inp_socket);
1144 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1145 if (tp->t_state == TCPS_FIN_WAIT_2)
1146 tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1147 }
1148 return (tp);
1149}
1150