]> The Tcpdump Group git mirrors - tcpdump/blob - print-pflog.c
We no longer use "packetp" for anything, so eliminate it. (If any
[tcpdump] / print-pflog.c
1 /* $OpenBSD: print-pflog.c,v 1.9 2001/09/18 14:52:53 jakob Exp $ */
2
3 /*
4 * Copyright (c) 1990, 1991, 1993, 1994, 1995, 1996
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24 #ifndef lint
25 static const char rcsid[] =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-pflog.c,v 1.5 2002-12-18 08:53:22 guy Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <pcap.h>
37
38 #include "interface.h"
39 #include "addrtoname.h"
40
41 /* The header in OpenBSD pflog files. */
42
43 struct pfloghdr {
44 u_int32_t af;
45 char ifname[16];
46 int16_t rnr;
47 u_int16_t reason;
48 u_int16_t action;
49 u_int16_t dir;
50 };
51 #define PFLOG_HDRLEN sizeof(struct pfloghdr)
52
53 /* Actions */
54 #define PF_PASS 0
55 #define PF_DROP 1
56 #define PF_SCRUB 2
57
58 /* Directions */
59 #define PF_IN 0
60 #define PF_OUT 1
61
62 static struct tok pf_reasons[] = {
63 { 0, "match" },
64 { 1, "bad-offset" },
65 { 2, "fragment" },
66 { 3, "short" },
67 { 4, "normalize" },
68 { 5, "memory" },
69 { 0, NULL }
70 };
71
72 static struct tok pf_actions[] = {
73 { PF_PASS, "pass" },
74 { PF_DROP, "drop" },
75 { PF_SCRUB, "scrub" },
76 { 0, NULL }
77 };
78
79 static struct tok pf_directions[] = {
80 { PF_IN, "in" },
81 { PF_OUT, "out" },
82 { 0, NULL }
83 };
84
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 printf("rule %d/%s: %s %s on %s: ",
92 (short)ntohs(hdr->rnr),
93 tok2str(pf_reasons, "unkn(%u)", ntohs(hdr->reason)),
94 tok2str(pf_actions, "unkn(%u)", ntohs(hdr->action)),
95 tok2str(pf_directions, "unkn(%u)", ntohs(hdr->dir)),
96 hdr->ifname);
97 }
98
99 void
100 pflog_if_print(u_char *user _U_, const struct pcap_pkthdr *h,
101 register const u_char *p)
102 {
103 u_int length = h->len;
104 u_int caplen = h->caplen;
105 const struct pfloghdr *hdr;
106 u_int8_t af;
107
108 ts_print(&h->ts);
109
110 if (caplen < PFLOG_HDRLEN) {
111 printf("[|pflog]");
112 goto out;
113 }
114
115 /*
116 * Some printers want to check that they're not walking off the
117 * end of the packet.
118 * Rather than pass it all the way down, we set this global.
119 */
120 snapend = p + caplen;
121
122 hdr = (const struct pfloghdr *)p;
123 if (eflag)
124 pflog_print(hdr);
125 af = ntohl(hdr->af);
126 length -= PFLOG_HDRLEN;
127 caplen -= PFLOG_HDRLEN;
128 p += PFLOG_HDRLEN;
129 switch (af) {
130
131 case OPENBSD_AF_INET:
132 ip_print(p, length);
133 break;
134
135 #ifdef INET6
136 case OPENBSD_AF_INET6:
137 ip6_print(p, length);
138 break;
139 #endif
140
141 default:
142 /* address family not handled, print raw packet */
143 if (!eflag)
144 pflog_print(hdr);
145 if (!xflag && !qflag)
146 default_print(p, caplen);
147 }
148
149 if (xflag)
150 default_print(p, caplen);
151 out:
152 putchar('\n');
153 --infodelay;
154 if (infoprint)
155 info(0);
156 }