xref: /netbsd-src/external/bsd/tcpdump/dist/print-pfsync.c (revision bab08dd002877d2f521c29b79f09e55b3c360e47)
1 /*	$NetBSD: print-pfsync.c,v 1.3 2016/01/23 19:08:02 christos Exp $	*/
2 /*	$OpenBSD: print-pfsync.c,v 1.30 2007/05/31 04:16:26 mcbride Exp $	*/
3 
4 /*
5  * Copyright (c) 2002 Michael Shalayeff
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #ifndef lint
32 #if 0
33 static const char rcsid[] =
34     "@(#) $Header: /cvsroot/src/external/bsd/tcpdump/dist/print-pfsync.c,v 1.3 2016/01/23 19:08:02 christos Exp $";
35 #else
36 __RCSID("$NetBSD: print-pfsync.c,v 1.3 2016/01/23 19:08:02 christos Exp $");
37 #endif
38 #endif
39 
40 #define NETDISECT_REWORKED
41 #ifdef HAVE_CONFIG_H
42 #include "config.h"
43 #endif
44 
45 #include <tcpdump-stdinc.h>
46 
47 #include <sys/param.h>
48 #include <sys/time.h>
49 #include <sys/socket.h>
50 #include <sys/file.h>
51 #include <sys/ioctl.h>
52 
53 #ifdef __STDC__
54 struct rtentry;
55 #endif
56 #include <net/if.h>
57 
58 #if 0
59 #include <netinet/in.h>
60 #include <netinet/in_systm.h>
61 #include <netinet/ip.h>
62 #endif
63 
64 #include <net/pfvar.h>
65 #include <net/if_pfsync.h>
66 
67 #include <ctype.h>
68 #include <netdb.h>
69 #include <pcap.h>
70 #include <signal.h>
71 #include <stdio.h>
72 #include <string.h>
73 
74 #include "interface.h"
75 #include "addrtoname.h"
76 #include "pfctl_parser.h"
77 #include "pfctl.h"
78 
79 const char *pfsync_acts[] = { PFSYNC_ACTIONS };
80 
81 static void pfsync_print(struct pfsync_header *, int);
82 
83 u_int
84 pfsync_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
85 {
86 	u_int caplen = h->caplen;
87 
88 	ts_print(ndo, &h->ts);
89 
90 	if (caplen < PFSYNC_HDRLEN) {
91 		ND_PRINT((ndo, "[|pfsync]"));
92 		goto out;
93 	}
94 
95 	pfsync_print((struct pfsync_header *)p,
96 	    caplen - sizeof(struct pfsync_header));
97 out:
98 	if (xflag) {
99 		default_print((const u_char *)h, caplen);
100 	}
101 	//putchar('\n');
102 
103 	return 0;
104 }
105 
106 void
107 pfsync_ip_print(const u_char *bp, u_int len, const u_char *bp2 __unused)
108 {
109 	struct pfsync_header *hdr = (struct pfsync_header *)bp;
110 
111 	if (len < PFSYNC_HDRLEN)
112 		printf("[|pfsync]");
113 	else
114 		pfsync_print(hdr, (len - sizeof(struct pfsync_header)));
115 	//putchar('\n');
116 }
117 
118 static void
119 pfsync_print(struct pfsync_header *hdr, int len)
120 {
121 	struct pfsync_state *s;
122 	struct pfsync_state_upd *u;
123 	struct pfsync_state_del *d;
124 	struct pfsync_state_clr *c;
125 	struct pfsync_state_upd_req *r;
126 	struct pfsync_state_bus *b;
127 	struct pfsync_tdb *t;
128 	int i, flags = 0, min, sec;
129 	u_int64_t id;
130 
131 	if (eflag)
132 		printf("PFSYNCv%d count %d: ",
133 		    hdr->version, hdr->count);
134 
135 	if (hdr->action < PFSYNC_ACT_MAX)
136 		printf("%s %s:", (vflag == 0) ? "PFSYNC" : "",
137 				pfsync_acts[hdr->action]);
138 	else
139 		printf("%s %d?:", (vflag == 0) ? "PFSYNC" : "",
140 				hdr->action);
141 
142 	if (!vflag)
143 		return;
144 	if (vflag)
145 		flags |= PF_OPT_VERBOSE;
146 	if (vflag > 1)
147 		flags |= PF_OPT_VERBOSE2;
148 	if (!nflag)
149 		flags |= PF_OPT_USEDNS;
150 
151 	switch (hdr->action) {
152 	case PFSYNC_ACT_CLR:
153 		if (sizeof(*c) <= len) {
154 			c = (void *)((char *)hdr + PFSYNC_HDRLEN);
155 			printf("\n\tcreatorid: %08x", htonl(c->creatorid));
156 			if (c->ifname[0] != '\0')
157 				printf(" interface: %s", c->ifname);
158 		}
159 	case PFSYNC_ACT_INS:
160 	case PFSYNC_ACT_UPD:
161 	case PFSYNC_ACT_DEL:
162 		for (i = 1, s = (void *)((char *)hdr + PFSYNC_HDRLEN);
163 		    i <= hdr->count && i * sizeof(*s) <= len; i++, s++) {
164 
165 			putchar('\n');
166 			print_state(s, flags);
167 			if (vflag > 1 && hdr->action == PFSYNC_ACT_UPD)
168 				printf(" updates: %d", s->updates);
169 		}
170 		break;
171 	case PFSYNC_ACT_UPD_C:
172 		for (i = 1, u = (void *)((char *)hdr + PFSYNC_HDRLEN);
173 		    i <= hdr->count && i * sizeof(*u) <= len; i++, u++) {
174 			memcpy(&id, &u->id, sizeof(id));
175 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
176 			    be64toh(id), ntohl(u->creatorid));
177 			if (vflag > 1)
178 				printf(" updates: %d", u->updates);
179 		}
180 		break;
181 	case PFSYNC_ACT_DEL_C:
182 		for (i = 1, d = (void *)((char *)hdr + PFSYNC_HDRLEN);
183 		    i <= hdr->count && i * sizeof(*d) <= len; i++, d++) {
184 			memcpy(&id, &d->id, sizeof(id));
185 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
186 			    be64toh(id), ntohl(d->creatorid));
187 		}
188 		break;
189 	case PFSYNC_ACT_UREQ:
190 		for (i = 1, r = (void *)((char *)hdr + PFSYNC_HDRLEN);
191 		    i <= hdr->count && i * sizeof(*r) <= len; i++, r++) {
192 			memcpy(&id, &r->id, sizeof(id));
193 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
194 			    be64toh(id), ntohl(r->creatorid));
195 		}
196 		break;
197 	case PFSYNC_ACT_BUS:
198 		if (sizeof(*b) <= len) {
199 			b = (void *)((char *)hdr + PFSYNC_HDRLEN);
200 			printf("\n\tcreatorid: %08x", htonl(b->creatorid));
201 			sec = b->endtime % 60;
202 			b->endtime /= 60;
203 			min = b->endtime % 60;
204 			b->endtime /= 60;
205 			printf(" age %.2u:%.2u:%.2u", b->endtime, min, sec);
206 			switch (b->status) {
207 			case PFSYNC_BUS_START:
208 				printf(" status: start");
209 				break;
210 			case PFSYNC_BUS_END:
211 				printf(" status: end");
212 				break;
213 			default:
214 				printf(" status: ?");
215 				break;
216 			}
217 		}
218 		break;
219 	case PFSYNC_ACT_TDB_UPD:
220 		for (i = 1, t = (void *)((char *)hdr + PFSYNC_HDRLEN);
221 		    i <= hdr->count && i * sizeof(*t) <= len; i++, t++)
222 			printf("\n\tspi: %08x rpl: %u cur_bytes: %" PRIu64,
223 			    htonl(t->spi), htonl(t->rpl),
224 			    be64toh(t->cur_bytes));
225 			/* XXX add dst and sproto? */
226 		break;
227 	default:
228 		break;
229 	}
230 }
231