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