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: head/sys/netinet/ipfw/ip_fw_log.c 200601 2009-12-16 10:48:40Z luigi $");
30 * Logging support for ipfw
33 #if !defined(KLD_MODULE)
35 #include "opt_ipdivert.h"
39 #error IPFIREWALL requires INET.
42 #include "opt_inet6.h"
43 #include "opt_ipsec.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
52 #include <net/ethernet.h> /* for ETHERTYPE_IP */
55 #include <net/if_types.h> /* for IFT_ETHER */
56 #include <net/bpf.h> /* for BPF */
58 #include <netinet/in.h>
59 #include <netinet/ip.h>
60 #include <netinet/ip_icmp.h>
61 #include <netinet/ip_fw.h>
62 #include <netinet/ipfw/ip_fw_private.h>
63 #include <netinet/tcp_var.h>
64 #include <netinet/udp.h>
66 #include <netinet/ip6.h>
67 #include <netinet/icmp6.h>
69 #include <netinet6/in6_var.h> /* ip6_sprintf() */
73 #include <security/mac/mac_framework.h>
77 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
78 * Other macros just cast void * into the appropriate type
80 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
81 #define TCP(p) ((struct tcphdr *)(p))
82 #define SCTP(p) ((struct sctphdr *)(p))
83 #define UDP(p) ((struct udphdr *)(p))
84 #define ICMP(p) ((struct icmphdr *)(p))
85 #define ICMP6(p) ((struct icmp6_hdr *)(p))
87 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
88 #define SNP(buf) buf, sizeof(buf)
92 ipfw_log_bpf(int onoff)
95 #else /* !WITHOUT_BPF */
96 static struct ifnet *log_if; /* hook to attach to bpf */
98 /* we use this dummy function for all ifnet callbacks */
100 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
106 ipfw_log_bpf(int onoff)
113 ifp = if_alloc(IFT_ETHER);
116 if_initname(ifp, "ipfw", 0);
118 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
119 ifp->if_init = (void *)log_dummy;
120 ifp->if_ioctl = log_dummy;
121 ifp->if_start = (void *)log_dummy;
122 ifp->if_output = (void *)log_dummy;
126 ifp->if_baudrate = IF_Mbps(10);
127 bpfattach(ifp, DLT_EN10MB, 14);
131 ether_ifdetach(log_if);
137 #endif /* !WITHOUT_BPF */
140 * We enter here when we have a rule with O_LOG.
141 * XXX this function alone takes about 2Kbytes of code!
144 ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
145 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
149 int limit_reached = 0;
150 char action2[40], proto[128], fragment[32];
152 if (V_fw_verbose == 0) {
156 if (log_if == NULL || log_if->if_bpf == NULL)
158 /* BPF treats the "mbuf" as read-only */
160 mh.mh_len = ETHER_HDR_LEN;
161 if (args->eh) { /* layer2, use orig hdr */
162 mh.mh_data = (char *)args->eh;
164 /* add fake header. Later we will store
165 * more info in the header
167 mh.mh_data = "DDDDDDSSSSSS\x08\x00";
169 BPF_MTAP(log_if, (struct mbuf *)&mh);
170 #endif /* !WITHOUT_BPF */
173 /* the old 'log' function */
177 if (f == NULL) { /* bogus pkt */
178 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
181 if (V_norule_counter == V_verbose_limit)
182 limit_reached = V_verbose_limit;
184 } else { /* O_LOG is the first action, find the real one */
185 ipfw_insn *cmd = ACTION_PTR(f);
186 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
188 if (l->max_log != 0 && l->log_left == 0)
191 if (l->log_left == 0)
192 limit_reached = l->max_log;
193 cmd += F_LEN(cmd); /* point to first action */
194 if (cmd->opcode == O_ALTQ) {
195 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
197 snprintf(SNPARGS(action2, 0), "Altq %d",
201 if (cmd->opcode == O_PROB)
204 if (cmd->opcode == O_TAG)
208 switch (cmd->opcode) {
214 if (cmd->arg1==ICMP_REJECT_RST)
216 else if (cmd->arg1==ICMP_UNREACH_HOST)
219 snprintf(SNPARGS(action2, 0), "Unreach %d",
224 if (cmd->arg1==ICMP6_UNREACH_RST)
227 snprintf(SNPARGS(action2, 0), "Unreach %d",
238 snprintf(SNPARGS(action2, 0), "Divert %d",
242 snprintf(SNPARGS(action2, 0), "Tee %d",
246 snprintf(SNPARGS(action2, 0), "SetFib %d",
250 snprintf(SNPARGS(action2, 0), "SkipTo %d",
254 snprintf(SNPARGS(action2, 0), "Pipe %d",
258 snprintf(SNPARGS(action2, 0), "Queue %d",
262 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
264 struct in_addr dummyaddr;
265 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
266 dummyaddr.s_addr = htonl(tablearg);
268 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
270 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
271 inet_ntoa(dummyaddr));
274 snprintf(SNPARGS(action2, len), ":%d",
279 snprintf(SNPARGS(action2, 0), "Netgraph %d",
283 snprintf(SNPARGS(action2, 0), "Ngtee %d",
298 if (hlen == 0) { /* non-ip */
299 snprintf(SNPARGS(proto, 0), "MAC");
304 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
306 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
308 struct icmphdr *icmp;
312 struct ip6_hdr *ip6 = NULL;
313 struct icmp6_hdr *icmp6;
318 if (IS_IP6_FLOW_ID(&(args->f_id))) {
319 char ip6buf[INET6_ADDRSTRLEN];
320 snprintf(src, sizeof(src), "[%s]",
321 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
322 snprintf(dst, sizeof(dst), "[%s]",
323 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
325 ip6 = (struct ip6_hdr *)ip;
326 tcp = (struct tcphdr *)(((char *)ip) + hlen);
327 udp = (struct udphdr *)(((char *)ip) + hlen);
331 tcp = L3HDR(struct tcphdr, ip);
332 udp = L3HDR(struct udphdr, ip);
334 inet_ntoa_r(ip->ip_src, src);
335 inet_ntoa_r(ip->ip_dst, dst);
338 switch (args->f_id.proto) {
340 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
342 snprintf(SNPARGS(proto, len), ":%d %s:%d",
343 ntohs(tcp->th_sport),
345 ntohs(tcp->th_dport));
347 snprintf(SNPARGS(proto, len), " %s", dst);
351 len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
353 snprintf(SNPARGS(proto, len), ":%d %s:%d",
354 ntohs(udp->uh_sport),
356 ntohs(udp->uh_dport));
358 snprintf(SNPARGS(proto, len), " %s", dst);
362 icmp = L3HDR(struct icmphdr, ip);
364 len = snprintf(SNPARGS(proto, 0),
366 icmp->icmp_type, icmp->icmp_code);
368 len = snprintf(SNPARGS(proto, 0), "ICMP ");
369 len += snprintf(SNPARGS(proto, len), "%s", src);
370 snprintf(SNPARGS(proto, len), " %s", dst);
374 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
376 len = snprintf(SNPARGS(proto, 0),
378 icmp6->icmp6_type, icmp6->icmp6_code);
380 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
381 len += snprintf(SNPARGS(proto, len), "%s", src);
382 snprintf(SNPARGS(proto, len), " %s", dst);
386 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
387 args->f_id.proto, src);
388 snprintf(SNPARGS(proto, len), " %s", dst);
393 if (IS_IP6_FLOW_ID(&(args->f_id))) {
394 if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
395 snprintf(SNPARGS(fragment, 0),
396 " (frag %08x:%d@%d%s)",
398 ntohs(ip6->ip6_plen) - hlen,
399 ntohs(offset & IP6F_OFF_MASK) << 3,
400 (offset & IP6F_MORE_FRAG) ? "+" : "");
405 ipoff = ntohs(ip->ip_off);
406 iplen = ntohs(ip->ip_len);
407 if (ipoff & (IP_MF | IP_OFFMASK))
408 snprintf(SNPARGS(fragment, 0),
409 " (frag %d:%d@%d%s)",
410 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
412 (ipoff & IP_MF) ? "+" : "");
416 if (oif || m->m_pkthdr.rcvif)
417 log(LOG_SECURITY | LOG_INFO,
418 "ipfw: %d %s %s %s via %s%s\n",
420 action, proto, oif ? "out" : "in",
421 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
425 log(LOG_SECURITY | LOG_INFO,
426 "ipfw: %d %s %s [no if info]%s\n",
428 action, proto, fragment);
430 log(LOG_SECURITY | LOG_NOTICE,
431 "ipfw: limit %d reached on entry %d\n",
432 limit_reached, f ? f->rulenum : -1);