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_var.h>
62 #include <netinet/ip_fw.h>
63 #include <netinet/ipfw/ip_fw_private.h>
64 #include <netinet/tcp_var.h>
65 #include <netinet/udp.h>
67 #include <netinet/ip6.h>
68 #include <netinet/icmp6.h>
70 #include <netinet6/in6_var.h> /* ip6_sprintf() */
74 #include <security/mac/mac_framework.h>
78 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
79 * Other macros just cast void * into the appropriate type
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))
88 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
89 #define SNP(buf) buf, sizeof(buf)
93 ipfw_log_bpf(int onoff)
96 #else /* !WITHOUT_BPF */
97 static struct ifnet *log_if; /* hook to attach to bpf */
99 /* we use this dummy function for all ifnet callbacks */
101 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
107 ipfw_log_bpf(int onoff)
114 ifp = if_alloc(IFT_ETHER);
117 if_initname(ifp, "ipfw", 0);
119 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
120 ifp->if_init = (void *)log_dummy;
121 ifp->if_ioctl = log_dummy;
122 ifp->if_start = (void *)log_dummy;
123 ifp->if_output = (void *)log_dummy;
127 ifp->if_baudrate = IF_Mbps(10);
128 bpfattach(ifp, DLT_EN10MB, 14);
132 ether_ifdetach(log_if);
138 #endif /* !WITHOUT_BPF */
141 * We enter here when we have a rule with O_LOG.
142 * XXX this function alone takes about 2Kbytes of code!
145 ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
146 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
150 int limit_reached = 0;
151 char action2[40], proto[128], fragment[32];
153 if (V_fw_verbose == 0) {
157 if (log_if == NULL || log_if->if_bpf == NULL)
159 /* BPF treats the "mbuf" as read-only */
161 mh.mh_len = ETHER_HDR_LEN;
162 if (args->eh) { /* layer2, use orig hdr */
163 mh.mh_data = (char *)args->eh;
165 /* add fake header. Later we will store
166 * more info in the header
168 mh.mh_data = "DDDDDDSSSSSS\x08\x00";
170 BPF_MTAP(log_if, (struct mbuf *)&mh);
171 #endif /* !WITHOUT_BPF */
174 /* the old 'log' function */
178 if (f == NULL) { /* bogus pkt */
179 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
182 if (V_norule_counter == V_verbose_limit)
183 limit_reached = V_verbose_limit;
185 } else { /* O_LOG is the first action, find the real one */
186 ipfw_insn *cmd = ACTION_PTR(f);
187 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
189 if (l->max_log != 0 && l->log_left == 0)
192 if (l->log_left == 0)
193 limit_reached = l->max_log;
194 cmd += F_LEN(cmd); /* point to first action */
195 if (cmd->opcode == O_ALTQ) {
196 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
198 snprintf(SNPARGS(action2, 0), "Altq %d",
202 if (cmd->opcode == O_PROB)
205 if (cmd->opcode == O_TAG)
209 switch (cmd->opcode) {
215 if (cmd->arg1==ICMP_REJECT_RST)
217 else if (cmd->arg1==ICMP_UNREACH_HOST)
220 snprintf(SNPARGS(action2, 0), "Unreach %d",
225 if (cmd->arg1==ICMP6_UNREACH_RST)
228 snprintf(SNPARGS(action2, 0), "Unreach %d",
239 snprintf(SNPARGS(action2, 0), "Divert %d",
243 snprintf(SNPARGS(action2, 0), "Tee %d",
247 snprintf(SNPARGS(action2, 0), "SetFib %d",
251 snprintf(SNPARGS(action2, 0), "SkipTo %d",
255 snprintf(SNPARGS(action2, 0), "Pipe %d",
259 snprintf(SNPARGS(action2, 0), "Queue %d",
263 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
265 struct in_addr dummyaddr;
266 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
267 dummyaddr.s_addr = htonl(tablearg);
269 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
271 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
272 inet_ntoa(dummyaddr));
275 snprintf(SNPARGS(action2, len), ":%d",
280 snprintf(SNPARGS(action2, 0), "Netgraph %d",
284 snprintf(SNPARGS(action2, 0), "Ngtee %d",
299 if (hlen == 0) { /* non-ip */
300 snprintf(SNPARGS(proto, 0), "MAC");
305 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
307 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
309 struct icmphdr *icmp;
313 struct ip6_hdr *ip6 = NULL;
314 struct icmp6_hdr *icmp6;
319 if (IS_IP6_FLOW_ID(&(args->f_id))) {
320 char ip6buf[INET6_ADDRSTRLEN];
321 snprintf(src, sizeof(src), "[%s]",
322 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
323 snprintf(dst, sizeof(dst), "[%s]",
324 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
326 ip6 = (struct ip6_hdr *)ip;
327 tcp = (struct tcphdr *)(((char *)ip) + hlen);
328 udp = (struct udphdr *)(((char *)ip) + hlen);
332 tcp = L3HDR(struct tcphdr, ip);
333 udp = L3HDR(struct udphdr, ip);
335 inet_ntoa_r(ip->ip_src, src);
336 inet_ntoa_r(ip->ip_dst, dst);
339 switch (args->f_id.proto) {
341 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
343 snprintf(SNPARGS(proto, len), ":%d %s:%d",
344 ntohs(tcp->th_sport),
346 ntohs(tcp->th_dport));
348 snprintf(SNPARGS(proto, len), " %s", dst);
352 len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
354 snprintf(SNPARGS(proto, len), ":%d %s:%d",
355 ntohs(udp->uh_sport),
357 ntohs(udp->uh_dport));
359 snprintf(SNPARGS(proto, len), " %s", dst);
363 icmp = L3HDR(struct icmphdr, ip);
365 len = snprintf(SNPARGS(proto, 0),
367 icmp->icmp_type, icmp->icmp_code);
369 len = snprintf(SNPARGS(proto, 0), "ICMP ");
370 len += snprintf(SNPARGS(proto, len), "%s", src);
371 snprintf(SNPARGS(proto, len), " %s", dst);
375 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
377 len = snprintf(SNPARGS(proto, 0),
379 icmp6->icmp6_type, icmp6->icmp6_code);
381 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
382 len += snprintf(SNPARGS(proto, len), "%s", src);
383 snprintf(SNPARGS(proto, len), " %s", dst);
387 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
388 args->f_id.proto, src);
389 snprintf(SNPARGS(proto, len), " %s", dst);
394 if (IS_IP6_FLOW_ID(&(args->f_id))) {
395 if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
396 snprintf(SNPARGS(fragment, 0),
397 " (frag %08x:%d@%d%s)",
399 ntohs(ip6->ip6_plen) - hlen,
400 ntohs(offset & IP6F_OFF_MASK) << 3,
401 (offset & IP6F_MORE_FRAG) ? "+" : "");
406 ipoff = ntohs(ip->ip_off);
407 iplen = ntohs(ip->ip_len);
408 if (ipoff & (IP_MF | IP_OFFMASK))
409 snprintf(SNPARGS(fragment, 0),
410 " (frag %d:%d@%d%s)",
411 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
413 (ipoff & IP_MF) ? "+" : "");
417 if (oif || m->m_pkthdr.rcvif)
418 log(LOG_SECURITY | LOG_INFO,
419 "ipfw: %d %s %s %s via %s%s\n",
421 action, proto, oif ? "out" : "in",
422 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
426 log(LOG_SECURITY | LOG_INFO,
427 "ipfw: %d %s %s [no if info]%s\n",
429 action, proto, fragment);
431 log(LOG_SECURITY | LOG_NOTICE,
432 "ipfw: limit %d reached on entry %d\n",
433 limit_reached, f ? f->rulenum : -1);