2 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2002 Michael Shalayeff
4 * Copyright (c) 2001 Daniel Hartmeier
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26 * THE POSSIBILITY OF SUCH DAMAGE.
28 * $OpenBSD: print-pfsync.c,v 1.38 2012/09/19 13:50:36 mikeb Exp $
29 * $OpenBSD: pf_print_state.c,v 1.11 2012/07/08 17:48:37 lteo Exp $
36 #ifndef HAVE_NET_PFVAR_H
37 #error "No pf headers available"
39 #include <sys/endian.h>
41 #include <net/pfvar.h>
42 #include <net/if_pfsync.h>
44 #include <netinet/tcp_fsm.h>
46 #include <netdissect-stdinc.h>
49 #include "netdissect.h"
50 #include "interface.h"
51 #include "addrtoname.h"
53 static void pfsync_print(netdissect_options *, struct pfsync_header *,
54 const u_char *, u_int);
55 static void print_src_dst(netdissect_options *,
56 const struct pfsync_state_peer *,
57 const struct pfsync_state_peer *, uint8_t);
58 static void print_state(netdissect_options *, struct pfsync_state *);
62 pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
63 register const u_char *p)
65 u_int caplen = h->caplen;
69 if (caplen < PFSYNC_HDRLEN) {
70 ND_PRINT((ndo, "[|pfsync]"));
74 pfsync_print((struct pfsync_header *)p,
75 p + sizeof(struct pfsync_header),
76 caplen - sizeof(struct pfsync_header));
79 default_print((const u_char *)p, caplen);
81 safeputchar(ndo, '\n');
86 pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
88 struct pfsync_header *hdr = (struct pfsync_header *)bp;
90 if (len < PFSYNC_HDRLEN)
91 ND_PRINT((ndo, "[|pfsync]"));
93 pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
94 len - sizeof(struct pfsync_header));
97 struct pfsync_actions {
100 void (*print)(netdissect_options *, const void *);
103 static void pfsync_print_clr(netdissect_options *, const void *);
104 static void pfsync_print_state(netdissect_options *, const void *);
105 static void pfsync_print_ins_ack(netdissect_options *, const void *);
106 static void pfsync_print_upd_c(netdissect_options *, const void *);
107 static void pfsync_print_upd_req(netdissect_options *, const void *);
108 static void pfsync_print_del_c(netdissect_options *, const void *);
109 static void pfsync_print_bus(netdissect_options *, const void *);
110 static void pfsync_print_tdb(netdissect_options *, const void *);
112 struct pfsync_actions actions[] = {
113 { "clear all", sizeof(struct pfsync_clr), pfsync_print_clr },
114 { "insert", sizeof(struct pfsync_state), pfsync_print_state },
115 { "insert ack", sizeof(struct pfsync_ins_ack), pfsync_print_ins_ack },
116 { "update", sizeof(struct pfsync_ins_ack), pfsync_print_state },
117 { "update compressed", sizeof(struct pfsync_upd_c),
118 pfsync_print_upd_c },
119 { "request uncompressed", sizeof(struct pfsync_upd_req),
120 pfsync_print_upd_req },
121 { "delete", sizeof(struct pfsync_state), pfsync_print_state },
122 { "delete compressed", sizeof(struct pfsync_del_c),
123 pfsync_print_del_c },
124 { "frag insert", 0, NULL },
125 { "frag delete", 0, NULL },
126 { "bulk update status", sizeof(struct pfsync_bus),
128 { "tdb", 0, pfsync_print_tdb },
133 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
134 const u_char *bp, u_int len)
136 struct pfsync_subheader *subh;
140 plen = ntohs(hdr->len);
142 ND_PRINT((ndo, "PFSYNCv%d len %d", hdr->version, plen));
144 if (hdr->version != PFSYNC_VERSION)
147 plen -= sizeof(*hdr);
150 if (len < sizeof(*subh))
153 subh = (struct pfsync_subheader *)bp;
155 len -= sizeof(*subh);
156 plen -= sizeof(*subh);
158 if (subh->action >= PFSYNC_ACT_MAX) {
159 ND_PRINT((ndo, "\n act UNKNOWN id %d",
164 count = ntohs(subh->count);
165 ND_PRINT((ndo, "\n %s count %d", actions[subh->action].name,
167 alen = actions[subh->action].len;
169 if (subh->action == PFSYNC_ACT_EOF)
172 if (actions[subh->action].print == NULL) {
173 ND_PRINT((ndo, "\n unimplemented action %hhu",
178 for (i = 0; i < count; i++) {
185 actions[subh->action].print(ndo, bp);
194 ND_PRINT((ndo, "\n ..."));
198 ND_PRINT((ndo, "\n invalid header length"));
202 ND_PRINT((ndo, "\n invalid packet length"));
206 pfsync_print_clr(netdissect_options *ndo, const void *bp)
208 const struct pfsync_clr *clr = bp;
210 ND_PRINT((ndo, "\n\tcreatorid: %08x", htonl(clr->creatorid)));
211 if (clr->ifname[0] != '\0')
212 ND_PRINT((ndo, " interface: %s", clr->ifname));
216 pfsync_print_state(netdissect_options *ndo, const void *bp)
218 struct pfsync_state *st = (struct pfsync_state *)bp;
220 safeputchar(ndo, '\n');
221 print_state(ndo, st);
225 pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
227 const struct pfsync_ins_ack *iack = bp;
229 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
230 (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid)));
234 pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
236 const struct pfsync_upd_c *u = bp;
238 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
239 (uintmax_t)be64toh(u->id), ntohl(u->creatorid)));
240 if (ndo->ndo_vflag > 2) {
241 ND_PRINT((ndo, "\n\tTCP? :"));
242 print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
247 pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
249 const struct pfsync_upd_req *ur = bp;
251 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
252 (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid)));
256 pfsync_print_del_c(netdissect_options *ndo, const void *bp)
258 const struct pfsync_del_c *d = bp;
260 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
261 (uintmax_t)be64toh(d->id), ntohl(d->creatorid)));
265 pfsync_print_bus(netdissect_options *ndo, const void *bp)
267 const struct pfsync_bus *b = bp;
272 endtime = ntohl(b->endtime);
279 case PFSYNC_BUS_START:
290 ND_PRINT((ndo, "\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
291 htonl(b->creatorid), endtime, min, sec, status));
295 pfsync_print_tdb(netdissect_options *ndo, const void *bp)
297 const struct pfsync_tdb *t = bp;
299 ND_PRINT((ndo, "\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
300 ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
301 (uintmax_t )be64toh(t->cur_bytes)));
305 print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
306 sa_family_t af, const char *proto)
310 if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
311 ND_PRINT((ndo, "?"));
313 ND_PRINT((ndo, "%s", buf));
316 ND_PRINT((ndo, ".%hu", ntohs(port)));
320 print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
323 ND_PRINT((ndo, "[%u + %u](+%u)", ntohl(p->seqlo),
324 ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)));
326 ND_PRINT((ndo, "[%u + %u]", ntohl(p->seqlo),
327 ntohl(p->seqhi) - ntohl(p->seqlo)));
331 print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
332 const struct pfsync_state_peer *dst, uint8_t proto)
335 if (proto == IPPROTO_TCP) {
336 if (src->state <= TCPS_TIME_WAIT &&
337 dst->state <= TCPS_TIME_WAIT)
338 ND_PRINT((ndo, " %s:%s", tcpstates[src->state],
339 tcpstates[dst->state]));
340 else if (src->state == PF_TCPS_PROXY_SRC ||
341 dst->state == PF_TCPS_PROXY_SRC)
342 ND_PRINT((ndo, " PROXY:SRC"));
343 else if (src->state == PF_TCPS_PROXY_DST ||
344 dst->state == PF_TCPS_PROXY_DST)
345 ND_PRINT((ndo, " PROXY:DST"));
347 ND_PRINT((ndo, " <BAD STATE LEVELS %u:%u>",
348 src->state, dst->state));
349 if (ndo->ndo_vflag > 1) {
350 ND_PRINT((ndo, "\n\t"));
352 if (src->wscale && dst->wscale)
353 ND_PRINT((ndo, " wscale %u",
354 src->wscale & PF_WSCALE_MASK));
355 ND_PRINT((ndo, " "));
357 if (src->wscale && dst->wscale)
358 ND_PRINT((ndo, " wscale %u",
359 dst->wscale & PF_WSCALE_MASK));
361 } else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
362 dst->state < PFUDPS_NSTATES) {
363 const char *states[] = PFUDPS_NAMES;
365 ND_PRINT((ndo, " %s:%s", states[src->state], states[dst->state]));
366 } else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
367 dst->state < PFOTHERS_NSTATES) {
368 /* XXX ICMP doesn't really have state levels */
369 const char *states[] = PFOTHERS_NAMES;
371 ND_PRINT((ndo, " %s:%s", states[src->state], states[dst->state]));
373 ND_PRINT((ndo, " %u:%u", src->state, dst->state));
378 print_state(netdissect_options *ndo, struct pfsync_state *s)
380 struct pfsync_state_peer *src, *dst;
381 struct pfsync_state_key *sk, *nk;
384 if (s->direction == PF_OUT) {
387 sk = &s->key[PF_SK_STACK];
388 nk = &s->key[PF_SK_WIRE];
389 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
390 sk->port[0] = nk->port[0];
394 sk = &s->key[PF_SK_WIRE];
395 nk = &s->key[PF_SK_STACK];
396 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
397 sk->port[1] = nk->port[1];
399 ND_PRINT((ndo, "\t%s ", s->ifname));
400 ND_PRINT((ndo, "proto %u ", s->proto));
402 print_host(ndo, &nk->addr[1], nk->port[1], s->af, NULL);
403 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
404 nk->port[1] != sk->port[1]) {
405 ND_PRINT((ndo, " ("));
406 print_host(ndo, &sk->addr[1], sk->port[1], s->af, NULL);
407 ND_PRINT((ndo, ")"));
409 if (s->direction == PF_OUT)
410 ND_PRINT((ndo, " -> "));
412 ND_PRINT((ndo, " <- "));
413 print_host(ndo, &nk->addr[0], nk->port[0], s->af, NULL);
414 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
415 nk->port[0] != sk->port[0]) {
416 ND_PRINT((ndo, " ("));
417 print_host(ndo, &sk->addr[0], sk->port[0], s->af, NULL);
418 ND_PRINT((ndo, ")"));
421 print_src_dst(ndo, src, dst, s->proto);
423 if (ndo->ndo_vflag > 1) {
426 uint32_t creation = ntohl(s->creation);
427 uint32_t expire = ntohl(s->expire);
433 ND_PRINT((ndo, "\n\tage %.2u:%.2u:%.2u", creation, min, sec));
438 ND_PRINT((ndo, ", expires in %.2u:%.2u:%.2u", expire, min, sec));
440 bcopy(s->packets[0], &packets[0], sizeof(uint64_t));
441 bcopy(s->packets[1], &packets[1], sizeof(uint64_t));
442 bcopy(s->bytes[0], &bytes[0], sizeof(uint64_t));
443 bcopy(s->bytes[1], &bytes[1], sizeof(uint64_t));
444 ND_PRINT((ndo, ", %ju:%ju pkts, %ju:%ju bytes",
445 be64toh(packets[0]), be64toh(packets[1]),
446 be64toh(bytes[0]), be64toh(bytes[1])));
447 if (s->anchor != ntohl(-1))
448 ND_PRINT((ndo, ", anchor %u", ntohl(s->anchor)));
449 if (s->rule != ntohl(-1))
450 ND_PRINT((ndo, ", rule %u", ntohl(s->rule)));
452 if (ndo->ndo_vflag > 1) {
455 bcopy(&s->id, &id, sizeof(uint64_t));
456 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
457 (uintmax_t )be64toh(id), ntohl(s->creatorid)));