1 /*-
2  * Copyright (c) 2002-2009 Luigi Rizzo, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: stable/9/sys/netpfil/ipfw/ip_fw_log.c 255987 2013-10-02 12:35:03Z philip $");
28 
29 /*
30  * Logging support for ipfw
31  */
32 
33 #include "opt_ipfw.h"
34 #include "opt_inet.h"
35 #ifndef INET
36 #error IPFIREWALL requires INET.
37 #endif /* INET */
38 #include "opt_inet6.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.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 */
48 #include <net/if.h>
49 #include <net/vnet.h>
50 #include <net/if_types.h>	/* for IFT_ETHER */
51 #include <net/bpf.h>		/* for BPF */
52 
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>
60 
61 #include <netinet/ip6.h>
62 #include <netinet/icmp6.h>
63 #ifdef INET6
64 #include <netinet6/in6_var.h>	/* ip6_sprintf() */
65 #endif
66 
67 #include <netpfil/ipfw/ip_fw_private.h>
68 
69 #ifdef MAC
70 #include <security/mac/mac_framework.h>
71 #endif
72 
73 /*
74  * L3HDR maps an ipv4 pointer into a layer3 header pointer of type T
75  * Other macros just cast void * into the appropriate type
76  */
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))
83 
84 #define SNPARGS(buf, len) buf + len, sizeof(buf) > len ? sizeof(buf) - len : 0
85 #define SNP(buf) buf, sizeof(buf)
86 
87 #ifdef WITHOUT_BPF
88 void
ipfw_log_bpf(int onoff)89 ipfw_log_bpf(int onoff)
90 {
91 }
92 #else /* !WITHOUT_BPF */
93 static struct ifnet *log_if;	/* hook to attach to bpf */
94 
95 /* we use this dummy function for all ifnet callbacks */
96 static int
log_dummy(struct ifnet * ifp,u_long cmd,caddr_t addr)97 log_dummy(struct ifnet *ifp, u_long cmd, caddr_t addr)
98 {
99 	return EINVAL;
100 }
101 
102 static int
ipfw_log_output(struct ifnet * ifp,struct mbuf * m,struct sockaddr * dst,struct route * ro)103 ipfw_log_output(struct ifnet *ifp, struct mbuf *m,
104 	struct sockaddr *dst, struct route *ro)
105 {
106 	if (m != NULL)
107 		m_freem(m);
108 	return EINVAL;
109 }
110 
111 static void
ipfw_log_start(struct ifnet * ifp)112 ipfw_log_start(struct ifnet* ifp)
113 {
114 	panic("ipfw_log_start() must not be called");
115 }
116 
117 static const u_char ipfwbroadcastaddr[6] =
118 	{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
119 
120 void
ipfw_log_bpf(int onoff)121 ipfw_log_bpf(int onoff)
122 {
123 	struct ifnet *ifp;
124 
125 	if (onoff) {
126 		if (log_if)
127 			return;
128 		ifp = if_alloc(IFT_ETHER);
129 		if (ifp == NULL)
130 			return;
131 		if_initname(ifp, "ipfw", 0);
132 		ifp->if_mtu = 65536;
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;
138 		ifp->if_addrlen = 6;
139 		ifp->if_hdrlen = 14;
140 		if_attach(ifp);
141 		ifp->if_broadcastaddr = ipfwbroadcastaddr;
142 		ifp->if_baudrate = IF_Mbps(10);
143 		bpfattach(ifp, DLT_EN10MB, 14);
144 		log_if = ifp;
145 	} else {
146 		if (log_if) {
147 			ether_ifdetach(log_if);
148 			if_free(log_if);
149 		}
150 		log_if = NULL;
151 	}
152 }
153 #endif /* !WITHOUT_BPF */
154 
155 /*
156  * We enter here when we have a rule with O_LOG.
157  * XXX this function alone takes about 2Kbytes of code!
158  */
159 void
ipfw_log(struct ip_fw * f,u_int hlen,struct ip_fw_args * args,struct mbuf * m,struct ifnet * oif,u_short offset,uint32_t tablearg,struct ip * ip)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,
162     struct ip *ip)
163 {
164 	char *action;
165 	int limit_reached = 0;
166 	char action2[92], proto[128], fragment[32];
167 
168 	if (V_fw_verbose == 0) {
169 #ifndef WITHOUT_BPF
170 
171 		if (log_if == NULL || log_if->if_bpf == NULL)
172 			return;
173 
174 		if (args->eh) /* layer2, use orig hdr */
175 			BPF_MTAP2(log_if, args->eh, ETHER_HDR_LEN, m);
176 		else {
177 			/* Add fake header. Later we will store
178 			 * more info in the header.
179 			 */
180 			if (ip->ip_v == 4)
181 				BPF_MTAP2(log_if, "DDDDDDSSSSSS\x08\x00", ETHER_HDR_LEN, m);
182 			else if  (ip->ip_v == 6)
183 				BPF_MTAP2(log_if, "DDDDDDSSSSSS\x86\xdd", ETHER_HDR_LEN, m);
184 			else
185 				/* Obviously bogus EtherType. */
186 				BPF_MTAP2(log_if, "DDDDDDSSSSSS\xff\xff", ETHER_HDR_LEN, m);
187 		}
188 #endif /* !WITHOUT_BPF */
189 		return;
190 	}
191 	/* the old 'log' function */
192 	fragment[0] = '\0';
193 	proto[0] = '\0';
194 
195 	if (f == NULL) {	/* bogus pkt */
196 		if (V_verbose_limit != 0 && V_norule_counter >= V_verbose_limit)
197 			return;
198 		V_norule_counter++;
199 		if (V_norule_counter == V_verbose_limit)
200 			limit_reached = V_verbose_limit;
201 		action = "Refuse";
202 	} else {	/* O_LOG is the first action, find the real one */
203 		ipfw_insn *cmd = ACTION_PTR(f);
204 		ipfw_insn_log *l = (ipfw_insn_log *)cmd;
205 
206 		if (l->max_log != 0 && l->log_left == 0)
207 			return;
208 		l->log_left--;
209 		if (l->log_left == 0)
210 			limit_reached = l->max_log;
211 		cmd += F_LEN(cmd);	/* point to first action */
212 		if (cmd->opcode == O_ALTQ) {
213 			ipfw_insn_altq *altq = (ipfw_insn_altq *)cmd;
214 
215 			snprintf(SNPARGS(action2, 0), "Altq %d",
216 				altq->qid);
217 			cmd += F_LEN(cmd);
218 		}
219 		if (cmd->opcode == O_PROB || cmd->opcode == O_TAG ||
220 		    cmd->opcode == O_SETDSCP)
221 			cmd += F_LEN(cmd);
222 
223 		action = action2;
224 		switch (cmd->opcode) {
225 		case O_DENY:
226 			action = "Deny";
227 			break;
228 
229 		case O_REJECT:
230 			if (cmd->arg1==ICMP_REJECT_RST)
231 				action = "Reset";
232 			else if (cmd->arg1==ICMP_UNREACH_HOST)
233 				action = "Reject";
234 			else
235 				snprintf(SNPARGS(action2, 0), "Unreach %d",
236 					cmd->arg1);
237 			break;
238 
239 		case O_UNREACH6:
240 			if (cmd->arg1==ICMP6_UNREACH_RST)
241 				action = "Reset";
242 			else
243 				snprintf(SNPARGS(action2, 0), "Unreach %d",
244 					cmd->arg1);
245 			break;
246 
247 		case O_ACCEPT:
248 			action = "Accept";
249 			break;
250 		case O_COUNT:
251 			action = "Count";
252 			break;
253 		case O_DIVERT:
254 			snprintf(SNPARGS(action2, 0), "Divert %d",
255 				cmd->arg1);
256 			break;
257 		case O_TEE:
258 			snprintf(SNPARGS(action2, 0), "Tee %d",
259 				cmd->arg1);
260 			break;
261 		case O_SETFIB:
262 			snprintf(SNPARGS(action2, 0), "SetFib %d",
263 				IP_FW_ARG_TABLEARG(cmd->arg1));
264 			break;
265 		case O_SKIPTO:
266 			snprintf(SNPARGS(action2, 0), "SkipTo %d",
267 				IP_FW_ARG_TABLEARG(cmd->arg1));
268 			break;
269 		case O_PIPE:
270 			snprintf(SNPARGS(action2, 0), "Pipe %d",
271 				IP_FW_ARG_TABLEARG(cmd->arg1));
272 			break;
273 		case O_QUEUE:
274 			snprintf(SNPARGS(action2, 0), "Queue %d",
275 				IP_FW_ARG_TABLEARG(cmd->arg1));
276 			break;
277 		case O_FORWARD_IP: {
278 			ipfw_insn_sa *sa = (ipfw_insn_sa *)cmd;
279 			int len;
280 			struct in_addr dummyaddr;
281 			if (sa->sa.sin_addr.s_addr == INADDR_ANY)
282 				dummyaddr.s_addr = htonl(tablearg);
283 			else
284 				dummyaddr.s_addr = sa->sa.sin_addr.s_addr;
285 
286 			len = snprintf(SNPARGS(action2, 0), "Forward to %s",
287 				inet_ntoa(dummyaddr));
288 
289 			if (sa->sa.sin_port)
290 				snprintf(SNPARGS(action2, len), ":%d",
291 				    sa->sa.sin_port);
292 			}
293 			break;
294 #ifdef INET6
295 		case O_FORWARD_IP6: {
296 			char buf[INET6_ADDRSTRLEN];
297 			ipfw_insn_sa6 *sa = (ipfw_insn_sa6 *)cmd;
298 			int len;
299 
300 			len = snprintf(SNPARGS(action2, 0), "Forward to [%s]",
301 			    ip6_sprintf(buf, &sa->sa.sin6_addr));
302 
303 			if (sa->sa.sin6_port)
304 				snprintf(SNPARGS(action2, len), ":%u",
305 				    sa->sa.sin6_port);
306 			}
307 			break;
308 #endif
309 		case O_NETGRAPH:
310 			snprintf(SNPARGS(action2, 0), "Netgraph %d",
311 				cmd->arg1);
312 			break;
313 		case O_NGTEE:
314 			snprintf(SNPARGS(action2, 0), "Ngtee %d",
315 				cmd->arg1);
316 			break;
317 		case O_NAT:
318 			action = "Nat";
319  			break;
320 		case O_REASS:
321 			action = "Reass";
322 			break;
323 		case O_CALLRETURN:
324 			if (cmd->len & F_NOT)
325 				action = "Return";
326 			else
327 				snprintf(SNPARGS(action2, 0), "Call %d",
328 				    cmd->arg1);
329 			break;
330 		default:
331 			action = "UNKNOWN";
332 			break;
333 		}
334 	}
335 
336 	if (hlen == 0) {	/* non-ip */
337 		snprintf(SNPARGS(proto, 0), "MAC");
338 
339 	} else {
340 		int len;
341 #ifdef INET6
342 		char src[INET6_ADDRSTRLEN + 2], dst[INET6_ADDRSTRLEN + 2];
343 #else
344 		char src[INET_ADDRSTRLEN], dst[INET_ADDRSTRLEN];
345 #endif
346 		struct icmphdr *icmp;
347 		struct tcphdr *tcp;
348 		struct udphdr *udp;
349 #ifdef INET6
350 		struct ip6_hdr *ip6 = NULL;
351 		struct icmp6_hdr *icmp6;
352 		u_short ip6f_mf;
353 #endif
354 		src[0] = '\0';
355 		dst[0] = '\0';
356 #ifdef INET6
357 		ip6f_mf = offset & IP6F_MORE_FRAG;
358 		offset &= IP6F_OFF_MASK;
359 
360 		if (IS_IP6_FLOW_ID(&(args->f_id))) {
361 			char ip6buf[INET6_ADDRSTRLEN];
362 			snprintf(src, sizeof(src), "[%s]",
363 			    ip6_sprintf(ip6buf, &args->f_id.src_ip6));
364 			snprintf(dst, sizeof(dst), "[%s]",
365 			    ip6_sprintf(ip6buf, &args->f_id.dst_ip6));
366 
367 			ip6 = (struct ip6_hdr *)ip;
368 			tcp = (struct tcphdr *)(((char *)ip) + hlen);
369 			udp = (struct udphdr *)(((char *)ip) + hlen);
370 		} else
371 #endif
372 		{
373 			tcp = L3HDR(struct tcphdr, ip);
374 			udp = L3HDR(struct udphdr, ip);
375 
376 			inet_ntop(AF_INET, &ip->ip_src, src, sizeof(src));
377 			inet_ntop(AF_INET, &ip->ip_dst, dst, sizeof(dst));
378 		}
379 
380 		switch (args->f_id.proto) {
381 		case IPPROTO_TCP:
382 			len = snprintf(SNPARGS(proto, 0), "TCP %s", src);
383 			if (offset == 0)
384 				snprintf(SNPARGS(proto, len), ":%d %s:%d",
385 				    ntohs(tcp->th_sport),
386 				    dst,
387 				    ntohs(tcp->th_dport));
388 			else
389 				snprintf(SNPARGS(proto, len), " %s", dst);
390 			break;
391 
392 		case IPPROTO_UDP:
393 			len = snprintf(SNPARGS(proto, 0), "UDP %s", src);
394 			if (offset == 0)
395 				snprintf(SNPARGS(proto, len), ":%d %s:%d",
396 				    ntohs(udp->uh_sport),
397 				    dst,
398 				    ntohs(udp->uh_dport));
399 			else
400 				snprintf(SNPARGS(proto, len), " %s", dst);
401 			break;
402 
403 		case IPPROTO_ICMP:
404 			icmp = L3HDR(struct icmphdr, ip);
405 			if (offset == 0)
406 				len = snprintf(SNPARGS(proto, 0),
407 				    "ICMP:%u.%u ",
408 				    icmp->icmp_type, icmp->icmp_code);
409 			else
410 				len = snprintf(SNPARGS(proto, 0), "ICMP ");
411 			len += snprintf(SNPARGS(proto, len), "%s", src);
412 			snprintf(SNPARGS(proto, len), " %s", dst);
413 			break;
414 #ifdef INET6
415 		case IPPROTO_ICMPV6:
416 			icmp6 = (struct icmp6_hdr *)(((char *)ip) + hlen);
417 			if (offset == 0)
418 				len = snprintf(SNPARGS(proto, 0),
419 				    "ICMPv6:%u.%u ",
420 				    icmp6->icmp6_type, icmp6->icmp6_code);
421 			else
422 				len = snprintf(SNPARGS(proto, 0), "ICMPv6 ");
423 			len += snprintf(SNPARGS(proto, len), "%s", src);
424 			snprintf(SNPARGS(proto, len), " %s", dst);
425 			break;
426 #endif
427 		default:
428 			len = snprintf(SNPARGS(proto, 0), "P:%d %s",
429 			    args->f_id.proto, src);
430 			snprintf(SNPARGS(proto, len), " %s", dst);
431 			break;
432 		}
433 
434 #ifdef INET6
435 		if (IS_IP6_FLOW_ID(&(args->f_id))) {
436 			if (offset & (IP6F_OFF_MASK | IP6F_MORE_FRAG))
437 				snprintf(SNPARGS(fragment, 0),
438 				    " (frag %08x:%d@%d%s)",
439 				    args->f_id.extra,
440 				    ntohs(ip6->ip6_plen) - hlen,
441 				    ntohs(offset) << 3, ip6f_mf ? "+" : "");
442 		} else
443 #endif
444 		{
445 			int ipoff, iplen;
446 			ipoff = ntohs(ip->ip_off);
447 			iplen = ntohs(ip->ip_len);
448 			if (ipoff & (IP_MF | IP_OFFMASK))
449 				snprintf(SNPARGS(fragment, 0),
450 				    " (frag %d:%d@%d%s)",
451 				    ntohs(ip->ip_id), iplen - (ip->ip_hl << 2),
452 				    offset << 3,
453 				    (ipoff & IP_MF) ? "+" : "");
454 		}
455 	}
456 #ifdef __FreeBSD__
457 	if (oif || m->m_pkthdr.rcvif)
458 		log(LOG_SECURITY | LOG_INFO,
459 		    "ipfw: %d %s %s %s via %s%s\n",
460 		    f ? f->rulenum : -1,
461 		    action, proto, oif ? "out" : "in",
462 		    oif ? oif->if_xname : m->m_pkthdr.rcvif->if_xname,
463 		    fragment);
464 	else
465 #endif
466 		log(LOG_SECURITY | LOG_INFO,
467 		    "ipfw: %d %s %s [no if info]%s\n",
468 		    f ? f->rulenum : -1,
469 		    action, proto, fragment);
470 	if (limit_reached)
471 		log(LOG_SECURITY | LOG_NOTICE,
472 		    "ipfw: limit %d reached on entry %d\n",
473 		    limit_reached, f ? f->rulenum : -1);
474 }
475 /* end of file */
476