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