1 /*-
2 * Copyright (c) 1982, 1986, 1988, 1993
3 * The Regents of the University of California.
4 * Copyright (c) 2006-2007 Robert N. M. Watson
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * From: @(#)tcp_usrreq.c 8.2 (Berkeley) 1/3/94
32 */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: releng/7.3/sys/netinet/tcp_usrreq.c 202914 2010-01-24 10:08:54Z bz $");
36
37 #include "opt_ddb.h"
38 #include "opt_inet.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/malloc.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/mbuf.h>
48 #ifdef INET6
49 #include <sys/domain.h>
50 #endif /* INET6 */
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/protosw.h>
54 #include <sys/proc.h>
55 #include <sys/jail.h>
56
57 #ifdef DDB
58 #include <ddb/ddb.h>
59 #endif
60
61 #include <net/if.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 #include <netinet6/scope6_var.h>
78 #endif
79 #include <netinet/tcp.h>
80 #include <netinet/tcp_fsm.h>
81 #include <netinet/tcp_seq.h>
82 #include <netinet/tcp_timer.h>
83 #include <netinet/tcp_var.h>
84 #include <netinet/tcpip.h>
85 #ifdef TCPDEBUG
86 #include <netinet/tcp_debug.h>
87 #endif
88 #include <netinet/tcp_offload.h>
89
90 /*
91 * TCP protocol interface to socket abstraction.
92 */
93 static int tcp_attach(struct socket *);
94 static int tcp_connect(struct tcpcb *, struct sockaddr *,
95 struct thread *td);
96 #ifdef INET6
97 static int tcp6_connect(struct tcpcb *, struct sockaddr *,
98 struct thread *td);
99 #endif /* INET6 */
100 static void tcp_disconnect(struct tcpcb *);
101 static void tcp_usrclosed(struct tcpcb *);
102 static void tcp_fill_info(struct tcpcb *, struct tcp_info *);
103
104 #ifdef TCPDEBUG
105 #define TCPDEBUG0 int ostate = 0
106 #define TCPDEBUG1() ostate = tp ? tp->t_state : 0
107 #define TCPDEBUG2(req) if (tp && (so->so_options & SO_DEBUG)) \
108 tcp_trace(TA_USER, ostate, tp, 0, 0, req)
109 #else
110 #define TCPDEBUG0
111 #define TCPDEBUG1()
112 #define TCPDEBUG2(req)
113 #endif
114
115 /*
116 * TCP attaches to socket via pru_attach(), reserving space,
117 * and an internet control block.
118 */
119 static int
120 tcp_usr_attach(struct socket *so, int proto, struct thread *td)
121 {
122 struct inpcb *inp;
123 struct tcpcb *tp = NULL;
124 int error;
125 TCPDEBUG0;
126
127 inp = sotoinpcb(so);
128 KASSERT(inp == NULL, ("tcp_usr_attach: inp != NULL"));
129 TCPDEBUG1();
130
131 error = tcp_attach(so);
132 if (error)
133 goto out;
134
135 if ((so->so_options & SO_LINGER) && so->so_linger == 0)
136 so->so_linger = TCP_LINGERTIME;
137
138 inp = sotoinpcb(so);
139 tp = intotcpcb(inp);
140 out:
141 TCPDEBUG2(PRU_ATTACH);
142 return error;
143 }
144
145 /*
146 * tcp_detach is called when the socket layer loses its final reference
147 * to the socket, be it a file descriptor reference, a reference from TCP,
148 * etc. At this point, there is only one case in which we will keep around
149 * inpcb state: time wait.
150 *
151 * This function can probably be re-absorbed back into tcp_usr_detach() now
152 * that there is a single detach path.
153 */
154 static void
155 tcp_detach(struct socket *so, struct inpcb *inp)
156 {
157 struct tcpcb *tp;
158
159 INP_INFO_WLOCK_ASSERT(&tcbinfo);
160 INP_WLOCK_ASSERT(inp);
161
162 KASSERT(so->so_pcb == inp, ("tcp_detach: so_pcb != inp"));
163 KASSERT(inp->inp_socket == so, ("tcp_detach: inp_socket != so"));
164
165 tp = intotcpcb(inp);
166
167 if (inp->inp_flags & INP_TIMEWAIT) {
168 /*
169 * There are two cases to handle: one in which the time wait
170 * state is being discarded (INP_DROPPED), and one in which
171 * this connection will remain in timewait. In the former,
172 * it is time to discard all state (except tcptw, which has
173 * already been discarded by the timewait close code, which
174 * should be further up the call stack somewhere). In the
175 * latter case, we detach from the socket, but leave the pcb
176 * present until timewait ends.
177 *
178 * XXXRW: Would it be cleaner to free the tcptw here?
179 */
180 if (inp->inp_flags & INP_DROPPED) {
181 KASSERT(tp == NULL, ("tcp_detach: INP_TIMEWAIT && "
182 "INP_DROPPED && tp != NULL"));
183 in_pcbdetach(inp);
184 in_pcbfree(inp);
185 } else {
186 in_pcbdetach(inp);
187 INP_WUNLOCK(inp);
188 }
189 } else {
190 /*
191 * If the connection is not in timewait, we consider two
192 * two conditions: one in which no further processing is
193 * necessary (dropped || embryonic), and one in which TCP is
194 * not yet done, but no longer requires the socket, so the
195 * pcb will persist for the time being.
196 *
197 * XXXRW: Does the second case still occur?
198 */
199 if (inp->inp_flags & INP_DROPPED ||
200 tp->t_state < TCPS_SYN_SENT) {
201 tcp_discardcb(tp);
202 in_pcbdetach(inp);
203 in_pcbfree(inp);
204 } else
205 in_pcbdetach(inp);
206 }
207 }
208
209 /*
210 * pru_detach() detaches the TCP protocol from the socket.
211 * If the protocol state is non-embryonic, then can't
212 * do this directly: have to initiate a pru_disconnect(),
213 * which may finish later; embryonic TCB's can just
214 * be discarded here.
215 */
216 static void
217 tcp_usr_detach(struct socket *so)
218 {
219 struct inpcb *inp;
220
221 inp = sotoinpcb(so);
222 KASSERT(inp != NULL, ("tcp_usr_detach: inp == NULL"));
223 INP_INFO_WLOCK(&tcbinfo);
224 INP_WLOCK(inp);
225 KASSERT(inp->inp_socket != NULL,
226 ("tcp_usr_detach: inp_socket == NULL"));
227 tcp_detach(so, inp);
228 INP_INFO_WUNLOCK(&tcbinfo);
229 }
230
231 /*
232 * Give the socket an address.
233 */
234 static int
235 tcp_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
236 {
237 int error = 0;
238 struct inpcb *inp;
239 struct tcpcb *tp = NULL;
240 struct sockaddr_in *sinp;
241
242 sinp = (struct sockaddr_in *)nam;
243 if (nam->sa_len != sizeof (*sinp))
244 return (EINVAL);
245 /*
246 * Must check for multicast addresses and disallow binding
247 * to them.
248 */
249 if (sinp->sin_family == AF_INET &&
250 IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
251 return (EAFNOSUPPORT);
252
253 TCPDEBUG0;
254 INP_INFO_WLOCK(&tcbinfo);
255 inp = sotoinpcb(so);
256 KASSERT(inp != NULL, ("tcp_usr_bind: inp == NULL"));
257 INP_WLOCK(inp);
258 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
259 error = EINVAL;
260 goto out;
261 }
262 tp = intotcpcb(inp);
263 TCPDEBUG1();
264 error = in_pcbbind(inp, nam, td->td_ucred);
265 out:
266 TCPDEBUG2(PRU_BIND);
267 INP_WUNLOCK(inp);
268 INP_INFO_WUNLOCK(&tcbinfo);
269
270 return (error);
271 }
272
273 #ifdef INET6
274 static int
275 tcp6_usr_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
276 {
277 int error = 0;
278 struct inpcb *inp;
279 struct tcpcb *tp = NULL;
280 struct sockaddr_in6 *sin6p;
281
282 sin6p = (struct sockaddr_in6 *)nam;
283 if (nam->sa_len != sizeof (*sin6p))
284 return (EINVAL);
285 /*
286 * Must check for multicast addresses and disallow binding
287 * to them.
288 */
289 if (sin6p->sin6_family == AF_INET6 &&
290 IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
291 return (EAFNOSUPPORT);
292
293 TCPDEBUG0;
294 INP_INFO_WLOCK(&tcbinfo);
295 inp = sotoinpcb(so);
296 KASSERT(inp != NULL, ("tcp6_usr_bind: inp == NULL"));
297 INP_WLOCK(inp);
298 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
299 error = EINVAL;
300 goto out;
301 }
302 tp = intotcpcb(inp);
303 TCPDEBUG1();
304 inp->inp_vflag &= ~INP_IPV4;
305 inp->inp_vflag |= INP_IPV6;
306 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
307 if (IN6_IS_ADDR_UNSPECIFIED(&sin6p->sin6_addr))
308 inp->inp_vflag |= INP_IPV4;
309 else if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
310 struct sockaddr_in sin;
311
312 in6_sin6_2_sin(&sin, sin6p);
313 inp->inp_vflag |= INP_IPV4;
314 inp->inp_vflag &= ~INP_IPV6;
315 error = in_pcbbind(inp, (struct sockaddr *)&sin,
316 td->td_ucred);
317 goto out;
318 }
319 }
320 error = in6_pcbbind(inp, nam, td->td_ucred);
321 out:
322 TCPDEBUG2(PRU_BIND);
323 INP_WUNLOCK(inp);
324 INP_INFO_WUNLOCK(&tcbinfo);
325 return (error);
326 }
327 #endif /* INET6 */
328
329 /*
330 * Prepare to accept connections.
331 */
332 static int
333 tcp_usr_listen(struct socket *so, int backlog, struct thread *td)
334 {
335 int error = 0;
336 struct inpcb *inp;
337 struct tcpcb *tp = NULL;
338
339 TCPDEBUG0;
340 INP_INFO_WLOCK(&tcbinfo);
341 inp = sotoinpcb(so);
342 KASSERT(inp != NULL, ("tcp_usr_listen: inp == NULL"));
343 INP_WLOCK(inp);
344 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
345 error = EINVAL;
346 goto out;
347 }
348 tp = intotcpcb(inp);
349 TCPDEBUG1();
350 SOCK_LOCK(so);
351 error = solisten_proto_check(so);
352 if (error == 0 && inp->inp_lport == 0)
353 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
354 if (error == 0) {
355 tp->t_state = TCPS_LISTEN;
356 solisten_proto(so, backlog);
357 tcp_offload_listen_open(tp);
358 }
359 SOCK_UNLOCK(so);
360
361 out:
362 TCPDEBUG2(PRU_LISTEN);
363 INP_WUNLOCK(inp);
364 INP_INFO_WUNLOCK(&tcbinfo);
365 return (error);
366 }
367
368 #ifdef INET6
369 static int
370 tcp6_usr_listen(struct socket *so, int backlog, struct thread *td)
371 {
372 int error = 0;
373 struct inpcb *inp;
374 struct tcpcb *tp = NULL;
375
376 TCPDEBUG0;
377 INP_INFO_WLOCK(&tcbinfo);
378 inp = sotoinpcb(so);
379 KASSERT(inp != NULL, ("tcp6_usr_listen: inp == NULL"));
380 INP_WLOCK(inp);
381 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
382 error = EINVAL;
383 goto out;
384 }
385 tp = intotcpcb(inp);
386 TCPDEBUG1();
387 SOCK_LOCK(so);
388 error = solisten_proto_check(so);
389 if (error == 0 && inp->inp_lport == 0) {
390 inp->inp_vflag &= ~INP_IPV4;
391 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
392 inp->inp_vflag |= INP_IPV4;
393 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
394 }
395 if (error == 0) {
396 tp->t_state = TCPS_LISTEN;
397 solisten_proto(so, backlog);
398 }
399 SOCK_UNLOCK(so);
400
401 out:
402 TCPDEBUG2(PRU_LISTEN);
403 INP_WUNLOCK(inp);
404 INP_INFO_WUNLOCK(&tcbinfo);
405 return (error);
406 }
407 #endif /* INET6 */
408
409 /*
410 * Initiate connection to peer.
411 * Create a template for use in transmissions on this connection.
412 * Enter SYN_SENT state, and mark socket as connecting.
413 * Start keep-alive timer, and seed output sequence space.
414 * Send initial segment on connection.
415 */
416 static int
417 tcp_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
418 {
419 int error = 0;
420 struct inpcb *inp;
421 struct tcpcb *tp = NULL;
422 struct sockaddr_in *sinp;
423
424 sinp = (struct sockaddr_in *)nam;
425 if (nam->sa_len != sizeof (*sinp))
426 return (EINVAL);
427 /*
428 * Must disallow TCP ``connections'' to multicast addresses.
429 */
430 if (sinp->sin_family == AF_INET
431 && IN_MULTICAST(ntohl(sinp->sin_addr.s_addr)))
432 return (EAFNOSUPPORT);
433 if ((error = prison_remote_ip4(td->td_ucred, &sinp->sin_addr)) != 0)
434 return (error);
435
436 TCPDEBUG0;
437 INP_INFO_WLOCK(&tcbinfo);
438 inp = sotoinpcb(so);
439 KASSERT(inp != NULL, ("tcp_usr_connect: inp == NULL"));
440 INP_WLOCK(inp);
441 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
442 error = EINVAL;
443 goto out;
444 }
445 tp = intotcpcb(inp);
446 TCPDEBUG1();
447 if ((error = tcp_connect(tp, nam, td)) != 0)
448 goto out;
449 error = tcp_output_connect(so, nam);
450 out:
451 TCPDEBUG2(PRU_CONNECT);
452 INP_WUNLOCK(inp);
453 INP_INFO_WUNLOCK(&tcbinfo);
454 return (error);
455 }
456
457 #ifdef INET6
458 static int
459 tcp6_usr_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
460 {
461 int error = 0;
462 struct inpcb *inp;
463 struct tcpcb *tp = NULL;
464 struct sockaddr_in6 *sin6p;
465
466 TCPDEBUG0;
467
468 sin6p = (struct sockaddr_in6 *)nam;
469 if (nam->sa_len != sizeof (*sin6p))
470 return (EINVAL);
471 /*
472 * Must disallow TCP ``connections'' to multicast addresses.
473 */
474 if (sin6p->sin6_family == AF_INET6
475 && IN6_IS_ADDR_MULTICAST(&sin6p->sin6_addr))
476 return (EAFNOSUPPORT);
477
478 INP_INFO_WLOCK(&tcbinfo);
479 inp = sotoinpcb(so);
480 KASSERT(inp != NULL, ("tcp6_usr_connect: inp == NULL"));
481 INP_WLOCK(inp);
482 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
483 error = EINVAL;
484 goto out;
485 }
486 tp = intotcpcb(inp);
487 TCPDEBUG1();
488 if (IN6_IS_ADDR_V4MAPPED(&sin6p->sin6_addr)) {
489 struct sockaddr_in sin;
490
491 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
492 error = EINVAL;
493 goto out;
494 }
495
496 in6_sin6_2_sin(&sin, sin6p);
497 inp->inp_vflag |= INP_IPV4;
498 inp->inp_vflag &= ~INP_IPV6;
499 if ((error = prison_remote_ip4(td->td_ucred,
500 &sin.sin_addr)) != 0)
501 goto out;
502 if ((error = tcp_connect(tp, (struct sockaddr *)&sin, td)) != 0)
503 goto out;
504 error = tcp_output_connect(so, nam);
505 goto out;
506 }
507 inp->inp_vflag &= ~INP_IPV4;
508 inp->inp_vflag |= INP_IPV6;
509 inp->inp_inc.inc_flags |= INC_ISIPV6;
510 if ((error = prison_remote_ip6(td->td_ucred, &sin6p->sin6_addr)) != 0)
511 goto out;
512 if ((error = tcp6_connect(tp, nam, td)) != 0)
513 goto out;
514 error = tcp_output_connect(so, nam);
515
516 out:
517 TCPDEBUG2(PRU_CONNECT);
518 INP_WUNLOCK(inp);
519 INP_INFO_WUNLOCK(&tcbinfo);
520 return (error);
521 }
522 #endif /* INET6 */
523
524 /*
525 * Initiate disconnect from peer.
526 * If connection never passed embryonic stage, just drop;
527 * else if don't need to let data drain, then can just drop anyways,
528 * else have to begin TCP shutdown process: mark socket disconnecting,
529 * drain unread data, state switch to reflect user close, and
530 * send segment (e.g. FIN) to peer. Socket will be really disconnected
531 * when peer sends FIN and acks ours.
532 *
533 * SHOULD IMPLEMENT LATER PRU_CONNECT VIA REALLOC TCPCB.
534 */
535 static int
536 tcp_usr_disconnect(struct socket *so)
537 {
538 struct inpcb *inp;
539 struct tcpcb *tp = NULL;
540 int error = 0;
541
542 TCPDEBUG0;
543 INP_INFO_WLOCK(&tcbinfo);
544 inp = sotoinpcb(so);
545 KASSERT(inp != NULL, ("tcp_usr_disconnect: inp == NULL"));
546 INP_WLOCK(inp);
547 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
548 error = ECONNRESET;
549 goto out;
550 }
551 tp = intotcpcb(inp);
552 TCPDEBUG1();
553 tcp_disconnect(tp);
554 out:
555 TCPDEBUG2(PRU_DISCONNECT);
556 INP_WUNLOCK(inp);
557 INP_INFO_WUNLOCK(&tcbinfo);
558 return (error);
559 }
560
561 /*
562 * Accept a connection. Essentially all the work is
563 * done at higher levels; just return the address
564 * of the peer, storing through addr.
565 */
566 static int
567 tcp_usr_accept(struct socket *so, struct sockaddr **nam)
568 {
569 int error = 0;
570 struct inpcb *inp = NULL;
571 struct tcpcb *tp = NULL;
572 struct in_addr addr;
573 in_port_t port = 0;
574 TCPDEBUG0;
575
576 if (so->so_state & SS_ISDISCONNECTED)
577 return (ECONNABORTED);
578
579 inp = sotoinpcb(so);
580 KASSERT(inp != NULL, ("tcp_usr_accept: inp == NULL"));
581 INP_INFO_RLOCK(&tcbinfo);
582 INP_WLOCK(inp);
583 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
584 error = ECONNABORTED;
585 goto out;
586 }
587 tp = intotcpcb(inp);
588 TCPDEBUG1();
589
590 /*
591 * We inline in_getpeeraddr and COMMON_END here, so that we can
592 * copy the data of interest and defer the malloc until after we
593 * release the lock.
594 */
595 port = inp->inp_fport;
596 addr = inp->inp_faddr;
597
598 out:
599 TCPDEBUG2(PRU_ACCEPT);
600 INP_WUNLOCK(inp);
601 INP_INFO_RUNLOCK(&tcbinfo);
602 if (error == 0)
603 *nam = in_sockaddr(port, &addr);
604 return error;
605 }
606
607 #ifdef INET6
608 static int
609 tcp6_usr_accept(struct socket *so, struct sockaddr **nam)
610 {
611 struct inpcb *inp = NULL;
612 int error = 0;
613 struct tcpcb *tp = NULL;
614 struct in_addr addr;
615 struct in6_addr addr6;
616 in_port_t port = 0;
617 int v4 = 0;
618 TCPDEBUG0;
619
620 if (so->so_state & SS_ISDISCONNECTED)
621 return (ECONNABORTED);
622
623 inp = sotoinpcb(so);
624 KASSERT(inp != NULL, ("tcp6_usr_accept: inp == NULL"));
625 INP_WLOCK(inp);
626 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
627 error = ECONNABORTED;
628 goto out;
629 }
630 tp = intotcpcb(inp);
631 TCPDEBUG1();
632
633 /*
634 * We inline in6_mapped_peeraddr and COMMON_END here, so that we can
635 * copy the data of interest and defer the malloc until after we
636 * release the lock.
637 */
638 if (inp->inp_vflag & INP_IPV4) {
639 v4 = 1;
640 port = inp->inp_fport;
641 addr = inp->inp_faddr;
642 } else {
643 port = inp->inp_fport;
644 addr6 = inp->in6p_faddr;
645 }
646
647 out:
648 TCPDEBUG2(PRU_ACCEPT);
649 INP_WUNLOCK(inp);
650 if (error == 0) {
651 if (v4)
652 *nam = in6_v4mapsin6_sockaddr(port, &addr);
653 else
654 *nam = in6_sockaddr(port, &addr6);
655 }
656 return error;
657 }
658 #endif /* INET6 */
659
660 /*
661 * Mark the connection as being incapable of further output.
662 */
663 static int
664 tcp_usr_shutdown(struct socket *so)
665 {
666 int error = 0;
667 struct inpcb *inp;
668 struct tcpcb *tp = NULL;
669
670 TCPDEBUG0;
671 INP_INFO_WLOCK(&tcbinfo);
672 inp = sotoinpcb(so);
673 KASSERT(inp != NULL, ("inp == NULL"));
674 INP_WLOCK(inp);
675 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
676 error = ECONNRESET;
677 goto out;
678 }
679 tp = intotcpcb(inp);
680 TCPDEBUG1();
681 socantsendmore(so);
682 tcp_usrclosed(tp);
683 if (!(inp->inp_flags & INP_DROPPED))
684 error = tcp_output_disconnect(tp);
685
686 out:
687 TCPDEBUG2(PRU_SHUTDOWN);
688 INP_WUNLOCK(inp);
689 INP_INFO_WUNLOCK(&tcbinfo);
690
691 return (error);
692 }
693
694 /*
695 * After a receive, possibly send window update to peer.
696 */
697 static int
698 tcp_usr_rcvd(struct socket *so, int flags)
699 {
700 struct inpcb *inp;
701 struct tcpcb *tp = NULL;
702 int error = 0;
703
704 TCPDEBUG0;
705 inp = sotoinpcb(so);
706 KASSERT(inp != NULL, ("tcp_usr_rcvd: inp == NULL"));
707 INP_WLOCK(inp);
708 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
709 error = ECONNRESET;
710 goto out;
711 }
712 tp = intotcpcb(inp);
713 TCPDEBUG1();
714 tcp_output_rcvd(tp);
715
716 out:
717 TCPDEBUG2(PRU_RCVD);
718 INP_WUNLOCK(inp);
719 return (error);
720 }
721
722 /*
723 * Do a send by putting data in output queue and updating urgent
724 * marker if URG set. Possibly send more data. Unlike the other
725 * pru_*() routines, the mbuf chains are our responsibility. We
726 * must either enqueue them or free them. The other pru_* routines
727 * generally are caller-frees.
728 */
729 static int
730 tcp_usr_send(struct socket *so, int flags, struct mbuf *m,
731 struct sockaddr *nam, struct mbuf *control, struct thread *td)
732 {
733 int error = 0;
734 struct inpcb *inp;
735 struct tcpcb *tp = NULL;
736 int headlocked = 0;
737 #ifdef INET6
738 int isipv6;
739 #endif
740 TCPDEBUG0;
741
742 /*
743 * We require the pcbinfo lock in two cases:
744 *
745 * (1) An implied connect is taking place, which can result in
746 * binding IPs and ports and hence modification of the pcb hash
747 * chains.
748 *
749 * (2) PRUS_EOF is set, resulting in explicit close on the send.
750 */
751 if ((nam != NULL) || (flags & PRUS_EOF)) {
752 INP_INFO_WLOCK(&tcbinfo);
753 headlocked = 1;
754 }
755 inp = sotoinpcb(so);
756 KASSERT(inp != NULL, ("tcp_usr_send: inp == NULL"));
757 INP_WLOCK(inp);
758 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
759 if (control)
760 m_freem(control);
761 if (m)
762 m_freem(m);
763 error = ECONNRESET;
764 goto out;
765 }
766 #ifdef INET6
767 isipv6 = nam && nam->sa_family == AF_INET6;
768 #endif /* INET6 */
769 tp = intotcpcb(inp);
770 TCPDEBUG1();
771 if (control) {
772 /* TCP doesn't do control messages (rights, creds, etc) */
773 if (control->m_len) {
774 m_freem(control);
775 if (m)
776 m_freem(m);
777 error = EINVAL;
778 goto out;
779 }
780 m_freem(control); /* empty control, just free it */
781 }
782 if (!(flags & PRUS_OOB)) {
783 sbappendstream(&so->so_snd, m);
784 if (nam && tp->t_state < TCPS_SYN_SENT) {
785 /*
786 * Do implied connect if not yet connected,
787 * initialize window to default value, and
788 * initialize maxseg/maxopd using peer's cached
789 * MSS.
790 */
791 INP_INFO_WLOCK_ASSERT(&tcbinfo);
792 #ifdef INET6
793 if (isipv6)
794 error = tcp6_connect(tp, nam, td);
795 else
796 #endif /* INET6 */
797 error = tcp_connect(tp, nam, td);
798 if (error)
799 goto out;
800 tp->snd_wnd = TTCP_CLIENT_SND_WND;
801 tcp_mss(tp, -1);
802 }
803 if (flags & PRUS_EOF) {
804 /*
805 * Close the send side of the connection after
806 * the data is sent.
807 */
808 INP_INFO_WLOCK_ASSERT(&tcbinfo);
809 socantsendmore(so);
810 tcp_usrclosed(tp);
811 }
812 if (headlocked) {
813 INP_INFO_WUNLOCK(&tcbinfo);
814 headlocked = 0;
815 }
816 if (!(inp->inp_flags & INP_DROPPED)) {
817 if (flags & PRUS_MORETOCOME)
818 tp->t_flags |= TF_MORETOCOME;
819 error = tcp_output_send(tp);
820 if (flags & PRUS_MORETOCOME)
821 tp->t_flags &= ~TF_MORETOCOME;
822 }
823 } else {
824 /*
825 * XXXRW: PRUS_EOF not implemented with PRUS_OOB?
826 */
827 SOCKBUF_LOCK(&so->so_snd);
828 if (sbspace(&so->so_snd) < -512) {
829 SOCKBUF_UNLOCK(&so->so_snd);
830 m_freem(m);
831 error = ENOBUFS;
832 goto out;
833 }
834 /*
835 * According to RFC961 (Assigned Protocols),
836 * the urgent pointer points to the last octet
837 * of urgent data. We continue, however,
838 * to consider it to indicate the first octet
839 * of data past the urgent section.
840 * Otherwise, snd_up should be one lower.
841 */
842 sbappendstream_locked(&so->so_snd, m);
843 SOCKBUF_UNLOCK(&so->so_snd);
844 if (nam && tp->t_state < TCPS_SYN_SENT) {
845 /*
846 * Do implied connect if not yet connected,
847 * initialize window to default value, and
848 * initialize maxseg/maxopd using peer's cached
849 * MSS.
850 */
851 INP_INFO_WLOCK_ASSERT(&tcbinfo);
852 #ifdef INET6
853 if (isipv6)
854 error = tcp6_connect(tp, nam, td);
855 else
856 #endif /* INET6 */
857 error = tcp_connect(tp, nam, td);
858 if (error)
859 goto out;
860 tp->snd_wnd = TTCP_CLIENT_SND_WND;
861 tcp_mss(tp, -1);
862 INP_INFO_WUNLOCK(&tcbinfo);
863 headlocked = 0;
864 } else if (nam) {
865 INP_INFO_WUNLOCK(&tcbinfo);
866 headlocked = 0;
867 }
868 tp->snd_up = tp->snd_una + so->so_snd.sb_cc;
869 tp->t_flags |= TF_FORCEDATA;
870 error = tcp_output_send(tp);
871 tp->t_flags &= ~TF_FORCEDATA;
872 }
873 out:
874 TCPDEBUG2((flags & PRUS_OOB) ? PRU_SENDOOB :
875 ((flags & PRUS_EOF) ? PRU_SEND_EOF : PRU_SEND));
876 INP_WUNLOCK(inp);
877 if (headlocked)
878 INP_INFO_WUNLOCK(&tcbinfo);
879 return (error);
880 }
881
882 /*
883 * Abort the TCP. Drop the connection abruptly.
884 */
885 static void
886 tcp_usr_abort(struct socket *so)
887 {
888 struct inpcb *inp;
889 struct tcpcb *tp = NULL;
890 TCPDEBUG0;
891
892 inp = sotoinpcb(so);
893 KASSERT(inp != NULL, ("tcp_usr_abort: inp == NULL"));
894
895 INP_INFO_WLOCK(&tcbinfo);
896 INP_WLOCK(inp);
897 KASSERT(inp->inp_socket != NULL,
898 ("tcp_usr_abort: inp_socket == NULL"));
899
900 /*
901 * If we still have full TCP state, and we're not dropped, drop.
902 */
903 if (!(inp->inp_flags & INP_TIMEWAIT) &&
904 !(inp->inp_flags & INP_DROPPED)) {
905 tp = intotcpcb(inp);
906 TCPDEBUG1();
907 tcp_drop(tp, ECONNABORTED);
908 TCPDEBUG2(PRU_ABORT);
909 }
910 if (!(inp->inp_flags & INP_DROPPED)) {
911 SOCK_LOCK(so);
912 so->so_state |= SS_PROTOREF;
913 SOCK_UNLOCK(so);
914 inp->inp_flags |= INP_SOCKREF;
915 }
916 INP_WUNLOCK(inp);
917 INP_INFO_WUNLOCK(&tcbinfo);
918 }
919
920 /*
921 * TCP socket is closed. Start friendly disconnect.
922 */
923 static void
924 tcp_usr_close(struct socket *so)
925 {
926 struct inpcb *inp;
927 struct tcpcb *tp = NULL;
928 TCPDEBUG0;
929
930 inp = sotoinpcb(so);
931 KASSERT(inp != NULL, ("tcp_usr_close: inp == NULL"));
932
933 INP_INFO_WLOCK(&tcbinfo);
934 INP_WLOCK(inp);
935 KASSERT(inp->inp_socket != NULL,
936 ("tcp_usr_close: inp_socket == NULL"));
937
938 /*
939 * If we still have full TCP state, and we're not dropped, initiate
940 * a disconnect.
941 */
942 if (!(inp->inp_flags & INP_TIMEWAIT) &&
943 !(inp->inp_flags & INP_DROPPED)) {
944 tp = intotcpcb(inp);
945 TCPDEBUG1();
946 tcp_disconnect(tp);
947 TCPDEBUG2(PRU_CLOSE);
948 }
949 if (!(inp->inp_flags & INP_DROPPED)) {
950 SOCK_LOCK(so);
951 so->so_state |= SS_PROTOREF;
952 SOCK_UNLOCK(so);
953 inp->inp_flags |= INP_SOCKREF;
954 }
955 INP_WUNLOCK(inp);
956 INP_INFO_WUNLOCK(&tcbinfo);
957 }
958
959 /*
960 * Receive out-of-band data.
961 */
962 static int
963 tcp_usr_rcvoob(struct socket *so, struct mbuf *m, int flags)
964 {
965 int error = 0;
966 struct inpcb *inp;
967 struct tcpcb *tp = NULL;
968
969 TCPDEBUG0;
970 inp = sotoinpcb(so);
971 KASSERT(inp != NULL, ("tcp_usr_rcvoob: inp == NULL"));
972 INP_WLOCK(inp);
973 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
974 error = ECONNRESET;
975 goto out;
976 }
977 tp = intotcpcb(inp);
978 TCPDEBUG1();
979 if ((so->so_oobmark == 0 &&
980 (so->so_rcv.sb_state & SBS_RCVATMARK) == 0) ||
981 so->so_options & SO_OOBINLINE ||
982 tp->t_oobflags & TCPOOB_HADDATA) {
983 error = EINVAL;
984 goto out;
985 }
986 if ((tp->t_oobflags & TCPOOB_HAVEDATA) == 0) {
987 error = EWOULDBLOCK;
988 goto out;
989 }
990 m->m_len = 1;
991 *mtod(m, caddr_t) = tp->t_iobc;
992 if ((flags & MSG_PEEK) == 0)
993 tp->t_oobflags ^= (TCPOOB_HAVEDATA | TCPOOB_HADDATA);
994
995 out:
996 TCPDEBUG2(PRU_RCVOOB);
997 INP_WUNLOCK(inp);
998 return (error);
999 }
1000
1001 struct pr_usrreqs tcp_usrreqs = {
1002 .pru_abort = tcp_usr_abort,
1003 .pru_accept = tcp_usr_accept,
1004 .pru_attach = tcp_usr_attach,
1005 .pru_bind = tcp_usr_bind,
1006 .pru_connect = tcp_usr_connect,
1007 .pru_control = in_control,
1008 .pru_detach = tcp_usr_detach,
1009 .pru_disconnect = tcp_usr_disconnect,
1010 .pru_listen = tcp_usr_listen,
1011 .pru_peeraddr = in_getpeeraddr,
1012 .pru_rcvd = tcp_usr_rcvd,
1013 .pru_rcvoob = tcp_usr_rcvoob,
1014 .pru_send = tcp_usr_send,
1015 .pru_shutdown = tcp_usr_shutdown,
1016 .pru_sockaddr = in_getsockaddr,
1017 .pru_sosetlabel = in_pcbsosetlabel,
1018 .pru_close = tcp_usr_close,
1019 };
1020
1021 #ifdef INET6
1022 struct pr_usrreqs tcp6_usrreqs = {
1023 .pru_abort = tcp_usr_abort,
1024 .pru_accept = tcp6_usr_accept,
1025 .pru_attach = tcp_usr_attach,
1026 .pru_bind = tcp6_usr_bind,
1027 .pru_connect = tcp6_usr_connect,
1028 .pru_control = in6_control,
1029 .pru_detach = tcp_usr_detach,
1030 .pru_disconnect = tcp_usr_disconnect,
1031 .pru_listen = tcp6_usr_listen,
1032 .pru_peeraddr = in6_mapped_peeraddr,
1033 .pru_rcvd = tcp_usr_rcvd,
1034 .pru_rcvoob = tcp_usr_rcvoob,
1035 .pru_send = tcp_usr_send,
1036 .pru_shutdown = tcp_usr_shutdown,
1037 .pru_sockaddr = in6_mapped_sockaddr,
1038 .pru_sosetlabel = in_pcbsosetlabel,
1039 .pru_close = tcp_usr_close,
1040 };
1041 #endif /* INET6 */
1042
1043 /*
1044 * Common subroutine to open a TCP connection to remote host specified
1045 * by struct sockaddr_in in mbuf *nam. Call in_pcbbind to assign a local
1046 * port number if needed. Call in_pcbconnect_setup to do the routing and
1047 * to choose a local host address (interface). If there is an existing
1048 * incarnation of the same connection in TIME-WAIT state and if the remote
1049 * host was sending CC options and if the connection duration was < MSL, then
1050 * truncate the previous TIME-WAIT state and proceed.
1051 * Initialize connection parameters and enter SYN-SENT state.
1052 */
1053 static int
1054 tcp_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1055 {
1056 struct inpcb *inp = tp->t_inpcb, *oinp;
1057 struct socket *so = inp->inp_socket;
1058 struct in_addr laddr;
1059 u_short lport;
1060 int error;
1061
1062 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1063 INP_WLOCK_ASSERT(inp);
1064
1065 if (inp->inp_lport == 0) {
1066 error = in_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1067 if (error)
1068 return error;
1069 }
1070
1071 /*
1072 * Cannot simply call in_pcbconnect, because there might be an
1073 * earlier incarnation of this same connection still in
1074 * TIME_WAIT state, creating an ADDRINUSE error.
1075 */
1076 laddr = inp->inp_laddr;
1077 lport = inp->inp_lport;
1078 error = in_pcbconnect_setup(inp, nam, &laddr.s_addr, &lport,
1079 &inp->inp_faddr.s_addr, &inp->inp_fport, &oinp, td->td_ucred);
1080 if (error && oinp == NULL)
1081 return error;
1082 if (oinp)
1083 return EADDRINUSE;
1084 inp->inp_laddr = laddr;
1085 in_pcbrehash(inp);
1086
1087 /*
1088 * Compute window scaling to request:
1089 * Scale to fit into sweet spot. See tcp_syncache.c.
1090 * XXX: This should move to tcp_output().
1091 */
1092 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1093 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1094 tp->request_r_scale++;
1095
1096 soisconnecting(so);
1097 tcpstat.tcps_connattempt++;
1098 tp->t_state = TCPS_SYN_SENT;
1099 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1100 tp->iss = tcp_new_isn(tp);
1101 tp->t_bw_rtseq = tp->iss;
1102 tcp_sendseqinit(tp);
1103
1104 return 0;
1105 }
1106
1107 #ifdef INET6
1108 static int
1109 tcp6_connect(struct tcpcb *tp, struct sockaddr *nam, struct thread *td)
1110 {
1111 struct inpcb *inp = tp->t_inpcb, *oinp;
1112 struct socket *so = inp->inp_socket;
1113 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)nam;
1114 struct in6_addr addr6;
1115 int error;
1116
1117 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1118 INP_WLOCK_ASSERT(inp);
1119
1120 if (inp->inp_lport == 0) {
1121 error = in6_pcbbind(inp, (struct sockaddr *)0, td->td_ucred);
1122 if (error)
1123 return error;
1124 }
1125
1126 /*
1127 * Cannot simply call in_pcbconnect, because there might be an
1128 * earlier incarnation of this same connection still in
1129 * TIME_WAIT state, creating an ADDRINUSE error.
1130 * in6_pcbladdr() also handles scope zone IDs.
1131 */
1132 error = in6_pcbladdr(inp, nam, &addr6);
1133 if (error)
1134 return error;
1135 oinp = in6_pcblookup_hash(inp->inp_pcbinfo,
1136 &sin6->sin6_addr, sin6->sin6_port,
1137 IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)
1138 ? &addr6
1139 : &inp->in6p_laddr,
1140 inp->inp_lport, 0, NULL);
1141 if (oinp)
1142 return EADDRINUSE;
1143 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
1144 inp->in6p_laddr = addr6;
1145 inp->in6p_faddr = sin6->sin6_addr;
1146 inp->inp_fport = sin6->sin6_port;
1147 /* update flowinfo - draft-itojun-ipv6-flowlabel-api-00 */
1148 inp->inp_flow &= ~IPV6_FLOWLABEL_MASK;
1149 if (inp->inp_flags & IN6P_AUTOFLOWLABEL)
1150 inp->inp_flow |=
1151 (htonl(ip6_randomflowlabel()) & IPV6_FLOWLABEL_MASK);
1152 in_pcbrehash(inp);
1153
1154 /* Compute window scaling to request. */
1155 while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
1156 (TCP_MAXWIN << tp->request_r_scale) < sb_max)
1157 tp->request_r_scale++;
1158
1159 soisconnecting(so);
1160 tcpstat.tcps_connattempt++;
1161 tp->t_state = TCPS_SYN_SENT;
1162 tcp_timer_activate(tp, TT_KEEP, tcp_keepinit);
1163 tp->iss = tcp_new_isn(tp);
1164 tp->t_bw_rtseq = tp->iss;
1165 tcp_sendseqinit(tp);
1166
1167 return 0;
1168 }
1169 #endif /* INET6 */
1170
1171 /*
1172 * Export TCP internal state information via a struct tcp_info, based on the
1173 * Linux 2.6 API. Not ABI compatible as our constants are mapped differently
1174 * (TCP state machine, etc). We export all information using FreeBSD-native
1175 * constants -- for example, the numeric values for tcpi_state will differ
1176 * from Linux.
1177 */
1178 static void
1179 tcp_fill_info(struct tcpcb *tp, struct tcp_info *ti)
1180 {
1181
1182 INP_WLOCK_ASSERT(tp->t_inpcb);
1183 bzero(ti, sizeof(*ti));
1184
1185 ti->tcpi_state = tp->t_state;
1186 if ((tp->t_flags & TF_REQ_TSTMP) && (tp->t_flags & TF_RCVD_TSTMP))
1187 ti->tcpi_options |= TCPI_OPT_TIMESTAMPS;
1188 if (tp->t_flags & TF_SACK_PERMIT)
1189 ti->tcpi_options |= TCPI_OPT_SACK;
1190 if ((tp->t_flags & TF_REQ_SCALE) && (tp->t_flags & TF_RCVD_SCALE)) {
1191 ti->tcpi_options |= TCPI_OPT_WSCALE;
1192 ti->tcpi_snd_wscale = tp->snd_scale;
1193 ti->tcpi_rcv_wscale = tp->rcv_scale;
1194 }
1195
1196 ti->tcpi_rto = tp->t_rxtcur * tick;
1197 ti->tcpi_last_data_recv = (long)(ticks - (int)tp->t_rcvtime) * tick;
1198 ti->tcpi_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1199 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1200
1201 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1202 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1203
1204 /*
1205 * FreeBSD-specific extension fields for tcp_info.
1206 */
1207 ti->tcpi_rcv_space = tp->rcv_wnd;
1208 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1209 ti->tcpi_snd_wnd = tp->snd_wnd;
1210 ti->tcpi_snd_bwnd = tp->snd_bwnd;
1211 ti->tcpi_snd_nxt = tp->snd_nxt;
1212 ti->tcpi_snd_mss = tp->t_maxseg;
1213 ti->tcpi_rcv_mss = tp->t_maxseg;
1214 if (tp->t_flags & TF_TOE)
1215 ti->tcpi_options |= TCPI_OPT_TOE;
1216 }
1217
1218 /*
1219 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1220 * socket option arguments. When it re-acquires the lock after the copy, it
1221 * has to revalidate that the connection is still valid for the socket
1222 * option.
1223 */
1224 #define INP_WLOCK_RECHECK(inp) do { \
1225 INP_WLOCK(inp); \
1226 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1227 INP_WUNLOCK(inp); \
1228 return (ECONNRESET); \
1229 } \
1230 tp = intotcpcb(inp); \
1231 } while(0)
1232
1233 int
1234 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1235 {
1236 int error, opt, optval;
1237 struct inpcb *inp;
1238 struct tcpcb *tp;
1239 struct tcp_info ti;
1240
1241 error = 0;
1242 inp = sotoinpcb(so);
1243 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1244 INP_WLOCK(inp);
1245 if (sopt->sopt_level != IPPROTO_TCP) {
1246 #ifdef INET6
1247 if (inp->inp_vflag & INP_IPV6PROTO) {
1248 INP_WUNLOCK(inp);
1249 error = ip6_ctloutput(so, sopt);
1250 } else {
1251 #endif /* INET6 */
1252 INP_WUNLOCK(inp);
1253 error = ip_ctloutput(so, sopt);
1254 #ifdef INET6
1255 }
1256 #endif
1257 return (error);
1258 }
1259 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1260 INP_WUNLOCK(inp);
1261 return (ECONNRESET);
1262 }
1263
1264 switch (sopt->sopt_dir) {
1265 case SOPT_SET:
1266 switch (sopt->sopt_name) {
1267 #ifdef TCP_SIGNATURE
1268 case TCP_MD5SIG:
1269 INP_WUNLOCK(inp);
1270 error = sooptcopyin(sopt, &optval, sizeof optval,
1271 sizeof optval);
1272 if (error)
1273 return (error);
1274
1275 INP_WLOCK_RECHECK(inp);
1276 if (optval > 0)
1277 tp->t_flags |= TF_SIGNATURE;
1278 else
1279 tp->t_flags &= ~TF_SIGNATURE;
1280 INP_WUNLOCK(inp);
1281 break;
1282 #endif /* TCP_SIGNATURE */
1283 case TCP_NODELAY:
1284 case TCP_NOOPT:
1285 INP_WUNLOCK(inp);
1286 error = sooptcopyin(sopt, &optval, sizeof optval,
1287 sizeof optval);
1288 if (error)
1289 return (error);
1290
1291 INP_WLOCK_RECHECK(inp);
1292 switch (sopt->sopt_name) {
1293 case TCP_NODELAY:
1294 opt = TF_NODELAY;
1295 break;
1296 case TCP_NOOPT:
1297 opt = TF_NOOPT;
1298 break;
1299 default:
1300 opt = 0; /* dead code to fool gcc */
1301 break;
1302 }
1303
1304 if (optval)
1305 tp->t_flags |= opt;
1306 else
1307 tp->t_flags &= ~opt;
1308 INP_WUNLOCK(inp);
1309 break;
1310
1311 case TCP_NOPUSH:
1312 INP_WUNLOCK(inp);
1313 error = sooptcopyin(sopt, &optval, sizeof optval,
1314 sizeof optval);
1315 if (error)
1316 return (error);
1317
1318 INP_WLOCK_RECHECK(inp);
1319 if (optval)
1320 tp->t_flags |= TF_NOPUSH;
1321 else {
1322 tp->t_flags &= ~TF_NOPUSH;
1323 error = tcp_output(tp);
1324 }
1325 INP_WUNLOCK(inp);
1326 break;
1327
1328 case TCP_MAXSEG:
1329 INP_WUNLOCK(inp);
1330 error = sooptcopyin(sopt, &optval, sizeof optval,
1331 sizeof optval);
1332 if (error)
1333 return (error);
1334
1335 INP_WLOCK_RECHECK(inp);
1336 if (optval > 0 && optval <= tp->t_maxseg &&
1337 optval + 40 >= tcp_minmss)
1338 tp->t_maxseg = optval;
1339 else
1340 error = EINVAL;
1341 INP_WUNLOCK(inp);
1342 break;
1343
1344 case TCP_INFO:
1345 INP_WUNLOCK(inp);
1346 error = EINVAL;
1347 break;
1348
1349 default:
1350 INP_WUNLOCK(inp);
1351 error = ENOPROTOOPT;
1352 break;
1353 }
1354 break;
1355
1356 case SOPT_GET:
1357 tp = intotcpcb(inp);
1358 switch (sopt->sopt_name) {
1359 #ifdef TCP_SIGNATURE
1360 case TCP_MD5SIG:
1361 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1362 INP_WUNLOCK(inp);
1363 error = sooptcopyout(sopt, &optval, sizeof optval);
1364 break;
1365 #endif
1366
1367 case TCP_NODELAY:
1368 optval = tp->t_flags & TF_NODELAY;
1369 INP_WUNLOCK(inp);
1370 error = sooptcopyout(sopt, &optval, sizeof optval);
1371 break;
1372 case TCP_MAXSEG:
1373 optval = tp->t_maxseg;
1374 INP_WUNLOCK(inp);
1375 error = sooptcopyout(sopt, &optval, sizeof optval);
1376 break;
1377 case TCP_NOOPT:
1378 optval = tp->t_flags & TF_NOOPT;
1379 INP_WUNLOCK(inp);
1380 error = sooptcopyout(sopt, &optval, sizeof optval);
1381 break;
1382 case TCP_NOPUSH:
1383 optval = tp->t_flags & TF_NOPUSH;
1384 INP_WUNLOCK(inp);
1385 error = sooptcopyout(sopt, &optval, sizeof optval);
1386 break;
1387 case TCP_INFO:
1388 tcp_fill_info(tp, &ti);
1389 INP_WUNLOCK(inp);
1390 error = sooptcopyout(sopt, &ti, sizeof ti);
1391 break;
1392 default:
1393 INP_WUNLOCK(inp);
1394 error = ENOPROTOOPT;
1395 break;
1396 }
1397 break;
1398 }
1399 return (error);
1400 }
1401 #undef INP_WLOCK_RECHECK
1402
1403 /*
1404 * tcp_sendspace and tcp_recvspace are the default send and receive window
1405 * sizes, respectively. These are obsolescent (this information should
1406 * be set by the route).
1407 */
1408 u_long tcp_sendspace = 1024*32;
1409 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1410 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1411 u_long tcp_recvspace = 1024*64;
1412 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1413 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1414
1415 /*
1416 * Attach TCP protocol to socket, allocating
1417 * internet protocol control block, tcp control block,
1418 * bufer space, and entering LISTEN state if to accept connections.
1419 */
1420 static int
1421 tcp_attach(struct socket *so)
1422 {
1423 struct tcpcb *tp;
1424 struct inpcb *inp;
1425 int error;
1426
1427 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1428 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1429 if (error)
1430 return (error);
1431 }
1432 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1433 so->so_snd.sb_flags |= SB_AUTOSIZE;
1434 INP_INFO_WLOCK(&tcbinfo);
1435 error = in_pcballoc(so, &tcbinfo);
1436 if (error) {
1437 INP_INFO_WUNLOCK(&tcbinfo);
1438 return (error);
1439 }
1440 inp = sotoinpcb(so);
1441 #ifdef INET6
1442 if (inp->inp_vflag & INP_IPV6PROTO) {
1443 inp->inp_vflag |= INP_IPV6;
1444 inp->in6p_hops = -1; /* use kernel default */
1445 }
1446 else
1447 #endif
1448 inp->inp_vflag |= INP_IPV4;
1449 tp = tcp_newtcpcb(inp);
1450 if (tp == NULL) {
1451 in_pcbdetach(inp);
1452 in_pcbfree(inp);
1453 INP_INFO_WUNLOCK(&tcbinfo);
1454 return (ENOBUFS);
1455 }
1456 tp->t_state = TCPS_CLOSED;
1457 INP_WUNLOCK(inp);
1458 INP_INFO_WUNLOCK(&tcbinfo);
1459 return (0);
1460 }
1461
1462 /*
1463 * Initiate (or continue) disconnect.
1464 * If embryonic state, just send reset (once).
1465 * If in ``let data drain'' option and linger null, just drop.
1466 * Otherwise (hard), mark socket disconnecting and drop
1467 * current input data; switch states based on user close, and
1468 * send segment to peer (with FIN).
1469 */
1470 static void
1471 tcp_disconnect(struct tcpcb *tp)
1472 {
1473 struct inpcb *inp = tp->t_inpcb;
1474 struct socket *so = inp->inp_socket;
1475
1476 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1477 INP_WLOCK_ASSERT(inp);
1478
1479 /*
1480 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1481 * socket is still open.
1482 */
1483 if (tp->t_state < TCPS_ESTABLISHED) {
1484 tp = tcp_close(tp);
1485 KASSERT(tp != NULL,
1486 ("tcp_disconnect: tcp_close() returned NULL"));
1487 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1488 tp = tcp_drop(tp, 0);
1489 KASSERT(tp != NULL,
1490 ("tcp_disconnect: tcp_drop() returned NULL"));
1491 } else {
1492 soisdisconnecting(so);
1493 sbflush(&so->so_rcv);
1494 tcp_usrclosed(tp);
1495 if (!(inp->inp_flags & INP_DROPPED))
1496 tcp_output_disconnect(tp);
1497 }
1498 }
1499
1500 /*
1501 * User issued close, and wish to trail through shutdown states:
1502 * if never received SYN, just forget it. If got a SYN from peer,
1503 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1504 * If already got a FIN from peer, then almost done; go to LAST_ACK
1505 * state. In all other cases, have already sent FIN to peer (e.g.
1506 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1507 * for peer to send FIN or not respond to keep-alives, etc.
1508 * We can let the user exit from the close as soon as the FIN is acked.
1509 */
1510 static void
1511 tcp_usrclosed(struct tcpcb *tp)
1512 {
1513
1514 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1515 INP_WLOCK_ASSERT(tp->t_inpcb);
1516
1517 switch (tp->t_state) {
1518 case TCPS_LISTEN:
1519 tcp_offload_listen_close(tp);
1520 /* FALLTHROUGH */
1521 case TCPS_CLOSED:
1522 tp->t_state = TCPS_CLOSED;
1523 tp = tcp_close(tp);
1524 /*
1525 * tcp_close() should never return NULL here as the socket is
1526 * still open.
1527 */
1528 KASSERT(tp != NULL,
1529 ("tcp_usrclosed: tcp_close() returned NULL"));
1530 break;
1531
1532 case TCPS_SYN_SENT:
1533 case TCPS_SYN_RECEIVED:
1534 tp->t_flags |= TF_NEEDFIN;
1535 break;
1536
1537 case TCPS_ESTABLISHED:
1538 tp->t_state = TCPS_FIN_WAIT_1;
1539 break;
1540
1541 case TCPS_CLOSE_WAIT:
1542 tp->t_state = TCPS_LAST_ACK;
1543 break;
1544 }
1545 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1546 soisdisconnected(tp->t_inpcb->inp_socket);
1547 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1548 if (tp->t_state == TCPS_FIN_WAIT_2) {
1549 int timeout;
1550
1551 timeout = (tcp_fast_finwait2_recycle) ?
1552 tcp_finwait2_timeout : tcp_maxidle;
1553 tcp_timer_activate(tp, TT_2MSL, timeout);
1554 }
1555 }
1556 }
1557
1558 #ifdef DDB
1559 static void
1560 db_print_indent(int indent)
1561 {
1562 int i;
1563
1564 for (i = 0; i < indent; i++)
1565 db_printf(" ");
1566 }
1567
1568 static void
1569 db_print_tstate(int t_state)
1570 {
1571
1572 switch (t_state) {
1573 case TCPS_CLOSED:
1574 db_printf("TCPS_CLOSED");
1575 return;
1576
1577 case TCPS_LISTEN:
1578 db_printf("TCPS_LISTEN");
1579 return;
1580
1581 case TCPS_SYN_SENT:
1582 db_printf("TCPS_SYN_SENT");
1583 return;
1584
1585 case TCPS_SYN_RECEIVED:
1586 db_printf("TCPS_SYN_RECEIVED");
1587 return;
1588
1589 case TCPS_ESTABLISHED:
1590 db_printf("TCPS_ESTABLISHED");
1591 return;
1592
1593 case TCPS_CLOSE_WAIT:
1594 db_printf("TCPS_CLOSE_WAIT");
1595 return;
1596
1597 case TCPS_FIN_WAIT_1:
1598 db_printf("TCPS_FIN_WAIT_1");
1599 return;
1600
1601 case TCPS_CLOSING:
1602 db_printf("TCPS_CLOSING");
1603 return;
1604
1605 case TCPS_LAST_ACK:
1606 db_printf("TCPS_LAST_ACK");
1607 return;
1608
1609 case TCPS_FIN_WAIT_2:
1610 db_printf("TCPS_FIN_WAIT_2");
1611 return;
1612
1613 case TCPS_TIME_WAIT:
1614 db_printf("TCPS_TIME_WAIT");
1615 return;
1616
1617 default:
1618 db_printf("unknown");
1619 return;
1620 }
1621 }
1622
1623 static void
1624 db_print_tflags(u_int t_flags)
1625 {
1626 int comma;
1627
1628 comma = 0;
1629 if (t_flags & TF_ACKNOW) {
1630 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1631 comma = 1;
1632 }
1633 if (t_flags & TF_DELACK) {
1634 db_printf("%sTF_DELACK", comma ? ", " : "");
1635 comma = 1;
1636 }
1637 if (t_flags & TF_NODELAY) {
1638 db_printf("%sTF_NODELAY", comma ? ", " : "");
1639 comma = 1;
1640 }
1641 if (t_flags & TF_NOOPT) {
1642 db_printf("%sTF_NOOPT", comma ? ", " : "");
1643 comma = 1;
1644 }
1645 if (t_flags & TF_SENTFIN) {
1646 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1647 comma = 1;
1648 }
1649 if (t_flags & TF_REQ_SCALE) {
1650 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1651 comma = 1;
1652 }
1653 if (t_flags & TF_RCVD_SCALE) {
1654 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1655 comma = 1;
1656 }
1657 if (t_flags & TF_REQ_TSTMP) {
1658 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1659 comma = 1;
1660 }
1661 if (t_flags & TF_RCVD_TSTMP) {
1662 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1663 comma = 1;
1664 }
1665 if (t_flags & TF_SACK_PERMIT) {
1666 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1667 comma = 1;
1668 }
1669 if (t_flags & TF_NEEDSYN) {
1670 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1671 comma = 1;
1672 }
1673 if (t_flags & TF_NEEDFIN) {
1674 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1675 comma = 1;
1676 }
1677 if (t_flags & TF_NOPUSH) {
1678 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1679 comma = 1;
1680 }
1681 if (t_flags & TF_NOPUSH) {
1682 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1683 comma = 1;
1684 }
1685 if (t_flags & TF_MORETOCOME) {
1686 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1687 comma = 1;
1688 }
1689 if (t_flags & TF_LQ_OVERFLOW) {
1690 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1691 comma = 1;
1692 }
1693 if (t_flags & TF_LASTIDLE) {
1694 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1695 comma = 1;
1696 }
1697 if (t_flags & TF_RXWIN0SENT) {
1698 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1699 comma = 1;
1700 }
1701 if (t_flags & TF_FASTRECOVERY) {
1702 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1703 comma = 1;
1704 }
1705 if (t_flags & TF_WASFRECOVERY) {
1706 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1707 comma = 1;
1708 }
1709 if (t_flags & TF_SIGNATURE) {
1710 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1711 comma = 1;
1712 }
1713 if (t_flags & TF_FORCEDATA) {
1714 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1715 comma = 1;
1716 }
1717 if (t_flags & TF_TSO) {
1718 db_printf("%sTF_TSO", comma ? ", " : "");
1719 comma = 1;
1720 }
1721 }
1722
1723 static void
1724 db_print_toobflags(char t_oobflags)
1725 {
1726 int comma;
1727
1728 comma = 0;
1729 if (t_oobflags & TCPOOB_HAVEDATA) {
1730 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1731 comma = 1;
1732 }
1733 if (t_oobflags & TCPOOB_HADDATA) {
1734 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1735 comma = 1;
1736 }
1737 }
1738
1739 static void
1740 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1741 {
1742
1743 db_print_indent(indent);
1744 db_printf("%s at %p\n", name, tp);
1745
1746 indent += 2;
1747
1748 db_print_indent(indent);
1749 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1750 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1751
1752 db_print_indent(indent);
1753 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1754 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1755
1756 db_print_indent(indent);
1757 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1758 &tp->t_timers->tt_delack, tp->t_inpcb);
1759
1760 db_print_indent(indent);
1761 db_printf("t_state: %d (", tp->t_state);
1762 db_print_tstate(tp->t_state);
1763 db_printf(")\n");
1764
1765 db_print_indent(indent);
1766 db_printf("t_flags: 0x%x (", tp->t_flags);
1767 db_print_tflags(tp->t_flags);
1768 db_printf(")\n");
1769
1770 db_print_indent(indent);
1771 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
1772 tp->snd_una, tp->snd_max, tp->snd_nxt);
1773
1774 db_print_indent(indent);
1775 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
1776 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1777
1778 db_print_indent(indent);
1779 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
1780 tp->iss, tp->irs, tp->rcv_nxt);
1781
1782 db_print_indent(indent);
1783 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
1784 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1785
1786 db_print_indent(indent);
1787 db_printf("snd_wnd: %lu snd_cwnd: %lu snd_bwnd: %lu\n",
1788 tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1789
1790 db_print_indent(indent);
1791 db_printf("snd_ssthresh: %lu snd_bandwidth: %lu snd_recover: "
1792 "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1793 tp->snd_recover);
1794
1795 db_print_indent(indent);
1796 db_printf("t_maxopd: %u t_rcvtime: %lu t_startime: %lu\n",
1797 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1798
1799 db_print_indent(indent);
1800 db_printf("t_rttime: %d t_rtsq: 0x%08x t_bw_rtttime: %d\n",
1801 tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1802
1803 db_print_indent(indent);
1804 db_printf("t_bw_rtseq: 0x%08x t_rxtcur: %d t_maxseg: %u "
1805 "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1806 tp->t_srtt);
1807
1808 db_print_indent(indent);
1809 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
1810 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1811 tp->t_rttbest);
1812
1813 db_print_indent(indent);
1814 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
1815 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1816
1817 db_print_indent(indent);
1818 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1819 db_print_toobflags(tp->t_oobflags);
1820 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
1821
1822 db_print_indent(indent);
1823 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
1824 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1825
1826 db_print_indent(indent);
1827 db_printf("ts_recent: %u ts_recent_age: %lu\n",
1828 tp->ts_recent, tp->ts_recent_age);
1829
1830 db_print_indent(indent);
1831 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
1832 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1833
1834 db_print_indent(indent);
1835 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
1836 "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1837 tp->snd_recover_prev, tp->t_badrxtwin);
1838
1839 db_print_indent(indent);
1840 db_printf("snd_numholes: %d snd_holes first: %p\n",
1841 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1842
1843 db_print_indent(indent);
1844 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
1845 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1846
1847 /* Skip sackblks, sackhint. */
1848
1849 db_print_indent(indent);
1850 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
1851 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1852 }
1853
1854 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1855 {
1856 struct tcpcb *tp;
1857
1858 if (!have_addr) {
1859 db_printf("usage: show tcpcb <addr>\n");
1860 return;
1861 }
1862 tp = (struct tcpcb *)addr;
1863
1864 db_print_tcpcb(tp, "tcpcb", 0);
1865 }
1866 #endif
Cache object: d05fc5d7d59bedb39c3230584e0363a8
|