blob: eb1b1766db004b1eee92cac81a4655eea924fd42 [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
Elliott Hughes820eced2021-08-20 18:00:50 -070025#include <config.h>
The Android Open Source Project2949f582009-03-03 19:30:46 -080026#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 Hughes820eced2021-08-20 18:00:50 -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
The Android Open Source Project2949f582009-03-03 19:30:46 -080042
JP Abgrall53f17a92014-02-12 14:02:41 -080043static const struct tok pf_reasons[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080044 { 0, "0(match)" },
45 { 1, "1(bad-offset)" },
46 { 2, "2(fragment)" },
47 { 3, "3(short)" },
48 { 4, "4(normalize)" },
49 { 5, "5(memory)" },
50 { 6, "6(bad-timestamp)" },
51 { 7, "7(congestion)" },
52 { 8, "8(ip-option)" },
53 { 9, "9(proto-cksum)" },
54 { 10, "10(state-mismatch)" },
55 { 11, "11(state-insert)" },
56 { 12, "12(state-limit)" },
57 { 13, "13(src-limit)" },
58 { 14, "14(synproxy)" },
59 { 0, NULL }
60};
61
JP Abgrall53f17a92014-02-12 14:02:41 -080062static const struct tok pf_actions[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080063 { PF_PASS, "pass" },
64 { PF_DROP, "block" },
65 { PF_SCRUB, "scrub" },
66 { PF_NAT, "nat" },
67 { PF_NONAT, "nat" },
68 { PF_BINAT, "binat" },
69 { PF_NOBINAT, "binat" },
70 { PF_RDR, "rdr" },
71 { PF_NORDR, "rdr" },
72 { PF_SYNPROXY_DROP, "synproxy-drop" },
73 { 0, NULL }
74};
75
JP Abgrall53f17a92014-02-12 14:02:41 -080076static const struct tok pf_directions[] = {
The Android Open Source Project2949f582009-03-03 19:30:46 -080077 { PF_INOUT, "in/out" },
78 { PF_IN, "in" },
79 { PF_OUT, "out" },
80 { 0, NULL }
81};
82
83/* For reading capture files on other systems */
84#define OPENBSD_AF_INET 2
85#define OPENBSD_AF_INET6 24
86
87static void
Elliott Hughes892a68b2015-10-19 14:43:53 -070088pflog_print(netdissect_options *ndo, const struct pfloghdr *hdr)
The Android Open Source Project2949f582009-03-03 19:30:46 -080089{
Elliott Hughes892a68b2015-10-19 14:43:53 -070090 uint32_t rulenr, subrulenr;
The Android Open Source Project2949f582009-03-03 19:30:46 -080091
Elliott Hughes820eced2021-08-20 18:00:50 -070092 ndo->ndo_protocol = "pflog";
93 rulenr = GET_BE_U_4(&hdr->rulenr);
94 subrulenr = GET_BE_U_4(&hdr->subrulenr);
Elliott Hughes892a68b2015-10-19 14:43:53 -070095 if (subrulenr == (uint32_t)-1)
Elliott Hughes820eced2021-08-20 18:00:50 -070096 ND_PRINT("rule %u/", rulenr);
97 else {
98 ND_PRINT("rule %u.", rulenr);
99 nd_printjnp(ndo, (const u_char*)hdr->ruleset, PFLOG_RULESET_NAME_SIZE);
100 ND_PRINT(".%u/", subrulenr);
101 }
The Android Open Source Project2949f582009-03-03 19:30:46 -0800102
Elliott Hughes820eced2021-08-20 18:00:50 -0700103 ND_PRINT("%s: %s %s on ",
104 tok2str(pf_reasons, "unkn(%u)", GET_U_1(&hdr->reason)),
105 tok2str(pf_actions, "unkn(%u)", GET_U_1(&hdr->action)),
106 tok2str(pf_directions, "unkn(%u)", GET_U_1(&hdr->dir)));
107 nd_printjnp(ndo, (const u_char*)hdr->ifname, IFNAMSIZ);
108 ND_PRINT(": ");
The Android Open Source Project2949f582009-03-03 19:30:46 -0800109}
110
Elliott Hughes820eced2021-08-20 18:00:50 -0700111void
Elliott Hughes892a68b2015-10-19 14:43:53 -0700112pflog_if_print(netdissect_options *ndo, const struct pcap_pkthdr *h,
Elliott Hughes820eced2021-08-20 18:00:50 -0700113 const u_char *p)
The Android Open Source Project2949f582009-03-03 19:30:46 -0800114{
115 u_int length = h->len;
116 u_int hdrlen;
117 u_int caplen = h->caplen;
118 const struct pfloghdr *hdr;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700119 uint8_t af;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800120
Elliott Hughes820eced2021-08-20 18:00:50 -0700121 ndo->ndo_protocol = "pflog";
The Android Open Source Project2949f582009-03-03 19:30:46 -0800122 /* check length */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700123 if (caplen < sizeof(uint8_t)) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700124 nd_print_trunc(ndo);
125 ndo->ndo_ll_hdr_len += h->caplen;
126 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800127 }
128
129#define MIN_PFLOG_HDRLEN 45
Elliott Hughese2e3bd12017-05-15 10:59:29 -0700130 hdr = (const struct pfloghdr *)p;
Elliott Hughes820eced2021-08-20 18:00:50 -0700131 if (GET_U_1(&hdr->length) < MIN_PFLOG_HDRLEN) {
132 ND_PRINT("[pflog: invalid header length!]");
133 ndo->ndo_ll_hdr_len += GET_U_1(&hdr->length); /* XXX: not really */
134 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800135 }
136 hdrlen = BPF_WORDALIGN(hdr->length);
137
138 if (caplen < hdrlen) {
Elliott Hughes820eced2021-08-20 18:00:50 -0700139 nd_print_trunc(ndo);
140 ndo->ndo_ll_hdr_len += hdrlen; /* XXX: true? */
141 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800142 }
143
144 /* print what we know */
Elliott Hughes820eced2021-08-20 18:00:50 -0700145 ND_TCHECK_SIZE(hdr);
Elliott Hughes892a68b2015-10-19 14:43:53 -0700146 if (ndo->ndo_eflag)
147 pflog_print(ndo, hdr);
148
The Android Open Source Project2949f582009-03-03 19:30:46 -0800149 /* skip to the real packet */
Elliott Hughes820eced2021-08-20 18:00:50 -0700150 af = GET_U_1(&hdr->af);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800151 length -= hdrlen;
152 caplen -= hdrlen;
153 p += hdrlen;
154 switch (af) {
155
156 case AF_INET:
157#if OPENBSD_AF_INET != AF_INET
158 case OPENBSD_AF_INET: /* XXX: read pcap files */
159#endif
Elliott Hughes892a68b2015-10-19 14:43:53 -0700160 ip_print(ndo, p, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800161 break;
162
Elliott Hughes892a68b2015-10-19 14:43:53 -0700163#if defined(AF_INET6) || defined(OPENBSD_AF_INET6)
164#ifdef AF_INET6
The Android Open Source Project2949f582009-03-03 19:30:46 -0800165 case AF_INET6:
Elliott Hughes892a68b2015-10-19 14:43:53 -0700166#endif /* AF_INET6 */
167#if !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6
The Android Open Source Project2949f582009-03-03 19:30:46 -0800168 case OPENBSD_AF_INET6: /* XXX: read pcap files */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700169#endif /* !defined(AF_INET6) || OPENBSD_AF_INET6 != AF_INET6 */
170 ip6_print(ndo, p, length);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800171 break;
Elliott Hughes892a68b2015-10-19 14:43:53 -0700172#endif /* defined(AF_INET6) || defined(OPENBSD_AF_INET6) */
The Android Open Source Project2949f582009-03-03 19:30:46 -0800173
174 default:
175 /* address family not handled, print raw packet */
Elliott Hughes892a68b2015-10-19 14:43:53 -0700176 if (!ndo->ndo_eflag)
177 pflog_print(ndo, hdr);
178 if (!ndo->ndo_suppress_default_print)
179 ND_DEFAULTPRINT(p, caplen);
The Android Open Source Project2949f582009-03-03 19:30:46 -0800180 }
Elliott Hughes892a68b2015-10-19 14:43:53 -0700181
Elliott Hughes820eced2021-08-20 18:00:50 -0700182 ndo->ndo_ll_hdr_len += hdrlen;
183 return;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800184trunc:
Elliott Hughes820eced2021-08-20 18:00:50 -0700185 nd_print_trunc(ndo);
186 ndo->ndo_ll_hdr_len += hdrlen;
The Android Open Source Project2949f582009-03-03 19:30:46 -0800187}