2 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
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
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.
22 /* \summary: *BSD/Darwin packet filter log file printer */
28 #include "netdissect-stdinc.h"
30 #define ND_LONGJMP_FROM_TCHECK
31 #include "netdissect.h"
36 * pflog headers, at least as they exist now.
38 #define PFLOG_IFNAMSIZ 16
39 #define PFLOG_RULESET_NAME_SIZE 16
45 } pfa
; /* 128-bit address */
53 * 61 bytes long on NetBSD, DragonFly BSD. and Darwin;
54 * 84 bytes lon on OpenBSD;
55 * 72 bytes long on FreeBSD;
57 * which, unfortunately, does not allow us to distinguish, based on
58 * the header length, between the three OSes listed as having 61-byte
59 * headers. As the action values differ between them, this makes it
60 * impossible to correctly dissect the reason values that differ
61 * between NetBSD and Darwin (reason value 15) without having some
62 * way to explicitly tell tcpdump what to do.
64 * (We could, I guess, label reason value 15 as
65 * "state-locked (NetBSD)/dummynet (macOS etc.)" or something such as
73 char ifname
[PFLOG_IFNAMSIZ
];
74 char ruleset
[PFLOG_RULESET_NAME_SIZE
];
76 nd_uint32_t subrulenr
;
83 * This is the minimum pflog header length; it includes none of
84 * the fields added either by OpenBSD or FreeBSD, and doesn't
85 * include any padding.
87 #define MIN_PFLOG_HDRLEN 61
88 #if defined(__OpenBSD__)
95 #if defined(__FreeBSD__)
96 nd_uint32_t ridentifier
;
99 #elif defined(__OpenBSD__)
100 struct pf_addr saddr
;
101 struct pf_addr daddr
;
106 #define MAX_PFLOG_HDRLEN 100 /* 61 + 3 + 16 + 16 + 2 + 2 */
111 #define PFRES_MATCH 0
112 #define PFRES_BADOFF 1
114 #define PFRES_SHORT 3
116 #define PFRES_MEMORY 5
118 #define PFRES_CONGEST 7
119 #define PFRES_IPOPTIONS 8
120 #define PFRES_PROTCKSUM 9
121 #define PFRES_BADSTATE 10
122 #define PFRES_STATEINS 11
123 #define PFRES_MAXSTATES 12
124 #define PFRES_SRCLIMIT 13
125 #define PFRES_SYNPROXY 14
126 #if defined(__FreeBSD__)
127 #define PFRES_MAPFAILED 15
128 #elif defined(__NetBSD__)
129 #define PFRES_STATELOCKED 15
130 #elif defined(__OpenBSD__)
131 #define PFRES_TRANSLATE 15
132 #define PFRES_NOROUTE 16
133 #elif defined(__APPLE__)
134 #define PFRES_DUMMYNET 15
137 static const struct tok pf_reasons
[] = {
138 { PFRES_MATCH
, "0(match)" },
139 { PFRES_BADOFF
, "1(bad-offset)" },
140 { PFRES_FRAG
, "2(fragment)" },
141 { PFRES_SHORT
, "3(short)" },
142 { PFRES_NORM
, "4(normalize)" },
143 { PFRES_MEMORY
, "5(memory)" },
144 { PFRES_TS
, "6(bad-timestamp)" },
145 { PFRES_CONGEST
, "7(congestion)" },
146 { PFRES_IPOPTIONS
, "8(ip-option)" },
147 { PFRES_PROTCKSUM
, "9(proto-cksum)" },
148 { PFRES_BADSTATE
, "10(state-mismatch)" },
149 { PFRES_STATEINS
, "11(state-insert)" },
150 { PFRES_MAXSTATES
, "12(state-limit)" },
151 { PFRES_SRCLIMIT
, "13(src-limit)" },
152 { PFRES_SYNPROXY
, "14(synproxy)" },
153 #if defined(__FreeBSD__)
154 { PFRES_MAPFAILED
, "15(map-failed)" },
155 #elif defined(__NetBSD__)
156 { PFRES_STATELOCKED
, "15(state-locked)" },
157 #elif defined(__OpenBSD__)
158 { PFRES_TRANSLATE
, "15(translate)" },
159 { PFRES_NOROUTE
, "16(no-route)" },
160 #elif defined(__APPLE__)
161 { PFRES_DUMMYNET
, "15(dummynet)" },
179 #define PF_SYNPROXY_DROP 10
180 #if defined(__FreeBSD__)
183 #elif defined(__OpenBSD__)
189 #elif defined(__APPLE__)
190 #define PF_DUMMYNET 11
191 #define PF_NODUMMYNET 12
193 #define PF_NONAT64 14
196 static const struct tok pf_actions
[] = {
198 { PF_DROP
, "block" },
199 { PF_SCRUB
, "scrub" },
200 { PF_NOSCRUB
, "noscrub" },
202 { PF_NONAT
, "nonat" },
203 { PF_BINAT
, "binat" },
204 { PF_NOBINAT
, "nobinat" },
206 { PF_NORDR
, "nordr" },
207 { PF_SYNPROXY_DROP
, "synproxy-drop" },
208 #if defined(__FreeBSD__)
209 { PF_DEFER
, "defer" },
210 { PF_MATCH
, "match" },
211 #elif defined(__OpenBSD__)
212 { PF_DEFER
, "defer" },
213 { PF_MATCH
, "match" },
214 { PF_DIVERT
, "divert" },
217 #elif defined(__APPLE__)
218 { PF_DUMMYNET
, "dummynet" },
219 { PF_NODUMMYNET
, "nodummynet" },
220 { PF_NAT64
, "nat64" },
221 { PF_NONAT64
, "nonat64" },
232 #if defined(__OpenBSD__)
236 static const struct tok pf_directions
[] = {
237 { PF_INOUT
, "in/out" },
240 #if defined(__OpenBSD__)
247 pflog_print(netdissect_options
*ndo
, const struct pfloghdr
*hdr
)
249 uint32_t rulenr
, subrulenr
;
250 #if defined(__FreeBSD__)
251 uint32_t ridentifier
;
254 ndo
->ndo_protocol
= "pflog";
255 rulenr
= GET_BE_U_4(hdr
->rulenr
);
256 subrulenr
= GET_BE_U_4(hdr
->subrulenr
);
257 #if defined(__FreeBSD__)
258 ridentifier
= GET_BE_U_4(hdr
->ridentifier
);
260 if (subrulenr
== (uint32_t)-1)
261 ND_PRINT("rule %u/", rulenr
);
263 ND_PRINT("rule %u.", rulenr
);
264 nd_printjnp(ndo
, (const u_char
*)hdr
->ruleset
, PFLOG_RULESET_NAME_SIZE
);
265 ND_PRINT(".%u/", subrulenr
);
268 ND_PRINT("%s", tok2str(pf_reasons
, "unkn(%u)", GET_U_1(hdr
->reason
)));
271 * All bits set means that the UID shouldn't be printed.
272 * That's UINT_MAX if signed, or -1 if unsigned.
274 if (GET_BE_U_4(hdr
->uid
) != UINT_MAX
)
275 ND_PRINT(" [uid %u]", GET_BE_U_4(hdr
->uid
));
277 #if defined(__FreeBSD__)
278 if (ridentifier
!= 0)
279 ND_PRINT(" [ridentifier %u]", ridentifier
);
282 ND_PRINT(": %s %s on ",
283 tok2str(pf_actions
, "unkn(%u)", GET_U_1(hdr
->action
)),
284 tok2str(pf_directions
, "unkn(%u)", GET_U_1(hdr
->dir
)));
285 nd_printjnp(ndo
, (const u_char
*)hdr
->ifname
, PFLOG_IFNAMSIZ
);
290 pflog_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
293 u_int length
= h
->len
;
295 u_int caplen
= h
->caplen
;
296 const struct pfloghdr
*hdr
;
299 ndo
->ndo_protocol
= "pflog";
301 ND_ICHECK_U(length
, <, MIN_PFLOG_HDRLEN
);
303 hdr
= (const struct pfloghdr
*)p
;
304 hdrlen
= GET_U_1(hdr
->length
);
305 ND_ICHECK_U(hdrlen
, <, MIN_PFLOG_HDRLEN
);
306 hdrlen
= roundup2(hdrlen
, 4);
307 ND_ICHECK_U(hdrlen
, >, MAX_PFLOG_HDRLEN
);
309 /* print what we know */
310 ND_TCHECK_LEN(hdr
, hdrlen
);
311 ndo
->ndo_ll_hdr_len
+= hdrlen
;
313 pflog_print(ndo
, hdr
);
315 /* skip to the real packet */
316 af
= GET_U_1(hdr
->af
);
323 * If there's a system that doesn't use the AF_INET
324 * from 4.2BSD, feel free to add its value to af.h
327 * Hopefully, there isn't.
330 ip_print(ndo
, p
, length
);
334 * Try all AF_INET6 values for all systems with pflog,
337 case BSD_AF_INET6_BSD
:
338 case BSD_AF_INET6_FREEBSD
:
339 case BSD_AF_INET6_DARWIN
:
340 ip6_print(ndo
, p
, length
);
344 /* address family not handled, print raw packet */
346 pflog_print(ndo
, hdr
);
347 if (!ndo
->ndo_suppress_default_print
)
348 ND_DEFAULTPRINT(p
, caplen
);
354 nd_print_invalid(ndo
);