tcp_usrreq.c revision 1.39.10.2 1 1.39.10.2 itojun /* $NetBSD: tcp_usrreq.c,v 1.39.10.2 1999/07/06 11:02:50 itojun Exp $ */
2 1.39.10.2 itojun
3 1.39.10.1 itojun /*
4 1.39.10.1 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.39.10.1 itojun * All rights reserved.
6 1.39.10.1 itojun *
7 1.39.10.1 itojun * Redistribution and use in source and binary forms, with or without
8 1.39.10.1 itojun * modification, are permitted provided that the following conditions
9 1.39.10.1 itojun * are met:
10 1.39.10.1 itojun * 1. Redistributions of source code must retain the above copyright
11 1.39.10.1 itojun * notice, this list of conditions and the following disclaimer.
12 1.39.10.1 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.39.10.1 itojun * notice, this list of conditions and the following disclaimer in the
14 1.39.10.1 itojun * documentation and/or other materials provided with the distribution.
15 1.39.10.1 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.39.10.1 itojun * may be used to endorse or promote products derived from this software
17 1.39.10.1 itojun * without specific prior written permission.
18 1.39.10.1 itojun *
19 1.39.10.1 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.39.10.1 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.39.10.1 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.39.10.1 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.39.10.1 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.39.10.1 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.39.10.1 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.39.10.1 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.39.10.1 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.39.10.1 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.39.10.1 itojun * SUCH DAMAGE.
30 1.39.10.1 itojun */
31 1.34 thorpej
32 1.34 thorpej /*-
33 1.34 thorpej * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
34 1.34 thorpej * All rights reserved.
35 1.34 thorpej *
36 1.34 thorpej * This code is derived from software contributed to The NetBSD Foundation
37 1.34 thorpej * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
38 1.34 thorpej * Facility, NASA Ames Research Center.
39 1.34 thorpej *
40 1.34 thorpej * Redistribution and use in source and binary forms, with or without
41 1.34 thorpej * modification, are permitted provided that the following conditions
42 1.34 thorpej * are met:
43 1.34 thorpej * 1. Redistributions of source code must retain the above copyright
44 1.34 thorpej * notice, this list of conditions and the following disclaimer.
45 1.34 thorpej * 2. Redistributions in binary form must reproduce the above copyright
46 1.34 thorpej * notice, this list of conditions and the following disclaimer in the
47 1.34 thorpej * documentation and/or other materials provided with the distribution.
48 1.34 thorpej * 3. All advertising materials mentioning features or use of this software
49 1.34 thorpej * must display the following acknowledgement:
50 1.34 thorpej * This product includes software developed by the NetBSD
51 1.34 thorpej * Foundation, Inc. and its contributors.
52 1.34 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
53 1.34 thorpej * contributors may be used to endorse or promote products derived
54 1.34 thorpej * from this software without specific prior written permission.
55 1.34 thorpej *
56 1.34 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
57 1.34 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 1.34 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59 1.34 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
60 1.34 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61 1.34 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62 1.34 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63 1.34 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64 1.34 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65 1.34 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
66 1.34 thorpej * POSSIBILITY OF SUCH DAMAGE.
67 1.34 thorpej */
68 1.10 cgd
69 1.1 cgd /*
70 1.33 thorpej * Copyright (c) 1982, 1986, 1988, 1993, 1995
71 1.9 mycroft * The Regents of the University of California. All rights reserved.
72 1.1 cgd *
73 1.1 cgd * Redistribution and use in source and binary forms, with or without
74 1.1 cgd * modification, are permitted provided that the following conditions
75 1.1 cgd * are met:
76 1.1 cgd * 1. Redistributions of source code must retain the above copyright
77 1.1 cgd * notice, this list of conditions and the following disclaimer.
78 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
79 1.1 cgd * notice, this list of conditions and the following disclaimer in the
80 1.1 cgd * documentation and/or other materials provided with the distribution.
81 1.1 cgd * 3. All advertising materials mentioning features or use of this software
82 1.1 cgd * must display the following acknowledgement:
83 1.1 cgd * This product includes software developed by the University of
84 1.1 cgd * California, Berkeley and its contributors.
85 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
86 1.1 cgd * may be used to endorse or promote products derived from this software
87 1.1 cgd * without specific prior written permission.
88 1.1 cgd *
89 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
90 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
91 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
92 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
93 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
94 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
95 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
96 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
97 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
98 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99 1.1 cgd * SUCH DAMAGE.
100 1.1 cgd *
101 1.33 thorpej * @(#)tcp_usrreq.c 8.5 (Berkeley) 6/21/95
102 1.1 cgd */
103 1.1 cgd
104 1.39.10.1 itojun #include "opt_inet.h"
105 1.39.10.1 itojun
106 1.5 mycroft #include <sys/param.h>
107 1.5 mycroft #include <sys/systm.h>
108 1.13 glass #include <sys/kernel.h>
109 1.5 mycroft #include <sys/malloc.h>
110 1.5 mycroft #include <sys/mbuf.h>
111 1.5 mycroft #include <sys/socket.h>
112 1.5 mycroft #include <sys/socketvar.h>
113 1.5 mycroft #include <sys/protosw.h>
114 1.5 mycroft #include <sys/errno.h>
115 1.5 mycroft #include <sys/stat.h>
116 1.20 christos #include <sys/proc.h>
117 1.20 christos #include <sys/ucred.h>
118 1.39.10.1 itojun #include <sys/domain.h>
119 1.22 mycroft
120 1.20 christos #include <vm/vm.h>
121 1.20 christos #include <sys/sysctl.h>
122 1.1 cgd
123 1.5 mycroft #include <net/if.h>
124 1.5 mycroft #include <net/route.h>
125 1.1 cgd
126 1.5 mycroft #include <netinet/in.h>
127 1.5 mycroft #include <netinet/in_systm.h>
128 1.14 cgd #include <netinet/in_var.h>
129 1.5 mycroft #include <netinet/ip.h>
130 1.5 mycroft #include <netinet/in_pcb.h>
131 1.5 mycroft #include <netinet/ip_var.h>
132 1.39.10.1 itojun
133 1.39.10.1 itojun #ifdef INET6
134 1.39.10.1 itojun #ifndef INET
135 1.39.10.1 itojun #include <netinet/in.h>
136 1.39.10.1 itojun #endif
137 1.39.10.1 itojun #include <netinet/ip6.h>
138 1.39.10.1 itojun #include <netinet6/in6_pcb.h>
139 1.39.10.1 itojun #include <netinet6/ip6_var.h>
140 1.39.10.1 itojun #endif
141 1.39.10.1 itojun
142 1.5 mycroft #include <netinet/tcp.h>
143 1.5 mycroft #include <netinet/tcp_fsm.h>
144 1.5 mycroft #include <netinet/tcp_seq.h>
145 1.5 mycroft #include <netinet/tcp_timer.h>
146 1.5 mycroft #include <netinet/tcp_var.h>
147 1.5 mycroft #include <netinet/tcpip.h>
148 1.5 mycroft #include <netinet/tcp_debug.h>
149 1.26 thorpej
150 1.26 thorpej #include "opt_tcp_recvspace.h"
151 1.26 thorpej #include "opt_tcp_sendspace.h"
152 1.1 cgd
153 1.39.10.1 itojun #ifdef IPSEC
154 1.39.10.1 itojun #include <netinet6/ipsec.h>
155 1.39.10.1 itojun #endif /*IPSEC*/
156 1.39.10.1 itojun
157 1.1 cgd /*
158 1.1 cgd * TCP protocol interface to socket abstraction.
159 1.1 cgd */
160 1.1 cgd extern char *tcpstates[];
161 1.1 cgd
162 1.1 cgd /*
163 1.1 cgd * Process a TCP user request for TCP tb. If this is a send request
164 1.1 cgd * then m is the mbuf chain of send data. If this is a timer expiration
165 1.1 cgd * (called from the software clock routine), then timertype tells which timer.
166 1.1 cgd */
167 1.1 cgd /*ARGSUSED*/
168 1.6 mycroft int
169 1.21 mycroft tcp_usrreq(so, req, m, nam, control, p)
170 1.1 cgd struct socket *so;
171 1.1 cgd int req;
172 1.1 cgd struct mbuf *m, *nam, *control;
173 1.21 mycroft struct proc *p;
174 1.1 cgd {
175 1.1 cgd register struct inpcb *inp;
176 1.39.10.1 itojun #ifdef INET6
177 1.39.10.1 itojun register struct in6pcb *in6p;
178 1.39.10.1 itojun #endif
179 1.20 christos register struct tcpcb *tp = NULL;
180 1.1 cgd int s;
181 1.1 cgd int error = 0;
182 1.1 cgd int ostate;
183 1.39.10.1 itojun int family; /* family of the socket */
184 1.39.10.1 itojun
185 1.39.10.1 itojun family = so->so_proto->pr_domain->dom_family;
186 1.1 cgd
187 1.39.10.1 itojun if (req == PRU_CONTROL) {
188 1.39.10.1 itojun switch (family) {
189 1.39.10.1 itojun case PF_INET:
190 1.39.10.1 itojun return (in_control(so, (long)m, (caddr_t)nam,
191 1.39.10.1 itojun (struct ifnet *)control, p));
192 1.39.10.1 itojun #ifdef INET6
193 1.39.10.1 itojun case PF_INET6:
194 1.39.10.1 itojun return (in6_control(so, (long)m, (caddr_t)nam,
195 1.39.10.1 itojun (struct ifnet *)control, p));
196 1.39.10.1 itojun #endif
197 1.39.10.1 itojun default:
198 1.39.10.1 itojun return EAFNOSUPPORT;
199 1.39.10.1 itojun }
200 1.39.10.1 itojun }
201 1.22 mycroft
202 1.22 mycroft s = splsoftnet();
203 1.39.10.1 itojun switch (family) {
204 1.39.10.1 itojun case PF_INET:
205 1.39.10.1 itojun inp = sotoinpcb(so);
206 1.39.10.1 itojun #ifdef INET6
207 1.39.10.1 itojun in6p = NULL;
208 1.39.10.1 itojun #endif
209 1.39.10.1 itojun break;
210 1.39.10.1 itojun #ifdef INET6
211 1.39.10.1 itojun case PF_INET6:
212 1.39.10.1 itojun inp = NULL;
213 1.39.10.1 itojun in6p = sotoin6pcb(so);
214 1.39.10.1 itojun break;
215 1.39.10.1 itojun #endif
216 1.39.10.1 itojun default:
217 1.39.10.1 itojun splx(s);
218 1.39.10.1 itojun return EAFNOSUPPORT;
219 1.39.10.1 itojun }
220 1.39.10.1 itojun
221 1.23 mycroft #ifdef DIAGNOSTIC
222 1.23 mycroft if (req != PRU_SEND && req != PRU_SENDOOB && control)
223 1.23 mycroft panic("tcp_usrreq: unexpected control mbuf");
224 1.23 mycroft #endif
225 1.1 cgd /*
226 1.1 cgd * When a TCP is attached to a socket, then there will be
227 1.1 cgd * a (struct inpcb) pointed at by the socket, and this
228 1.1 cgd * structure will point at a subsidary (struct tcpcb).
229 1.1 cgd */
230 1.39.10.1 itojun #ifndef INET6
231 1.39.10.1 itojun if (inp == 0 && req != PRU_ATTACH)
232 1.39.10.1 itojun #else
233 1.39.10.1 itojun if ((inp == 0 && in6p == 0) && req != PRU_ATTACH)
234 1.39.10.1 itojun #endif
235 1.39.10.1 itojun {
236 1.22 mycroft error = EINVAL;
237 1.22 mycroft goto release;
238 1.1 cgd }
239 1.1 cgd if (inp) {
240 1.1 cgd tp = intotcpcb(inp);
241 1.1 cgd /* WHAT IF TP IS 0? */
242 1.1 cgd #ifdef KPROF
243 1.1 cgd tcp_acounts[tp->t_state][req]++;
244 1.1 cgd #endif
245 1.1 cgd ostate = tp->t_state;
246 1.39.10.1 itojun }
247 1.39.10.1 itojun #ifdef INET6
248 1.39.10.1 itojun else if (in6p) {
249 1.39.10.1 itojun tp = in6totcpcb(in6p);
250 1.39.10.1 itojun /* WHAT IF TP IS 0? */
251 1.39.10.1 itojun #ifdef KPROF
252 1.39.10.1 itojun tcp_acounts[tp->t_state][req]++;
253 1.39.10.1 itojun #endif
254 1.39.10.1 itojun ostate = tp->t_state;
255 1.39.10.1 itojun }
256 1.39.10.1 itojun #endif
257 1.39.10.1 itojun else
258 1.1 cgd ostate = 0;
259 1.22 mycroft
260 1.1 cgd switch (req) {
261 1.1 cgd
262 1.1 cgd /*
263 1.1 cgd * TCP attaches to socket via PRU_ATTACH, reserving space,
264 1.1 cgd * and an internet control block.
265 1.1 cgd */
266 1.1 cgd case PRU_ATTACH:
267 1.39.10.1 itojun #ifndef INET6
268 1.39.10.1 itojun if (inp != 0)
269 1.39.10.1 itojun #else
270 1.39.10.1 itojun if (inp != 0 || in6p != 0)
271 1.39.10.1 itojun #endif
272 1.39.10.1 itojun {
273 1.1 cgd error = EISCONN;
274 1.1 cgd break;
275 1.1 cgd }
276 1.1 cgd error = tcp_attach(so);
277 1.1 cgd if (error)
278 1.1 cgd break;
279 1.1 cgd if ((so->so_options & SO_LINGER) && so->so_linger == 0)
280 1.32 thorpej so->so_linger = TCP_LINGERTIME;
281 1.1 cgd tp = sototcpcb(so);
282 1.1 cgd break;
283 1.1 cgd
284 1.1 cgd /*
285 1.1 cgd * PRU_DETACH detaches the TCP protocol from the socket.
286 1.1 cgd */
287 1.1 cgd case PRU_DETACH:
288 1.24 kleink tp = tcp_disconnect(tp);
289 1.1 cgd break;
290 1.1 cgd
291 1.1 cgd /*
292 1.1 cgd * Give the socket an address.
293 1.1 cgd */
294 1.1 cgd case PRU_BIND:
295 1.39.10.1 itojun switch (family) {
296 1.39.10.1 itojun case PF_INET:
297 1.39.10.1 itojun error = in_pcbbind(inp, nam, p);
298 1.39.10.1 itojun break;
299 1.39.10.1 itojun #ifdef INET6
300 1.39.10.1 itojun case PF_INET6:
301 1.39.10.1 itojun error = in6_pcbbind(in6p, nam /*, p*/ );
302 1.39.10.1 itojun /* mapped addr case */
303 1.39.10.1 itojun if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_laddr))
304 1.39.10.1 itojun tp->t_family = AF_INET;
305 1.39.10.1 itojun break;
306 1.39.10.1 itojun #endif
307 1.39.10.1 itojun }
308 1.1 cgd break;
309 1.1 cgd
310 1.1 cgd /*
311 1.1 cgd * Prepare to accept connections.
312 1.1 cgd */
313 1.1 cgd case PRU_LISTEN:
314 1.39.10.1 itojun if (inp && inp->inp_lport == 0) {
315 1.21 mycroft error = in_pcbbind(inp, (struct mbuf *)0,
316 1.21 mycroft (struct proc *)0);
317 1.22 mycroft if (error)
318 1.22 mycroft break;
319 1.22 mycroft }
320 1.39.10.1 itojun #ifdef INET6
321 1.39.10.1 itojun else if (in6p && in6p->in6p_lport == 0) {
322 1.39.10.1 itojun error = in6_pcbbind(in6p, (struct mbuf *)0 /*,
323 1.39.10.1 itojun (struct proc *)0 */ );
324 1.39.10.1 itojun if (error)
325 1.39.10.1 itojun break;
326 1.39.10.1 itojun }
327 1.39.10.1 itojun #endif
328 1.22 mycroft tp->t_state = TCPS_LISTEN;
329 1.1 cgd break;
330 1.1 cgd
331 1.1 cgd /*
332 1.1 cgd * Initiate connection to peer.
333 1.1 cgd * Create a template for use in transmissions on this connection.
334 1.1 cgd * Enter SYN_SENT state, and mark socket as connecting.
335 1.1 cgd * Start keep-alive timer, and seed output sequence space.
336 1.1 cgd * Send initial segment on connection.
337 1.1 cgd */
338 1.1 cgd case PRU_CONNECT:
339 1.39.10.1 itojun if (inp) {
340 1.39.10.1 itojun if (inp->inp_lport == 0) {
341 1.39.10.1 itojun error = in_pcbbind(inp, (struct mbuf *)0,
342 1.39.10.1 itojun (struct proc *)0);
343 1.39.10.1 itojun if (error)
344 1.39.10.1 itojun break;
345 1.39.10.1 itojun }
346 1.39.10.1 itojun error = in_pcbconnect(inp, nam);
347 1.39.10.1 itojun }
348 1.39.10.1 itojun #ifdef INET6
349 1.39.10.1 itojun else if (in6p) {
350 1.39.10.1 itojun if (in6p->in6p_lport == 0) {
351 1.39.10.1 itojun error = in6_pcbbind(in6p, (struct mbuf *)0 /*,
352 1.39.10.1 itojun (struct proc *)0 */ );
353 1.39.10.1 itojun if (error)
354 1.39.10.1 itojun break;
355 1.39.10.1 itojun }
356 1.39.10.1 itojun error = in6_pcbconnect(in6p, nam);
357 1.39.10.1 itojun /* mapped addr case */
358 1.39.10.1 itojun if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr))
359 1.39.10.1 itojun tp->t_family = AF_INET;
360 1.1 cgd }
361 1.39.10.1 itojun #endif
362 1.1 cgd if (error)
363 1.1 cgd break;
364 1.1 cgd tp->t_template = tcp_template(tp);
365 1.1 cgd if (tp->t_template == 0) {
366 1.39.10.1 itojun if (inp)
367 1.39.10.1 itojun in_pcbdisconnect(inp);
368 1.39.10.1 itojun #ifdef INET6
369 1.39.10.1 itojun else if (in6p)
370 1.39.10.1 itojun in6_pcbdisconnect(in6p);
371 1.39.10.1 itojun #endif
372 1.1 cgd error = ENOBUFS;
373 1.1 cgd break;
374 1.1 cgd }
375 1.9 mycroft /* Compute window scaling to request. */
376 1.9 mycroft while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
377 1.9 mycroft (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
378 1.9 mycroft tp->request_r_scale++;
379 1.1 cgd soisconnecting(so);
380 1.1 cgd tcpstat.tcps_connattempt++;
381 1.1 cgd tp->t_state = TCPS_SYN_SENT;
382 1.37 thorpej TCP_TIMER_ARM(tp, TCPT_KEEP, TCPTV_KEEP_INIT);
383 1.27 explorer tp->iss = tcp_new_iss(tp, sizeof(struct tcpcb), 0);
384 1.1 cgd tcp_sendseqinit(tp);
385 1.1 cgd error = tcp_output(tp);
386 1.1 cgd break;
387 1.1 cgd
388 1.1 cgd /*
389 1.1 cgd * Create a TCP connection between two sockets.
390 1.1 cgd */
391 1.1 cgd case PRU_CONNECT2:
392 1.1 cgd error = EOPNOTSUPP;
393 1.1 cgd break;
394 1.1 cgd
395 1.1 cgd /*
396 1.1 cgd * Initiate disconnect from peer.
397 1.1 cgd * If connection never passed embryonic stage, just drop;
398 1.1 cgd * else if don't need to let data drain, then can just drop anyways,
399 1.1 cgd * else have to begin TCP shutdown process: mark socket disconnecting,
400 1.1 cgd * drain unread data, state switch to reflect user close, and
401 1.1 cgd * send segment (e.g. FIN) to peer. Socket will be really disconnected
402 1.1 cgd * when peer sends FIN and acks ours.
403 1.1 cgd *
404 1.1 cgd * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
405 1.1 cgd */
406 1.1 cgd case PRU_DISCONNECT:
407 1.1 cgd tp = tcp_disconnect(tp);
408 1.1 cgd break;
409 1.1 cgd
410 1.1 cgd /*
411 1.1 cgd * Accept a connection. Essentially all the work is
412 1.1 cgd * done at higher levels; just return the address
413 1.1 cgd * of the peer, storing through addr.
414 1.1 cgd */
415 1.8 mycroft case PRU_ACCEPT:
416 1.39.10.1 itojun if (inp)
417 1.39.10.1 itojun in_setpeeraddr(inp, nam);
418 1.39.10.1 itojun #ifdef INET6
419 1.39.10.1 itojun else if (in6p)
420 1.39.10.1 itojun in6_setpeeraddr(in6p, nam);
421 1.39.10.1 itojun #endif
422 1.1 cgd break;
423 1.1 cgd
424 1.1 cgd /*
425 1.1 cgd * Mark the connection as being incapable of further output.
426 1.1 cgd */
427 1.1 cgd case PRU_SHUTDOWN:
428 1.1 cgd socantsendmore(so);
429 1.1 cgd tp = tcp_usrclosed(tp);
430 1.1 cgd if (tp)
431 1.1 cgd error = tcp_output(tp);
432 1.1 cgd break;
433 1.1 cgd
434 1.1 cgd /*
435 1.1 cgd * After a receive, possibly send window update to peer.
436 1.1 cgd */
437 1.1 cgd case PRU_RCVD:
438 1.1 cgd (void) tcp_output(tp);
439 1.1 cgd break;
440 1.1 cgd
441 1.1 cgd /*
442 1.1 cgd * Do a send by putting data in output queue and updating urgent
443 1.1 cgd * marker if URG set. Possibly send more data.
444 1.1 cgd */
445 1.1 cgd case PRU_SEND:
446 1.23 mycroft if (control && control->m_len) {
447 1.23 mycroft m_freem(control);
448 1.23 mycroft m_freem(m);
449 1.23 mycroft error = EINVAL;
450 1.23 mycroft break;
451 1.23 mycroft }
452 1.1 cgd sbappend(&so->so_snd, m);
453 1.1 cgd error = tcp_output(tp);
454 1.1 cgd break;
455 1.1 cgd
456 1.1 cgd /*
457 1.1 cgd * Abort the TCP.
458 1.1 cgd */
459 1.1 cgd case PRU_ABORT:
460 1.1 cgd tp = tcp_drop(tp, ECONNABORTED);
461 1.1 cgd break;
462 1.1 cgd
463 1.1 cgd case PRU_SENSE:
464 1.22 mycroft /*
465 1.22 mycroft * stat: don't bother with a blocksize.
466 1.22 mycroft */
467 1.22 mycroft splx(s);
468 1.1 cgd return (0);
469 1.1 cgd
470 1.1 cgd case PRU_RCVOOB:
471 1.23 mycroft if (control && control->m_len) {
472 1.23 mycroft m_freem(control);
473 1.23 mycroft m_freem(m);
474 1.23 mycroft error = EINVAL;
475 1.23 mycroft break;
476 1.23 mycroft }
477 1.1 cgd if ((so->so_oobmark == 0 &&
478 1.1 cgd (so->so_state & SS_RCVATMARK) == 0) ||
479 1.1 cgd so->so_options & SO_OOBINLINE ||
480 1.1 cgd tp->t_oobflags & TCPOOB_HADDATA) {
481 1.1 cgd error = EINVAL;
482 1.1 cgd break;
483 1.1 cgd }
484 1.1 cgd if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
485 1.1 cgd error = EWOULDBLOCK;
486 1.1 cgd break;
487 1.1 cgd }
488 1.1 cgd m->m_len = 1;
489 1.1 cgd *mtod(m, caddr_t) = tp->t_iobc;
490 1.14 cgd if (((long)nam & MSG_PEEK) == 0)
491 1.1 cgd tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
492 1.1 cgd break;
493 1.1 cgd
494 1.1 cgd case PRU_SENDOOB:
495 1.1 cgd if (sbspace(&so->so_snd) < -512) {
496 1.1 cgd m_freem(m);
497 1.1 cgd error = ENOBUFS;
498 1.1 cgd break;
499 1.1 cgd }
500 1.1 cgd /*
501 1.1 cgd * According to RFC961 (Assigned Protocols),
502 1.1 cgd * the urgent pointer points to the last octet
503 1.1 cgd * of urgent data. We continue, however,
504 1.1 cgd * to consider it to indicate the first octet
505 1.1 cgd * of data past the urgent section.
506 1.1 cgd * Otherwise, snd_up should be one lower.
507 1.1 cgd */
508 1.1 cgd sbappend(&so->so_snd, m);
509 1.1 cgd tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
510 1.1 cgd tp->t_force = 1;
511 1.1 cgd error = tcp_output(tp);
512 1.1 cgd tp->t_force = 0;
513 1.1 cgd break;
514 1.1 cgd
515 1.1 cgd case PRU_SOCKADDR:
516 1.39.10.1 itojun if (inp)
517 1.39.10.1 itojun in_setsockaddr(inp, nam);
518 1.39.10.1 itojun #ifdef INET6
519 1.39.10.1 itojun else if (in6p)
520 1.39.10.1 itojun in6_setsockaddr(in6p, nam);
521 1.39.10.1 itojun #endif
522 1.1 cgd break;
523 1.1 cgd
524 1.1 cgd case PRU_PEERADDR:
525 1.39.10.1 itojun if (inp)
526 1.39.10.1 itojun in_setpeeraddr(inp, nam);
527 1.39.10.1 itojun #ifdef INET6
528 1.39.10.1 itojun else if (in6p)
529 1.39.10.1 itojun in6_setpeeraddr(in6p, nam);
530 1.39.10.1 itojun #endif
531 1.1 cgd break;
532 1.1 cgd
533 1.1 cgd /*
534 1.1 cgd * TCP slow timer went off; going through this
535 1.1 cgd * routine for tracing's sake.
536 1.1 cgd */
537 1.1 cgd case PRU_SLOWTIMO:
538 1.14 cgd tp = tcp_timers(tp, (long)nam);
539 1.14 cgd req |= (long)nam << 8; /* for debug's sake */
540 1.1 cgd break;
541 1.1 cgd
542 1.1 cgd default:
543 1.1 cgd panic("tcp_usrreq");
544 1.1 cgd }
545 1.1 cgd if (tp && (so->so_options & SO_DEBUG))
546 1.39.10.1 itojun tcp_trace(TA_USER, ostate, tp, NULL, req);
547 1.22 mycroft
548 1.22 mycroft release:
549 1.1 cgd splx(s);
550 1.1 cgd return (error);
551 1.1 cgd }
552 1.1 cgd
553 1.6 mycroft int
554 1.1 cgd tcp_ctloutput(op, so, level, optname, mp)
555 1.1 cgd int op;
556 1.1 cgd struct socket *so;
557 1.1 cgd int level, optname;
558 1.1 cgd struct mbuf **mp;
559 1.1 cgd {
560 1.9 mycroft int error = 0, s;
561 1.9 mycroft struct inpcb *inp;
562 1.39.10.1 itojun #ifdef INET6
563 1.39.10.1 itojun register struct in6pcb *in6p;
564 1.39.10.1 itojun #endif
565 1.9 mycroft register struct tcpcb *tp;
566 1.1 cgd register struct mbuf *m;
567 1.9 mycroft register int i;
568 1.39.10.1 itojun int family; /* family of the socket */
569 1.39.10.1 itojun
570 1.39.10.1 itojun family = so->so_proto->pr_domain->dom_family;
571 1.1 cgd
572 1.16 mycroft s = splsoftnet();
573 1.39.10.1 itojun switch (family) {
574 1.39.10.1 itojun case PF_INET:
575 1.39.10.1 itojun inp = sotoinpcb(so);
576 1.39.10.1 itojun #ifdef INET6
577 1.39.10.1 itojun in6p = NULL;
578 1.39.10.1 itojun #endif
579 1.39.10.1 itojun break;
580 1.39.10.1 itojun #ifdef INET6
581 1.39.10.1 itojun case PF_INET6:
582 1.39.10.1 itojun inp = NULL;
583 1.39.10.1 itojun in6p = sotoin6pcb(so);
584 1.39.10.1 itojun break;
585 1.39.10.1 itojun #endif
586 1.39.10.1 itojun default:
587 1.39.10.1 itojun splx(s);
588 1.39.10.1 itojun return EAFNOSUPPORT;
589 1.39.10.1 itojun }
590 1.39.10.1 itojun #ifndef INET6
591 1.39.10.1 itojun if (inp == NULL)
592 1.39.10.1 itojun #else
593 1.39.10.1 itojun if (inp == NULL && in6p == NULL)
594 1.39.10.1 itojun #endif
595 1.39.10.1 itojun {
596 1.9 mycroft splx(s);
597 1.9 mycroft if (op == PRCO_SETOPT && *mp)
598 1.9 mycroft (void) m_free(*mp);
599 1.9 mycroft return (ECONNRESET);
600 1.9 mycroft }
601 1.9 mycroft if (level != IPPROTO_TCP) {
602 1.39.10.1 itojun switch (family) {
603 1.39.10.1 itojun case PF_INET:
604 1.39.10.1 itojun error = ip_ctloutput(op, so, level, optname, mp);
605 1.39.10.1 itojun break;
606 1.39.10.1 itojun #ifdef INET6
607 1.39.10.1 itojun case PF_INET6:
608 1.39.10.1 itojun error = ip6_ctloutput(op, so, level, optname, mp);
609 1.39.10.1 itojun break;
610 1.39.10.1 itojun #endif
611 1.39.10.1 itojun }
612 1.9 mycroft splx(s);
613 1.9 mycroft return (error);
614 1.9 mycroft }
615 1.39.10.1 itojun if (inp)
616 1.39.10.1 itojun tp = intotcpcb(inp);
617 1.39.10.1 itojun #ifdef INET6
618 1.39.10.1 itojun else if (in6p)
619 1.39.10.1 itojun tp = in6totcpcb(in6p);
620 1.39.10.1 itojun #endif
621 1.39.10.1 itojun else
622 1.39.10.1 itojun tp = NULL;
623 1.1 cgd
624 1.1 cgd switch (op) {
625 1.1 cgd
626 1.1 cgd case PRCO_SETOPT:
627 1.1 cgd m = *mp;
628 1.1 cgd switch (optname) {
629 1.1 cgd
630 1.1 cgd case TCP_NODELAY:
631 1.1 cgd if (m == NULL || m->m_len < sizeof (int))
632 1.1 cgd error = EINVAL;
633 1.1 cgd else if (*mtod(m, int *))
634 1.1 cgd tp->t_flags |= TF_NODELAY;
635 1.1 cgd else
636 1.1 cgd tp->t_flags &= ~TF_NODELAY;
637 1.1 cgd break;
638 1.1 cgd
639 1.9 mycroft case TCP_MAXSEG:
640 1.28 kml if (m && (i = *mtod(m, int *)) > 0 &&
641 1.28 kml i <= tp->t_peermss)
642 1.28 kml tp->t_peermss = i; /* limit on send size */
643 1.9 mycroft else
644 1.9 mycroft error = EINVAL;
645 1.9 mycroft break;
646 1.9 mycroft
647 1.1 cgd default:
648 1.9 mycroft error = ENOPROTOOPT;
649 1.1 cgd break;
650 1.1 cgd }
651 1.1 cgd if (m)
652 1.1 cgd (void) m_free(m);
653 1.1 cgd break;
654 1.1 cgd
655 1.1 cgd case PRCO_GETOPT:
656 1.1 cgd *mp = m = m_get(M_WAIT, MT_SOOPTS);
657 1.1 cgd m->m_len = sizeof(int);
658 1.1 cgd
659 1.1 cgd switch (optname) {
660 1.1 cgd case TCP_NODELAY:
661 1.1 cgd *mtod(m, int *) = tp->t_flags & TF_NODELAY;
662 1.1 cgd break;
663 1.1 cgd case TCP_MAXSEG:
664 1.28 kml *mtod(m, int *) = tp->t_peermss;
665 1.1 cgd break;
666 1.1 cgd default:
667 1.9 mycroft error = ENOPROTOOPT;
668 1.1 cgd break;
669 1.1 cgd }
670 1.1 cgd break;
671 1.1 cgd }
672 1.9 mycroft splx(s);
673 1.1 cgd return (error);
674 1.1 cgd }
675 1.1 cgd
676 1.11 mycroft #ifndef TCP_SENDSPACE
677 1.11 mycroft #define TCP_SENDSPACE 1024*16;
678 1.11 mycroft #endif
679 1.25 thorpej int tcp_sendspace = TCP_SENDSPACE;
680 1.11 mycroft #ifndef TCP_RECVSPACE
681 1.11 mycroft #define TCP_RECVSPACE 1024*16;
682 1.11 mycroft #endif
683 1.25 thorpej int tcp_recvspace = TCP_RECVSPACE;
684 1.1 cgd
685 1.1 cgd /*
686 1.1 cgd * Attach TCP protocol to socket, allocating
687 1.1 cgd * internet protocol control block, tcp control block,
688 1.1 cgd * bufer space, and entering LISTEN state if to accept connections.
689 1.1 cgd */
690 1.6 mycroft int
691 1.1 cgd tcp_attach(so)
692 1.1 cgd struct socket *so;
693 1.1 cgd {
694 1.1 cgd register struct tcpcb *tp;
695 1.1 cgd struct inpcb *inp;
696 1.39.10.1 itojun #ifdef INET6
697 1.39.10.1 itojun struct in6pcb *in6p;
698 1.39.10.1 itojun #endif
699 1.1 cgd int error;
700 1.39.10.1 itojun int family; /* family of the socket */
701 1.39.10.1 itojun
702 1.39.10.1 itojun family = so->so_proto->pr_domain->dom_family;
703 1.1 cgd
704 1.1 cgd if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
705 1.1 cgd error = soreserve(so, tcp_sendspace, tcp_recvspace);
706 1.1 cgd if (error)
707 1.1 cgd return (error);
708 1.1 cgd }
709 1.39.10.1 itojun switch (family) {
710 1.39.10.1 itojun case PF_INET:
711 1.39.10.1 itojun error = in_pcballoc(so, &tcbtable);
712 1.39.10.1 itojun if (error)
713 1.39.10.1 itojun return (error);
714 1.39.10.1 itojun inp = sotoinpcb(so);
715 1.39.10.1 itojun #ifdef INET6
716 1.39.10.1 itojun in6p = NULL;
717 1.39.10.1 itojun #endif
718 1.39.10.1 itojun break;
719 1.39.10.1 itojun #ifdef INET6
720 1.39.10.1 itojun case PF_INET6:
721 1.39.10.1 itojun error = in6_pcballoc(so, &tcb6);
722 1.39.10.1 itojun if (error)
723 1.39.10.1 itojun return (error);
724 1.39.10.1 itojun inp = NULL;
725 1.39.10.1 itojun in6p = sotoin6pcb(so);
726 1.39.10.1 itojun break;
727 1.39.10.1 itojun #endif
728 1.39.10.1 itojun default:
729 1.39.10.1 itojun return EAFNOSUPPORT;
730 1.39.10.1 itojun }
731 1.39.10.1 itojun #ifdef IPSEC
732 1.39.10.1 itojun if (inp && (error = ipsec_init_policy(&inp->inp_sp)) != 0) {
733 1.39.10.1 itojun in_pcbdetach(inp);
734 1.39.10.1 itojun return (error);
735 1.39.10.1 itojun }
736 1.39.10.1 itojun #ifdef INET6
737 1.39.10.1 itojun else if (in6p && (error = ipsec_init_policy(&in6p->in6p_sp)) != 0) {
738 1.39.10.1 itojun in6_pcbdetach(in6p);
739 1.1 cgd return (error);
740 1.39.10.1 itojun }
741 1.39.10.1 itojun #endif
742 1.39.10.1 itojun #endif /*IPSEC*/
743 1.39.10.1 itojun if (inp)
744 1.39.10.1 itojun tp = tcp_newtcpcb(family, (void *)inp);
745 1.39.10.1 itojun #ifdef INET6
746 1.39.10.1 itojun else if (in6p)
747 1.39.10.1 itojun tp = tcp_newtcpcb(family, (void *)in6p);
748 1.39.10.1 itojun #endif
749 1.39.10.1 itojun
750 1.1 cgd if (tp == 0) {
751 1.1 cgd int nofd = so->so_state & SS_NOFDREF; /* XXX */
752 1.1 cgd
753 1.1 cgd so->so_state &= ~SS_NOFDREF; /* don't free the socket yet */
754 1.39.10.1 itojun if (inp)
755 1.39.10.1 itojun in_pcbdetach(inp);
756 1.39.10.1 itojun #ifdef INET6
757 1.39.10.1 itojun else if (in6p)
758 1.39.10.1 itojun in6_pcbdetach(in6p);
759 1.39.10.1 itojun #endif
760 1.1 cgd so->so_state |= nofd;
761 1.1 cgd return (ENOBUFS);
762 1.1 cgd }
763 1.1 cgd tp->t_state = TCPS_CLOSED;
764 1.1 cgd return (0);
765 1.1 cgd }
766 1.1 cgd
767 1.1 cgd /*
768 1.1 cgd * Initiate (or continue) disconnect.
769 1.1 cgd * If embryonic state, just send reset (once).
770 1.1 cgd * If in ``let data drain'' option and linger null, just drop.
771 1.1 cgd * Otherwise (hard), mark socket disconnecting and drop
772 1.1 cgd * current input data; switch states based on user close, and
773 1.1 cgd * send segment to peer (with FIN).
774 1.1 cgd */
775 1.1 cgd struct tcpcb *
776 1.1 cgd tcp_disconnect(tp)
777 1.1 cgd register struct tcpcb *tp;
778 1.1 cgd {
779 1.39.10.1 itojun struct socket *so;
780 1.39.10.1 itojun
781 1.39.10.1 itojun if (tp->t_inpcb)
782 1.39.10.1 itojun so = tp->t_inpcb->inp_socket;
783 1.39.10.1 itojun #ifdef INET6
784 1.39.10.1 itojun else if (tp->t_in6pcb)
785 1.39.10.1 itojun so = tp->t_in6pcb->in6p_socket;
786 1.39.10.1 itojun #endif
787 1.39.10.1 itojun else
788 1.39.10.1 itojun so = NULL;
789 1.1 cgd
790 1.12 mycroft if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
791 1.1 cgd tp = tcp_close(tp);
792 1.1 cgd else if ((so->so_options & SO_LINGER) && so->so_linger == 0)
793 1.1 cgd tp = tcp_drop(tp, 0);
794 1.1 cgd else {
795 1.1 cgd soisdisconnecting(so);
796 1.1 cgd sbflush(&so->so_rcv);
797 1.1 cgd tp = tcp_usrclosed(tp);
798 1.1 cgd if (tp)
799 1.1 cgd (void) tcp_output(tp);
800 1.1 cgd }
801 1.1 cgd return (tp);
802 1.1 cgd }
803 1.1 cgd
804 1.1 cgd /*
805 1.1 cgd * User issued close, and wish to trail through shutdown states:
806 1.1 cgd * if never received SYN, just forget it. If got a SYN from peer,
807 1.1 cgd * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
808 1.1 cgd * If already got a FIN from peer, then almost done; go to LAST_ACK
809 1.1 cgd * state. In all other cases, have already sent FIN to peer (e.g.
810 1.1 cgd * after PRU_SHUTDOWN), and just have to play tedious game waiting
811 1.1 cgd * for peer to send FIN or not respond to keep-alives, etc.
812 1.1 cgd * We can let the user exit from the close as soon as the FIN is acked.
813 1.1 cgd */
814 1.1 cgd struct tcpcb *
815 1.1 cgd tcp_usrclosed(tp)
816 1.1 cgd register struct tcpcb *tp;
817 1.1 cgd {
818 1.1 cgd
819 1.1 cgd switch (tp->t_state) {
820 1.1 cgd
821 1.1 cgd case TCPS_CLOSED:
822 1.1 cgd case TCPS_LISTEN:
823 1.1 cgd case TCPS_SYN_SENT:
824 1.1 cgd tp->t_state = TCPS_CLOSED;
825 1.1 cgd tp = tcp_close(tp);
826 1.1 cgd break;
827 1.1 cgd
828 1.1 cgd case TCPS_SYN_RECEIVED:
829 1.1 cgd case TCPS_ESTABLISHED:
830 1.1 cgd tp->t_state = TCPS_FIN_WAIT_1;
831 1.1 cgd break;
832 1.1 cgd
833 1.1 cgd case TCPS_CLOSE_WAIT:
834 1.1 cgd tp->t_state = TCPS_LAST_ACK;
835 1.1 cgd break;
836 1.1 cgd }
837 1.18 mycroft if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
838 1.39.10.1 itojun struct socket *so;
839 1.39.10.1 itojun if (tp->t_inpcb)
840 1.39.10.1 itojun so = tp->t_inpcb->inp_socket;
841 1.39.10.1 itojun #ifdef INET6
842 1.39.10.1 itojun else if (tp->t_in6pcb)
843 1.39.10.1 itojun so = tp->t_in6pcb->in6p_socket;
844 1.39.10.1 itojun #endif
845 1.39.10.1 itojun else
846 1.39.10.1 itojun so = NULL;
847 1.39.10.1 itojun soisdisconnected(so);
848 1.19 mycroft /*
849 1.19 mycroft * If we are in FIN_WAIT_2, we arrived here because the
850 1.19 mycroft * application did a shutdown of the send side. Like the
851 1.19 mycroft * case of a transition from FIN_WAIT_1 to FIN_WAIT_2 after
852 1.19 mycroft * a full close, we start a timer to make sure sockets are
853 1.19 mycroft * not left in FIN_WAIT_2 forever.
854 1.19 mycroft */
855 1.38 mouse if ((tp->t_state == TCPS_FIN_WAIT_2) && (tcp_maxidle > 0))
856 1.37 thorpej TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
857 1.18 mycroft }
858 1.1 cgd return (tp);
859 1.17 thorpej }
860 1.17 thorpej
861 1.38 mouse static struct {
862 1.38 mouse unsigned int valid : 1;
863 1.38 mouse unsigned int rdonly : 1;
864 1.38 mouse int *var;
865 1.38 mouse int val;
866 1.38 mouse } tcp_ctlvars[] = TCPCTL_VARIABLES;
867 1.36 matt
868 1.17 thorpej /*
869 1.17 thorpej * Sysctl for tcp variables.
870 1.17 thorpej */
871 1.17 thorpej int
872 1.17 thorpej tcp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
873 1.17 thorpej int *name;
874 1.17 thorpej u_int namelen;
875 1.17 thorpej void *oldp;
876 1.17 thorpej size_t *oldlenp;
877 1.17 thorpej void *newp;
878 1.17 thorpej size_t newlen;
879 1.17 thorpej {
880 1.17 thorpej /* All sysctl names at this level are terminal. */
881 1.17 thorpej if (namelen != 1)
882 1.17 thorpej return (ENOTDIR);
883 1.17 thorpej
884 1.36 matt if (name[0] < sizeof(tcp_ctlvars)/sizeof(tcp_ctlvars[0])
885 1.39 tv && tcp_ctlvars[name[0]].valid) {
886 1.38 mouse if (tcp_ctlvars[name[0]].rdonly)
887 1.38 mouse return (sysctl_rdint(oldp, oldlenp, newp,
888 1.38 mouse tcp_ctlvars[name[0]].val));
889 1.38 mouse else
890 1.38 mouse return (sysctl_int(oldp, oldlenp, newp, newlen,
891 1.38 mouse tcp_ctlvars[name[0]].var));
892 1.39 tv }
893 1.38 mouse
894 1.36 matt return (ENOPROTOOPT);
895 1.1 cgd }
896