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