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