xref: /netbsd-src/external/bsd/tcpdump/dist/print-pfsync.c (revision c74ad2514f801c840bd3203347d1246afccaa98b)
1 /*	$NetBSD: print-pfsync.c,v 1.5 2023/08/17 20:19:40 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.5 2023/08/17 20:19:40 christos Exp $";
35 #else
36 __RCSID("$NetBSD: print-pfsync.c,v 1.5 2023/08/17 20:19:40 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 void
pfsync_if_print(netdissect_options * ndo,const struct pcap_pkthdr * h,const u_char * p)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("[|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 
108 void
pfsync_ip_print(netdissect_options * ndo,const u_char * bp,u_int len,const u_char * bp2 __unused)109 pfsync_ip_print(netdissect_options *ndo, const u_char *bp, u_int len, const u_char *bp2 __unused)
110 {
111 	struct pfsync_header *hdr = (struct pfsync_header *)bp;
112 
113 	if (len < PFSYNC_HDRLEN)
114 		printf("[|pfsync]");
115 	else
116 		pfsync_print(ndo, hdr, (len - sizeof(struct pfsync_header)));
117 	//putchar('\n');
118 }
119 
120 static void
pfsync_print(netdissect_options * ndo,struct pfsync_header * hdr,int len)121 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr, int len)
122 {
123 	struct pfsync_state *s;
124 	struct pfsync_state_upd *u;
125 	struct pfsync_state_del *d;
126 	struct pfsync_state_clr *c;
127 	struct pfsync_state_upd_req *r;
128 	struct pfsync_state_bus *b;
129 	struct pfsync_tdb *t;
130 	int i, flags = 0, min, sec;
131 	u_int64_t id;
132 
133 	if (ndo->ndo_eflag)
134 		printf("PFSYNCv%d count %d: ",
135 		    hdr->version, hdr->count);
136 
137 	if (hdr->action < PFSYNC_ACT_MAX)
138 		printf("%s %s:", (ndo->ndo_vflag == 0) ? "PFSYNC" : "",
139 				pfsync_acts[hdr->action]);
140 	else
141 		printf("%s %d?:", (ndo->ndo_vflag == 0) ? "PFSYNC" : "",
142 				hdr->action);
143 
144 	if (!ndo->ndo_vflag)
145 		return;
146 	if (ndo->ndo_vflag)
147 		flags |= PF_OPT_VERBOSE;
148 	if (ndo->ndo_vflag > 1)
149 		flags |= PF_OPT_VERBOSE2;
150 	if (!ndo->ndo_nflag)
151 		flags |= PF_OPT_USEDNS;
152 
153 	switch (hdr->action) {
154 	case PFSYNC_ACT_CLR:
155 		if (sizeof(*c) <= len) {
156 			c = (void *)((char *)hdr + PFSYNC_HDRLEN);
157 			printf("\n\tcreatorid: %08x", htonl(c->creatorid));
158 			if (c->ifname[0] != '\0')
159 				printf(" interface: %s", c->ifname);
160 		}
161 	case PFSYNC_ACT_INS:
162 	case PFSYNC_ACT_UPD:
163 	case PFSYNC_ACT_DEL:
164 		for (i = 1, s = (void *)((char *)hdr + PFSYNC_HDRLEN);
165 		    i <= hdr->count && i * sizeof(*s) <= len; i++, s++) {
166 
167 			putchar('\n');
168 			print_state(s, flags);
169 			if (ndo->ndo_vflag > 1 && hdr->action == PFSYNC_ACT_UPD)
170 				printf(" updates: %d", s->updates);
171 		}
172 		break;
173 	case PFSYNC_ACT_UPD_C:
174 		for (i = 1, u = (void *)((char *)hdr + PFSYNC_HDRLEN);
175 		    i <= hdr->count && i * sizeof(*u) <= len; i++, u++) {
176 			memcpy(&id, &u->id, sizeof(id));
177 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
178 			    be64toh(id), ntohl(u->creatorid));
179 			if (ndo->ndo_vflag > 1)
180 				printf(" updates: %d", u->updates);
181 		}
182 		break;
183 	case PFSYNC_ACT_DEL_C:
184 		for (i = 1, d = (void *)((char *)hdr + PFSYNC_HDRLEN);
185 		    i <= hdr->count && i * sizeof(*d) <= len; i++, d++) {
186 			memcpy(&id, &d->id, sizeof(id));
187 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
188 			    be64toh(id), ntohl(d->creatorid));
189 		}
190 		break;
191 	case PFSYNC_ACT_UREQ:
192 		for (i = 1, r = (void *)((char *)hdr + PFSYNC_HDRLEN);
193 		    i <= hdr->count && i * sizeof(*r) <= len; i++, r++) {
194 			memcpy(&id, &r->id, sizeof(id));
195 			printf("\n\tid: %" PRIu64 " creatorid: %08x",
196 			    be64toh(id), ntohl(r->creatorid));
197 		}
198 		break;
199 	case PFSYNC_ACT_BUS:
200 		if (sizeof(*b) <= len) {
201 			b = (void *)((char *)hdr + PFSYNC_HDRLEN);
202 			printf("\n\tcreatorid: %08x", htonl(b->creatorid));
203 			sec = b->endtime % 60;
204 			b->endtime /= 60;
205 			min = b->endtime % 60;
206 			b->endtime /= 60;
207 			printf(" age %.2u:%.2u:%.2u", b->endtime, min, sec);
208 			switch (b->status) {
209 			case PFSYNC_BUS_START:
210 				printf(" status: start");
211 				break;
212 			case PFSYNC_BUS_END:
213 				printf(" status: end");
214 				break;
215 			default:
216 				printf(" status: ?");
217 				break;
218 			}
219 		}
220 		break;
221 	case PFSYNC_ACT_TDB_UPD:
222 		for (i = 1, t = (void *)((char *)hdr + PFSYNC_HDRLEN);
223 		    i <= hdr->count && i * sizeof(*t) <= len; i++, t++)
224 			printf("\n\tspi: %08x rpl: %u cur_bytes: %" PRIu64,
225 			    htonl(t->spi), htonl(t->rpl),
226 			    be64toh(t->cur_bytes));
227 			/* XXX add dst and sproto? */
228 		break;
229 	default:
230 		break;
231 	}
232 }
233