xref: /csrg-svn/sys/netinet/tcp_usrreq.c (revision 5113)
1 /* tcp_usrreq.c 1.37 81/11/29 */
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/mbuf.h"
6 #include "../h/socket.h"
7 #include "../h/socketvar.h"
8 #include "../h/protosw.h"
9 #include "../net/in.h"
10 #include "../net/in_pcb.h"
11 #include "../net/in_systm.h"
12 #include "../net/if.h"
13 #include "../net/ip.h"
14 #include "../net/ip_var.h"
15 #include "../net/tcp.h"
16 #include "../net/tcp_fsm.h"
17 #include "../net/tcp_seq.h"
18 #include "../net/tcp_timer.h"
19 #include "../net/tcp_var.h"
20 #include "../net/tcpip.h"
21 #include "../errno.h"
22 
23 struct	tcpcb *tcp_newtcpcb();
24 /*
25  * Process a TCP user request for tcp tb.  If this is a send request
26  * then m is the mbuf chain of send data.  If this is a timer expiration
27  * (called from the software clock routine), then timertype tells which timer.
28  */
29 tcp_usrreq(so, req, m, addr)
30 	struct socket *so;
31 	int req;
32 	struct mbuf *m;
33 	caddr_t addr;
34 {
35 	register struct inpcb *inp = sotoinpcb(so);
36 	register struct tcpcb *tp;
37 	int s = splnet();
38 	int error = 0;
39 COUNT(TCP_USRREQ);
40 
41 	/*
42 	 * Make sure attached.  If not,
43 	 * only PRU_ATTACH is valid.
44 	 */
45 	if (inp == 0 && req != PRU_ATTACH) {
46 		splx(s);
47 		return (EINVAL);
48 	}
49 	if (inp) {
50 		tp = intotcpcb(inp);
51 #ifdef KPROF
52 		tcp_acounts[tp->t_state][req]++;
53 #endif
54 	}
55 	switch (req) {
56 
57 	case PRU_ATTACH:
58 		if (inp) {
59 			error = EISCONN;
60 			break;
61 		}
62 		error = in_pcballoc(so, &tcb, 2048, 2048, (struct sockaddr_in *)addr);
63 		if (error)
64 			break;
65 		inp = (struct inpcb *)so->so_pcb;
66 		if (so->so_options & SO_ACCEPTCONN) {
67 			tp = tcp_newtcpcb(inp);
68 			if (tp == 0) {
69 				in_pcbfree(inp);
70 				error = ENOBUFS;
71 				break;
72 			}
73 			tp->t_state = TCPS_LISTEN;
74 		} else
75 			tp->t_state = TCPS_CLOSED;
76 		break;
77 
78 	case PRU_DETACH:
79 		break;
80 
81 	case PRU_CONNECT:
82 		error = in_pcbsetpeer(inp, (struct sockaddr_in *)addr);
83 		if (error)
84 			break;
85 		tp = tcp_newtcpcb(inp);
86 		if (tp == 0) {
87 			inp->inp_faddr.s_addr = 0;
88 			error = ENOBUFS;
89 			break;
90 		}
91 		tp->t_inpcb = inp;
92 		inp->inp_ppcb = (caddr_t)tp;
93 		soisconnecting(so);
94 		tp->t_state = TCPS_SYN_SENT;
95 		(void) tcp_output(tp);
96 		break;
97 
98 	case PRU_ACCEPT:
99 		soisconnected(so);
100 		break;
101 
102 	case PRU_DISCONNECT:
103 		if (tp->t_state < TCPS_ESTABLISHED)
104 			tcp_close(tp);
105 		else {
106 			soisdisconnecting(so);
107 			(void) tcp_output(tp);
108 		}
109 		break;
110 
111 	case PRU_SHUTDOWN:
112 		socantsendmore(so);
113 		switch (tp->t_state) {
114 
115 		case TCPS_LISTEN:
116 		case TCPS_SYN_SENT:
117 			tp->t_state = TCPS_CLOSED;
118 			break;
119 
120 		case TCPS_SYN_RECEIVED:
121 		case TCPS_ESTABLISHED:
122 			tp->t_state = TCPS_FIN_WAIT_1;
123 			(void) tcp_output(tp);
124 			break;
125 
126 		case TCPS_CLOSE_WAIT:
127 			tp->t_state = TCPS_LAST_ACK;
128 			(void) tcp_output(tp);
129 			break;
130 		}
131 		break;
132 
133 	case PRU_RCVD:
134 		(void) tcp_output(tp);
135 		break;
136 
137 	case PRU_SEND:
138 		sbappend(&so->so_snd, m);
139 /*
140 		if (tp->t_flags & TF_PUSH)
141 			tp->snd_end = tp->snd_una + so->so_snd.sb_cc;
142  */
143 		if (tp->t_flags & TF_URG)
144 			tp->snd_up = tp->snd_una + so->so_snd.sb_cc + 1;
145 		(void) tcp_output(tp);
146 		break;
147 
148 	case PRU_ABORT:
149 		tcp_drop(tp, ECONNABORTED);
150 		break;
151 
152 	case PRU_CONTROL:
153 		error = EOPNOTSUPP;
154 		break;
155 
156 	case PRU_SENSE:
157 		error = EOPNOTSUPP;
158 		break;
159 
160 	case PRU_RCVOOB:
161 		error = EOPNOTSUPP;
162 		break;
163 
164 	case PRU_SENDOOB:
165 		error = EOPNOTSUPP;
166 		break;
167 
168 	case PRU_SLOWTIMO:
169 		tcp_timers(tp, (int)addr);
170 		break;
171 
172 	default:
173 		panic("tcp_usrreq");
174 	}
175 	splx(s);
176 	return (error);
177 }
178