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>
43 #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 */
50 #include <net/if_types.h> /* for IFT_ETHER */
51 #include <net/bpf.h> /* for BPF */
53 #include <netinet/in.h>
54 #include <netinet/ip.h>
55 #include <netinet/ip_icmp.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/ip_fw.h>
58 #include <netinet/tcp_var.h>
59 #include <netinet/udp.h>
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
64 #include <netinet6/in6_var.h> /* ip6_sprintf() */
67 #include <netpfil/ipfw/ip_fw_private.h>
70 #include <security/mac/mac_framework.h>
74 * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
75 * Other macros just cast void * into the appropriate type
77 #define L3HDR(T, ip) ((T *)((u_int32_t *)(ip) + (ip)->ip_hl))
78 #define TCP(p) ((struct tcphdr *)(p))
79 #define SCTP(p) ((struct sctphdr *)(p))
80 #define UDP(p) ((struct udphdr *)(p))
81 #define ICMP(p) ((struct icmphdr *)(p))
82 #define ICMP6(p) ((struct icmp6_hdr *)(p))
84 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
85 #define SNP(buf) buf, sizeof(buf)
89 ipfw_log_bpf(int onoff)
92 #else /* !WITHOUT_BPF */
93 static struct ifnet *log_if; /* hook to attach to bpf */
95 /* we use this dummy function for all ifnet callbacks */
97 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
103 ipfw_log_output(struct ifnet *ifp, struct mbuf *m,
104 struct sockaddr *dst, struct route *ro)
112 ipfw_log_start(struct ifnet* ifp)
114 panic("ipfw_log_start() must not be called");
117 static const u_char ipfwbroadcastaddr[6] =
118 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
121 ipfw_log_bpf(int onoff)
128 ifp = if_alloc(IFT_ETHER);
131 if_initname(ifp, "ipfw", 0);
133 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
134 ifp->if_init = (void *)log_dummy;
135 ifp->if_ioctl = log_dummy;
136 ifp->if_start = ipfw_log_start;
137 ifp->if_output = ipfw_log_output;
141 ifp->if_broadcastaddr = ipfwbroadcastaddr;
142 ifp->if_baudrate = IF_Mbps(10);
143 bpfattach(ifp, DLT_EN10MB, 14);
147 ether_ifdetach(log_if);
153 #endif /* !WITHOUT_BPF */
156 * We enter here when we have a rule with O_LOG.
157 * XXX this function alone takes about 2Kbytes of code!
160 ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
161 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
165 int limit_reached = 0;
166 char action2[92], proto[128], fragment[32];
168 if (V_fw_verbose == 0) {
171 if (log_if == NULL || log_if->if_bpf == NULL)
174 if (args->eh) /* layer2, use orig hdr */
175 BPF_MTAP2(log_if, args->eh, ETHER_HDR_LEN, m);
177 /* Add fake header. Later we will store
178 * more info in the header.
180 BPF_MTAP2(log_if, "DDDDDDSSSSSS\x08\x00", ETHER_HDR_LEN, m);
181 #endif /* !WITHOUT_BPF */
184 /* the old 'log' function */
188 if (f == NULL) { /* bogus pkt */
189 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
192 if (V_norule_counter == V_verbose_limit)
193 limit_reached = V_verbose_limit;
195 } else { /* O_LOG is the first action, find the real one */
196 ipfw_insn *cmd = ACTION_PTR(f);
197 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
199 if (l->max_log != 0 && l->log_left == 0)
202 if (l->log_left == 0)
203 limit_reached = l->max_log;
204 cmd += F_LEN(cmd); /* point to first action */
205 if (cmd->opcode == O_ALTQ) {
206 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
208 snprintf(SNPARGS(action2, 0), "Altq %d",
212 if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
213 cmd->opcode == O_SETDSCP)
217 switch (cmd->opcode) {
223 if (cmd->arg1==ICMP_REJECT_RST)
225 else if (cmd->arg1==ICMP_UNREACH_HOST)
228 snprintf(SNPARGS(action2, 0), "Unreach %d",
233 if (cmd->arg1==ICMP6_UNREACH_RST)
236 snprintf(SNPARGS(action2, 0), "Unreach %d",
247 snprintf(SNPARGS(action2, 0), "Divert %d",
251 snprintf(SNPARGS(action2, 0), "Tee %d",
255 snprintf(SNPARGS(action2, 0), "SetFib %d",
256 IP_FW_ARG_TABLEARG(cmd->arg1));
259 snprintf(SNPARGS(action2, 0), "SkipTo %d",
260 IP_FW_ARG_TABLEARG(cmd->arg1));
263 snprintf(SNPARGS(action2, 0), "Pipe %d",
264 IP_FW_ARG_TABLEARG(cmd->arg1));
267 snprintf(SNPARGS(action2, 0), "Queue %d",
268 IP_FW_ARG_TABLEARG(cmd->arg1));
271 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
273 struct in_addr dummyaddr;
274 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
275 dummyaddr.s_addr = htonl(tablearg);
277 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
279 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
280 inet_ntoa(dummyaddr));
283 snprintf(SNPARGS(action2, len), ":%d",
288 case O_FORWARD_IP6: {
289 char buf[INET6_ADDRSTRLEN];
290 ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
293 len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
294 ip6_sprintf(buf, &sa->sa.sin6_addr));
296 if (sa->sa.sin6_port)
297 snprintf(SNPARGS(action2, len), ":%u",
303 snprintf(SNPARGS(action2, 0), "Netgraph %d",
307 snprintf(SNPARGS(action2, 0), "Ngtee %d",
317 if (cmd->len & F_NOT)
320 snprintf(SNPARGS(action2, 0), "Call %d",
329 if (hlen == 0) { /* non-ip */
330 snprintf(SNPARGS(proto, 0), "MAC");
335 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
337 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
339 struct icmphdr *icmp;
343 struct ip6_hdr *ip6 = NULL;
344 struct icmp6_hdr *icmp6;
350 ip6f_mf = offset & IP6F_MORE_FRAG;
351 offset &= IP6F_OFF_MASK;
353 if (IS_IP6_FLOW_ID(&(args->f_id))) {
354 char ip6buf[INET6_ADDRSTRLEN];
355 snprintf(src, sizeof(src), "[%s]",
356 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
357 snprintf(dst, sizeof(dst), "[%s]",
358 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
360 ip6 = (struct ip6_hdr *)ip;
361 tcp = (struct tcphdr *)(((char *)ip) + hlen);
362 udp = (struct udphdr *)(((char *)ip) + hlen);
366 tcp = L3HDR(struct tcphdr, ip);
367 udp = L3HDR(struct udphdr, ip);
369 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
370 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
373 switch (args->f_id.proto) {
375 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
377 snprintf(SNPARGS(proto, len), ":%d %s:%d",
378 ntohs(tcp->th_sport),
380 ntohs(tcp->th_dport));
382 snprintf(SNPARGS(proto, len), " %s", dst);
386 len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
388 snprintf(SNPARGS(proto, len), ":%d %s:%d",
389 ntohs(udp->uh_sport),
391 ntohs(udp->uh_dport));
393 snprintf(SNPARGS(proto, len), " %s", dst);
397 icmp = L3HDR(struct icmphdr, ip);
399 len = snprintf(SNPARGS(proto, 0),
401 icmp->icmp_type, icmp->icmp_code);
403 len = snprintf(SNPARGS(proto, 0), "ICMP ");
404 len += snprintf(SNPARGS(proto, len), "%s", src);
405 snprintf(SNPARGS(proto, len), " %s", dst);
409 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
411 len = snprintf(SNPARGS(proto, 0),
413 icmp6->icmp6_type, icmp6->icmp6_code);
415 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
416 len += snprintf(SNPARGS(proto, len), "%s", src);
417 snprintf(SNPARGS(proto, len), " %s", dst);
421 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
422 args->f_id.proto, src);
423 snprintf(SNPARGS(proto, len), " %s", dst);
428 if (IS_IP6_FLOW_ID(&(args->f_id))) {
429 if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
430 snprintf(SNPARGS(fragment, 0),
431 " (frag %08x:%d@%d%s)",
433 ntohs(ip6->ip6_plen) - hlen,
434 ntohs(offset) << 3, ip6f_mf ? "+" : "");
439 ipoff = ntohs(ip->ip_off);
440 iplen = ntohs(ip->ip_len);
441 if (ipoff & (IP_MF | IP_OFFMASK))
442 snprintf(SNPARGS(fragment, 0),
443 " (frag %d:%d@%d%s)",
444 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
446 (ipoff & IP_MF) ? "+" : "");
450 if (oif || m->m_pkthdr.rcvif)
451 log(LOG_SECURITY | LOG_INFO,
452 "ipfw: %d %s %s %s via %s%s\n",
454 action, proto, oif ? "out" : "in",
455 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
459 log(LOG_SECURITY | LOG_INFO,
460 "ipfw: %d %s %s [no if info]%s\n",
462 action, proto, fragment);
464 log(LOG_SECURITY | LOG_NOTICE,
465 "ipfw: limit %d reached on entry %d\n",
466 limit_reached, f ? f->rulenum : -1);