2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * Logging support for ipfw
38 #error IPFIREWALL requires INET.
40 #include "opt_inet6.h"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
49 #include <net/ethernet.h> /* for ETHERTYPE_IP */
51 #include <net/if_var.h>
54 #include <netinet/in.h>
55 #include <netinet/ip.h>
56 #include <netinet/ip_icmp.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/ip_fw.h>
59 #include <netinet/tcp_var.h>
60 #include <netinet/udp.h>
62 #include <netinet/ip6.h>
63 #include <netinet/icmp6.h>
65 #include <netinet6/in6_var.h> /* ip6_sprintf() */
68 #include <netpfil/ipfw/ip_fw_private.h>
71 #include <security/mac/mac_framework.h>
75 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
76 * Other macros just cast void * into the appropriate type
78 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
79 #define TCP(p) ((struct tcphdr *)(p))
80 #define SCTP(p) ((struct sctphdr *)(p))
81 #define UDP(p) ((struct udphdr *)(p))
82 #define ICMP(p) ((struct icmphdr *)(p))
83 #define ICMP6(p) ((struct icmp6_hdr *)(p))
87 #define snprintf sprintf
88 #define SNPARGS(buf, len) buf + len
90 #else /* !__APPLE__ */
91 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
92 #define SNP(buf) buf, sizeof(buf)
93 #endif /* !__APPLE__ */
95 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f)
97 * We enter here when we have a rule with O_LOG.
98 * XXX this function alone takes about 2Kbytes of code!
101 ipfw_log(struct ip_fw_chain *chain, struct ip_fw *f, u_int hlen,
102 struct ip_fw_args *args, u_short offset, uint32_t tablearg, struct ip *ip)
105 int limit_reached = 0;
106 char action2[92], proto[128], fragment[32];
108 if (V_fw_verbose == 0) {
109 if (args->flags & IPFW_ARGS_LENMASK)
110 ipfw_bpf_tap(args->mem, IPFW_ARGS_LENGTH(args->flags));
111 else if (args->flags & IPFW_ARGS_ETHER)
112 /* layer2, use orig hdr */
113 ipfw_bpf_mtap(args->m);
115 /* Add fake header. Later we will store
116 * more info in the header.
119 ipfw_bpf_mtap2("DDDDDDSSSSSS\x08\x00",
120 ETHER_HDR_LEN, args->m);
121 else if (ip->ip_v == 6)
122 ipfw_bpf_mtap2("DDDDDDSSSSSS\x86\xdd",
123 ETHER_HDR_LEN, args->m);
125 /* Obviously bogus EtherType. */
126 ipfw_bpf_mtap2("DDDDDDSSSSSS\xff\xff",
127 ETHER_HDR_LEN, args->m);
131 /* the old 'log' function */
135 if (f == NULL) { /* bogus pkt */
136 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
139 if (V_norule_counter == V_verbose_limit)
140 limit_reached = V_verbose_limit;
142 } else { /* O_LOG is the first action, find the real one */
143 ipfw_insn *cmd = ACTION_PTR(f);
144 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
146 if (l->max_log != 0 && l->log_left == 0)
149 if (l->log_left == 0)
150 limit_reached = l->max_log;
151 cmd += F_LEN(cmd); /* point to first action */
152 if (cmd->opcode == O_ALTQ) {
153 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
155 snprintf(SNPARGS(action2, 0), "Altq %d",
159 if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
160 cmd->opcode == O_SETDSCP)
164 switch (cmd->opcode) {
170 if (cmd->arg1==ICMP_REJECT_RST)
172 else if (cmd->arg1==ICMP_REJECT_ABORT)
174 else if (cmd->arg1==ICMP_UNREACH_HOST)
177 snprintf(SNPARGS(action2, 0), "Unreach %d",
182 if (cmd->arg1==ICMP6_UNREACH_RST)
184 else if (cmd->arg1==ICMP6_UNREACH_ABORT)
187 snprintf(SNPARGS(action2, 0), "Unreach %d",
198 snprintf(SNPARGS(action2, 0), "Divert %d",
199 TARG(cmd->arg1, divert));
202 snprintf(SNPARGS(action2, 0), "Tee %d",
203 TARG(cmd->arg1, divert));
206 snprintf(SNPARGS(action2, 0), "SetFib %d",
207 TARG(cmd->arg1, fib) & 0x7FFF);
210 snprintf(SNPARGS(action2, 0), "SkipTo %d",
211 TARG(cmd->arg1, skipto));
214 snprintf(SNPARGS(action2, 0), "Pipe %d",
215 TARG(cmd->arg1, pipe));
218 snprintf(SNPARGS(action2, 0), "Queue %d",
219 TARG(cmd->arg1, pipe));
222 char buf[INET_ADDRSTRLEN];
223 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
225 struct in_addr dummyaddr;
226 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
227 dummyaddr.s_addr = htonl(tablearg);
229 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
231 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
232 inet_ntoa_r(dummyaddr, buf));
235 snprintf(SNPARGS(action2, len), ":%d",
240 case O_FORWARD_IP6: {
241 char buf[INET6_ADDRSTRLEN];
242 ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
245 len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
246 ip6_sprintf(buf, &sa->sa.sin6_addr));
248 if (sa->sa.sin6_port)
249 snprintf(SNPARGS(action2, len), ":%u",
255 snprintf(SNPARGS(action2, 0), "Netgraph %d",
259 snprintf(SNPARGS(action2, 0), "Ngtee %d",
269 if (cmd->len & F_NOT)
272 snprintf(SNPARGS(action2, 0), "Call %d",
275 case O_EXTERNAL_ACTION:
276 snprintf(SNPARGS(action2, 0), "Eaction %s",
277 ((struct named_object *)SRV_OBJECT(chain,
286 if (hlen == 0) { /* non-ip */
287 snprintf(SNPARGS(proto, 0), "MAC");
292 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
294 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
296 struct icmphdr *icmp;
300 struct ip6_hdr *ip6 = NULL;
301 struct icmp6_hdr *icmp6;
307 ip6f_mf = offset & IP6F_MORE_FRAG;
308 offset &= IP6F_OFF_MASK;
310 if (IS_IP6_FLOW_ID(&(args->f_id))) {
311 char ip6buf[INET6_ADDRSTRLEN];
312 snprintf(src, sizeof(src), "[%s]",
313 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
314 snprintf(dst, sizeof(dst), "[%s]",
315 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
317 ip6 = (struct ip6_hdr *)ip;
318 tcp = (struct tcphdr *)(((char *)ip) + hlen);
319 udp = (struct udphdr *)(((char *)ip) + hlen);
323 tcp = L3HDR(struct tcphdr, ip);
324 udp = L3HDR(struct udphdr, ip);
326 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
327 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
330 switch (args->f_id.proto) {
332 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
334 snprintf(SNPARGS(proto, len), ":%d %s:%d",
335 ntohs(tcp->th_sport),
337 ntohs(tcp->th_dport));
339 snprintf(SNPARGS(proto, len), " %s", dst);
343 case IPPROTO_UDPLITE:
344 len = snprintf(SNPARGS(proto, 0), "UDP%s%s",
345 args->f_id.proto == IPPROTO_UDP ? " ": "Lite ",
348 snprintf(SNPARGS(proto, len), ":%d %s:%d",
349 ntohs(udp->uh_sport),
351 ntohs(udp->uh_dport));
353 snprintf(SNPARGS(proto, len), " %s", dst);
357 icmp = L3HDR(struct icmphdr, ip);
359 len = snprintf(SNPARGS(proto, 0),
361 icmp->icmp_type, icmp->icmp_code);
363 len = snprintf(SNPARGS(proto, 0), "ICMP ");
364 len += snprintf(SNPARGS(proto, len), "%s", src);
365 snprintf(SNPARGS(proto, len), " %s", dst);
369 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
371 len = snprintf(SNPARGS(proto, 0),
373 icmp6->icmp6_type, icmp6->icmp6_code);
375 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
376 len += snprintf(SNPARGS(proto, len), "%s", src);
377 snprintf(SNPARGS(proto, len), " %s", dst);
381 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
382 args->f_id.proto, src);
383 snprintf(SNPARGS(proto, len), " %s", dst);
388 if (IS_IP6_FLOW_ID(&(args->f_id))) {
389 if (offset || ip6f_mf)
390 snprintf(SNPARGS(fragment, 0),
391 " (frag %08x:%d@%d%s)",
393 ntohs(ip6->ip6_plen) - hlen,
394 ntohs(offset) << 3, ip6f_mf ? "+" : "");
399 ipoff = ntohs(ip->ip_off);
400 iplen = ntohs(ip->ip_len);
401 if (ipoff & (IP_MF | IP_OFFMASK))
402 snprintf(SNPARGS(fragment, 0),
403 " (frag %d:%d@%d%s)",
404 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
406 (ipoff & IP_MF) ? "+" : "");
410 log(LOG_SECURITY | LOG_INFO, "ipfw: %d %s %s %s via %s%s\n",
411 f ? f->rulenum : -1, action, proto,
412 args->flags & IPFW_ARGS_OUT ? "out" : "in", args->ifp->if_xname,
415 log(LOG_SECURITY | LOG_INFO, "ipfw: %d %s %s [no if info]%s\n",
416 f ? f->rulenum : -1, action, proto, fragment);
419 log(LOG_SECURITY | LOG_NOTICE,
420 "ipfw: limit %d reached on entry %d\n",
421 limit_reached, f ? f->rulenum : -1);