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