xref: /openbsd-src/usr.sbin/tcpdump/print-pflog.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: print-pflog.c,v 1.25 2014/08/14 12:44:44 mpi 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 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/time.h>
27 #include <sys/socket.h>
28 #include <sys/file.h>
29 #include <sys/ioctl.h>
30 #include <sys/mbuf.h>
31 #include <sys/proc.h>
32 
33 #ifndef NO_PID
34 #define NO_PID	(32766+1)
35 #endif
36 
37 struct rtentry;
38 #include <net/if.h>
39 #include <net/if_pflog.h>
40 
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
43 
44 #include <net/pfvar.h>
45 
46 #include <arpa/inet.h>
47 
48 #include <ctype.h>
49 #include <netdb.h>
50 #include <pcap.h>
51 #include <signal.h>
52 #include <stdio.h>
53 
54 #include "interface.h"
55 #include "addrtoname.h"
56 
57 char *pf_reasons[PFRES_MAX+2] = PFRES_NAMES;
58 
59 void
60 pflog_if_print(u_char *user, const struct pcap_pkthdr *h,
61      register const u_char *p)
62 {
63 	u_int length = h->len;
64 	u_int hdrlen;
65 	u_int caplen = h->caplen;
66 	const struct ip *ip;
67 #ifdef INET6
68 	const struct ip6_hdr *ip6;
69 #endif
70 	const struct pfloghdr *hdr;
71 	u_int8_t af;
72 
73 	ts_print(&h->ts);
74 
75 	/* check length */
76 	if (caplen < sizeof(u_int8_t)) {
77 		printf("[|pflog]");
78 		goto out;
79 	}
80 
81 #define MIN_PFLOG_HDRLEN	45
82 	hdr = (struct pfloghdr *)p;
83 	if (hdr->length < MIN_PFLOG_HDRLEN) {
84 		printf("[pflog: invalid header length!]");
85 		goto out;
86 	}
87 	hdrlen = (hdr->length + 3) & 0xfc;
88 
89 	if (caplen < hdrlen) {
90 		printf("[|pflog]");
91 		goto out;
92 	}
93 
94 	/*
95 	 * Some printers want to get back at the link level addresses,
96 	 * and/or check that they're not walking off the end of the packet.
97 	 * Rather than pass them all the way down, we set these globals.
98 	 */
99 	packetp = p;
100 	snapend = p + caplen;
101 
102 	hdr = (struct pfloghdr *)p;
103 	if (eflag) {
104 		printf("rule ");
105 		if (ntohl(hdr->rulenr) == (u_int32_t) -1)
106 			printf("def");
107 		else {
108 			printf("%u", ntohl(hdr->rulenr));
109 			if (hdr->ruleset[0]) {
110 				printf(".%s", hdr->ruleset);
111 				if (ntohl(hdr->subrulenr) == (u_int32_t) -1)
112 					printf(".def");
113 				else
114 					printf(".%u", ntohl(hdr->subrulenr));
115 			}
116 		}
117 		if (hdr->reason < PFRES_MAX)
118 			printf("/(%s) ", pf_reasons[hdr->reason]);
119 		else
120 			printf("/(unkn %u) ", (unsigned)hdr->reason);
121 		if (vflag)
122 			printf("[uid %u, pid %u] ", (unsigned)hdr->rule_uid,
123 			    (unsigned)hdr->rule_pid);
124 
125 		switch (hdr->action) {
126 		case PF_MATCH:
127 			printf("match");
128 			break;
129 		case PF_SCRUB:
130 			printf("scrub");
131 			break;
132 		case PF_PASS:
133 			printf("pass");
134 			break;
135 		case PF_DROP:
136 			printf("block");
137 			break;
138 		case PF_NAT:
139 		case PF_NONAT:
140 			printf("nat");
141 			break;
142 		case PF_BINAT:
143 		case PF_NOBINAT:
144 			printf("binat");
145 			break;
146 		case PF_RDR:
147 		case PF_NORDR:
148 			printf("rdr");
149 			break;
150 		}
151 		printf(" %s on %s: ",
152 		    hdr->dir == PF_OUT ? "out" : "in",
153 		    hdr->ifname);
154 		if (vflag && hdr->pid != NO_PID)
155 			printf("[uid %u, pid %u] ", (unsigned)hdr->uid,
156 			    (unsigned)hdr->pid);
157 		if (vflag && hdr->rewritten) {
158 			char buf[48];
159 
160 			if (inet_ntop(hdr->af, &hdr->saddr.v4, buf,
161 			    sizeof(buf)) == NULL)
162 				printf("[orig src ?, ");
163 			else
164 				printf("[orig src %s:%u, ", buf,
165 				    ntohs(hdr->sport));
166 			if (inet_ntop(hdr->af, &hdr->daddr.v4, buf,
167 			    sizeof(buf)) == NULL)
168 				printf("dst ?] ");
169 			else
170 				printf("dst %s:%u] ", buf,
171 				    ntohs(hdr->dport));
172 		}
173 	}
174 	af = hdr->naf;
175 	length -= hdrlen;
176 	if (af == AF_INET) {
177 		ip = (struct ip *)(p + hdrlen);
178 		ip_print((const u_char *)ip, length);
179 		if (xflag)
180 			default_print((const u_char *)ip,
181 			    caplen - hdrlen);
182 	} else {
183 #ifdef INET6
184 		ip6 = (struct ip6_hdr *)(p + hdrlen);
185 		ip6_print((const u_char *)ip6, length);
186 		if (xflag)
187 			default_print((const u_char *)ip6,
188 			    caplen - hdrlen);
189 #endif
190 	}
191 
192 out:
193 	putchar('\n');
194 }
195