]> The Tcpdump Group git mirrors - tcpdump/blob - print-pflog.c
Fix build on FreeBSD.
[tcpdump] / print-pflog.c
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 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #ifndef HAVE_NET_PFVAR_H
27 #error "No pf headers available"
28 #endif
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <net/if.h>
32 #include <net/pfvar.h>
33 #include <net/if_pflog.h>
34
35 #include <tcpdump-stdinc.h>
36
37 #include <stdio.h>
38 #include <pcap.h>
39
40 #include "extract.h"
41 #include "interface.h"
42 #include "addrtoname.h"
43
44 static const char tstr[] = "[|pflog]";
45
46 static const struct tok pf_reasons[] = {
47 { 0, "0(match)" },
48 { 1, "1(bad-offset)" },
49 { 2, "2(fragment)" },
50 { 3, "3(short)" },
51 { 4, "4(normalize)" },
52 { 5, "5(memory)" },
53 { 6, "6(bad-timestamp)" },
54 { 7, "7(congestion)" },
55 { 8, "8(ip-option)" },
56 { 9, "9(proto-cksum)" },
57 { 10, "10(state-mismatch)" },
58 { 11, "11(state-insert)" },
59 { 12, "12(state-limit)" },
60 { 13, "13(src-limit)" },
61 { 14, "14(synproxy)" },
62 { 0, NULL }
63 };
64
65 static const struct tok pf_actions[] = {
66 { PF_PASS, "pass" },
67 { PF_DROP, "block" },
68 { PF_SCRUB, "scrub" },
69 { PF_NAT, "nat" },
70 { PF_NONAT, "nat" },
71 { PF_BINAT, "binat" },
72 { PF_NOBINAT, "binat" },
73 { PF_RDR, "rdr" },
74 { PF_NORDR, "rdr" },
75 { PF_SYNPROXY_DROP, "synproxy-drop" },
76 { 0, NULL }
77 };
78
79 static const struct tok pf_directions[] = {
80 { PF_INOUT, "in/out" },
81 { PF_IN, "in" },
82 { PF_OUT, "out" },
83 { 0, NULL }
84 };
85
86 /* For reading capture files on other systems */
87 #define OPENBSD_AF_INET 2
88 #define OPENBSD_AF_INET6 24
89
90 static void
91 pflog_print(const struct pfloghdr *hdr)
92 {
93 u_int32_t rulenr, subrulenr;
94
95 rulenr = EXTRACT_32BITS(&hdr->rulenr);
96 subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
97 if (subrulenr == (u_int32_t)-1)
98 printf("rule %u/", rulenr);
99 else
100 printf("rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr);
101
102 printf("%s: %s %s on %s: ",
103 tok2str(pf_reasons, "unkn(%u)", hdr->reason),
104 tok2str(pf_actions, "unkn(%u)", hdr->action),
105 tok2str(pf_directions, "unkn(%u)", hdr->dir),
106 hdr->ifname);
107 }
108
109 u_int
110 pflog_if_print(const struct pcap_pkthdr *h, register const u_char *p)
111 {
112 u_int length = h->len;
113 u_int hdrlen;
114 u_int caplen = h->caplen;
115 const struct pfloghdr *hdr;
116 u_int8_t af;
117
118 /* check length */
119 if (caplen < sizeof(u_int8_t)) {
120 printf("%s", tstr);
121 return (caplen);
122 }
123
124 #define MIN_PFLOG_HDRLEN 45
125 hdr = (struct pfloghdr *)p;
126 if (hdr->length < MIN_PFLOG_HDRLEN) {
127 printf("[pflog: invalid header length!]");
128 return (hdr->length); /* XXX: not really */
129 }
130 hdrlen = BPF_WORDALIGN(hdr->length);
131
132 if (caplen < hdrlen) {
133 printf("%s", tstr);
134 return (hdrlen); /* XXX: true? */
135 }
136
137 /* print what we know */
138 hdr = (struct pfloghdr *)p;
139 TCHECK(*hdr);
140 if (eflag)
141 pflog_print(hdr);
142
143 /* skip to the real packet */
144 af = hdr->af;
145 length -= hdrlen;
146 caplen -= hdrlen;
147 p += hdrlen;
148 switch (af) {
149
150 case AF_INET:
151 #if OPENBSD_AF_INET != AF_INET
152 case OPENBSD_AF_INET: /* XXX: read pcap files */
153 #endif
154 ip_print(gndo, p, length);
155 break;
156
157 #ifdef INET6
158 case AF_INET6:
159 #if OPENBSD_AF_INET6 != AF_INET6
160 case OPENBSD_AF_INET6: /* XXX: read pcap files */
161 #endif
162 ip6_print(gndo, p, length);
163 break;
164 #endif
165
166 default:
167 /* address family not handled, print raw packet */
168 if (!eflag)
169 pflog_print(hdr);
170 if (!suppress_default_print)
171 default_print(p, caplen);
172 }
173
174 return (hdrlen);
175 trunc:
176 printf("%s", tstr);
177 return (hdrlen);
178 }
179
180 /*
181 * Local Variables:
182 * c-style: whitesmith
183 * c-basic-offset: 8
184 * End:
185 */