Explicitly request literal mode after .Xr.
[netbsd-mini2440.git] / dist / tcpdump / print-pfsync.c
blob91af725fbabcf0280e05c1fffa089a641f2f5c76
1 /* $NetBSD: print-pfsync.c,v 1.1 2009/09/14 10:36:49 degroote Exp $ */
2 /* $OpenBSD: print-pfsync.c,v 1.30 2007/05/31 04:16:26 mcbride Exp $ */
4 /*
5 * Copyright (c) 2002 Michael Shalayeff
6 * All rights reserved.
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.
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.
30 #include <sys/cdefs.h>
31 #ifndef lint
32 #if 0
33 static const char rcsid[] =
34 "@(#) $Header: /pub/NetBSD/misc/repositories/cvsroot/src/dist/tcpdump/print-pfsync.c,v 1.2 2009/09/14 15:18:15 he Exp $";
35 #else
36 __RCSID("$NetBSD: print-pfsync.c,v 1.1 2009/09/14 10:36:49 degroote Exp $");
37 #endif
38 #endif
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/socket.h>
43 #include <sys/file.h>
44 #include <sys/ioctl.h>
45 #include <sys/mbuf.h>
47 #ifdef __STDC__
48 struct rtentry;
49 #endif
50 #include <net/if.h>
52 #include <netinet/in.h>
53 #include <netinet/in_systm.h>
54 #include <netinet/ip.h>
56 #include <net/pfvar.h>
57 #include <net/if_pfsync.h>
59 #include <ctype.h>
60 #include <netdb.h>
61 #include <pcap.h>
62 #include <signal.h>
63 #include <stdio.h>
64 #include <string.h>
66 #include "interface.h"
67 #include "addrtoname.h"
68 #include "pfctl_parser.h"
69 #include "pfctl.h"
71 const char *pfsync_acts[] = { PFSYNC_ACTIONS };
73 static void pfsync_print(struct pfsync_header *, int);
75 u_int
76 pfsync_if_print(const struct pcap_pkthdr *h, const u_char *p)
78 u_int caplen = h->caplen;
80 ts_print(&h->ts);
82 if (caplen < PFSYNC_HDRLEN) {
83 printf("[|pfsync]");
84 goto out;
87 pfsync_print((struct pfsync_header *)p,
88 caplen - sizeof(struct pfsync_header));
89 out:
90 if (xflag) {
91 default_print((const u_char *)h, caplen);
93 //putchar('\n');
95 return 0;
98 void
99 pfsync_ip_print(const u_char *bp, u_int len, const u_char *bp2 __unused)
101 struct pfsync_header *hdr = (struct pfsync_header *)bp;
103 if (len < PFSYNC_HDRLEN)
104 printf("[|pfsync]");
105 else
106 pfsync_print(hdr, (len - sizeof(struct pfsync_header)));
107 //putchar('\n');
110 static void
111 pfsync_print(struct pfsync_header *hdr, int len)
113 struct pfsync_state *s;
114 struct pfsync_state_upd *u;
115 struct pfsync_state_del *d;
116 struct pfsync_state_clr *c;
117 struct pfsync_state_upd_req *r;
118 struct pfsync_state_bus *b;
119 struct pfsync_tdb *t;
120 int i, flags = 0, min, sec;
121 u_int64_t id;
123 if (eflag)
124 printf("PFSYNCv%d count %d: ",
125 hdr->version, hdr->count);
127 if (hdr->action < PFSYNC_ACT_MAX)
128 printf("%s %s:", (vflag == 0) ? "PFSYNC" : "",
129 pfsync_acts[hdr->action]);
130 else
131 printf("%s %d?:", (vflag == 0) ? "PFSYNC" : "",
132 hdr->action);
134 if (!vflag)
135 return;
136 if (vflag)
137 flags |= PF_OPT_VERBOSE;
138 if (vflag > 1)
139 flags |= PF_OPT_VERBOSE2;
140 if (!nflag)
141 flags |= PF_OPT_USEDNS;
143 switch (hdr->action) {
144 case PFSYNC_ACT_CLR:
145 if (sizeof(*c) <= len) {
146 c = (void *)((char *)hdr + PFSYNC_HDRLEN);
147 printf("\n\tcreatorid: %08x", htonl(c->creatorid));
148 if (c->ifname[0] != '\0')
149 printf(" interface: %s", c->ifname);
151 case PFSYNC_ACT_INS:
152 case PFSYNC_ACT_UPD:
153 case PFSYNC_ACT_DEL:
154 for (i = 1, s = (void *)((char *)hdr + PFSYNC_HDRLEN);
155 i <= hdr->count && i * sizeof(*s) <= len; i++, s++) {
157 putchar('\n');
158 print_state(s, flags);
159 if (vflag > 1 && hdr->action == PFSYNC_ACT_UPD)
160 printf(" updates: %d", s->updates);
162 break;
163 case PFSYNC_ACT_UPD_C:
164 for (i = 1, u = (void *)((char *)hdr + PFSYNC_HDRLEN);
165 i <= hdr->count && i * sizeof(*u) <= len; i++, u++) {
166 memcpy(&id, &u->id, sizeof(id));
167 printf("\n\tid: %" PRIu64 " creatorid: %08x",
168 be64toh(id), ntohl(u->creatorid));
169 if (vflag > 1)
170 printf(" updates: %d", u->updates);
172 break;
173 case PFSYNC_ACT_DEL_C:
174 for (i = 1, d = (void *)((char *)hdr + PFSYNC_HDRLEN);
175 i <= hdr->count && i * sizeof(*d) <= len; i++, d++) {
176 memcpy(&id, &d->id, sizeof(id));
177 printf("\n\tid: %" PRIu64 " creatorid: %08x",
178 be64toh(id), ntohl(d->creatorid));
180 break;
181 case PFSYNC_ACT_UREQ:
182 for (i = 1, r = (void *)((char *)hdr + PFSYNC_HDRLEN);
183 i <= hdr->count && i * sizeof(*r) <= len; i++, r++) {
184 memcpy(&id, &r->id, sizeof(id));
185 printf("\n\tid: %" PRIu64 " creatorid: %08x",
186 be64toh(id), ntohl(r->creatorid));
188 break;
189 case PFSYNC_ACT_BUS:
190 if (sizeof(*b) <= len) {
191 b = (void *)((char *)hdr + PFSYNC_HDRLEN);
192 printf("\n\tcreatorid: %08x", htonl(b->creatorid));
193 sec = b->endtime % 60;
194 b->endtime /= 60;
195 min = b->endtime % 60;
196 b->endtime /= 60;
197 printf(" age %.2u:%.2u:%.2u", b->endtime, min, sec);
198 switch (b->status) {
199 case PFSYNC_BUS_START:
200 printf(" status: start");
201 break;
202 case PFSYNC_BUS_END:
203 printf(" status: end");
204 break;
205 default:
206 printf(" status: ?");
207 break;
210 break;
211 case PFSYNC_ACT_TDB_UPD:
212 for (i = 1, t = (void *)((char *)hdr + PFSYNC_HDRLEN);
213 i <= hdr->count && i * sizeof(*t) <= len; i++, t++)
214 printf("\n\tspi: %08x rpl: %u cur_bytes: %" PRIu64,
215 htonl(t->spi), htonl(t->rpl),
216 be64toh(t->cur_bytes));
217 /* XXX add dst and sproto? */
218 break;
219 default:
220 break;