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 #include <tcpdump-stdinc.h>
38 #include <sys/endian.h>
40 #include <net/pfvar.h> /* XXX */
41 #include <net/if_pfsync.h>
43 #include <netinet/tcp_fsm.h>
47 #include "interface.h"
48 #include "addrtoname.h"
50 static void pfsync_print(netdissect_options *, struct pfsync_header *,
51 const u_char *, u_int);
52 static void print_src_dst(netdissect_options *,
53 const struct pfsync_state_peer *,
54 const struct pfsync_state_peer *, uint8_t);
55 static void print_state(netdissect_options *, struct pfsync_state *);
59 pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
60 register const u_char *p)
62 u_int caplen = h->caplen;
66 if (caplen < PFSYNC_HDRLEN) {
67 ND_PRINT((ndo, "[|pfsync]"));
71 pfsync_print((struct pfsync_header *)p,
72 p + sizeof(struct pfsync_header),
73 caplen - sizeof(struct pfsync_header));
76 default_print((const u_char *)p, caplen);
78 safeputchar(ndo, '\n');
83 pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
85 struct pfsync_header *hdr = (struct pfsync_header *)bp;
87 if (len < PFSYNC_HDRLEN)
88 ND_PRINT((ndo, "[|pfsync]"));
90 pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
91 len - sizeof(struct pfsync_header));
94 struct pfsync_actions {
97 void (*print)(netdissect_options *, const void *);
100 static void pfsync_print_clr(netdissect_options *, const void *);
101 static void pfsync_print_state(netdissect_options *, const void *);
102 static void pfsync_print_ins_ack(netdissect_options *, const void *);
103 static void pfsync_print_upd_c(netdissect_options *, const void *);
104 static void pfsync_print_upd_req(netdissect_options *, const void *);
105 static void pfsync_print_del_c(netdissect_options *, const void *);
106 static void pfsync_print_bus(netdissect_options *, const void *);
107 static void pfsync_print_tdb(netdissect_options *, const void *);
109 struct pfsync_actions actions[] = {
110 { "clear all", sizeof(struct pfsync_clr), pfsync_print_clr },
111 { "insert", sizeof(struct pfsync_state), pfsync_print_state },
112 { "insert ack", sizeof(struct pfsync_ins_ack), pfsync_print_ins_ack },
113 { "update", sizeof(struct pfsync_ins_ack), pfsync_print_state },
114 { "update compressed", sizeof(struct pfsync_upd_c),
115 pfsync_print_upd_c },
116 { "request uncompressed", sizeof(struct pfsync_upd_req),
117 pfsync_print_upd_req },
118 { "delete", sizeof(struct pfsync_state), pfsync_print_state },
119 { "delete compressed", sizeof(struct pfsync_del_c),
120 pfsync_print_del_c },
121 { "frag insert", 0, NULL },
122 { "frag delete", 0, NULL },
123 { "bulk update status", sizeof(struct pfsync_bus),
125 { "tdb", 0, pfsync_print_tdb },
130 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
131 const u_char *bp, u_int len)
133 struct pfsync_subheader *subh;
137 plen = ntohs(hdr->len);
139 ND_PRINT((ndo, "PFSYNCv%d len %d", hdr->version, plen));
141 if (hdr->version != PFSYNC_VERSION)
144 plen -= sizeof(*hdr);
147 if (len < sizeof(*subh))
150 subh = (struct pfsync_subheader *)bp;
152 len -= sizeof(*subh);
153 plen -= sizeof(*subh);
155 if (subh->action >= PFSYNC_ACT_MAX) {
156 ND_PRINT((ndo, "\n act UNKNOWN id %d",
161 count = ntohs(subh->count);
162 ND_PRINT((ndo, "\n %s count %d", actions[subh->action].name,
164 alen = actions[subh->action].len;
166 if (subh->action == PFSYNC_ACT_EOF)
169 if (actions[subh->action].print == NULL) {
170 ND_PRINT((ndo, "\n unimplemented action %hhu",
175 for (i = 0; i < count; i++) {
182 actions[subh->action].print(ndo, bp);
191 ND_PRINT((ndo, "\n ..."));
195 ND_PRINT((ndo, "\n invalid header length"));
199 ND_PRINT((ndo, "\n invalid packet length"));
203 pfsync_print_clr(netdissect_options *ndo, const void *bp)
205 const struct pfsync_clr *clr = bp;
207 ND_PRINT((ndo, "\n\tcreatorid: %08x", htonl(clr->creatorid)));
208 if (clr->ifname[0] != '\0')
209 ND_PRINT((ndo, " interface: %s", clr->ifname));
213 pfsync_print_state(netdissect_options *ndo, const void *bp)
215 struct pfsync_state *st = (struct pfsync_state *)bp;
217 safeputchar(ndo, '\n');
218 print_state(ndo, st);
222 pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
224 const struct pfsync_ins_ack *iack = bp;
226 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
227 (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid)));
231 pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
233 const struct pfsync_upd_c *u = bp;
235 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
236 (uintmax_t)be64toh(u->id), ntohl(u->creatorid)));
238 ND_PRINT((ndo, "\n\tTCP? :"));
239 print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
244 pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
246 const struct pfsync_upd_req *ur = bp;
248 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
249 (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid)));
253 pfsync_print_del_c(netdissect_options *ndo, const void *bp)
255 const struct pfsync_del_c *d = bp;
257 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
258 (uintmax_t)be64toh(d->id), ntohl(d->creatorid)));
262 pfsync_print_bus(netdissect_options *ndo, const void *bp)
264 const struct pfsync_bus *b = bp;
269 endtime = ntohl(b->endtime);
276 case PFSYNC_BUS_START:
287 ND_PRINT((ndo, "\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
288 htonl(b->creatorid), endtime, min, sec, status));
292 pfsync_print_tdb(netdissect_options *ndo, const void *bp)
294 const struct pfsync_tdb *t = bp;
296 ND_PRINT((ndo, "\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
297 ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
298 (uintmax_t )be64toh(t->cur_bytes)));
302 print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
303 sa_family_t af, const char *proto)
307 if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
308 ND_PRINT((ndo, "?"));
310 ND_PRINT((ndo, "%s", buf));
313 ND_PRINT((ndo, ".%hu", ntohs(port)));
317 print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
320 ND_PRINT((ndo, "[%u + %u](+%u)", ntohl(p->seqlo),
321 ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)));
323 ND_PRINT((ndo, "[%u + %u]", ntohl(p->seqlo),
324 ntohl(p->seqhi) - ntohl(p->seqlo)));
328 print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
329 const struct pfsync_state_peer *dst, uint8_t proto)
332 if (proto == IPPROTO_TCP) {
333 if (src->state <= TCPS_TIME_WAIT &&
334 dst->state <= TCPS_TIME_WAIT)
335 ND_PRINT((ndo, " %s:%s", tcpstates[src->state],
336 tcpstates[dst->state]));
337 else if (src->state == PF_TCPS_PROXY_SRC ||
338 dst->state == PF_TCPS_PROXY_SRC)
339 ND_PRINT((ndo, " PROXY:SRC"));
340 else if (src->state == PF_TCPS_PROXY_DST ||
341 dst->state == PF_TCPS_PROXY_DST)
342 ND_PRINT((ndo, " PROXY:DST"));
344 ND_PRINT((ndo, " <BAD STATE LEVELS %u:%u>",
345 src->state, dst->state));
347 ND_PRINT((ndo, "\n\t"));
349 if (src->wscale && dst->wscale)
350 ND_PRINT((ndo, " wscale %u",
351 src->wscale & PF_WSCALE_MASK));
352 ND_PRINT((ndo, " "));
354 if (src->wscale && dst->wscale)
355 ND_PRINT((ndo, " wscale %u",
356 dst->wscale & PF_WSCALE_MASK));
358 } else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
359 dst->state < PFUDPS_NSTATES) {
360 const char *states[] = PFUDPS_NAMES;
362 ND_PRINT((ndo, " %s:%s", states[src->state], states[dst->state]));
363 } else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
364 dst->state < PFOTHERS_NSTATES) {
365 /* XXX ICMP doesn't really have state levels */
366 const char *states[] = PFOTHERS_NAMES;
368 ND_PRINT((ndo, " %s:%s", states[src->state], states[dst->state]));
370 ND_PRINT((ndo, " %u:%u", src->state, dst->state));
375 print_state(netdissect_options *ndo, struct pfsync_state *s)
377 struct pfsync_state_peer *src, *dst;
378 struct pfsync_state_key *sk, *nk;
381 if (s->direction == PF_OUT) {
384 sk = &s->key[PF_SK_STACK];
385 nk = &s->key[PF_SK_WIRE];
386 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
387 sk->port[0] = nk->port[0];
391 sk = &s->key[PF_SK_WIRE];
392 nk = &s->key[PF_SK_STACK];
393 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
394 sk->port[1] = nk->port[1];
396 ND_PRINT((ndo, "\t%s ", s->ifname));
397 ND_PRINT((ndo, "proto %u ", s->proto));
399 print_host(ndo, &nk->addr[1], nk->port[1], s->af, NULL);
400 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
401 nk->port[1] != sk->port[1]) {
402 ND_PRINT((ndo, " ("));
403 print_host(ndo, &sk->addr[1], sk->port[1], s->af, NULL);
404 ND_PRINT((ndo, ")"));
406 if (s->direction == PF_OUT)
407 ND_PRINT((ndo, " -> "));
409 ND_PRINT((ndo, " <- "));
410 print_host(ndo, &nk->addr[0], nk->port[0], s->af, NULL);
411 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
412 nk->port[0] != sk->port[0]) {
413 ND_PRINT((ndo, " ("));
414 print_host(ndo, &sk->addr[0], sk->port[0], s->af, NULL);
415 ND_PRINT((ndo, ")"));
418 print_src_dst(ndo, src, dst, s->proto);
423 uint32_t creation = ntohl(s->creation);
424 uint32_t expire = ntohl(s->expire);
430 ND_PRINT((ndo, "\n\tage %.2u:%.2u:%.2u", creation, min, sec));
435 ND_PRINT((ndo, ", expires in %.2u:%.2u:%.2u", expire, min, sec));
437 bcopy(s->packets[0], &packets[0], sizeof(uint64_t));
438 bcopy(s->packets[1], &packets[1], sizeof(uint64_t));
439 bcopy(s->bytes[0], &bytes[0], sizeof(uint64_t));
440 bcopy(s->bytes[1], &bytes[1], sizeof(uint64_t));
441 ND_PRINT((ndo, ", %ju:%ju pkts, %ju:%ju bytes",
442 be64toh(packets[0]), be64toh(packets[1]),
443 be64toh(bytes[0]), be64toh(bytes[1])));
444 if (s->anchor != ntohl(-1))
445 ND_PRINT((ndo, ", anchor %u", ntohl(s->anchor)));
446 if (s->rule != ntohl(-1))
447 ND_PRINT((ndo, ", rule %u", ntohl(s->rule)));
452 bcopy(&s->id, &id, sizeof(uint64_t));
453 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
454 (uintmax_t )be64toh(id), ntohl(s->creatorid)));