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