xref: /freebsd/contrib/tcpdump/print-pfsync.c (revision 4bf98559d9d6fa7c3571d26ed6f2b18823e3a30b)
1ffe9c13eSGleb Smirnoff /*
2ffe9c13eSGleb Smirnoff  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
3ffe9c13eSGleb Smirnoff  * Copyright (c) 2002 Michael Shalayeff
4ffe9c13eSGleb Smirnoff  * Copyright (c) 2001 Daniel Hartmeier
5ffe9c13eSGleb Smirnoff  * All rights reserved.
6ffe9c13eSGleb Smirnoff  *
7ffe9c13eSGleb Smirnoff  * Redistribution and use in source and binary forms, with or without
8ffe9c13eSGleb Smirnoff  * modification, are permitted provided that the following conditions
9ffe9c13eSGleb Smirnoff  * are met:
10ffe9c13eSGleb Smirnoff  * 1. Redistributions of source code must retain the above copyright
11ffe9c13eSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer.
12ffe9c13eSGleb Smirnoff  * 2. Redistributions in binary form must reproduce the above copyright
13ffe9c13eSGleb Smirnoff  *    notice, this list of conditions and the following disclaimer in the
14ffe9c13eSGleb Smirnoff  *    documentation and/or other materials provided with the distribution.
15ffe9c13eSGleb Smirnoff  *
16ffe9c13eSGleb Smirnoff  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17ffe9c13eSGleb Smirnoff  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18ffe9c13eSGleb Smirnoff  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19ffe9c13eSGleb Smirnoff  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20ffe9c13eSGleb Smirnoff  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21ffe9c13eSGleb Smirnoff  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22ffe9c13eSGleb Smirnoff  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ffe9c13eSGleb Smirnoff  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24ffe9c13eSGleb Smirnoff  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25ffe9c13eSGleb Smirnoff  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26ffe9c13eSGleb Smirnoff  * THE POSSIBILITY OF SUCH DAMAGE.
27ffe9c13eSGleb Smirnoff  *
28ffe9c13eSGleb Smirnoff  * $OpenBSD: print-pfsync.c,v 1.38 2012/09/19 13:50:36 mikeb Exp $
29ffe9c13eSGleb Smirnoff  * $OpenBSD: pf_print_state.c,v 1.11 2012/07/08 17:48:37 lteo Exp $
30ffe9c13eSGleb Smirnoff  */
31ffe9c13eSGleb Smirnoff 
32ffe9c13eSGleb Smirnoff #ifdef HAVE_CONFIG_H
33ffe9c13eSGleb Smirnoff #include "config.h"
34ffe9c13eSGleb Smirnoff #endif
35ffe9c13eSGleb Smirnoff 
363340d773SGleb Smirnoff #ifndef HAVE_NET_PFVAR_H
373340d773SGleb Smirnoff #error "No pf headers available"
383340d773SGleb Smirnoff #endif
39ffe9c13eSGleb Smirnoff #include <sys/endian.h>
40ffe9c13eSGleb Smirnoff #include <net/if.h>
413340d773SGleb Smirnoff #include <net/pfvar.h>
42ffe9c13eSGleb Smirnoff #include <net/if_pfsync.h>
43ffe9c13eSGleb Smirnoff #define	TCPSTATES
44ffe9c13eSGleb Smirnoff #include <netinet/tcp_fsm.h>
45ffe9c13eSGleb Smirnoff 
463340d773SGleb Smirnoff #include <netdissect-stdinc.h>
47ffe9c13eSGleb Smirnoff #include <string.h>
48ffe9c13eSGleb Smirnoff 
493340d773SGleb Smirnoff #include "netdissect.h"
50ffe9c13eSGleb Smirnoff #include "interface.h"
51ffe9c13eSGleb Smirnoff #include "addrtoname.h"
52ffe9c13eSGleb Smirnoff 
5304c53351SBrooks Davis static void	pfsync_print(netdissect_options *, struct pfsync_header *,
5404c53351SBrooks Davis 		    const u_char *, u_int);
5504c53351SBrooks Davis static void	print_src_dst(netdissect_options *,
5604c53351SBrooks Davis 		    const struct pfsync_state_peer *,
57ffe9c13eSGleb Smirnoff 		    const struct pfsync_state_peer *, uint8_t);
58*4bf98559SKajetan Staszkiewicz static void	print_state(netdissect_options *, union pfsync_state_union *, int);
59ffe9c13eSGleb Smirnoff 
603f240bdfSLuiz Otavio O Souza u_int
613f240bdfSLuiz Otavio O Souza pfsync_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
62ffe9c13eSGleb Smirnoff     register const u_char *p)
63ffe9c13eSGleb Smirnoff {
64ffe9c13eSGleb Smirnoff 	u_int caplen = h->caplen;
65ffe9c13eSGleb Smirnoff 
663f240bdfSLuiz Otavio O Souza 	ts_print(ndo, &h->ts);
67ffe9c13eSGleb Smirnoff 
68ffe9c13eSGleb Smirnoff 	if (caplen < PFSYNC_HDRLEN) {
6904c53351SBrooks Davis 		ND_PRINT((ndo, "[|pfsync]"));
70ffe9c13eSGleb Smirnoff 		goto out;
71ffe9c13eSGleb Smirnoff 	}
72ffe9c13eSGleb Smirnoff 
733f240bdfSLuiz Otavio O Souza 	pfsync_print(ndo, (struct pfsync_header *)p,
74ffe9c13eSGleb Smirnoff 	    p + sizeof(struct pfsync_header),
75ffe9c13eSGleb Smirnoff 	    caplen - sizeof(struct pfsync_header));
76ffe9c13eSGleb Smirnoff out:
773f240bdfSLuiz Otavio O Souza 	if (ndo->ndo_xflag) {
783f240bdfSLuiz Otavio O Souza 		hex_print(ndo, "\n\t", p, caplen);
79ffe9c13eSGleb Smirnoff 	}
8004c53351SBrooks Davis 	safeputchar(ndo, '\n');
813f240bdfSLuiz Otavio O Souza 	return (caplen);
82ffe9c13eSGleb Smirnoff }
83ffe9c13eSGleb Smirnoff 
84ffe9c13eSGleb Smirnoff void
8504c53351SBrooks Davis pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
86ffe9c13eSGleb Smirnoff {
87ffe9c13eSGleb Smirnoff 	struct pfsync_header *hdr = (struct pfsync_header *)bp;
88ffe9c13eSGleb Smirnoff 
89ffe9c13eSGleb Smirnoff 	if (len < PFSYNC_HDRLEN)
9004c53351SBrooks Davis 		ND_PRINT((ndo, "[|pfsync]"));
91ffe9c13eSGleb Smirnoff 	else
9204c53351SBrooks Davis 		pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
93ffe9c13eSGleb Smirnoff 		    len - sizeof(struct pfsync_header));
94ffe9c13eSGleb Smirnoff }
95ffe9c13eSGleb Smirnoff 
96ffe9c13eSGleb Smirnoff struct pfsync_actions {
97ffe9c13eSGleb Smirnoff 	const char *name;
98ffe9c13eSGleb Smirnoff 	size_t len;
9904c53351SBrooks Davis 	void (*print)(netdissect_options *, const void *);
100ffe9c13eSGleb Smirnoff };
101ffe9c13eSGleb Smirnoff 
10204c53351SBrooks Davis static void	pfsync_print_clr(netdissect_options *, const void *);
103*4bf98559SKajetan Staszkiewicz static void	pfsync_print_state_1301(netdissect_options *, const void *);
104*4bf98559SKajetan Staszkiewicz static void	pfsync_print_state_1400(netdissect_options *, const void *);
10504c53351SBrooks Davis static void	pfsync_print_ins_ack(netdissect_options *, const void *);
10604c53351SBrooks Davis static void	pfsync_print_upd_c(netdissect_options *, const void *);
10704c53351SBrooks Davis static void	pfsync_print_upd_req(netdissect_options *, const void *);
10804c53351SBrooks Davis static void	pfsync_print_del_c(netdissect_options *, const void *);
10904c53351SBrooks Davis static void	pfsync_print_bus(netdissect_options *, const void *);
11004c53351SBrooks Davis static void	pfsync_print_tdb(netdissect_options *, const void *);
111ffe9c13eSGleb Smirnoff 
112ffe9c13eSGleb Smirnoff struct pfsync_actions actions[] = {
113ffe9c13eSGleb Smirnoff 	{ "clear all", sizeof(struct pfsync_clr),	pfsync_print_clr },
114*4bf98559SKajetan Staszkiewicz 	{ "insert 13.1", sizeof(struct pfsync_state_1301),
115*4bf98559SKajetan Staszkiewicz 							pfsync_print_state_1301 },
116ffe9c13eSGleb Smirnoff 	{ "insert ack", sizeof(struct pfsync_ins_ack),	pfsync_print_ins_ack },
117*4bf98559SKajetan Staszkiewicz 	{ "update 13.1", sizeof(struct pfsync_state_1301),
118*4bf98559SKajetan Staszkiewicz 							pfsync_print_state_1301 },
119ffe9c13eSGleb Smirnoff 	{ "update compressed", sizeof(struct pfsync_upd_c),
120ffe9c13eSGleb Smirnoff 							pfsync_print_upd_c },
121ffe9c13eSGleb Smirnoff 	{ "request uncompressed", sizeof(struct pfsync_upd_req),
122ffe9c13eSGleb Smirnoff 							pfsync_print_upd_req },
123*4bf98559SKajetan Staszkiewicz 	{ "delete", sizeof(struct pfsync_state_1301),	pfsync_print_state_1301 },
124ffe9c13eSGleb Smirnoff 	{ "delete compressed", sizeof(struct pfsync_del_c),
125ffe9c13eSGleb Smirnoff 							pfsync_print_del_c },
126ffe9c13eSGleb Smirnoff 	{ "frag insert", 0,				NULL },
127ffe9c13eSGleb Smirnoff 	{ "frag delete", 0,				NULL },
128ffe9c13eSGleb Smirnoff 	{ "bulk update status", sizeof(struct pfsync_bus),
129ffe9c13eSGleb Smirnoff 							pfsync_print_bus },
130ffe9c13eSGleb Smirnoff 	{ "tdb", 0,					pfsync_print_tdb },
131ffe9c13eSGleb Smirnoff 	{ "eof", 0,					NULL },
132*4bf98559SKajetan Staszkiewicz 	{ "insert", sizeof(struct pfsync_state_1400),	pfsync_print_state_1400 },
133*4bf98559SKajetan Staszkiewicz 	{ "update", sizeof(struct pfsync_state_1400),	pfsync_print_state_1400 },
134ffe9c13eSGleb Smirnoff };
135ffe9c13eSGleb Smirnoff 
136ffe9c13eSGleb Smirnoff static void
13704c53351SBrooks Davis pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
13804c53351SBrooks Davis     const u_char *bp, u_int len)
139ffe9c13eSGleb Smirnoff {
140ffe9c13eSGleb Smirnoff 	struct pfsync_subheader *subh;
141ffe9c13eSGleb Smirnoff 	int count, plen, i;
142ffe9c13eSGleb Smirnoff 	u_int alen;
143ffe9c13eSGleb Smirnoff 
144ffe9c13eSGleb Smirnoff 	plen = ntohs(hdr->len);
145ffe9c13eSGleb Smirnoff 
14604c53351SBrooks Davis 	ND_PRINT((ndo, "PFSYNCv%d len %d", hdr->version, plen));
147ffe9c13eSGleb Smirnoff 
148ffe9c13eSGleb Smirnoff 	if (hdr->version != PFSYNC_VERSION)
149ffe9c13eSGleb Smirnoff 		return;
150ffe9c13eSGleb Smirnoff 
151ffe9c13eSGleb Smirnoff 	plen -= sizeof(*hdr);
152ffe9c13eSGleb Smirnoff 
153ffe9c13eSGleb Smirnoff 	while (plen > 0) {
154ffe9c13eSGleb Smirnoff 		if (len < sizeof(*subh))
155ffe9c13eSGleb Smirnoff 			break;
156ffe9c13eSGleb Smirnoff 
157ffe9c13eSGleb Smirnoff 		subh = (struct pfsync_subheader *)bp;
158ffe9c13eSGleb Smirnoff 		bp += sizeof(*subh);
159ffe9c13eSGleb Smirnoff 		len -= sizeof(*subh);
160ffe9c13eSGleb Smirnoff 		plen -= sizeof(*subh);
161ffe9c13eSGleb Smirnoff 
162ffe9c13eSGleb Smirnoff 		if (subh->action >= PFSYNC_ACT_MAX) {
16304c53351SBrooks Davis 			ND_PRINT((ndo, "\n    act UNKNOWN id %d",
16404c53351SBrooks Davis 			    subh->action));
165ffe9c13eSGleb Smirnoff 			return;
166ffe9c13eSGleb Smirnoff 		}
167ffe9c13eSGleb Smirnoff 
168ffe9c13eSGleb Smirnoff 		count = ntohs(subh->count);
16904c53351SBrooks Davis 		ND_PRINT((ndo, "\n    %s count %d", actions[subh->action].name,
17004c53351SBrooks Davis 		    count));
171ffe9c13eSGleb Smirnoff 		alen = actions[subh->action].len;
172ffe9c13eSGleb Smirnoff 
173ffe9c13eSGleb Smirnoff 		if (subh->action == PFSYNC_ACT_EOF)
174ffe9c13eSGleb Smirnoff 			return;
175ffe9c13eSGleb Smirnoff 
176ffe9c13eSGleb Smirnoff 		if (actions[subh->action].print == NULL) {
17704c53351SBrooks Davis 			ND_PRINT((ndo, "\n    unimplemented action %hhu",
17804c53351SBrooks Davis 			    subh->action));
179ffe9c13eSGleb Smirnoff 			return;
180ffe9c13eSGleb Smirnoff 		}
181ffe9c13eSGleb Smirnoff 
182ffe9c13eSGleb Smirnoff 		for (i = 0; i < count; i++) {
183ffe9c13eSGleb Smirnoff 			if (len < alen) {
184ffe9c13eSGleb Smirnoff 				len = 0;
185ffe9c13eSGleb Smirnoff 				break;
186ffe9c13eSGleb Smirnoff 			}
187ffe9c13eSGleb Smirnoff 
1883340d773SGleb Smirnoff 			if (ndo->ndo_vflag)
18904c53351SBrooks Davis 				actions[subh->action].print(ndo, bp);
190ffe9c13eSGleb Smirnoff 
191ffe9c13eSGleb Smirnoff 			bp += alen;
192ffe9c13eSGleb Smirnoff 			len -= alen;
193ffe9c13eSGleb Smirnoff 			plen -= alen;
194ffe9c13eSGleb Smirnoff 		}
195ffe9c13eSGleb Smirnoff 	}
196ffe9c13eSGleb Smirnoff 
197ffe9c13eSGleb Smirnoff 	if (plen > 0) {
19804c53351SBrooks Davis 		ND_PRINT((ndo, "\n    ..."));
199ffe9c13eSGleb Smirnoff 		return;
200ffe9c13eSGleb Smirnoff 	}
201ffe9c13eSGleb Smirnoff 	if (plen < 0) {
20204c53351SBrooks Davis 		ND_PRINT((ndo, "\n    invalid header length"));
203ffe9c13eSGleb Smirnoff 		return;
204ffe9c13eSGleb Smirnoff 	}
205ffe9c13eSGleb Smirnoff 	if (len > 0)
20604c53351SBrooks Davis 		ND_PRINT((ndo, "\n    invalid packet length"));
207ffe9c13eSGleb Smirnoff }
208ffe9c13eSGleb Smirnoff 
209ffe9c13eSGleb Smirnoff static void
21004c53351SBrooks Davis pfsync_print_clr(netdissect_options *ndo, const void *bp)
211ffe9c13eSGleb Smirnoff {
212ffe9c13eSGleb Smirnoff 	const struct pfsync_clr *clr = bp;
213ffe9c13eSGleb Smirnoff 
21404c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tcreatorid: %08x", htonl(clr->creatorid)));
215ffe9c13eSGleb Smirnoff 	if (clr->ifname[0] != '\0')
21604c53351SBrooks Davis 		ND_PRINT((ndo, " interface: %s", clr->ifname));
217ffe9c13eSGleb Smirnoff }
218ffe9c13eSGleb Smirnoff 
219ffe9c13eSGleb Smirnoff static void
220*4bf98559SKajetan Staszkiewicz pfsync_print_state_1301(netdissect_options *ndo, const void *bp)
221ffe9c13eSGleb Smirnoff {
222*4bf98559SKajetan Staszkiewicz 	struct pfsync_state_1301 *st = (struct pfsync_state_1301 *)bp;
223ffe9c13eSGleb Smirnoff 
22404c53351SBrooks Davis 	safeputchar(ndo, '\n');
225*4bf98559SKajetan Staszkiewicz 	print_state(ndo, (union pfsync_state_union *)st, PFSYNC_MSG_VERSION_1301);
226*4bf98559SKajetan Staszkiewicz }
227*4bf98559SKajetan Staszkiewicz 
228*4bf98559SKajetan Staszkiewicz static void
229*4bf98559SKajetan Staszkiewicz pfsync_print_state_1400(netdissect_options *ndo, const void *bp)
230*4bf98559SKajetan Staszkiewicz {
231*4bf98559SKajetan Staszkiewicz 	struct pfsync_state_1301 *st = (struct pfsync_state_1301 *)bp;
232*4bf98559SKajetan Staszkiewicz 
233*4bf98559SKajetan Staszkiewicz 	safeputchar(ndo, '\n');
234*4bf98559SKajetan Staszkiewicz 	print_state(ndo, (union pfsync_state_union *)st, PFSYNC_MSG_VERSION_1400);
235ffe9c13eSGleb Smirnoff }
236ffe9c13eSGleb Smirnoff 
237ffe9c13eSGleb Smirnoff static void
23804c53351SBrooks Davis pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
239ffe9c13eSGleb Smirnoff {
240ffe9c13eSGleb Smirnoff 	const struct pfsync_ins_ack *iack = bp;
241ffe9c13eSGleb Smirnoff 
24204c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
24304c53351SBrooks Davis 	    (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid)));
244ffe9c13eSGleb Smirnoff }
245ffe9c13eSGleb Smirnoff 
246ffe9c13eSGleb Smirnoff static void
24704c53351SBrooks Davis pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
248ffe9c13eSGleb Smirnoff {
249ffe9c13eSGleb Smirnoff 	const struct pfsync_upd_c *u = bp;
250ffe9c13eSGleb Smirnoff 
25104c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
25204c53351SBrooks Davis 	    (uintmax_t)be64toh(u->id), ntohl(u->creatorid)));
2533340d773SGleb Smirnoff 	if (ndo->ndo_vflag > 2) {
25404c53351SBrooks Davis 		ND_PRINT((ndo, "\n\tTCP? :"));
25504c53351SBrooks Davis 		print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
256ffe9c13eSGleb Smirnoff 	}
257ffe9c13eSGleb Smirnoff }
258ffe9c13eSGleb Smirnoff 
259ffe9c13eSGleb Smirnoff static void
26004c53351SBrooks Davis pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
261ffe9c13eSGleb Smirnoff {
262ffe9c13eSGleb Smirnoff 	const struct pfsync_upd_req *ur = bp;
263ffe9c13eSGleb Smirnoff 
26404c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
26504c53351SBrooks Davis 	    (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid)));
266ffe9c13eSGleb Smirnoff }
267ffe9c13eSGleb Smirnoff 
268ffe9c13eSGleb Smirnoff static void
26904c53351SBrooks Davis pfsync_print_del_c(netdissect_options *ndo, const void *bp)
270ffe9c13eSGleb Smirnoff {
271ffe9c13eSGleb Smirnoff 	const struct pfsync_del_c *d = bp;
272ffe9c13eSGleb Smirnoff 
27304c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
27404c53351SBrooks Davis 	    (uintmax_t)be64toh(d->id), ntohl(d->creatorid)));
275ffe9c13eSGleb Smirnoff }
276ffe9c13eSGleb Smirnoff 
277ffe9c13eSGleb Smirnoff static void
27804c53351SBrooks Davis pfsync_print_bus(netdissect_options *ndo, const void *bp)
279ffe9c13eSGleb Smirnoff {
280ffe9c13eSGleb Smirnoff 	const struct pfsync_bus *b = bp;
281ffe9c13eSGleb Smirnoff 	uint32_t endtime;
282ffe9c13eSGleb Smirnoff 	int min, sec;
283ffe9c13eSGleb Smirnoff 	const char *status;
284ffe9c13eSGleb Smirnoff 
285ffe9c13eSGleb Smirnoff 	endtime = ntohl(b->endtime);
286ffe9c13eSGleb Smirnoff 	sec = endtime % 60;
287ffe9c13eSGleb Smirnoff 	endtime /= 60;
288ffe9c13eSGleb Smirnoff 	min = endtime % 60;
289ffe9c13eSGleb Smirnoff 	endtime /= 60;
290ffe9c13eSGleb Smirnoff 
291ffe9c13eSGleb Smirnoff 	switch (b->status) {
292ffe9c13eSGleb Smirnoff 	case PFSYNC_BUS_START:
293ffe9c13eSGleb Smirnoff 		status = "start";
294ffe9c13eSGleb Smirnoff 		break;
295ffe9c13eSGleb Smirnoff 	case PFSYNC_BUS_END:
296ffe9c13eSGleb Smirnoff 		status = "end";
297ffe9c13eSGleb Smirnoff 		break;
298ffe9c13eSGleb Smirnoff 	default:
299ffe9c13eSGleb Smirnoff 		status = "UNKNOWN";
300ffe9c13eSGleb Smirnoff 		break;
301ffe9c13eSGleb Smirnoff 	}
302ffe9c13eSGleb Smirnoff 
30304c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
30404c53351SBrooks Davis 	    htonl(b->creatorid), endtime, min, sec, status));
305ffe9c13eSGleb Smirnoff }
306ffe9c13eSGleb Smirnoff 
307ffe9c13eSGleb Smirnoff static void
30804c53351SBrooks Davis pfsync_print_tdb(netdissect_options *ndo, const void *bp)
309ffe9c13eSGleb Smirnoff {
310ffe9c13eSGleb Smirnoff 	const struct pfsync_tdb *t = bp;
311ffe9c13eSGleb Smirnoff 
31204c53351SBrooks Davis 	ND_PRINT((ndo, "\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
313ffe9c13eSGleb Smirnoff 	    ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
31404c53351SBrooks Davis 	    (uintmax_t )be64toh(t->cur_bytes)));
315ffe9c13eSGleb Smirnoff }
316ffe9c13eSGleb Smirnoff 
317ffe9c13eSGleb Smirnoff static void
31804c53351SBrooks Davis print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
31904c53351SBrooks Davis     sa_family_t af, const char *proto)
320ffe9c13eSGleb Smirnoff {
321ffe9c13eSGleb Smirnoff 	char buf[48];
322ffe9c13eSGleb Smirnoff 
323ffe9c13eSGleb Smirnoff 	if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
32404c53351SBrooks Davis 		ND_PRINT((ndo, "?"));
325ffe9c13eSGleb Smirnoff 	else
32604c53351SBrooks Davis 		ND_PRINT((ndo, "%s", buf));
327ffe9c13eSGleb Smirnoff 
328ffe9c13eSGleb Smirnoff 	if (port)
32904c53351SBrooks Davis 		ND_PRINT((ndo, ".%hu", ntohs(port)));
330ffe9c13eSGleb Smirnoff }
331ffe9c13eSGleb Smirnoff 
332ffe9c13eSGleb Smirnoff static void
33304c53351SBrooks Davis print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
334ffe9c13eSGleb Smirnoff {
335ffe9c13eSGleb Smirnoff 	if (p->seqdiff)
33604c53351SBrooks Davis 		ND_PRINT((ndo, "[%u + %u](+%u)", ntohl(p->seqlo),
33704c53351SBrooks Davis 		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)));
338ffe9c13eSGleb Smirnoff 	else
33904c53351SBrooks Davis 		ND_PRINT((ndo, "[%u + %u]", ntohl(p->seqlo),
34004c53351SBrooks Davis 		    ntohl(p->seqhi) - ntohl(p->seqlo)));
341ffe9c13eSGleb Smirnoff }
342ffe9c13eSGleb Smirnoff 
343ffe9c13eSGleb Smirnoff static void
34404c53351SBrooks Davis print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
345ffe9c13eSGleb Smirnoff     const struct pfsync_state_peer *dst, uint8_t proto)
346ffe9c13eSGleb Smirnoff {
347ffe9c13eSGleb Smirnoff 
348ffe9c13eSGleb Smirnoff 	if (proto == IPPROTO_TCP) {
349ffe9c13eSGleb Smirnoff 		if (src->state <= TCPS_TIME_WAIT &&
350ffe9c13eSGleb Smirnoff 		    dst->state <= TCPS_TIME_WAIT)
35104c53351SBrooks Davis 			ND_PRINT((ndo, "   %s:%s", tcpstates[src->state],
35204c53351SBrooks Davis 			    tcpstates[dst->state]));
353ffe9c13eSGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_SRC ||
354ffe9c13eSGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_SRC)
35504c53351SBrooks Davis 			ND_PRINT((ndo, "   PROXY:SRC"));
356ffe9c13eSGleb Smirnoff 		else if (src->state == PF_TCPS_PROXY_DST ||
357ffe9c13eSGleb Smirnoff 		    dst->state == PF_TCPS_PROXY_DST)
35804c53351SBrooks Davis 			ND_PRINT((ndo, "   PROXY:DST"));
359ffe9c13eSGleb Smirnoff 		else
36004c53351SBrooks Davis 			ND_PRINT((ndo, "   <BAD STATE LEVELS %u:%u>",
36104c53351SBrooks Davis 			    src->state, dst->state));
3623340d773SGleb Smirnoff 		if (ndo->ndo_vflag > 1) {
36304c53351SBrooks Davis 			ND_PRINT((ndo, "\n\t"));
36404c53351SBrooks Davis 			print_seq(ndo, src);
365ffe9c13eSGleb Smirnoff 			if (src->wscale && dst->wscale)
36604c53351SBrooks Davis 				ND_PRINT((ndo, " wscale %u",
36704c53351SBrooks Davis 				    src->wscale & PF_WSCALE_MASK));
36804c53351SBrooks Davis 			ND_PRINT((ndo, "  "));
36904c53351SBrooks Davis 			print_seq(ndo, dst);
370ffe9c13eSGleb Smirnoff 			if (src->wscale && dst->wscale)
37104c53351SBrooks Davis 				ND_PRINT((ndo, " wscale %u",
37204c53351SBrooks Davis 				    dst->wscale & PF_WSCALE_MASK));
373ffe9c13eSGleb Smirnoff 		}
374ffe9c13eSGleb Smirnoff 	} else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
375ffe9c13eSGleb Smirnoff 	    dst->state < PFUDPS_NSTATES) {
376ffe9c13eSGleb Smirnoff 		const char *states[] = PFUDPS_NAMES;
377ffe9c13eSGleb Smirnoff 
37804c53351SBrooks Davis 		ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
379ffe9c13eSGleb Smirnoff 	} else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
380ffe9c13eSGleb Smirnoff 	    dst->state < PFOTHERS_NSTATES) {
381ffe9c13eSGleb Smirnoff 		/* XXX ICMP doesn't really have state levels */
382ffe9c13eSGleb Smirnoff 		const char *states[] = PFOTHERS_NAMES;
383ffe9c13eSGleb Smirnoff 
38404c53351SBrooks Davis 		ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
385ffe9c13eSGleb Smirnoff 	} else {
38604c53351SBrooks Davis 		ND_PRINT((ndo, "   %u:%u", src->state, dst->state));
387ffe9c13eSGleb Smirnoff 	}
388ffe9c13eSGleb Smirnoff }
389ffe9c13eSGleb Smirnoff 
390ffe9c13eSGleb Smirnoff static void
391*4bf98559SKajetan Staszkiewicz print_state(netdissect_options *ndo, union pfsync_state_union *s, int version)
392ffe9c13eSGleb Smirnoff {
393ffe9c13eSGleb Smirnoff 	struct pfsync_state_peer *src, *dst;
394ffe9c13eSGleb Smirnoff 	struct pfsync_state_key *sk, *nk;
395ffe9c13eSGleb Smirnoff 	int min, sec;
396ffe9c13eSGleb Smirnoff 
397*4bf98559SKajetan Staszkiewicz 	if (s->pfs_1301.direction == PF_OUT) {
398*4bf98559SKajetan Staszkiewicz 		src = &s->pfs_1301.src;
399*4bf98559SKajetan Staszkiewicz 		dst = &s->pfs_1301.dst;
400*4bf98559SKajetan Staszkiewicz 		sk = &s->pfs_1301.key[PF_SK_STACK];
401*4bf98559SKajetan Staszkiewicz 		nk = &s->pfs_1301.key[PF_SK_WIRE];
402*4bf98559SKajetan Staszkiewicz 		if (s->pfs_1301.proto == IPPROTO_ICMP || s->pfs_1301.proto == IPPROTO_ICMPV6)
403ffe9c13eSGleb Smirnoff 			sk->port[0] = nk->port[0];
404ffe9c13eSGleb Smirnoff 	} else {
405*4bf98559SKajetan Staszkiewicz 		src = &s->pfs_1301.dst;
406*4bf98559SKajetan Staszkiewicz 		dst = &s->pfs_1301.src;
407*4bf98559SKajetan Staszkiewicz 		sk = &s->pfs_1301.key[PF_SK_WIRE];
408*4bf98559SKajetan Staszkiewicz 		nk = &s->pfs_1301.key[PF_SK_STACK];
409*4bf98559SKajetan Staszkiewicz 		if (s->pfs_1301.proto == IPPROTO_ICMP || s->pfs_1301.proto == IPPROTO_ICMPV6)
410ffe9c13eSGleb Smirnoff 			sk->port[1] = nk->port[1];
411ffe9c13eSGleb Smirnoff 	}
412*4bf98559SKajetan Staszkiewicz 	ND_PRINT((ndo, "\t%s ", s->pfs_1301.ifname));
413*4bf98559SKajetan Staszkiewicz 	ND_PRINT((ndo, "proto %u ", s->pfs_1301.proto));
414ffe9c13eSGleb Smirnoff 
415*4bf98559SKajetan Staszkiewicz 	print_host(ndo, &nk->addr[1], nk->port[1], s->pfs_1301.af, NULL);
416*4bf98559SKajetan Staszkiewicz 	if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->pfs_1301.af) ||
417ffe9c13eSGleb Smirnoff 	    nk->port[1] != sk->port[1]) {
41804c53351SBrooks Davis 		ND_PRINT((ndo, " ("));
419*4bf98559SKajetan Staszkiewicz 		print_host(ndo, &sk->addr[1], sk->port[1], s->pfs_1301.af, NULL);
42004c53351SBrooks Davis 		ND_PRINT((ndo, ")"));
421ffe9c13eSGleb Smirnoff 	}
422*4bf98559SKajetan Staszkiewicz 	if (s->pfs_1301.direction == PF_OUT)
42304c53351SBrooks Davis 		ND_PRINT((ndo, " -> "));
424ffe9c13eSGleb Smirnoff 	else
42504c53351SBrooks Davis 		ND_PRINT((ndo, " <- "));
426*4bf98559SKajetan Staszkiewicz 	print_host(ndo, &nk->addr[0], nk->port[0], s->pfs_1301.af, NULL);
427*4bf98559SKajetan Staszkiewicz 	if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->pfs_1301.af) ||
428ffe9c13eSGleb Smirnoff 	    nk->port[0] != sk->port[0]) {
42904c53351SBrooks Davis 		ND_PRINT((ndo, " ("));
430*4bf98559SKajetan Staszkiewicz 		print_host(ndo, &sk->addr[0], sk->port[0], s->pfs_1301.af, NULL);
43104c53351SBrooks Davis 		ND_PRINT((ndo, ")"));
432ffe9c13eSGleb Smirnoff 	}
433ffe9c13eSGleb Smirnoff 
434*4bf98559SKajetan Staszkiewicz 	print_src_dst(ndo, src, dst, s->pfs_1301.proto);
435ffe9c13eSGleb Smirnoff 
4363340d773SGleb Smirnoff 	if (ndo->ndo_vflag > 1) {
437ffe9c13eSGleb Smirnoff 		uint64_t packets[2];
438ffe9c13eSGleb Smirnoff 		uint64_t bytes[2];
439*4bf98559SKajetan Staszkiewicz 		uint32_t creation = ntohl(s->pfs_1301.creation);
440*4bf98559SKajetan Staszkiewicz 		uint32_t expire = ntohl(s->pfs_1301.expire);
441ffe9c13eSGleb Smirnoff 
442ffe9c13eSGleb Smirnoff 		sec = creation % 60;
443ffe9c13eSGleb Smirnoff 		creation /= 60;
444ffe9c13eSGleb Smirnoff 		min = creation % 60;
445ffe9c13eSGleb Smirnoff 		creation /= 60;
44604c53351SBrooks Davis 		ND_PRINT((ndo, "\n\tage %.2u:%.2u:%.2u", creation, min, sec));
447ffe9c13eSGleb Smirnoff 		sec = expire % 60;
448ffe9c13eSGleb Smirnoff 		expire /= 60;
449ffe9c13eSGleb Smirnoff 		min = expire % 60;
450ffe9c13eSGleb Smirnoff 		expire /= 60;
45104c53351SBrooks Davis 		ND_PRINT((ndo, ", expires in %.2u:%.2u:%.2u", expire, min, sec));
452ffe9c13eSGleb Smirnoff 
453*4bf98559SKajetan Staszkiewicz 		bcopy(s->pfs_1301.packets[0], &packets[0], sizeof(uint64_t));
454*4bf98559SKajetan Staszkiewicz 		bcopy(s->pfs_1301.packets[1], &packets[1], sizeof(uint64_t));
455*4bf98559SKajetan Staszkiewicz 		bcopy(s->pfs_1301.bytes[0], &bytes[0], sizeof(uint64_t));
456*4bf98559SKajetan Staszkiewicz 		bcopy(s->pfs_1301.bytes[1], &bytes[1], sizeof(uint64_t));
45704c53351SBrooks Davis 		ND_PRINT((ndo, ", %ju:%ju pkts, %ju:%ju bytes",
458ffe9c13eSGleb Smirnoff 		    be64toh(packets[0]), be64toh(packets[1]),
45904c53351SBrooks Davis 		    be64toh(bytes[0]), be64toh(bytes[1])));
460*4bf98559SKajetan Staszkiewicz 		if (s->pfs_1301.anchor != ntohl(-1))
461*4bf98559SKajetan Staszkiewicz 			ND_PRINT((ndo, ", anchor %u", ntohl(s->pfs_1301.anchor)));
462*4bf98559SKajetan Staszkiewicz 		if (s->pfs_1301.rule != ntohl(-1))
463*4bf98559SKajetan Staszkiewicz 			ND_PRINT((ndo, ", rule %u", ntohl(s->pfs_1301.rule)));
464ffe9c13eSGleb Smirnoff 	}
4653340d773SGleb Smirnoff 	if (ndo->ndo_vflag > 1) {
466ffe9c13eSGleb Smirnoff 		uint64_t id;
467ffe9c13eSGleb Smirnoff 
468*4bf98559SKajetan Staszkiewicz 		bcopy(&s->pfs_1301.id, &id, sizeof(uint64_t));
46904c53351SBrooks Davis 		ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
470*4bf98559SKajetan Staszkiewicz 		    (uintmax_t )be64toh(id), ntohl(s->pfs_1301.creatorid)));
471ffe9c13eSGleb Smirnoff 	}
472ffe9c13eSGleb Smirnoff }
473