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$");
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_rtt = ((u_int64_t)tp->t_srtt * tick) >> TCP_RTT_SHIFT;
1197 ti->tcpi_rttvar = ((u_int64_t)tp->t_rttvar * tick) >> TCP_RTTVAR_SHIFT;
1198
1199 ti->tcpi_snd_ssthresh = tp->snd_ssthresh;
1200 ti->tcpi_snd_cwnd = tp->snd_cwnd;
1201
1202 /*
1203 * FreeBSD-specific extension fields for tcp_info.
1204 */
1205 ti->tcpi_rcv_space = tp->rcv_wnd;
1206 ti->tcpi_rcv_nxt = tp->rcv_nxt;
1207 ti->tcpi_snd_wnd = tp->snd_wnd;
1208 ti->tcpi_snd_bwnd = tp->snd_bwnd;
1209 ti->tcpi_snd_nxt = tp->snd_nxt;
1210 ti->__tcpi_snd_mss = tp->t_maxseg;
1211 ti->__tcpi_rcv_mss = tp->t_maxseg;
1212 if (tp->t_flags & TF_TOE)
1213 ti->tcpi_options |= TCPI_OPT_TOE;
1214 }
1215
1216 /*
1217 * tcp_ctloutput() must drop the inpcb lock before performing copyin on
1218 * socket option arguments. When it re-acquires the lock after the copy, it
1219 * has to revalidate that the connection is still valid for the socket
1220 * option.
1221 */
1222 #define INP_WLOCK_RECHECK(inp) do { \
1223 INP_WLOCK(inp); \
1224 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) { \
1225 INP_WUNLOCK(inp); \
1226 return (ECONNRESET); \
1227 } \
1228 tp = intotcpcb(inp); \
1229 } while(0)
1230
1231 int
1232 tcp_ctloutput(struct socket *so, struct sockopt *sopt)
1233 {
1234 int error, opt, optval;
1235 struct inpcb *inp;
1236 struct tcpcb *tp;
1237 struct tcp_info ti;
1238
1239 error = 0;
1240 inp = sotoinpcb(so);
1241 KASSERT(inp != NULL, ("tcp_ctloutput: inp == NULL"));
1242 INP_WLOCK(inp);
1243 if (sopt->sopt_level != IPPROTO_TCP) {
1244 #ifdef INET6
1245 if (inp->inp_vflag & INP_IPV6PROTO) {
1246 INP_WUNLOCK(inp);
1247 error = ip6_ctloutput(so, sopt);
1248 } else {
1249 #endif /* INET6 */
1250 INP_WUNLOCK(inp);
1251 error = ip_ctloutput(so, sopt);
1252 #ifdef INET6
1253 }
1254 #endif
1255 return (error);
1256 }
1257 if (inp->inp_flags & (INP_TIMEWAIT | INP_DROPPED)) {
1258 INP_WUNLOCK(inp);
1259 return (ECONNRESET);
1260 }
1261
1262 switch (sopt->sopt_dir) {
1263 case SOPT_SET:
1264 switch (sopt->sopt_name) {
1265 #ifdef TCP_SIGNATURE
1266 case TCP_MD5SIG:
1267 INP_WUNLOCK(inp);
1268 error = sooptcopyin(sopt, &optval, sizeof optval,
1269 sizeof optval);
1270 if (error)
1271 return (error);
1272
1273 INP_WLOCK_RECHECK(inp);
1274 if (optval > 0)
1275 tp->t_flags |= TF_SIGNATURE;
1276 else
1277 tp->t_flags &= ~TF_SIGNATURE;
1278 INP_WUNLOCK(inp);
1279 break;
1280 #endif /* TCP_SIGNATURE */
1281 case TCP_NODELAY:
1282 case TCP_NOOPT:
1283 INP_WUNLOCK(inp);
1284 error = sooptcopyin(sopt, &optval, sizeof optval,
1285 sizeof optval);
1286 if (error)
1287 return (error);
1288
1289 INP_WLOCK_RECHECK(inp);
1290 switch (sopt->sopt_name) {
1291 case TCP_NODELAY:
1292 opt = TF_NODELAY;
1293 break;
1294 case TCP_NOOPT:
1295 opt = TF_NOOPT;
1296 break;
1297 default:
1298 opt = 0; /* dead code to fool gcc */
1299 break;
1300 }
1301
1302 if (optval)
1303 tp->t_flags |= opt;
1304 else
1305 tp->t_flags &= ~opt;
1306 INP_WUNLOCK(inp);
1307 break;
1308
1309 case TCP_NOPUSH:
1310 INP_WUNLOCK(inp);
1311 error = sooptcopyin(sopt, &optval, sizeof optval,
1312 sizeof optval);
1313 if (error)
1314 return (error);
1315
1316 INP_WLOCK_RECHECK(inp);
1317 if (optval)
1318 tp->t_flags |= TF_NOPUSH;
1319 else {
1320 tp->t_flags &= ~TF_NOPUSH;
1321 error = tcp_output(tp);
1322 }
1323 INP_WUNLOCK(inp);
1324 break;
1325
1326 case TCP_MAXSEG:
1327 INP_WUNLOCK(inp);
1328 error = sooptcopyin(sopt, &optval, sizeof optval,
1329 sizeof optval);
1330 if (error)
1331 return (error);
1332
1333 INP_WLOCK_RECHECK(inp);
1334 if (optval > 0 && optval <= tp->t_maxseg &&
1335 optval + 40 >= tcp_minmss)
1336 tp->t_maxseg = optval;
1337 else
1338 error = EINVAL;
1339 INP_WUNLOCK(inp);
1340 break;
1341
1342 case TCP_INFO:
1343 INP_WUNLOCK(inp);
1344 error = EINVAL;
1345 break;
1346
1347 default:
1348 INP_WUNLOCK(inp);
1349 error = ENOPROTOOPT;
1350 break;
1351 }
1352 break;
1353
1354 case SOPT_GET:
1355 tp = intotcpcb(inp);
1356 switch (sopt->sopt_name) {
1357 #ifdef TCP_SIGNATURE
1358 case TCP_MD5SIG:
1359 optval = (tp->t_flags & TF_SIGNATURE) ? 1 : 0;
1360 INP_WUNLOCK(inp);
1361 error = sooptcopyout(sopt, &optval, sizeof optval);
1362 break;
1363 #endif
1364
1365 case TCP_NODELAY:
1366 optval = tp->t_flags & TF_NODELAY;
1367 INP_WUNLOCK(inp);
1368 error = sooptcopyout(sopt, &optval, sizeof optval);
1369 break;
1370 case TCP_MAXSEG:
1371 optval = tp->t_maxseg;
1372 INP_WUNLOCK(inp);
1373 error = sooptcopyout(sopt, &optval, sizeof optval);
1374 break;
1375 case TCP_NOOPT:
1376 optval = tp->t_flags & TF_NOOPT;
1377 INP_WUNLOCK(inp);
1378 error = sooptcopyout(sopt, &optval, sizeof optval);
1379 break;
1380 case TCP_NOPUSH:
1381 optval = tp->t_flags & TF_NOPUSH;
1382 INP_WUNLOCK(inp);
1383 error = sooptcopyout(sopt, &optval, sizeof optval);
1384 break;
1385 case TCP_INFO:
1386 tcp_fill_info(tp, &ti);
1387 INP_WUNLOCK(inp);
1388 error = sooptcopyout(sopt, &ti, sizeof ti);
1389 break;
1390 default:
1391 INP_WUNLOCK(inp);
1392 error = ENOPROTOOPT;
1393 break;
1394 }
1395 break;
1396 }
1397 return (error);
1398 }
1399 #undef INP_WLOCK_RECHECK
1400
1401 /*
1402 * tcp_sendspace and tcp_recvspace are the default send and receive window
1403 * sizes, respectively. These are obsolescent (this information should
1404 * be set by the route).
1405 */
1406 u_long tcp_sendspace = 1024*32;
1407 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_SENDSPACE, sendspace, CTLFLAG_RW,
1408 &tcp_sendspace , 0, "Maximum outgoing TCP datagram size");
1409 u_long tcp_recvspace = 1024*64;
1410 SYSCTL_ULONG(_net_inet_tcp, TCPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1411 &tcp_recvspace , 0, "Maximum incoming TCP datagram size");
1412
1413 /*
1414 * Attach TCP protocol to socket, allocating
1415 * internet protocol control block, tcp control block,
1416 * bufer space, and entering LISTEN state if to accept connections.
1417 */
1418 static int
1419 tcp_attach(struct socket *so)
1420 {
1421 struct tcpcb *tp;
1422 struct inpcb *inp;
1423 int error;
1424
1425 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1426 error = soreserve(so, tcp_sendspace, tcp_recvspace);
1427 if (error)
1428 return (error);
1429 }
1430 so->so_rcv.sb_flags |= SB_AUTOSIZE;
1431 so->so_snd.sb_flags |= SB_AUTOSIZE;
1432 INP_INFO_WLOCK(&tcbinfo);
1433 error = in_pcballoc(so, &tcbinfo);
1434 if (error) {
1435 INP_INFO_WUNLOCK(&tcbinfo);
1436 return (error);
1437 }
1438 inp = sotoinpcb(so);
1439 #ifdef INET6
1440 if (inp->inp_vflag & INP_IPV6PROTO) {
1441 inp->inp_vflag |= INP_IPV6;
1442 inp->in6p_hops = -1; /* use kernel default */
1443 }
1444 else
1445 #endif
1446 inp->inp_vflag |= INP_IPV4;
1447 tp = tcp_newtcpcb(inp);
1448 if (tp == NULL) {
1449 in_pcbdetach(inp);
1450 in_pcbfree(inp);
1451 INP_INFO_WUNLOCK(&tcbinfo);
1452 return (ENOBUFS);
1453 }
1454 tp->t_state = TCPS_CLOSED;
1455 INP_WUNLOCK(inp);
1456 INP_INFO_WUNLOCK(&tcbinfo);
1457 return (0);
1458 }
1459
1460 /*
1461 * Initiate (or continue) disconnect.
1462 * If embryonic state, just send reset (once).
1463 * If in ``let data drain'' option and linger null, just drop.
1464 * Otherwise (hard), mark socket disconnecting and drop
1465 * current input data; switch states based on user close, and
1466 * send segment to peer (with FIN).
1467 */
1468 static void
1469 tcp_disconnect(struct tcpcb *tp)
1470 {
1471 struct inpcb *inp = tp->t_inpcb;
1472 struct socket *so = inp->inp_socket;
1473
1474 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1475 INP_WLOCK_ASSERT(inp);
1476
1477 /*
1478 * Neither tcp_close() nor tcp_drop() should return NULL, as the
1479 * socket is still open.
1480 */
1481 if (tp->t_state < TCPS_ESTABLISHED) {
1482 tp = tcp_close(tp);
1483 KASSERT(tp != NULL,
1484 ("tcp_disconnect: tcp_close() returned NULL"));
1485 } else if ((so->so_options & SO_LINGER) && so->so_linger == 0) {
1486 tp = tcp_drop(tp, 0);
1487 KASSERT(tp != NULL,
1488 ("tcp_disconnect: tcp_drop() returned NULL"));
1489 } else {
1490 soisdisconnecting(so);
1491 sbflush(&so->so_rcv);
1492 tcp_usrclosed(tp);
1493 if (!(inp->inp_flags & INP_DROPPED))
1494 tcp_output_disconnect(tp);
1495 }
1496 }
1497
1498 /*
1499 * User issued close, and wish to trail through shutdown states:
1500 * if never received SYN, just forget it. If got a SYN from peer,
1501 * but haven't sent FIN, then go to FIN_WAIT_1 state to send peer a FIN.
1502 * If already got a FIN from peer, then almost done; go to LAST_ACK
1503 * state. In all other cases, have already sent FIN to peer (e.g.
1504 * after PRU_SHUTDOWN), and just have to play tedious game waiting
1505 * for peer to send FIN or not respond to keep-alives, etc.
1506 * We can let the user exit from the close as soon as the FIN is acked.
1507 */
1508 static void
1509 tcp_usrclosed(struct tcpcb *tp)
1510 {
1511
1512 INP_INFO_WLOCK_ASSERT(&tcbinfo);
1513 INP_WLOCK_ASSERT(tp->t_inpcb);
1514
1515 switch (tp->t_state) {
1516 case TCPS_LISTEN:
1517 tcp_offload_listen_close(tp);
1518 /* FALLTHROUGH */
1519 case TCPS_CLOSED:
1520 tp->t_state = TCPS_CLOSED;
1521 tp = tcp_close(tp);
1522 /*
1523 * tcp_close() should never return NULL here as the socket is
1524 * still open.
1525 */
1526 KASSERT(tp != NULL,
1527 ("tcp_usrclosed: tcp_close() returned NULL"));
1528 break;
1529
1530 case TCPS_SYN_SENT:
1531 case TCPS_SYN_RECEIVED:
1532 tp->t_flags |= TF_NEEDFIN;
1533 break;
1534
1535 case TCPS_ESTABLISHED:
1536 tp->t_state = TCPS_FIN_WAIT_1;
1537 break;
1538
1539 case TCPS_CLOSE_WAIT:
1540 tp->t_state = TCPS_LAST_ACK;
1541 break;
1542 }
1543 if (tp->t_state >= TCPS_FIN_WAIT_2) {
1544 soisdisconnected(tp->t_inpcb->inp_socket);
1545 /* Prevent the connection hanging in FIN_WAIT_2 forever. */
1546 if (tp->t_state == TCPS_FIN_WAIT_2) {
1547 int timeout;
1548
1549 timeout = (tcp_fast_finwait2_recycle) ?
1550 tcp_finwait2_timeout : tcp_maxidle;
1551 tcp_timer_activate(tp, TT_2MSL, timeout);
1552 }
1553 }
1554 }
1555
1556 #ifdef DDB
1557 static void
1558 db_print_indent(int indent)
1559 {
1560 int i;
1561
1562 for (i = 0; i < indent; i++)
1563 db_printf(" ");
1564 }
1565
1566 static void
1567 db_print_tstate(int t_state)
1568 {
1569
1570 switch (t_state) {
1571 case TCPS_CLOSED:
1572 db_printf("TCPS_CLOSED");
1573 return;
1574
1575 case TCPS_LISTEN:
1576 db_printf("TCPS_LISTEN");
1577 return;
1578
1579 case TCPS_SYN_SENT:
1580 db_printf("TCPS_SYN_SENT");
1581 return;
1582
1583 case TCPS_SYN_RECEIVED:
1584 db_printf("TCPS_SYN_RECEIVED");
1585 return;
1586
1587 case TCPS_ESTABLISHED:
1588 db_printf("TCPS_ESTABLISHED");
1589 return;
1590
1591 case TCPS_CLOSE_WAIT:
1592 db_printf("TCPS_CLOSE_WAIT");
1593 return;
1594
1595 case TCPS_FIN_WAIT_1:
1596 db_printf("TCPS_FIN_WAIT_1");
1597 return;
1598
1599 case TCPS_CLOSING:
1600 db_printf("TCPS_CLOSING");
1601 return;
1602
1603 case TCPS_LAST_ACK:
1604 db_printf("TCPS_LAST_ACK");
1605 return;
1606
1607 case TCPS_FIN_WAIT_2:
1608 db_printf("TCPS_FIN_WAIT_2");
1609 return;
1610
1611 case TCPS_TIME_WAIT:
1612 db_printf("TCPS_TIME_WAIT");
1613 return;
1614
1615 default:
1616 db_printf("unknown");
1617 return;
1618 }
1619 }
1620
1621 static void
1622 db_print_tflags(u_int t_flags)
1623 {
1624 int comma;
1625
1626 comma = 0;
1627 if (t_flags & TF_ACKNOW) {
1628 db_printf("%sTF_ACKNOW", comma ? ", " : "");
1629 comma = 1;
1630 }
1631 if (t_flags & TF_DELACK) {
1632 db_printf("%sTF_DELACK", comma ? ", " : "");
1633 comma = 1;
1634 }
1635 if (t_flags & TF_NODELAY) {
1636 db_printf("%sTF_NODELAY", comma ? ", " : "");
1637 comma = 1;
1638 }
1639 if (t_flags & TF_NOOPT) {
1640 db_printf("%sTF_NOOPT", comma ? ", " : "");
1641 comma = 1;
1642 }
1643 if (t_flags & TF_SENTFIN) {
1644 db_printf("%sTF_SENTFIN", comma ? ", " : "");
1645 comma = 1;
1646 }
1647 if (t_flags & TF_REQ_SCALE) {
1648 db_printf("%sTF_REQ_SCALE", comma ? ", " : "");
1649 comma = 1;
1650 }
1651 if (t_flags & TF_RCVD_SCALE) {
1652 db_printf("%sTF_RECVD_SCALE", comma ? ", " : "");
1653 comma = 1;
1654 }
1655 if (t_flags & TF_REQ_TSTMP) {
1656 db_printf("%sTF_REQ_TSTMP", comma ? ", " : "");
1657 comma = 1;
1658 }
1659 if (t_flags & TF_RCVD_TSTMP) {
1660 db_printf("%sTF_RCVD_TSTMP", comma ? ", " : "");
1661 comma = 1;
1662 }
1663 if (t_flags & TF_SACK_PERMIT) {
1664 db_printf("%sTF_SACK_PERMIT", comma ? ", " : "");
1665 comma = 1;
1666 }
1667 if (t_flags & TF_NEEDSYN) {
1668 db_printf("%sTF_NEEDSYN", comma ? ", " : "");
1669 comma = 1;
1670 }
1671 if (t_flags & TF_NEEDFIN) {
1672 db_printf("%sTF_NEEDFIN", comma ? ", " : "");
1673 comma = 1;
1674 }
1675 if (t_flags & TF_NOPUSH) {
1676 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1677 comma = 1;
1678 }
1679 if (t_flags & TF_NOPUSH) {
1680 db_printf("%sTF_NOPUSH", comma ? ", " : "");
1681 comma = 1;
1682 }
1683 if (t_flags & TF_MORETOCOME) {
1684 db_printf("%sTF_MORETOCOME", comma ? ", " : "");
1685 comma = 1;
1686 }
1687 if (t_flags & TF_LQ_OVERFLOW) {
1688 db_printf("%sTF_LQ_OVERFLOW", comma ? ", " : "");
1689 comma = 1;
1690 }
1691 if (t_flags & TF_LASTIDLE) {
1692 db_printf("%sTF_LASTIDLE", comma ? ", " : "");
1693 comma = 1;
1694 }
1695 if (t_flags & TF_RXWIN0SENT) {
1696 db_printf("%sTF_RXWIN0SENT", comma ? ", " : "");
1697 comma = 1;
1698 }
1699 if (t_flags & TF_FASTRECOVERY) {
1700 db_printf("%sTF_FASTRECOVERY", comma ? ", " : "");
1701 comma = 1;
1702 }
1703 if (t_flags & TF_WASFRECOVERY) {
1704 db_printf("%sTF_WASFRECOVERY", comma ? ", " : "");
1705 comma = 1;
1706 }
1707 if (t_flags & TF_SIGNATURE) {
1708 db_printf("%sTF_SIGNATURE", comma ? ", " : "");
1709 comma = 1;
1710 }
1711 if (t_flags & TF_FORCEDATA) {
1712 db_printf("%sTF_FORCEDATA", comma ? ", " : "");
1713 comma = 1;
1714 }
1715 if (t_flags & TF_TSO) {
1716 db_printf("%sTF_TSO", comma ? ", " : "");
1717 comma = 1;
1718 }
1719 }
1720
1721 static void
1722 db_print_toobflags(char t_oobflags)
1723 {
1724 int comma;
1725
1726 comma = 0;
1727 if (t_oobflags & TCPOOB_HAVEDATA) {
1728 db_printf("%sTCPOOB_HAVEDATA", comma ? ", " : "");
1729 comma = 1;
1730 }
1731 if (t_oobflags & TCPOOB_HADDATA) {
1732 db_printf("%sTCPOOB_HADDATA", comma ? ", " : "");
1733 comma = 1;
1734 }
1735 }
1736
1737 static void
1738 db_print_tcpcb(struct tcpcb *tp, const char *name, int indent)
1739 {
1740
1741 db_print_indent(indent);
1742 db_printf("%s at %p\n", name, tp);
1743
1744 indent += 2;
1745
1746 db_print_indent(indent);
1747 db_printf("t_segq first: %p t_segqlen: %d t_dupacks: %d\n",
1748 LIST_FIRST(&tp->t_segq), tp->t_segqlen, tp->t_dupacks);
1749
1750 db_print_indent(indent);
1751 db_printf("tt_rexmt: %p tt_persist: %p tt_keep: %p\n",
1752 &tp->t_timers->tt_rexmt, &tp->t_timers->tt_persist, &tp->t_timers->tt_keep);
1753
1754 db_print_indent(indent);
1755 db_printf("tt_2msl: %p tt_delack: %p t_inpcb: %p\n", &tp->t_timers->tt_2msl,
1756 &tp->t_timers->tt_delack, tp->t_inpcb);
1757
1758 db_print_indent(indent);
1759 db_printf("t_state: %d (", tp->t_state);
1760 db_print_tstate(tp->t_state);
1761 db_printf(")\n");
1762
1763 db_print_indent(indent);
1764 db_printf("t_flags: 0x%x (", tp->t_flags);
1765 db_print_tflags(tp->t_flags);
1766 db_printf(")\n");
1767
1768 db_print_indent(indent);
1769 db_printf("snd_una: 0x%08x snd_max: 0x%08x snd_nxt: x0%08x\n",
1770 tp->snd_una, tp->snd_max, tp->snd_nxt);
1771
1772 db_print_indent(indent);
1773 db_printf("snd_up: 0x%08x snd_wl1: 0x%08x snd_wl2: 0x%08x\n",
1774 tp->snd_up, tp->snd_wl1, tp->snd_wl2);
1775
1776 db_print_indent(indent);
1777 db_printf("iss: 0x%08x irs: 0x%08x rcv_nxt: 0x%08x\n",
1778 tp->iss, tp->irs, tp->rcv_nxt);
1779
1780 db_print_indent(indent);
1781 db_printf("rcv_adv: 0x%08x rcv_wnd: %lu rcv_up: 0x%08x\n",
1782 tp->rcv_adv, tp->rcv_wnd, tp->rcv_up);
1783
1784 db_print_indent(indent);
1785 db_printf("snd_wnd: %lu snd_cwnd: %lu snd_bwnd: %lu\n",
1786 tp->snd_wnd, tp->snd_cwnd, tp->snd_bwnd);
1787
1788 db_print_indent(indent);
1789 db_printf("snd_ssthresh: %lu snd_bandwidth: %lu snd_recover: "
1790 "0x%08x\n", tp->snd_ssthresh, tp->snd_bandwidth,
1791 tp->snd_recover);
1792
1793 db_print_indent(indent);
1794 db_printf("t_maxopd: %u t_rcvtime: %lu t_startime: %lu\n",
1795 tp->t_maxopd, tp->t_rcvtime, tp->t_starttime);
1796
1797 db_print_indent(indent);
1798 db_printf("t_rttime: %d t_rtsq: 0x%08x t_bw_rtttime: %d\n",
1799 tp->t_rtttime, tp->t_rtseq, tp->t_bw_rtttime);
1800
1801 db_print_indent(indent);
1802 db_printf("t_bw_rtseq: 0x%08x t_rxtcur: %d t_maxseg: %u "
1803 "t_srtt: %d\n", tp->t_bw_rtseq, tp->t_rxtcur, tp->t_maxseg,
1804 tp->t_srtt);
1805
1806 db_print_indent(indent);
1807 db_printf("t_rttvar: %d t_rxtshift: %d t_rttmin: %u "
1808 "t_rttbest: %u\n", tp->t_rttvar, tp->t_rxtshift, tp->t_rttmin,
1809 tp->t_rttbest);
1810
1811 db_print_indent(indent);
1812 db_printf("t_rttupdated: %lu max_sndwnd: %lu t_softerror: %d\n",
1813 tp->t_rttupdated, tp->max_sndwnd, tp->t_softerror);
1814
1815 db_print_indent(indent);
1816 db_printf("t_oobflags: 0x%x (", tp->t_oobflags);
1817 db_print_toobflags(tp->t_oobflags);
1818 db_printf(") t_iobc: 0x%02x\n", tp->t_iobc);
1819
1820 db_print_indent(indent);
1821 db_printf("snd_scale: %u rcv_scale: %u request_r_scale: %u\n",
1822 tp->snd_scale, tp->rcv_scale, tp->request_r_scale);
1823
1824 db_print_indent(indent);
1825 db_printf("ts_recent: %u ts_recent_age: %lu\n",
1826 tp->ts_recent, tp->ts_recent_age);
1827
1828 db_print_indent(indent);
1829 db_printf("ts_offset: %u last_ack_sent: 0x%08x snd_cwnd_prev: "
1830 "%lu\n", tp->ts_offset, tp->last_ack_sent, tp->snd_cwnd_prev);
1831
1832 db_print_indent(indent);
1833 db_printf("snd_ssthresh_prev: %lu snd_recover_prev: 0x%08x "
1834 "t_badrxtwin: %lu\n", tp->snd_ssthresh_prev,
1835 tp->snd_recover_prev, tp->t_badrxtwin);
1836
1837 db_print_indent(indent);
1838 db_printf("snd_numholes: %d snd_holes first: %p\n",
1839 tp->snd_numholes, TAILQ_FIRST(&tp->snd_holes));
1840
1841 db_print_indent(indent);
1842 db_printf("snd_fack: 0x%08x rcv_numsacks: %d sack_newdata: "
1843 "0x%08x\n", tp->snd_fack, tp->rcv_numsacks, tp->sack_newdata);
1844
1845 /* Skip sackblks, sackhint. */
1846
1847 db_print_indent(indent);
1848 db_printf("t_rttlow: %d rfbuf_ts: %u rfbuf_cnt: %d\n",
1849 tp->t_rttlow, tp->rfbuf_ts, tp->rfbuf_cnt);
1850 }
1851
1852 DB_SHOW_COMMAND(tcpcb, db_show_tcpcb)
1853 {
1854 struct tcpcb *tp;
1855
1856 if (!have_addr) {
1857 db_printf("usage: show tcpcb <addr>\n");
1858 return;
1859 }
1860 tp = (struct tcpcb *)addr;
1861
1862 db_print_tcpcb(tp, "tcpcb", 0);
1863 }
1864 #endif
Cache object: 336fe656475939bc03361cdf321f4781
|