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