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