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