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