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>
48 #include <sys/rwlock.h>
49 #include <net/ethernet.h> /* for ETHERTYPE_IP */
51 #include <net/if_clone.h>
53 #include <net/if_types.h> /* for IFT_PFLOG */
54 #include <net/bpf.h> /* for BPF */
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_fw.h>
61 #include <netinet/tcp_var.h>
62 #include <netinet/udp.h>
64 #include <netinet/ip6.h>
65 #include <netinet/icmp6.h>
67 #include <netinet6/in6_var.h> /* ip6_sprintf() */
70 #include <netpfil/ipfw/ip_fw_private.h>
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 */
97 static struct rwlock log_if_lock;
98 #define LOGIF_LOCK_INIT(x) rw_init(&log_if_lock, "ipfw log_if lock")
99 #define LOGIF_LOCK_DESTROY(x) rw_destroy(&log_if_lock)
100 #define LOGIF_RLOCK(x) rw_rlock(&log_if_lock)
101 #define LOGIF_RUNLOCK(x) rw_runlock(&log_if_lock)
102 #define LOGIF_WLOCK(x) rw_wlock(&log_if_lock)
103 #define LOGIF_WUNLOCK(x) rw_wunlock(&log_if_lock)
105 static const char ipfwname[] = "ipfw";
107 /* we use this dummy function for all ifnet callbacks */
109 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
115 ipfw_log_output(struct ifnet *ifp, struct mbuf *m,
116 const struct sockaddr *dst, struct route *ro)
124 ipfw_log_start(struct ifnet* ifp)
126 panic("ipfw_log_start() must not be called");
129 static const u_char ipfwbroadcastaddr[6] =
130 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
133 ipfw_log_clone_match(struct if_clone *ifc, const char *name)
136 return (strncmp(name, ipfwname, sizeof(ipfwname) - 1) == 0);
140 ipfw_log_clone_create(struct if_clone *ifc, char *name, size_t len,
147 error = ifc_name2unit(name, &unit);
151 error = ifc_alloc_unit(ifc, &unit);
155 ifp = if_alloc(IFT_PFLOG);
157 ifc_free_unit(ifc, unit);
160 ifp->if_dname = ipfwname;
161 ifp->if_dunit = unit;
162 snprintf(ifp->if_xname, IFNAMSIZ, "%s%d", ipfwname, unit);
163 strlcpy(name, ifp->if_xname, len);
165 ifp->if_flags = IFF_UP | IFF_SIMPLEX | IFF_MULTICAST;
166 ifp->if_init = (void *)log_dummy;
167 ifp->if_ioctl = log_dummy;
168 ifp->if_start = ipfw_log_start;
169 ifp->if_output = ipfw_log_output;
172 ifp->if_broadcastaddr = ipfwbroadcastaddr;
173 ifp->if_baudrate = IF_Mbps(10);
181 ifc_free_unit(ifc, unit);
186 bpfattach(ifp, DLT_EN10MB, 14);
192 ipfw_log_clone_destroy(struct if_clone *ifc, struct ifnet *ifp)
200 if (log_if != NULL && ifp == log_if)
208 unit = ifp->if_dunit;
212 ifc_free_unit(ifc, unit);
217 static struct if_clone *ipfw_log_cloner;
220 ipfw_log_bpf(int onoff)
225 ipfw_log_cloner = if_clone_advanced(ipfwname, 0,
226 ipfw_log_clone_match, ipfw_log_clone_create,
227 ipfw_log_clone_destroy);
229 if_clone_detach(ipfw_log_cloner);
230 LOGIF_LOCK_DESTROY();
233 #endif /* !WITHOUT_BPF */
236 * We enter here when we have a rule with O_LOG.
237 * XXX this function alone takes about 2Kbytes of code!
240 ipfw_log(struct ip_fw *f, u_int hlen, struct ip_fw_args *args,
241 struct mbuf *m, struct ifnet *oif, u_short offset, uint32_t tablearg,
245 int limit_reached = 0;
246 char action2[92], proto[128], fragment[32];
248 if (V_fw_verbose == 0) {
251 if (log_if == NULL || log_if->if_bpf == NULL) {
256 if (args->eh) /* layer2, use orig hdr */
257 BPF_MTAP2(log_if, args->eh, ETHER_HDR_LEN, m);
259 /* Add fake header. Later we will store
260 * more info in the header.
263 BPF_MTAP2(log_if, "DDDDDDSSSSSS\x08\x00", ETHER_HDR_LEN, m);
264 else if (ip->ip_v == 6)
265 BPF_MTAP2(log_if, "DDDDDDSSSSSS\x86\xdd", ETHER_HDR_LEN, m);
267 /* Obviously bogus EtherType. */
268 BPF_MTAP2(log_if, "DDDDDDSSSSSS\xff\xff", ETHER_HDR_LEN, m);
271 #endif /* !WITHOUT_BPF */
274 /* the old 'log' function */
278 if (f == NULL) { /* bogus pkt */
279 if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
282 if (V_norule_counter == V_verbose_limit)
283 limit_reached = V_verbose_limit;
285 } else { /* O_LOG is the first action, find the real one */
286 ipfw_insn *cmd = ACTION_PTR(f);
287 ipfw_insn_log *l = (ipfw_insn_log *)cmd;
289 if (l->max_log != 0 && l->log_left == 0)
292 if (l->log_left == 0)
293 limit_reached = l->max_log;
294 cmd += F_LEN(cmd); /* point to first action */
295 if (cmd->opcode == O_ALTQ) {
296 ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
298 snprintf(SNPARGS(action2, 0), "Altq %d",
302 if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
303 cmd->opcode == O_SETDSCP)
307 switch (cmd->opcode) {
313 if (cmd->arg1==ICMP_REJECT_RST)
315 else if (cmd->arg1==ICMP_UNREACH_HOST)
318 snprintf(SNPARGS(action2, 0), "Unreach %d",
323 if (cmd->arg1==ICMP6_UNREACH_RST)
326 snprintf(SNPARGS(action2, 0), "Unreach %d",
337 snprintf(SNPARGS(action2, 0), "Divert %d",
341 snprintf(SNPARGS(action2, 0), "Tee %d",
345 snprintf(SNPARGS(action2, 0), "SetFib %d",
346 IP_FW_ARG_TABLEARG(cmd->arg1));
349 snprintf(SNPARGS(action2, 0), "SkipTo %d",
350 IP_FW_ARG_TABLEARG(cmd->arg1));
353 snprintf(SNPARGS(action2, 0), "Pipe %d",
354 IP_FW_ARG_TABLEARG(cmd->arg1));
357 snprintf(SNPARGS(action2, 0), "Queue %d",
358 IP_FW_ARG_TABLEARG(cmd->arg1));
361 ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
363 struct in_addr dummyaddr;
364 if (sa->sa.sin_addr.s_addr == INADDR_ANY)
365 dummyaddr.s_addr = htonl(tablearg);
367 dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
369 len = snprintf(SNPARGS(action2, 0), "Forward to %s",
370 inet_ntoa(dummyaddr));
373 snprintf(SNPARGS(action2, len), ":%d",
378 case O_FORWARD_IP6: {
379 char buf[INET6_ADDRSTRLEN];
380 ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
383 len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
384 ip6_sprintf(buf, &sa->sa.sin6_addr));
386 if (sa->sa.sin6_port)
387 snprintf(SNPARGS(action2, len), ":%u",
393 snprintf(SNPARGS(action2, 0), "Netgraph %d",
397 snprintf(SNPARGS(action2, 0), "Ngtee %d",
407 if (cmd->len & F_NOT)
410 snprintf(SNPARGS(action2, 0), "Call %d",
419 if (hlen == 0) { /* non-ip */
420 snprintf(SNPARGS(proto, 0), "MAC");
425 char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
427 char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
429 struct icmphdr *icmp;
433 struct ip6_hdr *ip6 = NULL;
434 struct icmp6_hdr *icmp6;
440 ip6f_mf = offset & IP6F_MORE_FRAG;
441 offset &= IP6F_OFF_MASK;
443 if (IS_IP6_FLOW_ID(&(args->f_id))) {
444 char ip6buf[INET6_ADDRSTRLEN];
445 snprintf(src, sizeof(src), "[%s]",
446 ip6_sprintf(ip6buf, &args->f_id.src_ip6));
447 snprintf(dst, sizeof(dst), "[%s]",
448 ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
450 ip6 = (struct ip6_hdr *)ip;
451 tcp = (struct tcphdr *)(((char *)ip) + hlen);
452 udp = (struct udphdr *)(((char *)ip) + hlen);
456 tcp = L3HDR(struct tcphdr, ip);
457 udp = L3HDR(struct udphdr, ip);
459 inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
460 inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
463 switch (args->f_id.proto) {
465 len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
467 snprintf(SNPARGS(proto, len), ":%d %s:%d",
468 ntohs(tcp->th_sport),
470 ntohs(tcp->th_dport));
472 snprintf(SNPARGS(proto, len), " %s", dst);
476 len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
478 snprintf(SNPARGS(proto, len), ":%d %s:%d",
479 ntohs(udp->uh_sport),
481 ntohs(udp->uh_dport));
483 snprintf(SNPARGS(proto, len), " %s", dst);
487 icmp = L3HDR(struct icmphdr, ip);
489 len = snprintf(SNPARGS(proto, 0),
491 icmp->icmp_type, icmp->icmp_code);
493 len = snprintf(SNPARGS(proto, 0), "ICMP ");
494 len += snprintf(SNPARGS(proto, len), "%s", src);
495 snprintf(SNPARGS(proto, len), " %s", dst);
499 icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
501 len = snprintf(SNPARGS(proto, 0),
503 icmp6->icmp6_type, icmp6->icmp6_code);
505 len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
506 len += snprintf(SNPARGS(proto, len), "%s", src);
507 snprintf(SNPARGS(proto, len), " %s", dst);
511 len = snprintf(SNPARGS(proto, 0), "P:%d %s",
512 args->f_id.proto, src);
513 snprintf(SNPARGS(proto, len), " %s", dst);
518 if (IS_IP6_FLOW_ID(&(args->f_id))) {
519 if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
520 snprintf(SNPARGS(fragment, 0),
521 " (frag %08x:%d@%d%s)",
523 ntohs(ip6->ip6_plen) - hlen,
524 ntohs(offset) << 3, ip6f_mf ? "+" : "");
529 ipoff = ntohs(ip->ip_off);
530 iplen = ntohs(ip->ip_len);
531 if (ipoff & (IP_MF | IP_OFFMASK))
532 snprintf(SNPARGS(fragment, 0),
533 " (frag %d:%d@%d%s)",
534 ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
536 (ipoff & IP_MF) ? "+" : "");
540 if (oif || m->m_pkthdr.rcvif)
541 log(LOG_SECURITY | LOG_INFO,
542 "ipfw: %d %s %s %s via %s%s\n",
544 action, proto, oif ? "out" : "in",
545 oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
549 log(LOG_SECURITY | LOG_INFO,
550 "ipfw: %d %s %s [no if info]%s\n",
552 action, proto, fragment);
554 log(LOG_SECURITY | LOG_NOTICE,
555 "ipfw: limit %d reached on entry %d\n",
556 limit_reached, f ? f->rulenum : -1);