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