xref: /freebsd/sys/netpfil/ipfw/ip_fw_log.c (revision f4dc9bf43457515e5c88d1400d4f5ff70a82d9c7)
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/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 #include <sys/syslog.h>
48 #include <sys/lock.h>
49 #include <sys/rwlock.h>
50 #include <net/ethernet.h> /* for ETHERTYPE_IP */
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_clone.h>
54 #include <net/vnet.h>
55 #include <net/if_types.h>	/* for IFT_PFLOG */
56 #include <net/bpf.h>		/* for BPF */
57 
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/ip_var.h>
62 #include <netinet/ip_fw.h>
63 #include <netinet/tcp_var.h>
64 #include <netinet/udp.h>
65 
66 #include <netinet/ip6.h>
67 #include <netinet/icmp6.h>
68 #ifdef INET6
69 #include <netinet6/in6_var.h>	/* ip6_sprintf() */
70 #endif
71 
72 #include <netpfil/ipfw/ip_fw_private.h>
73 
74 #ifdef MAC
75 #include <security/mac/mac_framework.h>
76 #endif
77 
78 /*
79  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
80  * Other macros just cast void * into the appropriate type
81  */
82 #define	L3HDR(T, ip)	((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
83 #define	TCP(p)		((struct tcphdr *)(p))
84 #define	SCTP(p)		((struct sctphdr *)(p))
85 #define	UDP(p)		((struct udphdr *)(p))
86 #define	ICMP(p)		((struct icmphdr *)(p))
87 #define	ICMP6(p)	((struct icmp6_hdr *)(p))
88 
89 #ifdef __APPLE__
90 #undef snprintf
91 #define snprintf	sprintf
92 #define SNPARGS(buf, len) buf + len
93 #define SNP(buf) buf
94 #else	/* !__APPLE__ */
95 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
96 #define SNP(buf) buf, sizeof(buf)
97 #endif /* !__APPLE__ */
98 
99 #ifdef WITHOUT_BPF
100 void
101 ipfw_log_bpf(int onoff)
102 {
103 }
104 #else /* !WITHOUT_BPF */
105 static VNET_DEFINE(struct ifnet *, log_if);	/* hook to attach to bpf */
106 #define	V_log_if		VNET(log_if)
107 static struct rwlock log_if_lock;
108 #define	LOGIF_LOCK_INIT(x)	rw_init(&log_if_lock, "ipfw log_if lock")
109 #define	LOGIF_LOCK_DESTROY(x)	rw_destroy(&log_if_lock)
110 #define	LOGIF_RLOCK(x)		rw_rlock(&log_if_lock)
111 #define	LOGIF_RUNLOCK(x)	rw_runlock(&log_if_lock)
112 #define	LOGIF_WLOCK(x)		rw_wlock(&log_if_lock)
113 #define	LOGIF_WUNLOCK(x)	rw_wunlock(&log_if_lock)
114 
115 static const char ipfwname[] = "ipfw";
116 
117 /* we use this dummy function for all ifnet callbacks */
118 static int
119 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
120 {
121 	return EINVAL;
122 }
123 
124 static int
125 ipfw_log_output(struct ifnet *ifp, struct mbuf *m,
126 	const struct sockaddr *dst, struct route *ro)
127 {
128 	if (m != NULL)
129 		FREE_PKT(m);
130 	return EINVAL;
131 }
132 
133 static void
134 ipfw_log_start(struct ifnet* ifp)
135 {
136 	panic("ipfw_log_start() must not be called");
137 }
138 
139 static const u_char ipfwbroadcastaddr[6] =
140 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
141 
142 static int
143 ipfw_log_clone_match(struct if_clone *ifc, const char *name)
144 {
145 
146 	return (strncmp(name, ipfwname, sizeof(ipfwname) - 1) == 0);
147 }
148 
149 static int
150 ipfw_log_clone_create(struct if_clone *ifc, char *name, size_t len,
151     caddr_t params)
152 {
153 	int error;
154 	int unit;
155 	struct ifnet *ifp;
156 
157 	error = ifc_name2unit(name, &unit);
158 	if (error)
159 		return (error);
160 
161 	error = ifc_alloc_unit(ifc, &unit);
162 	if (error)
163 		return (error);
164 
165 	ifp = if_alloc(IFT_PFLOG);
166 	if (ifp == NULL) {
167 		ifc_free_unit(ifc, unit);
168 		return (ENOSPC);
169 	}
170 	ifp->if_dname = ipfwname;
171 	ifp->if_dunit = unit;
172 	snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", ipfwname, unit);
173 	strlcpy(name, ifp->if_xname, len);
174 	ifp->if_mtu = 65536;
175 	ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
176 	ifp->if_init = (void *)log_dummy;
177 	ifp->if_ioctl = log_dummy;
178 	ifp->if_start = ipfw_log_start;
179 	ifp->if_output = ipfw_log_output;
180 	ifp->if_addrlen = 6;
181 	ifp->if_hdrlen = 14;
182 	ifp->if_broadcastaddr = ipfwbroadcastaddr;
183 	ifp->if_baudrate = IF_Mbps(10);
184 
185 	LOGIF_WLOCK();
186 	if (V_log_if == NULL)
187 		V_log_if = ifp;
188 	else {
189 		LOGIF_WUNLOCK();
190 		if_free(ifp);
191 		ifc_free_unit(ifc, unit);
192 		return (EEXIST);
193 	}
194 	LOGIF_WUNLOCK();
195 	if_attach(ifp);
196 	bpfattach(ifp, DLT_EN10MB, 14);
197 
198 	return (0);
199 }
200 
201 static int
202 ipfw_log_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
203 {
204 	int unit;
205 
206 	if (ifp == NULL)
207 		return (0);
208 
209 	LOGIF_WLOCK();
210 	if (V_log_if != NULL && ifp == V_log_if)
211 		V_log_if = NULL;
212 	else {
213 		LOGIF_WUNLOCK();
214 		return (EINVAL);
215 	}
216 	LOGIF_WUNLOCK();
217 
218 	unit = ifp->if_dunit;
219 	bpfdetach(ifp);
220 	if_detach(ifp);
221 	if_free(ifp);
222 	ifc_free_unit(ifc, unit);
223 
224 	return (0);
225 }
226 
227 static VNET_DEFINE(struct if_clone *, ipfw_log_cloner);
228 #define	V_ipfw_log_cloner		VNET(ipfw_log_cloner)
229 
230 void
231 ipfw_log_bpf(int onoff)
232 {
233 
234 	if (onoff) {
235 		if (IS_DEFAULT_VNET(curvnet))
236 			LOGIF_LOCK_INIT();
237 		V_ipfw_log_cloner = if_clone_advanced(ipfwname, 0,
238 		    ipfw_log_clone_match, ipfw_log_clone_create,
239 		    ipfw_log_clone_destroy);
240 	} else {
241 		if_clone_detach(V_ipfw_log_cloner);
242 		if (IS_DEFAULT_VNET(curvnet))
243 			LOGIF_LOCK_DESTROY();
244 	}
245 }
246 #endif /* !WITHOUT_BPF */
247 
248 #define	TARG(k, f)	IP_FW_ARG_TABLEARG(chain, k, f)
249 /*
250  * We enter here when we have a rule with O_LOG.
251  * XXX this function alone takes about 2Kbytes of code!
252  */
253 void
254 ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
255     struct ip_fw_args *args, struct mbuf *m, struct ifnet *oif,
256     u_short offset, uint32_t tablearg, struct ip *ip)
257 {
258 	char *action;
259 	int limit_reached = 0;
260 	char action2[92], proto[128], fragment[32];
261 
262 	if (V_fw_verbose == 0) {
263 #ifndef WITHOUT_BPF
264 		LOGIF_RLOCK();
265 		if (V_log_if == NULL || V_log_if->if_bpf == NULL) {
266 			LOGIF_RUNLOCK();
267 			return;
268 		}
269 
270 		if (args->eh) /* layer2, use orig hdr */
271 			BPF_MTAP2(V_log_if, args->eh, ETHER_HDR_LEN, m);
272 		else {
273 			/* Add fake header. Later we will store
274 			 * more info in the header.
275 			 */
276 			if (ip->ip_v == 4)
277 				BPF_MTAP2(V_log_if, "DDDDDDSSSSSS\x08\x00", ETHER_HDR_LEN, m);
278 			else if  (ip->ip_v == 6)
279 				BPF_MTAP2(V_log_if, "DDDDDDSSSSSS\x86\xdd", ETHER_HDR_LEN, m);
280 			else
281 				/* Obviously bogus EtherType. */
282 				BPF_MTAP2(V_log_if, "DDDDDDSSSSSS\xff\xff", ETHER_HDR_LEN, m);
283 		}
284 		LOGIF_RUNLOCK();
285 #endif /* !WITHOUT_BPF */
286 		return;
287 	}
288 	/* the old 'log' function */
289 	fragment[0] = '\0';
290 	proto[0] = '\0';
291 
292 	if (f == NULL) {	/* bogus pkt */
293 		if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
294 			return;
295 		V_norule_counter++;
296 		if (V_norule_counter == V_verbose_limit)
297 			limit_reached = V_verbose_limit;
298 		action = "Refuse";
299 	} else {	/* O_LOG is the first action, find the real one */
300 		ipfw_insn *cmd = ACTION_PTR(f);
301 		ipfw_insn_log *l = (ipfw_insn_log *)cmd;
302 
303 		if (l->max_log != 0 && l->log_left == 0)
304 			return;
305 		l->log_left--;
306 		if (l->log_left == 0)
307 			limit_reached = l->max_log;
308 		cmd += F_LEN(cmd);	/* point to first action */
309 		if (cmd->opcode == O_ALTQ) {
310 			ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
311 
312 			snprintf(SNPARGS(action2, 0), "Altq %d",
313 				altq->qid);
314 			cmd += F_LEN(cmd);
315 		}
316 		if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
317 		    cmd->opcode == O_SETDSCP)
318 			cmd += F_LEN(cmd);
319 
320 		action = action2;
321 		switch (cmd->opcode) {
322 		case O_DENY:
323 			action = "Deny";
324 			break;
325 
326 		case O_REJECT:
327 			if (cmd->arg1==ICMP_REJECT_RST)
328 				action = "Reset";
329 			else if (cmd->arg1==ICMP_UNREACH_HOST)
330 				action = "Reject";
331 			else
332 				snprintf(SNPARGS(action2, 0), "Unreach %d",
333 					cmd->arg1);
334 			break;
335 
336 		case O_UNREACH6:
337 			if (cmd->arg1==ICMP6_UNREACH_RST)
338 				action = "Reset";
339 			else
340 				snprintf(SNPARGS(action2, 0), "Unreach %d",
341 					cmd->arg1);
342 			break;
343 
344 		case O_ACCEPT:
345 			action = "Accept";
346 			break;
347 		case O_COUNT:
348 			action = "Count";
349 			break;
350 		case O_DIVERT:
351 			snprintf(SNPARGS(action2, 0), "Divert %d",
352 				TARG(cmd->arg1, divert));
353 			break;
354 		case O_TEE:
355 			snprintf(SNPARGS(action2, 0), "Tee %d",
356 				TARG(cmd->arg1, divert));
357 			break;
358 		case O_SETFIB:
359 			snprintf(SNPARGS(action2, 0), "SetFib %d",
360 				TARG(cmd->arg1, fib) & 0x7FFF);
361 			break;
362 		case O_SKIPTO:
363 			snprintf(SNPARGS(action2, 0), "SkipTo %d",
364 				TARG(cmd->arg1, skipto));
365 			break;
366 		case O_PIPE:
367 			snprintf(SNPARGS(action2, 0), "Pipe %d",
368 				TARG(cmd->arg1, pipe));
369 			break;
370 		case O_QUEUE:
371 			snprintf(SNPARGS(action2, 0), "Queue %d",
372 				TARG(cmd->arg1, pipe));
373 			break;
374 		case O_FORWARD_IP: {
375 			ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
376 			int len;
377 			struct in_addr dummyaddr;
378 			if (sa->sa.sin_addr.s_addr == INADDR_ANY)
379 				dummyaddr.s_addr = htonl(tablearg);
380 			else
381 				dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
382 
383 			len = snprintf(SNPARGS(action2, 0), "Forward to %s",
384 				inet_ntoa(dummyaddr));
385 
386 			if (sa->sa.sin_port)
387 				snprintf(SNPARGS(action2, len), ":%d",
388 				    sa->sa.sin_port);
389 			}
390 			break;
391 #ifdef INET6
392 		case O_FORWARD_IP6: {
393 			char buf[INET6_ADDRSTRLEN];
394 			ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
395 			int len;
396 
397 			len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
398 			    ip6_sprintf(buf, &sa->sa.sin6_addr));
399 
400 			if (sa->sa.sin6_port)
401 				snprintf(SNPARGS(action2, len), ":%u",
402 				    sa->sa.sin6_port);
403 			}
404 			break;
405 #endif
406 		case O_NETGRAPH:
407 			snprintf(SNPARGS(action2, 0), "Netgraph %d",
408 				cmd->arg1);
409 			break;
410 		case O_NGTEE:
411 			snprintf(SNPARGS(action2, 0), "Ngtee %d",
412 				cmd->arg1);
413 			break;
414 		case O_NAT:
415 			action = "Nat";
416  			break;
417 		case O_REASS:
418 			action = "Reass";
419 			break;
420 		case O_CALLRETURN:
421 			if (cmd->len & F_NOT)
422 				action = "Return";
423 			else
424 				snprintf(SNPARGS(action2, 0), "Call %d",
425 				    cmd->arg1);
426 			break;
427 		default:
428 			action = "UNKNOWN";
429 			break;
430 		}
431 	}
432 
433 	if (hlen == 0) {	/* non-ip */
434 		snprintf(SNPARGS(proto, 0), "MAC");
435 
436 	} else {
437 		int len;
438 #ifdef INET6
439 		char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
440 #else
441 		char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
442 #endif
443 		struct icmphdr *icmp;
444 		struct tcphdr *tcp;
445 		struct udphdr *udp;
446 #ifdef INET6
447 		struct ip6_hdr *ip6 = NULL;
448 		struct icmp6_hdr *icmp6;
449 		u_short ip6f_mf;
450 #endif
451 		src[0] = '\0';
452 		dst[0] = '\0';
453 #ifdef INET6
454 		ip6f_mf = offset & IP6F_MORE_FRAG;
455 		offset &= IP6F_OFF_MASK;
456 
457 		if (IS_IP6_FLOW_ID(&(args->f_id))) {
458 			char ip6buf[INET6_ADDRSTRLEN];
459 			snprintf(src, sizeof(src), "[%s]",
460 			    ip6_sprintf(ip6buf, &args->f_id.src_ip6));
461 			snprintf(dst, sizeof(dst), "[%s]",
462 			    ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
463 
464 			ip6 = (struct ip6_hdr *)ip;
465 			tcp = (struct tcphdr *)(((char *)ip) + hlen);
466 			udp = (struct udphdr *)(((char *)ip) + hlen);
467 		} else
468 #endif
469 		{
470 			tcp = L3HDR(struct tcphdr, ip);
471 			udp = L3HDR(struct udphdr, ip);
472 
473 			inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
474 			inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
475 		}
476 
477 		switch (args->f_id.proto) {
478 		case IPPROTO_TCP:
479 			len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
480 			if (offset == 0)
481 				snprintf(SNPARGS(proto, len), ":%d %s:%d",
482 				    ntohs(tcp->th_sport),
483 				    dst,
484 				    ntohs(tcp->th_dport));
485 			else
486 				snprintf(SNPARGS(proto, len), " %s", dst);
487 			break;
488 
489 		case IPPROTO_UDP:
490 			len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
491 			if (offset == 0)
492 				snprintf(SNPARGS(proto, len), ":%d %s:%d",
493 				    ntohs(udp->uh_sport),
494 				    dst,
495 				    ntohs(udp->uh_dport));
496 			else
497 				snprintf(SNPARGS(proto, len), " %s", dst);
498 			break;
499 
500 		case IPPROTO_ICMP:
501 			icmp = L3HDR(struct icmphdr, ip);
502 			if (offset == 0)
503 				len = snprintf(SNPARGS(proto, 0),
504 				    "ICMP:%u.%u ",
505 				    icmp->icmp_type, icmp->icmp_code);
506 			else
507 				len = snprintf(SNPARGS(proto, 0), "ICMP ");
508 			len += snprintf(SNPARGS(proto, len), "%s", src);
509 			snprintf(SNPARGS(proto, len), " %s", dst);
510 			break;
511 #ifdef INET6
512 		case IPPROTO_ICMPV6:
513 			icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
514 			if (offset == 0)
515 				len = snprintf(SNPARGS(proto, 0),
516 				    "ICMPv6:%u.%u ",
517 				    icmp6->icmp6_type, icmp6->icmp6_code);
518 			else
519 				len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
520 			len += snprintf(SNPARGS(proto, len), "%s", src);
521 			snprintf(SNPARGS(proto, len), " %s", dst);
522 			break;
523 #endif
524 		default:
525 			len = snprintf(SNPARGS(proto, 0), "P:%d %s",
526 			    args->f_id.proto, src);
527 			snprintf(SNPARGS(proto, len), " %s", dst);
528 			break;
529 		}
530 
531 #ifdef INET6
532 		if (IS_IP6_FLOW_ID(&(args->f_id))) {
533 			if (offset || ip6f_mf)
534 				snprintf(SNPARGS(fragment, 0),
535 				    " (frag %08x:%d@%d%s)",
536 				    args->f_id.extra,
537 				    ntohs(ip6->ip6_plen) - hlen,
538 				    ntohs(offset) << 3, ip6f_mf ? "+" : "");
539 		} else
540 #endif
541 		{
542 			int ipoff, iplen;
543 			ipoff = ntohs(ip->ip_off);
544 			iplen = ntohs(ip->ip_len);
545 			if (ipoff & (IP_MF | IP_OFFMASK))
546 				snprintf(SNPARGS(fragment, 0),
547 				    " (frag %d:%d@%d%s)",
548 				    ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
549 				    offset << 3,
550 				    (ipoff & IP_MF) ? "+" : "");
551 		}
552 	}
553 #ifdef __FreeBSD__
554 	if (oif || m->m_pkthdr.rcvif)
555 		log(LOG_SECURITY | LOG_INFO,
556 		    "ipfw: %d %s %s %s via %s%s\n",
557 		    f ? f->rulenum : -1,
558 		    action, proto, oif ? "out" : "in",
559 		    oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
560 		    fragment);
561 	else
562 #endif
563 		log(LOG_SECURITY | LOG_INFO,
564 		    "ipfw: %d %s %s [no if info]%s\n",
565 		    f ? f->rulenum : -1,
566 		    action, proto, fragment);
567 	if (limit_reached)
568 		log(LOG_SECURITY | LOG_NOTICE,
569 		    "ipfw: limit %d reached on entry %d\n",
570 		    limit_reached, f ? f->rulenum : -1);
571 }
572 /* end of file */
573