xref: /freebsd/sys/netpfil/ipfw/ip_fw_log.c (revision cbd30a72ca196976c1c700400ecd424baa1b9c16)
1 /*-
2  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28 
29 /*
30  * Logging support for ipfw
31  */
32 
33 #include "opt_ipfw.h"
34 #include "opt_inet.h"
35 #ifndef INET
36 #error IPFIREWALL requires INET.
37 #endif /* INET */
38 #include "opt_inet6.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/mbuf.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <net/ethernet.h> /* for ETHERTYPE_IP */
48 #include <net/if.h>
49 #include <net/if_var.h>
50 #include <net/vnet.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip_icmp.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/ip_fw.h>
57 #include <netinet/tcp_var.h>
58 #include <netinet/udp.h>
59 
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
62 #ifdef INET6
63 #include <netinet6/in6_var.h>	/* ip6_sprintf() */
64 #endif
65 
66 #include <netpfil/ipfw/ip_fw_private.h>
67 
68 #ifdef MAC
69 #include <security/mac/mac_framework.h>
70 #endif
71 
72 /*
73  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
74  * Other macros just cast void * into the appropriate type
75  */
76 #define	L3HDR(T, ip)	((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
77 #define	TCP(p)		((struct tcphdr *)(p))
78 #define	SCTP(p)		((struct sctphdr *)(p))
79 #define	UDP(p)		((struct udphdr *)(p))
80 #define	ICMP(p)		((struct icmphdr *)(p))
81 #define	ICMP6(p)	((struct icmp6_hdr *)(p))
82 
83 #ifdef __APPLE__
84 #undef snprintf
85 #define snprintf	sprintf
86 #define SNPARGS(buf, len) buf + len
87 #define SNP(buf) buf
88 #else	/* !__APPLE__ */
89 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
90 #define SNP(buf) buf, sizeof(buf)
91 #endif /* !__APPLE__ */
92 
93 #define	TARG(k, f)	IP_FW_ARG_TABLEARG(chain, k, f)
94 /*
95  * We enter here when we have a rule with O_LOG.
96  * XXX this function alone takes about 2Kbytes of code!
97  */
98 void
99 ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
100     struct ip_fw_args *args, struct mbuf *m, struct ifnet *oif,
101     u_short offset, uint32_t tablearg, struct ip *ip)
102 {
103 	char *action;
104 	int limit_reached = 0;
105 	char action2[92], proto[128], fragment[32];
106 
107 	if (V_fw_verbose == 0) {
108 		if (args->eh) /* layer2, use orig hdr */
109 			ipfw_bpf_mtap2(args->eh, ETHER_HDR_LEN, m);
110 		else {
111 			/* Add fake header. Later we will store
112 			 * more info in the header.
113 			 */
114 			if (ip->ip_v == 4)
115 				ipfw_bpf_mtap2("DDDDDDSSSSSS\x08\x00",
116 				    ETHER_HDR_LEN, m);
117 			else if (ip->ip_v == 6)
118 				ipfw_bpf_mtap2("DDDDDDSSSSSS\x86\xdd",
119 				    ETHER_HDR_LEN, m);
120 			else
121 				/* Obviously bogus EtherType. */
122 				ipfw_bpf_mtap2("DDDDDDSSSSSS\xff\xff",
123 				    ETHER_HDR_LEN, m);
124 		}
125 		return;
126 	}
127 	/* the old 'log' function */
128 	fragment[0] = '\0';
129 	proto[0] = '\0';
130 
131 	if (f == NULL) {	/* bogus pkt */
132 		if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
133 			return;
134 		V_norule_counter++;
135 		if (V_norule_counter == V_verbose_limit)
136 			limit_reached = V_verbose_limit;
137 		action = "Refuse";
138 	} else {	/* O_LOG is the first action, find the real one */
139 		ipfw_insn *cmd = ACTION_PTR(f);
140 		ipfw_insn_log *l = (ipfw_insn_log *)cmd;
141 
142 		if (l->max_log != 0 && l->log_left == 0)
143 			return;
144 		l->log_left--;
145 		if (l->log_left == 0)
146 			limit_reached = l->max_log;
147 		cmd += F_LEN(cmd);	/* point to first action */
148 		if (cmd->opcode == O_ALTQ) {
149 			ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
150 
151 			snprintf(SNPARGS(action2, 0), "Altq %d",
152 				altq->qid);
153 			cmd += F_LEN(cmd);
154 		}
155 		if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
156 		    cmd->opcode == O_SETDSCP)
157 			cmd += F_LEN(cmd);
158 
159 		action = action2;
160 		switch (cmd->opcode) {
161 		case O_DENY:
162 			action = "Deny";
163 			break;
164 
165 		case O_REJECT:
166 			if (cmd->arg1==ICMP_REJECT_RST)
167 				action = "Reset";
168 			else if (cmd->arg1==ICMP_UNREACH_HOST)
169 				action = "Reject";
170 			else
171 				snprintf(SNPARGS(action2, 0), "Unreach %d",
172 					cmd->arg1);
173 			break;
174 
175 		case O_UNREACH6:
176 			if (cmd->arg1==ICMP6_UNREACH_RST)
177 				action = "Reset";
178 			else
179 				snprintf(SNPARGS(action2, 0), "Unreach %d",
180 					cmd->arg1);
181 			break;
182 
183 		case O_ACCEPT:
184 			action = "Accept";
185 			break;
186 		case O_COUNT:
187 			action = "Count";
188 			break;
189 		case O_DIVERT:
190 			snprintf(SNPARGS(action2, 0), "Divert %d",
191 				TARG(cmd->arg1, divert));
192 			break;
193 		case O_TEE:
194 			snprintf(SNPARGS(action2, 0), "Tee %d",
195 				TARG(cmd->arg1, divert));
196 			break;
197 		case O_SETFIB:
198 			snprintf(SNPARGS(action2, 0), "SetFib %d",
199 				TARG(cmd->arg1, fib) & 0x7FFF);
200 			break;
201 		case O_SKIPTO:
202 			snprintf(SNPARGS(action2, 0), "SkipTo %d",
203 				TARG(cmd->arg1, skipto));
204 			break;
205 		case O_PIPE:
206 			snprintf(SNPARGS(action2, 0), "Pipe %d",
207 				TARG(cmd->arg1, pipe));
208 			break;
209 		case O_QUEUE:
210 			snprintf(SNPARGS(action2, 0), "Queue %d",
211 				TARG(cmd->arg1, pipe));
212 			break;
213 		case O_FORWARD_IP: {
214 			char buf[INET_ADDRSTRLEN];
215 			ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
216 			int len;
217 			struct in_addr dummyaddr;
218 			if (sa->sa.sin_addr.s_addr == INADDR_ANY)
219 				dummyaddr.s_addr = htonl(tablearg);
220 			else
221 				dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
222 
223 			len = snprintf(SNPARGS(action2, 0), "Forward to %s",
224 				inet_ntoa_r(dummyaddr, buf));
225 
226 			if (sa->sa.sin_port)
227 				snprintf(SNPARGS(action2, len), ":%d",
228 				    sa->sa.sin_port);
229 			}
230 			break;
231 #ifdef INET6
232 		case O_FORWARD_IP6: {
233 			char buf[INET6_ADDRSTRLEN];
234 			ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
235 			int len;
236 
237 			len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
238 			    ip6_sprintf(buf, &sa->sa.sin6_addr));
239 
240 			if (sa->sa.sin6_port)
241 				snprintf(SNPARGS(action2, len), ":%u",
242 				    sa->sa.sin6_port);
243 			}
244 			break;
245 #endif
246 		case O_NETGRAPH:
247 			snprintf(SNPARGS(action2, 0), "Netgraph %d",
248 				cmd->arg1);
249 			break;
250 		case O_NGTEE:
251 			snprintf(SNPARGS(action2, 0), "Ngtee %d",
252 				cmd->arg1);
253 			break;
254 		case O_NAT:
255 			action = "Nat";
256  			break;
257 		case O_REASS:
258 			action = "Reass";
259 			break;
260 		case O_CALLRETURN:
261 			if (cmd->len & F_NOT)
262 				action = "Return";
263 			else
264 				snprintf(SNPARGS(action2, 0), "Call %d",
265 				    cmd->arg1);
266 			break;
267 		default:
268 			action = "UNKNOWN";
269 			break;
270 		}
271 	}
272 
273 	if (hlen == 0) {	/* non-ip */
274 		snprintf(SNPARGS(proto, 0), "MAC");
275 
276 	} else {
277 		int len;
278 #ifdef INET6
279 		char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
280 #else
281 		char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
282 #endif
283 		struct icmphdr *icmp;
284 		struct tcphdr *tcp;
285 		struct udphdr *udp;
286 #ifdef INET6
287 		struct ip6_hdr *ip6 = NULL;
288 		struct icmp6_hdr *icmp6;
289 		u_short ip6f_mf;
290 #endif
291 		src[0] = '\0';
292 		dst[0] = '\0';
293 #ifdef INET6
294 		ip6f_mf = offset & IP6F_MORE_FRAG;
295 		offset &= IP6F_OFF_MASK;
296 
297 		if (IS_IP6_FLOW_ID(&(args->f_id))) {
298 			char ip6buf[INET6_ADDRSTRLEN];
299 			snprintf(src, sizeof(src), "[%s]",
300 			    ip6_sprintf(ip6buf, &args->f_id.src_ip6));
301 			snprintf(dst, sizeof(dst), "[%s]",
302 			    ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
303 
304 			ip6 = (struct ip6_hdr *)ip;
305 			tcp = (struct tcphdr *)(((char *)ip) + hlen);
306 			udp = (struct udphdr *)(((char *)ip) + hlen);
307 		} else
308 #endif
309 		{
310 			tcp = L3HDR(struct tcphdr, ip);
311 			udp = L3HDR(struct udphdr, ip);
312 
313 			inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
314 			inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
315 		}
316 
317 		switch (args->f_id.proto) {
318 		case IPPROTO_TCP:
319 			len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
320 			if (offset == 0)
321 				snprintf(SNPARGS(proto, len), ":%d %s:%d",
322 				    ntohs(tcp->th_sport),
323 				    dst,
324 				    ntohs(tcp->th_dport));
325 			else
326 				snprintf(SNPARGS(proto, len), " %s", dst);
327 			break;
328 
329 		case IPPROTO_UDP:
330 			len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
331 			if (offset == 0)
332 				snprintf(SNPARGS(proto, len), ":%d %s:%d",
333 				    ntohs(udp->uh_sport),
334 				    dst,
335 				    ntohs(udp->uh_dport));
336 			else
337 				snprintf(SNPARGS(proto, len), " %s", dst);
338 			break;
339 
340 		case IPPROTO_ICMP:
341 			icmp = L3HDR(struct icmphdr, ip);
342 			if (offset == 0)
343 				len = snprintf(SNPARGS(proto, 0),
344 				    "ICMP:%u.%u ",
345 				    icmp->icmp_type, icmp->icmp_code);
346 			else
347 				len = snprintf(SNPARGS(proto, 0), "ICMP ");
348 			len += snprintf(SNPARGS(proto, len), "%s", src);
349 			snprintf(SNPARGS(proto, len), " %s", dst);
350 			break;
351 #ifdef INET6
352 		case IPPROTO_ICMPV6:
353 			icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
354 			if (offset == 0)
355 				len = snprintf(SNPARGS(proto, 0),
356 				    "ICMPv6:%u.%u ",
357 				    icmp6->icmp6_type, icmp6->icmp6_code);
358 			else
359 				len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
360 			len += snprintf(SNPARGS(proto, len), "%s", src);
361 			snprintf(SNPARGS(proto, len), " %s", dst);
362 			break;
363 #endif
364 		default:
365 			len = snprintf(SNPARGS(proto, 0), "P:%d %s",
366 			    args->f_id.proto, src);
367 			snprintf(SNPARGS(proto, len), " %s", dst);
368 			break;
369 		}
370 
371 #ifdef INET6
372 		if (IS_IP6_FLOW_ID(&(args->f_id))) {
373 			if (offset || ip6f_mf)
374 				snprintf(SNPARGS(fragment, 0),
375 				    " (frag %08x:%d@%d%s)",
376 				    args->f_id.extra,
377 				    ntohs(ip6->ip6_plen) - hlen,
378 				    ntohs(offset) << 3, ip6f_mf ? "+" : "");
379 		} else
380 #endif
381 		{
382 			int ipoff, iplen;
383 			ipoff = ntohs(ip->ip_off);
384 			iplen = ntohs(ip->ip_len);
385 			if (ipoff & (IP_MF | IP_OFFMASK))
386 				snprintf(SNPARGS(fragment, 0),
387 				    " (frag %d:%d@%d%s)",
388 				    ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
389 				    offset << 3,
390 				    (ipoff & IP_MF) ? "+" : "");
391 		}
392 	}
393 #ifdef __FreeBSD__
394 	if (oif || m->m_pkthdr.rcvif)
395 		log(LOG_SECURITY | LOG_INFO,
396 		    "ipfw: %d %s %s %s via %s%s\n",
397 		    f ? f->rulenum : -1,
398 		    action, proto, oif ? "out" : "in",
399 		    oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
400 		    fragment);
401 	else
402 #endif
403 		log(LOG_SECURITY | LOG_INFO,
404 		    "ipfw: %d %s %s [no if info]%s\n",
405 		    f ? f->rulenum : -1,
406 		    action, proto, fragment);
407 	if (limit_reached)
408 		log(LOG_SECURITY | LOG_NOTICE,
409 		    "ipfw: limit %d reached on entry %d\n",
410 		    limit_reached, f ? f->rulenum : -1);
411 }
412 /* end of file */
413