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