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