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