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);
584bf98559SKajetan Staszkiewicz static void print_state(netdissect_options *, union pfsync_state_union *, int);
59ffe9c13eSGleb Smirnoff
601ad8d2eeSJoseph Mingrone void
pfsync_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,register const u_char * p)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) {
69ee67461eSJoseph Mingrone ND_PRINT("[|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 }
80ee67461eSJoseph Mingrone fn_print_char(ndo, '\n');
811ad8d2eeSJoseph Mingrone return;
82ffe9c13eSGleb Smirnoff }
83ffe9c13eSGleb Smirnoff
84ffe9c13eSGleb Smirnoff void
pfsync_ip_print(netdissect_options * ndo,const u_char * bp,u_int len)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
89*4848eb3aSKristof Provost if (len < PFSYNC_HDRLEN || !ND_TTEST_LEN(bp, len))
90ee67461eSJoseph Mingrone ND_PRINT("[|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 *);
1034bf98559SKajetan Staszkiewicz static void pfsync_print_state_1301(netdissect_options *, const void *);
1044bf98559SKajetan 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 },
1144bf98559SKajetan Staszkiewicz { "insert 13.1", sizeof(struct pfsync_state_1301),
1154bf98559SKajetan Staszkiewicz pfsync_print_state_1301 },
116ffe9c13eSGleb Smirnoff { "insert ack", sizeof(struct pfsync_ins_ack), pfsync_print_ins_ack },
1174bf98559SKajetan Staszkiewicz { "update 13.1", sizeof(struct pfsync_state_1301),
1184bf98559SKajetan 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 },
1234bf98559SKajetan 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 },
1324bf98559SKajetan Staszkiewicz { "insert", sizeof(struct pfsync_state_1400), pfsync_print_state_1400 },
1334bf98559SKajetan Staszkiewicz { "update", sizeof(struct pfsync_state_1400), pfsync_print_state_1400 },
134ffe9c13eSGleb Smirnoff };
135ffe9c13eSGleb Smirnoff
136ffe9c13eSGleb Smirnoff static void
pfsync_print(netdissect_options * ndo,struct pfsync_header * hdr,const u_char * bp,u_int len)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
146ee67461eSJoseph Mingrone ND_PRINT("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) {
163ee67461eSJoseph Mingrone ND_PRINT("\n act UNKNOWN id %d",
164ee67461eSJoseph Mingrone subh->action);
165ffe9c13eSGleb Smirnoff return;
166ffe9c13eSGleb Smirnoff }
167ffe9c13eSGleb Smirnoff
168ffe9c13eSGleb Smirnoff count = ntohs(subh->count);
169ee67461eSJoseph Mingrone ND_PRINT("\n %s count %d", actions[subh->action].name,
170ee67461eSJoseph Mingrone 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) {
177ee67461eSJoseph Mingrone ND_PRINT("\n unimplemented action %hhu",
178ee67461eSJoseph Mingrone 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) {
198ee67461eSJoseph Mingrone ND_PRINT("\n ...");
199ffe9c13eSGleb Smirnoff return;
200ffe9c13eSGleb Smirnoff }
201ffe9c13eSGleb Smirnoff if (plen < 0) {
202ee67461eSJoseph Mingrone ND_PRINT("\n invalid header length");
203ffe9c13eSGleb Smirnoff return;
204ffe9c13eSGleb Smirnoff }
205ffe9c13eSGleb Smirnoff if (len > 0)
206ee67461eSJoseph Mingrone ND_PRINT("\n invalid packet length");
207ffe9c13eSGleb Smirnoff }
208ffe9c13eSGleb Smirnoff
209ffe9c13eSGleb Smirnoff static void
pfsync_print_clr(netdissect_options * ndo,const void * bp)21004c53351SBrooks Davis pfsync_print_clr(netdissect_options *ndo, const void *bp)
211ffe9c13eSGleb Smirnoff {
212ffe9c13eSGleb Smirnoff const struct pfsync_clr *clr = bp;
213ffe9c13eSGleb Smirnoff
214ee67461eSJoseph Mingrone ND_PRINT("\n\tcreatorid: %08x", htonl(clr->creatorid));
215ffe9c13eSGleb Smirnoff if (clr->ifname[0] != '\0')
216ee67461eSJoseph Mingrone ND_PRINT(" interface: %s", clr->ifname);
217ffe9c13eSGleb Smirnoff }
218ffe9c13eSGleb Smirnoff
219ffe9c13eSGleb Smirnoff static void
pfsync_print_state_1301(netdissect_options * ndo,const void * bp)2204bf98559SKajetan Staszkiewicz pfsync_print_state_1301(netdissect_options *ndo, const void *bp)
221ffe9c13eSGleb Smirnoff {
2224bf98559SKajetan Staszkiewicz struct pfsync_state_1301 *st = (struct pfsync_state_1301 *)bp;
223ffe9c13eSGleb Smirnoff
224ee67461eSJoseph Mingrone fn_print_char(ndo, '\n');
2254bf98559SKajetan Staszkiewicz print_state(ndo, (union pfsync_state_union *)st, PFSYNC_MSG_VERSION_1301);
2264bf98559SKajetan Staszkiewicz }
2274bf98559SKajetan Staszkiewicz
2284bf98559SKajetan Staszkiewicz static void
pfsync_print_state_1400(netdissect_options * ndo,const void * bp)2294bf98559SKajetan Staszkiewicz pfsync_print_state_1400(netdissect_options *ndo, const void *bp)
2304bf98559SKajetan Staszkiewicz {
2314bf98559SKajetan Staszkiewicz struct pfsync_state_1301 *st = (struct pfsync_state_1301 *)bp;
2324bf98559SKajetan Staszkiewicz
233ee67461eSJoseph Mingrone fn_print_char(ndo, '\n');
2344bf98559SKajetan Staszkiewicz print_state(ndo, (union pfsync_state_union *)st, PFSYNC_MSG_VERSION_1400);
235ffe9c13eSGleb Smirnoff }
236ffe9c13eSGleb Smirnoff
237ffe9c13eSGleb Smirnoff static void
pfsync_print_ins_ack(netdissect_options * ndo,const void * bp)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
242ee67461eSJoseph Mingrone ND_PRINT("\n\tid: %016jx creatorid: %08x",
243ee67461eSJoseph Mingrone (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid));
244ffe9c13eSGleb Smirnoff }
245ffe9c13eSGleb Smirnoff
246ffe9c13eSGleb Smirnoff static void
pfsync_print_upd_c(netdissect_options * ndo,const void * bp)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
251ee67461eSJoseph Mingrone ND_PRINT("\n\tid: %016jx creatorid: %08x",
252ee67461eSJoseph Mingrone (uintmax_t)be64toh(u->id), ntohl(u->creatorid));
2533340d773SGleb Smirnoff if (ndo->ndo_vflag > 2) {
254ee67461eSJoseph Mingrone ND_PRINT("\n\tTCP? :");
25504c53351SBrooks Davis print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
256ffe9c13eSGleb Smirnoff }
257ffe9c13eSGleb Smirnoff }
258ffe9c13eSGleb Smirnoff
259ffe9c13eSGleb Smirnoff static void
pfsync_print_upd_req(netdissect_options * ndo,const void * bp)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
264ee67461eSJoseph Mingrone ND_PRINT("\n\tid: %016jx creatorid: %08x",
265ee67461eSJoseph Mingrone (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid));
266ffe9c13eSGleb Smirnoff }
267ffe9c13eSGleb Smirnoff
268ffe9c13eSGleb Smirnoff static void
pfsync_print_del_c(netdissect_options * ndo,const void * bp)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
273ee67461eSJoseph Mingrone ND_PRINT("\n\tid: %016jx creatorid: %08x",
274ee67461eSJoseph Mingrone (uintmax_t)be64toh(d->id), ntohl(d->creatorid));
275ffe9c13eSGleb Smirnoff }
276ffe9c13eSGleb Smirnoff
277ffe9c13eSGleb Smirnoff static void
pfsync_print_bus(netdissect_options * ndo,const void * bp)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
303ee67461eSJoseph Mingrone ND_PRINT("\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
304ee67461eSJoseph Mingrone htonl(b->creatorid), endtime, min, sec, status);
305ffe9c13eSGleb Smirnoff }
306ffe9c13eSGleb Smirnoff
307ffe9c13eSGleb Smirnoff static void
pfsync_print_tdb(netdissect_options * ndo,const void * bp)30804c53351SBrooks Davis pfsync_print_tdb(netdissect_options *ndo, const void *bp)
309ffe9c13eSGleb Smirnoff {
310ffe9c13eSGleb Smirnoff const struct pfsync_tdb *t = bp;
311ffe9c13eSGleb Smirnoff
312ee67461eSJoseph Mingrone ND_PRINT("\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
313ffe9c13eSGleb Smirnoff ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
314ee67461eSJoseph Mingrone (uintmax_t )be64toh(t->cur_bytes));
315ffe9c13eSGleb Smirnoff }
316ffe9c13eSGleb Smirnoff
317ffe9c13eSGleb Smirnoff static void
print_host(netdissect_options * ndo,struct pf_addr * addr,uint16_t port,sa_family_t af,const char * proto)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)
324ee67461eSJoseph Mingrone ND_PRINT("?");
325ffe9c13eSGleb Smirnoff else
326ee67461eSJoseph Mingrone ND_PRINT("%s", buf);
327ffe9c13eSGleb Smirnoff
328ffe9c13eSGleb Smirnoff if (port)
329ee67461eSJoseph Mingrone ND_PRINT(".%hu", ntohs(port));
330ffe9c13eSGleb Smirnoff }
331ffe9c13eSGleb Smirnoff
332ffe9c13eSGleb Smirnoff static void
print_seq(netdissect_options * ndo,const struct pfsync_state_peer * p)33304c53351SBrooks Davis print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
334ffe9c13eSGleb Smirnoff {
335ffe9c13eSGleb Smirnoff if (p->seqdiff)
336ee67461eSJoseph Mingrone ND_PRINT("[%u + %u](+%u)", ntohl(p->seqlo),
337ee67461eSJoseph Mingrone ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
338ffe9c13eSGleb Smirnoff else
339ee67461eSJoseph Mingrone ND_PRINT("[%u + %u]", ntohl(p->seqlo),
340ee67461eSJoseph Mingrone ntohl(p->seqhi) - ntohl(p->seqlo));
341ffe9c13eSGleb Smirnoff }
342ffe9c13eSGleb Smirnoff
343ffe9c13eSGleb Smirnoff static void
print_src_dst(netdissect_options * ndo,const struct pfsync_state_peer * src,const struct pfsync_state_peer * dst,uint8_t proto)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)
351ee67461eSJoseph Mingrone ND_PRINT(" %s:%s", tcpstates[src->state],
352ee67461eSJoseph Mingrone tcpstates[dst->state]);
353ffe9c13eSGleb Smirnoff else if (src->state == PF_TCPS_PROXY_SRC ||
354ffe9c13eSGleb Smirnoff dst->state == PF_TCPS_PROXY_SRC)
355ee67461eSJoseph Mingrone ND_PRINT(" PROXY:SRC");
356ffe9c13eSGleb Smirnoff else if (src->state == PF_TCPS_PROXY_DST ||
357ffe9c13eSGleb Smirnoff dst->state == PF_TCPS_PROXY_DST)
358ee67461eSJoseph Mingrone ND_PRINT(" PROXY:DST");
359ffe9c13eSGleb Smirnoff else
360ee67461eSJoseph Mingrone ND_PRINT(" <BAD STATE LEVELS %u:%u>",
361ee67461eSJoseph Mingrone src->state, dst->state);
3623340d773SGleb Smirnoff if (ndo->ndo_vflag > 1) {
363ee67461eSJoseph Mingrone ND_PRINT("\n\t");
36404c53351SBrooks Davis print_seq(ndo, src);
365ffe9c13eSGleb Smirnoff if (src->wscale && dst->wscale)
366ee67461eSJoseph Mingrone ND_PRINT(" wscale %u",
367ee67461eSJoseph Mingrone src->wscale & PF_WSCALE_MASK);
368ee67461eSJoseph Mingrone ND_PRINT(" ");
36904c53351SBrooks Davis print_seq(ndo, dst);
370ffe9c13eSGleb Smirnoff if (src->wscale && dst->wscale)
371ee67461eSJoseph Mingrone ND_PRINT(" wscale %u",
372ee67461eSJoseph Mingrone 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
378ee67461eSJoseph Mingrone ND_PRINT(" %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
384ee67461eSJoseph Mingrone ND_PRINT(" %s:%s", states[src->state], states[dst->state]);
385ffe9c13eSGleb Smirnoff } else {
386ee67461eSJoseph Mingrone ND_PRINT(" %u:%u", src->state, dst->state);
387ffe9c13eSGleb Smirnoff }
388ffe9c13eSGleb Smirnoff }
389ffe9c13eSGleb Smirnoff
390ffe9c13eSGleb Smirnoff static void
print_state(netdissect_options * ndo,union pfsync_state_union * s,int version)3914bf98559SKajetan 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
3974bf98559SKajetan Staszkiewicz if (s->pfs_1301.direction == PF_OUT) {
3984bf98559SKajetan Staszkiewicz src = &s->pfs_1301.src;
3994bf98559SKajetan Staszkiewicz dst = &s->pfs_1301.dst;
4004bf98559SKajetan Staszkiewicz sk = &s->pfs_1301.key[PF_SK_STACK];
4014bf98559SKajetan Staszkiewicz nk = &s->pfs_1301.key[PF_SK_WIRE];
4024bf98559SKajetan 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 {
4054bf98559SKajetan Staszkiewicz src = &s->pfs_1301.dst;
4064bf98559SKajetan Staszkiewicz dst = &s->pfs_1301.src;
4074bf98559SKajetan Staszkiewicz sk = &s->pfs_1301.key[PF_SK_WIRE];
4084bf98559SKajetan Staszkiewicz nk = &s->pfs_1301.key[PF_SK_STACK];
4094bf98559SKajetan Staszkiewicz if (s->pfs_1301.proto == IPPROTO_ICMP || s->pfs_1301.proto == IPPROTO_ICMPV6)
410ffe9c13eSGleb Smirnoff sk->port[1] = nk->port[1];
411ffe9c13eSGleb Smirnoff }
412ee67461eSJoseph Mingrone ND_PRINT("\t%s ", s->pfs_1301.ifname);
413ee67461eSJoseph Mingrone ND_PRINT("proto %u ", s->pfs_1301.proto);
414ffe9c13eSGleb Smirnoff
4154bf98559SKajetan Staszkiewicz print_host(ndo, &nk->addr[1], nk->port[1], s->pfs_1301.af, NULL);
4164bf98559SKajetan Staszkiewicz if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->pfs_1301.af) ||
417ffe9c13eSGleb Smirnoff nk->port[1] != sk->port[1]) {
418ee67461eSJoseph Mingrone ND_PRINT((" ("));
4194bf98559SKajetan Staszkiewicz print_host(ndo, &sk->addr[1], sk->port[1], s->pfs_1301.af, NULL);
420ee67461eSJoseph Mingrone ND_PRINT(")");
421ffe9c13eSGleb Smirnoff }
4224bf98559SKajetan Staszkiewicz if (s->pfs_1301.direction == PF_OUT)
423ee67461eSJoseph Mingrone ND_PRINT((" -> "));
424ffe9c13eSGleb Smirnoff else
425ee67461eSJoseph Mingrone ND_PRINT((" <- "));
4264bf98559SKajetan Staszkiewicz print_host(ndo, &nk->addr[0], nk->port[0], s->pfs_1301.af, NULL);
4274bf98559SKajetan Staszkiewicz if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->pfs_1301.af) ||
428ffe9c13eSGleb Smirnoff nk->port[0] != sk->port[0]) {
429ee67461eSJoseph Mingrone ND_PRINT((" ("));
4304bf98559SKajetan Staszkiewicz print_host(ndo, &sk->addr[0], sk->port[0], s->pfs_1301.af, NULL);
431ee67461eSJoseph Mingrone ND_PRINT((")"));
432ffe9c13eSGleb Smirnoff }
433ffe9c13eSGleb Smirnoff
4344bf98559SKajetan 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];
4394bf98559SKajetan Staszkiewicz uint32_t creation = ntohl(s->pfs_1301.creation);
4404bf98559SKajetan 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;
446ee67461eSJoseph Mingrone ND_PRINT("\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;
451ee67461eSJoseph Mingrone ND_PRINT(", expires in %.2u:%.2u:%.2u", expire, min, sec);
452ffe9c13eSGleb Smirnoff
4534bf98559SKajetan Staszkiewicz bcopy(s->pfs_1301.packets[0], &packets[0], sizeof(uint64_t));
4544bf98559SKajetan Staszkiewicz bcopy(s->pfs_1301.packets[1], &packets[1], sizeof(uint64_t));
4554bf98559SKajetan Staszkiewicz bcopy(s->pfs_1301.bytes[0], &bytes[0], sizeof(uint64_t));
4564bf98559SKajetan Staszkiewicz bcopy(s->pfs_1301.bytes[1], &bytes[1], sizeof(uint64_t));
457ee67461eSJoseph Mingrone ND_PRINT(", %ju:%ju pkts, %ju:%ju bytes",
458ffe9c13eSGleb Smirnoff be64toh(packets[0]), be64toh(packets[1]),
459ee67461eSJoseph Mingrone be64toh(bytes[0]), be64toh(bytes[1]));
4604bf98559SKajetan Staszkiewicz if (s->pfs_1301.anchor != ntohl(-1))
461ee67461eSJoseph Mingrone ND_PRINT(", anchor %u", ntohl(s->pfs_1301.anchor));
4624bf98559SKajetan Staszkiewicz if (s->pfs_1301.rule != ntohl(-1))
463ee67461eSJoseph Mingrone ND_PRINT(", rule %u", ntohl(s->pfs_1301.rule));
464ffe9c13eSGleb Smirnoff }
4653340d773SGleb Smirnoff if (ndo->ndo_vflag > 1) {
466ffe9c13eSGleb Smirnoff uint64_t id;
467ffe9c13eSGleb Smirnoff
4684bf98559SKajetan Staszkiewicz bcopy(&s->pfs_1301.id, &id, sizeof(uint64_t));
469ee67461eSJoseph Mingrone ND_PRINT("\n\tid: %016jx creatorid: %08x",
470ee67461eSJoseph Mingrone (uintmax_t )be64toh(id), ntohl(s->pfs_1301.creatorid));
471ffe9c13eSGleb Smirnoff }
472ffe9c13eSGleb Smirnoff }
473