2 * Routines for pflog (OpenBSD Firewall Logging) packet disassembly
4 * $Id: packet-pflog.c,v 1.3 2002/02/05 00:43:59 guy Exp $
6 * Copyright 2001 Mike Frantzen
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
41 #include <epan/packet.h>
43 #include <epan/resolv.h>
44 #include "packet-ip.h"
45 #include "packet-ipv6.h"
46 #include "packet-pflog.h"
49 /* Can't trust stddef.h to be there for us */
50 # define offsetof(type, member) ((size_t)(&((type *)0)->member))
53 static dissector_handle_t data_handle, ip_handle, ipv6_handle, pflog_handle;
56 static int proto_pflog = -1;
57 static int hf_pflog_af = -1;
58 static int hf_pflog_ifname = -1;
59 static int hf_pflog_rnr = -1;
60 static int hf_pflog_reason = -1;
61 static int hf_pflog_action = -1;
62 static int hf_pflog_dir = -1;
64 static gint ett_pflog = -1;
67 capture_pflog(const u_char *pd, int offset, int len, packet_counts *ld)
69 struct pfloghdr pflogh;
71 if (!BYTES_ARE_IN_FRAME(offset, len, (int)PFLOG_HDRLEN)) {
76 offset += PFLOG_HDRLEN;
78 /* Copy out the pflog header to insure alignment */
79 memcpy(&pflogh, pd, sizeof(pflogh));
85 capture_ip(pd, offset, len, ld);
90 capture_ipv6(pd, offset, len, ld);
100 static const value_string af_vals[] = {
101 { BSD_PF_INET, "IPv4" },
102 { BSD_PF_INET6, "IPv6" },
106 static const value_string reason_vals[] = {
116 static const value_string action_vals[] = {
117 { PF_PASS, "passed" },
118 { PF_DROP, "dropped" },
119 { PF_SCRUB, "scrubbed" },
123 static const value_string dir_vals[] = {
130 dissect_pflog(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
132 struct pfloghdr pflogh;
134 proto_tree *pflog_tree;
137 if (check_col(pinfo->cinfo, COL_PROTOCOL))
138 col_set_str(pinfo->cinfo, COL_PROTOCOL, "PFLOG");
140 /* Copy out the pflog header to insure alignment */
141 tvb_memcpy(tvb, (guint8 *)&pflogh, 0, sizeof(pflogh));
143 /* Byteswap the header now */
146 NTOHS(pflogh.reason);
147 NTOHS(pflogh.action);
151 ti = proto_tree_add_protocol_format(tree, proto_pflog, tvb, 0,
153 "PF Log %s %s on %s by rule %d",
154 val_to_str(pflogh.af, af_vals, "unknown (%u)"),
155 val_to_str(pflogh.action, action_vals, "unknown (%u)"),
158 pflog_tree = proto_item_add_subtree(ti, ett_pflog);
160 proto_tree_add_uint(pflog_tree, hf_pflog_af, tvb,
161 offsetof(struct pfloghdr, af), sizeof(pflogh.af),
163 proto_tree_add_int(pflog_tree, hf_pflog_rnr, tvb,
164 offsetof(struct pfloghdr, rnr), sizeof(pflogh.rnr),
166 proto_tree_add_string(pflog_tree, hf_pflog_ifname, tvb,
167 offsetof(struct pfloghdr, ifname), sizeof(pflogh.ifname),
169 proto_tree_add_uint(pflog_tree, hf_pflog_reason, tvb,
170 offsetof(struct pfloghdr, reason), sizeof(pflogh.reason),
172 proto_tree_add_uint(pflog_tree, hf_pflog_action, tvb,
173 offsetof(struct pfloghdr, action), sizeof(pflogh.action),
175 proto_tree_add_uint(pflog_tree, hf_pflog_dir, tvb,
176 offsetof(struct pfloghdr, dir), sizeof(pflogh.dir),
180 /* Set the tvbuff for the payload after the header */
181 next_tvb = tvb_new_subset(tvb, PFLOG_HDRLEN, -1, -1);
186 call_dissector(ip_handle, next_tvb, pinfo, tree);
190 call_dissector(ipv6_handle, next_tvb, pinfo, tree);
194 call_dissector(data_handle, next_tvb, pinfo, tree);
198 if (check_col(pinfo->cinfo, COL_INFO)) {
199 col_prepend_fstr(pinfo->cinfo, COL_INFO, "[%s %s/#%d] ",
200 val_to_str(pflogh.action, action_vals, "unknown (%u)"),
207 proto_register_pflog(void)
209 static hf_register_info hf[] = {
211 { "Address Family", "pflog.af", FT_UINT32, BASE_DEC, VALS(af_vals), 0x0,
212 "Protocol (IPv4 vs IPv6)", HFILL }},
214 { "Interface", "pflog.ifname", FT_STRING, BASE_NONE, NULL, 0x0,
215 "Interface", HFILL }},
217 { "Rule Number", "pflog.rnr", FT_INT16, BASE_DEC, NULL, 0x0,
218 "Last matched firewall rule number", HFILL }},
220 { "Reason", "pflog.reason", FT_UINT16, BASE_DEC, VALS(reason_vals), 0x0,
221 "Reason for logging the packet", HFILL }},
223 { "Action", "pflog.action", FT_UINT16, BASE_DEC, VALS(action_vals), 0x0,
224 "Action taken by PF on the packet", HFILL }},
226 { "Direction", "pflog.dir", FT_UINT16, BASE_DEC, VALS(dir_vals), 0x0,
227 "Direction of packet in stack (inbound versus outbound)", HFILL }},
229 static gint *ett[] = { &ett_pflog };
231 proto_pflog = proto_register_protocol("OpenBSD Packet Filter log file",
233 proto_register_field_array(proto_pflog, hf, array_length(hf));
234 proto_register_subtree_array(ett, array_length(ett));
236 register_dissector("pflog", dissect_pflog, proto_pflog);
240 proto_reg_handoff_pflog(void)
242 dissector_handle_t pflog_handle;
244 pflog_handle = find_dissector("pflog");
245 ip_handle = find_dissector("ip");
246 ipv6_handle = find_dissector("ipv6");
247 data_handle = find_dissector("data");
248 dissector_add("wtap_encap", WTAP_ENCAP_PFLOG, pflog_handle);