2 * Routines for pflog (Firewall Logging) packet disassembly
4 * Copyright 2001 Mike Frantzen
7 * SPDX-License-Identifier: BSD-1-Clause
15 * https://cvsweb.openbsd.org/src/sys/net/if_pflog.c
16 * https://cvsweb.openbsd.org/src/sys/net/if_pflog.h
17 * https://cvsweb.openbsd.org/src/sys/net/pfvar.h
21 * https://cgit.freebsd.org/src/tree/sys/net/if_pflog.h
22 * https://cgit.freebsd.org/src/tree/sys/netpfil/pf/if_pflog.c
23 * https://cgit.freebsd.org/src/tree/sys/netpfil/pf/pf.h
27 * http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/dist/pf/net/if_pflog.c
28 * http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/dist/pf/net/if_pflog.h
29 * http://cvsweb.netbsd.org/bsdweb.cgi/src/sys/dist/pf/net/pfvar.h
31 * DragonFly BSD PF log:
33 * https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/net/pf/if_pflog.c
34 * https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/net/pf/if_pflog.h
35 * https://gitweb.dragonflybsd.org/dragonfly.git/blob/HEAD:/sys/net/pf/pfvar.h
37 * macOS/Darwin PF log:
39 * https://github.com/apple-oss-distributions/xnu/blob/main/bsd/net/if_pflog.c
40 * https://github.com/apple-oss-distributions/xnu/blob/main/bsd/net/if_pflog.h
41 * https://github.com/apple-oss-distributions/xnu/blob/main/bsd/net/pfvar.h
45 #include <epan/packet.h>
47 #include <epan/aftypes.h>
48 #include <epan/addr_resolv.h>
49 #include <epan/expert.h>
50 #include <epan/prefs.h>
52 #include <wsutil/ws_roundup.h>
54 void proto_register_pflog(void);
55 void proto_reg_handoff_pflog(void);
56 void proto_register_old_pflog(void);
57 void proto_reg_handoff_old_pflog(void);
59 static dissector_handle_t old_pflog_handle
;
60 static dissector_handle_t pflog_handle
;
61 static dissector_handle_t ip_handle
, ipv6_handle
;
64 static int proto_pflog
;
65 static int hf_pflog_length
;
66 static int hf_pflog_af
;
67 static int hf_pflog_action
;
68 static int hf_pflog_reason
;
69 static int hf_pflog_ifname
;
70 static int hf_pflog_ruleset
;
71 static int hf_pflog_rulenr
;
72 static int hf_pflog_subrulenr
;
73 static int hf_pflog_uid
;
74 static int hf_pflog_pid
;
75 static int hf_pflog_rule_uid
;
76 static int hf_pflog_rule_pid
;
77 static int hf_pflog_dir
;
78 static int hf_pflog_rewritten
;
79 static int hf_pflog_pad
;
80 static int hf_pflog_saddr_ipv4
;
81 static int hf_pflog_daddr_ipv4
;
82 static int hf_pflog_saddr_ipv6
;
83 static int hf_pflog_daddr_ipv6
;
84 static int hf_pflog_saddr
;
85 static int hf_pflog_daddr
;
86 static int hf_pflog_sport
;
87 static int hf_pflog_dport
;
90 static expert_field ei_pflog_invalid_header_length
;
93 static int proto_old_pflog
;
94 static int hf_old_pflog_af
;
95 static int hf_old_pflog_ifname
;
96 static int hf_old_pflog_rnr
;
97 static int hf_old_pflog_reason
;
98 static int hf_old_pflog_action
;
99 static int hf_old_pflog_dir
;
101 static int ett_old_pflog
;
104 * Because ENC_HOST_ENDIAN is either equal to ENC_BIG_ENDIAN or
105 * ENC_LITTLE_ENDIAN, it will be confusing if we use ENC_ values
106 * directly, as, if the current setting is "Host-endian", it'll
107 * look like "Big-endian" on big-endian machines and like
108 * "Little-endian" on little-endian machines, and will display
109 * as such if you open up the preferences.
111 #define ID_HOST_ENDIAN 0
112 #define ID_BIG_ENDIAN 1
113 #define ID_LITTLE_ENDIAN 2
115 static int id_endian
= ID_HOST_ENDIAN
;
116 static const enum_val_t id_endian_vals
[] = {
117 { "host", "Host-endian", ID_HOST_ENDIAN
},
118 { "big", "Big-endian", ID_BIG_ENDIAN
},
119 { "little", "Little-endian", ID_LITTLE_ENDIAN
},
124 * Length as of OpenBSD 3.4, not including padding.
126 #define LEN_PFLOG_OPENBSD_3_4 45
129 * Length as of OpenBSD 3.8, not including padding.
131 * Also the current length on DragonFly BSD, NetBSD, and Darwin;
132 * those all have the same log message header.
134 #define LEN_PFLOG_OPENBSD_3_8 61
137 * Length as of OpenBSD 4.9; there are 2 internal pad bytes, but no
138 * padding at the end.
140 #define LEN_PFLOG_OPENBSD_4_9 100
142 static const value_string pflog_af_vals
[] = {
143 { BSD_AF_INET
, "IPv4" },
144 { BSD_AF_INET6_BSD
, "IPv6" },
145 { BSD_AF_INET6_FREEBSD
, "IPv6" },
146 { BSD_AF_INET6_DARWIN
, "IPv6" },
153 * Past 14, these differ for different OSes.
155 static const value_string pflog_reason_vals
[] = {
165 { 9, "proto-cksum" },
166 { 10, "state-mismatch" },
167 { 11, "state-ins-fail" },
168 { 12, "max-states" },
169 { 13, "srcnode-limit" },
171 #if defined(__FreeBSD__)
172 { 15, "map-failed" },
173 #elif defined(__NetBSD__)
174 { 15, "state-locked" },
175 #elif defined(__OpenBSD__)
178 #elif defined(__APPLE__)
187 * Past 10, these differ for different OSes.
199 #define PF_SYNPROXY_DROP 10
200 #if defined(__FreeBSD__)
202 #elif defined(__OpenBSD__)
208 #elif defined(__APPLE__)
209 #define PF_DUMMYNET 11
210 #define PF_NODUMMYNET 12
212 #define PF_NONAT64 14
215 static const value_string pflog_action_vals
[] = {
217 { PF_DROP
, "block" },
218 { PF_SCRUB
, "scrub" },
220 { PF_NONAT
, "nonat" },
221 { PF_BINAT
, "binat" },
222 { PF_NOBINAT
, "nobinat" },
224 { PF_NORDR
, "nordr" },
225 { PF_SYNPROXY_DROP
, "synproxy-drop" },
226 #if defined(__FreeBSD__)
227 { PF_DEFER
, "defer" },
228 #elif defined(__OpenBSD__)
229 { PF_DEFER
, "defer" },
230 { PF_MATCH
, "match" },
231 { PF_DIVERT
, "divert" },
234 #elif defined(__APPLE__)
235 { PF_DUMMYNET
, "dummynet" },
236 { PF_NODUMMYNET
, "nodummynet" },
237 { PF_NAT64
, "nat64" },
238 { PF_NONAT64
, "nonat64" },
250 #define PF_FWD 3 /* for now, 3 is only used by OpenBSD */
252 static const value_string pflog_old_dir_vals
[] = {
254 { PF_OLD_OUT
, "out" },
258 static const value_string pflog_dir_vals
[] = {
259 { PF_INOUT
, "inout" },
267 dissect_pflog(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void* data _U_
)
270 proto_tree
*pflog_tree
;
271 proto_item
*ti
= NULL
, *ti_len
;
272 uint32_t length
, padded_length
;
274 const uint8_t *ifname
;
278 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "PFLOG");
280 ti
= proto_tree_add_item(tree
, proto_pflog
, tvb
, offset
, -1, ENC_NA
);
281 pflog_tree
= proto_item_add_subtree(ti
, ett_pflog
);
283 ti_len
= proto_tree_add_item_ret_uint(pflog_tree
, hf_pflog_length
, tvb
, offset
, 1, ENC_BIG_ENDIAN
, &length
);
284 if(length
< LEN_PFLOG_OPENBSD_3_4
)
286 expert_add_info_format(pinfo
, ti_len
, &ei_pflog_invalid_header_length
, "Invalid header length %u", length
);
289 padded_length
= WS_ROUNDUP_4(length
);
293 proto_tree_add_item_ret_uint(pflog_tree
, hf_pflog_af
, tvb
, offset
, 1, ENC_BIG_ENDIAN
, &af
);
296 proto_tree_add_item_ret_uint(pflog_tree
, hf_pflog_action
, tvb
, offset
, 1, ENC_BIG_ENDIAN
, &action
);
299 proto_tree_add_item(pflog_tree
, hf_pflog_reason
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
302 proto_tree_add_item_ret_string(pflog_tree
, hf_pflog_ifname
, tvb
, offset
, 16, ENC_ASCII
|ENC_NA
, pinfo
->pool
, &ifname
);
305 proto_tree_add_item(pflog_tree
, hf_pflog_ruleset
, tvb
, offset
, 16, ENC_ASCII
);
308 proto_tree_add_item_ret_int(pflog_tree
, hf_pflog_rulenr
, tvb
, offset
, 4, ENC_BIG_ENDIAN
, &rulenr
);
311 proto_tree_add_item(pflog_tree
, hf_pflog_subrulenr
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
314 if(length
>= LEN_PFLOG_OPENBSD_3_8
)
321 endian
= ENC_HOST_ENDIAN
;
325 endian
= ENC_BIG_ENDIAN
;
328 case ID_LITTLE_ENDIAN
:
329 endian
= ENC_LITTLE_ENDIAN
;
333 DISSECTOR_ASSERT_NOT_REACHED();
336 proto_tree_add_item(pflog_tree
, hf_pflog_uid
, tvb
, offset
, 4, endian
);
339 proto_tree_add_item(pflog_tree
, hf_pflog_pid
, tvb
, offset
, 4, endian
);
342 proto_tree_add_item(pflog_tree
, hf_pflog_rule_uid
, tvb
, offset
, 4, endian
);
345 proto_tree_add_item(pflog_tree
, hf_pflog_rule_pid
, tvb
, offset
, 4, endian
);
348 proto_tree_add_item(pflog_tree
, hf_pflog_dir
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
351 if(length
>= LEN_PFLOG_OPENBSD_4_9
)
353 proto_tree_add_item(pflog_tree
, hf_pflog_rewritten
, tvb
, offset
, 1, ENC_BIG_ENDIAN
);
356 /* Internal padding */
357 proto_tree_add_item(pflog_tree
, hf_pflog_pad
, tvb
, offset
, 2, ENC_NA
);
363 proto_tree_add_item(pflog_tree
, hf_pflog_saddr_ipv4
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
366 proto_tree_add_item(pflog_tree
, hf_pflog_daddr_ipv4
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
370 case BSD_AF_INET6_BSD
:
371 proto_tree_add_item(pflog_tree
, hf_pflog_saddr_ipv6
, tvb
, offset
, 16, ENC_NA
);
374 proto_tree_add_item(pflog_tree
, hf_pflog_daddr_ipv6
, tvb
, offset
, 16, ENC_NA
);
379 proto_tree_add_item(pflog_tree
, hf_pflog_saddr
, tvb
, offset
, 16, ENC_NA
);
382 proto_tree_add_item(pflog_tree
, hf_pflog_daddr
, tvb
, offset
, 16, ENC_NA
);
387 proto_tree_add_item(pflog_tree
, hf_pflog_sport
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
390 proto_tree_add_item(pflog_tree
, hf_pflog_dport
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
393 /* End-of-header padding */
394 proto_tree_add_item(pflog_tree
, hf_pflog_pad
, tvb
, offset
, 3, ENC_NA
);
398 proto_item_set_text(ti
, "PF Log %s %s on %s by rule %d",
399 val_to_str(af
, pflog_af_vals
, "unknown (%u)"),
400 val_to_str(action
, pflog_action_vals
, "unknown (%u)"),
403 proto_item_set_len(ti
, offset
);
405 /* Set the tvbuff for the payload after the header */
406 next_tvb
= tvb_new_subset_remaining(tvb
, padded_length
);
411 call_dissector(ip_handle
, next_tvb
, pinfo
, tree
);
414 case BSD_AF_INET6_BSD
:
415 case BSD_AF_INET6_FREEBSD
:
416 case BSD_AF_INET6_DARWIN
:
417 call_dissector(ipv6_handle
, next_tvb
, pinfo
, tree
);
421 call_data_dissector(next_tvb
, pinfo
, tree
);
425 col_prepend_fstr(pinfo
->cinfo
, COL_INFO
, "[%s %s/%d] ",
426 val_to_str(action
, pflog_action_vals
, "unknown (%u)"),
429 return tvb_captured_length(tvb
);
433 proto_register_pflog(void)
435 static hf_register_info hf
[] = {
437 { "Header Length", "pflog.length", FT_UINT8
, BASE_DEC
, NULL
, 0x0,
438 "Length of Header", HFILL
}},
440 { "Address Family", "pflog.af", FT_UINT32
, BASE_DEC
, VALS(pflog_af_vals
), 0x0,
441 "Protocol (IPv4 vs IPv6)", HFILL
}},
443 { "Action", "pflog.action", FT_UINT8
, BASE_DEC
, VALS(pflog_action_vals
), 0x0,
444 "Action taken by PF on the packet", HFILL
}},
446 { "Reason", "pflog.reason", FT_UINT8
, BASE_DEC
, VALS(pflog_reason_vals
), 0x0,
447 "Reason for logging the packet", HFILL
}},
449 { "Interface", "pflog.ifname", FT_STRING
, BASE_NONE
, NULL
, 0x0,
452 { "Ruleset", "pflog.ruleset", FT_STRING
, BASE_NONE
, NULL
, 0x0,
453 "Ruleset name in anchor", HFILL
}},
455 * XXX - these are u_int32_t/uint32_t in struct pfloghdr, but are
456 * FT_INT32 here, and at least one capture, from issue #6115, has
457 * 0xFFFFFFFF as a sub rule number; that looks suspiciously as
460 * At least in OpenBSD, the rule and subrule are unsigned in the
461 * kernel, and -1 - which really means 0xFFFFFFFFU - is used if
462 * there is no subrule. Perhaps we should treat that value
463 * specially and report it as "None" or something such as that.
466 { "Rule Number", "pflog.rulenr", FT_INT32
, BASE_DEC
, NULL
, 0x0,
467 "Last matched firewall main ruleset rule number", HFILL
}},
468 { &hf_pflog_subrulenr
,
469 { "Sub Rule Number", "pflog.subrulenr", FT_INT32
, BASE_DEC
, NULL
, 0x0,
470 "Last matched firewall anchored ruleset rule number", HFILL
}},
472 { "UID", "pflog.uid", FT_INT32
, BASE_DEC
, NULL
, 0x0,
475 { "PID", "pflog.pid", FT_INT32
, BASE_DEC
, NULL
, 0x0,
477 { &hf_pflog_rule_uid
,
478 { "Rule UID", "pflog.rule_uid", FT_INT32
, BASE_DEC
, NULL
, 0x0,
480 { &hf_pflog_rule_pid
,
481 { "Rule PID", "pflog.rule_pid", FT_INT32
, BASE_DEC
, NULL
, 0x0,
483 { &hf_pflog_rewritten
,
484 { "Rewritten", "pflog.rewritten", FT_UINT8
, BASE_DEC
, NULL
, 0x0,
487 { "Padding", "pflog.pad", FT_BYTES
, BASE_NONE
, NULL
, 0x0,
488 "Must be Zero", HFILL
}},
489 { &hf_pflog_saddr_ipv4
,
490 { "Source Address", "pflog.saddr.ipv4", FT_IPv4
, BASE_NONE
, NULL
, 0x0,
492 { &hf_pflog_daddr_ipv4
,
493 { "Destination Address", "pflog.daddr.ipv4", FT_IPv4
, BASE_NONE
, NULL
, 0x0,
495 { &hf_pflog_saddr_ipv6
,
496 { "Source Address", "pflog.saddr.ipv6", FT_IPv6
, BASE_NONE
, NULL
, 0x0,
498 { &hf_pflog_daddr_ipv6
,
499 { "Destination Address", "pflog.daddr.ipv6", FT_IPv6
, BASE_NONE
, NULL
, 0x0,
502 { "Source Address", "pflog.saddr.bytes", FT_BYTES
, BASE_NONE
, NULL
, 0x0,
505 { "Destination Address", "pflog.daddr.bytes", FT_BYTES
, BASE_NONE
, NULL
, 0x0,
508 { "Source Port", "pflog.sport", FT_UINT16
, BASE_DEC
, NULL
, 0x0,
511 { "Destination Port", "pflog.dport", FT_UINT16
, BASE_DEC
, NULL
, 0x0,
514 { "Direction", "pflog.dir", FT_UINT8
, BASE_DEC
, VALS(pflog_dir_vals
), 0x0,
515 "Direction of packet in stack (inbound versus outbound)", HFILL
}},
517 static int *ett
[] = { &ett_pflog
};
519 static ei_register_info ei
[] = {
520 { &ei_pflog_invalid_header_length
, { "pflog.invalid_header_length", PI_MALFORMED
, PI_ERROR
, "Invalid header length", EXPFILL
}},
523 expert_module_t
* expert_pflog
;
524 module_t
*pflog_module
;
526 proto_pflog
= proto_register_protocol("OpenBSD Packet Filter log file", "PFLOG", "pflog");
527 proto_register_field_array(proto_pflog
, hf
, array_length(hf
));
528 proto_register_subtree_array(ett
, array_length(ett
));
529 expert_pflog
= expert_register_protocol(proto_pflog
);
530 expert_register_field_array(expert_pflog
, ei
, array_length(ei
));
532 pflog_handle
= register_dissector("pflog", dissect_pflog
, proto_pflog
);
534 pflog_module
= prefs_register_protocol(proto_pflog
, NULL
);
536 prefs_register_enum_preference(pflog_module
, "id_endian",
537 "Byte order for UID and PID fields",
538 "Whether or not UID and PID fields are dissected in host, big, or little endian byte order",
539 &id_endian
, id_endian_vals
, false);
540 prefs_register_obsolete_preference(pflog_module
, "uid_endian");
544 proto_reg_handoff_pflog(void)
546 ip_handle
= find_dissector_add_dependency("ip", proto_pflog
);
547 ipv6_handle
= find_dissector_add_dependency("ipv6", proto_pflog
);
549 dissector_add_uint("wtap_encap", WTAP_ENCAP_PFLOG
, pflog_handle
);
553 dissect_old_pflog(tvbuff_t
*tvb
, packet_info
*pinfo
, proto_tree
*tree
, void *data _U_
)
556 proto_tree
*pflog_tree
;
559 const uint8_t *ifname
;
560 uint16_t rnr
, action
;
563 col_set_str(pinfo
->cinfo
, COL_PROTOCOL
, "PFLOG-OLD");
565 ti
= proto_tree_add_item(tree
, proto_old_pflog
, tvb
, 0, -1, ENC_NA
);
566 pflog_tree
= proto_item_add_subtree(ti
, ett_pflog
);
568 proto_tree_add_item(pflog_tree
, hf_old_pflog_af
, tvb
, offset
, 4, ENC_BIG_ENDIAN
);
570 af
= tvb_get_ntohl(tvb
, offset
);
573 proto_tree_add_item_ret_string(pflog_tree
, hf_old_pflog_ifname
, tvb
, offset
, 16, ENC_ASCII
|ENC_NA
, pinfo
->pool
, &ifname
);
576 proto_tree_add_item(pflog_tree
, hf_old_pflog_rnr
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
577 rnr
= tvb_get_ntohs(tvb
, offset
);
580 proto_tree_add_item(pflog_tree
, hf_old_pflog_reason
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
583 proto_tree_add_item(pflog_tree
, hf_old_pflog_action
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
584 action
= tvb_get_ntohs(tvb
, offset
);
587 proto_tree_add_item(pflog_tree
, hf_old_pflog_dir
, tvb
, offset
, 2, ENC_BIG_ENDIAN
);
590 proto_item_set_text(ti
, "PF Log (pre 3.4) %s %s on %s by rule %d",
591 val_to_str(af
, pflog_af_vals
, "unknown (%u)"),
592 val_to_str(action
, pflog_action_vals
, "unknown (%u)"),
595 proto_item_set_len(ti
, offset
);
597 /* Set the tvbuff for the payload after the header */
598 next_tvb
= tvb_new_subset_remaining(tvb
, offset
);
603 offset
+= call_dissector(ip_handle
, next_tvb
, pinfo
, tree
);
606 case BSD_AF_INET6_BSD
:
607 offset
+= call_dissector(ipv6_handle
, next_tvb
, pinfo
, tree
);
611 offset
+= call_data_dissector(next_tvb
, pinfo
, tree
);
615 col_prepend_fstr(pinfo
->cinfo
, COL_INFO
, "[%s %s/#%d] ",
616 val_to_str(action
, pflog_action_vals
, "unknown (%u)"),
624 proto_register_old_pflog(void)
626 static hf_register_info hf
[] = {
628 { "Address Family", "pflog.af", FT_UINT32
, BASE_DEC
, VALS(pflog_af_vals
), 0x0,
629 "Protocol (IPv4 vs IPv6)", HFILL
}},
630 { &hf_old_pflog_ifname
,
631 { "Interface", "pflog.ifname", FT_STRING
, BASE_NONE
, NULL
, 0x0,
634 { "Rule Number", "pflog.rnr", FT_INT16
, BASE_DEC
, NULL
, 0x0,
635 "Last matched firewall rule number", HFILL
}},
636 { &hf_old_pflog_reason
,
637 { "Reason", "pflog.reason", FT_UINT16
, BASE_DEC
, VALS(pflog_reason_vals
), 0x0,
638 "Reason for logging the packet", HFILL
}},
639 { &hf_old_pflog_action
,
640 { "Action", "pflog.action", FT_UINT16
, BASE_DEC
, VALS(pflog_action_vals
), 0x0,
641 "Action taken by PF on the packet", HFILL
}},
643 { "Direction", "pflog.dir", FT_UINT16
, BASE_DEC
, VALS(pflog_old_dir_vals
), 0x0,
644 "Direction of packet in stack (inbound versus outbound)", HFILL
}},
646 static int *ett
[] = { &ett_old_pflog
};
648 proto_old_pflog
= proto_register_protocol("OpenBSD Packet Filter log file, pre 3.4", "PFLOG-OLD", "pflog-old");
649 proto_register_field_array(proto_old_pflog
, hf
, array_length(hf
));
650 proto_register_subtree_array(ett
, array_length(ett
));
652 old_pflog_handle
= register_dissector("pflog-old", dissect_old_pflog
, proto_old_pflog
);
656 proto_reg_handoff_old_pflog(void)
658 dissector_add_uint("wtap_encap", WTAP_ENCAP_OLD_PFLOG
, old_pflog_handle
);
666 * indent-tabs-mode: nil
669 * ex: set shiftwidth=2 tabstop=8 expandtab:
670 * :indentSize=2:tabSize=8:noTabs=true: