Upgrade to tcpdump-4.0.0.
[dragonfly.git] / contrib / tcpdump / print-pflog.c
CommitLineData
c8cf0f94
PA
1/*
2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22#ifndef lint
23static const char rcsid[] _U_ =
ea7b4bf5 24 "@(#) $Header: /tcpdump/master/tcpdump/print-pflog.c,v 1.16 2007-09-12 19:36:18 guy Exp $ (LBL)";
c8cf0f94
PA
25#endif
26
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
825e8fa1
PA
31#ifndef HAVE_NET_PFVAR_H
32#error "No pf headers available"
33#endif
825e8fa1 34#include <sys/types.h>
825e8fa1 35#include <sys/socket.h>
825e8fa1
PA
36#include <net/if.h>
37#include <net/pfvar.h>
38#include <net/if_pflog.h>
39
c8cf0f94
PA
40#include <tcpdump-stdinc.h>
41
42#include <stdio.h>
43#include <pcap.h>
44
45#include "interface.h"
46#include "addrtoname.h"
c8cf0f94
PA
47
48static struct tok pf_reasons[] = {
49 { 0, "0(match)" },
50 { 1, "1(bad-offset)" },
51 { 2, "2(fragment)" },
52 { 3, "3(short)" },
53 { 4, "4(normalize)" },
54 { 5, "5(memory)" },
66170f0a
PA
55 { 6, "6(bad-timestamp)" },
56 { 7, "7(congestion)" },
57 { 8, "8(ip-option)" },
58 { 9, "9(proto-cksum)" },
59 { 10, "10(state-mismatch)" },
60 { 11, "11(state-insert)" },
61 { 12, "12(state-limit)" },
62 { 13, "13(src-limit)" },
63 { 14, "14(synproxy)" },
c8cf0f94
PA
64 { 0, NULL }
65};
66
67static struct tok pf_actions[] = {
68 { PF_PASS, "pass" },
69 { PF_DROP, "block" },
70 { PF_SCRUB, "scrub" },
71 { PF_NAT, "nat" },
72 { PF_NONAT, "nat" },
73 { PF_BINAT, "binat" },
74 { PF_NOBINAT, "binat" },
75 { PF_RDR, "rdr" },
76 { PF_NORDR, "rdr" },
77 { PF_SYNPROXY_DROP, "synproxy-drop" },
78 { 0, NULL }
79};
80
81static struct tok pf_directions[] = {
82 { PF_INOUT, "in/out" },
83 { PF_IN, "in" },
84 { PF_OUT, "out" },
85 { 0, NULL }
86};
87
88/* For reading capture files on other systems */
89#define OPENBSD_AF_INET 2
90#define OPENBSD_AF_INET6 24
91
92static void
93pflog_print(const struct pfloghdr *hdr)
94{
95 u_int32_t rulenr, subrulenr;
96
97 rulenr = ntohl(hdr->rulenr);
98 subrulenr = ntohl(hdr->subrulenr);
99 if (subrulenr == (u_int32_t)-1)
100 printf("rule %u/", rulenr);
101 else
102 printf("rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr);
103
104 printf("%s: %s %s on %s: ",
105 tok2str(pf_reasons, "unkn(%u)", hdr->reason),
106 tok2str(pf_actions, "unkn(%u)", hdr->action),
107 tok2str(pf_directions, "unkn(%u)", hdr->dir),
108 hdr->ifname);
109}
110
111u_int
112pflog_if_print(const struct pcap_pkthdr *h, register const u_char *p)
113{
114 u_int length = h->len;
115 u_int hdrlen;
116 u_int caplen = h->caplen;
117 const struct pfloghdr *hdr;
118 u_int8_t af;
119
120 /* check length */
121 if (caplen < sizeof(u_int8_t)) {
122 printf("[|pflog]");
123 return (caplen);
124 }
125
126#define MIN_PFLOG_HDRLEN 45
127 hdr = (struct pfloghdr *)p;
128 if (hdr->length < MIN_PFLOG_HDRLEN) {
129 printf("[pflog: invalid header length!]");
130 return (hdr->length); /* XXX: not really */
131 }
132 hdrlen = BPF_WORDALIGN(hdr->length);
133
134 if (caplen < hdrlen) {
135 printf("[|pflog]");
136 return (hdrlen); /* XXX: true? */
137 }
138
139 /* print what we know */
140 hdr = (struct pfloghdr *)p;
141 TCHECK(*hdr);
142 if (eflag)
143 pflog_print(hdr);
144
145 /* skip to the real packet */
146 af = hdr->af;
147 length -= hdrlen;
148 caplen -= hdrlen;
149 p += hdrlen;
150 switch (af) {
151
152 case AF_INET:
153#if OPENBSD_AF_INET != AF_INET
154 case OPENBSD_AF_INET: /* XXX: read pcap files */
155#endif
156 ip_print(gndo, p, length);
157 break;
158
159#ifdef INET6
160 case AF_INET6:
161#if OPENBSD_AF_INET6 != AF_INET6
162 case OPENBSD_AF_INET6: /* XXX: read pcap files */
163#endif
164 ip6_print(p, length);
165 break;
166#endif
167
168 default:
169 /* address family not handled, print raw packet */
170 if (!eflag)
171 pflog_print(hdr);
172 if (!suppress_default_print)
173 default_print(p, caplen);
174 }
175
176 return (hdrlen);
177trunc:
178 printf("[|pflog]");
179 return (hdrlen);
180}
181
182/*
183 * Local Variables:
184 * c-style: whitesmith
185 * c-basic-offset: 8
186 * End:
187 */