2 * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * Logging support for ipfw
36 #error IPFIREWALL requires INET.
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
44 #include <sys/socket.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <net/ethernet.h> /* for ETHERTYPE_IP */
49 #include <net/if_var.h>
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>
60 #include <netinet/ip6.h>
61 #include <netinet/icmp6.h>
63 #include <netinet6/in6_var.h> /* ip6_sprintf() */
66 #include <netpfil/ipfw/ip_fw_private.h>
69 #include <security/mac/mac_framework.h>
73 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
74 * Other macros just cast void * into the appropriate type
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))
85 #define snprintf sprintf
86 #define SNPARGS(buf, len) buf + len
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__ */
93 #define TARG(k, f) IP_FW_ARG_TABLEARG(chain, k, f)
95 * We enter here when we have a rule with O_LOG.
96 * XXX this function alone takes about 2Kbytes of code!
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)
104 int limit_reached = 0;
105 char action2[92], proto[128], fragment[32];
107 if (V_fw_verbose == 0) {
108 if (args->flags & IPFW_ARGS_ETHER) /* layer2, use orig hdr */
109 ipfw_bpf_mtap2(args->eh, ETHER_HDR_LEN, m);
111 /* Add fake header. Later we will store
112 * more info in the header.
115 ipfw_bpf_mtap2("DDDDDDSSSSSS\x08\x00",
117 else if (ip->ip_v == 6)
118 ipfw_bpf_mtap2("DDDDDDSSSSSS\x86\xdd",
121 /* Obviously bogus EtherType. */
122 ipfw_bpf_mtap2("DDDDDDSSSSSS\xff\xff",
127 /* the old 'log' function */
131 if (f == NULL) { /* bogus pkt */
132 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
135 if (V_norule_counter == V_verbose_limit)
136 limit_reached = V_verbose_limit;
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;
142 if (l->max_log != 0 && l->log_left == 0)
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;
151 snprintf(SNPARGS(action2, 0), "Altq %d",
155 if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
156 cmd->opcode == O_SETDSCP)
160 switch (cmd->opcode) {
166 if (cmd->arg1==ICMP_REJECT_RST)
168 else if (cmd->arg1==ICMP_REJECT_ABORT)
170 else if (cmd->arg1==ICMP_UNREACH_HOST)
173 snprintf(SNPARGS(action2, 0), "Unreach %d",
178 if (cmd->arg1==ICMP6_UNREACH_RST)
180 else if (cmd->arg1==ICMP6_UNREACH_ABORT)
183 snprintf(SNPARGS(action2, 0), "Unreach %d",
194 snprintf(SNPARGS(action2, 0), "Divert %d",
195 TARG(cmd->arg1, divert));
198 snprintf(SNPARGS(action2, 0), "Tee %d",
199 TARG(cmd->arg1, divert));
202 snprintf(SNPARGS(action2, 0), "SetFib %d",
203 TARG(cmd->arg1, fib) & 0x7FFF);
206 snprintf(SNPARGS(action2, 0), "SkipTo %d",
207 TARG(cmd->arg1, skipto));
210 snprintf(SNPARGS(action2, 0), "Pipe %d",
211 TARG(cmd->arg1, pipe));
214 snprintf(SNPARGS(action2, 0), "Queue %d",
215 TARG(cmd->arg1, pipe));
218 char buf[INET_ADDRSTRLEN];
219 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
221 struct in_addr dummyaddr;
222 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
223 dummyaddr.s_addr = htonl(tablearg);
225 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
227 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
228 inet_ntoa_r(dummyaddr, buf));
231 snprintf(SNPARGS(action2, len), ":%d",
236 case O_FORWARD_IP6: {
237 char buf[INET6_ADDRSTRLEN];
238 ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
241 len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
242 ip6_sprintf(buf, &sa->sa.sin6_addr));
244 if (sa->sa.sin6_port)
245 snprintf(SNPARGS(action2, len), ":%u",
251 snprintf(SNPARGS(action2, 0), "Netgraph %d",
255 snprintf(SNPARGS(action2, 0), "Ngtee %d",
265 if (cmd->len & F_NOT)
268 snprintf(SNPARGS(action2, 0), "Call %d",
271 case O_EXTERNAL_ACTION:
272 snprintf(SNPARGS(action2, 0), "Eaction %s",
273 ((struct named_object *)SRV_OBJECT(chain,
282 if (hlen == 0) { /* non-ip */
283 snprintf(SNPARGS(proto, 0), "MAC");
288 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
290 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
292 struct icmphdr *icmp;
296 struct ip6_hdr *ip6 = NULL;
297 struct icmp6_hdr *icmp6;
303 ip6f_mf = offset & IP6F_MORE_FRAG;
304 offset &= IP6F_OFF_MASK;
306 if (IS_IP6_FLOW_ID(&(args->f_id))) {
307 char ip6buf[INET6_ADDRSTRLEN];
308 snprintf(src, sizeof(src), "[%s]",
309 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
310 snprintf(dst, sizeof(dst), "[%s]",
311 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
313 ip6 = (struct ip6_hdr *)ip;
314 tcp = (struct tcphdr *)(((char *)ip) + hlen);
315 udp = (struct udphdr *)(((char *)ip) + hlen);
319 tcp = L3HDR(struct tcphdr, ip);
320 udp = L3HDR(struct udphdr, ip);
322 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
323 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
326 switch (args->f_id.proto) {
328 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
330 snprintf(SNPARGS(proto, len), ":%d %s:%d",
331 ntohs(tcp->th_sport),
333 ntohs(tcp->th_dport));
335 snprintf(SNPARGS(proto, len), " %s", dst);
339 case IPPROTO_UDPLITE:
340 len = snprintf(SNPARGS(proto, 0), "UDP%s%s",
341 args->f_id.proto == IPPROTO_UDP ? " ": "Lite ",
344 snprintf(SNPARGS(proto, len), ":%d %s:%d",
345 ntohs(udp->uh_sport),
347 ntohs(udp->uh_dport));
349 snprintf(SNPARGS(proto, len), " %s", dst);
353 icmp = L3HDR(struct icmphdr, ip);
355 len = snprintf(SNPARGS(proto, 0),
357 icmp->icmp_type, icmp->icmp_code);
359 len = snprintf(SNPARGS(proto, 0), "ICMP ");
360 len += snprintf(SNPARGS(proto, len), "%s", src);
361 snprintf(SNPARGS(proto, len), " %s", dst);
365 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
367 len = snprintf(SNPARGS(proto, 0),
369 icmp6->icmp6_type, icmp6->icmp6_code);
371 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
372 len += snprintf(SNPARGS(proto, len), "%s", src);
373 snprintf(SNPARGS(proto, len), " %s", dst);
377 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
378 args->f_id.proto, src);
379 snprintf(SNPARGS(proto, len), " %s", dst);
384 if (IS_IP6_FLOW_ID(&(args->f_id))) {
385 if (offset || ip6f_mf)
386 snprintf(SNPARGS(fragment, 0),
387 " (frag %08x:%d@%d%s)",
389 ntohs(ip6->ip6_plen) - hlen,
390 ntohs(offset) << 3, ip6f_mf ? "+" : "");
395 ipoff = ntohs(ip->ip_off);
396 iplen = ntohs(ip->ip_len);
397 if (ipoff & (IP_MF | IP_OFFMASK))
398 snprintf(SNPARGS(fragment, 0),
399 " (frag %d:%d@%d%s)",
400 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
402 (ipoff & IP_MF) ? "+" : "");
406 if (oif || m->m_pkthdr.rcvif)
407 log(LOG_SECURITY | LOG_INFO,
408 "ipfw: %d %s %s %s via %s%s\n",
410 action, proto, oif ? "out" : "in",
411 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
415 log(LOG_SECURITY | LOG_INFO,
416 "ipfw: %d %s %s [no if info]%s\n",
418 action, proto, fragment);
420 log(LOG_SECURITY | LOG_NOTICE,
421 "ipfw: limit %d reached on entry %d\n",
422 limit_reached, f ? f->rulenum : -1);