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 209845 2010-07-09 11:27:33Z glebius $");
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_output(struct ifnet *ifp, struct mbuf *m,
108 struct sockaddr *dst, struct route *ro)
116 ipfw_log_start(struct ifnet* ifp)
118 panic("ipfw_log_start() must not be called");
121 static const u_char ipfwbroadcastaddr[6] =
122 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
125 ipfw_log_bpf(int onoff)
132 ifp = if_alloc(IFT_ETHER);
135 if_initname(ifp, "ipfw", 0);
137 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
138 ifp->if_init = (void *)log_dummy;
139 ifp->if_ioctl = log_dummy;
140 ifp->if_start = ipfw_log_start;
141 ifp->if_output = ipfw_log_output;
145 ifp->if_broadcastaddr = ipfwbroadcastaddr;
146 ifp->if_baudrate = IF_Mbps(10);
147 bpfattach(ifp, DLT_EN10MB, 14);
151 ether_ifdetach(log_if);
157 #endif /* !WITHOUT_BPF */
160 * We enter here when we have a rule with O_LOG.
161 * XXX this function alone takes about 2Kbytes of code!
164 ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
165 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
169 int limit_reached = 0;
170 char action2[40], proto[128], fragment[32];
172 if (V_fw_verbose == 0) {
175 if (log_if == NULL || log_if->if_bpf == NULL)
178 if (args->eh) /* layer2, use orig hdr */
179 BPF_MTAP2(log_if, args->eh, ETHER_HDR_LEN, m);
181 /* Add fake header. Later we will store
182 * more info in the header.
184 BPF_MTAP2(log_if, "DDDDDDSSSSSS\x08\x00", ETHER_HDR_LEN, m);
185 #endif /* !WITHOUT_BPF */
188 /* the old 'log' function */
192 if (f == NULL) { /* bogus pkt */
193 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
196 if (V_norule_counter == V_verbose_limit)
197 limit_reached = V_verbose_limit;
199 } else { /* O_LOG is the first action, find the real one */
200 ipfw_insn *cmd = ACTION_PTR(f);
201 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
203 if (l->max_log != 0 && l->log_left == 0)
206 if (l->log_left == 0)
207 limit_reached = l->max_log;
208 cmd += F_LEN(cmd); /* point to first action */
209 if (cmd->opcode == O_ALTQ) {
210 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
212 snprintf(SNPARGS(action2, 0), "Altq %d",
216 if (cmd->opcode == O_PROB)
219 if (cmd->opcode == O_TAG)
223 switch (cmd->opcode) {
229 if (cmd->arg1==ICMP_REJECT_RST)
231 else if (cmd->arg1==ICMP_UNREACH_HOST)
234 snprintf(SNPARGS(action2, 0), "Unreach %d",
239 if (cmd->arg1==ICMP6_UNREACH_RST)
242 snprintf(SNPARGS(action2, 0), "Unreach %d",
253 snprintf(SNPARGS(action2, 0), "Divert %d",
257 snprintf(SNPARGS(action2, 0), "Tee %d",
261 snprintf(SNPARGS(action2, 0), "SetFib %d",
265 snprintf(SNPARGS(action2, 0), "SkipTo %d",
269 snprintf(SNPARGS(action2, 0), "Pipe %d",
273 snprintf(SNPARGS(action2, 0), "Queue %d",
277 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
279 struct in_addr dummyaddr;
280 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
281 dummyaddr.s_addr = htonl(tablearg);
283 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
285 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
286 inet_ntoa(dummyaddr));
289 snprintf(SNPARGS(action2, len), ":%d",
294 snprintf(SNPARGS(action2, 0), "Netgraph %d",
298 snprintf(SNPARGS(action2, 0), "Ngtee %d",
313 if (hlen == 0) { /* non-ip */
314 snprintf(SNPARGS(proto, 0), "MAC");
319 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
321 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
323 struct icmphdr *icmp;
327 struct ip6_hdr *ip6 = NULL;
328 struct icmp6_hdr *icmp6;
333 if (IS_IP6_FLOW_ID(&(args->f_id))) {
334 char ip6buf[INET6_ADDRSTRLEN];
335 snprintf(src, sizeof(src), "[%s]",
336 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
337 snprintf(dst, sizeof(dst), "[%s]",
338 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
340 ip6 = (struct ip6_hdr *)ip;
341 tcp = (struct tcphdr *)(((char *)ip) + hlen);
342 udp = (struct udphdr *)(((char *)ip) + hlen);
346 tcp = L3HDR(struct tcphdr, ip);
347 udp = L3HDR(struct udphdr, ip);
349 inet_ntoa_r(ip->ip_src, src);
350 inet_ntoa_r(ip->ip_dst, dst);
353 switch (args->f_id.proto) {
355 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
357 snprintf(SNPARGS(proto, len), ":%d %s:%d",
358 ntohs(tcp->th_sport),
360 ntohs(tcp->th_dport));
362 snprintf(SNPARGS(proto, len), " %s", dst);
366 len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
368 snprintf(SNPARGS(proto, len), ":%d %s:%d",
369 ntohs(udp->uh_sport),
371 ntohs(udp->uh_dport));
373 snprintf(SNPARGS(proto, len), " %s", dst);
377 icmp = L3HDR(struct icmphdr, ip);
379 len = snprintf(SNPARGS(proto, 0),
381 icmp->icmp_type, icmp->icmp_code);
383 len = snprintf(SNPARGS(proto, 0), "ICMP ");
384 len += snprintf(SNPARGS(proto, len), "%s", src);
385 snprintf(SNPARGS(proto, len), " %s", dst);
389 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
391 len = snprintf(SNPARGS(proto, 0),
393 icmp6->icmp6_type, icmp6->icmp6_code);
395 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
396 len += snprintf(SNPARGS(proto, len), "%s", src);
397 snprintf(SNPARGS(proto, len), " %s", dst);
401 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
402 args->f_id.proto, src);
403 snprintf(SNPARGS(proto, len), " %s", dst);
408 if (IS_IP6_FLOW_ID(&(args->f_id))) {
409 if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
410 snprintf(SNPARGS(fragment, 0),
411 " (frag %08x:%d@%d%s)",
413 ntohs(ip6->ip6_plen) - hlen,
414 ntohs(offset & IP6F_OFF_MASK) << 3,
415 (offset & IP6F_MORE_FRAG) ? "+" : "");
420 ipoff = ntohs(ip->ip_off);
421 iplen = ntohs(ip->ip_len);
422 if (ipoff & (IP_MF | IP_OFFMASK))
423 snprintf(SNPARGS(fragment, 0),
424 " (frag %d:%d@%d%s)",
425 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
427 (ipoff & IP_MF) ? "+" : "");
431 if (oif || m->m_pkthdr.rcvif)
432 log(LOG_SECURITY | LOG_INFO,
433 "ipfw: %d %s %s %s via %s%s\n",
435 action, proto, oif ? "out" : "in",
436 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
440 log(LOG_SECURITY | LOG_INFO,
441 "ipfw: %d %s %s [no if info]%s\n",
443 action, proto, fragment);
445 log(LOG_SECURITY | LOG_NOTICE,
446 "ipfw: limit %d reached on entry %d\n",
447 limit_reached, f ? f->rulenum : -1);