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