print-tcp.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920
  1. /* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
  2. /*
  3. * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Copyright (c) 1999-2004 The tcpdump.org project
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that: (1) source code distributions
  10. * retain the above copyright notice and this paragraph in its entirety, (2)
  11. * distributions including binary code include the above copyright notice and
  12. * this paragraph in its entirety in the documentation or other materials
  13. * provided with the distribution, and (3) all advertising materials mentioning
  14. * features or use of this software display the following acknowledgement:
  15. * ``This product includes software developed by the University of California,
  16. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  17. * the University nor the names of its contributors may be used to endorse
  18. * or promote products derived from this software without specific prior
  19. * written permission.
  20. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. */
  24. /* \summary: TCP printer */
  25. #ifndef lint
  26. #else
  27. __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
  28. #endif
  29. #ifdef HAVE_CONFIG_H
  30. #include "config.h"
  31. #endif
  32. #include <netdissect-stdinc.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include "netdissect.h"
  36. #include "addrtoname.h"
  37. #include "extract.h"
  38. #include "tcp.h"
  39. #include "ip.h"
  40. #include "ip6.h"
  41. #include "ipproto.h"
  42. #include "rpc_auth.h"
  43. #include "rpc_msg.h"
  44. #ifdef HAVE_LIBCRYPTO
  45. #include <openssl/md5.h>
  46. #include "signature.h"
  47. static int tcp_verify_signature(netdissect_options *ndo,
  48. const struct ip *ip, const struct tcphdr *tp,
  49. const u_char *data, int length, const u_char *rcvsig);
  50. #endif
  51. static void print_tcp_rst_data(netdissect_options *, register const u_char *sp, u_int length);
  52. static void print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp,
  53. u_int datalen, int exp);
  54. #define MAX_RST_DATA_LEN 30
  55. struct tha {
  56. struct in_addr src;
  57. struct in_addr dst;
  58. u_int port;
  59. };
  60. struct tcp_seq_hash {
  61. struct tcp_seq_hash *nxt;
  62. struct tha addr;
  63. tcp_seq seq;
  64. tcp_seq ack;
  65. };
  66. struct tha6 {
  67. struct in6_addr src;
  68. struct in6_addr dst;
  69. u_int port;
  70. };
  71. struct tcp_seq_hash6 {
  72. struct tcp_seq_hash6 *nxt;
  73. struct tha6 addr;
  74. tcp_seq seq;
  75. tcp_seq ack;
  76. };
  77. #define TSEQ_HASHSIZE 919
  78. /* These tcp optinos do not have the size octet */
  79. #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
  80. static struct tcp_seq_hash tcp_seq_hash4[TSEQ_HASHSIZE];
  81. static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE];
  82. static const struct tok tcp_flag_values[] = {
  83. { TH_FIN, "F" },
  84. { TH_SYN, "S" },
  85. { TH_RST, "R" },
  86. { TH_PUSH, "P" },
  87. { TH_ACK, "." },
  88. { TH_URG, "U" },
  89. { TH_ECNECHO, "E" },
  90. { TH_CWR, "W" },
  91. { 0, NULL }
  92. };
  93. static const struct tok tcp_option_values[] = {
  94. { TCPOPT_EOL, "eol" },
  95. { TCPOPT_NOP, "nop" },
  96. { TCPOPT_MAXSEG, "mss" },
  97. { TCPOPT_WSCALE, "wscale" },
  98. { TCPOPT_SACKOK, "sackOK" },
  99. { TCPOPT_SACK, "sack" },
  100. { TCPOPT_ECHO, "echo" },
  101. { TCPOPT_ECHOREPLY, "echoreply" },
  102. { TCPOPT_TIMESTAMP, "TS" },
  103. { TCPOPT_CC, "cc" },
  104. { TCPOPT_CCNEW, "ccnew" },
  105. { TCPOPT_CCECHO, "" },
  106. { TCPOPT_SIGNATURE, "md5" },
  107. { TCPOPT_SCPS, "scps" },
  108. { TCPOPT_UTO, "uto" },
  109. { TCPOPT_TCPAO, "tcp-ao" },
  110. { TCPOPT_MPTCP, "mptcp" },
  111. { TCPOPT_FASTOPEN, "tfo" },
  112. { TCPOPT_EXPERIMENT2, "exp" },
  113. { 0, NULL }
  114. };
  115. static int
  116. tcp_cksum(netdissect_options *ndo,
  117. register const struct ip *ip,
  118. register const struct tcphdr *tp,
  119. register u_int len)
  120. {
  121. return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len,
  122. IPPROTO_TCP);
  123. }
  124. static int
  125. tcp6_cksum(netdissect_options *ndo,
  126. register const struct ip6_hdr *ip6,
  127. register const struct tcphdr *tp,
  128. register u_int len)
  129. {
  130. return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len,
  131. IPPROTO_TCP);
  132. }
  133. void
  134. tcp_print(netdissect_options *ndo,
  135. register const u_char *bp, register u_int length,
  136. register const u_char *bp2, int fragmented)
  137. {
  138. register const struct tcphdr *tp;
  139. register const struct ip *ip;
  140. register u_char flags;
  141. register u_int hlen;
  142. register char ch;
  143. uint16_t sport, dport, win, urp;
  144. uint32_t seq, ack, thseq, thack;
  145. u_int utoval;
  146. uint16_t magic;
  147. register int rev;
  148. register const struct ip6_hdr *ip6;
  149. tp = (const struct tcphdr *)bp;
  150. ip = (const struct ip *)bp2;
  151. if (IP_V(ip) == 6)
  152. ip6 = (const struct ip6_hdr *)bp2;
  153. else
  154. ip6 = NULL;
  155. ch = '\0';
  156. if (!ND_TTEST(tp->th_dport)) {
  157. ND_PRINT((ndo, "%s > %s: [|tcp]",
  158. ipaddr_string(ndo, &ip->ip_src),
  159. ipaddr_string(ndo, &ip->ip_dst)));
  160. return;
  161. }
  162. sport = EXTRACT_16BITS(&tp->th_sport);
  163. dport = EXTRACT_16BITS(&tp->th_dport);
  164. if (ip6) {
  165. if (ip6->ip6_nxt == IPPROTO_TCP) {
  166. ND_PRINT((ndo, "%s.%s > %s.%s: ",
  167. ip6addr_string(ndo, &ip6->ip6_src),
  168. tcpport_string(ndo, sport),
  169. ip6addr_string(ndo, &ip6->ip6_dst),
  170. tcpport_string(ndo, dport)));
  171. } else {
  172. ND_PRINT((ndo, "%s > %s: ",
  173. tcpport_string(ndo, sport), tcpport_string(ndo, dport)));
  174. }
  175. } else {
  176. if (ip->ip_p == IPPROTO_TCP) {
  177. ND_PRINT((ndo, "%s.%s > %s.%s: ",
  178. ipaddr_string(ndo, &ip->ip_src),
  179. tcpport_string(ndo, sport),
  180. ipaddr_string(ndo, &ip->ip_dst),
  181. tcpport_string(ndo, dport)));
  182. } else {
  183. ND_PRINT((ndo, "%s > %s: ",
  184. tcpport_string(ndo, sport), tcpport_string(ndo, dport)));
  185. }
  186. }
  187. ND_TCHECK(*tp);
  188. hlen = TH_OFF(tp) * 4;
  189. if (hlen < sizeof(*tp)) {
  190. ND_PRINT((ndo, " tcp %d [bad hdr length %u - too short, < %lu]",
  191. length - hlen, hlen, (unsigned long)sizeof(*tp)));
  192. return;
  193. }
  194. seq = EXTRACT_32BITS(&tp->th_seq);
  195. ack = EXTRACT_32BITS(&tp->th_ack);
  196. win = EXTRACT_16BITS(&tp->th_win);
  197. urp = EXTRACT_16BITS(&tp->th_urp);
  198. if (ndo->ndo_qflag) {
  199. ND_PRINT((ndo, "tcp %d", length - hlen));
  200. if (hlen > length) {
  201. ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
  202. hlen, length));
  203. }
  204. return;
  205. }
  206. flags = tp->th_flags;
  207. ND_PRINT((ndo, "Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags)));
  208. if (!ndo->ndo_Sflag && (flags & TH_ACK)) {
  209. /*
  210. * Find (or record) the initial sequence numbers for
  211. * this conversation. (we pick an arbitrary
  212. * collating order so there's only one entry for
  213. * both directions).
  214. */
  215. rev = 0;
  216. if (ip6) {
  217. register struct tcp_seq_hash6 *th;
  218. struct tcp_seq_hash6 *tcp_seq_hash;
  219. const struct in6_addr *src, *dst;
  220. struct tha6 tha;
  221. tcp_seq_hash = tcp_seq_hash6;
  222. src = &ip6->ip6_src;
  223. dst = &ip6->ip6_dst;
  224. if (sport > dport)
  225. rev = 1;
  226. else if (sport == dport) {
  227. if (UNALIGNED_MEMCMP(src, dst, sizeof ip6->ip6_dst) > 0)
  228. rev = 1;
  229. }
  230. if (rev) {
  231. UNALIGNED_MEMCPY(&tha.src, dst, sizeof ip6->ip6_dst);
  232. UNALIGNED_MEMCPY(&tha.dst, src, sizeof ip6->ip6_src);
  233. tha.port = dport << 16 | sport;
  234. } else {
  235. UNALIGNED_MEMCPY(&tha.dst, dst, sizeof ip6->ip6_dst);
  236. UNALIGNED_MEMCPY(&tha.src, src, sizeof ip6->ip6_src);
  237. tha.port = sport << 16 | dport;
  238. }
  239. for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
  240. th->nxt; th = th->nxt)
  241. if (memcmp((char *)&tha, (char *)&th->addr,
  242. sizeof(th->addr)) == 0)
  243. break;
  244. if (!th->nxt || (flags & TH_SYN)) {
  245. /* didn't find it or new conversation */
  246. if (th->nxt == NULL) {
  247. th->nxt = (struct tcp_seq_hash6 *)
  248. calloc(1, sizeof(*th));
  249. if (th->nxt == NULL)
  250. (*ndo->ndo_error)(ndo,
  251. "tcp_print: calloc");
  252. }
  253. th->addr = tha;
  254. if (rev)
  255. th->ack = seq, th->seq = ack - 1;
  256. else
  257. th->seq = seq, th->ack = ack - 1;
  258. } else {
  259. if (rev)
  260. seq -= th->ack, ack -= th->seq;
  261. else
  262. seq -= th->seq, ack -= th->ack;
  263. }
  264. thseq = th->seq;
  265. thack = th->ack;
  266. } else {
  267. register struct tcp_seq_hash *th;
  268. struct tcp_seq_hash *tcp_seq_hash;
  269. struct tha tha;
  270. tcp_seq_hash = tcp_seq_hash4;
  271. if (sport > dport)
  272. rev = 1;
  273. else if (sport == dport) {
  274. if (UNALIGNED_MEMCMP(&ip->ip_src, &ip->ip_dst, sizeof ip->ip_dst) > 0)
  275. rev = 1;
  276. }
  277. if (rev) {
  278. UNALIGNED_MEMCPY(&tha.src, &ip->ip_dst, sizeof ip->ip_dst);
  279. UNALIGNED_MEMCPY(&tha.dst, &ip->ip_src, sizeof ip->ip_src);
  280. tha.port = dport << 16 | sport;
  281. } else {
  282. UNALIGNED_MEMCPY(&tha.dst, &ip->ip_dst, sizeof ip->ip_dst);
  283. UNALIGNED_MEMCPY(&tha.src, &ip->ip_src, sizeof ip->ip_src);
  284. tha.port = sport << 16 | dport;
  285. }
  286. for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
  287. th->nxt; th = th->nxt)
  288. if (memcmp((char *)&tha, (char *)&th->addr,
  289. sizeof(th->addr)) == 0)
  290. break;
  291. if (!th->nxt || (flags & TH_SYN)) {
  292. /* didn't find it or new conversation */
  293. if (th->nxt == NULL) {
  294. th->nxt = (struct tcp_seq_hash *)
  295. calloc(1, sizeof(*th));
  296. if (th->nxt == NULL)
  297. (*ndo->ndo_error)(ndo,
  298. "tcp_print: calloc");
  299. }
  300. th->addr = tha;
  301. if (rev)
  302. th->ack = seq, th->seq = ack - 1;
  303. else
  304. th->seq = seq, th->ack = ack - 1;
  305. } else {
  306. if (rev)
  307. seq -= th->ack, ack -= th->seq;
  308. else
  309. seq -= th->seq, ack -= th->ack;
  310. }
  311. thseq = th->seq;
  312. thack = th->ack;
  313. }
  314. } else {
  315. /*fool gcc*/
  316. thseq = thack = rev = 0;
  317. }
  318. if (hlen > length) {
  319. ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
  320. hlen, length));
  321. return;
  322. }
  323. if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) {
  324. /* Check the checksum, if possible. */
  325. uint16_t sum, tcp_sum;
  326. if (IP_V(ip) == 4) {
  327. if (ND_TTEST2(tp->th_sport, length)) {
  328. sum = tcp_cksum(ndo, ip, tp, length);
  329. tcp_sum = EXTRACT_16BITS(&tp->th_sum);
  330. ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum));
  331. if (sum != 0)
  332. ND_PRINT((ndo, " (incorrect -> 0x%04x)",
  333. in_cksum_shouldbe(tcp_sum, sum)));
  334. else
  335. ND_PRINT((ndo, " (correct)"));
  336. }
  337. } else if (IP_V(ip) == 6 && ip6->ip6_plen) {
  338. if (ND_TTEST2(tp->th_sport, length)) {
  339. sum = tcp6_cksum(ndo, ip6, tp, length);
  340. tcp_sum = EXTRACT_16BITS(&tp->th_sum);
  341. ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum));
  342. if (sum != 0)
  343. ND_PRINT((ndo, " (incorrect -> 0x%04x)",
  344. in_cksum_shouldbe(tcp_sum, sum)));
  345. else
  346. ND_PRINT((ndo, " (correct)"));
  347. }
  348. }
  349. }
  350. length -= hlen;
  351. if (ndo->ndo_vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
  352. ND_PRINT((ndo, ", seq %u", seq));
  353. if (length > 0) {
  354. ND_PRINT((ndo, ":%u", seq + length));
  355. }
  356. }
  357. if (flags & TH_ACK) {
  358. ND_PRINT((ndo, ", ack %u", ack));
  359. }
  360. ND_PRINT((ndo, ", win %d", win));
  361. if (flags & TH_URG)
  362. ND_PRINT((ndo, ", urg %d", urp));
  363. /*
  364. * Handle any options.
  365. */
  366. if (hlen > sizeof(*tp)) {
  367. register const u_char *cp;
  368. register u_int i, opt, datalen;
  369. register u_int len;
  370. hlen -= sizeof(*tp);
  371. cp = (const u_char *)tp + sizeof(*tp);
  372. ND_PRINT((ndo, ", options ["));
  373. while (hlen > 0) {
  374. if (ch != '\0')
  375. ND_PRINT((ndo, "%c", ch));
  376. ND_TCHECK(*cp);
  377. opt = *cp++;
  378. if (ZEROLENOPT(opt))
  379. len = 1;
  380. else {
  381. ND_TCHECK(*cp);
  382. len = *cp++; /* total including type, len */
  383. if (len < 2 || len > hlen)
  384. goto bad;
  385. --hlen; /* account for length byte */
  386. }
  387. --hlen; /* account for type byte */
  388. datalen = 0;
  389. /* Bail if "l" bytes of data are not left or were not captured */
  390. #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); }
  391. ND_PRINT((ndo, "%s", tok2str(tcp_option_values, "unknown-%u", opt)));
  392. switch (opt) {
  393. case TCPOPT_MAXSEG:
  394. datalen = 2;
  395. LENCHECK(datalen);
  396. ND_PRINT((ndo, " %u", EXTRACT_16BITS(cp)));
  397. break;
  398. case TCPOPT_WSCALE:
  399. datalen = 1;
  400. LENCHECK(datalen);
  401. ND_PRINT((ndo, " %u", *cp));
  402. break;
  403. case TCPOPT_SACK:
  404. datalen = len - 2;
  405. if (datalen % 8 != 0) {
  406. ND_PRINT((ndo, " invalid sack"));
  407. } else {
  408. uint32_t s, e;
  409. ND_PRINT((ndo, " %d ", datalen / 8));
  410. for (i = 0; i < datalen; i += 8) {
  411. LENCHECK(i + 4);
  412. s = EXTRACT_32BITS(cp + i);
  413. LENCHECK(i + 8);
  414. e = EXTRACT_32BITS(cp + i + 4);
  415. if (rev) {
  416. s -= thseq;
  417. e -= thseq;
  418. } else {
  419. s -= thack;
  420. e -= thack;
  421. }
  422. ND_PRINT((ndo, "{%u:%u}", s, e));
  423. }
  424. }
  425. break;
  426. case TCPOPT_CC:
  427. case TCPOPT_CCNEW:
  428. case TCPOPT_CCECHO:
  429. case TCPOPT_ECHO:
  430. case TCPOPT_ECHOREPLY:
  431. /*
  432. * those options share their semantics.
  433. * fall through
  434. */
  435. datalen = 4;
  436. LENCHECK(datalen);
  437. ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
  438. break;
  439. case TCPOPT_TIMESTAMP:
  440. datalen = 8;
  441. LENCHECK(datalen);
  442. ND_PRINT((ndo, " val %u ecr %u",
  443. EXTRACT_32BITS(cp),
  444. EXTRACT_32BITS(cp + 4)));
  445. break;
  446. case TCPOPT_SIGNATURE:
  447. datalen = TCP_SIGLEN;
  448. LENCHECK(datalen);
  449. ND_PRINT((ndo, " "));
  450. #ifdef HAVE_LIBCRYPTO
  451. switch (tcp_verify_signature(ndo, ip, tp,
  452. bp + TH_OFF(tp) * 4, length, cp)) {
  453. case SIGNATURE_VALID:
  454. ND_PRINT((ndo, "valid"));
  455. break;
  456. case SIGNATURE_INVALID:
  457. ND_PRINT((ndo, "invalid"));
  458. break;
  459. case CANT_CHECK_SIGNATURE:
  460. ND_PRINT((ndo, "can't check - "));
  461. for (i = 0; i < TCP_SIGLEN; ++i)
  462. ND_PRINT((ndo, "%02x", cp[i]));
  463. break;
  464. }
  465. #else
  466. for (i = 0; i < TCP_SIGLEN; ++i)
  467. ND_PRINT((ndo, "%02x", cp[i]));
  468. #endif
  469. break;
  470. case TCPOPT_SCPS:
  471. datalen = 2;
  472. LENCHECK(datalen);
  473. ND_PRINT((ndo, " cap %02x id %u", cp[0], cp[1]));
  474. break;
  475. case TCPOPT_TCPAO:
  476. datalen = len - 2;
  477. /* RFC 5925 Section 2.2:
  478. * "The Length value MUST be greater than or equal to 4."
  479. * (This includes the Kind and Length fields already processed
  480. * at this point.)
  481. */
  482. if (datalen < 2) {
  483. ND_PRINT((ndo, " invalid"));
  484. } else {
  485. LENCHECK(1);
  486. ND_PRINT((ndo, " keyid %u", cp[0]));
  487. LENCHECK(2);
  488. ND_PRINT((ndo, " rnextkeyid %u", cp[1]));
  489. if (datalen > 2) {
  490. ND_PRINT((ndo, " mac 0x"));
  491. for (i = 2; i < datalen; i++) {
  492. LENCHECK(i + 1);
  493. ND_PRINT((ndo, "%02x", cp[i]));
  494. }
  495. }
  496. }
  497. break;
  498. case TCPOPT_EOL:
  499. case TCPOPT_NOP:
  500. case TCPOPT_SACKOK:
  501. /*
  502. * Nothing interesting.
  503. * fall through
  504. */
  505. break;
  506. case TCPOPT_UTO:
  507. datalen = 2;
  508. LENCHECK(datalen);
  509. utoval = EXTRACT_16BITS(cp);
  510. ND_PRINT((ndo, " 0x%x", utoval));
  511. if (utoval & 0x0001)
  512. utoval = (utoval >> 1) * 60;
  513. else
  514. utoval >>= 1;
  515. ND_PRINT((ndo, " %u", utoval));
  516. break;
  517. case TCPOPT_MPTCP:
  518. datalen = len - 2;
  519. LENCHECK(datalen);
  520. if (!mptcp_print(ndo, cp-2, len, flags))
  521. goto bad;
  522. break;
  523. case TCPOPT_FASTOPEN:
  524. datalen = len - 2;
  525. LENCHECK(datalen);
  526. ND_PRINT((ndo, " "));
  527. print_tcp_fastopen_option(ndo, cp, datalen, FALSE);
  528. break;
  529. case TCPOPT_EXPERIMENT2:
  530. datalen = len - 2;
  531. LENCHECK(datalen);
  532. if (datalen < 2)
  533. goto bad;
  534. /* RFC6994 */
  535. magic = EXTRACT_16BITS(cp);
  536. ND_PRINT((ndo, "-"));
  537. switch(magic) {
  538. case 0xf989: /* TCP Fast Open RFC 7413 */
  539. print_tcp_fastopen_option(ndo, cp + 2, datalen - 2, TRUE);
  540. break;
  541. default:
  542. /* Unknown magic number */
  543. ND_PRINT((ndo, "%04x", magic));
  544. break;
  545. }
  546. break;
  547. default:
  548. datalen = len - 2;
  549. if (datalen)
  550. ND_PRINT((ndo, " 0x"));
  551. for (i = 0; i < datalen; ++i) {
  552. LENCHECK(i + 1);
  553. ND_PRINT((ndo, "%02x", cp[i]));
  554. }
  555. break;
  556. }
  557. /* Account for data printed */
  558. cp += datalen;
  559. hlen -= datalen;
  560. /* Check specification against observed length */
  561. ++datalen; /* option octet */
  562. if (!ZEROLENOPT(opt))
  563. ++datalen; /* size octet */
  564. if (datalen != len)
  565. ND_PRINT((ndo, "[len %d]", len));
  566. ch = ',';
  567. if (opt == TCPOPT_EOL)
  568. break;
  569. }
  570. ND_PRINT((ndo, "]"));
  571. }
  572. /*
  573. * Print length field before crawling down the stack.
  574. */
  575. ND_PRINT((ndo, ", length %u", length));
  576. if (length <= 0)
  577. return;
  578. /*
  579. * Decode payload if necessary.
  580. */
  581. bp += TH_OFF(tp) * 4;
  582. if ((flags & TH_RST) && ndo->ndo_vflag) {
  583. print_tcp_rst_data(ndo, bp, length);
  584. return;
  585. }
  586. if (ndo->ndo_packettype) {
  587. switch (ndo->ndo_packettype) {
  588. case PT_ZMTP1:
  589. zmtp1_print(ndo, bp, length);
  590. break;
  591. case PT_RESP:
  592. resp_print(ndo, bp, length);
  593. break;
  594. }
  595. return;
  596. }
  597. if (IS_SRC_OR_DST_PORT(TELNET_PORT)) {
  598. telnet_print(ndo, bp, length);
  599. } else if (IS_SRC_OR_DST_PORT(SMTP_PORT)) {
  600. ND_PRINT((ndo, ": "));
  601. smtp_print(ndo, bp, length);
  602. } else if (IS_SRC_OR_DST_PORT(BGP_PORT))
  603. bgp_print(ndo, bp, length);
  604. else if (IS_SRC_OR_DST_PORT(PPTP_PORT))
  605. pptp_print(ndo, bp);
  606. else if (IS_SRC_OR_DST_PORT(REDIS_PORT))
  607. resp_print(ndo, bp, length);
  608. #ifdef ENABLE_SMB
  609. else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT))
  610. nbt_tcp_print(ndo, bp, length);
  611. else if (IS_SRC_OR_DST_PORT(SMB_PORT))
  612. smb_tcp_print(ndo, bp, length);
  613. #endif
  614. else if (IS_SRC_OR_DST_PORT(BEEP_PORT))
  615. beep_print(ndo, bp, length);
  616. else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA))
  617. openflow_print(ndo, bp, length);
  618. else if (IS_SRC_OR_DST_PORT(FTP_PORT)) {
  619. ND_PRINT((ndo, ": "));
  620. ftp_print(ndo, bp, length);
  621. } else if (IS_SRC_OR_DST_PORT(HTTP_PORT) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT)) {
  622. ND_PRINT((ndo, ": "));
  623. http_print(ndo, bp, length);
  624. } else if (IS_SRC_OR_DST_PORT(RTSP_PORT) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT)) {
  625. ND_PRINT((ndo, ": "));
  626. rtsp_print(ndo, bp, length);
  627. } else if (length > 2 &&
  628. (IS_SRC_OR_DST_PORT(NAMESERVER_PORT))) {
  629. /*
  630. * TCP DNS query has 2byte length at the head.
  631. * XXX packet could be unaligned, it can go strange
  632. */
  633. ns_print(ndo, bp + 2, length - 2, 0);
  634. } else if (IS_SRC_OR_DST_PORT(MSDP_PORT)) {
  635. msdp_print(ndo, bp, length);
  636. } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT)) {
  637. rpki_rtr_print(ndo, bp, length);
  638. }
  639. else if (length > 0 && (IS_SRC_OR_DST_PORT(LDP_PORT))) {
  640. ldp_print(ndo, bp, length);
  641. }
  642. else if ((IS_SRC_OR_DST_PORT(NFS_PORT)) &&
  643. length >= 4 && ND_TTEST2(*bp, 4)) {
  644. /*
  645. * If data present, header length valid, and NFS port used,
  646. * assume NFS.
  647. * Pass offset of data plus 4 bytes for RPC TCP msg length
  648. * to NFS print routines.
  649. */
  650. uint32_t fraglen;
  651. register const struct sunrpc_msg *rp;
  652. enum sunrpc_msg_type direction;
  653. fraglen = EXTRACT_32BITS(bp) & 0x7FFFFFFF;
  654. if (fraglen > (length) - 4)
  655. fraglen = (length) - 4;
  656. rp = (const struct sunrpc_msg *)(bp + 4);
  657. if (ND_TTEST(rp->rm_direction)) {
  658. direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
  659. if (dport == NFS_PORT && direction == SUNRPC_CALL) {
  660. ND_PRINT((ndo, ": NFS request xid %u ", EXTRACT_32BITS(&rp->rm_xid)));
  661. nfsreq_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
  662. return;
  663. }
  664. if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
  665. ND_PRINT((ndo, ": NFS reply xid %u ", EXTRACT_32BITS(&rp->rm_xid)));
  666. nfsreply_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
  667. return;
  668. }
  669. }
  670. }
  671. return;
  672. bad:
  673. ND_PRINT((ndo, "[bad opt]"));
  674. if (ch != '\0')
  675. ND_PRINT((ndo, ">"));
  676. return;
  677. trunc:
  678. ND_PRINT((ndo, "[|tcp]"));
  679. if (ch != '\0')
  680. ND_PRINT((ndo, ">"));
  681. }
  682. /*
  683. * RFC1122 says the following on data in RST segments:
  684. *
  685. * 4.2.2.12 RST Segment: RFC-793 Section 3.4
  686. *
  687. * A TCP SHOULD allow a received RST segment to include data.
  688. *
  689. * DISCUSSION
  690. * It has been suggested that a RST segment could contain
  691. * ASCII text that encoded and explained the cause of the
  692. * RST. No standard has yet been established for such
  693. * data.
  694. *
  695. */
  696. static void
  697. print_tcp_rst_data(netdissect_options *ndo,
  698. register const u_char *sp, u_int length)
  699. {
  700. int c;
  701. ND_PRINT((ndo, ND_TTEST2(*sp, length) ? " [RST" : " [!RST"));
  702. if (length > MAX_RST_DATA_LEN) {
  703. length = MAX_RST_DATA_LEN; /* can use -X for longer */
  704. ND_PRINT((ndo, "+")); /* indicate we truncate */
  705. }
  706. ND_PRINT((ndo, " "));
  707. while (length-- && sp < ndo->ndo_snapend) {
  708. c = *sp++;
  709. safeputchar(ndo, c);
  710. }
  711. ND_PRINT((ndo, "]"));
  712. }
  713. static void
  714. print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp,
  715. u_int datalen, int exp)
  716. {
  717. u_int i;
  718. if (exp)
  719. ND_PRINT((ndo, "tfo"));
  720. if (datalen == 0) {
  721. /* Fast Open Cookie Request */
  722. ND_PRINT((ndo, " cookiereq"));
  723. } else {
  724. /* Fast Open Cookie */
  725. if (datalen % 2 != 0 || datalen < 4 || datalen > 16) {
  726. ND_PRINT((ndo, " invalid"));
  727. } else {
  728. ND_PRINT((ndo, " cookie "));
  729. for (i = 0; i < datalen; ++i)
  730. ND_PRINT((ndo, "%02x", cp[i]));
  731. }
  732. }
  733. }
  734. #ifdef HAVE_LIBCRYPTO
  735. USES_APPLE_DEPRECATED_API
  736. static int
  737. tcp_verify_signature(netdissect_options *ndo,
  738. const struct ip *ip, const struct tcphdr *tp,
  739. const u_char *data, int length, const u_char *rcvsig)
  740. {
  741. struct tcphdr tp1;
  742. u_char sig[TCP_SIGLEN];
  743. char zero_proto = 0;
  744. MD5_CTX ctx;
  745. uint16_t savecsum, tlen;
  746. const struct ip6_hdr *ip6;
  747. uint32_t len32;
  748. uint8_t nxt;
  749. if (data + length > ndo->ndo_snapend) {
  750. ND_PRINT((ndo, "snaplen too short, "));
  751. return (CANT_CHECK_SIGNATURE);
  752. }
  753. tp1 = *tp;
  754. if (ndo->ndo_sigsecret == NULL) {
  755. ND_PRINT((ndo, "shared secret not supplied with -M, "));
  756. return (CANT_CHECK_SIGNATURE);
  757. }
  758. MD5_Init(&ctx);
  759. /*
  760. * Step 1: Update MD5 hash with IP pseudo-header.
  761. */
  762. if (IP_V(ip) == 4) {
  763. MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src));
  764. MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst));
  765. MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto));
  766. MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p));
  767. tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
  768. tlen = htons(tlen);
  769. MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen));
  770. } else if (IP_V(ip) == 6) {
  771. ip6 = (const struct ip6_hdr *)ip;
  772. MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
  773. MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
  774. len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen));
  775. MD5_Update(&ctx, (const char *)&len32, sizeof(len32));
  776. nxt = 0;
  777. MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
  778. MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
  779. MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
  780. nxt = IPPROTO_TCP;
  781. MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
  782. } else {
  783. ND_PRINT((ndo, "IP version not 4 or 6, "));
  784. return (CANT_CHECK_SIGNATURE);
  785. }
  786. /*
  787. * Step 2: Update MD5 hash with TCP header, excluding options.
  788. * The TCP checksum must be set to zero.
  789. */
  790. savecsum = tp1.th_sum;
  791. tp1.th_sum = 0;
  792. MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr));
  793. tp1.th_sum = savecsum;
  794. /*
  795. * Step 3: Update MD5 hash with TCP segment data, if present.
  796. */
  797. if (length > 0)
  798. MD5_Update(&ctx, data, length);
  799. /*
  800. * Step 4: Update MD5 hash with shared secret.
  801. */
  802. MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret));
  803. MD5_Final(sig, &ctx);
  804. if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
  805. return (SIGNATURE_VALID);
  806. else
  807. return (SIGNATURE_INVALID);
  808. }
  809. USES_APPLE_RST
  810. #endif /* HAVE_LIBCRYPTO */
  811. /*
  812. * Local Variables:
  813. * c-style: whitesmith
  814. * c-basic-offset: 8
  815. * End:
  816. */