]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/tcpdump/print-pfsync.c
Checkpoint initial integration work
[FreeBSD/FreeBSD.git] / contrib / tcpdump / print-pfsync.c
1 /*
2  * Copyright (c) 2012 Gleb Smirnoff <glebius@FreeBSD.org>
3  * Copyright (c) 2002 Michael Shalayeff
4  * Copyright (c) 2001 Daniel Hartmeier
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  * $OpenBSD: print-pfsync.c,v 1.38 2012/09/19 13:50:36 mikeb Exp $
29  * $OpenBSD: pf_print_state.c,v 1.11 2012/07/08 17:48:37 lteo Exp $
30  */
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <tcpdump-stdinc.h>
37
38 #include <sys/endian.h>
39 #include <net/if.h>
40 #include <net/pfvar.h>  /* XXX */
41 #include <net/if_pfsync.h>
42 #define TCPSTATES
43 #include <netinet/tcp_fsm.h>
44
45 #include <string.h>
46
47 #include "interface.h"
48 #include "addrtoname.h"
49
50 static void     pfsync_print(netdissect_options *, struct pfsync_header *,
51                     const u_char *, u_int);
52 static void     print_src_dst(netdissect_options *,
53                     const struct pfsync_state_peer *,
54                     const struct pfsync_state_peer *, uint8_t);
55 static void     print_state(netdissect_options *, struct pfsync_state *);
56
57 #ifdef notyet
58 void
59 pfsync_if_print(u_char *user, const struct pcap_pkthdr *h,
60     register const u_char *p)
61 {
62         u_int caplen = h->caplen;
63
64         ts_print(&h->ts);
65
66         if (caplen < PFSYNC_HDRLEN) {
67                 ND_PRINT((ndo, "[|pfsync]"));
68                 goto out;
69         }
70
71         pfsync_print((struct pfsync_header *)p,
72             p + sizeof(struct pfsync_header),
73             caplen - sizeof(struct pfsync_header));
74 out:
75         if (xflag) {
76                 default_print((const u_char *)p, caplen);
77         }
78         safeputchar(ndo, '\n');
79 }
80 #endif /* notyet */
81
82 void
83 pfsync_ip_print(netdissect_options *ndo , const u_char *bp, u_int len)
84 {
85         struct pfsync_header *hdr = (struct pfsync_header *)bp;
86
87         if (len < PFSYNC_HDRLEN)
88                 ND_PRINT((ndo, "[|pfsync]"));
89         else
90                 pfsync_print(ndo, hdr, bp + sizeof(struct pfsync_header),
91                     len - sizeof(struct pfsync_header));
92 }
93
94 struct pfsync_actions {
95         const char *name;
96         size_t len;
97         void (*print)(netdissect_options *, const void *);
98 };
99
100 static void     pfsync_print_clr(netdissect_options *, const void *);
101 static void     pfsync_print_state(netdissect_options *, const void *);
102 static void     pfsync_print_ins_ack(netdissect_options *, const void *);
103 static void     pfsync_print_upd_c(netdissect_options *, const void *);
104 static void     pfsync_print_upd_req(netdissect_options *, const void *);
105 static void     pfsync_print_del_c(netdissect_options *, const void *);
106 static void     pfsync_print_bus(netdissect_options *, const void *);
107 static void     pfsync_print_tdb(netdissect_options *, const void *);
108
109 struct pfsync_actions actions[] = {
110         { "clear all", sizeof(struct pfsync_clr),       pfsync_print_clr },
111         { "insert", sizeof(struct pfsync_state),        pfsync_print_state },
112         { "insert ack", sizeof(struct pfsync_ins_ack),  pfsync_print_ins_ack },
113         { "update", sizeof(struct pfsync_ins_ack),      pfsync_print_state },
114         { "update compressed", sizeof(struct pfsync_upd_c),
115                                                         pfsync_print_upd_c },
116         { "request uncompressed", sizeof(struct pfsync_upd_req),
117                                                         pfsync_print_upd_req },
118         { "delete", sizeof(struct pfsync_state),        pfsync_print_state },
119         { "delete compressed", sizeof(struct pfsync_del_c),
120                                                         pfsync_print_del_c },
121         { "frag insert", 0,                             NULL },
122         { "frag delete", 0,                             NULL },
123         { "bulk update status", sizeof(struct pfsync_bus),
124                                                         pfsync_print_bus },
125         { "tdb", 0,                                     pfsync_print_tdb },
126         { "eof", 0,                                     NULL },
127 };
128
129 static void
130 pfsync_print(netdissect_options *ndo, struct pfsync_header *hdr,
131     const u_char *bp, u_int len)
132 {
133         struct pfsync_subheader *subh;
134         int count, plen, i;
135         u_int alen;
136
137         plen = ntohs(hdr->len);
138
139         ND_PRINT((ndo, "PFSYNCv%d len %d", hdr->version, plen));
140
141         if (hdr->version != PFSYNC_VERSION)
142                 return;
143
144         plen -= sizeof(*hdr);
145
146         while (plen > 0) {
147                 if (len < sizeof(*subh))
148                         break;
149
150                 subh = (struct pfsync_subheader *)bp;
151                 bp += sizeof(*subh);
152                 len -= sizeof(*subh);
153                 plen -= sizeof(*subh);
154
155                 if (subh->action >= PFSYNC_ACT_MAX) {
156                         ND_PRINT((ndo, "\n    act UNKNOWN id %d",
157                             subh->action));
158                         return;
159                 }
160
161                 count = ntohs(subh->count);
162                 ND_PRINT((ndo, "\n    %s count %d", actions[subh->action].name,
163                     count));
164                 alen = actions[subh->action].len;
165
166                 if (subh->action == PFSYNC_ACT_EOF)
167                         return;
168
169                 if (actions[subh->action].print == NULL) {
170                         ND_PRINT((ndo, "\n    unimplemented action %hhu",
171                             subh->action));
172                         return;
173                 }
174
175                 for (i = 0; i < count; i++) {
176                         if (len < alen) {
177                                 len = 0;
178                                 break;
179                         }
180
181                         if (vflag)
182                                 actions[subh->action].print(ndo, bp);
183
184                         bp += alen;
185                         len -= alen;
186                         plen -= alen;
187                 }
188         }
189
190         if (plen > 0) {
191                 ND_PRINT((ndo, "\n    ..."));
192                 return;
193         }
194         if (plen < 0) {
195                 ND_PRINT((ndo, "\n    invalid header length"));
196                 return;
197         }
198         if (len > 0)
199                 ND_PRINT((ndo, "\n    invalid packet length"));
200 }
201
202 static void
203 pfsync_print_clr(netdissect_options *ndo, const void *bp)
204 {
205         const struct pfsync_clr *clr = bp;
206
207         ND_PRINT((ndo, "\n\tcreatorid: %08x", htonl(clr->creatorid)));
208         if (clr->ifname[0] != '\0')
209                 ND_PRINT((ndo, " interface: %s", clr->ifname));
210 }
211
212 static void
213 pfsync_print_state(netdissect_options *ndo, const void *bp)
214 {
215         struct pfsync_state *st = (struct pfsync_state *)bp;
216
217         safeputchar(ndo, '\n');
218         print_state(ndo, st);
219 }
220
221 static void
222 pfsync_print_ins_ack(netdissect_options *ndo, const void *bp)
223 {
224         const struct pfsync_ins_ack *iack = bp;
225
226         ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
227             (uintmax_t)be64toh(iack->id), ntohl(iack->creatorid)));
228 }
229
230 static void
231 pfsync_print_upd_c(netdissect_options *ndo, const void *bp)
232 {
233         const struct pfsync_upd_c *u = bp;
234
235         ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
236             (uintmax_t)be64toh(u->id), ntohl(u->creatorid)));
237         if (vflag > 2) {
238                 ND_PRINT((ndo, "\n\tTCP? :"));
239                 print_src_dst(ndo, &u->src, &u->dst, IPPROTO_TCP);
240         }
241 }
242
243 static void
244 pfsync_print_upd_req(netdissect_options *ndo, const void *bp)
245 {
246         const struct pfsync_upd_req *ur = bp;
247
248         ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
249             (uintmax_t)be64toh(ur->id), ntohl(ur->creatorid)));
250 }
251
252 static void
253 pfsync_print_del_c(netdissect_options *ndo, const void *bp)
254 {
255         const struct pfsync_del_c *d = bp;
256
257         ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
258             (uintmax_t)be64toh(d->id), ntohl(d->creatorid)));
259 }
260
261 static void
262 pfsync_print_bus(netdissect_options *ndo, const void *bp)
263 {
264         const struct pfsync_bus *b = bp;
265         uint32_t endtime;
266         int min, sec;
267         const char *status;
268
269         endtime = ntohl(b->endtime);
270         sec = endtime % 60;
271         endtime /= 60;
272         min = endtime % 60;
273         endtime /= 60;
274
275         switch (b->status) {
276         case PFSYNC_BUS_START:
277                 status = "start";
278                 break;
279         case PFSYNC_BUS_END:
280                 status = "end";
281                 break;
282         default:
283                 status = "UNKNOWN";
284                 break;
285         }
286
287         ND_PRINT((ndo, "\n\tcreatorid: %08x age: %.2u:%.2u:%.2u status: %s",
288             htonl(b->creatorid), endtime, min, sec, status));
289 }
290
291 static void
292 pfsync_print_tdb(netdissect_options *ndo, const void *bp)
293 {
294         const struct pfsync_tdb *t = bp;
295
296         ND_PRINT((ndo, "\n\tspi: 0x%08x rpl: %ju cur_bytes: %ju",
297             ntohl(t->spi), (uintmax_t )be64toh(t->rpl),
298             (uintmax_t )be64toh(t->cur_bytes)));
299 }
300
301 static void
302 print_host(netdissect_options *ndo, struct pf_addr *addr, uint16_t port,
303     sa_family_t af, const char *proto)
304 {
305         char buf[48];
306
307         if (inet_ntop(af, addr, buf, sizeof(buf)) == NULL)
308                 ND_PRINT((ndo, "?"));
309         else
310                 ND_PRINT((ndo, "%s", buf));
311
312         if (port)
313                 ND_PRINT((ndo, ".%hu", ntohs(port)));
314 }
315
316 static void
317 print_seq(netdissect_options *ndo, const struct pfsync_state_peer *p)
318 {
319         if (p->seqdiff)
320                 ND_PRINT((ndo, "[%u + %u](+%u)", ntohl(p->seqlo),
321                     ntohl(p->seqhi) - ntohl(p->seqlo), ntohl(p->seqdiff)));
322         else
323                 ND_PRINT((ndo, "[%u + %u]", ntohl(p->seqlo),
324                     ntohl(p->seqhi) - ntohl(p->seqlo)));
325 }
326
327 static void
328 print_src_dst(netdissect_options *ndo, const struct pfsync_state_peer *src,
329     const struct pfsync_state_peer *dst, uint8_t proto)
330 {
331
332         if (proto == IPPROTO_TCP) {
333                 if (src->state <= TCPS_TIME_WAIT &&
334                     dst->state <= TCPS_TIME_WAIT)
335                         ND_PRINT((ndo, "   %s:%s", tcpstates[src->state],
336                             tcpstates[dst->state]));
337                 else if (src->state == PF_TCPS_PROXY_SRC ||
338                     dst->state == PF_TCPS_PROXY_SRC)
339                         ND_PRINT((ndo, "   PROXY:SRC"));
340                 else if (src->state == PF_TCPS_PROXY_DST ||
341                     dst->state == PF_TCPS_PROXY_DST)
342                         ND_PRINT((ndo, "   PROXY:DST"));
343                 else
344                         ND_PRINT((ndo, "   <BAD STATE LEVELS %u:%u>",
345                             src->state, dst->state));
346                 if (vflag > 1) {
347                         ND_PRINT((ndo, "\n\t"));
348                         print_seq(ndo, src);
349                         if (src->wscale && dst->wscale)
350                                 ND_PRINT((ndo, " wscale %u",
351                                     src->wscale & PF_WSCALE_MASK));
352                         ND_PRINT((ndo, "  "));
353                         print_seq(ndo, dst);
354                         if (src->wscale && dst->wscale)
355                                 ND_PRINT((ndo, " wscale %u",
356                                     dst->wscale & PF_WSCALE_MASK));
357                 }
358         } else if (proto == IPPROTO_UDP && src->state < PFUDPS_NSTATES &&
359             dst->state < PFUDPS_NSTATES) {
360                 const char *states[] = PFUDPS_NAMES;
361
362                 ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
363         } else if (proto != IPPROTO_ICMP && src->state < PFOTHERS_NSTATES &&
364             dst->state < PFOTHERS_NSTATES) {
365                 /* XXX ICMP doesn't really have state levels */
366                 const char *states[] = PFOTHERS_NAMES;
367
368                 ND_PRINT((ndo, "   %s:%s", states[src->state], states[dst->state]));
369         } else {
370                 ND_PRINT((ndo, "   %u:%u", src->state, dst->state));
371         }
372 }
373
374 static void
375 print_state(netdissect_options *ndo, struct pfsync_state *s)
376 {
377         struct pfsync_state_peer *src, *dst;
378         struct pfsync_state_key *sk, *nk;
379         int min, sec;
380
381         if (s->direction == PF_OUT) {
382                 src = &s->src;
383                 dst = &s->dst;
384                 sk = &s->key[PF_SK_STACK];
385                 nk = &s->key[PF_SK_WIRE];
386                 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
387                         sk->port[0] = nk->port[0];
388         } else {
389                 src = &s->dst;
390                 dst = &s->src;
391                 sk = &s->key[PF_SK_WIRE];
392                 nk = &s->key[PF_SK_STACK];
393                 if (s->proto == IPPROTO_ICMP || s->proto == IPPROTO_ICMPV6)
394                         sk->port[1] = nk->port[1];
395         }
396         ND_PRINT((ndo, "\t%s ", s->ifname));
397         ND_PRINT((ndo, "proto %u ", s->proto));
398
399         print_host(ndo, &nk->addr[1], nk->port[1], s->af, NULL);
400         if (PF_ANEQ(&nk->addr[1], &sk->addr[1], s->af) ||
401             nk->port[1] != sk->port[1]) {
402                 ND_PRINT((ndo, " ("));
403                 print_host(ndo, &sk->addr[1], sk->port[1], s->af, NULL);
404                 ND_PRINT((ndo, ")"));
405         }
406         if (s->direction == PF_OUT)
407                 ND_PRINT((ndo, " -> "));
408         else
409                 ND_PRINT((ndo, " <- "));
410         print_host(ndo, &nk->addr[0], nk->port[0], s->af, NULL);
411         if (PF_ANEQ(&nk->addr[0], &sk->addr[0], s->af) ||
412             nk->port[0] != sk->port[0]) {
413                 ND_PRINT((ndo, " ("));
414                 print_host(ndo, &sk->addr[0], sk->port[0], s->af, NULL);
415                 ND_PRINT((ndo, ")"));
416         }
417
418         print_src_dst(ndo, src, dst, s->proto);
419
420         if (vflag > 1) {
421                 uint64_t packets[2];
422                 uint64_t bytes[2];
423                 uint32_t creation = ntohl(s->creation);
424                 uint32_t expire = ntohl(s->expire);
425
426                 sec = creation % 60;
427                 creation /= 60;
428                 min = creation % 60;
429                 creation /= 60;
430                 ND_PRINT((ndo, "\n\tage %.2u:%.2u:%.2u", creation, min, sec));
431                 sec = expire % 60;
432                 expire /= 60;
433                 min = expire % 60;
434                 expire /= 60;
435                 ND_PRINT((ndo, ", expires in %.2u:%.2u:%.2u", expire, min, sec));
436
437                 bcopy(s->packets[0], &packets[0], sizeof(uint64_t));
438                 bcopy(s->packets[1], &packets[1], sizeof(uint64_t));
439                 bcopy(s->bytes[0], &bytes[0], sizeof(uint64_t));
440                 bcopy(s->bytes[1], &bytes[1], sizeof(uint64_t));
441                 ND_PRINT((ndo, ", %ju:%ju pkts, %ju:%ju bytes",
442                     be64toh(packets[0]), be64toh(packets[1]),
443                     be64toh(bytes[0]), be64toh(bytes[1])));
444                 if (s->anchor != ntohl(-1))
445                         ND_PRINT((ndo, ", anchor %u", ntohl(s->anchor)));
446                 if (s->rule != ntohl(-1))
447                         ND_PRINT((ndo, ", rule %u", ntohl(s->rule)));
448         }
449         if (vflag > 1) {
450                 uint64_t id;
451
452                 bcopy(&s->id, &id, sizeof(uint64_t));
453                 ND_PRINT((ndo, "\n\tid: %016jx creatorid: %08x",
454                     (uintmax_t )be64toh(id), ntohl(s->creatorid)));
455         }
456 }