1 /*
2 * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
3 * Copyright (c) 2002 Michael Shalayeff
4 * Copyright (c) 2001 Daniel Hartmeier
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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.
27 *
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 $
30 */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #ifndef HAVE_NET_PFVAR_H
37 #error "No pf headers available"
38 #endif
39 #include <sys/endian.h>
40 #include <net/if.h>
41 #include <net/pfvar.h>
42 #include <net/if_pfsync.h>
43 #define TCPSTATES
44 #include <netinet/tcp_fsm.h>
45
46 #include <netdissect-stdinc.h>
47 #include <string.h>
48
49 #include "netdissect.h"
50 #include "interface.h"
51 #include "addrtoname.h"
52
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 *, union pfsync_state_union *, int);
59
60 void
pfsync_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,register const u_char * p)61 pfsync_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
62 register const u_char *p)
63 {
64 u_int caplen = h->caplen;
65
66 ts_print(ndo, &h->ts);
67
68 if (caplen < PFSYNC_HDRLEN) {
69 ND_PRINT("[|pfsync]");
70 goto out;
71 }
72
73 pfsync_print(ndo, (struct pfsync_header *)p,
74 p + sizeof(struct pfsync_header),
75 caplen - sizeof(struct pfsync_header));
76 out:
77 if (ndo->ndo_xflag) {
78 hex_print(ndo, "\n\t", p, caplen);
79 }
80 fn_print_char(ndo, '\n');
81 return;
82 }
83
84 void
pfsync_ip_print(netdissect_options * ndo,const u_char * bp,u_int len)85 pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
86 {
87 struct pfsync_header *hdr = (struct pfsync_header *)bp;
88
89 if (len < PFSYNC_HDRLEN || !ND_TTEST_LEN(bp, len))
90 ND_PRINT("[|pfsync]");
91 else
92 pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
93 len - sizeof(struct pfsync_header));
94 }
95
96 struct pfsync_actions {
97 const char *name;
98 size_t len;
99 void (*print)(netdissect_options *, const void *);
100 };
101
102 static void pfsync_print_clr(netdissect_options *, const void *);
103 static void pfsync_print_state_1301(netdissect_options *, const void *);
104 static void pfsync_print_state_1400(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 *);
111
112 struct pfsync_actions actions[] = {
113 { "clear all", sizeof(struct pfsync_clr), pfsync_print_clr },
114 { "insert 13.1", sizeof(struct pfsync_state_1301),
115 pfsync_print_state_1301 },
116 { "insert ack", sizeof(struct pfsync_ins_ack), pfsync_print_ins_ack },
117 { "update 13.1", sizeof(struct pfsync_state_1301),
118 pfsync_print_state_1301 },
119 { "update compressed", sizeof(struct pfsync_upd_c),
120 pfsync_print_upd_c },
121 { "request uncompressed", sizeof(struct pfsync_upd_req),
122 pfsync_print_upd_req },
123 { "delete", sizeof(struct pfsync_state_1301), pfsync_print_state_1301 },
124 { "delete compressed", sizeof(struct pfsync_del_c),
125 pfsync_print_del_c },
126 { "frag insert", 0, NULL },
127 { "frag delete", 0, NULL },
128 { "bulk update status", sizeof(struct pfsync_bus),
129 pfsync_print_bus },
130 { "tdb", 0, pfsync_print_tdb },
131 { "eof", 0, NULL },
132 { "insert", sizeof(struct pfsync_state_1400), pfsync_print_state_1400 },
133 { "update", sizeof(struct pfsync_state_1400), pfsync_print_state_1400 },
134 };
135
136 static void
pfsync_print(netdissect_options * ndo,struct pfsync_header * hdr,const u_char * bp,u_int len)137 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
138 const u_char *bp, u_int len)
139 {
140 struct pfsync_subheader *subh;
141 int count, plen, i;
142 u_int alen;
143
144 plen = ntohs(hdr->len);
145
146 ND_PRINT("PFSYNCv%d len %d", hdr->version, plen);
147
148 if (hdr->version != PFSYNC_VERSION)
149 return;
150
151 plen -= sizeof(*hdr);
152
153 while (plen > 0) {
154 if (len < sizeof(*subh))
155 break;
156
157 subh = (struct pfsync_subheader *)bp;
158 bp += sizeof(*subh);
159 len -= sizeof(*subh);
160 plen -= sizeof(*subh);
161
162 if (subh->action >= PFSYNC_ACT_MAX) {
163 ND_PRINT("\n act UNKNOWN id %d",
164 subh->action);
165 return;
166 }
167
168 count = ntohs(subh->count);
169 ND_PRINT("\n %s count %d", actions[subh->action].name,
170 count);
171 alen = actions[subh->action].len;
172
173 if (subh->action == PFSYNC_ACT_EOF)
174 return;
175
176 if (actions[subh->action].print == NULL) {
177 ND_PRINT("\n unimplemented action %hhu",
178 subh->action);
179 return;
180 }
181
182 for (i = 0; i < count; i++) {
183 if (len < alen) {
184 len = 0;
185 break;
186 }
187
188 if (ndo->ndo_vflag)
189 actions[subh->action].print(ndo, bp);
190
191 bp += alen;
192 len -= alen;
193 plen -= alen;
194 }
195 }
196
197 if (plen > 0) {
198 ND_PRINT("\n ...");
199 return;
200 }
201 if (plen < 0) {
202 ND_PRINT("\n invalid header length");
203 return;
204 }
205 if (len > 0)
206 ND_PRINT("\n invalid packet length");
207 }
208
209 static void
pfsync_print_clr(netdissect_options * ndo,const void * bp)210 pfsync_print_clr(netdissect_options *ndo, const void *bp)
211 {
212 const struct pfsync_clr *clr = bp;
213
214 ND_PRINT("\n\tcreatorid: %08x", htonl(clr->creatorid));
215 if (clr->ifname[0] != '\0')
216 ND_PRINT(" interface: %s", clr->ifname);
217 }
218
219 static void
pfsync_print_state_1301(netdissect_options * ndo,const void * bp)220 pfsync_print_state_1301(netdissect_options *ndo, const void *bp)
221 {
222 struct pfsync_state_1301 *st = (struct pfsync_state_1301 *)bp;
223
224 fn_print_char(ndo, '\n');
225 print_state(ndo, (union pfsync_state_union *)st, PFSYNC_MSG_VERSION_1301);
226 }
227
228 static void
pfsync_print_state_1400(netdissect_options * ndo,const void * bp)229 pfsync_print_state_1400(netdissect_options *ndo, const void *bp)
230 {
231 struct pfsync_state_1301 *st = (struct pfsync_state_1301 *)bp;
232
233 fn_print_char(ndo, '\n');
234 print_state(ndo, (union pfsync_state_union *)st, PFSYNC_MSG_VERSION_1400);
235 }
236
237 static void
pfsync_print_ins_ack(netdissect_options * ndo,const void * bp)238 pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
239 {
240 const struct pfsync_ins_ack *iack = bp;
241
242 ND_PRINT("\n\tid: %016jx creatorid: %08x",
243 (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid));
244 }
245
246 static void
pfsync_print_upd_c(netdissect_options * ndo,const void * bp)247 pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
248 {
249 const struct pfsync_upd_c *u = bp;
250
251 ND_PRINT("\n\tid: %016jx creatorid: %08x",
252 (uintmax_t)be64toh(u->id), ntohl(u->creatorid));
253 if (ndo->ndo_vflag > 2) {
254 ND_PRINT("\n\tTCP? :");
255 print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
256 }
257 }
258
259 static void
pfsync_print_upd_req(netdissect_options * ndo,const void * bp)260 pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
261 {
262 const struct pfsync_upd_req *ur = bp;
263
264 ND_PRINT("\n\tid: %016jx creatorid: %08x",
265 (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid));
266 }
267
268 static void
pfsync_print_del_c(netdissect_options * ndo,const void * bp)269 pfsync_print_del_c(netdissect_options *ndo, const void *bp)
270 {
271 const struct pfsync_del_c *d = bp;
272
273 ND_PRINT("\n\tid: %016jx creatorid: %08x",
274 (uintmax_t)be64toh(d->id), ntohl(d->creatorid));
275 }
276
277 static void
pfsync_print_bus(netdissect_options * ndo,const void * bp)278 pfsync_print_bus(netdissect_options *ndo, const void *bp)
279 {
280 const struct pfsync_bus *b = bp;
281 uint32_t endtime;
282 int min, sec;
283 const char *status;
284
285 endtime = ntohl(b->endtime);
286 sec = endtime % 60;
287 endtime /= 60;
288 min = endtime % 60;
289 endtime /= 60;
290
291 switch (b->status) {
292 case PFSYNC_BUS_START:
293 status = "start";
294 break;
295 case PFSYNC_BUS_END:
296 status = "end";
297 break;
298 default:
299 status = "UNKNOWN";
300 break;
301 }
302
303 ND_PRINT("\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
304 htonl(b->creatorid), endtime, min, sec, status);
305 }
306
307 static void
pfsync_print_tdb(netdissect_options * ndo,const void * bp)308 pfsync_print_tdb(netdissect_options *ndo, const void *bp)
309 {
310 const struct pfsync_tdb *t = bp;
311
312 ND_PRINT("\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
313 ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
314 (uintmax_t )be64toh(t->cur_bytes));
315 }
316
317 static void
print_host(netdissect_options * ndo,struct pf_addr * addr,uint16_t port,sa_family_t af,const char * proto)318 print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
319 sa_family_t af, const char *proto)
320 {
321 char buf[48];
322
323 if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
324 ND_PRINT("?");
325 else
326 ND_PRINT("%s", buf);
327
328 if (port)
329 ND_PRINT(".%hu", ntohs(port));
330 }
331
332 static void
print_seq(netdissect_options * ndo,const struct pfsync_state_peer * p)333 print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
334 {
335 if (p->seqdiff)
336 ND_PRINT("[%u + %u](+%u)", ntohl(p->seqlo),
337 ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
338 else
339 ND_PRINT("[%u + %u]", ntohl(p->seqlo),
340 ntohl(p->seqhi) - ntohl(p->seqlo));
341 }
342
343 static void
print_src_dst(netdissect_options * ndo,const struct pfsync_state_peer * src,const struct pfsync_state_peer * dst,uint8_t proto)344 print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
345 const struct pfsync_state_peer *dst, uint8_t proto)
346 {
347
348 if (proto == IPPROTO_TCP) {
349 if (src->state <= TCPS_TIME_WAIT &&
350 dst->state <= TCPS_TIME_WAIT)
351 ND_PRINT(" %s:%s", tcpstates[src->state],
352 tcpstates[dst->state]);
353 else if (src->state == PF_TCPS_PROXY_SRC ||
354 dst->state == PF_TCPS_PROXY_SRC)
355 ND_PRINT(" PROXY:SRC");
356 else if (src->state == PF_TCPS_PROXY_DST ||
357 dst->state == PF_TCPS_PROXY_DST)
358 ND_PRINT(" PROXY:DST");
359 else
360 ND_PRINT(" <BAD STATE LEVELS %u:%u>",
361 src->state, dst->state);
362 if (ndo->ndo_vflag > 1) {
363 ND_PRINT("\n\t");
364 print_seq(ndo, src);
365 if (src->wscale && dst->wscale)
366 ND_PRINT(" wscale %u",
367 src->wscale & PF_WSCALE_MASK);
368 ND_PRINT(" ");
369 print_seq(ndo, dst);
370 if (src->wscale && dst->wscale)
371 ND_PRINT(" wscale %u",
372 dst->wscale & PF_WSCALE_MASK);
373 }
374 } else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
375 dst->state < PFUDPS_NSTATES) {
376 const char *states[] = PFUDPS_NAMES;
377
378 ND_PRINT(" %s:%s", states[src->state], states[dst->state]);
379 } else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
380 dst->state < PFOTHERS_NSTATES) {
381 /* XXX ICMP doesn't really have state levels */
382 const char *states[] = PFOTHERS_NAMES;
383
384 ND_PRINT(" %s:%s", states[src->state], states[dst->state]);
385 } else {
386 ND_PRINT(" %u:%u", src->state, dst->state);
387 }
388 }
389
390 static void
print_state(netdissect_options * ndo,union pfsync_state_union * s,int version)391 print_state(netdissect_options *ndo, union pfsync_state_union *s, int version)
392 {
393 struct pfsync_state_peer *src, *dst;
394 struct pfsync_state_key *sk, *nk;
395 int min, sec;
396
397 if (s->pfs_1301.direction == PF_OUT) {
398 src = &s->pfs_1301.src;
399 dst = &s->pfs_1301.dst;
400 sk = &s->pfs_1301.key[PF_SK_STACK];
401 nk = &s->pfs_1301.key[PF_SK_WIRE];
402 if (s->pfs_1301.proto == IPPROTO_ICMP || s->pfs_1301.proto == IPPROTO_ICMPV6)
403 sk->port[0] = nk->port[0];
404 } else {
405 src = &s->pfs_1301.dst;
406 dst = &s->pfs_1301.src;
407 sk = &s->pfs_1301.key[PF_SK_WIRE];
408 nk = &s->pfs_1301.key[PF_SK_STACK];
409 if (s->pfs_1301.proto == IPPROTO_ICMP || s->pfs_1301.proto == IPPROTO_ICMPV6)
410 sk->port[1] = nk->port[1];
411 }
412 ND_PRINT("\t%s ", s->pfs_1301.ifname);
413 ND_PRINT("proto %u ", s->pfs_1301.proto);
414
415 print_host(ndo, &nk->addr[1], nk->port[1], s->pfs_1301.af, NULL);
416 if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->pfs_1301.af) ||
417 nk->port[1] != sk->port[1]) {
418 ND_PRINT((" ("));
419 print_host(ndo, &sk->addr[1], sk->port[1], s->pfs_1301.af, NULL);
420 ND_PRINT(")");
421 }
422 if (s->pfs_1301.direction == PF_OUT)
423 ND_PRINT((" -> "));
424 else
425 ND_PRINT((" <- "));
426 print_host(ndo, &nk->addr[0], nk->port[0], s->pfs_1301.af, NULL);
427 if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->pfs_1301.af) ||
428 nk->port[0] != sk->port[0]) {
429 ND_PRINT((" ("));
430 print_host(ndo, &sk->addr[0], sk->port[0], s->pfs_1301.af, NULL);
431 ND_PRINT((")"));
432 }
433
434 print_src_dst(ndo, src, dst, s->pfs_1301.proto);
435
436 if (ndo->ndo_vflag > 1) {
437 uint64_t packets[2];
438 uint64_t bytes[2];
439 uint32_t creation = ntohl(s->pfs_1301.creation);
440 uint32_t expire = ntohl(s->pfs_1301.expire);
441
442 sec = creation % 60;
443 creation /= 60;
444 min = creation % 60;
445 creation /= 60;
446 ND_PRINT("\n\tage %.2u:%.2u:%.2u", creation, min, sec);
447 sec = expire % 60;
448 expire /= 60;
449 min = expire % 60;
450 expire /= 60;
451 ND_PRINT(", expires in %.2u:%.2u:%.2u", expire, min, sec);
452
453 bcopy(s->pfs_1301.packets[0], &packets[0], sizeof(uint64_t));
454 bcopy(s->pfs_1301.packets[1], &packets[1], sizeof(uint64_t));
455 bcopy(s->pfs_1301.bytes[0], &bytes[0], sizeof(uint64_t));
456 bcopy(s->pfs_1301.bytes[1], &bytes[1], sizeof(uint64_t));
457 ND_PRINT(", %ju:%ju pkts, %ju:%ju bytes",
458 be64toh(packets[0]), be64toh(packets[1]),
459 be64toh(bytes[0]), be64toh(bytes[1]));
460 if (s->pfs_1301.anchor != ntohl(-1))
461 ND_PRINT(", anchor %u", ntohl(s->pfs_1301.anchor));
462 if (s->pfs_1301.rule != ntohl(-1))
463 ND_PRINT(", rule %u", ntohl(s->pfs_1301.rule));
464 }
465 if (ndo->ndo_vflag > 1) {
466 uint64_t id;
467
468 bcopy(&s->pfs_1301.id, &id, sizeof(uint64_t));
469 ND_PRINT("\n\tid: %016jx creatorid: %08x",
470 (uintmax_t )be64toh(id), ntohl(s->pfs_1301.creatorid));
471 }
472 }
473