xref: /freebsd/contrib/tcpdump/print-pfsync.c (revision cc16dea626cf2fc80cde667ac4798065108e596c)
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 #include <tcpdump-stdinc.h>
37 
38 #include <sys/endian.h>
39 #include <net/if.h>
40 #include <net/pfvar.h>	/* XXX */
41 #include <net/if_pfsync.h>
42 #include <netinet/ip.h>
43 #define	TCPSTATES
44 #include <netinet/tcp_fsm.h>
45 
46 #include <string.h>
47 
48 #include "interface.h"
49 #include "addrtoname.h"
50 
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 *);
55 
56 #ifdef notyet
57 void
58 pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
59     register const u_char *p)
60 {
61 	u_int caplen = h->caplen;
62 
63 	ts_print(&h->ts);
64 
65 	if (caplen < PFSYNC_HDRLEN) {
66 		printf("[|pfsync]");
67 		goto out;
68 	}
69 
70 	pfsync_print((struct pfsync_header *)p,
71 	    p + sizeof(struct pfsync_header),
72 	    caplen - sizeof(struct pfsync_header));
73 out:
74 	if (xflag) {
75 		default_print((const u_char *)p, caplen);
76 	}
77 	putchar('\n');
78 }
79 #endif /* notyet */
80 
81 void
82 pfsync_ip_print(const u_char *bp, u_int len)
83 {
84 	struct pfsync_header *hdr = (struct pfsync_header *)bp;
85 
86 	if (len < PFSYNC_HDRLEN)
87 		printf("[|pfsync]");
88 	else
89 		pfsync_print(hdr, bp + sizeof(struct pfsync_header),
90 		    len - sizeof(struct pfsync_header));
91 }
92 
93 struct pfsync_actions {
94 	const char *name;
95 	size_t len;
96 	void (*print)(const void *);
97 };
98 
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 *);
107 
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),
123 							pfsync_print_bus },
124 	{ "tdb", 0,					pfsync_print_tdb },
125 	{ "eof", 0,					NULL },
126 };
127 
128 static void
129 pfsync_print(struct pfsync_header *hdr, const u_char *bp, u_int len)
130 {
131 	struct pfsync_subheader *subh;
132 	int count, plen, i;
133 	u_int alen;
134 
135 	plen = ntohs(hdr->len);
136 
137 	printf("PFSYNCv%d len %d", hdr->version, plen);
138 
139 	if (hdr->version != PFSYNC_VERSION)
140 		return;
141 
142 	plen -= sizeof(*hdr);
143 
144 	while (plen > 0) {
145 		if (len < sizeof(*subh))
146 			break;
147 
148 		subh = (struct pfsync_subheader *)bp;
149 		bp += sizeof(*subh);
150 		len -= sizeof(*subh);
151 		plen -= sizeof(*subh);
152 
153 		if (subh->action >= PFSYNC_ACT_MAX) {
154 			printf("\n    act UNKNOWN id %d", subh->action);
155 			return;
156 		}
157 
158 		count = ntohs(subh->count);
159 		printf("\n    %s count %d", actions[subh->action].name, count);
160 		alen = actions[subh->action].len;
161 
162 		if (subh->action == PFSYNC_ACT_EOF)
163 			return;
164 
165 		if (actions[subh->action].print == NULL) {
166 			printf("\n    unimplemented action %hhu", subh->action);
167 			return;
168 		}
169 
170 		for (i = 0; i < count; i++) {
171 			if (len < alen) {
172 				len = 0;
173 				break;
174 			}
175 
176 			if (vflag)
177 				actions[subh->action].print(bp);
178 
179 			bp += alen;
180 			len -= alen;
181 			plen -= alen;
182 		}
183 	}
184 
185 	if (plen > 0) {
186 		printf("\n    ...");
187 		return;
188 	}
189 	if (plen < 0) {
190 		printf("\n    invalid header length");
191 		return;
192 	}
193 	if (len > 0)
194 		printf("\n    invalid packet length");
195 }
196 
197 static void
198 pfsync_print_clr(const void *bp)
199 {
200 	const struct pfsync_clr *clr = bp;
201 
202 	printf("\n\tcreatorid: %08x", htonl(clr->creatorid));
203 	if (clr->ifname[0] != '\0')
204 		printf(" interface: %s", clr->ifname);
205 }
206 
207 static void
208 pfsync_print_state(const void *bp)
209 {
210 	struct pfsync_state *st = (struct pfsync_state *)bp;
211 
212 	putchar('\n');
213 	print_state(st);
214 }
215 
216 static void
217 pfsync_print_ins_ack(const void *bp)
218 {
219 	const struct pfsync_ins_ack *iack = bp;
220 
221 	printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(iack->id),
222 	    ntohl(iack->creatorid));
223 }
224 
225 static void
226 pfsync_print_upd_c(const void *bp)
227 {
228 	const struct pfsync_upd_c *u = bp;
229 
230 	printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(u->id),
231 	    ntohl(u->creatorid));
232 	if (vflag > 2) {
233 		printf("\n\tTCP? :");
234 		print_src_dst(&u->src, &u->dst, IPPROTO_TCP);
235 	}
236 }
237 
238 static void
239 pfsync_print_upd_req(const void *bp)
240 {
241 	const struct pfsync_upd_req *ur = bp;
242 
243 	printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(ur->id),
244 	    ntohl(ur->creatorid));
245 }
246 
247 static void
248 pfsync_print_del_c(const void *bp)
249 {
250 	const struct pfsync_del_c *d = bp;
251 
252 	printf("\n\tid: %016jx creatorid: %08x", (uintmax_t )be64toh(d->id),
253 	    ntohl(d->creatorid));
254 }
255 
256 static void
257 pfsync_print_bus(const void *bp)
258 {
259 	const struct pfsync_bus *b = bp;
260 	uint32_t endtime;
261 	int min, sec;
262 	const char *status;
263 
264 	endtime = ntohl(b->endtime);
265 	sec = endtime % 60;
266 	endtime /= 60;
267 	min = endtime % 60;
268 	endtime /= 60;
269 
270 	switch (b->status) {
271 	case PFSYNC_BUS_START:
272 		status = "start";
273 		break;
274 	case PFSYNC_BUS_END:
275 		status = "end";
276 		break;
277 	default:
278 		status = "UNKNOWN";
279 		break;
280 	}
281 
282 	printf("\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
283 	    htonl(b->creatorid), endtime, min, sec, status);
284 }
285 
286 static void
287 pfsync_print_tdb(const void *bp)
288 {
289 	const struct pfsync_tdb *t = bp;
290 
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));
294 }
295 
296 static void
297 print_host(struct pf_addr *addr, uint16_t port, sa_family_t af,
298     const char *proto)
299 {
300 	char buf[48];
301 
302 	if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
303 		printf("?");
304 	else
305 		printf("%s", buf);
306 
307 	if (port)
308 		printf(".%hu", ntohs(port));
309 }
310 
311 static void
312 print_seq(const struct pfsync_state_peer *p)
313 {
314 	if (p->seqdiff)
315 		printf("[%u + %u](+%u)", ntohl(p->seqlo),
316 		    ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff));
317 	else
318 		printf("[%u + %u]", ntohl(p->seqlo),
319 		    ntohl(p->seqhi) - ntohl(p->seqlo));
320 }
321 
322 static void
323 print_src_dst(const struct pfsync_state_peer *src,
324     const struct pfsync_state_peer *dst, uint8_t proto)
325 {
326 
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");
338 		else
339 			printf("   <BAD STATE LEVELS %u:%u>",
340 			    src->state, dst->state);
341 		if (vflag > 1) {
342 			printf("\n\t");
343 			print_seq(src);
344 			if (src->wscale && dst->wscale)
345 				printf(" wscale %u",
346 				    src->wscale & PF_WSCALE_MASK);
347 			printf("  ");
348 			print_seq(dst);
349 			if (src->wscale && dst->wscale)
350 				printf(" wscale %u",
351 				    dst->wscale & PF_WSCALE_MASK);
352 		}
353 	} else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
354 	    dst->state < PFUDPS_NSTATES) {
355 		const char *states[] = PFUDPS_NAMES;
356 
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;
362 
363 		printf("   %s:%s", states[src->state], states[dst->state]);
364 	} else {
365 		printf("   %u:%u", src->state, dst->state);
366 	}
367 }
368 
369 static void
370 print_state(struct pfsync_state *s)
371 {
372 	struct pfsync_state_peer *src, *dst;
373 	struct pfsync_state_key *sk, *nk;
374 	int min, sec;
375 
376 	if (s->direction == PF_OUT) {
377 		src = &s->src;
378 		dst = &s->dst;
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];
383 	} else {
384 		src = &s->dst;
385 		dst = &s->src;
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];
390 	}
391 	printf("\t%s ", s->ifname);
392 	printf("proto %u ", s->proto);
393 
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]) {
397 		printf(" (");
398 		print_host(&sk->addr[1], sk->port[1], s->af, NULL);
399 		printf(")");
400 	}
401 	if (s->direction == PF_OUT)
402 		printf(" -> ");
403 	else
404 		printf(" <- ");
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]) {
408 		printf(" (");
409 		print_host(&sk->addr[0], sk->port[0], s->af, NULL);
410 		printf(")");
411 	}
412 
413 	print_src_dst(src, dst, s->proto);
414 
415 	if (vflag > 1) {
416 		uint64_t packets[2];
417 		uint64_t bytes[2];
418 		uint32_t creation = ntohl(s->creation);
419 		uint32_t expire = ntohl(s->expire);
420 
421 		sec = creation % 60;
422 		creation /= 60;
423 		min = creation % 60;
424 		creation /= 60;
425 		printf("\n\tage %.2u:%.2u:%.2u", creation, min, sec);
426 		sec = expire % 60;
427 		expire /= 60;
428 		min = expire % 60;
429 		expire /= 60;
430 		printf(", expires in %.2u:%.2u:%.2u", expire, min, sec);
431 
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));
443 	}
444 	if (vflag > 1) {
445 		uint64_t id;
446 
447 		bcopy(&s->id, &id, sizeof(uint64_t));
448 		printf("\n\tid: %016jx creatorid: %08x",
449 		    (uintmax_t )be64toh(id), ntohl(s->creatorid));
450 	}
451 }
452