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