xref: /freebsd/contrib/tcpdump/print-pfsync.c (revision d3d381b2b194b4d24853e92eecef55f262688d1a)
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 *, struct pfsync_state *);
59 
60 #ifdef notyet
61 void
62 pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
63     register const u_char *p)
64 {
65 	u_int caplen = h->caplen;
66 
67 	ts_print(&h->ts);
68 
69 	if (caplen < PFSYNC_HDRLEN) {
70 		ND_PRINT((ndo, "[|pfsync]"));
71 		goto out;
72 	}
73 
74 	pfsync_print((struct pfsync_header *)p,
75 	    p + sizeof(struct pfsync_header),
76 	    caplen - sizeof(struct pfsync_header));
77 out:
78 	if (xflag) {
79 		default_print((const u_char *)p, caplen);
80 	}
81 	safeputchar(ndo, '\n');
82 }
83 #endif /* notyet */
84 
85 void
86 pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
87 {
88 	struct pfsync_header *hdr = (struct pfsync_header *)bp;
89 
90 	if (len < PFSYNC_HDRLEN)
91 		ND_PRINT((ndo, "[|pfsync]"));
92 	else
93 		pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
94 		    len - sizeof(struct pfsync_header));
95 }
96 
97 struct pfsync_actions {
98 	const char *name;
99 	size_t len;
100 	void (*print)(netdissect_options *, const void *);
101 };
102 
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 *);
111 
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),
127 							pfsync_print_bus },
128 	{ "tdb", 0,					pfsync_print_tdb },
129 	{ "eof", 0,					NULL },
130 };
131 
132 static void
133 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
134     const u_char *bp, u_int len)
135 {
136 	struct pfsync_subheader *subh;
137 	int count, plen, i;
138 	u_int alen;
139 
140 	plen = ntohs(hdr->len);
141 
142 	ND_PRINT((ndo, "PFSYNCv%d len %d", hdr->version, plen));
143 
144 	if (hdr->version != PFSYNC_VERSION)
145 		return;
146 
147 	plen -= sizeof(*hdr);
148 
149 	while (plen > 0) {
150 		if (len < sizeof(*subh))
151 			break;
152 
153 		subh = (struct pfsync_subheader *)bp;
154 		bp += sizeof(*subh);
155 		len -= sizeof(*subh);
156 		plen -= sizeof(*subh);
157 
158 		if (subh->action >= PFSYNC_ACT_MAX) {
159 			ND_PRINT((ndo, "\n    act UNKNOWN id %d",
160 			    subh->action));
161 			return;
162 		}
163 
164 		count = ntohs(subh->count);
165 		ND_PRINT((ndo, "\n    %s count %d", actions[subh->action].name,
166 		    count));
167 		alen = actions[subh->action].len;
168 
169 		if (subh->action == PFSYNC_ACT_EOF)
170 			return;
171 
172 		if (actions[subh->action].print == NULL) {
173 			ND_PRINT((ndo, "\n    unimplemented action %hhu",
174 			    subh->action));
175 			return;
176 		}
177 
178 		for (i = 0; i < count; i++) {
179 			if (len < alen) {
180 				len = 0;
181 				break;
182 			}
183 
184 			if (ndo->ndo_vflag)
185 				actions[subh->action].print(ndo, bp);
186 
187 			bp += alen;
188 			len -= alen;
189 			plen -= alen;
190 		}
191 	}
192 
193 	if (plen > 0) {
194 		ND_PRINT((ndo, "\n    ..."));
195 		return;
196 	}
197 	if (plen < 0) {
198 		ND_PRINT((ndo, "\n    invalid header length"));
199 		return;
200 	}
201 	if (len > 0)
202 		ND_PRINT((ndo, "\n    invalid packet length"));
203 }
204 
205 static void
206 pfsync_print_clr(netdissect_options *ndo, const void *bp)
207 {
208 	const struct pfsync_clr *clr = bp;
209 
210 	ND_PRINT((ndo, "\n\tcreatorid: %08x", htonl(clr->creatorid)));
211 	if (clr->ifname[0] != '\0')
212 		ND_PRINT((ndo, " interface: %s", clr->ifname));
213 }
214 
215 static void
216 pfsync_print_state(netdissect_options *ndo, const void *bp)
217 {
218 	struct pfsync_state *st = (struct pfsync_state *)bp;
219 
220 	safeputchar(ndo, '\n');
221 	print_state(ndo, st);
222 }
223 
224 static void
225 pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
226 {
227 	const struct pfsync_ins_ack *iack = bp;
228 
229 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
230 	    (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid)));
231 }
232 
233 static void
234 pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
235 {
236 	const struct pfsync_upd_c *u = bp;
237 
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);
243 	}
244 }
245 
246 static void
247 pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
248 {
249 	const struct pfsync_upd_req *ur = bp;
250 
251 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
252 	    (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid)));
253 }
254 
255 static void
256 pfsync_print_del_c(netdissect_options *ndo, const void *bp)
257 {
258 	const struct pfsync_del_c *d = bp;
259 
260 	ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
261 	    (uintmax_t)be64toh(d->id), ntohl(d->creatorid)));
262 }
263 
264 static void
265 pfsync_print_bus(netdissect_options *ndo, const void *bp)
266 {
267 	const struct pfsync_bus *b = bp;
268 	uint32_t endtime;
269 	int min, sec;
270 	const char *status;
271 
272 	endtime = ntohl(b->endtime);
273 	sec = endtime % 60;
274 	endtime /= 60;
275 	min = endtime % 60;
276 	endtime /= 60;
277 
278 	switch (b->status) {
279 	case PFSYNC_BUS_START:
280 		status = "start";
281 		break;
282 	case PFSYNC_BUS_END:
283 		status = "end";
284 		break;
285 	default:
286 		status = "UNKNOWN";
287 		break;
288 	}
289 
290 	ND_PRINT((ndo, "\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
291 	    htonl(b->creatorid), endtime, min, sec, status));
292 }
293 
294 static void
295 pfsync_print_tdb(netdissect_options *ndo, const void *bp)
296 {
297 	const struct pfsync_tdb *t = bp;
298 
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)));
302 }
303 
304 static void
305 print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
306     sa_family_t af, const char *proto)
307 {
308 	char buf[48];
309 
310 	if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
311 		ND_PRINT((ndo, "?"));
312 	else
313 		ND_PRINT((ndo, "%s", buf));
314 
315 	if (port)
316 		ND_PRINT((ndo, ".%hu", ntohs(port)));
317 }
318 
319 static void
320 print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
321 {
322 	if (p->seqdiff)
323 		ND_PRINT((ndo, "[%u + %u](+%u)", ntohl(p->seqlo),
324 		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)));
325 	else
326 		ND_PRINT((ndo, "[%u + %u]", ntohl(p->seqlo),
327 		    ntohl(p->seqhi) - ntohl(p->seqlo)));
328 }
329 
330 static void
331 print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
332     const struct pfsync_state_peer *dst, uint8_t proto)
333 {
334 
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"));
346 		else
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"));
351 			print_seq(ndo, src);
352 			if (src->wscale && dst->wscale)
353 				ND_PRINT((ndo, " wscale %u",
354 				    src->wscale & PF_WSCALE_MASK));
355 			ND_PRINT((ndo, "  "));
356 			print_seq(ndo, dst);
357 			if (src->wscale && dst->wscale)
358 				ND_PRINT((ndo, " wscale %u",
359 				    dst->wscale & PF_WSCALE_MASK));
360 		}
361 	} else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
362 	    dst->state < PFUDPS_NSTATES) {
363 		const char *states[] = PFUDPS_NAMES;
364 
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;
370 
371 		ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
372 	} else {
373 		ND_PRINT((ndo, "   %u:%u", src->state, dst->state));
374 	}
375 }
376 
377 static void
378 print_state(netdissect_options *ndo, struct pfsync_state *s)
379 {
380 	struct pfsync_state_peer *src, *dst;
381 	struct pfsync_state_key *sk, *nk;
382 	int min, sec;
383 
384 	if (s->direction == PF_OUT) {
385 		src = &s->src;
386 		dst = &s->dst;
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];
391 	} else {
392 		src = &s->dst;
393 		dst = &s->src;
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];
398 	}
399 	ND_PRINT((ndo, "\t%s ", s->ifname));
400 	ND_PRINT((ndo, "proto %u ", s->proto));
401 
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, ")"));
408 	}
409 	if (s->direction == PF_OUT)
410 		ND_PRINT((ndo, " -> "));
411 	else
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, ")"));
419 	}
420 
421 	print_src_dst(ndo, src, dst, s->proto);
422 
423 	if (ndo->ndo_vflag > 1) {
424 		uint64_t packets[2];
425 		uint64_t bytes[2];
426 		uint32_t creation = ntohl(s->creation);
427 		uint32_t expire = ntohl(s->expire);
428 
429 		sec = creation % 60;
430 		creation /= 60;
431 		min = creation % 60;
432 		creation /= 60;
433 		ND_PRINT((ndo, "\n\tage %.2u:%.2u:%.2u", creation, min, sec));
434 		sec = expire % 60;
435 		expire /= 60;
436 		min = expire % 60;
437 		expire /= 60;
438 		ND_PRINT((ndo, ", expires in %.2u:%.2u:%.2u", expire, min, sec));
439 
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)));
451 	}
452 	if (ndo->ndo_vflag > 1) {
453 		uint64_t id;
454 
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)));
458 	}
459 }
460