tcp_timewait.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
  5. * The Regents of the University of California. 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. * 3. 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. * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
  32. */
  33. #include <sys/cdefs.h>
  34. __FBSDID("$FreeBSD$");
  35. #include "opt_inet.h"
  36. #include "opt_inet6.h"
  37. #include "opt_tcpdebug.h"
  38. #include <sys/param.h>
  39. #include <sys/systm.h>
  40. #include <sys/callout.h>
  41. #include <sys/kernel.h>
  42. #include <sys/sysctl.h>
  43. #include <sys/malloc.h>
  44. #include <sys/mbuf.h>
  45. #include <sys/priv.h>
  46. #include <sys/proc.h>
  47. #include <sys/socket.h>
  48. #include <sys/socketvar.h>
  49. #ifndef INVARIANTS
  50. #include <sys/syslog.h>
  51. #endif
  52. #include <sys/protosw.h>
  53. #include <sys/random.h>
  54. #include <vm/uma.h>
  55. #include <net/route.h>
  56. #include <net/if.h>
  57. #include <net/if_var.h>
  58. #include <net/vnet.h>
  59. #include <netinet/in.h>
  60. #include <netinet/in_kdtrace.h>
  61. #include <netinet/in_pcb.h>
  62. #include <netinet/in_systm.h>
  63. #include <netinet/in_var.h>
  64. #include <netinet/ip.h>
  65. #include <netinet/ip_icmp.h>
  66. #include <netinet/ip_var.h>
  67. #ifdef INET6
  68. #include <netinet/ip6.h>
  69. #include <netinet6/in6_pcb.h>
  70. #include <netinet6/ip6_var.h>
  71. #include <netinet6/scope6_var.h>
  72. #include <netinet6/nd6.h>
  73. #endif
  74. #include <netinet/tcp.h>
  75. #include <netinet/tcp_fsm.h>
  76. #include <netinet/tcp_seq.h>
  77. #include <netinet/tcp_timer.h>
  78. #include <netinet/tcp_var.h>
  79. #ifdef INET6
  80. #include <netinet6/tcp6_var.h>
  81. #endif
  82. #include <netinet/tcpip.h>
  83. #ifdef TCPDEBUG
  84. #include <netinet/tcp_debug.h>
  85. #endif
  86. #ifdef INET6
  87. #include <netinet6/ip6protosw.h>
  88. #endif
  89. #include <machine/in_cksum.h>
  90. #include <security/mac/mac_framework.h>
  91. VNET_DEFINE_STATIC(uma_zone_t, tcptw_zone);
  92. #define V_tcptw_zone VNET(tcptw_zone)
  93. static int maxtcptw;
  94. /*
  95. * The timed wait queue contains references to each of the TCP sessions
  96. * currently in the TIME_WAIT state. The queue pointers, including the
  97. * queue pointers in each tcptw structure, are protected using the global
  98. * timewait lock, which must be held over queue iteration and modification.
  99. *
  100. * Rules on tcptw usage:
  101. * - a inpcb is always freed _after_ its tcptw
  102. * - a tcptw relies on its inpcb reference counting for memory stability
  103. * - a tcptw is dereferenceable only while its inpcb is locked
  104. */
  105. VNET_DEFINE_STATIC(TAILQ_HEAD(, tcptw), twq_2msl);
  106. #define V_twq_2msl VNET(twq_2msl)
  107. /* Global timewait lock */
  108. VNET_DEFINE_STATIC(struct rwlock, tw_lock);
  109. #define V_tw_lock VNET(tw_lock)
  110. #define TW_LOCK_INIT(tw, d) rw_init_flags(&(tw), (d), 0)
  111. #define TW_LOCK_DESTROY(tw) rw_destroy(&(tw))
  112. #define TW_RLOCK(tw) rw_rlock(&(tw))
  113. #define TW_WLOCK(tw) rw_wlock(&(tw))
  114. #define TW_RUNLOCK(tw) rw_runlock(&(tw))
  115. #define TW_WUNLOCK(tw) rw_wunlock(&(tw))
  116. #define TW_LOCK_ASSERT(tw) rw_assert(&(tw), RA_LOCKED)
  117. #define TW_RLOCK_ASSERT(tw) rw_assert(&(tw), RA_RLOCKED)
  118. #define TW_WLOCK_ASSERT(tw) rw_assert(&(tw), RA_WLOCKED)
  119. #define TW_UNLOCK_ASSERT(tw) rw_assert(&(tw), RA_UNLOCKED)
  120. static void tcp_tw_2msl_reset(struct tcptw *, int);
  121. static void tcp_tw_2msl_stop(struct tcptw *, int);
  122. static int tcp_twrespond(struct tcptw *, int);
  123. static int
  124. tcptw_auto_size(void)
  125. {
  126. int halfrange;
  127. /*
  128. * Max out at half the ephemeral port range so that TIME_WAIT
  129. * sockets don't tie up too many ephemeral ports.
  130. */
  131. if (V_ipport_lastauto > V_ipport_firstauto)
  132. halfrange = (V_ipport_lastauto - V_ipport_firstauto) / 2;
  133. else
  134. halfrange = (V_ipport_firstauto - V_ipport_lastauto) / 2;
  135. /* Protect against goofy port ranges smaller than 32. */
  136. return (imin(imax(halfrange, 32), maxsockets / 5));
  137. }
  138. static int
  139. sysctl_maxtcptw(SYSCTL_HANDLER_ARGS)
  140. {
  141. int error, new;
  142. if (maxtcptw == 0)
  143. new = tcptw_auto_size();
  144. else
  145. new = maxtcptw;
  146. error = sysctl_handle_int(oidp, &new, 0, req);
  147. if (error == 0 && req->newptr)
  148. if (new >= 32) {
  149. maxtcptw = new;
  150. uma_zone_set_max(V_tcptw_zone, maxtcptw);
  151. }
  152. return (error);
  153. }
  154. SYSCTL_PROC(_net_inet_tcp, OID_AUTO, maxtcptw,
  155. CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
  156. &maxtcptw, 0, sysctl_maxtcptw, "IU",
  157. "Maximum number of compressed TCP TIME_WAIT entries");
  158. VNET_DEFINE_STATIC(int, nolocaltimewait) = 0;
  159. #define V_nolocaltimewait VNET(nolocaltimewait)
  160. SYSCTL_INT(_net_inet_tcp, OID_AUTO, nolocaltimewait, CTLFLAG_VNET | CTLFLAG_RW,
  161. &VNET_NAME(nolocaltimewait), 0,
  162. "Do not create compressed TCP TIME_WAIT entries for local connections");
  163. void
  164. tcp_tw_zone_change(void)
  165. {
  166. if (maxtcptw == 0)
  167. uma_zone_set_max(V_tcptw_zone, tcptw_auto_size());
  168. }
  169. void
  170. tcp_tw_init(void)
  171. {
  172. V_tcptw_zone = uma_zcreate("tcptw", sizeof(struct tcptw),
  173. NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
  174. TUNABLE_INT_FETCH("net.inet.tcp.maxtcptw", &maxtcptw);
  175. if (maxtcptw == 0)
  176. uma_zone_set_max(V_tcptw_zone, tcptw_auto_size());
  177. else
  178. uma_zone_set_max(V_tcptw_zone, maxtcptw);
  179. TAILQ_INIT(&V_twq_2msl);
  180. TW_LOCK_INIT(V_tw_lock, "tcptw");
  181. }
  182. #ifdef VIMAGE
  183. void
  184. tcp_tw_destroy(void)
  185. {
  186. struct tcptw *tw;
  187. struct epoch_tracker et;
  188. NET_EPOCH_ENTER(et);
  189. while ((tw = TAILQ_FIRST(&V_twq_2msl)) != NULL)
  190. tcp_twclose(tw, 0);
  191. NET_EPOCH_EXIT(et);
  192. TW_LOCK_DESTROY(V_tw_lock);
  193. uma_zdestroy(V_tcptw_zone);
  194. }
  195. #endif
  196. /*
  197. * Move a TCP connection into TIME_WAIT state.
  198. * tcbinfo is locked.
  199. * inp is locked, and is unlocked before returning.
  200. */
  201. void
  202. tcp_twstart(struct tcpcb *tp)
  203. {
  204. struct tcptw twlocal, *tw;
  205. struct inpcb *inp = tp->t_inpcb;
  206. struct socket *so;
  207. uint32_t recwin;
  208. bool acknow, local;
  209. #ifdef INET6
  210. bool isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6;
  211. #endif
  212. NET_EPOCH_ASSERT();
  213. INP_WLOCK_ASSERT(inp);
  214. /* A dropped inp should never transition to TIME_WAIT state. */
  215. KASSERT((inp->inp_flags & INP_DROPPED) == 0, ("tcp_twstart: "
  216. "(inp->inp_flags & INP_DROPPED) != 0"));
  217. if (V_nolocaltimewait) {
  218. #ifdef INET6
  219. if (isipv6)
  220. local = in6_localaddr(&inp->in6p_faddr);
  221. else
  222. #endif
  223. #ifdef INET
  224. local = in_localip(inp->inp_faddr);
  225. #else
  226. local = false;
  227. #endif
  228. } else
  229. local = false;
  230. /*
  231. * For use only by DTrace. We do not reference the state
  232. * after this point so modifying it in place is not a problem.
  233. */
  234. tcp_state_change(tp, TCPS_TIME_WAIT);
  235. if (local)
  236. tw = &twlocal;
  237. else
  238. tw = uma_zalloc(V_tcptw_zone, M_NOWAIT);
  239. if (tw == NULL) {
  240. /*
  241. * Reached limit on total number of TIMEWAIT connections
  242. * allowed. Remove a connection from TIMEWAIT queue in LRU
  243. * fashion to make room for this connection.
  244. *
  245. * XXX: Check if it possible to always have enough room
  246. * in advance based on guarantees provided by uma_zalloc().
  247. */
  248. tw = tcp_tw_2msl_scan(1);
  249. if (tw == NULL) {
  250. tp = tcp_close(tp);
  251. if (tp != NULL)
  252. INP_WUNLOCK(inp);
  253. return;
  254. }
  255. }
  256. /*
  257. * For !local case the tcptw will hold a reference on its inpcb
  258. * until tcp_twclose is called.
  259. */
  260. tw->tw_inpcb = inp;
  261. /*
  262. * Recover last window size sent.
  263. */
  264. so = inp->inp_socket;
  265. recwin = lmin(lmax(sbspace(&so->so_rcv), 0),
  266. (long)TCP_MAXWIN << tp->rcv_scale);
  267. if (recwin < (so->so_rcv.sb_hiwat / 4) &&
  268. recwin < tp->t_maxseg)
  269. recwin = 0;
  270. if (SEQ_GT(tp->rcv_adv, tp->rcv_nxt) &&
  271. recwin < (tp->rcv_adv - tp->rcv_nxt))
  272. recwin = (tp->rcv_adv - tp->rcv_nxt);
  273. tw->last_win = (u_short)(recwin >> tp->rcv_scale);
  274. /*
  275. * Set t_recent if timestamps are used on the connection.
  276. */
  277. if ((tp->t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP|TF_NOOPT)) ==
  278. (TF_REQ_TSTMP|TF_RCVD_TSTMP)) {
  279. tw->t_recent = tp->ts_recent;
  280. tw->ts_offset = tp->ts_offset;
  281. } else {
  282. tw->t_recent = 0;
  283. tw->ts_offset = 0;
  284. }
  285. tw->snd_nxt = tp->snd_nxt;
  286. tw->rcv_nxt = tp->rcv_nxt;
  287. tw->iss = tp->iss;
  288. tw->irs = tp->irs;
  289. tw->t_starttime = tp->t_starttime;
  290. tw->tw_time = 0;
  291. /* XXX
  292. * If this code will
  293. * be used for fin-wait-2 state also, then we may need
  294. * a ts_recent from the last segment.
  295. */
  296. acknow = tp->t_flags & TF_ACKNOW;
  297. /*
  298. * First, discard tcpcb state, which includes stopping its timers and
  299. * freeing it. tcp_discardcb() used to also release the inpcb, but
  300. * that work is now done in the caller.
  301. *
  302. * Note: soisdisconnected() call used to be made in tcp_discardcb(),
  303. * and might not be needed here any longer.
  304. */
  305. tcp_discardcb(tp);
  306. soisdisconnected(so);
  307. tw->tw_so_options = so->so_options;
  308. inp->inp_flags |= INP_TIMEWAIT;
  309. if (acknow)
  310. tcp_twrespond(tw, TH_ACK);
  311. if (local)
  312. in_pcbdrop(inp);
  313. else {
  314. in_pcbref(inp); /* Reference from tw */
  315. tw->tw_cred = crhold(so->so_cred);
  316. inp->inp_ppcb = tw;
  317. TCPSTATES_INC(TCPS_TIME_WAIT);
  318. tcp_tw_2msl_reset(tw, 0);
  319. }
  320. /*
  321. * If the inpcb owns the sole reference to the socket, then we can
  322. * detach and free the socket as it is not needed in time wait.
  323. */
  324. if (inp->inp_flags & INP_SOCKREF) {
  325. KASSERT(so->so_state & SS_PROTOREF,
  326. ("tcp_twstart: !SS_PROTOREF"));
  327. inp->inp_flags &= ~INP_SOCKREF;
  328. INP_WUNLOCK(inp);
  329. SOCK_LOCK(so);
  330. so->so_state &= ~SS_PROTOREF;
  331. sofree(so);
  332. } else
  333. INP_WUNLOCK(inp);
  334. }
  335. /*
  336. * Returns 1 if the TIME_WAIT state was killed and we should start over,
  337. * looking for a pcb in the listen state. Returns 0 otherwise.
  338. * It be called with to == NULL only for pure SYN-segments.
  339. */
  340. int
  341. tcp_twcheck(struct inpcb *inp, struct tcpopt *to, struct tcphdr *th,
  342. struct mbuf *m, int tlen)
  343. {
  344. struct tcptw *tw;
  345. int thflags;
  346. tcp_seq seq;
  347. NET_EPOCH_ASSERT();
  348. INP_WLOCK_ASSERT(inp);
  349. /*
  350. * XXXRW: Time wait state for inpcb has been recycled, but inpcb is
  351. * still present. This is undesirable, but temporarily necessary
  352. * until we work out how to handle inpcb's who's timewait state has
  353. * been removed.
  354. */
  355. tw = intotw(inp);
  356. if (tw == NULL)
  357. goto drop;
  358. thflags = th->th_flags;
  359. KASSERT(to != NULL || (thflags & (TH_SYN | TH_ACK)) == TH_SYN,
  360. ("tcp_twcheck: called without options on a non-SYN segment"));
  361. /*
  362. * NOTE: for FIN_WAIT_2 (to be added later),
  363. * must validate sequence number before accepting RST
  364. */
  365. /*
  366. * If the segment contains RST:
  367. * Drop the segment - see Stevens, vol. 2, p. 964 and
  368. * RFC 1337.
  369. */
  370. if (thflags & TH_RST)
  371. goto drop;
  372. #if 0
  373. /* PAWS not needed at the moment */
  374. /*
  375. * RFC 1323 PAWS: If we have a timestamp reply on this segment
  376. * and it's less than ts_recent, drop it.
  377. */
  378. if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
  379. TSTMP_LT(to.to_tsval, tp->ts_recent)) {
  380. if ((thflags & TH_ACK) == 0)
  381. goto drop;
  382. goto ack;
  383. }
  384. /*
  385. * ts_recent is never updated because we never accept new segments.
  386. */
  387. #endif
  388. /*
  389. * If a new connection request is received
  390. * while in TIME_WAIT, drop the old connection
  391. * and start over if the sequence numbers
  392. * are above the previous ones.
  393. */
  394. if ((thflags & TH_SYN) && SEQ_GT(th->th_seq, tw->rcv_nxt)) {
  395. tcp_twclose(tw, 0);
  396. return (1);
  397. }
  398. /*
  399. * Drop the segment if it does not contain an ACK.
  400. */
  401. if ((thflags & TH_ACK) == 0)
  402. goto drop;
  403. /*
  404. * If timestamps were negotiated during SYN/ACK and a
  405. * segment without a timestamp is received, silently drop
  406. * the segment, unless the missing timestamps are tolerated.
  407. * See section 3.2 of RFC 7323.
  408. */
  409. if (((to->to_flags & TOF_TS) == 0) && (tw->t_recent != 0) &&
  410. (V_tcp_tolerate_missing_ts == 0)) {
  411. goto drop;
  412. }
  413. /*
  414. * Reset the 2MSL timer if this is a duplicate FIN.
  415. */
  416. if (thflags & TH_FIN) {
  417. seq = th->th_seq + tlen + (thflags & TH_SYN ? 1 : 0);
  418. if (seq + 1 == tw->rcv_nxt)
  419. tcp_tw_2msl_reset(tw, 1);
  420. }
  421. /*
  422. * Acknowledge the segment if it has data or is not a duplicate ACK.
  423. */
  424. if (thflags != TH_ACK || tlen != 0 ||
  425. th->th_seq != tw->rcv_nxt || th->th_ack != tw->snd_nxt) {
  426. TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
  427. tcp_twrespond(tw, TH_ACK);
  428. goto dropnoprobe;
  429. }
  430. drop:
  431. TCP_PROBE5(receive, NULL, NULL, m, NULL, th);
  432. dropnoprobe:
  433. INP_WUNLOCK(inp);
  434. m_freem(m);
  435. return (0);
  436. }
  437. void
  438. tcp_twclose(struct tcptw *tw, int reuse)
  439. {
  440. struct socket *so;
  441. struct inpcb *inp;
  442. /*
  443. * At this point, we are in one of two situations:
  444. *
  445. * (1) We have no socket, just an inpcb<->twtcp pair. We can free
  446. * all state.
  447. *
  448. * (2) We have a socket -- if we own a reference, release it and
  449. * notify the socket layer.
  450. */
  451. inp = tw->tw_inpcb;
  452. KASSERT((inp->inp_flags & INP_TIMEWAIT), ("tcp_twclose: !timewait"));
  453. KASSERT(intotw(inp) == tw, ("tcp_twclose: inp_ppcb != tw"));
  454. NET_EPOCH_ASSERT();
  455. INP_WLOCK_ASSERT(inp);
  456. tcp_tw_2msl_stop(tw, reuse);
  457. inp->inp_ppcb = NULL;
  458. in_pcbdrop(inp);
  459. so = inp->inp_socket;
  460. if (so != NULL) {
  461. /*
  462. * If there's a socket, handle two cases: first, we own a
  463. * strong reference, which we will now release, or we don't
  464. * in which case another reference exists (XXXRW: think
  465. * about this more), and we don't need to take action.
  466. */
  467. if (inp->inp_flags & INP_SOCKREF) {
  468. inp->inp_flags &= ~INP_SOCKREF;
  469. INP_WUNLOCK(inp);
  470. SOCK_LOCK(so);
  471. KASSERT(so->so_state & SS_PROTOREF,
  472. ("tcp_twclose: INP_SOCKREF && !SS_PROTOREF"));
  473. so->so_state &= ~SS_PROTOREF;
  474. sofree(so);
  475. } else {
  476. /*
  477. * If we don't own the only reference, the socket and
  478. * inpcb need to be left around to be handled by
  479. * tcp_usr_detach() later.
  480. */
  481. INP_WUNLOCK(inp);
  482. }
  483. } else {
  484. /*
  485. * The socket has been already cleaned-up for us, only free the
  486. * inpcb.
  487. */
  488. in_pcbfree(inp);
  489. }
  490. TCPSTAT_INC(tcps_closed);
  491. }
  492. static int
  493. tcp_twrespond(struct tcptw *tw, int flags)
  494. {
  495. struct inpcb *inp = tw->tw_inpcb;
  496. #if defined(INET6) || defined(INET)
  497. struct tcphdr *th = NULL;
  498. #endif
  499. struct mbuf *m;
  500. #ifdef INET
  501. struct ip *ip = NULL;
  502. #endif
  503. u_int hdrlen, optlen;
  504. int error = 0; /* Keep compiler happy */
  505. struct tcpopt to;
  506. #ifdef INET6
  507. struct ip6_hdr *ip6 = NULL;
  508. int isipv6 = inp->inp_inc.inc_flags & INC_ISIPV6;
  509. #endif
  510. hdrlen = 0; /* Keep compiler happy */
  511. INP_WLOCK_ASSERT(inp);
  512. m = m_gethdr(M_NOWAIT, MT_DATA);
  513. if (m == NULL)
  514. return (ENOBUFS);
  515. m->m_data += max_linkhdr;
  516. #ifdef MAC
  517. mac_inpcb_create_mbuf(inp, m);
  518. #endif
  519. #ifdef INET6
  520. if (isipv6) {
  521. hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
  522. ip6 = mtod(m, struct ip6_hdr *);
  523. th = (struct tcphdr *)(ip6 + 1);
  524. tcpip_fillheaders(inp, ip6, th);
  525. }
  526. #endif
  527. #if defined(INET6) && defined(INET)
  528. else
  529. #endif
  530. #ifdef INET
  531. {
  532. hdrlen = sizeof(struct tcpiphdr);
  533. ip = mtod(m, struct ip *);
  534. th = (struct tcphdr *)(ip + 1);
  535. tcpip_fillheaders(inp, ip, th);
  536. }
  537. #endif
  538. to.to_flags = 0;
  539. /*
  540. * Send a timestamp and echo-reply if both our side and our peer
  541. * have sent timestamps in our SYN's and this is not a RST.
  542. */
  543. if (tw->t_recent && flags == TH_ACK) {
  544. to.to_flags |= TOF_TS;
  545. to.to_tsval = tcp_ts_getticks() + tw->ts_offset;
  546. to.to_tsecr = tw->t_recent;
  547. }
  548. optlen = tcp_addoptions(&to, (u_char *)(th + 1));
  549. m->m_len = hdrlen + optlen;
  550. m->m_pkthdr.len = m->m_len;
  551. KASSERT(max_linkhdr + m->m_len <= MHLEN, ("tcptw: mbuf too small"));
  552. th->th_seq = htonl(tw->snd_nxt);
  553. th->th_ack = htonl(tw->rcv_nxt);
  554. th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
  555. th->th_flags = flags;
  556. th->th_win = htons(tw->last_win);
  557. m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
  558. #ifdef INET6
  559. if (isipv6) {
  560. m->m_pkthdr.csum_flags = CSUM_TCP_IPV6;
  561. th->th_sum = in6_cksum_pseudo(ip6,
  562. sizeof(struct tcphdr) + optlen, IPPROTO_TCP, 0);
  563. ip6->ip6_hlim = in6_selecthlim(inp, NULL);
  564. TCP_PROBE5(send, NULL, NULL, ip6, NULL, th);
  565. error = ip6_output(m, inp->in6p_outputopts, NULL,
  566. (tw->tw_so_options & SO_DONTROUTE), NULL, NULL, inp);
  567. }
  568. #endif
  569. #if defined(INET6) && defined(INET)
  570. else
  571. #endif
  572. #ifdef INET
  573. {
  574. m->m_pkthdr.csum_flags = CSUM_TCP;
  575. th->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
  576. htons(sizeof(struct tcphdr) + optlen + IPPROTO_TCP));
  577. ip->ip_len = htons(m->m_pkthdr.len);
  578. if (V_path_mtu_discovery)
  579. ip->ip_off |= htons(IP_DF);
  580. TCP_PROBE5(send, NULL, NULL, ip, NULL, th);
  581. error = ip_output(m, inp->inp_options, NULL,
  582. ((tw->tw_so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0),
  583. NULL, inp);
  584. }
  585. #endif
  586. if (flags & TH_ACK)
  587. TCPSTAT_INC(tcps_sndacks);
  588. else
  589. TCPSTAT_INC(tcps_sndctrl);
  590. TCPSTAT_INC(tcps_sndtotal);
  591. return (error);
  592. }
  593. static void
  594. tcp_tw_2msl_reset(struct tcptw *tw, int rearm)
  595. {
  596. NET_EPOCH_ASSERT();
  597. INP_WLOCK_ASSERT(tw->tw_inpcb);
  598. TW_WLOCK(V_tw_lock);
  599. if (rearm)
  600. TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl);
  601. tw->tw_time = ticks + 2 * tcp_msl;
  602. TAILQ_INSERT_TAIL(&V_twq_2msl, tw, tw_2msl);
  603. TW_WUNLOCK(V_tw_lock);
  604. }
  605. static void
  606. tcp_tw_2msl_stop(struct tcptw *tw, int reuse)
  607. {
  608. struct ucred *cred;
  609. struct inpcb *inp;
  610. int released __unused;
  611. NET_EPOCH_ASSERT();
  612. TW_WLOCK(V_tw_lock);
  613. inp = tw->tw_inpcb;
  614. tw->tw_inpcb = NULL;
  615. TAILQ_REMOVE(&V_twq_2msl, tw, tw_2msl);
  616. cred = tw->tw_cred;
  617. tw->tw_cred = NULL;
  618. TW_WUNLOCK(V_tw_lock);
  619. if (cred != NULL)
  620. crfree(cred);
  621. released = in_pcbrele_wlocked(inp);
  622. KASSERT(!released, ("%s: inp should not be released here", __func__));
  623. if (!reuse)
  624. uma_zfree(V_tcptw_zone, tw);
  625. TCPSTATES_DEC(TCPS_TIME_WAIT);
  626. }
  627. struct tcptw *
  628. tcp_tw_2msl_scan(int reuse)
  629. {
  630. struct tcptw *tw;
  631. struct inpcb *inp;
  632. NET_EPOCH_ASSERT();
  633. for (;;) {
  634. TW_RLOCK(V_tw_lock);
  635. tw = TAILQ_FIRST(&V_twq_2msl);
  636. if (tw == NULL || (!reuse && (tw->tw_time - ticks) > 0)) {
  637. TW_RUNLOCK(V_tw_lock);
  638. break;
  639. }
  640. KASSERT(tw->tw_inpcb != NULL, ("%s: tw->tw_inpcb == NULL",
  641. __func__));
  642. inp = tw->tw_inpcb;
  643. in_pcbref(inp);
  644. TW_RUNLOCK(V_tw_lock);
  645. INP_WLOCK(inp);
  646. tw = intotw(inp);
  647. if (in_pcbrele_wlocked(inp)) {
  648. if (__predict_true(tw == NULL)) {
  649. continue;
  650. } else {
  651. /* This should not happen as in TIMEWAIT
  652. * state the inp should not be destroyed
  653. * before its tcptw. If INVARIANTS is
  654. * defined panic.
  655. */
  656. #ifdef INVARIANTS
  657. panic("%s: Panic before an infinite "
  658. "loop: INP_TIMEWAIT && (INP_FREED "
  659. "|| inp last reference) && tw != "
  660. "NULL", __func__);
  661. #else
  662. log(LOG_ERR, "%s: Avoid an infinite "
  663. "loop: INP_TIMEWAIT && (INP_FREED "
  664. "|| inp last reference) && tw != "
  665. "NULL", __func__);
  666. #endif
  667. break;
  668. }
  669. }
  670. if (tw == NULL) {
  671. /* tcp_twclose() has already been called */
  672. INP_WUNLOCK(inp);
  673. continue;
  674. }
  675. tcp_twclose(tw, reuse);
  676. if (reuse)
  677. return tw;
  678. }
  679. return NULL;
  680. }