ip_fw_pfil.c revision 225793
1/*-
2 * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw_pfil.c 225793 2011-09-27 13:27:17Z bz $");
29
30#include "opt_ipfw.h"
31#if !defined(KLD_MODULE)
32#include "opt_ipdn.h"
33#include "opt_inet.h"
34#include "opt_inet6.h"
35#ifndef INET
36#error IPFIREWALL requires INET.
37#endif /* INET */
38#endif /* KLD_MODULE */
39
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/module.h>
45#include <sys/kernel.h>
46#include <sys/lock.h>
47#include <sys/rwlock.h>
48#include <sys/socket.h>
49#include <sys/sysctl.h>
50
51#include <net/if.h>
52#include <net/route.h>
53#include <net/pfil.h>
54#include <net/vnet.h>
55
56#include <netinet/in.h>
57#include <netinet/in_systm.h>
58#include <netinet/ip.h>
59#include <netinet/ip_var.h>
60#include <netinet/ip_fw.h>
61#ifdef INET6
62#include <netinet/ip6.h>
63#include <netinet6/ip6_var.h>
64#endif
65#include <netinet/ipfw/ip_fw_private.h>
66#include <netgraph/ng_ipfw.h>
67
68#include <machine/in_cksum.h>
69
70static VNET_DEFINE(int, fw_enable) = 1;
71#define V_fw_enable	VNET(fw_enable)
72
73#ifdef INET6
74static VNET_DEFINE(int, fw6_enable) = 1;
75#define V_fw6_enable	VNET(fw6_enable)
76#endif
77
78int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
79
80/* Forward declarations. */
81static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
82
83#ifdef SYSCTL_NODE
84
85SYSBEGIN(f1)
86
87SYSCTL_DECL(_net_inet_ip_fw);
88SYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, enable,
89    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_enable), 0,
90    ipfw_chg_hook, "I", "Enable ipfw");
91#ifdef INET6
92SYSCTL_DECL(_net_inet6_ip6_fw);
93SYSCTL_VNET_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
94    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw6_enable), 0,
95    ipfw_chg_hook, "I", "Enable ipfw+6");
96#endif /* INET6 */
97
98SYSEND
99
100#endif /* SYSCTL_NODE */
101
102/*
103 * The pfilter hook to pass packets to ipfw_chk and then to
104 * dummynet, divert, netgraph or other modules.
105 * The packet may be consumed.
106 */
107int
108ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
109    struct inpcb *inp)
110{
111	struct ip_fw_args args;
112	struct m_tag *tag;
113	int ipfw;
114	int ret;
115
116	/* all the processing now uses ip_len in net format */
117	if (mtod(*m0, struct ip *)->ip_v == 4)
118		SET_NET_IPLEN(mtod(*m0, struct ip *));
119
120	/* convert dir to IPFW values */
121	dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
122	bzero(&args, sizeof(args));
123
124again:
125	/*
126	 * extract and remove the tag if present. If we are left
127	 * with onepass, optimize the outgoing path.
128	 */
129	tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
130	if (tag != NULL) {
131		args.rule = *((struct ipfw_rule_ref *)(tag+1));
132		m_tag_delete(*m0, tag);
133		if (args.rule.info & IPFW_ONEPASS) {
134			if (mtod(*m0, struct ip *)->ip_v == 4)
135				SET_HOST_IPLEN(mtod(*m0, struct ip *));
136			return (0);
137		}
138	}
139
140	args.m = *m0;
141	args.oif = dir == DIR_OUT ? ifp : NULL;
142	args.inp = inp;
143
144	ipfw = ipfw_chk(&args);
145	*m0 = args.m;
146
147	KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
148	    __func__));
149
150	/* breaking out of the switch means drop */
151	ret = 0;	/* default return value for pass */
152	switch (ipfw) {
153	case IP_FW_PASS:
154		/* next_hop may be set by ipfw_chk */
155		if (args.next_hop == NULL && args.next_hop6 == NULL)
156			break; /* pass */
157#if !defined(IPFIREWALL_FORWARD) || (!defined(INET6) && !defined(INET))
158		ret = EACCES;
159#else
160	    {
161		struct m_tag *fwd_tag;
162		size_t len;
163
164		KASSERT(args.next_hop == NULL || args.next_hop6 == NULL,
165		    ("%s: both next_hop=%p and next_hop6=%p not NULL", __func__,
166		     args.next_hop, args.next_hop6));
167#ifdef INET6
168		if (args.next_hop6 != NULL)
169			len = sizeof(struct sockaddr_in6);
170#endif
171#ifdef INET
172		if (args.next_hop != NULL)
173			len = sizeof(struct sockaddr_in);
174#endif
175
176		/* Incoming packets should not be tagged so we do not
177		 * m_tag_find. Outgoing packets may be tagged, so we
178		 * reuse the tag if present.
179		 */
180		fwd_tag = (dir == DIR_IN) ? NULL :
181			m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
182		if (fwd_tag != NULL) {
183			m_tag_unlink(*m0, fwd_tag);
184		} else {
185			fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
186			    M_NOWAIT);
187			if (fwd_tag == NULL) {
188				ret = EACCES;
189				break; /* i.e. drop */
190			}
191		}
192#ifdef INET6
193		if (args.next_hop6 != NULL) {
194			bcopy(args.next_hop6, (fwd_tag+1), len);
195			if (in6_localip(&args.next_hop6->sin6_addr))
196				(*m0)->m_flags |= M_FASTFWD_OURS;
197		}
198#endif
199#ifdef INET
200		if (args.next_hop != NULL) {
201			bcopy(args.next_hop, (fwd_tag+1), len);
202			if (in_localip(args.next_hop->sin_addr))
203				(*m0)->m_flags |= M_FASTFWD_OURS;
204		}
205#endif
206		m_tag_prepend(*m0, fwd_tag);
207	    }
208#endif /* IPFIREWALL_FORWARD */
209		break;
210
211	case IP_FW_DENY:
212		ret = EACCES;
213		break; /* i.e. drop */
214
215	case IP_FW_DUMMYNET:
216		ret = EACCES;
217		if (ip_dn_io_ptr == NULL)
218			break; /* i.e. drop */
219		if (mtod(*m0, struct ip *)->ip_v == 4)
220			ret = ip_dn_io_ptr(m0, dir, &args);
221		else if (mtod(*m0, struct ip *)->ip_v == 6)
222			ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
223		else
224			break; /* drop it */
225		/*
226		 * XXX should read the return value.
227		 * dummynet normally eats the packet and sets *m0=NULL
228		 * unless the packet can be sent immediately. In this
229		 * case args is updated and we should re-run the
230		 * check without clearing args.
231		 */
232		if (*m0 != NULL)
233			goto again;
234		break;
235
236	case IP_FW_TEE:
237	case IP_FW_DIVERT:
238		if (ip_divert_ptr == NULL) {
239			ret = EACCES;
240			break; /* i.e. drop */
241		}
242		ret = ipfw_divert(m0, dir, &args.rule,
243			(ipfw == IP_FW_TEE) ? 1 : 0);
244		/* continue processing for the original packet (tee). */
245		if (*m0)
246			goto again;
247		break;
248
249	case IP_FW_NGTEE:
250	case IP_FW_NETGRAPH:
251		if (ng_ipfw_input_p == NULL) {
252			ret = EACCES;
253			break; /* i.e. drop */
254		}
255		ret = ng_ipfw_input_p(m0, dir, &args,
256			(ipfw == IP_FW_NGTEE) ? 1 : 0);
257		if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
258			goto again;	/* continue with packet */
259		break;
260
261	case IP_FW_NAT:
262		/* honor one-pass in case of successful nat */
263		if (V_fw_one_pass)
264			break; /* ret is already 0 */
265		goto again;
266
267	case IP_FW_REASS:
268		goto again;		/* continue with packet */
269
270	default:
271		KASSERT(0, ("%s: unknown retval", __func__));
272	}
273
274	if (ret != 0) {
275		if (*m0)
276			FREE_PKT(*m0);
277		*m0 = NULL;
278	}
279	if (*m0 && mtod(*m0, struct ip *)->ip_v == 4)
280		SET_HOST_IPLEN(mtod(*m0, struct ip *));
281	return ret;
282}
283
284/* do the divert, return 1 on error 0 on success */
285static int
286ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
287	int tee)
288{
289	/*
290	 * ipfw_chk() has already tagged the packet with the divert tag.
291	 * If tee is set, copy packet and return original.
292	 * If not tee, consume packet and send it to divert socket.
293	 */
294	struct mbuf *clone;
295	struct ip *ip = mtod(*m0, struct ip *);
296	struct m_tag *tag;
297
298	/* Cloning needed for tee? */
299	if (tee == 0) {
300		clone = *m0;	/* use the original mbuf */
301		*m0 = NULL;
302	} else {
303		clone = m_dup(*m0, M_DONTWAIT);
304		/* If we cannot duplicate the mbuf, we sacrifice the divert
305		 * chain and continue with the tee-ed packet.
306		 */
307		if (clone == NULL)
308			return 1;
309	}
310
311	/*
312	 * Divert listeners can normally handle non-fragmented packets,
313	 * but we can only reass in the non-tee case.
314	 * This means that listeners on a tee rule may get fragments,
315	 * and have to live with that.
316	 * Note that we now have the 'reass' ipfw option so if we care
317	 * we can do it before a 'tee'.
318	 */
319	if (!tee) switch (ip->ip_v) {
320	case IPVERSION:
321	    if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
322		int hlen;
323		struct mbuf *reass;
324
325		SET_HOST_IPLEN(ip); /* ip_reass wants host order */
326		reass = ip_reass(clone); /* Reassemble packet. */
327		if (reass == NULL)
328			return 0; /* not an error */
329		/* if reass = NULL then it was consumed by ip_reass */
330		/*
331		 * IP header checksum fixup after reassembly and leave header
332		 * in network byte order.
333		 */
334		ip = mtod(reass, struct ip *);
335		hlen = ip->ip_hl << 2;
336		SET_NET_IPLEN(ip);
337		ip->ip_sum = 0;
338		if (hlen == sizeof(struct ip))
339			ip->ip_sum = in_cksum_hdr(ip);
340		else
341			ip->ip_sum = in_cksum(reass, hlen);
342		clone = reass;
343	    }
344	    break;
345#ifdef INET6
346	case IPV6_VERSION >> 4:
347	    {
348	    struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
349
350		if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
351			int nxt, off;
352
353			off = sizeof(struct ip6_hdr);
354			nxt = frag6_input(&clone, &off, 0);
355			if (nxt == IPPROTO_DONE)
356				return (0);
357		}
358		break;
359	    }
360#endif
361	}
362
363	/* attach a tag to the packet with the reinject info */
364	tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
365		    sizeof(struct ipfw_rule_ref), M_NOWAIT);
366	if (tag == NULL) {
367		FREE_PKT(clone);
368		return 1;
369	}
370	*((struct ipfw_rule_ref *)(tag+1)) = *rule;
371	m_tag_prepend(clone, tag);
372
373	/* Do the dirty job... */
374	ip_divert_ptr(clone, incoming);
375	return 0;
376}
377
378/*
379 * attach or detach hooks for a given protocol family
380 */
381static int
382ipfw_hook(int onoff, int pf)
383{
384	struct pfil_head *pfh;
385
386	pfh = pfil_head_get(PFIL_TYPE_AF, pf);
387	if (pfh == NULL)
388		return ENOENT;
389
390	(void) (onoff ? pfil_add_hook : pfil_remove_hook)
391	    (ipfw_check_hook, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh);
392
393	return 0;
394}
395
396int
397ipfw_attach_hooks(int arg)
398{
399	int error = 0;
400
401	if (arg == 0) /* detach */
402		ipfw_hook(0, AF_INET);
403	else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
404                error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
405                printf("ipfw_hook() error\n");
406        }
407#ifdef INET6
408	if (arg == 0) /* detach */
409		ipfw_hook(0, AF_INET6);
410	else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
411                error = ENOENT;
412                printf("ipfw6_hook() error\n");
413        }
414#endif
415	return error;
416}
417
418int
419ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
420{
421	int enable;
422	int oldenable;
423	int error;
424	int af;
425
426	if (arg1 == &VNET_NAME(fw_enable)) {
427		enable = V_fw_enable;
428		af = AF_INET;
429	}
430#ifdef INET6
431	else if (arg1 == &VNET_NAME(fw6_enable)) {
432		enable = V_fw6_enable;
433		af = AF_INET6;
434	}
435#endif
436	else
437		return (EINVAL);
438
439	oldenable = enable;
440
441	error = sysctl_handle_int(oidp, &enable, 0, req);
442
443	if (error)
444		return (error);
445
446	enable = (enable) ? 1 : 0;
447
448	if (enable == oldenable)
449		return (0);
450
451	error = ipfw_hook(enable, af);
452	if (error)
453		return (error);
454	if (af == AF_INET)
455		V_fw_enable = enable;
456#ifdef INET6
457	else if (af == AF_INET6)
458		V_fw6_enable = enable;
459#endif
460
461	return (0);
462}
463/* end of file */
464