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