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 */
26 #include "netdissect-stdinc.h"
28 #define ND_LONGJMP_FROM_TCHECK
29 #include "netdissect.h"
34 * pflog headers, at least as they exist now.
36 #define PFLOG_IFNAMSIZ 16
37 #define PFLOG_RULESET_NAME_SIZE 16
43 } pfa
; /* 128-bit address */
53 char ifname
[PFLOG_IFNAMSIZ
];
54 char ruleset
[PFLOG_RULESET_NAME_SIZE
];
56 nd_uint32_t subrulenr
;
62 /* Minimum header length (without padding): 61 */
63 #define MIN_PFLOG_HDRLEN 61
64 #if defined(__OpenBSD__)
71 #if defined(__FreeBSD__)
72 nd_uint32_t ridentifier
;
75 #elif defined(__OpenBSD__)
82 #define MAX_PFLOG_HDRLEN 100 /* 61 + 3 + 16 + 16 + 2 + 2 */
88 #define PFRES_BADOFF 1
92 #define PFRES_MEMORY 5
94 #define PFRES_CONGEST 7
95 #define PFRES_IPOPTIONS 8
96 #define PFRES_PROTCKSUM 9
97 #define PFRES_BADSTATE 10
98 #define PFRES_STATEINS 11
99 #define PFRES_MAXSTATES 12
100 #define PFRES_SRCLIMIT 13
101 #define PFRES_SYNPROXY 14
102 #if defined(__FreeBSD__)
103 #define PFRES_MAPFAILED 15
104 #elif defined(__NetBSD__)
105 #define PFRES_STATELOCKED 15
106 #elif defined(__OpenBSD__)
107 #define PFRES_TRANSLATE 15
108 #define PFRES_NOROUTE 16
109 #elif defined(__APPLE__)
110 #define PFRES_DUMMYNET 15
113 static const struct tok pf_reasons
[] = {
114 { PFRES_MATCH
, "0(match)" },
115 { PFRES_BADOFF
, "1(bad-offset)" },
116 { PFRES_FRAG
, "2(fragment)" },
117 { PFRES_SHORT
, "3(short)" },
118 { PFRES_NORM
, "4(normalize)" },
119 { PFRES_MEMORY
, "5(memory)" },
120 { PFRES_TS
, "6(bad-timestamp)" },
121 { PFRES_CONGEST
, "7(congestion)" },
122 { PFRES_IPOPTIONS
, "8(ip-option)" },
123 { PFRES_PROTCKSUM
, "9(proto-cksum)" },
124 { PFRES_BADSTATE
, "10(state-mismatch)" },
125 { PFRES_STATEINS
, "11(state-insert)" },
126 { PFRES_MAXSTATES
, "12(state-limit)" },
127 { PFRES_SRCLIMIT
, "13(src-limit)" },
128 { PFRES_SYNPROXY
, "14(synproxy)" },
129 #if defined(__FreeBSD__)
130 { PFRES_MAPFAILED
, "15(map-failed)" },
131 #elif defined(__NetBSD__)
132 { PFRES_STATELOCKED
, "15(state-locked)" },
133 #elif defined(__OpenBSD__)
134 { PFRES_TRANSLATE
, "15(translate)" },
135 { PFRES_NOROUTE
, "16(no-route)" },
136 #elif defined(__APPLE__)
137 { PFRES_DUMMYNET
, "15(dummynet)" },
155 #define PF_SYNPROXY_DROP 10
156 #if defined(__FreeBSD__)
159 #elif defined(__OpenBSD__)
165 #elif defined(__APPLE__)
166 #define PF_DUMMYNET 11
167 #define PF_NODUMMYNET 12
169 #define PF_NONAT64 14
172 static const struct tok pf_actions
[] = {
174 { PF_DROP
, "block" },
175 { PF_SCRUB
, "scrub" },
176 { PF_NOSCRUB
, "noscrub" },
178 { PF_NONAT
, "nonat" },
179 { PF_BINAT
, "binat" },
180 { PF_NOBINAT
, "nobinat" },
182 { PF_NORDR
, "nordr" },
183 { PF_SYNPROXY_DROP
, "synproxy-drop" },
184 #if defined(__FreeBSD__)
185 { PF_DEFER
, "defer" },
186 { PF_MATCH
, "match" },
187 #elif defined(__OpenBSD__)
188 { PF_DEFER
, "defer" },
189 { PF_MATCH
, "match" },
190 { PF_DIVERT
, "divert" },
193 #elif defined(__APPLE__)
194 { PF_DUMMYNET
, "dummynet" },
195 { PF_NODUMMYNET
, "nodummynet" },
196 { PF_NAT64
, "nat64" },
197 { PF_NONAT64
, "nonat64" },
208 #if defined(__OpenBSD__)
212 static const struct tok pf_directions
[] = {
213 { PF_INOUT
, "in/out" },
216 #if defined(__OpenBSD__)
223 pflog_print(netdissect_options
*ndo
, const struct pfloghdr
*hdr
)
225 uint32_t rulenr
, subrulenr
;
227 ndo
->ndo_protocol
= "pflog";
228 rulenr
= GET_BE_U_4(hdr
->rulenr
);
229 subrulenr
= GET_BE_U_4(hdr
->subrulenr
);
230 if (subrulenr
== (uint32_t)-1)
231 ND_PRINT("rule %u/", rulenr
);
233 ND_PRINT("rule %u.", rulenr
);
234 nd_printjnp(ndo
, (const u_char
*)hdr
->ruleset
, PFLOG_RULESET_NAME_SIZE
);
235 ND_PRINT(".%u/", subrulenr
);
238 ND_PRINT("%s: %s %s on ",
239 tok2str(pf_reasons
, "unkn(%u)", GET_U_1(hdr
->reason
)),
240 tok2str(pf_actions
, "unkn(%u)", GET_U_1(hdr
->action
)),
241 tok2str(pf_directions
, "unkn(%u)", GET_U_1(hdr
->dir
)));
242 nd_printjnp(ndo
, (const u_char
*)hdr
->ifname
, PFLOG_IFNAMSIZ
);
247 pflog_if_print(netdissect_options
*ndo
, const struct pcap_pkthdr
*h
,
250 u_int length
= h
->len
;
252 u_int caplen
= h
->caplen
;
253 const struct pfloghdr
*hdr
;
256 ndo
->ndo_protocol
= "pflog";
258 ND_ICHECK_U(length
, <, MIN_PFLOG_HDRLEN
);
260 hdr
= (const struct pfloghdr
*)p
;
261 hdrlen
= GET_U_1(hdr
->length
);
262 ND_ICHECK_U(hdrlen
, <, MIN_PFLOG_HDRLEN
);
263 hdrlen
= roundup2(hdrlen
, 4);
264 ND_ICHECK_U(hdrlen
, >, MAX_PFLOG_HDRLEN
);
266 /* print what we know */
267 ND_TCHECK_LEN(hdr
, hdrlen
);
268 ndo
->ndo_ll_hdr_len
+= hdrlen
;
270 pflog_print(ndo
, hdr
);
272 /* skip to the real packet */
273 af
= GET_U_1(hdr
->af
);
280 * If there's a system that doesn't use the AF_INET
281 * from 4.2BSD, feel free to add its value to af.h
284 * Hopefully, there isn't.
287 ip_print(ndo
, p
, length
);
291 * Try all AF_INET6 values for all systems with pflog,
294 case BSD_AF_INET6_BSD
:
295 case BSD_AF_INET6_FREEBSD
:
296 case BSD_AF_INET6_DARWIN
:
297 ip6_print(ndo
, p
, length
);
301 /* address family not handled, print raw packet */
303 pflog_print(ndo
, hdr
);
304 if (!ndo
->ndo_suppress_default_print
)
305 ND_DEFAULTPRINT(p
, caplen
);
311 nd_print_invalid(ndo
);