]> The Tcpdump Group git mirrors - tcpdump/blob - print-pflog.c
Include code to handle OpenBSD DLT_PFLOG files, based on the OpenBSD
[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.1 2002-02-05 10:07:39 guy Exp $ (LBL)";
27 #endif
28
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/socket.h>
32
33 #include <netinet/in.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, 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 get back at the link level addresses,
117 * and/or check that they're not walking off the end of the packet.
118 * Rather than pass them all the way down, we set these globals.
119 */
120 packetp = p;
121 snapend = p + caplen;
122
123 hdr = (const struct pfloghdr *)p;
124 if (eflag)
125 pflog_print(hdr);
126 af = ntohl(hdr->af);
127 length -= PFLOG_HDRLEN;
128 caplen -= PFLOG_HDRLEN;
129 p += PFLOG_HDRLEN;
130 switch (af) {
131
132 case OPENBSD_AF_INET:
133 ip_print(p, length);
134 break;
135
136 #ifdef INET6
137 case OPENBSD_AF_INET6:
138 ip6_print(p, length);
139 break;
140 #endif
141
142 default:
143 /* address family not handled, print raw packet */
144 if (!eflag)
145 pflog_print(hdr);
146 if (!xflag && !qflag)
147 default_print(p, caplen);
148 }
149
150 if (xflag)
151 default_print(p, caplen);
152 out:
153 putchar('\n');
154 --infodelay;
155 if (infoprint)
156 info(0);
157 }