PageRenderTime 98ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/bsd/sys/netinet/tcp_subr.cc

https://gitlab.com/jforge/osv
C++ | 2284 lines | 1630 code | 214 blank | 440 comment | 345 complexity | 824f0618819a3e16373872efd1e2e6e2 MD5 | raw file
Possible License(s): BSD-3-Clause, 0BSD, MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. /*-
  2. * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. *
  29. * @(#)tcp_subr.c 8.2 (Berkeley) 5/24/95
  30. */
  31. #include <sys/cdefs.h>
  32. #include <bsd/porting/netport.h>
  33. #include <bsd/sys/sys/libkern.h>
  34. #include <bsd/sys/sys/param.h>
  35. #include <bsd/sys/sys/mbuf.h>
  36. #ifdef INET6
  37. #include <bsd/sys/sys/domain.h>
  38. #endif
  39. #include <bsd/sys/sys/priv.h>
  40. #include <bsd/sys/sys/socket.h>
  41. #include <bsd/sys/sys/socketvar.h>
  42. #include <bsd/sys/sys/protosw.h>
  43. #include <bsd/sys/net/route.h>
  44. #include <bsd/sys/net/if.h>
  45. #include <bsd/sys/net/vnet.h>
  46. #include <bsd/sys/netinet/cc.h>
  47. #include <bsd/sys/netinet/in.h>
  48. #include <bsd/sys/netinet/in_pcb.h>
  49. #include <bsd/sys/netinet/in_systm.h>
  50. #include <bsd/sys/netinet/in_var.h>
  51. #include <bsd/sys/netinet/ip.h>
  52. #include <bsd/sys/netinet/ip_icmp.h>
  53. #include <bsd/sys/netinet/ip_var.h>
  54. #ifdef INET6
  55. #include <bsd/sys/netinet/ip6.h>
  56. #include <bsd/sys/netinet6/in6_pcb.h>
  57. #include <bsd/sys/netinet6/ip6_var.h>
  58. #include <bsd/sys/netinet6/scope6_var.h>
  59. #include <bsd/sys/netinet6/nd6.h>
  60. #endif
  61. #include <bsd/sys/netinet/tcp_fsm.h>
  62. #include <bsd/sys/netinet/tcp_seq.h>
  63. #include <bsd/sys/netinet/tcp_timer.h>
  64. #include <bsd/sys/netinet/tcp_var.h>
  65. #include <bsd/sys/netinet/tcp_syncache.h>
  66. #ifdef INET6
  67. #include <bsd/sys/netinet6/tcp6_var.h>
  68. #endif
  69. #include <bsd/sys/netinet/tcpip.h>
  70. #ifdef TCPDEBUG
  71. #include <bsd/sys/netinet/tcp_debug.h>
  72. #endif
  73. #ifdef INET6
  74. #include <bsd/sys/netinet6/ip6protosw.h>
  75. #endif
  76. #ifdef IPSEC
  77. #include <netipsec/ipsec.h>
  78. #include <netipsec/xform.h>
  79. #ifdef INET6
  80. #include <netipsec/ipsec6.h>
  81. #endif
  82. #include <netipsec/key.h>
  83. #include <sys/syslog.h>
  84. #endif /*IPSEC*/
  85. #include <machine/in_cksum.h>
  86. #include <bsd/sys/sys/md5.h>
  87. #include <bsd/sys/net/routecache.hh>
  88. VNET_DEFINE(int, tcp_mssdflt) = TCP_MSS;
  89. #ifdef INET6
  90. VNET_DEFINE(int, tcp_v6mssdflt) = TCP6_MSS;
  91. #endif
  92. #if 0
  93. static int
  94. sysctl_net_inet_tcp_mss_check(SYSCTL_HANDLER_ARGS)
  95. {
  96. int error, new;
  97. new = V_tcp_mssdflt;
  98. error = sysctl_handle_int(oidp, &new, 0, req);
  99. if (error == 0 && req->newptr) {
  100. if (new < TCP_MINMSS)
  101. error = EINVAL;
  102. else
  103. V_tcp_mssdflt = new;
  104. }
  105. return (error);
  106. }
  107. SYSCTL_VNET_PROC(_net_inet_tcp, TCPCTL_MSSDFLT, mssdflt,
  108. CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(tcp_mssdflt), 0,
  109. &sysctl_net_inet_tcp_mss_check, "I",
  110. "Default TCP Maximum Segment Size");
  111. #endif
  112. #ifdef INET6
  113. static int
  114. sysctl_net_inet_tcp_mss_v6_check(SYSCTL_HANDLER_ARGS)
  115. {
  116. int error, new;
  117. new = V_tcp_v6mssdflt;
  118. error = sysctl_handle_int(oidp, &new, 0, req);
  119. if (error == 0 && req->newptr) {
  120. if (new < TCP_MINMSS)
  121. error = EINVAL;
  122. else
  123. V_tcp_v6mssdflt = new;
  124. }
  125. return (error);
  126. }
  127. SYSCTL_VNET_PROC(_net_inet_tcp, TCPCTL_V6MSSDFLT, v6mssdflt,
  128. CTLTYPE_INT|CTLFLAG_RW, &VNET_NAME(tcp_v6mssdflt), 0,
  129. &sysctl_net_inet_tcp_mss_v6_check, "I",
  130. "Default TCP Maximum Segment Size for IPv6");
  131. #endif /* INET6 */
  132. /*
  133. * Minimum MSS we accept and use. This prevents DoS attacks where
  134. * we are forced to a ridiculous low MSS like 20 and send hundreds
  135. * of packets instead of one. The effect scales with the available
  136. * bandwidth and quickly saturates the CPU and network interface
  137. * with packet generation and sending. Set to zero to disable MINMSS
  138. * checking. This setting prevents us from sending too small packets.
  139. */
  140. VNET_DEFINE(int, tcp_minmss) = TCP_MINMSS;
  141. SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, minmss, CTLFLAG_RW,
  142. &VNET_NAME(tcp_minmss), 0,
  143. "Minmum TCP Maximum Segment Size");
  144. VNET_DEFINE(int, tcp_do_rfc1323) = 1;
  145. SYSCTL_VNET_INT(_net_inet_tcp, TCPCTL_DO_RFC1323, rfc1323, CTLFLAG_RW,
  146. &VNET_NAME(tcp_do_rfc1323), 0,
  147. "Enable rfc1323 (high performance TCP) extensions");
  148. static int tcp_log_debug = 0;
  149. SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_debug, CTLFLAG_RW,
  150. &tcp_log_debug, 0, "Log errors caused by incoming TCP segments");
  151. static int tcp_tcbhashsize = 0;
  152. SYSCTL_INT(_net_inet_tcp, OID_AUTO, tcbhashsize, CTLFLAG_RDTUN,
  153. &tcp_tcbhashsize, 0, "Size of TCP control-block hashtable");
  154. static int do_tcpdrain = 1;
  155. SYSCTL_INT(_net_inet_tcp, OID_AUTO, do_tcpdrain, CTLFLAG_RW, &do_tcpdrain, 0,
  156. "Enable tcp_drain routine for extra help when low on mbufs");
  157. SYSCTL_VNET_UINT(_net_inet_tcp, OID_AUTO, pcbcount, CTLFLAG_RD,
  158. &VNET_NAME(tcbinfo.ipi_count), 0, "Number of active PCBs");
  159. static VNET_DEFINE(int, icmp_may_rst) = 1;
  160. #define V_icmp_may_rst VNET(icmp_may_rst)
  161. SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, icmp_may_rst, CTLFLAG_RW,
  162. &VNET_NAME(icmp_may_rst), 0,
  163. "Certain ICMP unreachable messages may abort connections in SYN_SENT");
  164. static VNET_DEFINE(int, tcp_isn_reseed_interval) = 0;
  165. #define V_tcp_isn_reseed_interval VNET(tcp_isn_reseed_interval)
  166. SYSCTL_VNET_INT(_net_inet_tcp, OID_AUTO, isn_reseed_interval, CTLFLAG_RW,
  167. &VNET_NAME(tcp_isn_reseed_interval), 0,
  168. "Seconds between reseeding of ISN secret");
  169. static int tcp_soreceive_stream = 0;
  170. SYSCTL_INT(_net_inet_tcp, OID_AUTO, soreceive_stream, CTLFLAG_RDTUN,
  171. &tcp_soreceive_stream, 0, "Using soreceive_stream for TCP sockets");
  172. #ifdef TCP_SIGNATURE
  173. static int tcp_sig_checksigs = 1;
  174. SYSCTL_INT(_net_inet_tcp, OID_AUTO, signature_verify_input, CTLFLAG_RW,
  175. &tcp_sig_checksigs, 0, "Verify RFC2385 digests on inbound traffic");
  176. #endif
  177. VNET_DEFINE(uma_zone_t, sack_hole_zone);
  178. #define V_sack_hole_zone VNET(sack_hole_zone)
  179. VNET_DEFINE(struct hhook_head *, tcp_hhh[HHOOK_TCP_LAST+1]);
  180. static struct inpcb *tcp_notify(struct inpcb *, int);
  181. static struct inpcb *tcp_mtudisc_notify(struct inpcb *, int);
  182. static char * tcp_log_addr(struct in_conninfo *inc, struct tcphdr *th,
  183. void *ip4hdr, const void *ip6hdr);
  184. /*
  185. * Target size of TCP PCB hash tables. Must be a power of two.
  186. *
  187. * Note that this can be overridden by the kernel environment
  188. * variable net.inet.tcp.tcbhashsize
  189. */
  190. #ifndef TCBHASHSIZE
  191. #define TCBHASHSIZE 4096
  192. #endif
  193. /*
  194. * XXX
  195. * Callouts should be moved into struct tcp directly. They are currently
  196. * separate because the tcpcb structure is exported to userland for sysctl
  197. * parsing purposes, which do not know about callouts.
  198. */
  199. struct tcpcb_mem {
  200. struct tcpcb tcb;
  201. struct tcp_timer tt;
  202. struct cc_var ccv;
  203. };
  204. static VNET_DEFINE(uma_zone_t, tcpcb_zone);
  205. #define V_tcpcb_zone VNET(tcpcb_zone)
  206. MALLOC_DEFINE(M_TCPLOG, "tcplog", "TCP address and flags print buffers");
  207. static struct mtx isn_mtx;
  208. #define ISN_LOCK_INIT() mtx_init(&isn_mtx, "isn_mtx", NULL, MTX_DEF)
  209. #define ISN_LOCK() mtx_lock(&isn_mtx)
  210. #define ISN_UNLOCK() mtx_unlock(&isn_mtx)
  211. /*
  212. * TCP initialization.
  213. */
  214. static void
  215. tcp_zone_change(void *tag)
  216. {
  217. // FIXME: uma_zone_set_max(V_tcbinfo.ipi_zone, maxsockets);
  218. uma_zone_set_max(V_tcpcb_zone, maxsockets);
  219. tcp_tw_zone_change();
  220. }
  221. void
  222. tcp_init(void)
  223. {
  224. int hashsize;
  225. hashsize = TCBHASHSIZE;
  226. TUNABLE_INT_FETCH("net.inet.tcp.tcbhashsize", &hashsize);
  227. if (!powerof2(hashsize)) {
  228. printf("WARNING: TCB hash size not a power of 2\n");
  229. hashsize = 512; /* safe default */
  230. }
  231. in_pcbinfo_init(&V_tcbinfo, "tcp", &V_tcb, hashsize, hashsize,
  232. IPI_HASHFIELDS_4TUPLE);
  233. /*
  234. * These have to be type stable for the benefit of the timers.
  235. */
  236. V_tcpcb_zone = uma_zcreate("tcpcb", sizeof(struct tcpcb_mem),
  237. NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  238. uma_zone_set_max(V_tcpcb_zone, maxsockets);
  239. tcp_tw_init();
  240. syncache_init();
  241. tcp_hc_init();
  242. tcp_reass_init();
  243. TUNABLE_INT_FETCH("net.inet.tcp.sack.enable", &V_tcp_do_sack);
  244. V_sack_hole_zone = uma_zcreate("sackhole", sizeof(struct sackhole),
  245. NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  246. /* Skip initialization of globals for non-default instances. */
  247. if (!IS_DEFAULT_VNET(curvnet))
  248. return;
  249. /* XXX virtualize those below? */
  250. tcp_delacktime = TCPTV_DELACK;
  251. tcp_keepinit = TCPTV_KEEP_INIT;
  252. tcp_keepidle = (int)TCPTV_KEEP_IDLE;
  253. tcp_keepintvl = TCPTV_KEEPINTVL;
  254. tcp_maxpersistidle = (int)TCPTV_KEEP_IDLE;
  255. tcp_msl = TCPTV_MSL;
  256. tcp_rexmit_min = TCPTV_MIN;
  257. if (tcp_rexmit_min < 1)
  258. tcp_rexmit_min = 1;
  259. tcp_rexmit_slop = TCPTV_CPU_VAR;
  260. tcp_finwait2_timeout = TCPTV_FINWAIT2_TIMEOUT;
  261. tcp_tcbhashsize = hashsize;
  262. TUNABLE_INT_FETCH("net.inet.tcp.soreceive_stream", &tcp_soreceive_stream);
  263. if (tcp_soreceive_stream) {
  264. #ifdef INET
  265. tcp_usrreqs.pru_soreceive = soreceive_stream;
  266. #endif
  267. #ifdef INET6
  268. tcp6_usrreqs.pru_soreceive = soreceive_stream;
  269. #endif /* INET6 */
  270. }
  271. #ifdef INET6
  272. #define TCP_MINPROTOHDR (sizeof(struct ip6_hdr) + sizeof(struct tcphdr))
  273. #else /* INET6 */
  274. #define TCP_MINPROTOHDR (sizeof(struct tcpiphdr))
  275. #endif /* INET6 */
  276. if (max_protohdr < TCP_MINPROTOHDR)
  277. max_protohdr = TCP_MINPROTOHDR;
  278. if (max_linkhdr + TCP_MINPROTOHDR > MHLEN)
  279. panic("tcp_init");
  280. #undef TCP_MINPROTOHDR
  281. ISN_LOCK_INIT();
  282. /* FIXME: OSv - tcp shutdown event */
  283. #if 0
  284. EVENTHANDLER_REGISTER(shutdown_pre_sync, tcp_fini, NULL,
  285. SHUTDOWN_PRI_DEFAULT);
  286. #endif
  287. EVENTHANDLER_REGISTER(maxsockets_change, (void *)tcp_zone_change, NULL,
  288. EVENTHANDLER_PRI_ANY);
  289. }
  290. #ifdef VIMAGE
  291. void
  292. tcp_destroy(void)
  293. {
  294. tcp_reass_destroy();
  295. tcp_hc_destroy();
  296. syncache_destroy();
  297. tcp_tw_destroy();
  298. in_pcbinfo_destroy(&V_tcbinfo);
  299. uma_zdestroy(V_sack_hole_zone);
  300. uma_zdestroy(V_tcpcb_zone);
  301. }
  302. #endif
  303. void
  304. tcp_fini(void *xtp)
  305. {
  306. }
  307. /*
  308. * Fill in the IP and TCP headers for an outgoing packet, given the tcpcb.
  309. * tcp_template used to store this data in mbufs, but we now recopy it out
  310. * of the tcpcb each time to conserve mbufs.
  311. */
  312. void
  313. tcpip_fillheaders(struct inpcb *inp, void *ip_ptr, void *tcp_ptr)
  314. {
  315. struct tcphdr *th = (struct tcphdr *)tcp_ptr;
  316. INP_LOCK_ASSERT(inp);
  317. #ifdef INET6
  318. if ((inp->inp_vflag & INP_IPV6) != 0) {
  319. struct ip6_hdr *ip6;
  320. ip6 = (struct ip6_hdr *)ip_ptr;
  321. ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
  322. (inp->inp_flow & IPV6_FLOWINFO_MASK);
  323. ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
  324. (IPV6_VERSION & IPV6_VERSION_MASK);
  325. ip6->ip6_nxt = IPPROTO_TCP;
  326. ip6->ip6_plen = htons(sizeof(struct tcphdr));
  327. ip6->ip6_src = inp->in6p_laddr;
  328. ip6->ip6_dst = inp->in6p_faddr;
  329. }
  330. #endif /* INET6 */
  331. #if defined(INET6) && defined(INET)
  332. else
  333. #endif
  334. #ifdef INET
  335. {
  336. struct ip *ip;
  337. ip = (struct ip *)ip_ptr;
  338. ip->ip_v = IPVERSION;
  339. ip->ip_hl = 5;
  340. ip->ip_tos = inp->inp_ip_tos;
  341. ip->ip_len = 0;
  342. ip->ip_id = 0;
  343. ip->ip_off = 0;
  344. ip->ip_ttl = inp->inp_ip_ttl;
  345. ip->ip_sum = 0;
  346. ip->ip_p = IPPROTO_TCP;
  347. ip->ip_src = inp->inp_laddr;
  348. ip->ip_dst = inp->inp_faddr;
  349. }
  350. #endif /* INET */
  351. th->th_sport = inp->inp_lport;
  352. th->th_dport = inp->inp_fport;
  353. th->th_seq = tcp_seq(0);
  354. th->th_ack = tcp_seq(0);
  355. th->th_x2 = 0;
  356. th->th_off = 5;
  357. th->th_flags = 0;
  358. th->th_win = 0;
  359. th->th_urp = 0;
  360. th->th_sum = 0; /* in_pseudo() is called later for ipv4 */
  361. }
  362. /*
  363. * Create template to be used to send tcp packets on a connection.
  364. * Allocates an mbuf and fills in a skeletal tcp/ip header. The only
  365. * use for this function is in keepalives, which use tcp_respond.
  366. */
  367. struct tcptemp *
  368. tcpip_maketemplate(struct inpcb *inp)
  369. {
  370. struct tcptemp *t;
  371. t = (tcptemp *)malloc(sizeof(*t));
  372. if (t == NULL)
  373. return (NULL);
  374. tcpip_fillheaders(inp, (void *)&t->tt_ipgen, (void *)&t->tt_t);
  375. return (t);
  376. }
  377. /*
  378. * Send a single message to the TCP at address specified by
  379. * the given TCP/IP header. If m == NULL, then we make a copy
  380. * of the tcpiphdr at ti and send directly to the addressed host.
  381. * This is used to force keep alive messages out using the TCP
  382. * template for a connection. If flags are given then we send
  383. * a message back to the TCP which originated the * segment ti,
  384. * and discard the mbuf containing it and any other attached mbufs.
  385. *
  386. * In any case the ack and sequence number of the transmitted
  387. * segment are as specified by the parameters.
  388. *
  389. * NOTE: If m != NULL, then ti must point to *inside* the mbuf.
  390. */
  391. void
  392. tcp_respond(struct tcpcb *tp, void *ipgen, struct tcphdr *th, struct mbuf *m,
  393. tcp_seq ack, tcp_seq seq, int flags)
  394. {
  395. int tlen;
  396. int win = 0;
  397. struct ip *ip;
  398. struct tcphdr *nth;
  399. #ifdef INET6
  400. struct ip6_hdr *ip6;
  401. int isipv6;
  402. #endif /* INET6 */
  403. int ipflags = 0;
  404. struct inpcb *inp;
  405. KASSERT(tp != NULL || m != NULL, ("tcp_respond: tp and m both NULL"));
  406. #ifdef INET6
  407. isipv6 = ((struct ip *)ipgen)->ip_v == (IPV6_VERSION >> 4);
  408. ip6 = ipgen;
  409. #endif /* INET6 */
  410. ip = (struct ip *)ipgen;
  411. if (tp != NULL) {
  412. inp = tp->t_inpcb;
  413. KASSERT(inp != NULL, ("tcp control block w/o inpcb"));
  414. INP_LOCK_ASSERT(inp);
  415. } else
  416. inp = NULL;
  417. if (tp != NULL) {
  418. if (!(flags & TH_RST)) {
  419. win = sbspace(&inp->inp_socket->so_rcv);
  420. if (win > (long)TCP_MAXWIN << tp->rcv_scale)
  421. win = (long)TCP_MAXWIN << tp->rcv_scale;
  422. }
  423. }
  424. if (m == NULL) {
  425. m = m_gethdr(M_DONTWAIT, MT_DATA);
  426. if (m == NULL)
  427. return;
  428. tlen = 0;
  429. m->m_hdr.mh_data += max_linkhdr;
  430. #ifdef INET6
  431. if (isipv6) {
  432. bcopy((caddr_t)ip6, mtod(m, caddr_t),
  433. sizeof(struct ip6_hdr));
  434. ip6 = mtod(m, struct ip6_hdr *);
  435. nth = (struct tcphdr *)(ip6 + 1);
  436. } else
  437. #endif /* INET6 */
  438. {
  439. bcopy((caddr_t)ip, mtod(m, caddr_t), sizeof(struct ip));
  440. ip = mtod(m, struct ip *);
  441. nth = (struct tcphdr *)(ip + 1);
  442. }
  443. bcopy((caddr_t)th, (caddr_t)nth, sizeof(struct tcphdr));
  444. flags = TH_ACK;
  445. } else {
  446. /*
  447. * reuse the mbuf.
  448. * XXX MRT We inherrit the FIB, which is lucky.
  449. */
  450. m_freem(m->m_hdr.mh_next);
  451. m->m_hdr.mh_next = NULL;
  452. m->m_hdr.mh_data = (caddr_t)ipgen;
  453. m_addr_changed(m);
  454. /* m_hdr.mh_len is set later */
  455. tlen = 0;
  456. #define xchg(a,b,type) { type t; t=a; a=b; b=t; }
  457. #ifdef INET6
  458. if (isipv6) {
  459. xchg(ip6->ip6_dst, ip6->ip6_src, struct in6_addr);
  460. nth = (struct tcphdr *)(ip6 + 1);
  461. } else
  462. #endif /* INET6 */
  463. {
  464. xchg(ip->ip_dst.s_addr, ip->ip_src.s_addr, uint32_t);
  465. nth = (struct tcphdr *)(ip + 1);
  466. }
  467. if (th != nth) {
  468. /*
  469. * this is usually a case when an extension header
  470. * exists between the IPv6 header and the
  471. * TCP header.
  472. */
  473. nth->th_sport = th->th_sport;
  474. nth->th_dport = th->th_dport;
  475. }
  476. xchg(nth->th_dport, nth->th_sport, uint16_t);
  477. #undef xchg
  478. }
  479. #ifdef INET6
  480. if (isipv6) {
  481. ip6->ip6_flow = 0;
  482. ip6->ip6_vfc = IPV6_VERSION;
  483. ip6->ip6_nxt = IPPROTO_TCP;
  484. ip6->ip6_plen = 0; /* Set in ip6_output(). */
  485. tlen += sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
  486. }
  487. #endif
  488. #if defined(INET) && defined(INET6)
  489. else
  490. #endif
  491. #ifdef INET
  492. {
  493. tlen += sizeof (struct tcpiphdr);
  494. ip->ip_len = tlen;
  495. ip->ip_ttl = V_ip_defttl;
  496. if (V_path_mtu_discovery)
  497. ip->ip_off |= IP_DF;
  498. }
  499. #endif
  500. m->m_hdr.mh_len = tlen;
  501. m->M_dat.MH.MH_pkthdr.len = tlen;
  502. m->M_dat.MH.MH_pkthdr.rcvif = NULL;
  503. #ifdef MAC
  504. if (inp != NULL) {
  505. /*
  506. * Packet is associated with a socket, so allow the
  507. * label of the response to reflect the socket label.
  508. */
  509. INP_LOCK_ASSERT(inp);
  510. mac_inpcb_create_mbuf(inp, m);
  511. } else {
  512. /*
  513. * Packet is not associated with a socket, so possibly
  514. * update the label in place.
  515. */
  516. mac_netinet_tcp_reply(m);
  517. }
  518. #endif
  519. nth->th_seq = htonl(seq);
  520. nth->th_ack = htonl(ack);
  521. nth->th_x2 = 0;
  522. nth->th_off = sizeof (struct tcphdr) >> 2;
  523. nth->th_flags = flags;
  524. if (tp != NULL)
  525. nth->th_win = htons((u_short) (win >> tp->rcv_scale));
  526. else
  527. nth->th_win = htons((u_short)win);
  528. nth->th_urp = 0;
  529. m->M_dat.MH.MH_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
  530. #ifdef INET6
  531. if (isipv6) {
  532. m->M_dat.MH.MH_pkthdr.csum_flags = CSUM_TCP_IPV6;
  533. nth->th_sum = in6_cksum_pseudo(ip6,
  534. tlen - sizeof(struct ip6_hdr), IPPROTO_TCP, 0);
  535. ip6->ip6_hlim = in6_selecthlim(tp != NULL ? tp->t_inpcb :
  536. NULL, NULL);
  537. }
  538. #endif /* INET6 */
  539. #if defined(INET6) && defined(INET)
  540. else
  541. #endif
  542. #ifdef INET
  543. {
  544. m->M_dat.MH.MH_pkthdr.csum_flags = CSUM_TCP;
  545. nth->th_sum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr,
  546. htons((u_short)(tlen - sizeof(struct ip) + ip->ip_p)));
  547. }
  548. #endif /* INET */
  549. #ifdef TCPDEBUG
  550. if (tp == NULL || (inp->inp_socket->so_options & SO_DEBUG))
  551. tcp_trace(TA_OUTPUT, 0, tp, mtod(m, void *), th, 0);
  552. #endif
  553. #ifdef INET6
  554. if (isipv6)
  555. (void) ip6_output(m, NULL, NULL, ipflags, NULL, NULL, inp);
  556. #endif /* INET6 */
  557. #if defined(INET) && defined(INET6)
  558. else
  559. #endif
  560. #ifdef INET
  561. (void) ip_output(m, NULL, NULL, ipflags, NULL, inp);
  562. #endif
  563. }
  564. /*
  565. * Create a new TCP control block, making an
  566. * empty reassembly queue and hooking it to the argument
  567. * protocol control block. The `inp' parameter must have
  568. * come from the zone allocator set up in tcp_init().
  569. */
  570. struct tcpcb *
  571. tcp_newtcpcb(struct inpcb *inp)
  572. {
  573. struct tcpcb_mem *tm;
  574. struct tcpcb *tp;
  575. #ifdef INET6
  576. int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
  577. #endif /* INET6 */
  578. tm = (tcpcb_mem *)uma_zalloc(V_tcpcb_zone, M_NOWAIT | M_ZERO);
  579. if (tm == NULL)
  580. return (NULL);
  581. tp = &tm->tcb;
  582. /* Initialise cc_var struct for this tcpcb. */
  583. tp->ccv = &tm->ccv;
  584. tp->ccv->type = IPPROTO_TCP;
  585. tp->ccv->ccvc.tcp = tp;
  586. /*
  587. * Use the current system default CC algorithm.
  588. */
  589. CC_LIST_RLOCK();
  590. KASSERT(!STAILQ_EMPTY(&cc_list), ("cc_list is empty!"));
  591. CC_ALGO(tp) = CC_DEFAULT();
  592. CC_LIST_RUNLOCK();
  593. if (CC_ALGO(tp)->cb_init != NULL)
  594. if (CC_ALGO(tp)->cb_init(tp->ccv) > 0) {
  595. uma_zfree(V_tcpcb_zone, tm);
  596. return (NULL);
  597. }
  598. #ifdef VIMAGE
  599. tp->t_vnet = inp->inp_vnet;
  600. #endif
  601. tp->t_timers = &tm->tt;
  602. /* LIST_INIT(&tp->t_segq); */ /* XXX covered by M_ZERO */
  603. tp->t_maxseg = tp->t_maxopd =
  604. #ifdef INET6
  605. isipv6 ? V_tcp_v6mssdflt :
  606. #endif /* INET6 */
  607. V_tcp_mssdflt;
  608. init_timers(tp->t_timers, tp, inp);
  609. if (V_tcp_do_rfc1323)
  610. tp->t_flags = (TF_REQ_SCALE|TF_REQ_TSTMP);
  611. if (V_tcp_do_sack)
  612. tp->t_flags |= TF_SACK_PERMIT;
  613. TAILQ_INIT(&tp->snd_holes);
  614. tp->t_inpcb = inp; /* XXX */
  615. /*
  616. * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no
  617. * rtt estimate. Set rttvar so that srtt + 4 * rttvar gives
  618. * reasonable initial retransmit time.
  619. */
  620. tp->t_srtt = TCPTV_SRTTBASE;
  621. tp->t_rttvar = ((TCPTV_RTOBASE - TCPTV_SRTTBASE) << TCP_RTTVAR_SHIFT) / 4;
  622. tp->t_rttmin = tcp_rexmit_min;
  623. tp->t_rxtcur = TCPTV_RTOBASE;
  624. tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT;
  625. tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT;
  626. tp->t_rcvtime = bsd_ticks;
  627. /*
  628. * IPv4 TTL initialization is necessary for an IPv6 socket as well,
  629. * because the socket may be bound to an IPv6 wildcard address,
  630. * which may match an IPv4-mapped IPv6 address.
  631. */
  632. inp->inp_ip_ttl = V_ip_defttl;
  633. inp->inp_ppcb = tp;
  634. return (tp); /* XXX */
  635. }
  636. /*
  637. * Switch the congestion control algorithm back to NewReno for any active
  638. * control blocks using an algorithm which is about to go away.
  639. * This ensures the CC framework can allow the unload to proceed without leaving
  640. * any dangling pointers which would trigger a panic.
  641. * Returning non-zero would inform the CC framework that something went wrong
  642. * and it would be unsafe to allow the unload to proceed. However, there is no
  643. * way for this to occur with this implementation so we always return zero.
  644. */
  645. int
  646. tcp_ccalgounload(struct cc_algo *unload_algo)
  647. {
  648. struct cc_algo *tmpalgo;
  649. struct inpcb *inp;
  650. struct tcpcb *tp;
  651. VNET_ITERATOR_DECL(vnet_iter);
  652. /*
  653. * Check all active control blocks across all network stacks and change
  654. * any that are using "unload_algo" back to NewReno. If "unload_algo"
  655. * requires cleanup code to be run, call it.
  656. */
  657. VNET_LIST_RLOCK();
  658. VNET_FOREACH(vnet_iter) {
  659. CURVNET_SET(vnet_iter);
  660. INP_INFO_WLOCK(&V_tcbinfo);
  661. /*
  662. * New connections already part way through being initialised
  663. * with the CC algo we're removing will not race with this code
  664. * because the INP_INFO_WLOCK is held during initialisation. We
  665. * therefore don't enter the loop below until the connection
  666. * list has stabilised.
  667. */
  668. LIST_FOREACH(inp, &V_tcb, inp_list) {
  669. INP_LOCK(inp);
  670. /* Important to skip tcptw structs. */
  671. if (!(inp->inp_flags & INP_TIMEWAIT) &&
  672. (tp = intotcpcb(inp)) != NULL) {
  673. /*
  674. * By holding INP_LOCK here, we are assured
  675. * that the connection is not currently
  676. * executing inside the CC module's functions
  677. * i.e. it is safe to make the switch back to
  678. * NewReno.
  679. */
  680. if (CC_ALGO(tp) == unload_algo) {
  681. tmpalgo = CC_ALGO(tp);
  682. /* NewReno does not require any init. */
  683. CC_ALGO(tp) = &newreno_cc_algo;
  684. if (tmpalgo->cb_destroy != NULL)
  685. tmpalgo->cb_destroy(tp->ccv);
  686. }
  687. }
  688. INP_UNLOCK(inp);
  689. }
  690. INP_INFO_WUNLOCK(&V_tcbinfo);
  691. CURVNET_RESTORE();
  692. }
  693. VNET_LIST_RUNLOCK();
  694. return (0);
  695. }
  696. /*
  697. * Drop a TCP connection, reporting
  698. * the specified error. If connection is synchronized,
  699. * then send a RST to peer.
  700. */
  701. void tcp_drop_noclose(struct tcpcb *tp, int errval)
  702. {
  703. struct socket *so = tp->t_inpcb->inp_socket;
  704. INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
  705. INP_LOCK_ASSERT(tp->t_inpcb);
  706. if (TCPS_HAVERCVDSYN(tp->get_state())) {
  707. tp->set_state(TCPS_CLOSED);
  708. (void) tcp_output(tp);
  709. TCPSTAT_INC(tcps_drops);
  710. } else
  711. TCPSTAT_INC(tcps_conndrops);
  712. if (errval == ETIMEDOUT && tp->t_softerror)
  713. errval = tp->t_softerror;
  714. so->so_error = errval;
  715. }
  716. struct tcpcb *
  717. tcp_drop(struct tcpcb *tp, int errval)
  718. {
  719. tcp_drop_noclose(tp, errval);
  720. return tcp_close(tp);
  721. }
  722. void
  723. tcp_discardcb(struct tcpcb *tp)
  724. {
  725. struct inpcb *inp = tp->t_inpcb;
  726. struct socket *so = inp->inp_socket;
  727. #ifdef INET6
  728. int isipv6 = (inp->inp_vflag & INP_IPV6) != 0;
  729. #endif /* INET6 */
  730. INP_LOCK_ASSERT(inp);
  731. for (auto& t : *tp->t_timers) {
  732. t->cancel();
  733. }
  734. /*
  735. * If we got enough samples through the srtt filter,
  736. * save the rtt and rttvar in the routing entry.
  737. * 'Enough' is arbitrarily defined as 4 rtt samples.
  738. * 4 samples is enough for the srtt filter to converge
  739. * to within enough % of the correct value; fewer samples
  740. * and we could save a bogus rtt. The danger is not high
  741. * as tcp quickly recovers from everything.
  742. * XXX: Works very well but needs some more statistics!
  743. */
  744. if (tp->t_rttupdated >= 4) {
  745. struct hc_metrics_lite metrics;
  746. u_long ssthresh;
  747. bzero(&metrics, sizeof(metrics));
  748. /*
  749. * Update the ssthresh always when the conditions below
  750. * are satisfied. This gives us better new start value
  751. * for the congestion avoidance for new connections.
  752. * ssthresh is only set if packet loss occured on a session.
  753. *
  754. * XXXRW: 'so' may be NULL here, and/or socket buffer may be
  755. * being torn down. Ideally this code would not use 'so'.
  756. */
  757. ssthresh = tp->snd_ssthresh;
  758. if (ssthresh != 0 && ssthresh < so->so_snd.sb_hiwat / 2) {
  759. /*
  760. * convert the limit from user data bytes to
  761. * packets then to packet data bytes.
  762. */
  763. ssthresh = (ssthresh + tp->t_maxseg / 2) / tp->t_maxseg;
  764. if (ssthresh < 2)
  765. ssthresh = 2;
  766. ssthresh *= (u_long)(tp->t_maxseg +
  767. #ifdef INET6
  768. (isipv6 ? sizeof (struct ip6_hdr) +
  769. sizeof (struct tcphdr) :
  770. #endif
  771. sizeof (struct tcpiphdr)
  772. #ifdef INET6
  773. )
  774. #endif
  775. );
  776. } else
  777. ssthresh = 0;
  778. metrics.rmx_ssthresh = ssthresh;
  779. metrics.rmx_rtt = tp->t_srtt;
  780. metrics.rmx_rttvar = tp->t_rttvar;
  781. metrics.rmx_cwnd = tp->snd_cwnd;
  782. metrics.rmx_sendpipe = 0;
  783. metrics.rmx_recvpipe = 0;
  784. tcp_hc_update(&inp->inp_inc, &metrics);
  785. }
  786. /* free the reassembly queue, if any */
  787. tcp_reass_flush(tp);
  788. tcp_free_sackholes(tp);
  789. tcp_free_net_channel(tp);
  790. /* Allow the CC algorithm to clean up after itself. */
  791. if (CC_ALGO(tp)->cb_destroy != NULL)
  792. CC_ALGO(tp)->cb_destroy(tp->ccv);
  793. CC_ALGO(tp) = NULL;
  794. inp->inp_ppcb = NULL;
  795. /*
  796. * We must prevent inpcb from being freed until all
  797. * timers are stopped because they try to take a lock on it.
  798. */
  799. in_pcbref(tp->t_inpcb);
  800. async::run_later([inp, tp] {
  801. INP_LOCK(inp);
  802. for (auto& t : *tp->t_timers) {
  803. t->cancel_sync();
  804. delete t;
  805. }
  806. if (!in_pcbrele_locked(inp)) {
  807. INP_UNLOCK(inp);
  808. }
  809. tp->t_inpcb = NULL;
  810. uma_zfree(V_tcpcb_zone, tp);
  811. });
  812. }
  813. /*
  814. * Attempt to close a TCP control block, marking it as dropped, and freeing
  815. * the socket if we hold the only reference.
  816. */
  817. struct tcpcb *
  818. tcp_close(struct tcpcb *tp)
  819. {
  820. struct inpcb *inp = tp->t_inpcb;
  821. struct socket *so;
  822. INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
  823. INP_LOCK_ASSERT(inp);
  824. in_pcbdrop(inp);
  825. TCPSTAT_INC(tcps_closed);
  826. KASSERT(inp->inp_socket != NULL, ("tcp_close: inp_socket NULL"));
  827. so = inp->inp_socket;
  828. soisdisconnected(so);
  829. if (inp->inp_flags & INP_SOCKREF) {
  830. KASSERT(so->so_state & SS_PROTOREF,
  831. ("tcp_close: !SS_PROTOREF"));
  832. inp->inp_flags &= ~INP_SOCKREF;
  833. INP_UNLOCK(inp);
  834. ACCEPT_LOCK();
  835. SOCK_LOCK(so);
  836. so->so_state &= ~SS_PROTOREF;
  837. sofree(so);
  838. return (NULL);
  839. }
  840. return (tp);
  841. }
  842. void
  843. tcp_drain(void)
  844. {
  845. VNET_ITERATOR_DECL(vnet_iter);
  846. if (!do_tcpdrain)
  847. return;
  848. VNET_LIST_RLOCK_NOSLEEP();
  849. VNET_FOREACH(vnet_iter) {
  850. CURVNET_SET(vnet_iter);
  851. struct inpcb *inpb;
  852. struct tcpcb *tcpb;
  853. /*
  854. * Walk the tcpbs, if existing, and flush the reassembly queue,
  855. * if there is one...
  856. * XXX: The "Net/3" implementation doesn't imply that the TCP
  857. * reassembly queue should be flushed, but in a situation
  858. * where we're really low on mbufs, this is potentially
  859. * usefull.
  860. */
  861. INP_INFO_WLOCK(&V_tcbinfo);
  862. LIST_FOREACH(inpb, V_tcbinfo.ipi_listhead, inp_list) {
  863. if (inpb->inp_flags & INP_TIMEWAIT)
  864. continue;
  865. INP_LOCK(inpb);
  866. if ((tcpb = intotcpcb(inpb)) != NULL) {
  867. tcp_reass_flush(tcpb);
  868. tcp_clean_sackreport(tcpb);
  869. }
  870. INP_UNLOCK(inpb);
  871. }
  872. INP_INFO_WUNLOCK(&V_tcbinfo);
  873. CURVNET_RESTORE();
  874. }
  875. VNET_LIST_RUNLOCK_NOSLEEP();
  876. }
  877. /*
  878. * Notify a tcp user of an asynchronous error;
  879. * store error as soft error, but wake up user
  880. * (for now, won't do anything until can select for soft error).
  881. *
  882. * Do not wake up user since there currently is no mechanism for
  883. * reporting soft errors (yet - a kqueue filter may be added).
  884. */
  885. static struct inpcb *
  886. tcp_notify(struct inpcb *inp, int error)
  887. {
  888. struct tcpcb *tp;
  889. INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
  890. INP_LOCK_ASSERT(inp);
  891. if ((inp->inp_flags & INP_TIMEWAIT) ||
  892. (inp->inp_flags & INP_DROPPED))
  893. return (inp);
  894. tp = intotcpcb(inp);
  895. KASSERT(tp != NULL, ("tcp_notify: tp == NULL"));
  896. /*
  897. * Ignore some errors if we are hooked up.
  898. * If connection hasn't completed, has retransmitted several times,
  899. * and receives a second error, give up now. This is better
  900. * than waiting a long time to establish a connection that
  901. * can never complete.
  902. */
  903. if (tp->get_state() == TCPS_ESTABLISHED &&
  904. (error == EHOSTUNREACH || error == ENETUNREACH ||
  905. error == EHOSTDOWN)) {
  906. return (inp);
  907. } else if (tp->get_state() < TCPS_ESTABLISHED && tp->t_rxtshift > 3 &&
  908. tp->t_softerror) {
  909. tp = tcp_drop(tp, error);
  910. if (tp != NULL)
  911. return (inp);
  912. else
  913. return (NULL);
  914. } else {
  915. tp->t_softerror = error;
  916. return (inp);
  917. }
  918. #if 0
  919. wakeup( &so->so_timeo);
  920. sorwakeup(so);
  921. sowwakeup(so);
  922. #endif
  923. }
  924. #if 0
  925. static int
  926. tcp_pcblist(SYSCTL_HANDLER_ARGS)
  927. {
  928. int error, i, m, n, pcb_count;
  929. struct inpcb *inp, **inp_list;
  930. inp_gen_t gencnt;
  931. struct xinpgen xig;
  932. /*
  933. * The process of preparing the TCB list is too time-consuming and
  934. * resource-intensive to repeat twice on every request.
  935. */
  936. if (req->oldptr == NULL) {
  937. n = V_tcbinfo.ipi_count + syncache_pcbcount();
  938. n += imax(n / 8, 10);
  939. req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xtcpcb);
  940. return (0);
  941. }
  942. if (req->newptr != NULL)
  943. return (EPERM);
  944. /*
  945. * OK, now we're committed to doing something.
  946. */
  947. INP_INFO_RLOCK(&V_tcbinfo);
  948. gencnt = V_tcbinfo.ipi_gencnt;
  949. n = V_tcbinfo.ipi_count;
  950. INP_INFO_RUNLOCK(&V_tcbinfo);
  951. m = syncache_pcbcount();
  952. error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
  953. + (n + m) * sizeof(struct xtcpcb));
  954. if (error != 0)
  955. return (error);
  956. xig.xig_len = sizeof xig;
  957. xig.xig_count = n + m;
  958. xig.xig_gen = gencnt;
  959. xig.xig_sogen = so_gencnt;
  960. error = SYSCTL_OUT(req, &xig, sizeof xig);
  961. if (error)
  962. return (error);
  963. error = syncache_pcblist(req, m, &pcb_count);
  964. if (error)
  965. return (error);
  966. inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  967. if (inp_list == NULL)
  968. return (ENOMEM);
  969. INP_INFO_RLOCK(&V_tcbinfo);
  970. for (inp = LIST_FIRST(V_tcbinfo.ipi_listhead), i = 0;
  971. inp != NULL && i < n; inp = LIST_NEXT(inp, inp_list)) {
  972. INP_LOCK(inp);
  973. if (inp->inp_gencnt <= gencnt) {
  974. /*
  975. * XXX: This use of cr_cansee(), introduced with
  976. * TCP state changes, is not quite right, but for
  977. * now, better than nothing.
  978. */
  979. if (inp->inp_flags & INP_TIMEWAIT) {
  980. if (intotw(inp) != NULL)
  981. error = cr_cansee(req->td->td_ucred,
  982. intotw(inp)->tw_cred);
  983. else
  984. error = EINVAL; /* Skip this inp. */
  985. } else
  986. error = cr_canseeinpcb(req->td->td_ucred, inp);
  987. if (error == 0) {
  988. in_pcbref(inp);
  989. inp_list[i++] = inp;
  990. }
  991. }
  992. INP_UNLOCK(inp);
  993. }
  994. INP_INFO_RUNLOCK(&V_tcbinfo);
  995. n = i;
  996. error = 0;
  997. for (i = 0; i < n; i++) {
  998. inp = inp_list[i];
  999. INP_LOCK(inp);
  1000. if (inp->inp_gencnt <= gencnt) {
  1001. struct xtcpcb xt;
  1002. void *inp_ppcb;
  1003. bzero(&xt, sizeof(xt));
  1004. xt.xt_len = sizeof xt;
  1005. /* XXX should avoid extra copy */
  1006. bcopy(inp, &xt.xt_inp, sizeof *inp);
  1007. inp_ppcb = inp->inp_ppcb;
  1008. if (inp_ppcb == NULL)
  1009. bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
  1010. else if (inp->inp_flags & INP_TIMEWAIT) {
  1011. bzero((char *) &xt.xt_tp, sizeof xt.xt_tp);
  1012. xt.xt_tp.t_state = TCPS_TIME_WAIT;
  1013. } else {
  1014. bcopy(inp_ppcb, &xt.xt_tp, sizeof xt.xt_tp);
  1015. if (xt.xt_tp.t_timers)
  1016. tcp_timer_to_xtimer(&xt.xt_tp, xt.xt_tp.t_timers, &xt.xt_timer);
  1017. }
  1018. if (inp->inp_socket != NULL)
  1019. sotoxsocket(inp->inp_socket, &xt.xt_socket);
  1020. else {
  1021. bzero(&xt.xt_socket, sizeof xt.xt_socket);
  1022. xt.xt_socket.xso_protocol = IPPROTO_TCP;
  1023. }
  1024. xt.xt_inp.inp_gencnt = inp->inp_gencnt;
  1025. INP_UNLOCK(inp);
  1026. error = SYSCTL_OUT(req, &xt, sizeof xt);
  1027. } else
  1028. INP_UNLOCK(inp);
  1029. }
  1030. INP_INFO_WLOCK(&V_tcbinfo);
  1031. for (i = 0; i < n; i++) {
  1032. inp = inp_list[i];
  1033. INP_LOCK(inp);
  1034. if (!in_pcbrele_locked(inp))
  1035. INP_UNLOCK(inp);
  1036. }
  1037. INP_INFO_WUNLOCK(&V_tcbinfo);
  1038. if (!error) {
  1039. /*
  1040. * Give the user an updated idea of our state.
  1041. * If the generation differs from what we told
  1042. * her before, she knows that something happened
  1043. * while we were processing this request, and it
  1044. * might be necessary to retry.
  1045. */
  1046. INP_INFO_RLOCK(&V_tcbinfo);
  1047. xig.xig_gen = V_tcbinfo.ipi_gencnt;
  1048. xig.xig_sogen = so_gencnt;
  1049. xig.xig_count = V_tcbinfo.ipi_count + pcb_count;
  1050. INP_INFO_RUNLOCK(&V_tcbinfo);
  1051. error = SYSCTL_OUT(req, &xig, sizeof xig);
  1052. }
  1053. free(inp_list, M_TEMP);
  1054. return (error);
  1055. }
  1056. SYSCTL_PROC(_net_inet_tcp, TCPCTL_PCBLIST, pcblist,
  1057. CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
  1058. tcp_pcblist, "S,xtcpcb", "List of active TCP connections");
  1059. #ifdef INET
  1060. static int
  1061. tcp_getcred(SYSCTL_HANDLER_ARGS)
  1062. {
  1063. struct xucred xuc;
  1064. struct bsd_sockaddr_in addrs[2];
  1065. struct inpcb *inp;
  1066. int error;
  1067. error = priv_check(req->td, PRIV_NETINET_GETCRED);
  1068. if (error)
  1069. return (error);
  1070. error = SYSCTL_IN(req, addrs, sizeof(addrs));
  1071. if (error)
  1072. return (error);
  1073. inp = in_pcblookup(&V_tcbinfo, addrs[1].sin_addr, addrs[1].sin_port,
  1074. addrs[0].sin_addr, addrs[0].sin_port, INPLOOKUP_RLOCKPCB, NULL);
  1075. if (inp != NULL) {
  1076. if (inp->inp_socket == NULL)
  1077. error = ENOENT;
  1078. if (error == 0)
  1079. error = cr_canseeinpcb(req->td->td_ucred, inp);
  1080. if (error == 0)
  1081. cru2x(inp->inp_cred, &xuc);
  1082. INP_UNLOCK(inp);
  1083. } else
  1084. error = ENOENT;
  1085. if (error == 0)
  1086. error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  1087. return (error);
  1088. }
  1089. SYSCTL_PROC(_net_inet_tcp, OID_AUTO, getcred,
  1090. CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
  1091. tcp_getcred, "S,xucred", "Get the xucred of a TCP connection");
  1092. #endif /* INET */
  1093. #ifdef INET6
  1094. static int
  1095. tcp6_getcred(SYSCTL_HANDLER_ARGS)
  1096. {
  1097. struct xucred xuc;
  1098. struct bsd_sockaddr_in6 addrs[2];
  1099. struct inpcb *inp;
  1100. int error;
  1101. #ifdef INET
  1102. int mapped = 0;
  1103. #endif
  1104. error = priv_check(req->td, PRIV_NETINET_GETCRED);
  1105. if (error)
  1106. return (error);
  1107. error = SYSCTL_IN(req, addrs, sizeof(addrs));
  1108. if (error)
  1109. return (error);
  1110. if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
  1111. (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
  1112. return (error);
  1113. }
  1114. if (IN6_IS_ADDR_V4MAPPED(&addrs[0].sin6_addr)) {
  1115. #ifdef INET
  1116. if (IN6_IS_ADDR_V4MAPPED(&addrs[1].sin6_addr))
  1117. mapped = 1;
  1118. else
  1119. #endif
  1120. return (EINVAL);
  1121. }
  1122. #ifdef INET
  1123. if (mapped == 1)
  1124. inp = in_pcblookup(&V_tcbinfo,
  1125. *(struct in_addr *)&addrs[1].sin6_addr.s6_addr[12],
  1126. addrs[1].sin6_port,
  1127. *(struct in_addr *)&addrs[0].sin6_addr.s6_addr[12],
  1128. addrs[0].sin6_port, INPLOOKUP_RLOCKPCB, NULL);
  1129. else
  1130. #endif
  1131. inp = in6_pcblookup(&V_tcbinfo,
  1132. &addrs[1].sin6_addr, addrs[1].sin6_port,
  1133. &addrs[0].sin6_addr, addrs[0].sin6_port,
  1134. INPLOOKUP_RLOCKPCB, NULL);
  1135. if (inp != NULL) {
  1136. if (inp->inp_socket == NULL)
  1137. error = ENOENT;
  1138. if (error == 0)
  1139. error = cr_canseeinpcb(req->td->td_ucred, inp);
  1140. if (error == 0)
  1141. cru2x(inp->inp_cred, &xuc);
  1142. INP_UNLOCK(inp);
  1143. } else
  1144. error = ENOENT;
  1145. if (error == 0)
  1146. error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  1147. return (error);
  1148. }
  1149. SYSCTL_PROC(_net_inet6_tcp6, OID_AUTO, getcred,
  1150. CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
  1151. tcp6_getcred, "S,xucred", "Get the xucred of a TCP6 connection");
  1152. #endif /* INET6 */
  1153. #endif
  1154. #ifdef INET
  1155. void
  1156. tcp_ctlinput(int cmd, struct bsd_sockaddr *sa, void *vip)
  1157. {
  1158. struct ip *ip = (struct ip *)vip;
  1159. struct tcphdr *th;
  1160. struct in_addr faddr;
  1161. struct inpcb *inp;
  1162. struct tcpcb *tp;
  1163. struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify;
  1164. struct icmp *icp;
  1165. struct in_conninfo inc;
  1166. tcp_seq icmp_tcp_seq;
  1167. int mtu;
  1168. faddr = ((struct bsd_sockaddr_in *)sa)->sin_addr;
  1169. if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  1170. return;
  1171. if (cmd == PRC_MSGSIZE)
  1172. notify = tcp_mtudisc_notify;
  1173. else if (V_icmp_may_rst && (cmd == PRC_UNREACH_ADMIN_PROHIB ||
  1174. cmd == PRC_UNREACH_PORT || cmd == PRC_TIMXCEED_INTRANS) && ip)
  1175. notify = tcp_drop_syn_sent;
  1176. /*
  1177. * Redirects don't need to be handled up here.
  1178. */
  1179. else if (PRC_IS_REDIRECT(cmd))
  1180. return;
  1181. /*
  1182. * Source quench is depreciated.
  1183. */
  1184. else if (cmd == PRC_QUENCH)
  1185. return;
  1186. /*
  1187. * Hostdead is ugly because it goes linearly through all PCBs.
  1188. * XXX: We never get this from ICMP, otherwise it makes an
  1189. * excellent DoS attack on machines with many connections.
  1190. */
  1191. else if (cmd == PRC_HOSTDEAD)
  1192. ip = NULL;
  1193. else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
  1194. return;
  1195. if (ip != NULL) {
  1196. icp = (struct icmp *)((caddr_t)ip
  1197. - offsetof(struct icmp, icmp_ip));
  1198. th = (struct tcphdr *)((caddr_t)ip
  1199. + (ip->ip_hl << 2));
  1200. INP_INFO_WLOCK(&V_tcbinfo);
  1201. inp = in_pcblookup(&V_tcbinfo, faddr, th->th_dport,
  1202. ip->ip_src, th->th_sport, INPLOOKUP_LOCKPCB, NULL);
  1203. if (inp != NULL) {
  1204. if (!(inp->inp_flags & INP_TIMEWAIT) &&
  1205. !(inp->inp_flags & INP_DROPPED) &&
  1206. !(inp->inp_socket == NULL)) {
  1207. icmp_tcp_seq = htonl(th->th_seq);
  1208. tp = intotcpcb(inp);
  1209. if (icmp_tcp_seq >= tp->snd_una &&
  1210. icmp_tcp_seq < tp->snd_max) {
  1211. if (cmd == PRC_MSGSIZE) {
  1212. /*
  1213. * MTU discovery:
  1214. * If we got a needfrag set the MTU
  1215. * in the route to the suggested new
  1216. * value (if given) and then notify.
  1217. */
  1218. bzero(&inc, sizeof(inc));
  1219. inc.inc_faddr = faddr;
  1220. inc.inc_fibnum =
  1221. inp->inp_inc.inc_fibnum;
  1222. mtu = ntohs(icp->icmp_nextmtu);
  1223. /*
  1224. * If no alternative MTU was
  1225. * proposed, try the next smaller
  1226. * one. ip->ip_len has already
  1227. * been swapped in icmp_input().
  1228. */
  1229. if (!mtu)
  1230. mtu = ip_next_mtu(ip->ip_len,
  1231. 1);
  1232. if (mtu < V_tcp_minmss
  1233. + sizeof(struct tcpiphdr))
  1234. mtu = V_tcp_minmss
  1235. + sizeof(struct tcpiphdr);
  1236. /*
  1237. * Only cache the MTU if it
  1238. * is smaller than the interface
  1239. * or route MTU. tcp_mtudisc()
  1240. * will do right thing by itself.
  1241. */
  1242. if (mtu <= tcp_maxmtu(&inc, NULL))
  1243. tcp_hc_updatemtu(&inc, mtu);
  1244. tcp_mtudisc(inp, mtu);
  1245. } else
  1246. inp = (*notify)(inp,
  1247. inetctlerrmap[cmd]);
  1248. }
  1249. }
  1250. if (inp != NULL)
  1251. INP_UNLOCK(inp);
  1252. } else {
  1253. bzero(&inc, sizeof(inc));
  1254. inc.inc_fport = th->th_dport;
  1255. inc.inc_lport = th->th_sport;
  1256. inc.inc_faddr = faddr;
  1257. inc.inc_laddr = ip->ip_src;
  1258. syncache_unreach(&inc, th);
  1259. }
  1260. INP_INFO_WUNLOCK(&V_tcbinfo);
  1261. } else
  1262. in_pcbnotifyall(&V_tcbinfo, faddr, inetctlerrmap[cmd], notify);
  1263. }
  1264. #endif /* INET */
  1265. #ifdef INET6
  1266. void
  1267. tcp6_ctlinput(int cmd, struct bsd_sockaddr *sa, void *d)
  1268. {
  1269. struct tcphdr th;
  1270. struct inpcb *(*notify)(struct inpcb *, int) = tcp_notify;
  1271. struct ip6_hdr *ip6;
  1272. struct mbuf *m;
  1273. struct ip6ctlparam *ip6cp = NULL;
  1274. const struct bsd_sockaddr_in6 *sa6_src = NULL;
  1275. int off;
  1276. struct tcp_portonly {
  1277. u_int16_t th_sport;
  1278. u_int16_t th_dport;
  1279. } *thp;
  1280. if (sa->sa_family != AF_INET6 ||
  1281. sa->sa_len != sizeof(struct bsd_sockaddr_in6))
  1282. return;
  1283. if (cmd == PRC_MSGSIZE)
  1284. notify = tcp_mtudisc_notify;
  1285. else if (!PRC_IS_REDIRECT(cmd) &&
  1286. ((unsigned)cmd >= PRC_NCMDS || inet6ctlerrmap[cmd] == 0))
  1287. return;
  1288. /* Source quench is depreciated. */
  1289. else if (cmd == PRC_QUENCH)
  1290. return;
  1291. /* if the parameter is from icmp6, decode it. */
  1292. if (d != NULL) {
  1293. ip6cp = (struct ip6ctlparam *)d;
  1294. m = ip6cp->ip6c_m;
  1295. ip6 = ip6cp->ip6c_ip6;
  1296. off = ip6cp->ip6c_off;
  1297. sa6_src = ip6cp->ip6c_src;
  1298. } else {
  1299. m = NULL;
  1300. ip6 = NULL;
  1301. off = 0; /* fool gcc */
  1302. sa6_src = &sa6_any;
  1303. }
  1304. if (ip6 != NULL) {
  1305. struct in_conninfo inc;
  1306. /*
  1307. * XXX: We assume that when IPV6 is non NULL,
  1308. * M and OFF are valid.
  1309. */
  1310. /* check if we can safely examine src and dst ports */
  1311. if (m->M_dat.MH.MH_pkthdr.len < off + sizeof(*thp))
  1312. return;
  1313. bzero(&th, sizeof(th));
  1314. m_copydata(m, off, sizeof(*thp), (caddr_t)&th);
  1315. in6_pcbnotify(&V_tcbinfo, sa, th.th_dport,
  1316. (struct bsd_sockaddr *)ip6cp->ip6c_src,
  1317. th.th_sport, cmd, NULL, notify);
  1318. bzero(&inc, sizeof(inc));
  1319. inc.inc_fport = th.th_dport;
  1320. inc.inc_lport = th.th_sport;
  1321. inc.inc6_faddr = ((struct bsd_sockaddr_in6 *)sa)->sin6_addr;
  1322. inc.inc6_laddr = ip6cp->ip6c_src->sin6_addr;
  1323. inc.inc_flags |= INC_ISIPV6;
  1324. INP_INFO_WLOCK(&V_tcbinfo);
  1325. syncache_unreach(&inc, &th);
  1326. INP_INFO_WUNLOCK(&V_tcbinfo);
  1327. } else
  1328. in6_pcbnotify(&V_tcbinfo, sa, 0, (const struct bsd_sockaddr *)sa6_src,
  1329. 0, cmd, NULL, notify);
  1330. }
  1331. #endif /* INET6 */
  1332. /*
  1333. * Following is where TCP initial sequence number generation occurs.
  1334. *
  1335. * There are two places where we must use initial sequence numbers:
  1336. * 1. In SYN-ACK packets.
  1337. * 2. In SYN packets.
  1338. *
  1339. * All ISNs for SYN-ACK packets are generated by the syncache. See
  1340. * tcp_syncache.c for details.
  1341. *
  1342. * The ISNs in SYN packets must be monotonic; TIME_WAIT recycling
  1343. * depends on this property. In addition, these ISNs should be
  1344. * unguessable so as to prevent connection hijacking. To satisfy
  1345. * the requirements of this situation, the algorithm outlined in
  1346. * RFC 1948 is used, with only small modifications.
  1347. *
  1348. * Implementation details:
  1349. *
  1350. * Time is based off the system timer, and is corrected so that it
  1351. * increases by one megabyte per second. This allows for proper
  1352. * recycling on high speed LANs while still leaving over an hour
  1353. * before rollover.
  1354. *
  1355. * As reading the *exact* system time is too expensive to be done
  1356. * whenever setting up a TCP connection, we increment the time
  1357. * offset in two ways. First, a small random positive increment
  1358. * is added to isn_offset for each connection that is set up.
  1359. * Second, the function tcp_isn_tick fires once per clock tick
  1360. * and increments isn_offset as necessary so that sequence numbers
  1361. * are incremented at approximately ISN_BYTES_PER_SECOND. The
  1362. * random positive increments serve only to ensure that the same
  1363. * exact sequence number is never sent out twice (as could otherwise
  1364. * happen when a port is recycled in less than the system tick
  1365. * interval.)
  1366. *
  1367. * net.inet.tcp.isn_reseed_interval controls the number of seconds
  1368. * between seeding of isn_secret. This is normally set to zero,
  1369. * as reseeding should not be necessary.
  1370. *
  1371. * Locking of the global variables isn_secret, isn_last_reseed, isn_offset,
  1372. * isn_offset_old, and isn_ctx is performed using the TCP pcbinfo lock. In
  1373. * general, this means holding an exclusive (write) lock.
  1374. */
  1375. #define ISN_BYTES_PER_SECOND 1048576
  1376. #define ISN_STATIC_INCREMENT 4096
  1377. #define ISN_RANDOM_INCREMENT (4096 - 1)
  1378. static VNET_DEFINE(u_char, isn_secret[32]);
  1379. static VNET_DEFINE(int, isn_last);
  1380. static VNET_DEFINE(int, isn_last_reseed);
  1381. static VNET_DEFINE(u_int32_t, isn_offset);
  1382. static VNET_DEFINE(u_int32_t, isn_offset_old);
  1383. #define V_isn_secret VNET(isn_secret)
  1384. #define V_isn_last VNET(isn_last)
  1385. #define V_isn_last_reseed VNET(isn_last_reseed)
  1386. #define V_isn_offset VNET(isn_offset)
  1387. #define V_isn_offset_old VNET(isn_offset_old)
  1388. tcp_seq
  1389. tcp_new_isn(struct tcpcb *tp)
  1390. {
  1391. MD5_CTX isn_ctx;
  1392. u_int32_t md5_buffer[4];
  1393. tcp_seq new_isn;
  1394. u_int32_t projected_offset;
  1395. INP_LOCK_ASSERT(tp->t_inpcb);
  1396. ISN_LOCK();
  1397. /* Seed if this is the first use, reseed if requested. */
  1398. if ((V_isn_last_reseed == 0) || ((V_tcp_isn_reseed_interval > 0) &&
  1399. (((u_int)V_isn_last_reseed + (u_int)V_tcp_isn_reseed_interval*hz)
  1400. < (u_int)bsd_ticks))) {
  1401. read_random(&V_isn_secret, sizeof(V_isn_secret));
  1402. V_isn_last_reseed = bsd_ticks;
  1403. }
  1404. /* Compute the md5 hash and return the ISN. */
  1405. MD5Init(&isn_ctx);
  1406. MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_fport, sizeof(u_short));
  1407. MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_lport, sizeof(u_short));
  1408. #ifdef INET6
  1409. if ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) {
  1410. MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_faddr,
  1411. sizeof(struct in6_addr));
  1412. MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->in6p_laddr,
  1413. sizeof(struct in6_addr));
  1414. } else
  1415. #endif
  1416. {
  1417. MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_faddr,
  1418. sizeof(struct in_addr));
  1419. MD5Update(&isn_ctx, (u_char *) &tp->t_inpcb->inp_laddr,
  1420. sizeof(struct in_addr));
  1421. }
  1422. MD5Update(&isn_ctx, (u_char *) &V_isn_secret, sizeof(V_isn_secret));
  1423. MD5Final((u_char *) &md5_buffer, &isn_ctx);
  1424. new_isn = (tcp_seq) md5_buffer[0];
  1425. V_isn_offset += ISN_STATIC_INCREMENT +
  1426. (arc4random() & ISN_RANDOM_INCREMENT);
  1427. if (bsd_ticks != V_isn_last) {
  1428. projected_offset = V_isn_offset_old +
  1429. ISN_BYTES_PER_SECOND / hz * (bsd_ticks - V_isn_last);
  1430. if (tcp_seq(projected_offset) > tcp_seq(V_isn_offset))
  1431. V_isn_offset = projected_offset;
  1432. V_isn_offset_old = V_isn_offset;
  1433. V_isn_last = bsd_ticks;
  1434. }
  1435. new_isn += V_isn_offset;
  1436. ISN_UNLOCK();
  1437. return (new_isn);
  1438. }
  1439. /*
  1440. * When a specific ICMP unreachable message is received and the
  1441. * connection state is SYN-SENT, drop the connection. This behavior
  1442. * is controlled by the icmp_may_rst sysctl.
  1443. */
  1444. struct inpcb *
  1445. tcp_drop_syn_sent(struct inpcb *inp, int errval)
  1446. {
  1447. struct tcpcb *tp;
  1448. INP_INFO_WLOCK_ASSERT(&V_tcbinfo);
  1449. INP_LOCK_ASSERT(inp);
  1450. if ((inp->inp_flags & INP_TIMEWAIT) ||
  1451. (inp->inp_flags & INP_DROPPED))
  1452. return (inp);
  1453. tp = intotcpcb(inp);
  1454. if (tp->get_state() != TCPS_SYN_SENT)
  1455. return (inp);
  1456. tp = tcp_drop(tp, errval);
  1457. if (tp != NULL)
  1458. return (inp);
  1459. else
  1460. return (NULL);
  1461. }
  1462. /*
  1463. * When `need fragmentation' ICMP is received, update our idea of the MSS
  1464. * based on the new value. Also nudge TCP to send something, since we
  1465. * know the packet we just sent was dropped.
  1466. * This duplicates some code in the tcp_mss() function in tcp_input.c.
  1467. */
  1468. static struct inpcb *
  1469. tcp_mtudisc_notify(struct inpcb *inp, int error)
  1470. {
  1471. return (tcp_mtudisc(inp, -1));
  1472. }
  1473. struct inpcb *
  1474. tcp_mtudisc(struct inpcb *inp, int mtuoffer)
  1475. {
  1476. struct tcpcb *tp;
  1477. struct socket *so;
  1478. INP_LOCK_ASSERT(inp);
  1479. if ((inp->inp_flags & INP_TIMEWAIT) ||
  1480. (inp->inp_flags & INP_DROPPED))
  1481. return (inp);
  1482. tp = intotcpcb(inp);
  1483. KASSERT(tp != NULL, ("tcp_mtudisc: tp == NULL"));
  1484. tcp_mss_update(tp, -1, mtuoffer, NULL, NULL);
  1485. so = inp->inp_socket;
  1486. SOCK_LOCK(so);
  1487. /* If the mss is larger than the socket buffer, decrease the mss. */
  1488. if (so->so_snd.sb_hiwat < tp->t_maxseg)
  1489. tp->t_maxseg = so->so_snd.sb_hiwat;
  1490. SOCK_UNLOCK(so);
  1491. TCPSTAT_INC(tcps_mturesent);
  1492. tp->t_rtttime = 0;
  1493. tp->snd_nxt = tp->snd_una;
  1494. tcp_free_sackholes(tp);
  1495. tp->snd_recover = tp->snd_max;
  1496. if (tp->t_flags & TF_SACK_PERMIT)
  1497. EXIT_FASTRECOVERY(tp->t_flags);
  1498. tcp_output(tp);
  1499. return (inp);
  1500. }
  1501. #ifdef INET
  1502. /*
  1503. * Look-up the routing entry to the peer of this inpcb. If no route
  1504. * is found and it cannot be allocated, then return 0. This routine
  1505. * is called by TCP routines that access the rmx structure and by
  1506. * tcp_mss_update to get the peer/interface MTU.
  1507. */
  1508. u_long
  1509. tcp_maxmtu(struct in_conninfo *inc, int *flags)
  1510. {
  1511. struct route sro;
  1512. struct rtentry rte_one;
  1513. struct bsd_sockaddr_in *dst;
  1514. struct ifnet *ifp;
  1515. u_long maxmtu = 0;
  1516. KASSERT(inc != NULL, ("tcp_maxmtu with NULL in_conninfo pointer"));
  1517. bzero(&sro, sizeof(sro));
  1518. if (inc->inc_faddr.s_addr != INADDR_ANY) {
  1519. dst = (struct bsd_sockaddr_in *)&sro.ro_dst;
  1520. dst->sin_family = AF_INET;
  1521. dst->sin_len = sizeof(*dst);
  1522. dst->sin_addr = inc->inc_faddr;
  1523. if (route_cache::lookup(dst, inc->inc_fibnum, &rte_one)) {
  1524. sro.ro_rt = &rte_one;
  1525. } else {
  1526. sro.ro_rt = NULL;
  1527. }
  1528. }
  1529. if (sro.ro_rt != NULL) {
  1530. ifp = sro.ro_rt->rt_ifp;
  1531. if (sro.ro_rt->rt_rmx.rmx_mtu == 0)
  1532. maxmtu = ifp->if_mtu;
  1533. else
  1534. maxmtu = bsd_min(sro.ro_rt->rt_rmx.rmx_mtu, ifp->if_mtu);
  1535. /* Report additional interface capabilities. */
  1536. if (flags != NULL) {
  1537. if (ifp->if_capenable & IFCAP_TSO4 &&
  1538. ifp->if_hwassist & CSUM_TSO)
  1539. *flags |= CSUM_TSO;
  1540. }
  1541. }
  1542. return (maxmtu);
  1543. }
  1544. #endif /* INET */
  1545. #ifdef INET6
  1546. u_long
  1547. tcp_maxmtu6(struct in_conninfo *inc, int *flags)
  1548. {
  1549. struct route_in6 sro6;
  1550. struct ifnet *ifp;
  1551. u_long maxmtu = 0;
  1552. KASSERT(inc != NULL, ("tcp_maxmtu6 with NULL in_conninfo pointer"));
  1553. bzero(&sro6, sizeof(sro6));
  1554. if (!IN6_IS_ADDR_UNSPECIFIED(&inc->inc6_faddr)) {
  1555. sro6.ro_dst.sin6_family = AF_INET6;
  1556. sro6.ro_dst.sin6_len = sizeof(struct bsd_sockaddr_in6);
  1557. sro6.ro_dst.sin6_addr = inc->inc6_faddr;
  1558. in6_rtalloc_ign(&sro6, 0, inc->inc_fibnum);
  1559. }
  1560. if (sro6.ro_rt != NULL) {
  1561. ifp = sro6.ro_rt->rt_ifp;
  1562. if (sro6.ro_rt->rt_rmx.rmx_mtu == 0)
  1563. maxmtu = IN6_LINKMTU(sro6.ro_rt->rt_ifp);
  1564. else
  1565. maxmtu = min(sro6.ro_rt->rt_rmx.rmx_mtu,
  1566. IN6_LINKMTU(sro6.ro_rt->rt_ifp));
  1567. /* Report additional interface capabilities. */
  1568. if (flags != NULL) {
  1569. if (ifp->if_capenable & IFCAP_TSO6 &&
  1570. ifp->if_hwassist & CSUM_TSO)
  1571. *flags |= CSUM_TSO;
  1572. }
  1573. RTFREE(sro6.ro_rt);
  1574. }
  1575. return (maxmtu);
  1576. }
  1577. #endif /* INET6 */
  1578. #ifdef IPSEC
  1579. /* compute ESP/AH header size for TCP, including outer IP header. */
  1580. size_t
  1581. ipsec_hdrsiz_tcp(struct tcpcb *tp)
  1582. {
  1583. struct inpcb *inp;
  1584. struct mbuf *m;
  1585. size_t hdrsiz;
  1586. struct ip *ip;
  1587. #ifdef INET6
  1588. struct ip6_hdr *ip6;
  1589. #endif
  1590. struct tcphdr *th;
  1591. if ((tp == NULL) || ((inp = tp->t_inpcb) == NULL))
  1592. return (0);
  1593. MGETHDR(m, M_DONTWAIT, MT_DATA);
  1594. if (!m)
  1595. return (0);
  1596. #ifdef INET6
  1597. if ((inp->inp_vflag & INP_IPV6) != 0) {
  1598. ip6 = mtod(m, struct ip6_hdr *);
  1599. th = (struct tcphdr *)(ip6 + 1);
  1600. m->M_dat.MH.MH_pkthdr.len = m->m_hdr.mh_len =
  1601. sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
  1602. tcpip_fillheaders(inp, ip6, th);
  1603. hdrsiz = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
  1604. } else
  1605. #endif /* INET6 */
  1606. {
  1607. ip = mtod(m, struct ip *);
  1608. th = (struct tcphdr *)(ip + 1);
  1609. m->M_dat.MH.MH_pkthdr.len = m->m_hdr.mh_len = sizeof(struct tcpiphdr);
  1610. tcpip_fillheaders(inp, ip, th);
  1611. hdrsiz = ipsec_hdrsiz(m, IPSEC_DIR_OUTBOUND, inp);
  1612. }
  1613. m_free(m);
  1614. return (hdrsiz);
  1615. }
  1616. #endif /* IPSEC */
  1617. #ifdef TCP_SIGNATURE
  1618. /*
  1619. * Callback function invoked by m_apply() to digest TCP segment data
  1620. * contained within an mbuf chain.
  1621. */
  1622. static int
  1623. tcp_signature_apply(void *fstate, void *data, u_int len)
  1624. {
  1625. MD5Update(fstate, (u_char *)data, len);
  1626. return (0);
  1627. }
  1628. /*
  1629. * Compute TCP-MD5 hash of a TCP segment. (RFC2385)
  1630. *
  1631. * Parameters:
  1632. * m pointer to head of mbuf chain
  1633. * _unused
  1634. * len length of TCP segment data, excluding options
  1635. * optlen length of TCP segment options
  1636. * buf pointer to storage for computed MD5 digest
  1637. * direction direction of flow (IPSEC_DIR_INBOUND or OUTBOUND)
  1638. *
  1639. * We do this over ip, tcphdr, segment data, and the key in the SADB.
  1640. * When called from tcp_input(), we can be sure that th_sum has been
  1641. * zeroed out and verified already.
  1642. *
  1643. * Return 0 if successful, otherwise return -1.
  1644. *
  1645. * XXX The key is retrieved from the system's PF_KEY SADB, by keying a
  1646. * search with the destination IP address, and a 'magic SPI' to be
  1647. * determined by the application. This is hardcoded elsewhere to 1179
  1648. * right now. Another branch of this code exists which uses the SPD to
  1649. * specify per-application flows but it is unstable.
  1650. */
  1651. int
  1652. tcp_signature_compute(struct mb

Large files files are truncated, but you can click here to view the full file