]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/tcp_usrreq.c
This commit was generated by cvs2svn to compensate for changes in r97241,
[FreeBSD/FreeBSD.git] / sys / netinet / tcp_usrreq.c
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      From: @(#)tcp_usrreq.c  8.2 (Berkeley) 1/3/94
34  * $FreeBSD$
35  */
36
37 #include "opt_ipsec.h"
38 #include "opt_inet6.h"
39 #include "opt_tcpdebug.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/sysctl.h>
45 #include <sys/mbuf.h>
46 #ifdef INET6
47 #include <sys/domain.h>
48 #endif /* INET6 */
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/protosw.h>
52 #include <sys/proc.h>
53 #include <sys/jail.h>
54
55 #include <net/if.h>
56 #include <net/route.h>
57
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #ifdef INET6
61 #include <netinet/ip6.h>
62 #endif
63 #include <netinet/in_pcb.h>
64 #ifdef INET6
65 #include <netinet6/in6_pcb.h>
66 #endif
67 #include <netinet/in_var.h>
68 #include <netinet/ip_var.h>
69 #ifdef INET6
70 #include <netinet6/ip6_var.h>
71 #endif
72 #include <netinet/tcp.h>
73 #include <netinet/tcp_fsm.h>
74 #include <netinet/tcp_seq.h>
75 #include <netinet/tcp_timer.h>
76 #include <netinet/tcp_var.h>
77 #include <netinet/tcpip.h>
78 #ifdef TCPDEBUG
79 #include <netinet/tcp_debug.h>
80 #endif
81
82 #ifdef IPSEC
83 #include <netinet6/ipsec.h>
84 #endif /*IPSEC*/
85
86 /*
87  * TCP protocol interface to socket abstraction.
88  */
89 extern  char *tcpstates[];      /* XXX ??? */
90
91 static int      tcp_attach(struct socket *, struct thread *td);
92 static int      tcp_connect(struct tcpcb *, struct sockaddr *,
93                     struct thread *td);
94 #ifdef INET6
95 static int      tcp6_connect(struct tcpcb *, struct sockaddr *,
96                     struct thread *td);
97 #endif /* INET6 */
98 static struct tcpcb *
99                 tcp_disconnect(struct tcpcb *);
100 static struct tcpcb *
101                 tcp_usrclosed(struct tcpcb *);
102
103 #ifdef TCPDEBUG
104 #define TCPDEBUG0       int ostate = 0
105 #define TCPDEBUG1()     ostate = tp ? tp->t_state : 0
106 #define TCPDEBUG2(req)                                                          \
107         do {                                                                    \
108                 if (tp != 0) {                                                  \
109                         SOCK_LOCK(so);                                          \
110                         if (so->so_options & SO_DEBUG) {                        \
111                                 SOCK_UNLOCK(so);                                \
112                                 tcp_trace(TA_USER, ostate, tp, 0, 0, req);      \
113                         } else                                                  \
114                                 SOCK_UNLOCK(so);                                \
115                 }                                                               \
116         } while(0)
117 #else
118 #define TCPDEBUG0
119 #define TCPDEBUG1()
120 #define TCPDEBUG2(req)
121 #endif
122
123 /*
124  * TCP attaches to socket via pru_attach(), reserving space,
125  * and an internet control block.
126  */
127 static int
128 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
129 {
130         int s = splnet();
131         int error;
132         struct inpcb *inp = sotoinpcb(so);
133         struct tcpcb *tp = 0;
134         TCPDEBUG0;
135
136         TCPDEBUG1();
137         if (inp) {
138                 error = EISCONN;
139                 goto out;
140         }
141
142         error = tcp_attach(so, td);
143         if (error)
144                 goto out;
145
146         SOCK_LOCK(so);
147         if ((so->so_options & SO_LINGER) && so->so_linger == 0)
148                 so->so_linger = TCP_LINGERTIME;
149         SOCK_UNLOCK(so);
150         tp = sototcpcb(so);
151 out:
152         TCPDEBUG2(PRU_ATTACH);
153         splx(s);
154         return error;
155 }
156
157 /*
158  * pru_detach() detaches the TCP protocol from the socket.
159  * If the protocol state is non-embryonic, then can't
160  * do this directly: have to initiate a pru_disconnect(),
161  * which may finish later; embryonic TCB's can just
162  * be discarded here.
163  */
164 static int
165 tcp_usr_detach(struct socket *so)
166 {
167         int s = splnet();
168         int error = 0;
169         struct inpcb *inp = sotoinpcb(so);
170         struct tcpcb *tp;
171         TCPDEBUG0;
172
173         if (inp == 0) {
174                 splx(s);
175                 return EINVAL;  /* XXX */
176         }
177         tp = intotcpcb(inp);
178         TCPDEBUG1();
179         tp = tcp_disconnect(tp);
180
181         TCPDEBUG2(PRU_DETACH);
182         splx(s);
183         return error;
184 }
185
186 #define COMMON_START()  TCPDEBUG0; \
187                         do { \
188                                      if (inp == 0) { \
189                                              splx(s); \
190                                              return EINVAL; \
191                                      } \
192                                      tp = intotcpcb(inp); \
193                                      TCPDEBUG1(); \
194                      } while(0)
195                              
196 #define COMMON_END(req) out: TCPDEBUG2(req); splx(s); return error; goto out
197
198
199 /*
200  * Give the socket an address.
201  */
202 static int
203 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
204 {
205         int s = splnet();
206         int error = 0;
207         struct inpcb *inp = sotoinpcb(so);
208         struct tcpcb *tp;
209         struct sockaddr_in *sinp;
210
211         COMMON_START();
212
213         /*
214          * Must check for multicast addresses and disallow binding
215          * to them.
216          */
217         sinp = (struct sockaddr_in *)nam;
218         if (sinp->sin_family == AF_INET &&
219             IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
220                 error = EAFNOSUPPORT;
221                 goto out;
222         }
223         error = in_pcbbind(inp, nam, td);
224         if (error)
225                 goto out;
226         COMMON_END(PRU_BIND);
227
228 }
229
230 #ifdef INET6
231 static int
232 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
233 {
234         int s = splnet();
235         int error = 0;
236         struct inpcb *inp = sotoinpcb(so);
237         struct tcpcb *tp;
238         struct sockaddr_in6 *sin6p;
239
240         COMMON_START();
241
242         /*
243          * Must check for multicast addresses and disallow binding
244          * to them.
245          */
246         sin6p = (struct sockaddr_in6 *)nam;
247         if (sin6p->sin6_family == AF_INET6 &&
248             IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
249                 error = EAFNOSUPPORT;
250                 goto out;
251         }
252         inp->inp_vflag &= ~INP_IPV4;
253         inp->inp_vflag |= INP_IPV6;
254         if (ip6_mapped_addr_on && (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
255                 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
256                         inp->inp_vflag |= INP_IPV4;
257                 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
258                         struct sockaddr_in sin;
259
260                         in6_sin6_2_sin(&sin, sin6p);
261                         inp->inp_vflag |= INP_IPV4;
262                         inp->inp_vflag &= ~INP_IPV6;
263                         error = in_pcbbind(inp, (struct sockaddr *)&sin, td);
264                         goto out;
265                 }
266         }
267         error = in6_pcbbind(inp, nam, td);
268         if (error)
269                 goto out;
270         COMMON_END(PRU_BIND);
271 }
272 #endif /* INET6 */
273
274 /*
275  * Prepare to accept connections.
276  */
277 static int
278 tcp_usr_listen(struct socket *so, struct thread *td)
279 {
280         int s = splnet();
281         int error = 0;
282         struct inpcb *inp = sotoinpcb(so);
283         struct tcpcb *tp;
284
285         COMMON_START();
286         if (inp->inp_lport == 0)
287                 error = in_pcbbind(inp, (struct sockaddr *)0, td);
288         if (error == 0)
289                 tp->t_state = TCPS_LISTEN;
290         COMMON_END(PRU_LISTEN);
291 }
292
293 #ifdef INET6
294 static int
295 tcp6_usr_listen(struct socket *so, struct thread *td)
296 {
297         int s = splnet();
298         int error = 0;
299         struct inpcb *inp = sotoinpcb(so);
300         struct tcpcb *tp;
301
302         COMMON_START();
303         if (inp->inp_lport == 0) {
304                 inp->inp_vflag &= ~INP_IPV4;
305                 if (ip6_mapped_addr_on &&
306                     (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
307                         inp->inp_vflag |= INP_IPV4;
308                 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
309         }
310         if (error == 0)
311                 tp->t_state = TCPS_LISTEN;
312         COMMON_END(PRU_LISTEN);
313 }
314 #endif /* INET6 */
315
316 /*
317  * Initiate connection to peer.
318  * Create a template for use in transmissions on this connection.
319  * Enter SYN_SENT state, and mark socket as connecting.
320  * Start keep-alive timer, and seed output sequence space.
321  * Send initial segment on connection.
322  */
323 static int
324 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
325 {
326         int s = splnet();
327         int error = 0;
328         struct inpcb *inp = sotoinpcb(so);
329         struct tcpcb *tp;
330         struct sockaddr_in *sinp;
331
332         COMMON_START();
333
334         /*
335          * Must disallow TCP ``connections'' to multicast addresses.
336          */
337         sinp = (struct sockaddr_in *)nam;
338         if (sinp->sin_family == AF_INET
339             && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr))) {
340                 error = EAFNOSUPPORT;
341                 goto out;
342         }
343
344         if (td && jailed(td->td_ucred))
345                 prison_remote_ip(td->td_ucred, 0, &sinp->sin_addr.s_addr);
346
347         if ((error = tcp_connect(tp, nam, td)) != 0)
348                 goto out;
349         error = tcp_output(tp);
350         COMMON_END(PRU_CONNECT);
351 }
352
353 #ifdef INET6
354 static int
355 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
356 {
357         int s = splnet();
358         int error = 0;
359         struct inpcb *inp = sotoinpcb(so);
360         struct tcpcb *tp;
361         struct sockaddr_in6 *sin6p;
362
363         COMMON_START();
364
365         /*
366          * Must disallow TCP ``connections'' to multicast addresses.
367          */
368         sin6p = (struct sockaddr_in6 *)nam;
369         if (sin6p->sin6_family == AF_INET6
370             && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr)) {
371                 error = EAFNOSUPPORT;
372                 goto out;
373         }
374
375         if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
376                 struct sockaddr_in sin;
377
378                 if (!ip6_mapped_addr_on ||
379                     (inp->inp_flags & IN6P_IPV6_V6ONLY))
380                         return(EINVAL);
381
382                 in6_sin6_2_sin(&sin, sin6p);
383                 inp->inp_vflag |= INP_IPV4;
384                 inp->inp_vflag &= ~INP_IPV6;
385                 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
386                         goto out;
387                 error = tcp_output(tp);
388                 goto out;
389         }
390         inp->inp_vflag &= ~INP_IPV4;
391         inp->inp_vflag |= INP_IPV6;
392         inp->inp_inc.inc_isipv6 = 1;
393         if ((error = tcp6_connect(tp, nam, td)) != 0)
394                 goto out;
395         error = tcp_output(tp);
396         COMMON_END(PRU_CONNECT);
397 }
398 #endif /* INET6 */
399
400 /*
401  * Initiate disconnect from peer.
402  * If connection never passed embryonic stage, just drop;
403  * else if don't need to let data drain, then can just drop anyways,
404  * else have to begin TCP shutdown process: mark socket disconnecting,
405  * drain unread data, state switch to reflect user close, and
406  * send segment (e.g. FIN) to peer.  Socket will be really disconnected
407  * when peer sends FIN and acks ours.
408  *
409  * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
410  */
411 static int
412 tcp_usr_disconnect(struct socket *so)
413 {
414         int s = splnet();
415         int error = 0;
416         struct inpcb *inp = sotoinpcb(so);
417         struct tcpcb *tp;
418
419         COMMON_START();
420         tp = tcp_disconnect(tp);
421         COMMON_END(PRU_DISCONNECT);
422 }
423
424 /*
425  * Accept a connection.  Essentially all the work is
426  * done at higher levels; just return the address
427  * of the peer, storing through addr.
428  */
429 static int
430 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
431 {
432         int s = splnet();
433         int error = 0;
434         struct inpcb *inp = sotoinpcb(so);
435         struct tcpcb *tp = NULL;
436         TCPDEBUG0;
437
438         SOCK_LOCK(so);
439         if (so->so_state & SS_ISDISCONNECTED) {
440                 SOCK_UNLOCK(so);
441                 error = ECONNABORTED;
442                 goto out;
443         }
444         SOCK_UNLOCK(so);
445         if (inp == 0) {
446                 splx(s);
447                 return (EINVAL);
448         }
449         tp = intotcpcb(inp);
450         TCPDEBUG1();
451         in_setpeeraddr(so, nam);
452         COMMON_END(PRU_ACCEPT);
453 }
454
455 #ifdef INET6
456 static int
457 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
458 {
459         int s = splnet();
460         int error = 0;
461         struct inpcb *inp = sotoinpcb(so);
462         struct tcpcb *tp = NULL;
463         TCPDEBUG0;
464
465         SOCK_LOCK(so);
466         if (so->so_state & SS_ISDISCONNECTED) {
467                 SOCK_UNLOCK(so);
468                 error = ECONNABORTED;
469                 goto out;
470         }
471         SOCK_UNLOCK(so);
472         if (inp == 0) {
473                 splx(s);
474                 return (EINVAL);
475         }
476         tp = intotcpcb(inp);
477         TCPDEBUG1();
478         in6_mapped_peeraddr(so, nam);
479         COMMON_END(PRU_ACCEPT);
480 }
481 #endif /* INET6 */
482 /*
483  * Mark the connection as being incapable of further output.
484  */
485 static int
486 tcp_usr_shutdown(struct socket *so)
487 {
488         int s = splnet();
489         int error = 0;
490         struct inpcb *inp = sotoinpcb(so);
491         struct tcpcb *tp;
492
493         COMMON_START();
494         socantsendmore(so);
495         tp = tcp_usrclosed(tp);
496         if (tp)
497                 error = tcp_output(tp);
498         COMMON_END(PRU_SHUTDOWN);
499 }
500
501 /*
502  * After a receive, possibly send window update to peer.
503  */
504 static int
505 tcp_usr_rcvd(struct socket *so, int flags)
506 {
507         int s = splnet();
508         int error = 0;
509         struct inpcb *inp = sotoinpcb(so);
510         struct tcpcb *tp;
511
512         COMMON_START();
513         tcp_output(tp);
514         COMMON_END(PRU_RCVD);
515 }
516
517 /*
518  * Do a send by putting data in output queue and updating urgent
519  * marker if URG set.  Possibly send more data.  Unlike the other
520  * pru_*() routines, the mbuf chains are our responsibility.  We
521  * must either enqueue them or free them.  The other pru_* routines
522  * generally are caller-frees.
523  */
524 static int
525 tcp_usr_send(struct socket *so, int flags, struct mbuf *m, 
526              struct sockaddr *nam, struct mbuf *control, struct thread *td)
527 {
528         int s = splnet();
529         int error = 0;
530         struct inpcb *inp = sotoinpcb(so);
531         struct tcpcb *tp;
532 #ifdef INET6
533         int isipv6;
534 #endif
535         TCPDEBUG0;
536
537         if (inp == NULL) {
538                 /*
539                  * OOPS! we lost a race, the TCP session got reset after
540                  * we checked SS_CANTSENDMORE, eg: while doing uiomove or a
541                  * network interrupt in the non-splnet() section of sosend().
542                  */
543                 if (m)
544                         m_freem(m);
545                 if (control)
546                         m_freem(control);
547                 error = ECONNRESET;     /* XXX EPIPE? */
548                 tp = NULL;
549                 TCPDEBUG1();
550                 goto out;
551         }
552 #ifdef INET6
553         isipv6 = nam && nam->sa_family == AF_INET6;
554 #endif /* INET6 */
555         tp = intotcpcb(inp);
556         TCPDEBUG1();
557         if (control) {
558                 /* TCP doesn't do control messages (rights, creds, etc) */
559                 if (control->m_len) {
560                         m_freem(control);
561                         if (m)
562                                 m_freem(m);
563                         error = EINVAL;
564                         goto out;
565                 }
566                 m_freem(control);       /* empty control, just free it */
567         }
568         if(!(flags & PRUS_OOB)) {
569                 sbappend(&so->so_snd, m);
570                 if (nam && tp->t_state < TCPS_SYN_SENT) {
571                         /*
572                          * Do implied connect if not yet connected,
573                          * initialize window to default value, and
574                          * initialize maxseg/maxopd using peer's cached
575                          * MSS.
576                          */
577 #ifdef INET6
578                         if (isipv6)
579                                 error = tcp6_connect(tp, nam, td);
580                         else
581 #endif /* INET6 */
582                         error = tcp_connect(tp, nam, td);
583                         if (error)
584                                 goto out;
585                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
586                         tcp_mss(tp, -1);
587                 }
588
589                 if (flags & PRUS_EOF) {
590                         /*
591                          * Close the send side of the connection after
592                          * the data is sent.
593                          */
594                         socantsendmore(so);
595                         tp = tcp_usrclosed(tp);
596                 }
597                 if (tp != NULL) {
598                         if (flags & PRUS_MORETOCOME)
599                                 tp->t_flags |= TF_MORETOCOME;
600                         error = tcp_output(tp);
601                         if (flags & PRUS_MORETOCOME)
602                                 tp->t_flags &= ~TF_MORETOCOME;
603                 }
604         } else {
605                 if (sbspace(&so->so_snd) < -512) {
606                         m_freem(m);
607                         error = ENOBUFS;
608                         goto out;
609                 }
610                 /*
611                  * According to RFC961 (Assigned Protocols),
612                  * the urgent pointer points to the last octet
613                  * of urgent data.  We continue, however,
614                  * to consider it to indicate the first octet
615                  * of data past the urgent section.
616                  * Otherwise, snd_up should be one lower.
617                  */
618                 sbappend(&so->so_snd, m);
619                 if (nam && tp->t_state < TCPS_SYN_SENT) {
620                         /*
621                          * Do implied connect if not yet connected,
622                          * initialize window to default value, and
623                          * initialize maxseg/maxopd using peer's cached
624                          * MSS.
625                          */
626 #ifdef INET6
627                         if (isipv6)
628                                 error = tcp6_connect(tp, nam, td);
629                         else
630 #endif /* INET6 */
631                         error = tcp_connect(tp, nam, td);
632                         if (error)
633                                 goto out;
634                         tp->snd_wnd = TTCP_CLIENT_SND_WND;
635                         tcp_mss(tp, -1);
636                 }
637                 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
638                 tp->t_force = 1;
639                 error = tcp_output(tp);
640                 tp->t_force = 0;
641         }
642         COMMON_END((flags & PRUS_OOB) ? PRU_SENDOOB : 
643                    ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
644 }
645
646 /*
647  * Abort the TCP.
648  */
649 static int
650 tcp_usr_abort(struct socket *so)
651 {
652         int s = splnet();
653         int error = 0;
654         struct inpcb *inp = sotoinpcb(so);
655         struct tcpcb *tp;
656
657         COMMON_START();
658         tp = tcp_drop(tp, ECONNABORTED);
659         COMMON_END(PRU_ABORT);
660 }
661
662 /*
663  * Receive out-of-band data.
664  */
665 static int
666 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
667 {
668         int s = splnet();
669         int error = 0;
670         struct inpcb *inp = sotoinpcb(so);
671         struct tcpcb *tp;
672
673         COMMON_START();
674         SOCK_LOCK(so);
675         if ((so->so_oobmark == 0 &&
676              (so->so_state & SS_RCVATMARK) == 0) ||
677             so->so_options & SO_OOBINLINE) {
678                 SOCK_UNLOCK(so);
679                 error = EINVAL;
680                 goto out;
681         }
682         SOCK_UNLOCK(so);
683         if (tp->t_oobflags & TCPOOB_HADDATA) {
684                 error = EINVAL;
685                 goto out;
686         }
687         if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
688                 error = EWOULDBLOCK;
689                 goto out;
690         }
691         m->m_len = 1;
692         *mtod(m, caddr_t) = tp->t_iobc;
693         if ((flags & MSG_PEEK) == 0)
694                 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
695         COMMON_END(PRU_RCVOOB);
696 }
697
698 /* xxx - should be const */
699 struct pr_usrreqs tcp_usrreqs = {
700         tcp_usr_abort, tcp_usr_accept, tcp_usr_attach, tcp_usr_bind,
701         tcp_usr_connect, pru_connect2_notsupp, in_control, tcp_usr_detach,
702         tcp_usr_disconnect, tcp_usr_listen, in_setpeeraddr, tcp_usr_rcvd,
703         tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
704         in_setsockaddr, sosend, soreceive, sopoll
705 };
706
707 #ifdef INET6
708 struct pr_usrreqs tcp6_usrreqs = {
709         tcp_usr_abort, tcp6_usr_accept, tcp_usr_attach, tcp6_usr_bind,
710         tcp6_usr_connect, pru_connect2_notsupp, in6_control, tcp_usr_detach,
711         tcp_usr_disconnect, tcp6_usr_listen, in6_mapped_peeraddr, tcp_usr_rcvd,
712         tcp_usr_rcvoob, tcp_usr_send, pru_sense_null, tcp_usr_shutdown,
713         in6_mapped_sockaddr, sosend, soreceive, sopoll
714 };
715 #endif /* INET6 */
716
717 /*
718  * Common subroutine to open a TCP connection to remote host specified
719  * by struct sockaddr_in in mbuf *nam.  Call in_pcbbind to assign a local
720  * port number if needed.  Call in_pcbladdr to do the routing and to choose
721  * a local host address (interface).  If there is an existing incarnation
722  * of the same connection in TIME-WAIT state and if the remote host was
723  * sending CC options and if the connection duration was < MSL, then
724  * truncate the previous TIME-WAIT state and proceed.
725  * Initialize connection parameters and enter SYN-SENT state.
726  */
727 static int
728 tcp_connect(tp, nam, td)
729         register struct tcpcb *tp;
730         struct sockaddr *nam;
731         struct thread *td;
732 {
733         struct inpcb *inp = tp->t_inpcb, *oinp;
734         struct socket *so = inp->inp_socket;
735         struct tcpcb *otp;
736         struct sockaddr_in *sin = (struct sockaddr_in *)nam;
737         struct sockaddr_in *ifaddr;
738         struct rmxp_tao *taop;
739         struct rmxp_tao tao_noncached;
740         int error;
741
742         if (inp->inp_lport == 0) {
743                 error = in_pcbbind(inp, (struct sockaddr *)0, td);
744                 if (error)
745                         return error;
746         }
747
748         /*
749          * Cannot simply call in_pcbconnect, because there might be an
750          * earlier incarnation of this same connection still in
751          * TIME_WAIT state, creating an ADDRINUSE error.
752          */
753         error = in_pcbladdr(inp, nam, &ifaddr);
754         if (error)
755                 return error;
756         oinp = in_pcblookup_hash(inp->inp_pcbinfo,
757             sin->sin_addr, sin->sin_port,
758             inp->inp_laddr.s_addr != INADDR_ANY ? inp->inp_laddr
759                                                 : ifaddr->sin_addr,
760             inp->inp_lport,  0, NULL);
761         if (oinp) {
762                 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
763                 otp->t_state == TCPS_TIME_WAIT &&
764                     (ticks - otp->t_starttime) < tcp_msl &&
765                     (otp->t_flags & TF_RCVD_CC))
766                         otp = tcp_close(otp);
767                 else
768                         return EADDRINUSE;
769         }
770         if (inp->inp_laddr.s_addr == INADDR_ANY)
771                 inp->inp_laddr = ifaddr->sin_addr;
772         inp->inp_faddr = sin->sin_addr;
773         inp->inp_fport = sin->sin_port;
774         in_pcbrehash(inp);
775
776         /* Compute window scaling to request.  */
777         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
778             (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
779                 tp->request_r_scale++;
780
781         SOCK_LOCK(so);
782         soisconnecting(so);
783         SOCK_UNLOCK(so);
784         tcpstat.tcps_connattempt++;
785         tp->t_state = TCPS_SYN_SENT;
786         callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
787         tp->iss = tcp_new_isn(tp);
788         tcp_sendseqinit(tp);
789
790         /*
791          * Generate a CC value for this connection and
792          * check whether CC or CCnew should be used.
793          */
794         if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
795                 taop = &tao_noncached;
796                 bzero(taop, sizeof(*taop));
797         }
798
799         tp->cc_send = CC_INC(tcp_ccgen);
800         if (taop->tao_ccsent != 0 &&
801             CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
802                 taop->tao_ccsent = tp->cc_send;
803         } else {
804                 taop->tao_ccsent = 0;
805                 tp->t_flags |= TF_SENDCCNEW;
806         }
807
808         return 0;
809 }
810
811 #ifdef INET6
812 static int
813 tcp6_connect(tp, nam, td)
814         register struct tcpcb *tp;
815         struct sockaddr *nam;
816         struct thread *td;
817 {
818         struct inpcb *inp = tp->t_inpcb, *oinp;
819         struct socket *so = inp->inp_socket;
820         struct tcpcb *otp;
821         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
822         struct in6_addr *addr6;
823         struct rmxp_tao *taop;
824         struct rmxp_tao tao_noncached;
825         int error;
826
827         if (inp->inp_lport == 0) {
828                 error = in6_pcbbind(inp, (struct sockaddr *)0, td);
829                 if (error)
830                         return error;
831         }
832
833         /*
834          * Cannot simply call in_pcbconnect, because there might be an
835          * earlier incarnation of this same connection still in
836          * TIME_WAIT state, creating an ADDRINUSE error.
837          */
838         error = in6_pcbladdr(inp, nam, &addr6);
839         if (error)
840                 return error;
841         oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
842                                   &sin6->sin6_addr, sin6->sin6_port,
843                                   IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
844                                   ? addr6
845                                   : &inp->in6p_laddr,
846                                   inp->inp_lport,  0, NULL);
847         if (oinp) {
848                 if (oinp != inp && (otp = intotcpcb(oinp)) != NULL &&
849                     otp->t_state == TCPS_TIME_WAIT &&
850                     (ticks - otp->t_starttime) < tcp_msl &&
851                     (otp->t_flags & TF_RCVD_CC))
852                         otp = tcp_close(otp);
853                 else
854                         return EADDRINUSE;
855         }
856         if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
857                 inp->in6p_laddr = *addr6;
858         inp->in6p_faddr = sin6->sin6_addr;
859         inp->inp_fport = sin6->sin6_port;
860         if ((sin6->sin6_flowinfo & IPV6_FLOWINFO_MASK) != NULL)
861                 inp->in6p_flowinfo = sin6->sin6_flowinfo;
862         in_pcbrehash(inp);
863
864         /* Compute window scaling to request.  */
865         while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
866             (TCP_MAXWIN << tp->request_r_scale) < so->so_rcv.sb_hiwat)
867                 tp->request_r_scale++;
868
869         SOCK_LOCK(so);
870         soisconnecting(so);
871         SOCK_UNLOCK(so);
872         tcpstat.tcps_connattempt++;
873         tp->t_state = TCPS_SYN_SENT;
874         callout_reset(tp->tt_keep, tcp_keepinit, tcp_timer_keep, tp);
875         tp->iss = tcp_new_isn(tp);
876         tcp_sendseqinit(tp);
877
878         /*
879          * Generate a CC value for this connection and
880          * check whether CC or CCnew should be used.
881          */
882         if ((taop = tcp_gettaocache(&tp->t_inpcb->inp_inc)) == NULL) {
883                 taop = &tao_noncached;
884                 bzero(taop, sizeof(*taop));
885         }
886
887         tp->cc_send = CC_INC(tcp_ccgen);
888         if (taop->tao_ccsent != 0 &&
889             CC_GEQ(tp->cc_send, taop->tao_ccsent)) {
890                 taop->tao_ccsent = tp->cc_send;
891         } else {
892                 taop->tao_ccsent = 0;
893                 tp->t_flags |= TF_SENDCCNEW;
894         }
895
896         return 0;
897 }
898 #endif /* INET6 */
899
900 /*
901  * The new sockopt interface makes it possible for us to block in the
902  * copyin/out step (if we take a page fault).  Taking a page fault at
903  * splnet() is probably a Bad Thing.  (Since sockets and pcbs both now
904  * use TSM, there probably isn't any need for this function to run at
905  * splnet() any more.  This needs more examination.)
906  */
907 int
908 tcp_ctloutput(so, sopt)
909         struct socket *so;
910         struct sockopt *sopt;
911 {
912         int     error, opt, optval, s;
913         struct  inpcb *inp;
914         struct  tcpcb *tp;
915
916         error = 0;
917         s = splnet();           /* XXX */
918         inp = sotoinpcb(so);
919         if (inp == NULL) {
920                 splx(s);
921                 return (ECONNRESET);
922         }
923         if (sopt->sopt_level != IPPROTO_TCP) {
924 #ifdef INET6
925                 if (INP_CHECK_SOCKAF(so, AF_INET6))
926                         error = ip6_ctloutput(so, sopt);
927                 else
928 #endif /* INET6 */
929                 error = ip_ctloutput(so, sopt);
930                 splx(s);
931                 return (error);
932         }
933         tp = intotcpcb(inp);
934
935         switch (sopt->sopt_dir) {
936         case SOPT_SET:
937                 switch (sopt->sopt_name) {
938                 case TCP_NODELAY:
939                 case TCP_NOOPT:
940                         error = sooptcopyin(sopt, &optval, sizeof optval,
941                                             sizeof optval);
942                         if (error)
943                                 break;
944
945                         switch (sopt->sopt_name) {
946                         case TCP_NODELAY:
947                                 opt = TF_NODELAY;
948                                 break;
949                         case TCP_NOOPT:
950                                 opt = TF_NOOPT;
951                                 break;
952                         default:
953                                 opt = 0; /* dead code to fool gcc */
954                                 break;
955                         }
956
957                         if (optval)
958                                 tp->t_flags |= opt;
959                         else
960                                 tp->t_flags &= ~opt;
961                         break;
962
963                 case TCP_NOPUSH:
964                         error = sooptcopyin(sopt, &optval, sizeof optval,
965                                             sizeof optval);
966                         if (error)
967                                 break;
968
969                         if (optval)
970                                 tp->t_flags |= TF_NOPUSH;
971                         else {
972                                 tp->t_flags &= ~TF_NOPUSH;
973                                 error = tcp_output(tp);
974                         }
975                         break;
976
977                 case TCP_MAXSEG:
978                         error = sooptcopyin(sopt, &optval, sizeof optval,
979                                             sizeof optval);
980                         if (error)
981                                 break;
982
983                         if (optval > 0 && optval <= tp->t_maxseg)
984                                 tp->t_maxseg = optval;
985                         else
986                                 error = EINVAL;
987                         break;
988
989                 default:
990                         error = ENOPROTOOPT;
991                         break;
992                 }
993                 break;
994
995         case SOPT_GET:
996                 switch (sopt->sopt_name) {
997                 case TCP_NODELAY:
998                         optval = tp->t_flags & TF_NODELAY;
999                         break;
1000                 case TCP_MAXSEG:
1001                         optval = tp->t_maxseg;
1002                         break;
1003                 case TCP_NOOPT:
1004                         optval = tp->t_flags & TF_NOOPT;
1005                         break;
1006                 case TCP_NOPUSH:
1007                         optval = tp->t_flags & TF_NOPUSH;
1008                         break;
1009                 default:
1010                         error = ENOPROTOOPT;
1011                         break;
1012                 }
1013                 if (error == 0)
1014                         error = sooptcopyout(sopt, &optval, sizeof optval);
1015                 break;
1016         }
1017         splx(s);
1018         return (error);
1019 }
1020
1021 /*
1022  * tcp_sendspace and tcp_recvspace are the default send and receive window
1023  * sizes, respectively.  These are obsolescent (this information should
1024  * be set by the route).
1025  */
1026 u_long  tcp_sendspace = 1024*32;
1027 SYSCTL_INT(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW, 
1028     &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1029 u_long  tcp_recvspace = 1024*64;
1030 SYSCTL_INT(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 
1031     &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1032
1033 /*
1034  * Attach TCP protocol to socket, allocating
1035  * internet protocol control block, tcp control block,
1036  * bufer space, and entering LISTEN state if to accept connections.
1037  */
1038 static int
1039 tcp_attach(so, td)
1040         struct socket *so;
1041         struct thread *td;
1042 {
1043         register struct tcpcb *tp;
1044         struct inpcb *inp;
1045         int error;
1046 #ifdef INET6
1047         int isipv6 = INP_CHECK_SOCKAF(so, AF_INET6) != NULL;
1048 #endif
1049
1050         if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1051                 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1052                 if (error)
1053                         return (error);
1054         }
1055         error = in_pcballoc(so, &tcbinfo, td);
1056         if (error)
1057                 return (error);
1058         inp = sotoinpcb(so);
1059 #ifdef INET6
1060         if (isipv6) {
1061                 inp->inp_vflag |= INP_IPV6;
1062                 inp->in6p_hops = -1;    /* use kernel default */
1063         }
1064         else
1065 #endif
1066         inp->inp_vflag |= INP_IPV4;
1067         tp = tcp_newtcpcb(inp);
1068         if (tp == 0) {
1069                 int nofd;
1070
1071                 SOCK_LOCK(so);
1072                 nofd = so->so_state & SS_NOFDREF;       /* XXX */
1073                 so->so_state &= ~SS_NOFDREF;    /* don't free the socket yet */
1074                 SOCK_UNLOCK(so);
1075 #ifdef INET6
1076                 if (isipv6)
1077                         in6_pcbdetach(inp);
1078                 else
1079 #endif
1080                 in_pcbdetach(inp);
1081                 SOCK_LOCK(so);
1082                 so->so_state |= nofd;
1083                 SOCK_UNLOCK(so);
1084                 return (ENOBUFS);
1085         }
1086         tp->t_state = TCPS_CLOSED;
1087         return (0);
1088 }
1089
1090 /*
1091  * Initiate (or continue) disconnect.
1092  * If embryonic state, just send reset (once).
1093  * If in ``let data drain'' option and linger null, just drop.
1094  * Otherwise (hard), mark socket disconnecting and drop
1095  * current input data; switch states based on user close, and
1096  * send segment to peer (with FIN).
1097  */
1098 static struct tcpcb *
1099 tcp_disconnect(tp)
1100         register struct tcpcb *tp;
1101 {
1102         struct socket *so = tp->t_inpcb->inp_socket;
1103
1104         if (tp->t_state < TCPS_ESTABLISHED)
1105                 tp = tcp_close(tp);
1106         else {
1107                 SOCK_LOCK(so);
1108                 if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1109                         SOCK_UNLOCK(so);
1110                         tp = tcp_drop(tp, 0);
1111                 } else {
1112                         soisdisconnecting(so);
1113                         SOCK_UNLOCK(so);
1114                         sbflush(&so->so_rcv);
1115                         tp = tcp_usrclosed(tp);
1116                         if (tp)
1117                                 (void) tcp_output(tp);
1118                 }
1119         }
1120         return (tp);
1121 }
1122
1123 /*
1124  * User issued close, and wish to trail through shutdown states:
1125  * if never received SYN, just forget it.  If got a SYN from peer,
1126  * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1127  * If already got a FIN from peer, then almost done; go to LAST_ACK
1128  * state.  In all other cases, have already sent FIN to peer (e.g.
1129  * after PRU_SHUTDOWN), and just have to play tedious game waiting
1130  * for peer to send FIN or not respond to keep-alives, etc.
1131  * We can let the user exit from the close as soon as the FIN is acked.
1132  */
1133 static struct tcpcb *
1134 tcp_usrclosed(tp)
1135         register struct tcpcb *tp;
1136 {
1137
1138         switch (tp->t_state) {
1139
1140         case TCPS_CLOSED:
1141         case TCPS_LISTEN:
1142                 tp->t_state = TCPS_CLOSED;
1143                 tp = tcp_close(tp);
1144                 break;
1145
1146         case TCPS_SYN_SENT:
1147         case TCPS_SYN_RECEIVED:
1148                 tp->t_flags |= TF_NEEDFIN;
1149                 break;
1150
1151         case TCPS_ESTABLISHED:
1152                 tp->t_state = TCPS_FIN_WAIT_1;
1153                 break;
1154
1155         case TCPS_CLOSE_WAIT:
1156                 tp->t_state = TCPS_LAST_ACK;
1157                 break;
1158         }
1159         if (tp && tp->t_state >= TCPS_FIN_WAIT_2) {
1160                 SOCK_LOCK(tp->t_inpcb->inp_socket);
1161                 soisdisconnected(tp->t_inpcb->inp_socket);
1162                 SOCK_UNLOCK(tp->t_inpcb->inp_socket);
1163                 /* To prevent the connection hanging in FIN_WAIT_2 forever. */
1164                 if (tp->t_state == TCPS_FIN_WAIT_2)
1165                         callout_reset(tp->tt_2msl, tcp_maxidle,
1166                                       tcp_timer_2msl, tp);
1167         }
1168         return (tp);
1169 }
1170