]> The Tcpdump Group git mirrors - tcpdump/blob - print-pflog.c
We have to set the filter on every new file.
[tcpdump] / print-pflog.c
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #ifndef HAVE_NET_PFVAR_H
27 #error "No pf headers available"
28 #endif
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 #include <net/pfvar.h>
33 #include <net/if_pflog.h>
34
35 #include <netdissect-stdinc.h>
36
37 #include "netdissect.h"
38 #include "extract.h"
39
40 static const char tstr[] = "[|pflog]";
41
42 static const struct tok pf_reasons[] = {
43 { 0, "0(match)" },
44 { 1, "1(bad-offset)" },
45 { 2, "2(fragment)" },
46 { 3, "3(short)" },
47 { 4, "4(normalize)" },
48 { 5, "5(memory)" },
49 { 6, "6(bad-timestamp)" },
50 { 7, "7(congestion)" },
51 { 8, "8(ip-option)" },
52 { 9, "9(proto-cksum)" },
53 { 10, "10(state-mismatch)" },
54 { 11, "11(state-insert)" },
55 { 12, "12(state-limit)" },
56 { 13, "13(src-limit)" },
57 { 14, "14(synproxy)" },
58 { 0, NULL }
59 };
60
61 static const struct tok pf_actions[] = {
62 { PF_PASS, "pass" },
63 { PF_DROP, "block" },
64 { PF_SCRUB, "scrub" },
65 { PF_NAT, "nat" },
66 { PF_NONAT, "nat" },
67 { PF_BINAT, "binat" },
68 { PF_NOBINAT, "binat" },
69 { PF_RDR, "rdr" },
70 { PF_NORDR, "rdr" },
71 { PF_SYNPROXY_DROP, "synproxy-drop" },
72 { 0, NULL }
73 };
74
75 static const struct tok pf_directions[] = {
76 { PF_INOUT, "in/out" },
77 { PF_IN, "in" },
78 { PF_OUT, "out" },
79 { 0, NULL }
80 };
81
82 /* For reading capture files on other systems */
83 #define OPENBSD_AF_INET 2
84 #define OPENBSD_AF_INET6 24
85
86 static void
87 pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
88 {
89 uint32_t rulenr, subrulenr;
90
91 rulenr = EXTRACT_32BITS(&hdr->rulenr);
92 subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
93 if (subrulenr == (uint32_t)-1)
94 ND_PRINT((ndo, "rule %u/", rulenr));
95 else
96 ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr));
97
98 ND_PRINT((ndo, "%s: %s %s on %s: ",
99 tok2str(pf_reasons, "unkn(%u)", hdr->reason),
100 tok2str(pf_actions, "unkn(%u)", hdr->action),
101 tok2str(pf_directions, "unkn(%u)", hdr->dir),
102 hdr->ifname));
103 }
104
105 u_int
106 pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
107 register const u_char *p)
108 {
109 u_int length = h->len;
110 u_int hdrlen;
111 u_int caplen = h->caplen;
112 const struct pfloghdr *hdr;
113 uint8_t af;
114
115 /* check length */
116 if (caplen < sizeof(uint8_t)) {
117 ND_PRINT((ndo, "%s", tstr));
118 return (caplen);
119 }
120
121 #define MIN_PFLOG_HDRLEN 45
122 hdr = (struct pfloghdr *)p;
123 if (hdr->length < MIN_PFLOG_HDRLEN) {
124 ND_PRINT((ndo, "[pflog: invalid header length!]"));
125 return (hdr->length); /* XXX: not really */
126 }
127 hdrlen = BPF_WORDALIGN(hdr->length);
128
129 if (caplen < hdrlen) {
130 ND_PRINT((ndo, "%s", tstr));
131 return (hdrlen); /* XXX: true? */
132 }
133
134 /* print what we know */
135 hdr = (struct pfloghdr *)p;
136 ND_TCHECK(*hdr);
137 if (ndo->ndo_eflag)
138 pflog_print(ndo, hdr);
139
140 /* skip to the real packet */
141 af = hdr->af;
142 length -= hdrlen;
143 caplen -= hdrlen;
144 p += hdrlen;
145 switch (af) {
146
147 case AF_INET:
148 #if OPENBSD_AF_INET != AF_INET
149 case OPENBSD_AF_INET: /* XXX: read pcap files */
150 #endif
151 ip_print(ndo, p, length);
152 break;
153
154 #if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
155 #ifdef AF_INET6
156 case AF_INET6:
157 #endif /* AF_INET6 */
158 #if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
159 case OPENBSD_AF_INET6: /* XXX: read pcap files */
160 #endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
161 ip6_print(ndo, p, length);
162 break;
163 #endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
164
165 default:
166 /* address family not handled, print raw packet */
167 if (!ndo->ndo_eflag)
168 pflog_print(ndo, hdr);
169 if (!ndo->ndo_suppress_default_print)
170 ND_DEFAULTPRINT(p, caplen);
171 }
172
173 return (hdrlen);
174 trunc:
175 ND_PRINT((ndo, "%s", tstr));
176 return (hdrlen);
177 }
178
179 /*
180 * Local Variables:
181 * c-style: whitesmith
182 * c-basic-offset: 8
183 * End:
184 */