]> The Tcpdump Group git mirrors - tcpdump/blob - print-pflog.c
Merge pull request #375 from fxlb/print-llc
[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
39 #include "extract.h"
40 #include "interface.h"
41
42 static const char tstr[] = "[|pflog]";
43
44 static const struct tok pf_reasons[] = {
45 { 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
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, "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
77 static const struct tok pf_directions[] = {
78 { 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
88 static void
89 pflog_print(const struct pfloghdr *hdr)
90 {
91 u_int32_t rulenr, subrulenr;
92
93 rulenr = EXTRACT_32BITS(&hdr->rulenr);
94 subrulenr = EXTRACT_32BITS(&hdr->subrulenr);
95 if (subrulenr == (u_int32_t)-1)
96 printf("rule %u/", rulenr);
97 else
98 printf("rule %u.%s.%u/", rulenr, hdr->ruleset, subrulenr);
99
100 printf("%s: %s %s on %s: ",
101 tok2str(pf_reasons, "unkn(%u)", hdr->reason),
102 tok2str(pf_actions, "unkn(%u)", hdr->action),
103 tok2str(pf_directions, "unkn(%u)", hdr->dir),
104 hdr->ifname);
105 }
106
107 u_int
108 pflog_if_print(const struct pcap_pkthdr *h, register const u_char *p)
109 {
110 u_int length = h->len;
111 u_int hdrlen;
112 u_int caplen = h->caplen;
113 const struct pfloghdr *hdr;
114 u_int8_t af;
115
116 /* check length */
117 if (caplen < sizeof(u_int8_t)) {
118 printf("%s", tstr);
119 return (caplen);
120 }
121
122 #define MIN_PFLOG_HDRLEN 45
123 hdr = (struct pfloghdr *)p;
124 if (hdr->length < MIN_PFLOG_HDRLEN) {
125 printf("[pflog: invalid header length!]");
126 return (hdr->length); /* XXX: not really */
127 }
128 hdrlen = BPF_WORDALIGN(hdr->length);
129
130 if (caplen < hdrlen) {
131 printf("%s", tstr);
132 return (hdrlen); /* XXX: true? */
133 }
134
135 /* print what we know */
136 hdr = (struct pfloghdr *)p;
137 TCHECK(*hdr);
138 if (eflag)
139 pflog_print(hdr);
140
141 /* 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
152 ip_print(gndo, p, length);
153 break;
154
155 #ifdef INET6
156 case AF_INET6:
157 #if OPENBSD_AF_INET6 != AF_INET6
158 case OPENBSD_AF_INET6: /* XXX: read pcap files */
159 #endif
160 ip6_print(gndo, p, length);
161 break;
162 #endif
163
164 default:
165 /* address family not handled, print raw packet */
166 if (!eflag)
167 pflog_print(hdr);
168 if (!suppress_default_print)
169 default_print(p, caplen);
170 }
171
172 return (hdrlen);
173 trunc:
174 printf("%s", tstr);
175 return (hdrlen);
176 }
177
178 /*
179 * Local Variables:
180 * c-style: whitesmith
181 * c-basic-offset: 8
182 * End:
183 */