blob: e0d0531b36b26eed49ff08449cc6c880cb0524e0 [file] [log] [blame]
JP Abgrall53f17a92014-02-12 14:02:41 -08001/* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
2
The Android Open Source Project2949f582009-03-03 19:30:46 -08003/*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Copyright (c) 1999-2004 The tcpdump.org project
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that: (1) source code distributions
11 * retain the above copyright notice and this paragraph in its entirety, (2)
12 * distributions including binary code include the above copyright notice and
13 * this paragraph in its entirety in the documentation or other materials
14 * provided with the distribution, and (3) all advertising materials mentioning
15 * features or use of this software display the following acknowledgement:
16 * ``This product includes software developed by the University of California,
17 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
18 * the University nor the names of its contributors may be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 */
25
Elliott Hughese2e3bd12017-05-15 10:59:29 -070026/* \summary: TCP printer */
27
The Android Open Source Project2949f582009-03-03 19:30:46 -080028#ifndef lint
JP Abgrall53f17a92014-02-12 14:02:41 -080029#else
30__RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#endif
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
Elliott Hughese2e3bd12017-05-15 10:59:29 -070037#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080038
The Android Open Source Project2949f582009-03-03 19:30:46 -080039#include <stdlib.h>
40#include <string.h>
41
Elliott Hughese2e3bd12017-05-15 10:59:29 -070042#include "netdissect.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080043#include "addrtoname.h"
44#include "extract.h"
45
46#include "tcp.h"
47
48#include "ip.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080049#include "ip6.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080050#include "ipproto.h"
51#include "rpc_auth.h"
52#include "rpc_msg.h"
53
The Android Open Source Project2949f582009-03-03 19:30:46 -080054#ifdef HAVE_LIBCRYPTO
55#include <openssl/md5.h>
Elliott Hughese2e3bd12017-05-15 10:59:29 -070056#include "signature.h"
The Android Open Source Project2949f582009-03-03 19:30:46 -080057
Elliott Hughes892a68b2015-10-19 14:43:53 -070058static int tcp_verify_signature(netdissect_options *ndo,
59 const struct ip *ip, const struct tcphdr *tp,
JP Abgrall53f17a92014-02-12 14:02:41 -080060 const u_char *data, int length, const u_char *rcvsig);
The Android Open Source Project2949f582009-03-03 19:30:46 -080061#endif
62
Elliott Hughes892a68b2015-10-19 14:43:53 -070063static void print_tcp_rst_data(netdissect_options *, register const u_char *sp, u_int length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -070064static void print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp,
65 u_int datalen, int exp);
The Android Open Source Project2949f582009-03-03 19:30:46 -080066
67#define MAX_RST_DATA_LEN 30
68
69
70struct tha {
JP Abgrall53f17a92014-02-12 14:02:41 -080071 struct in_addr src;
72 struct in_addr dst;
73 u_int port;
The Android Open Source Project2949f582009-03-03 19:30:46 -080074};
75
76struct tcp_seq_hash {
JP Abgrall53f17a92014-02-12 14:02:41 -080077 struct tcp_seq_hash *nxt;
78 struct tha addr;
79 tcp_seq seq;
80 tcp_seq ack;
The Android Open Source Project2949f582009-03-03 19:30:46 -080081};
82
JP Abgrall53f17a92014-02-12 14:02:41 -080083struct tha6 {
84 struct in6_addr src;
85 struct in6_addr dst;
86 u_int port;
87};
88
89struct tcp_seq_hash6 {
90 struct tcp_seq_hash6 *nxt;
91 struct tha6 addr;
92 tcp_seq seq;
93 tcp_seq ack;
94};
JP Abgrall53f17a92014-02-12 14:02:41 -080095
The Android Open Source Project2949f582009-03-03 19:30:46 -080096#define TSEQ_HASHSIZE 919
97
98/* These tcp optinos do not have the size octet */
99#define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
100
JP Abgrall53f17a92014-02-12 14:02:41 -0800101static struct tcp_seq_hash tcp_seq_hash4[TSEQ_HASHSIZE];
JP Abgrall53f17a92014-02-12 14:02:41 -0800102static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE];
The Android Open Source Project2949f582009-03-03 19:30:46 -0800103
JP Abgrall53f17a92014-02-12 14:02:41 -0800104static const struct tok tcp_flag_values[] = {
105 { TH_FIN, "F" },
106 { TH_SYN, "S" },
107 { TH_RST, "R" },
108 { TH_PUSH, "P" },
109 { TH_ACK, "." },
110 { TH_URG, "U" },
111 { TH_ECNECHO, "E" },
112 { TH_CWR, "W" },
113 { 0, NULL }
114};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800115
JP Abgrall53f17a92014-02-12 14:02:41 -0800116static const struct tok tcp_option_values[] = {
117 { TCPOPT_EOL, "eol" },
118 { TCPOPT_NOP, "nop" },
119 { TCPOPT_MAXSEG, "mss" },
120 { TCPOPT_WSCALE, "wscale" },
121 { TCPOPT_SACKOK, "sackOK" },
122 { TCPOPT_SACK, "sack" },
123 { TCPOPT_ECHO, "echo" },
124 { TCPOPT_ECHOREPLY, "echoreply" },
125 { TCPOPT_TIMESTAMP, "TS" },
126 { TCPOPT_CC, "cc" },
127 { TCPOPT_CCNEW, "ccnew" },
128 { TCPOPT_CCECHO, "" },
129 { TCPOPT_SIGNATURE, "md5" },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700130 { TCPOPT_SCPS, "scps" },
JP Abgrall53f17a92014-02-12 14:02:41 -0800131 { TCPOPT_UTO, "uto" },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700132 { TCPOPT_TCPAO, "tcp-ao" },
JP Abgrall53f17a92014-02-12 14:02:41 -0800133 { TCPOPT_MPTCP, "mptcp" },
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700134 { TCPOPT_FASTOPEN, "tfo" },
JP Abgrall53f17a92014-02-12 14:02:41 -0800135 { TCPOPT_EXPERIMENT2, "exp" },
136 { 0, NULL }
137};
The Android Open Source Project2949f582009-03-03 19:30:46 -0800138
Elliott Hughes892a68b2015-10-19 14:43:53 -0700139static int
140tcp_cksum(netdissect_options *ndo,
141 register const struct ip *ip,
142 register const struct tcphdr *tp,
143 register u_int len)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800144{
Elliott Hughes892a68b2015-10-19 14:43:53 -0700145 return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len,
146 IPPROTO_TCP);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800147}
148
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700149static int
150tcp6_cksum(netdissect_options *ndo,
151 register const struct ip6_hdr *ip6,
152 register const struct tcphdr *tp,
153 register u_int len)
154{
155 return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len,
156 IPPROTO_TCP);
157}
158
The Android Open Source Project2949f582009-03-03 19:30:46 -0800159void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700160tcp_print(netdissect_options *ndo,
161 register const u_char *bp, register u_int length,
162 register const u_char *bp2, int fragmented)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800163{
JP Abgrall53f17a92014-02-12 14:02:41 -0800164 register const struct tcphdr *tp;
165 register const struct ip *ip;
166 register u_char flags;
167 register u_int hlen;
168 register char ch;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700169 uint16_t sport, dport, win, urp;
170 uint32_t seq, ack, thseq, thack;
JP Abgrall53f17a92014-02-12 14:02:41 -0800171 u_int utoval;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700172 uint16_t magic;
JP Abgrall53f17a92014-02-12 14:02:41 -0800173 register int rev;
JP Abgrall53f17a92014-02-12 14:02:41 -0800174 register const struct ip6_hdr *ip6;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800175
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700176 tp = (const struct tcphdr *)bp;
177 ip = (const struct ip *)bp2;
JP Abgrall53f17a92014-02-12 14:02:41 -0800178 if (IP_V(ip) == 6)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700179 ip6 = (const struct ip6_hdr *)bp2;
JP Abgrall53f17a92014-02-12 14:02:41 -0800180 else
181 ip6 = NULL;
JP Abgrall53f17a92014-02-12 14:02:41 -0800182 ch = '\0';
Elliott Hughes892a68b2015-10-19 14:43:53 -0700183 if (!ND_TTEST(tp->th_dport)) {
184 ND_PRINT((ndo, "%s > %s: [|tcp]",
185 ipaddr_string(ndo, &ip->ip_src),
186 ipaddr_string(ndo, &ip->ip_dst)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800187 return;
188 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800189
JP Abgrall53f17a92014-02-12 14:02:41 -0800190 sport = EXTRACT_16BITS(&tp->th_sport);
191 dport = EXTRACT_16BITS(&tp->th_dport);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800192
JP Abgrall53f17a92014-02-12 14:02:41 -0800193 if (ip6) {
194 if (ip6->ip6_nxt == IPPROTO_TCP) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700195 ND_PRINT((ndo, "%s.%s > %s.%s: ",
196 ip6addr_string(ndo, &ip6->ip6_src),
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700197 tcpport_string(ndo, sport),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700198 ip6addr_string(ndo, &ip6->ip6_dst),
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700199 tcpport_string(ndo, dport)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800200 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700201 ND_PRINT((ndo, "%s > %s: ",
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700202 tcpport_string(ndo, sport), tcpport_string(ndo, dport)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800203 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700204 } else {
JP Abgrall53f17a92014-02-12 14:02:41 -0800205 if (ip->ip_p == IPPROTO_TCP) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700206 ND_PRINT((ndo, "%s.%s > %s.%s: ",
207 ipaddr_string(ndo, &ip->ip_src),
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700208 tcpport_string(ndo, sport),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700209 ipaddr_string(ndo, &ip->ip_dst),
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700210 tcpport_string(ndo, dport)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800211 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700212 ND_PRINT((ndo, "%s > %s: ",
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700213 tcpport_string(ndo, sport), tcpport_string(ndo, dport)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800214 }
215 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800216
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700217 ND_TCHECK(*tp);
218
219 hlen = TH_OFF(tp) * 4;
220
JP Abgrall53f17a92014-02-12 14:02:41 -0800221 if (hlen < sizeof(*tp)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700222 ND_PRINT((ndo, " tcp %d [bad hdr length %u - too short, < %lu]",
223 length - hlen, hlen, (unsigned long)sizeof(*tp)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800224 return;
225 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800226
JP Abgrall53f17a92014-02-12 14:02:41 -0800227 seq = EXTRACT_32BITS(&tp->th_seq);
228 ack = EXTRACT_32BITS(&tp->th_ack);
229 win = EXTRACT_16BITS(&tp->th_win);
230 urp = EXTRACT_16BITS(&tp->th_urp);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800231
Elliott Hughes892a68b2015-10-19 14:43:53 -0700232 if (ndo->ndo_qflag) {
233 ND_PRINT((ndo, "tcp %d", length - hlen));
JP Abgrall53f17a92014-02-12 14:02:41 -0800234 if (hlen > length) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700235 ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
236 hlen, length));
JP Abgrall53f17a92014-02-12 14:02:41 -0800237 }
238 return;
239 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800240
JP Abgrall53f17a92014-02-12 14:02:41 -0800241 flags = tp->th_flags;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700242 ND_PRINT((ndo, "Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800243
Elliott Hughes892a68b2015-10-19 14:43:53 -0700244 if (!ndo->ndo_Sflag && (flags & TH_ACK)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800245 /*
246 * Find (or record) the initial sequence numbers for
247 * this conversation. (we pick an arbitrary
248 * collating order so there's only one entry for
249 * both directions).
250 */
251 rev = 0;
JP Abgrall53f17a92014-02-12 14:02:41 -0800252 if (ip6) {
253 register struct tcp_seq_hash6 *th;
254 struct tcp_seq_hash6 *tcp_seq_hash;
255 const struct in6_addr *src, *dst;
256 struct tha6 tha;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800257
JP Abgrall53f17a92014-02-12 14:02:41 -0800258 tcp_seq_hash = tcp_seq_hash6;
259 src = &ip6->ip6_src;
260 dst = &ip6->ip6_dst;
261 if (sport > dport)
262 rev = 1;
263 else if (sport == dport) {
264 if (UNALIGNED_MEMCMP(src, dst, sizeof ip6->ip6_dst) > 0)
265 rev = 1;
266 }
267 if (rev) {
268 UNALIGNED_MEMCPY(&tha.src, dst, sizeof ip6->ip6_dst);
269 UNALIGNED_MEMCPY(&tha.dst, src, sizeof ip6->ip6_src);
270 tha.port = dport << 16 | sport;
271 } else {
272 UNALIGNED_MEMCPY(&tha.dst, dst, sizeof ip6->ip6_dst);
273 UNALIGNED_MEMCPY(&tha.src, src, sizeof ip6->ip6_src);
274 tha.port = sport << 16 | dport;
275 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800276
JP Abgrall53f17a92014-02-12 14:02:41 -0800277 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
278 th->nxt; th = th->nxt)
279 if (memcmp((char *)&tha, (char *)&th->addr,
280 sizeof(th->addr)) == 0)
281 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800282
JP Abgrall53f17a92014-02-12 14:02:41 -0800283 if (!th->nxt || (flags & TH_SYN)) {
284 /* didn't find it or new conversation */
285 if (th->nxt == NULL) {
286 th->nxt = (struct tcp_seq_hash6 *)
287 calloc(1, sizeof(*th));
288 if (th->nxt == NULL)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700289 (*ndo->ndo_error)(ndo,
290 "tcp_print: calloc");
JP Abgrall53f17a92014-02-12 14:02:41 -0800291 }
292 th->addr = tha;
293 if (rev)
294 th->ack = seq, th->seq = ack - 1;
295 else
296 th->seq = seq, th->ack = ack - 1;
297 } else {
298 if (rev)
299 seq -= th->ack, ack -= th->seq;
300 else
301 seq -= th->seq, ack -= th->ack;
302 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800303
JP Abgrall53f17a92014-02-12 14:02:41 -0800304 thseq = th->seq;
305 thack = th->ack;
306 } else {
JP Abgrall53f17a92014-02-12 14:02:41 -0800307 register struct tcp_seq_hash *th;
308 struct tcp_seq_hash *tcp_seq_hash;
JP Abgrall53f17a92014-02-12 14:02:41 -0800309 struct tha tha;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800310
JP Abgrall53f17a92014-02-12 14:02:41 -0800311 tcp_seq_hash = tcp_seq_hash4;
JP Abgrall53f17a92014-02-12 14:02:41 -0800312 if (sport > dport)
313 rev = 1;
314 else if (sport == dport) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700315 if (UNALIGNED_MEMCMP(&ip->ip_src, &ip->ip_dst, sizeof ip->ip_dst) > 0)
JP Abgrall53f17a92014-02-12 14:02:41 -0800316 rev = 1;
317 }
318 if (rev) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700319 UNALIGNED_MEMCPY(&tha.src, &ip->ip_dst, sizeof ip->ip_dst);
320 UNALIGNED_MEMCPY(&tha.dst, &ip->ip_src, sizeof ip->ip_src);
JP Abgrall53f17a92014-02-12 14:02:41 -0800321 tha.port = dport << 16 | sport;
322 } else {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700323 UNALIGNED_MEMCPY(&tha.dst, &ip->ip_dst, sizeof ip->ip_dst);
324 UNALIGNED_MEMCPY(&tha.src, &ip->ip_src, sizeof ip->ip_src);
JP Abgrall53f17a92014-02-12 14:02:41 -0800325 tha.port = sport << 16 | dport;
326 }
327
328 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
329 th->nxt; th = th->nxt)
330 if (memcmp((char *)&tha, (char *)&th->addr,
331 sizeof(th->addr)) == 0)
332 break;
333
334 if (!th->nxt || (flags & TH_SYN)) {
335 /* didn't find it or new conversation */
336 if (th->nxt == NULL) {
337 th->nxt = (struct tcp_seq_hash *)
338 calloc(1, sizeof(*th));
339 if (th->nxt == NULL)
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700340 (*ndo->ndo_error)(ndo,
341 "tcp_print: calloc");
JP Abgrall53f17a92014-02-12 14:02:41 -0800342 }
343 th->addr = tha;
344 if (rev)
345 th->ack = seq, th->seq = ack - 1;
346 else
347 th->seq = seq, th->ack = ack - 1;
348 } else {
349 if (rev)
350 seq -= th->ack, ack -= th->seq;
351 else
352 seq -= th->seq, ack -= th->ack;
353 }
354
355 thseq = th->seq;
356 thack = th->ack;
357 }
358 } else {
359 /*fool gcc*/
360 thseq = thack = rev = 0;
361 }
362 if (hlen > length) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700363 ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
364 hlen, length));
JP Abgrall53f17a92014-02-12 14:02:41 -0800365 return;
366 }
367
Elliott Hughes892a68b2015-10-19 14:43:53 -0700368 if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800369 /* Check the checksum, if possible. */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700370 uint16_t sum, tcp_sum;
JP Abgrall53f17a92014-02-12 14:02:41 -0800371
372 if (IP_V(ip) == 4) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700373 if (ND_TTEST2(tp->th_sport, length)) {
374 sum = tcp_cksum(ndo, ip, tp, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800375 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
376
Elliott Hughes892a68b2015-10-19 14:43:53 -0700377 ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum));
JP Abgrall53f17a92014-02-12 14:02:41 -0800378 if (sum != 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700379 ND_PRINT((ndo, " (incorrect -> 0x%04x)",
380 in_cksum_shouldbe(tcp_sum, sum)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800381 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700382 ND_PRINT((ndo, " (correct)"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800383 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700384 } else if (IP_V(ip) == 6 && ip6->ip6_plen) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700385 if (ND_TTEST2(tp->th_sport, length)) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700386 sum = tcp6_cksum(ndo, ip6, tp, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800387 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800388
Elliott Hughes892a68b2015-10-19 14:43:53 -0700389 ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum));
JP Abgrall53f17a92014-02-12 14:02:41 -0800390 if (sum != 0)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700391 ND_PRINT((ndo, " (incorrect -> 0x%04x)",
392 in_cksum_shouldbe(tcp_sum, sum)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800393 else
Elliott Hughes892a68b2015-10-19 14:43:53 -0700394 ND_PRINT((ndo, " (correct)"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800395
396 }
397 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800398 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800399
JP Abgrall53f17a92014-02-12 14:02:41 -0800400 length -= hlen;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700401 if (ndo->ndo_vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
402 ND_PRINT((ndo, ", seq %u", seq));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800403
JP Abgrall53f17a92014-02-12 14:02:41 -0800404 if (length > 0) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700405 ND_PRINT((ndo, ":%u", seq + length));
JP Abgrall53f17a92014-02-12 14:02:41 -0800406 }
407 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800408
JP Abgrall53f17a92014-02-12 14:02:41 -0800409 if (flags & TH_ACK) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700410 ND_PRINT((ndo, ", ack %u", ack));
JP Abgrall53f17a92014-02-12 14:02:41 -0800411 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800412
Elliott Hughes892a68b2015-10-19 14:43:53 -0700413 ND_PRINT((ndo, ", win %d", win));
JP Abgrall53f17a92014-02-12 14:02:41 -0800414
415 if (flags & TH_URG)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700416 ND_PRINT((ndo, ", urg %d", urp));
JP Abgrall53f17a92014-02-12 14:02:41 -0800417 /*
418 * Handle any options.
419 */
420 if (hlen > sizeof(*tp)) {
421 register const u_char *cp;
422 register u_int i, opt, datalen;
423 register u_int len;
424
425 hlen -= sizeof(*tp);
426 cp = (const u_char *)tp + sizeof(*tp);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700427 ND_PRINT((ndo, ", options ["));
JP Abgrall53f17a92014-02-12 14:02:41 -0800428 while (hlen > 0) {
429 if (ch != '\0')
Elliott Hughes892a68b2015-10-19 14:43:53 -0700430 ND_PRINT((ndo, "%c", ch));
431 ND_TCHECK(*cp);
JP Abgrall53f17a92014-02-12 14:02:41 -0800432 opt = *cp++;
433 if (ZEROLENOPT(opt))
434 len = 1;
435 else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700436 ND_TCHECK(*cp);
JP Abgrall53f17a92014-02-12 14:02:41 -0800437 len = *cp++; /* total including type, len */
438 if (len < 2 || len > hlen)
439 goto bad;
440 --hlen; /* account for length byte */
441 }
442 --hlen; /* account for type byte */
443 datalen = 0;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800444
445/* Bail if "l" bytes of data are not left or were not captured */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700446#define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800447
The Android Open Source Project2949f582009-03-03 19:30:46 -0800448
Elliott Hughes892a68b2015-10-19 14:43:53 -0700449 ND_PRINT((ndo, "%s", tok2str(tcp_option_values, "unknown-%u", opt)));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800450
JP Abgrall53f17a92014-02-12 14:02:41 -0800451 switch (opt) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800452
JP Abgrall53f17a92014-02-12 14:02:41 -0800453 case TCPOPT_MAXSEG:
454 datalen = 2;
455 LENCHECK(datalen);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700456 ND_PRINT((ndo, " %u", EXTRACT_16BITS(cp)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800457 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800458
JP Abgrall53f17a92014-02-12 14:02:41 -0800459 case TCPOPT_WSCALE:
460 datalen = 1;
461 LENCHECK(datalen);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700462 ND_PRINT((ndo, " %u", *cp));
JP Abgrall53f17a92014-02-12 14:02:41 -0800463 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800464
JP Abgrall53f17a92014-02-12 14:02:41 -0800465 case TCPOPT_SACK:
466 datalen = len - 2;
467 if (datalen % 8 != 0) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700468 ND_PRINT((ndo, " invalid sack"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800469 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700470 uint32_t s, e;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800471
Elliott Hughes892a68b2015-10-19 14:43:53 -0700472 ND_PRINT((ndo, " %d ", datalen / 8));
JP Abgrall53f17a92014-02-12 14:02:41 -0800473 for (i = 0; i < datalen; i += 8) {
474 LENCHECK(i + 4);
475 s = EXTRACT_32BITS(cp + i);
476 LENCHECK(i + 8);
477 e = EXTRACT_32BITS(cp + i + 4);
478 if (rev) {
479 s -= thseq;
480 e -= thseq;
481 } else {
482 s -= thack;
483 e -= thack;
484 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700485 ND_PRINT((ndo, "{%u:%u}", s, e));
JP Abgrall53f17a92014-02-12 14:02:41 -0800486 }
487 }
488 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800489
JP Abgrall53f17a92014-02-12 14:02:41 -0800490 case TCPOPT_CC:
491 case TCPOPT_CCNEW:
492 case TCPOPT_CCECHO:
493 case TCPOPT_ECHO:
494 case TCPOPT_ECHOREPLY:
The Android Open Source Project2949f582009-03-03 19:30:46 -0800495
JP Abgrall53f17a92014-02-12 14:02:41 -0800496 /*
497 * those options share their semantics.
498 * fall through
499 */
500 datalen = 4;
501 LENCHECK(datalen);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700502 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800503 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800504
JP Abgrall53f17a92014-02-12 14:02:41 -0800505 case TCPOPT_TIMESTAMP:
506 datalen = 8;
507 LENCHECK(datalen);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700508 ND_PRINT((ndo, " val %u ecr %u",
JP Abgrall53f17a92014-02-12 14:02:41 -0800509 EXTRACT_32BITS(cp),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700510 EXTRACT_32BITS(cp + 4)));
JP Abgrall53f17a92014-02-12 14:02:41 -0800511 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800512
JP Abgrall53f17a92014-02-12 14:02:41 -0800513 case TCPOPT_SIGNATURE:
514 datalen = TCP_SIGLEN;
515 LENCHECK(datalen);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700516 ND_PRINT((ndo, " "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800517#ifdef HAVE_LIBCRYPTO
Elliott Hughes892a68b2015-10-19 14:43:53 -0700518 switch (tcp_verify_signature(ndo, ip, tp,
JP Abgrall53f17a92014-02-12 14:02:41 -0800519 bp + TH_OFF(tp) * 4, length, cp)) {
The Android Open Source Project2949f582009-03-03 19:30:46 -0800520
JP Abgrall53f17a92014-02-12 14:02:41 -0800521 case SIGNATURE_VALID:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700522 ND_PRINT((ndo, "valid"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800523 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800524
JP Abgrall53f17a92014-02-12 14:02:41 -0800525 case SIGNATURE_INVALID:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700526 ND_PRINT((ndo, "invalid"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800527 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800528
JP Abgrall53f17a92014-02-12 14:02:41 -0800529 case CANT_CHECK_SIGNATURE:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700530 ND_PRINT((ndo, "can't check - "));
JP Abgrall53f17a92014-02-12 14:02:41 -0800531 for (i = 0; i < TCP_SIGLEN; ++i)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700532 ND_PRINT((ndo, "%02x", cp[i]));
JP Abgrall53f17a92014-02-12 14:02:41 -0800533 break;
534 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800535#else
JP Abgrall53f17a92014-02-12 14:02:41 -0800536 for (i = 0; i < TCP_SIGLEN; ++i)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700537 ND_PRINT((ndo, "%02x", cp[i]));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800538#endif
JP Abgrall53f17a92014-02-12 14:02:41 -0800539 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800540
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700541 case TCPOPT_SCPS:
542 datalen = 2;
543 LENCHECK(datalen);
544 ND_PRINT((ndo, " cap %02x id %u", cp[0], cp[1]));
JP Abgrall53f17a92014-02-12 14:02:41 -0800545 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800546
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700547 case TCPOPT_TCPAO:
548 datalen = len - 2;
549 /* RFC 5925 Section 2.2:
550 * "The Length value MUST be greater than or equal to 4."
551 * (This includes the Kind and Length fields already processed
552 * at this point.)
553 */
554 if (datalen < 2) {
555 ND_PRINT((ndo, " invalid"));
556 } else {
557 LENCHECK(1);
558 ND_PRINT((ndo, " keyid %u", cp[0]));
559 LENCHECK(2);
560 ND_PRINT((ndo, " rnextkeyid %u", cp[1]));
561 if (datalen > 2) {
562 ND_PRINT((ndo, " mac 0x"));
563 for (i = 2; i < datalen; i++) {
564 LENCHECK(i + 1);
565 ND_PRINT((ndo, "%02x", cp[i]));
566 }
567 }
568 }
569 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800570
JP Abgrall53f17a92014-02-12 14:02:41 -0800571 case TCPOPT_EOL:
572 case TCPOPT_NOP:
573 case TCPOPT_SACKOK:
574 /*
575 * Nothing interesting.
576 * fall through
577 */
578 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800579
JP Abgrall53f17a92014-02-12 14:02:41 -0800580 case TCPOPT_UTO:
581 datalen = 2;
582 LENCHECK(datalen);
583 utoval = EXTRACT_16BITS(cp);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700584 ND_PRINT((ndo, " 0x%x", utoval));
JP Abgrall53f17a92014-02-12 14:02:41 -0800585 if (utoval & 0x0001)
586 utoval = (utoval >> 1) * 60;
587 else
588 utoval >>= 1;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700589 ND_PRINT((ndo, " %u", utoval));
JP Abgrall53f17a92014-02-12 14:02:41 -0800590 break;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800591
JP Abgrall53f17a92014-02-12 14:02:41 -0800592 case TCPOPT_MPTCP:
593 datalen = len - 2;
594 LENCHECK(datalen);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700595 if (!mptcp_print(ndo, cp-2, len, flags))
JP Abgrall53f17a92014-02-12 14:02:41 -0800596 goto bad;
597 break;
598
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700599 case TCPOPT_FASTOPEN:
600 datalen = len - 2;
601 LENCHECK(datalen);
602 ND_PRINT((ndo, " "));
603 print_tcp_fastopen_option(ndo, cp, datalen, FALSE);
604 break;
605
JP Abgrall53f17a92014-02-12 14:02:41 -0800606 case TCPOPT_EXPERIMENT2:
607 datalen = len - 2;
608 LENCHECK(datalen);
609 if (datalen < 2)
610 goto bad;
611 /* RFC6994 */
612 magic = EXTRACT_16BITS(cp);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700613 ND_PRINT((ndo, "-"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800614
615 switch(magic) {
616
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700617 case 0xf989: /* TCP Fast Open RFC 7413 */
618 print_tcp_fastopen_option(ndo, cp + 2, datalen - 2, TRUE);
JP Abgrall53f17a92014-02-12 14:02:41 -0800619 break;
620
621 default:
622 /* Unknown magic number */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700623 ND_PRINT((ndo, "%04x", magic));
JP Abgrall53f17a92014-02-12 14:02:41 -0800624 break;
625 }
626 break;
627
628 default:
629 datalen = len - 2;
630 if (datalen)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700631 ND_PRINT((ndo, " 0x"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800632 for (i = 0; i < datalen; ++i) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700633 LENCHECK(i + 1);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700634 ND_PRINT((ndo, "%02x", cp[i]));
JP Abgrall53f17a92014-02-12 14:02:41 -0800635 }
636 break;
637 }
638
639 /* Account for data printed */
640 cp += datalen;
641 hlen -= datalen;
642
643 /* Check specification against observed length */
644 ++datalen; /* option octet */
645 if (!ZEROLENOPT(opt))
646 ++datalen; /* size octet */
647 if (datalen != len)
Elliott Hughes892a68b2015-10-19 14:43:53 -0700648 ND_PRINT((ndo, "[len %d]", len));
JP Abgrall53f17a92014-02-12 14:02:41 -0800649 ch = ',';
650 if (opt == TCPOPT_EOL)
651 break;
652 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700653 ND_PRINT((ndo, "]"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800654 }
655
656 /*
657 * Print length field before crawling down the stack.
658 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700659 ND_PRINT((ndo, ", length %u", length));
JP Abgrall53f17a92014-02-12 14:02:41 -0800660
661 if (length <= 0)
662 return;
663
664 /*
665 * Decode payload if necessary.
666 */
667 bp += TH_OFF(tp) * 4;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700668 if ((flags & TH_RST) && ndo->ndo_vflag) {
669 print_tcp_rst_data(ndo, bp, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800670 return;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700671 }
JP Abgrall53f17a92014-02-12 14:02:41 -0800672
Elliott Hughes892a68b2015-10-19 14:43:53 -0700673 if (ndo->ndo_packettype) {
674 switch (ndo->ndo_packettype) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800675 case PT_ZMTP1:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700676 zmtp1_print(ndo, bp, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800677 break;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700678 case PT_RESP:
679 resp_print(ndo, bp, length);
680 break;
JP Abgrall53f17a92014-02-12 14:02:41 -0800681 }
682 return;
683 }
684
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700685 if (IS_SRC_OR_DST_PORT(TELNET_PORT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700686 telnet_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700687 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700688 ND_PRINT((ndo, ": "));
689 smtp_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700690 } else if (IS_SRC_OR_DST_PORT(BGP_PORT))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700691 bgp_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700692 else if (IS_SRC_OR_DST_PORT(PPTP_PORT))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700693 pptp_print(ndo, bp);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700694 else if (IS_SRC_OR_DST_PORT(REDIS_PORT))
695 resp_print(ndo, bp, length);
696#ifdef ENABLE_SMB
697 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700698 nbt_tcp_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700699 else if (IS_SRC_OR_DST_PORT(SMB_PORT))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700700 smb_tcp_print(ndo, bp, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800701#endif
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700702 else if (IS_SRC_OR_DST_PORT(BEEP_PORT))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700703 beep_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700704 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA))
Elliott Hughes892a68b2015-10-19 14:43:53 -0700705 openflow_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700706 else if (IS_SRC_OR_DST_PORT(FTP_PORT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700707 ND_PRINT((ndo, ": "));
708 ftp_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700709 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700710 ND_PRINT((ndo, ": "));
711 http_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700712 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700713 ND_PRINT((ndo, ": "));
714 rtsp_print(ndo, bp, length);
715 } else if (length > 2 &&
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700716 (IS_SRC_OR_DST_PORT(NAMESERVER_PORT))) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800717 /*
718 * TCP DNS query has 2byte length at the head.
719 * XXX packet could be unaligned, it can go strange
720 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700721 ns_print(ndo, bp + 2, length - 2, 0);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700722 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700723 msdp_print(ndo, bp, length);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700724 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT)) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700725 rpki_rtr_print(ndo, bp, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800726 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700727 else if (length > 0 && (IS_SRC_OR_DST_PORT(LDP_PORT))) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700728 ldp_print(ndo, bp, length);
JP Abgrall53f17a92014-02-12 14:02:41 -0800729 }
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700730 else if ((IS_SRC_OR_DST_PORT(NFS_PORT)) &&
Elliott Hughes892a68b2015-10-19 14:43:53 -0700731 length >= 4 && ND_TTEST2(*bp, 4)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800732 /*
733 * If data present, header length valid, and NFS port used,
734 * assume NFS.
735 * Pass offset of data plus 4 bytes for RPC TCP msg length
736 * to NFS print routines.
737 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700738 uint32_t fraglen;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700739 register const struct sunrpc_msg *rp;
JP Abgrall53f17a92014-02-12 14:02:41 -0800740 enum sunrpc_msg_type direction;
741
742 fraglen = EXTRACT_32BITS(bp) & 0x7FFFFFFF;
743 if (fraglen > (length) - 4)
744 fraglen = (length) - 4;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700745 rp = (const struct sunrpc_msg *)(bp + 4);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700746 if (ND_TTEST(rp->rm_direction)) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800747 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
748 if (dport == NFS_PORT && direction == SUNRPC_CALL) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700749 ND_PRINT((ndo, ": NFS request xid %u ", EXTRACT_32BITS(&rp->rm_xid)));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700750 nfsreq_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
JP Abgrall53f17a92014-02-12 14:02:41 -0800751 return;
752 }
753 if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700754 ND_PRINT((ndo, ": NFS reply xid %u ", EXTRACT_32BITS(&rp->rm_xid)));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700755 nfsreply_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
JP Abgrall53f17a92014-02-12 14:02:41 -0800756 return;
757 }
758 }
759 }
760
761 return;
762 bad:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700763 ND_PRINT((ndo, "[bad opt]"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800764 if (ch != '\0')
Elliott Hughes892a68b2015-10-19 14:43:53 -0700765 ND_PRINT((ndo, ">"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800766 return;
767 trunc:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700768 ND_PRINT((ndo, "[|tcp]"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800769 if (ch != '\0')
Elliott Hughes892a68b2015-10-19 14:43:53 -0700770 ND_PRINT((ndo, ">"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800771}
772
773/*
774 * RFC1122 says the following on data in RST segments:
775 *
776 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
777 *
778 * A TCP SHOULD allow a received RST segment to include data.
779 *
780 * DISCUSSION
781 * It has been suggested that a RST segment could contain
782 * ASCII text that encoded and explained the cause of the
783 * RST. No standard has yet been established for such
784 * data.
785 *
786 */
787
788static void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700789print_tcp_rst_data(netdissect_options *ndo,
790 register const u_char *sp, u_int length)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800791{
JP Abgrall53f17a92014-02-12 14:02:41 -0800792 int c;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800793
Elliott Hughes892a68b2015-10-19 14:43:53 -0700794 ND_PRINT((ndo, ND_TTEST2(*sp, length) ? " [RST" : " [!RST"));
JP Abgrall53f17a92014-02-12 14:02:41 -0800795 if (length > MAX_RST_DATA_LEN) {
796 length = MAX_RST_DATA_LEN; /* can use -X for longer */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700797 ND_PRINT((ndo, "+")); /* indicate we truncate */
JP Abgrall53f17a92014-02-12 14:02:41 -0800798 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700799 ND_PRINT((ndo, " "));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700800 while (length-- && sp < ndo->ndo_snapend) {
JP Abgrall53f17a92014-02-12 14:02:41 -0800801 c = *sp++;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700802 safeputchar(ndo, c);
JP Abgrall53f17a92014-02-12 14:02:41 -0800803 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700804 ND_PRINT((ndo, "]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800805}
806
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700807static void
808print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp,
809 u_int datalen, int exp)
810{
811 u_int i;
812
813 if (exp)
814 ND_PRINT((ndo, "tfo"));
815
816 if (datalen == 0) {
817 /* Fast Open Cookie Request */
818 ND_PRINT((ndo, " cookiereq"));
819 } else {
820 /* Fast Open Cookie */
821 if (datalen % 2 != 0 || datalen < 4 || datalen > 16) {
822 ND_PRINT((ndo, " invalid"));
823 } else {
824 ND_PRINT((ndo, " cookie "));
825 for (i = 0; i < datalen; ++i)
826 ND_PRINT((ndo, "%02x", cp[i]));
827 }
828 }
829}
830
The Android Open Source Project2949f582009-03-03 19:30:46 -0800831#ifdef HAVE_LIBCRYPTO
JP Abgrall53f17a92014-02-12 14:02:41 -0800832USES_APPLE_DEPRECATED_API
The Android Open Source Project2949f582009-03-03 19:30:46 -0800833static int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700834tcp_verify_signature(netdissect_options *ndo,
835 const struct ip *ip, const struct tcphdr *tp,
JP Abgrall53f17a92014-02-12 14:02:41 -0800836 const u_char *data, int length, const u_char *rcvsig)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800837{
838 struct tcphdr tp1;
JP Abgrall53f17a92014-02-12 14:02:41 -0800839 u_char sig[TCP_SIGLEN];
840 char zero_proto = 0;
841 MD5_CTX ctx;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700842 uint16_t savecsum, tlen;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700843 const struct ip6_hdr *ip6;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700844 uint32_t len32;
845 uint8_t nxt;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800846
Elliott Hughes892a68b2015-10-19 14:43:53 -0700847 if (data + length > ndo->ndo_snapend) {
848 ND_PRINT((ndo, "snaplen too short, "));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800849 return (CANT_CHECK_SIGNATURE);
JP Abgrall53f17a92014-02-12 14:02:41 -0800850 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800851
JP Abgrall53f17a92014-02-12 14:02:41 -0800852 tp1 = *tp;
853
Elliott Hughes892a68b2015-10-19 14:43:53 -0700854 if (ndo->ndo_sigsecret == NULL) {
855 ND_PRINT((ndo, "shared secret not supplied with -M, "));
JP Abgrall53f17a92014-02-12 14:02:41 -0800856 return (CANT_CHECK_SIGNATURE);
857 }
858
859 MD5_Init(&ctx);
860 /*
861 * Step 1: Update MD5 hash with IP pseudo-header.
862 */
863 if (IP_V(ip) == 4) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700864 MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src));
865 MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst));
866 MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto));
867 MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p));
JP Abgrall53f17a92014-02-12 14:02:41 -0800868 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
869 tlen = htons(tlen);
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700870 MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen));
JP Abgrall53f17a92014-02-12 14:02:41 -0800871 } else if (IP_V(ip) == 6) {
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700872 ip6 = (const struct ip6_hdr *)ip;
873 MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
874 MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
JP Abgrall53f17a92014-02-12 14:02:41 -0800875 len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen));
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700876 MD5_Update(&ctx, (const char *)&len32, sizeof(len32));
JP Abgrall53f17a92014-02-12 14:02:41 -0800877 nxt = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700878 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
879 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
880 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
JP Abgrall53f17a92014-02-12 14:02:41 -0800881 nxt = IPPROTO_TCP;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700882 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
JP Abgrall53f17a92014-02-12 14:02:41 -0800883 } else {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700884 ND_PRINT((ndo, "IP version not 4 or 6, "));
JP Abgrall53f17a92014-02-12 14:02:41 -0800885 return (CANT_CHECK_SIGNATURE);
886 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800887
JP Abgrall53f17a92014-02-12 14:02:41 -0800888 /*
889 * Step 2: Update MD5 hash with TCP header, excluding options.
890 * The TCP checksum must be set to zero.
891 */
892 savecsum = tp1.th_sum;
893 tp1.th_sum = 0;
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700894 MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr));
JP Abgrall53f17a92014-02-12 14:02:41 -0800895 tp1.th_sum = savecsum;
896 /*
897 * Step 3: Update MD5 hash with TCP segment data, if present.
898 */
899 if (length > 0)
900 MD5_Update(&ctx, data, length);
901 /*
902 * Step 4: Update MD5 hash with shared secret.
903 */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700904 MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret));
JP Abgrall53f17a92014-02-12 14:02:41 -0800905 MD5_Final(sig, &ctx);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800906
JP Abgrall53f17a92014-02-12 14:02:41 -0800907 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
908 return (SIGNATURE_VALID);
909 else
910 return (SIGNATURE_INVALID);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800911}
JP Abgrall53f17a92014-02-12 14:02:41 -0800912USES_APPLE_RST
The Android Open Source Project2949f582009-03-03 19:30:46 -0800913#endif /* HAVE_LIBCRYPTO */
JP Abgrall53f17a92014-02-12 14:02:41 -0800914
915/*
916 * Local Variables:
917 * c-style: whitesmith
918 * c-basic-offset: 8
919 * End:
920 */