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>
42 #include <netinet/ip.h>
44 #include <netinet/tcp_fsm.h>
48 #include "interface.h"
49 #include "addrtoname.h"
51 static void pfsync_print(struct pfsync_header *, const u_char *, u_int);
52 static void print_src_dst(const struct pfsync_state_peer *,
53 const struct pfsync_state_peer *, uint8_t);
54 static void print_state(struct pfsync_state *);
58 pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
59 register const u_char *p)
61 u_int caplen = h->caplen;
65 if (caplen < PFSYNC_HDRLEN) {
70 pfsync_print((struct pfsync_header *)p,
71 p + sizeof(struct pfsync_header),
72 caplen - sizeof(struct pfsync_header));
75 default_print((const u_char *)p, caplen);
82 pfsync_ip_print(const u_char *bp, u_int len)
84 struct pfsync_header *hdr = (struct pfsync_header *)bp;
86 if (len < PFSYNC_HDRLEN)
89 pfsync_print(hdr, bp + sizeof(struct pfsync_header),
90 len - sizeof(struct pfsync_header));
93 struct pfsync_actions {
96 void (*print)(const void *);
99 static void pfsync_print_clr(const void *);
100 static void pfsync_print_state(const void *);
101 static void pfsync_print_ins_ack(const void *);
102 static void pfsync_print_upd_c(const void *);
103 static void pfsync_print_upd_req(const void *);
104 static void pfsync_print_del_c(const void *);
105 static void pfsync_print_bus(const void *);
106 static void pfsync_print_tdb(const void *);
108 struct pfsync_actions actions[] = {
109 { "clear all", sizeof(struct pfsync_clr), pfsync_print_clr },
110 { "insert", sizeof(struct pfsync_state), pfsync_print_state },
111 { "insert ack", sizeof(struct pfsync_ins_ack), pfsync_print_ins_ack },
112 { "update", sizeof(struct pfsync_ins_ack), pfsync_print_state },
113 { "update compressed", sizeof(struct pfsync_upd_c),
114 pfsync_print_upd_c },
115 { "request uncompressed", sizeof(struct pfsync_upd_req),
116 pfsync_print_upd_req },
117 { "delete", sizeof(struct pfsync_state), pfsync_print_state },
118 { "delete compressed", sizeof(struct pfsync_del_c),
119 pfsync_print_del_c },
120 { "frag insert", 0, NULL },
121 { "frag delete", 0, NULL },
122 { "bulk update status", sizeof(struct pfsync_bus),
124 { "tdb", 0, pfsync_print_tdb },
129 pfsync_print(struct pfsync_header *hdr, const u_char *bp, u_int len)
131 struct pfsync_subheader *subh;
135 plen = ntohs(hdr->len);
137 printf("PFSYNCv%d len %d", hdr->version, plen);
139 if (hdr->version != PFSYNC_VERSION)
142 plen -= sizeof(*hdr);
145 if (len < sizeof(*subh))
148 subh = (struct pfsync_subheader *)bp;
150 len -= sizeof(*subh);
151 plen -= sizeof(*subh);
153 if (subh->action >= PFSYNC_ACT_MAX) {
154 printf("\n act UNKNOWN id %d", subh->action);
158 count = ntohs(subh->count);
159 printf("\n %s count %d", actions[subh->action].name, count);
160 alen = actions[subh->action].len;
162 if (subh->action == PFSYNC_ACT_EOF)
165 if (actions[subh->action].print == NULL) {
166 printf("\n unimplemented action %hhu", subh->action);
170 for (i = 0; i < count; i++) {
177 actions[subh->action].print(bp);
190 printf("\n invalid header length");
194 printf("\n invalid packet length");
198 pfsync_print_clr(const void *bp)
200 const struct pfsync_clr *clr = bp;
202 printf("\n\tcreatorid: %08x", htonl(clr->creatorid));
203 if (clr->ifname[0] != '\0')
204 printf(" interface: %s", clr->ifname);
208 pfsync_print_state(const void *bp)
210 struct pfsync_state *st = (struct pfsync_state *)bp;
217 pfsync_print_ins_ack(const void *bp)
219 const struct pfsync_ins_ack *iack = bp;
221 printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(iack->id),
222 ntohl(iack->creatorid));
226 pfsync_print_upd_c(const void *bp)
228 const struct pfsync_upd_c *u = bp;
230 printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(u->id),
231 ntohl(u->creatorid));
233 printf("\n\tTCP? :");
234 print_src_dst(&u->src, &u->dst, IPPROTO_TCP);
239 pfsync_print_upd_req(const void *bp)
241 const struct pfsync_upd_req *ur = bp;
243 printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(ur->id),
244 ntohl(ur->creatorid));
248 pfsync_print_del_c(const void *bp)
250 const struct pfsync_del_c *d = bp;
252 printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(d->id),
253 ntohl(d->creatorid));
257 pfsync_print_bus(const void *bp)
259 const struct pfsync_bus *b = bp;
264 endtime = ntohl(b->endtime);
271 case PFSYNC_BUS_START:
282 printf("\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
283 htonl(b->creatorid), endtime, min, sec, status);
287 pfsync_print_tdb(const void *bp)
289 const struct pfsync_tdb *t = bp;
291 printf("\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
292 ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
293 (uintmax_t )be64toh(t->cur_bytes));
297 print_host(struct pf_addr *addr, uint16_t port, sa_family_t af,
302 if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
308 printf(".%hu", ntohs(port));
312 print_seq(const struct pfsync_state_peer *p)
315 printf("[%u + %u](+%u)", ntohl(p->seqlo),
316 ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
318 printf("[%u + %u]", ntohl(p->seqlo),
319 ntohl(p->seqhi) - ntohl(p->seqlo));
323 print_src_dst(const struct pfsync_state_peer *src,
324 const struct pfsync_state_peer *dst, uint8_t proto)
327 if (proto == IPPROTO_TCP) {
328 if (src->state <= TCPS_TIME_WAIT &&
329 dst->state <= TCPS_TIME_WAIT)
330 printf(" %s:%s", tcpstates[src->state],
331 tcpstates[dst->state]);
332 else if (src->state == PF_TCPS_PROXY_SRC ||
333 dst->state == PF_TCPS_PROXY_SRC)
334 printf(" PROXY:SRC");
335 else if (src->state == PF_TCPS_PROXY_DST ||
336 dst->state == PF_TCPS_PROXY_DST)
337 printf(" PROXY:DST");
339 printf(" <BAD STATE LEVELS %u:%u>",
340 src->state, dst->state);
344 if (src->wscale && dst->wscale)
346 src->wscale & PF_WSCALE_MASK);
349 if (src->wscale && dst->wscale)
351 dst->wscale & PF_WSCALE_MASK);
353 } else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
354 dst->state < PFUDPS_NSTATES) {
355 const char *states[] = PFUDPS_NAMES;
357 printf(" %s:%s", states[src->state], states[dst->state]);
358 } else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
359 dst->state < PFOTHERS_NSTATES) {
360 /* XXX ICMP doesn't really have state levels */
361 const char *states[] = PFOTHERS_NAMES;
363 printf(" %s:%s", states[src->state], states[dst->state]);
365 printf(" %u:%u", src->state, dst->state);
370 print_state(struct pfsync_state *s)
372 struct pfsync_state_peer *src, *dst;
373 struct pfsync_state_key *sk, *nk;
376 if (s->direction == PF_OUT) {
379 sk = &s->key[PF_SK_STACK];
380 nk = &s->key[PF_SK_WIRE];
381 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
382 sk->port[0] = nk->port[0];
386 sk = &s->key[PF_SK_WIRE];
387 nk = &s->key[PF_SK_STACK];
388 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
389 sk->port[1] = nk->port[1];
391 printf("\t%s ", s->ifname);
392 printf("proto %u ", s->proto);
394 print_host(&nk->addr[1], nk->port[1], s->af, NULL);
395 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
396 nk->port[1] != sk->port[1]) {
398 print_host(&sk->addr[1], sk->port[1], s->af, NULL);
401 if (s->direction == PF_OUT)
405 print_host(&nk->addr[0], nk->port[0], s->af, NULL);
406 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
407 nk->port[0] != sk->port[0]) {
409 print_host(&sk->addr[0], sk->port[0], s->af, NULL);
413 print_src_dst(src, dst, s->proto);
418 uint32_t creation = ntohl(s->creation);
419 uint32_t expire = ntohl(s->expire);
425 printf("\n\tage %.2u:%.2u:%.2u", creation, min, sec);
430 printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
432 bcopy(s->packets[0], &packets[0], sizeof(uint64_t));
433 bcopy(s->packets[1], &packets[1], sizeof(uint64_t));
434 bcopy(s->bytes[0], &bytes[0], sizeof(uint64_t));
435 bcopy(s->bytes[1], &bytes[1], sizeof(uint64_t));
436 printf(", %ju:%ju pkts, %ju:%ju bytes",
437 be64toh(packets[0]), be64toh(packets[1]),
438 be64toh(bytes[0]), be64toh(bytes[1]));
439 if (s->anchor != ntohl(-1))
440 printf(", anchor %u", ntohl(s->anchor));
441 if (s->rule != ntohl(-1))
442 printf(", rule %u", ntohl(s->rule));
447 bcopy(&s->id, &id, sizeof(uint64_t));
448 printf("\n\tid: %016jx creatorid: %08x",
449 (uintmax_t )be64toh(id), ntohl(s->creatorid));