print-pflog.c revision 1.10 1 1.1 christos /*
2 1.1 christos * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
3 1.1 christos * The Regents of the University of California. All rights reserved.
4 1.1 christos *
5 1.1 christos * Redistribution and use in source and binary forms, with or without
6 1.1 christos * modification, are permitted provided that: (1) source code distributions
7 1.1 christos * retain the above copyright notice and this paragraph in its entirety, (2)
8 1.1 christos * distributions including binary code include the above copyright notice and
9 1.1 christos * this paragraph in its entirety in the documentation or other materials
10 1.1 christos * provided with the distribution, and (3) all advertising materials mentioning
11 1.1 christos * features or use of this software display the following acknowledgement:
12 1.1 christos * ``This product includes software developed by the University of California,
13 1.1 christos * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 1.1 christos * the University nor the names of its contributors may be used to endorse
15 1.1 christos * or promote products derived from this software without specific prior
16 1.1 christos * written permission.
17 1.1 christos * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 1.1 christos * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 1.1 christos * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 1.1 christos */
21 1.1 christos
22 1.2 christos #include <sys/cdefs.h>
23 1.1 christos #ifndef lint
24 1.10 christos __RCSID("$NetBSD: print-pflog.c,v 1.10 2024/09/02 16:15:32 christos Exp $");
25 1.1 christos #endif
26 1.1 christos
27 1.9 christos /* \summary: *BSD/Darwin packet filter log file printer */
28 1.8 spz
29 1.9 christos #include <config.h>
30 1.1 christos
31 1.9 christos #include "netdissect-stdinc.h"
32 1.1 christos
33 1.7 christos #include "netdissect.h"
34 1.5 christos #include "extract.h"
35 1.9 christos #include "af.h"
36 1.1 christos
37 1.9 christos #include "pflog.h"
38 1.1 christos
39 1.4 christos static const struct tok pf_reasons[] = {
40 1.9 christos { PFRES_MATCH, "0(match)" },
41 1.9 christos { PFRES_BADOFF, "1(bad-offset)" },
42 1.9 christos { PFRES_FRAG, "2(fragment)" },
43 1.10 christos { PFRES_SHORT, "3(short)" },
44 1.9 christos { PFRES_NORM, "4(normalize)" },
45 1.9 christos { PFRES_MEMORY, "5(memory)" },
46 1.9 christos { PFRES_TS, "6(bad-timestamp)" },
47 1.9 christos { PFRES_CONGEST, "7(congestion)" },
48 1.9 christos { PFRES_IPOPTIONS, "8(ip-option)" },
49 1.9 christos { PFRES_PROTCKSUM, "9(proto-cksum)" },
50 1.9 christos { PFRES_BADSTATE, "10(state-mismatch)" },
51 1.9 christos { PFRES_STATEINS, "11(state-insert)" },
52 1.9 christos { PFRES_MAXSTATES, "12(state-limit)" },
53 1.9 christos { PFRES_SRCLIMIT, "13(src-limit)" },
54 1.9 christos { PFRES_SYNPROXY, "14(synproxy)" },
55 1.9 christos #if defined(__FreeBSD__)
56 1.9 christos { PFRES_MAPFAILED, "15(map-failed)" },
57 1.9 christos #elif defined(__NetBSD__)
58 1.9 christos { PFRES_STATELOCKED, "15(state-locked)" },
59 1.9 christos #elif defined(__OpenBSD__)
60 1.9 christos { PFRES_TRANSLATE, "15(translate)" },
61 1.9 christos { PFRES_NOROUTE, "16(no-route)" },
62 1.9 christos #elif defined(__APPLE__)
63 1.9 christos { PFRES_DUMMYNET, "15(dummynet)" },
64 1.9 christos #endif
65 1.1 christos { 0, NULL }
66 1.1 christos };
67 1.1 christos
68 1.4 christos static const struct tok pf_actions[] = {
69 1.1 christos { PF_PASS, "pass" },
70 1.1 christos { PF_DROP, "block" },
71 1.1 christos { PF_SCRUB, "scrub" },
72 1.1 christos { PF_NAT, "nat" },
73 1.9 christos { PF_NONAT, "nonat" },
74 1.1 christos { PF_BINAT, "binat" },
75 1.9 christos { PF_NOBINAT, "nobinat" },
76 1.1 christos { PF_RDR, "rdr" },
77 1.9 christos { PF_NORDR, "nordr" },
78 1.1 christos { PF_SYNPROXY_DROP, "synproxy-drop" },
79 1.9 christos #if defined(__FreeBSD__)
80 1.9 christos { PF_DEFER, "defer" },
81 1.9 christos #elif defined(__OpenBSD__)
82 1.9 christos { PF_DEFER, "defer" },
83 1.9 christos { PF_MATCH, "match" },
84 1.9 christos { PF_DIVERT, "divert" },
85 1.9 christos { PF_RT, "rt" },
86 1.9 christos { PF_AFRT, "afrt" },
87 1.9 christos #elif defined(__APPLE__)
88 1.9 christos { PF_DUMMYNET, "dummynet" },
89 1.9 christos { PF_NODUMMYNET, "nodummynet" },
90 1.9 christos { PF_NAT64, "nat64" },
91 1.9 christos { PF_NONAT64, "nonat64" },
92 1.9 christos #endif
93 1.1 christos { 0, NULL }
94 1.1 christos };
95 1.1 christos
96 1.4 christos static const struct tok pf_directions[] = {
97 1.1 christos { PF_INOUT, "in/out" },
98 1.1 christos { PF_IN, "in" },
99 1.1 christos { PF_OUT, "out" },
100 1.9 christos #if defined(__OpenBSD__)
101 1.9 christos { PF_FWD, "fwd" },
102 1.9 christos #endif
103 1.1 christos { 0, NULL }
104 1.1 christos };
105 1.1 christos
106 1.1 christos static void
107 1.5 christos pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
108 1.1 christos {
109 1.5 christos uint32_t rulenr, subrulenr;
110 1.1 christos
111 1.9 christos ndo->ndo_protocol = "pflog";
112 1.10 christos rulenr = GET_BE_U_4(hdr->rulenr);
113 1.10 christos subrulenr = GET_BE_U_4(hdr->subrulenr);
114 1.5 christos if (subrulenr == (uint32_t)-1)
115 1.9 christos ND_PRINT("rule %u/", rulenr);
116 1.9 christos else {
117 1.9 christos ND_PRINT("rule %u.", rulenr);
118 1.9 christos nd_printjnp(ndo, (const u_char*)hdr->ruleset, PFLOG_RULESET_NAME_SIZE);
119 1.9 christos ND_PRINT(".%u/", subrulenr);
120 1.9 christos }
121 1.9 christos
122 1.9 christos ND_PRINT("%s: %s %s on ",
123 1.10 christos tok2str(pf_reasons, "unkn(%u)", GET_U_1(hdr->reason)),
124 1.10 christos tok2str(pf_actions, "unkn(%u)", GET_U_1(hdr->action)),
125 1.10 christos tok2str(pf_directions, "unkn(%u)", GET_U_1(hdr->dir)));
126 1.9 christos nd_printjnp(ndo, (const u_char*)hdr->ifname, PFLOG_IFNAMSIZ);
127 1.9 christos ND_PRINT(": ");
128 1.1 christos }
129 1.1 christos
130 1.9 christos void
131 1.5 christos pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
132 1.9 christos const u_char *p)
133 1.1 christos {
134 1.1 christos u_int length = h->len;
135 1.1 christos u_int hdrlen;
136 1.1 christos u_int caplen = h->caplen;
137 1.1 christos const struct pfloghdr *hdr;
138 1.5 christos uint8_t af;
139 1.1 christos
140 1.9 christos ndo->ndo_protocol = "pflog";
141 1.1 christos /* check length */
142 1.5 christos if (caplen < sizeof(uint8_t)) {
143 1.9 christos nd_print_trunc(ndo);
144 1.9 christos ndo->ndo_ll_hdr_len += h->caplen;
145 1.9 christos return;
146 1.1 christos }
147 1.1 christos
148 1.8 spz hdr = (const struct pfloghdr *)p;
149 1.10 christos hdrlen = GET_U_1(hdr->length);
150 1.10 christos if (hdrlen < MIN_PFLOG_HDRLEN) {
151 1.9 christos ND_PRINT("[pflog: invalid header length!]");
152 1.10 christos ndo->ndo_ll_hdr_len += hdrlen; /* XXX: not really */
153 1.9 christos return;
154 1.1 christos }
155 1.10 christos hdrlen = roundup2(hdrlen, 4);
156 1.1 christos
157 1.1 christos if (caplen < hdrlen) {
158 1.9 christos nd_print_trunc(ndo);
159 1.9 christos ndo->ndo_ll_hdr_len += hdrlen; /* XXX: true? */
160 1.9 christos return;
161 1.1 christos }
162 1.1 christos
163 1.1 christos /* print what we know */
164 1.9 christos ND_TCHECK_SIZE(hdr);
165 1.5 christos if (ndo->ndo_eflag)
166 1.5 christos pflog_print(ndo, hdr);
167 1.5 christos
168 1.1 christos /* skip to the real packet */
169 1.10 christos af = GET_U_1(hdr->af);
170 1.1 christos length -= hdrlen;
171 1.1 christos caplen -= hdrlen;
172 1.1 christos p += hdrlen;
173 1.1 christos switch (af) {
174 1.1 christos
175 1.9 christos /*
176 1.9 christos * If there's a system that doesn't use the AF_INET
177 1.9 christos * from 4.2BSD, feel free to add its value to af.h
178 1.9 christos * and use it here.
179 1.9 christos *
180 1.9 christos * Hopefully, there isn't.
181 1.9 christos */
182 1.9 christos case BSD_AFNUM_INET:
183 1.5 christos ip_print(ndo, p, length);
184 1.1 christos break;
185 1.1 christos
186 1.9 christos /*
187 1.9 christos * Try all AF_INET6 values for all systems with pflog,
188 1.9 christos * including Darwin.
189 1.9 christos */
190 1.9 christos case BSD_AFNUM_INET6_BSD:
191 1.9 christos case BSD_AFNUM_INET6_FREEBSD:
192 1.9 christos case BSD_AFNUM_INET6_DARWIN:
193 1.5 christos ip6_print(ndo, p, length);
194 1.1 christos break;
195 1.1 christos
196 1.1 christos default:
197 1.1 christos /* address family not handled, print raw packet */
198 1.5 christos if (!ndo->ndo_eflag)
199 1.5 christos pflog_print(ndo, hdr);
200 1.5 christos if (!ndo->ndo_suppress_default_print)
201 1.5 christos ND_DEFAULTPRINT(p, caplen);
202 1.1 christos }
203 1.5 christos
204 1.9 christos ndo->ndo_ll_hdr_len += hdrlen;
205 1.9 christos return;
206 1.1 christos trunc:
207 1.9 christos nd_print_trunc(ndo);
208 1.9 christos ndo->ndo_ll_hdr_len += hdrlen;
209 1.1 christos }
210