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