blob: 265efd3c866b9595f5ee8f01225db77ae2c3a793 [file] [log] [blame]
The Android Open Source Project2949f582009-03-03 19:30:46 -08001/*
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
Elliott Hughese2e3bd12017-05-15 10:59:29 -070022/* \summary: OpenBSD packet filter log file printer */
23
The Android Open Source Project2949f582009-03-03 19:30:46 -080024#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#ifndef HAVE_NET_PFVAR_H
29#error "No pf headers available"
30#endif
The Android Open Source Project2949f582009-03-03 19:30:46 -080031#include <sys/types.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080032#include <sys/socket.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080033#include <net/if.h>
34#include <net/pfvar.h>
35#include <net/if_pflog.h>
36
Elliott Hughese2e3bd12017-05-15 10:59:29 -070037#include <netdissect-stdinc.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080038
Elliott Hughese2e3bd12017-05-15 10:59:29 -070039#include "netdissect.h"
Elliott Hughes892a68b2015-10-19 14:43:53 -070040#include "extract.h"
41
42static const char tstr[] = "[|pflog]";
The Android Open Source Project2949f582009-03-03 19:30:46 -080043
JP Abgrall53f17a92014-02-12 14:02:41 -080044static const struct tok pf_reasons[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080045 { 0, "0(match)" },
46 { 1, "1(bad-offset)" },
47 { 2, "2(fragment)" },
48 { 3, "3(short)" },
49 { 4, "4(normalize)" },
50 { 5, "5(memory)" },
51 { 6, "6(bad-timestamp)" },
52 { 7, "7(congestion)" },
53 { 8, "8(ip-option)" },
54 { 9, "9(proto-cksum)" },
55 { 10, "10(state-mismatch)" },
56 { 11, "11(state-insert)" },
57 { 12, "12(state-limit)" },
58 { 13, "13(src-limit)" },
59 { 14, "14(synproxy)" },
60 { 0, NULL }
61};
62
JP Abgrall53f17a92014-02-12 14:02:41 -080063static const struct tok pf_actions[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080064 { PF_PASS, "pass" },
65 { PF_DROP, "block" },
66 { PF_SCRUB, "scrub" },
67 { PF_NAT, "nat" },
68 { PF_NONAT, "nat" },
69 { PF_BINAT, "binat" },
70 { PF_NOBINAT, "binat" },
71 { PF_RDR, "rdr" },
72 { PF_NORDR, "rdr" },
73 { PF_SYNPROXY_DROP, "synproxy-drop" },
74 { 0, NULL }
75};
76
JP Abgrall53f17a92014-02-12 14:02:41 -080077static const struct tok pf_directions[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080078 { PF_INOUT, "in/out" },
79 { PF_IN, "in" },
80 { PF_OUT, "out" },
81 { 0, NULL }
82};
83
84/* For reading capture files on other systems */
85#define OPENBSD_AF_INET 2
86#define OPENBSD_AF_INET6 24
87
88static void
Elliott Hughes892a68b2015-10-19 14:43:53 -070089pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
The Android Open Source Project2949f582009-03-03 19:30:46 -080090{
Elliott Hughes892a68b2015-10-19 14:43:53 -070091 uint32_t rulenr, subrulenr;
The Android Open Source Project2949f582009-03-03 19:30:46 -080092
JP Abgrall53f17a92014-02-12 14:02:41 -080093 rulenr = EXTRACT_32BITS(&hdr->rulenr);
94 subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
Elliott Hughes892a68b2015-10-19 14:43:53 -070095 if (subrulenr == (uint32_t)-1)
96 ND_PRINT((ndo, "rule %u/", rulenr));
The Android Open Source Project2949f582009-03-03 19:30:46 -080097 else
Elliott Hughes892a68b2015-10-19 14:43:53 -070098 ND_PRINT((ndo, "rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr));
The Android Open Source Project2949f582009-03-03 19:30:46 -080099
Elliott Hughes892a68b2015-10-19 14:43:53 -0700100 ND_PRINT((ndo, "%s: %s %s on %s: ",
The Android Open Source Project2949f582009-03-03 19:30:46 -0800101 tok2str(pf_reasons, "unkn(%u)", hdr->reason),
102 tok2str(pf_actions, "unkn(%u)", hdr->action),
103 tok2str(pf_directions, "unkn(%u)", hdr->dir),
Elliott Hughes892a68b2015-10-19 14:43:53 -0700104 hdr->ifname));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800105}
106
107u_int
Elliott Hughes892a68b2015-10-19 14:43:53 -0700108pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
109 register const u_char *p)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800110{
111 u_int length = h->len;
112 u_int hdrlen;
113 u_int caplen = h->caplen;
114 const struct pfloghdr *hdr;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700115 uint8_t af;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800116
117 /* check length */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700118 if (caplen < sizeof(uint8_t)) {
119 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800120 return (caplen);
121 }
122
123#define MIN_PFLOG_HDRLEN 45
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700124 hdr = (const struct pfloghdr *)p;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800125 if (hdr->length < MIN_PFLOG_HDRLEN) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700126 ND_PRINT((ndo, "[pflog: invalid header length!]"));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800127 return (hdr->length); /* XXX: not really */
128 }
129 hdrlen = BPF_WORDALIGN(hdr->length);
130
131 if (caplen < hdrlen) {
Elliott Hughes892a68b2015-10-19 14:43:53 -0700132 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800133 return (hdrlen); /* XXX: true? */
134 }
135
136 /* print what we know */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700137 ND_TCHECK(*hdr);
138 if (ndo->ndo_eflag)
139 pflog_print(ndo, hdr);
140
The Android Open Source Project2949f582009-03-03 19:30:46 -0800141 /* skip to the real packet */
142 af = hdr->af;
143 length -= hdrlen;
144 caplen -= hdrlen;
145 p += hdrlen;
146 switch (af) {
147
148 case AF_INET:
149#if OPENBSD_AF_INET != AF_INET
150 case OPENBSD_AF_INET: /* XXX: read pcap files */
151#endif
Elliott Hughes892a68b2015-10-19 14:43:53 -0700152 ip_print(ndo, p, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800153 break;
154
Elliott Hughes892a68b2015-10-19 14:43:53 -0700155#if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
156#ifdef AF_INET6
The Android Open Source Project2949f582009-03-03 19:30:46 -0800157 case AF_INET6:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700158#endif /* AF_INET6 */
159#if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
The Android Open Source Project2949f582009-03-03 19:30:46 -0800160 case OPENBSD_AF_INET6: /* XXX: read pcap files */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700161#endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
162 ip6_print(ndo, p, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800163 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700164#endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800165
166 default:
167 /* address family not handled, print raw packet */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700168 if (!ndo->ndo_eflag)
169 pflog_print(ndo, hdr);
170 if (!ndo->ndo_suppress_default_print)
171 ND_DEFAULTPRINT(p, caplen);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800172 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700173
The Android Open Source Project2949f582009-03-03 19:30:46 -0800174 return (hdrlen);
175trunc:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700176 ND_PRINT((ndo, "%s", tstr));
The Android Open Source Project2949f582009-03-03 19:30:46 -0800177 return (hdrlen);
178}
179
180/*
181 * Local Variables:
182 * c-style: whitesmith
183 * c-basic-offset: 8
184 * End:
185 */