xref: /netbsd-src/external/bsd/tcpdump/dist/print-pfsync.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: print-pfsync.c,v 1.4 2017/01/24 23:29:14 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.4 2017/01/24 23:29:14 christos Exp $";
35 #else
36 __RCSID("$NetBSD: print-pfsync.c,v 1.4 2017/01/24 23:29:14 christos Exp $");
37 #endif
38 #endif
39 
40 #ifdef HAVE_CONFIG_H
41 #include "config.h"
42 #endif
43 
44 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <net/if.h>
47 #include <net/pfvar.h>
48 #include <net/if_pflog.h>
49 
50 #include <netdissect-stdinc.h>
51 
52 #include <sys/param.h>
53 #include <sys/time.h>
54 #include <sys/socket.h>
55 #include <sys/file.h>
56 #include <sys/ioctl.h>
57 
58 #ifdef __STDC__
59 struct rtentry;
60 #endif
61 #include <net/if.h>
62 
63 #if 0
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #endif
68 
69 #include <net/pfvar.h>
70 #include <net/if_pfsync.h>
71 
72 #include <ctype.h>
73 #include <netdb.h>
74 #include <pcap.h>
75 #include <signal.h>
76 #include <stdio.h>
77 #include <string.h>
78 
79 #include "interface.h"
80 #include "netdissect.h"
81 #include "pfctl_parser.h"
82 #include "pfctl.h"
83 
84 const char *pfsync_acts[] = { PFSYNC_ACTIONS };
85 
86 static void pfsync_print(netdissect_options *, struct pfsync_header *, int);
87 
88 u_int
89 pfsync_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h, const u_char *p)
90 {
91 	u_int caplen = h->caplen;
92 
93 	ts_print(ndo, &h->ts);
94 
95 	if (caplen < PFSYNC_HDRLEN) {
96 		ND_PRINT((ndo, "[|pfsync]"));
97 		goto out;
98 	}
99 
100 	pfsync_print(ndo, (struct pfsync_header *)p,
101 	    caplen - sizeof(struct pfsync_header));
102 out:
103 	if (ndo->ndo_suppress_default_print)
104 	    ND_DEFAULTPRINT((const u_char *)h, caplen);
105 	//putchar('\n');
106 
107 	return 0;
108 }
109 
110 void
111 pfsync_ip_print(netdissect_options *ndo, const u_char *bp, u_int len, const u_char *bp2 __unused)
112 {
113 	struct pfsync_header *hdr = (struct pfsync_header *)bp;
114 
115 	if (len < PFSYNC_HDRLEN)
116 		printf("[|pfsync]");
117 	else
118 		pfsync_print(ndo, hdr, (len - sizeof(struct pfsync_header)));
119 	//putchar('\n');
120 }
121 
122 static void
123 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr, int len)
124 {
125 	struct pfsync_state *s;
126 	struct pfsync_state_upd *u;
127 	struct pfsync_state_del *d;
128 	struct pfsync_state_clr *c;
129 	struct pfsync_state_upd_req *r;
130 	struct pfsync_state_bus *b;
131 	struct pfsync_tdb *t;
132 	int i, flags = 0, min, sec;
133 	u_int64_t id;
134 
135 	if (ndo->ndo_eflag)
136 		printf("PFSYNCv%d count %d: ",
137 		    hdr->version, hdr->count);
138 
139 	if (hdr->action < PFSYNC_ACT_MAX)
140 		printf("%s %s:", (ndo->ndo_vflag == 0) ? "PFSYNC" : "",
141 				pfsync_acts[hdr->action]);
142 	else
143 		printf("%s %d?:", (ndo->ndo_vflag == 0) ? "PFSYNC" : "",
144 				hdr->action);
145 
146 	if (!ndo->ndo_vflag)
147 		return;
148 	if (ndo->ndo_vflag)
149 		flags |= PF_OPT_VERBOSE;
150 	if (ndo->ndo_vflag > 1)
151 		flags |= PF_OPT_VERBOSE2;
152 	if (!ndo->ndo_nflag)
153 		flags |= PF_OPT_USEDNS;
154 
155 	switch (hdr->action) {
156 	case PFSYNC_ACT_CLR:
157 		if (sizeof(*c) <= len) {
158 			c = (void *)((char *)hdr + PFSYNC_HDRLEN);
159 			printf("\n\tcreatorid: %08x", htonl(c->creatorid));
160 			if (c->ifname[0] != '\0')
161 				printf(" interface: %s", c->ifname);
162 		}
163 	case PFSYNC_ACT_INS:
164 	case PFSYNC_ACT_UPD:
165 	case PFSYNC_ACT_DEL:
166 		for (i = 1, s = (void *)((char *)hdr + PFSYNC_HDRLEN);
167 		    i <= hdr->count && i * sizeof(*s) <= len; i++, s++) {
168 
169 			putchar('\n');
170 			print_state(s, flags);
171 			if (ndo->ndo_vflag > 1 && hdr->action == PFSYNC_ACT_UPD)
172 				printf(" updates: %d", s->updates);
173 		}
174 		break;
175 	case PFSYNC_ACT_UPD_C:
176 		for (i = 1, u = (void *)((char *)hdr + PFSYNC_HDRLEN);
177 		    i <= hdr->count && i * sizeof(*u) <= len; i++, u++) {
178 			memcpy(&id, &u->id, sizeof(id));
179 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
180 			    be64toh(id), ntohl(u->creatorid));
181 			if (ndo->ndo_vflag > 1)
182 				printf(" updates: %d", u->updates);
183 		}
184 		break;
185 	case PFSYNC_ACT_DEL_C:
186 		for (i = 1, d = (void *)((char *)hdr + PFSYNC_HDRLEN);
187 		    i <= hdr->count && i * sizeof(*d) <= len; i++, d++) {
188 			memcpy(&id, &d->id, sizeof(id));
189 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
190 			    be64toh(id), ntohl(d->creatorid));
191 		}
192 		break;
193 	case PFSYNC_ACT_UREQ:
194 		for (i = 1, r = (void *)((char *)hdr + PFSYNC_HDRLEN);
195 		    i <= hdr->count && i * sizeof(*r) <= len; i++, r++) {
196 			memcpy(&id, &r->id, sizeof(id));
197 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
198 			    be64toh(id), ntohl(r->creatorid));
199 		}
200 		break;
201 	case PFSYNC_ACT_BUS:
202 		if (sizeof(*b) <= len) {
203 			b = (void *)((char *)hdr + PFSYNC_HDRLEN);
204 			printf("\n\tcreatorid: %08x", htonl(b->creatorid));
205 			sec = b->endtime % 60;
206 			b->endtime /= 60;
207 			min = b->endtime % 60;
208 			b->endtime /= 60;
209 			printf(" age %.2u:%.2u:%.2u", b->endtime, min, sec);
210 			switch (b->status) {
211 			case PFSYNC_BUS_START:
212 				printf(" status: start");
213 				break;
214 			case PFSYNC_BUS_END:
215 				printf(" status: end");
216 				break;
217 			default:
218 				printf(" status: ?");
219 				break;
220 			}
221 		}
222 		break;
223 	case PFSYNC_ACT_TDB_UPD:
224 		for (i = 1, t = (void *)((char *)hdr + PFSYNC_HDRLEN);
225 		    i <= hdr->count && i * sizeof(*t) <= len; i++, t++)
226 			printf("\n\tspi: %08x rpl: %u cur_bytes: %" PRIu64,
227 			    htonl(t->spi), htonl(t->rpl),
228 			    be64toh(t->cur_bytes));
229 			/* XXX add dst and sproto? */
230 		break;
231 	default:
232 		break;
233 	}
234 }
235