source: rtems/cpukit/libnetworking/netinet/tcp_usrreq.c @ 6e401331

4.104.115
Last change on this file since 6e401331 was b25b88e7, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/10 at 05:50:29

Add HAVE_CONFIG_H support to let files receive configure defines.

  • Property mode set to 100644
File size: 19.7 KB
Line 
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 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *      From: @(#)tcp_usrreq.c  8.2 (Berkeley) 1/3/94
30 * $FreeBSD: src/sys/netinet/tcp_usrreq.c,v 1.120 2005/05/01 14:01:38 rwatson Exp $
31 */
32 
33/*
34 *      $Id$
35 */
36
37#ifdef HAVE_CONFIG_H
38#include "config.h"
39#endif
40
41#include "opt_tcpdebug.h"
42
43#include <sys/param.h>
44#include <rtems/bsd/sys/queue.h>
45#include <sys/systm.h>
46#include <sys/kernel.h>
47#include <sys/sysctl.h>
48#include <sys/malloc.h>
49#include <sys/mbuf.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/protosw.h>
53#include <errno.h>
54#include <sys/stat.h>
55
56#include <net/if.h>
57#include <net/route.h>
58
59#include <netinet/in.h>
60#include <netinet/in_systm.h>
61#include <netinet/ip.h>
62#include <netinet/in_pcb.h>
63#include <netinet/in_var.h>
64#include <netinet/ip_var.h>
65#include <netinet/tcp.h>
66#include <netinet/tcp_fsm.h>
67#include <netinet/tcp_seq.h>
68#include <netinet/tcp_timer.h>
69#include <netinet/tcp_var.h>
70#include <netinet/tcpip.h>
71#ifdef TCPDEBUG
72#include <netinet/tcp_debug.h>
73#endif
74
75/*
76 * TCP protocol interface to socket abstraction.
77 */
78extern  char *tcpstates[];
79
80static int      tcp_attach(struct socket *);
81static int      tcp_connect(struct tcpcb *, struct mbuf *);
82static struct tcpcb *
83                tcp_disconnect(struct tcpcb *);
84static struct tcpcb *
85                tcp_usrclosed(struct tcpcb *);
86
87#ifdef TCPDEBUG
88#define TCPDEBUG0       int ostate
89#define TCPDEBUG1()     ostate = tp ? tp->t_state : 0
90#define TCPDEBUG2(req)  if (tp && (so->so_options & SO_DEBUG)) \
91                                tcp_trace(TA_USER, ostate, tp, 0, req)
92#else
93#define TCPDEBUG0
94#define TCPDEBUG1()
95#define TCPDEBUG2(req)
96#endif
97
98/*
99 * TCP attaches to socket via pru_attach(), reserving space,
100 * and an internet control block.
101 */
102static int
103tcp_usr_attach(struct socket *so, intptr_t proto)
104{
105        int s = splnet();
106        int error;
107        struct inpcb *inp = sotoinpcb(so);
108        struct tcpcb *tp = 0;
109        TCPDEBUG0;
110
111        TCPDEBUG1();
112        if (inp) {
113                error = EISCONN;
114                goto out;
115        }
116
117        error = tcp_attach(so);
118        if (error)
119                goto out;
120
121        if ((so->so_options & SO_LINGER) && so->so_linger == 0)
122                so->so_linger = TCP_LINGERTIME * hz;
123        tp = sototcpcb(so);
124out:
125        TCPDEBUG2(PRU_ATTACH);
126        splx(s);
127        return error;
128}
129
130/*
131 * pru_detach() detaches the TCP protocol from the socket.
132 * If the protocol state is non-embryonic, then can't
133 * do this directly: have to initiate a pru_disconnect(),
134 * which may finish later; embryonic TCB's can just
135 * be discarded here.
136 */
137static int
138tcp_usr_detach(struct socket *so)
139{
140        int s = splnet();
141        int error = 0;
142        struct inpcb *inp = sotoinpcb(so);
143        struct tcpcb *tp;
144        TCPDEBUG0;
145
146        if (inp == 0) {
147                splx(s);
148                return EINVAL;  /* XXX */
149        }
150        tp = intotcpcb(inp);
151        TCPDEBUG1();
152        if (tp->t_state > TCPS_LISTEN)
153                tp = tcp_disconnect(tp);
154        else
155                tp = tcp_close(tp);
156
157        TCPDEBUG2(PRU_DETACH);
158        splx(s);
159        return error;
160}
161
162#define COMMON_START()  TCPDEBUG0; \
163                        do { \
164                                     if (inp == 0) { \
165                                             splx(s); \
166                                             return EINVAL; \
167                                     } \
168                                     tp = intotcpcb(inp); \
169                                     TCPDEBUG1(); \
170                     } while(0)
171                             
172#define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
173
174
175/*
176 * Give the socket an address.
177 */
178static int
179tcp_usr_bind(struct socket *so, struct mbuf *nam)
180{
181        int s = splnet();
182        int error = 0;
183        struct inpcb *inp = sotoinpcb(so);
184        struct tcpcb *tp;
185        struct sockaddr_in *sinp;
186
187        COMMON_START();
188
189        /*
190         * Must check for multicast addresses and disallow binding
191         * to them.
192         */
193        sinp = mtod(nam, struct sockaddr_in *);
194        if (sinp->sin_family == AF_INET &&
195            IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
196                error = EAFNOSUPPORT;
197                goto out;
198        }
199        error = in_pcbbind(inp, nam);
200        if (error)
201                goto out;
202        COMMON_END(PRU_BIND);
203
204}
205
206/*
207 * Prepare to accept connections.
208 */
209static int
210tcp_usr_listen(struct socket *so)
211{
212        int s = splnet();
213        int error = 0;
214        struct inpcb *inp = sotoinpcb(so);
215        struct tcpcb *tp;
216
217        COMMON_START();
218        if (inp->inp_lport == 0)
219                error = in_pcbbind(inp, NULL);
220        if (error == 0)
221                tp->t_state = TCPS_LISTEN;
222        COMMON_END(PRU_LISTEN);
223}
224
225/*
226 * Initiate connection to peer.
227 * Create a template for use in transmissions on this connection.
228 * Enter SYN_SENT state, and mark socket as connecting.
229 * Start keep-alive timer, and seed output sequence space.
230 * Send initial segment on connection.
231 */
232static int
233tcp_usr_connect(struct socket *so, struct mbuf *nam)
234{
235        int s = splnet();
236        int error = 0;
237        struct inpcb *inp = sotoinpcb(so);
238        struct tcpcb *tp;
239        struct sockaddr_in *sinp;
240
241        COMMON_START();
242
243        /*
244         * Must disallow TCP ``connections'' to multicast addresses.
245         */
246        sinp = mtod(nam, struct sockaddr_in *);
247        if (sinp->sin_family == AF_INET
248            && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
249                error = EAFNOSUPPORT;
250                goto out;
251        }
252
253        if ((error = tcp_connect(tp, nam)) != 0)
254                goto out;
255        error = tcp_output(tp);
256        COMMON_END(PRU_CONNECT);
257}
258
259/*
260 * Initiate disconnect from peer.
261 * If connection never passed embryonic stage, just drop;
262 * else if don't need to let data drain, then can just drop anyways,
263 * else have to begin TCP shutdown process: mark socket disconnecting,
264 * drain unread data, state switch to reflect user close, and
265 * send segment (e.g. FIN) to peer.  Socket will be really disconnected
266 * when peer sends FIN and acks ours.
267 *
268 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
269 */
270static int
271tcp_usr_disconnect(struct socket *so)
272{
273        int s = splnet();
274        int error = 0;
275        struct inpcb *inp = sotoinpcb(so);
276        struct tcpcb *tp;
277
278        COMMON_START();
279        tp = tcp_disconnect(tp);
280        COMMON_END(PRU_DISCONNECT);
281}
282
283/*
284 * Accept a connection.  Essentially all the work is
285 * done at higher levels; just return the address
286 * of the peer, storing through addr.
287 */
288static int
289tcp_usr_accept(struct socket *so, struct mbuf *nam)
290{
291        int s = splnet();
292        int error = 0;
293        struct inpcb *inp = sotoinpcb(so);
294        struct tcpcb *tp;
295
296        COMMON_START();
297        in_setpeeraddr(inp, nam);
298        COMMON_END(PRU_ACCEPT);
299}
300
301/*
302 * Mark the connection as being incapable of further output.
303 */
304static int
305tcp_usr_shutdown(struct socket *so)
306{
307        int s = splnet();
308        int error = 0;
309        struct inpcb *inp = sotoinpcb(so);
310        struct tcpcb *tp;
311
312        COMMON_START();
313        socantsendmore(so);
314        tp = tcp_usrclosed(tp);
315        if (tp)
316                error = tcp_output(tp);
317        COMMON_END(PRU_SHUTDOWN);
318}
319
320/*
321 * After a receive, possibly send window update to peer.
322 */
323static int
324tcp_usr_rcvd(struct socket *so, intptr_t flags)
325{
326        int s = splnet();
327        int error = 0;
328        struct inpcb *inp = sotoinpcb(so);
329        struct tcpcb *tp;
330
331        COMMON_START();
332        tcp_output(tp);
333        COMMON_END(PRU_RCVD);
334}
335
336/*
337 * Do a send by putting data in output queue and updating urgent
338 * marker if URG set.  Possibly send more data.
339 */
340static int
341tcp_usr_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam,
342             struct mbuf *control)
343{
344        int s = splnet();
345        int error = 0;
346        struct inpcb *inp = sotoinpcb(so);
347        struct tcpcb *tp;
348
349        COMMON_START();
350        if (control && control->m_len) {
351                m_freem(control); /* XXX shouldn't caller do this??? */
352                if (m)
353                        m_freem(m);
354                error = EINVAL;
355                goto out;
356        }
357
358        if(!(flags & PRUS_OOB)) {
359                sbappend(&so->so_snd, m);
360                if (nam && tp->t_state < TCPS_SYN_SENT) {
361                        /*
362                         * Do implied connect if not yet connected,
363                         * initialize window to default value, and
364                         * initialize maxseg/maxopd using peer's cached
365                         * MSS.
366                         */
367                        error = tcp_connect(tp, nam);
368                        if (error)
369                                goto out;
370                        tp->snd_wnd = TTCP_CLIENT_SND_WND;
371                        tcp_mss(tp, -1);
372                }
373
374                if (flags & PRUS_EOF) {
375                        /*
376                         * Close the send side of the connection after
377                         * the data is sent.
378                         */
379                        socantsendmore(so);
380                        tp = tcp_usrclosed(tp);
381                }
382                if (tp != NULL)
383                        error = tcp_output(tp);
384        } else {
385                if (sbspace(&so->so_snd) < -512) {
386                        m_freem(m);
387                        error = ENOBUFS;
388                        goto out;
389                }
390                /*
391                 * According to RFC961 (Assigned Protocols),
392                 * the urgent pointer points to the last octet
393                 * of urgent data.  We continue, however,
394                 * to consider it to indicate the first octet
395                 * of data past the urgent section.
396                 * Otherwise, snd_up should be one lower.
397                 */
398                sbappend(&so->so_snd, m);
399                if (nam && tp->t_state < TCPS_SYN_SENT) {
400                        /*
401                         * Do implied connect if not yet connected,
402                         * initialize window to default value, and
403                         * initialize maxseg/maxopd using peer's cached
404                         * MSS.
405                         */
406                        error = tcp_connect(tp, nam);
407                        if (error)
408                                goto out;
409                        tp->snd_wnd = TTCP_CLIENT_SND_WND;
410                        tcp_mss(tp, -1);
411                }
412                tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
413                tp->t_force = 1;
414                error = tcp_output(tp);
415                tp->t_force = 0;
416        }
417        COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB :
418                   ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
419}
420
421/*
422 * Abort the TCP.
423 */
424static int
425tcp_usr_abort(struct socket *so)
426{
427        int s = splnet();
428        int error = 0;
429        struct inpcb *inp = sotoinpcb(so);
430        struct tcpcb *tp;
431
432        COMMON_START();
433        tp = tcp_drop(tp, ECONNABORTED);
434        COMMON_END(PRU_ABORT);
435}
436
437/*
438 * Fill in st_bklsize for fstat() operations on a socket.
439 */
440static int
441tcp_usr_sense(struct socket *so, struct stat *sb)
442{
443        int s = splnet();
444
445        sb->st_blksize = so->so_snd.sb_hiwat;
446        splx(s);
447        return 0;
448}
449
450/*
451 * Receive out-of-band data.
452 */
453static int
454tcp_usr_rcvoob(struct socket *so, struct mbuf *m, intptr_t flags)
455{
456        int s = splnet();
457        int error = 0;
458        struct inpcb *inp = sotoinpcb(so);
459        struct tcpcb *tp;
460
461        COMMON_START();
462        if ((so->so_oobmark == 0 &&
463             (so->so_state & SS_RCVATMARK) == 0) ||
464            so->so_options & SO_OOBINLINE ||
465            tp->t_oobflags & TCPOOB_HADDATA) {
466                error = EINVAL;
467                goto out;
468        }
469        if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
470                error = EWOULDBLOCK;
471                goto out;
472        }
473        m->m_len = 1;
474        *mtod(m, caddr_t) = tp->t_iobc;
475        if ((flags & MSG_PEEK) == 0)
476                tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
477        COMMON_END(PRU_RCVOOB);
478}
479
480static int
481tcp_usr_sockaddr(struct socket *so, struct mbuf *nam)
482{
483        int s = splnet();
484        int error = 0;
485        struct inpcb *inp = sotoinpcb(so);
486        struct tcpcb *tp;
487
488        COMMON_START();
489        in_setsockaddr(inp, nam);
490        COMMON_END(PRU_SOCKADDR);
491}
492
493static int
494tcp_usr_peeraddr(struct socket *so, struct mbuf *nam)
495{
496        int s = splnet();
497        int error = 0;
498        struct inpcb *inp = sotoinpcb(so);
499        struct tcpcb *tp;
500
501        COMMON_START();
502        in_setpeeraddr(inp, nam);
503        COMMON_END(PRU_PEERADDR);
504}
505
506/*
507 * XXX - this should just be a call to in_control, but we need to get
508 * the types worked out.
509 */
510static int
511tcp_usr_control(struct socket *so, intptr_t cmd, caddr_t arg, struct ifnet *ifp)
512{
513        return in_control(so, cmd, arg, ifp);
514}
515
516/* xxx - should be const */
517struct pr_usrreqs tcp_usrreqs = {
518        tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
519        tcp_usr_connect, pru_connect2_notsupp, tcp_usr_control, tcp_usr_detach,
520        tcp_usr_disconnect, tcp_usr_listen, tcp_usr_peeraddr, tcp_usr_rcvd,
521        tcp_usr_rcvoob, tcp_usr_send, tcp_usr_sense, tcp_usr_shutdown,
522        tcp_usr_sockaddr
523};
524
525/*
526 * Common subroutine to open a TCP connection to remote host specified
527 * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
528 * port number if needed.  Call in_pcbladdr to do the routing and to choose
529 * a local host address (interface).  If there is an existing incarnation
530 * of the same connection in TIME-WAIT state and if the remote host was
531 * sending CC options and if the connection duration was < MSL, then
532 * truncate the previous TIME-WAIT state and proceed.
533 * Initialize connection parameters and enter SYN-SENT state.
534 */
535static int
536tcp_connect(struct tcpcb *tp, struct mbuf *nam)
537{
538        struct inpcb *inp = tp->t_inpcb, *oinp;
539        struct socket *so = inp->inp_socket;
540        struct tcpcb *otp;
541        struct sockaddr_in *sin = mtod(nam, struct sockaddr_in *);
542        struct sockaddr_in *ifaddr;
543        int error;
544        struct rmxp_tao *taop;
545        struct rmxp_tao tao_noncached;
546
547        if (inp->inp_lport == 0) {
548                error = in_pcbbind(inp, NULL);
549                if (error)
550                        return error;
551        }
552
553        /*
554         * Cannot simply call in_pcbconnect, because there might be an
555         * earlier incarnation of this same connection still in
556         * TIME_WAIT state, creating an ADDRINUSE error.
557         */
558        error = in_pcbladdr(inp, nam, &ifaddr);
559        if (error)
560                return error;
561        oinp = in_pcblookuphash(inp->inp_pcbinfo,
562            sin->sin_addr, sin->sin_port,
563            inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
564                                                : ifaddr->sin_addr,
565            inp->inp_lport,  0);
566        if (oinp) {
567                if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
568                otp->t_state == TCPS_TIME_WAIT &&
569                    otp->t_duration < TCPTV_MSL &&
570                    (otp->t_flags & TF_RCVD_CC))
571                        otp = tcp_close(otp);
572                else
573                        return EADDRINUSE;
574        }
575        if (inp->inp_laddr.s_addr == INADDR_ANY)
576                inp->inp_laddr = ifaddr->sin_addr;
577        inp->inp_faddr = sin->sin_addr;
578        inp->inp_fport = sin->sin_port;
579        in_pcbrehash(inp);
580
581        tp->t_template = tcp_template(tp);
582        if (tp->t_template == 0) {
583                in_pcbdisconnect(inp);
584                return ENOBUFS;
585        }
586
587        /* Compute window scaling to request.  */
588        while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
589            (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
590                tp->request_r_scale++;
591
592        soisconnecting(so);
593        tcpstat.tcps_connattempt++;
594        tp->t_state = TCPS_SYN_SENT;
595        tp->t_timer[TCPT_KEEP] = tcp_keepinit;
596        tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2;
597        tcp_sendseqinit(tp);
598
599        /*
600         * Generate a CC value for this connection and
601         * check whether CC or CCnew should be used.
602         */
603        if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
604                taop = &tao_noncached;
605                bzero(taop, sizeof(*taop));
606        }
607
608        tp->cc_send = CC_INC(tcp_ccgen);
609        if (taop->tao_ccsent != 0 &&
610            CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
611                taop->tao_ccsent = tp->cc_send;
612        } else {
613                taop->tao_ccsent = 0;
614                tp->t_flags |= TF_SENDCCNEW;
615        }
616
617        return 0;
618}
619
620int
621tcp_ctloutput(int op, struct socket *so, int level, int optname,
622    struct mbuf **mp)
623{
624        int error = 0, s;
625        struct inpcb *inp;
626        register struct tcpcb *tp;
627        register struct mbuf *m;
628        register int i;
629
630        s = splnet();
631        inp = sotoinpcb(so);
632        if (inp == NULL) {
633                splx(s);
634                if (op == PRCO_SETOPT && *mp)
635                        (void) m_free(*mp);
636                return (ECONNRESET);
637        }
638        if (level != IPPROTO_TCP) {
639                error = ip_ctloutput(op, so, level, optname, mp);
640                splx(s);
641                return (error);
642        }
643        tp = intotcpcb(inp);
644
645        switch (op) {
646
647        case PRCO_SETOPT:
648                m = *mp;
649                switch (optname) {
650
651                case TCP_NODELAY:
652                        if (m == NULL || m->m_len < sizeof (int))
653                                error = EINVAL;
654                        else if (*mtod(m, int *))
655                                tp->t_flags |= TF_NODELAY;
656                        else
657                                tp->t_flags &= ~TF_NODELAY;
658                        break;
659
660                case TCP_MAXSEG:
661                        if (m && (i = *mtod(m, int *)) > 0 && i <= tp->t_maxseg)
662                                tp->t_maxseg = i;
663                        else
664                                error = EINVAL;
665                        break;
666
667                case TCP_NOOPT:
668                        if (m == NULL || m->m_len < sizeof (int))
669                                error = EINVAL;
670                        else if (*mtod(m, int *))
671                                tp->t_flags |= TF_NOOPT;
672                        else
673                                tp->t_flags &= ~TF_NOOPT;
674                        break;
675
676                case TCP_NOPUSH:
677                        if (m == NULL || m->m_len < sizeof (int))
678                                error = EINVAL;
679                        else if (*mtod(m, int *))
680                                tp->t_flags |= TF_NOPUSH;
681                        else
682                                tp->t_flags &= ~TF_NOPUSH;
683                        break;
684
685                default:
686                        error = ENOPROTOOPT;
687                        break;
688                }
689                if (m)
690                        (void) m_free(m);
691                break;
692
693        case PRCO_GETOPT:
694                *mp = m = m_get(M_WAIT, MT_SOOPTS);
695                m->m_len = sizeof(int);
696
697                switch (optname) {
698                case TCP_NODELAY:
699                        *mtod(m, int *) = tp->t_flags & TF_NODELAY;
700                        break;
701                case TCP_MAXSEG:
702                        *mtod(m, int *) = tp->t_maxseg;
703                        break;
704                case TCP_NOOPT:
705                        *mtod(m, int *) = tp->t_flags & TF_NOOPT;
706                        break;
707                case TCP_NOPUSH:
708                        *mtod(m, int *) = tp->t_flags & TF_NOPUSH;
709                        break;
710                default:
711                        error = ENOPROTOOPT;
712                        break;
713                }
714                break;
715        }
716        splx(s);
717        return (error);
718}
719
720/*
721 * tcp_sendspace and tcp_recvspace are the default send and receive window
722 * sizes, respectively.  These are obsolescent (this information should
723 * be set by the route).
724 */
725u_long  tcp_sendspace = 1024*16;
726SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace,
727        CTLFLAG_RW, &tcp_sendspace , 0, "");
728u_long  tcp_recvspace = 1024*16;
729SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace,
730        CTLFLAG_RW, &tcp_recvspace , 0, "");
731
732#if defined(__rtems__)
733void rtems_set_tcp_buffer_sizes(u_long sendspace, u_long recvspace)
734{
735    if ( sendspace != 0 )
736      tcp_sendspace = sendspace;
737    if ( recvspace != 0 )
738      tcp_recvspace = recvspace;
739}
740#endif
741
742/*
743 * Attach TCP protocol to socket, allocating
744 * internet protocol control block, tcp control block,
745 * bufer space, and entering LISTEN state if to accept connections.
746 */
747static int
748tcp_attach(struct socket *so)
749{
750        register struct tcpcb *tp;
751        struct inpcb *inp;
752        int error;
753
754        if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
755                error = soreserve(so, tcp_sendspace, tcp_recvspace);
756                if (error)
757                        return (error);
758        }
759        error = in_pcballoc(so, &tcbinfo);
760        if (error)
761                return (error);
762        inp = sotoinpcb(so);
763        tp = tcp_newtcpcb(inp);
764        if (tp == 0) {
765                int nofd = so->so_state & SS_NOFDREF;   /* XXX */
766
767                so->so_state &= ~SS_NOFDREF;    /* don't free the socket yet */
768                in_pcbdetach(inp);
769                so->so_state |= nofd;
770                return (ENOBUFS);
771        }
772        tp->t_state = TCPS_CLOSED;
773        return (0);
774}
775
776/*
777 * Initiate (or continue) disconnect.
778 * If embryonic state, just send reset (once).
779 * If in ``let data drain'' option and linger null, just drop.
780 * Otherwise (hard), mark socket disconnecting and drop
781 * current input data; switch states based on user close, and
782 * send segment to peer (with FIN).
783 */
784static struct tcpcb *
785tcp_disconnect(struct tcpcb *tp)
786{
787        struct socket *so = tp->t_inpcb->inp_socket;
788
789        if (tp->t_state < TCPS_ESTABLISHED)
790                tp = tcp_close(tp);
791        else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
792                tp = tcp_drop(tp, 0);
793        else {
794                soisdisconnecting(so);
795                sbflush(&so->so_rcv);
796                tp = tcp_usrclosed(tp);
797                if (tp)
798                        (void) tcp_output(tp);
799        }
800        return (tp);
801}
802
803/*
804 * User issued close, and wish to trail through shutdown states:
805 * if never received SYN, just forget it.  If got a SYN from peer,
806 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
807 * If already got a FIN from peer, then almost done; go to LAST_ACK
808 * state.  In all other cases, have already sent FIN to peer (e.g.
809 * after PRU_SHUTDOWN), and just have to play tedious game waiting
810 * for peer to send FIN or not respond to keep-alives, etc.
811 * We can let the user exit from the close as soon as the FIN is acked.
812 */
813static struct tcpcb *
814tcp_usrclosed(struct tcpcb *tp)
815{
816
817        switch (tp->t_state) {
818
819        case TCPS_CLOSED:
820        case TCPS_LISTEN:
821                tp->t_state = TCPS_CLOSED;
822                tp = tcp_close(tp);
823                break;
824
825        case TCPS_SYN_SENT:
826        case TCPS_SYN_RECEIVED:
827                tp->t_flags |= TF_NEEDFIN;
828                break;
829
830        case TCPS_ESTABLISHED:
831                tp->t_state = TCPS_FIN_WAIT_1;
832                break;
833
834        case TCPS_CLOSE_WAIT:
835                tp->t_state = TCPS_LAST_ACK;
836                break;
837        }
838        if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
839                soisdisconnected(tp->t_inpcb->inp_socket);
840                /* To prevent the connection hanging in FIN_WAIT_2 forever. */
841                if (tp->t_state == TCPS_FIN_WAIT_2)
842                        tp->t_timer[TCPT_2MSL] = tcp_maxidle;
843        }
844        return (tp);
845}
Note: See TracBrowser for help on using the repository browser.