ip_fw_pfil.c revision 227085
1238384Sjkim/*-
2238384Sjkim * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
3238384Sjkim * All rights reserved.
4238384Sjkim *
5238384Sjkim * Redistribution and use in source and binary forms, with or without
6238384Sjkim * modification, are permitted provided that the following conditions
7238384Sjkim * are met:
8238384Sjkim * 1. Redistributions of source code must retain the above copyright
9238384Sjkim *    notice, this list of conditions and the following disclaimer.
10238384Sjkim * 2. Redistributions in binary form must reproduce the above copyright
11238384Sjkim *    notice, this list of conditions and the following disclaimer in the
12238384Sjkim *    documentation and/or other materials provided with the distribution.
13238384Sjkim *
14238384Sjkim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15238384Sjkim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16238384Sjkim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17238384Sjkim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18238384Sjkim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19238384Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20238384Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21238384Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22238384Sjkim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23238384Sjkim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24238384Sjkim * SUCH DAMAGE.
25238384Sjkim */
26238384Sjkim
27238384Sjkim#include <sys/cdefs.h>
28238384Sjkim__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw_pfil.c 227085 2011-11-04 16:24:19Z bz $");
29238384Sjkim
30238384Sjkim#include "opt_ipfw.h"
31238384Sjkim#include "opt_inet.h"
32238384Sjkim#include "opt_inet6.h"
33238384Sjkim#ifndef INET
34238384Sjkim#error IPFIREWALL requires INET.
35238384Sjkim#endif /* INET */
36238384Sjkim
37238384Sjkim#include <sys/param.h>
38238384Sjkim#include <sys/systm.h>
39238384Sjkim#include <sys/malloc.h>
40238384Sjkim#include <sys/mbuf.h>
41238384Sjkim#include <sys/module.h>
42238384Sjkim#include <sys/kernel.h>
43238384Sjkim#include <sys/lock.h>
44238384Sjkim#include <sys/rwlock.h>
45246772Sjkim#include <sys/socket.h>
46238384Sjkim#include <sys/sysctl.h>
47238384Sjkim
48238384Sjkim#include <net/if.h>
49238384Sjkim#include <net/route.h>
50238384Sjkim#include <net/pfil.h>
51238384Sjkim#include <net/vnet.h>
52238384Sjkim
53238384Sjkim#include <netinet/in.h>
54238384Sjkim#include <netinet/in_systm.h>
55238384Sjkim#include <netinet/ip.h>
56238384Sjkim#include <netinet/ip_var.h>
57238384Sjkim#include <netinet/ip_fw.h>
58238384Sjkim#ifdef INET6
59238384Sjkim#include <netinet/ip6.h>
60238384Sjkim#include <netinet6/ip6_var.h>
61238384Sjkim#endif
62238384Sjkim#include <netinet/ipfw/ip_fw_private.h>
63238384Sjkim#include <netgraph/ng_ipfw.h>
64238384Sjkim
65238384Sjkim#include <machine/in_cksum.h>
66238384Sjkim
67238384Sjkimstatic VNET_DEFINE(int, fw_enable) = 1;
68238384Sjkim#define V_fw_enable	VNET(fw_enable)
69238384Sjkim
70238384Sjkim#ifdef INET6
71238384Sjkimstatic VNET_DEFINE(int, fw6_enable) = 1;
72238384Sjkim#define V_fw6_enable	VNET(fw6_enable)
73238384Sjkim#endif
74238384Sjkim
75238384Sjkimint ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
76238384Sjkim
77238384Sjkim/* Forward declarations. */
78238384Sjkimstatic int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
79238384Sjkim
80238384Sjkim#ifdef SYSCTL_NODE
81238384Sjkim
82238384SjkimSYSBEGIN(f1)
83238384Sjkim
84238384SjkimSYSCTL_DECL(_net_inet_ip_fw);
85238384SjkimSYSCTL_VNET_PROC(_net_inet_ip_fw, OID_AUTO, enable,
86238384Sjkim    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw_enable), 0,
87238384Sjkim    ipfw_chg_hook, "I", "Enable ipfw");
88238384Sjkim#ifdef INET6
89238384SjkimSYSCTL_DECL(_net_inet6_ip6_fw);
90238384SjkimSYSCTL_VNET_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
91238384Sjkim    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3, &VNET_NAME(fw6_enable), 0,
92238384Sjkim    ipfw_chg_hook, "I", "Enable ipfw+6");
93238384Sjkim#endif /* INET6 */
94238384Sjkim
95238384SjkimSYSEND
96238384Sjkim
97238384Sjkim#endif /* SYSCTL_NODE */
98238384Sjkim
99238384Sjkim/*
100238384Sjkim * The pfilter hook to pass packets to ipfw_chk and then to
101238384Sjkim * dummynet, divert, netgraph or other modules.
102238384Sjkim * The packet may be consumed.
103238384Sjkim */
104238384Sjkimint
105238384Sjkimipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
106238384Sjkim    struct inpcb *inp)
107238384Sjkim{
108238384Sjkim	struct ip_fw_args args;
109238384Sjkim	struct m_tag *tag;
110238384Sjkim	int ipfw;
111238384Sjkim	int ret;
112238384Sjkim
113238384Sjkim	/* all the processing now uses ip_len in net format */
114238384Sjkim	if (mtod(*m0, struct ip *)->ip_v == 4)
115238384Sjkim		SET_NET_IPLEN(mtod(*m0, struct ip *));
116238384Sjkim
117238384Sjkim	/* convert dir to IPFW values */
118238384Sjkim	dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
119238384Sjkim	bzero(&args, sizeof(args));
120238384Sjkim
121238384Sjkimagain:
122238384Sjkim	/*
123238384Sjkim	 * extract and remove the tag if present. If we are left
124238384Sjkim	 * with onepass, optimize the outgoing path.
125238384Sjkim	 */
126238384Sjkim	tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
127238384Sjkim	if (tag != NULL) {
128238384Sjkim		args.rule = *((struct ipfw_rule_ref *)(tag+1));
129238384Sjkim		m_tag_delete(*m0, tag);
130238384Sjkim		if (args.rule.info & IPFW_ONEPASS) {
131238384Sjkim			if (mtod(*m0, struct ip *)->ip_v == 4)
132238384Sjkim				SET_HOST_IPLEN(mtod(*m0, struct ip *));
133238384Sjkim			return (0);
134238384Sjkim		}
135238384Sjkim	}
136238384Sjkim
137238384Sjkim	args.m = *m0;
138238384Sjkim	args.oif = dir == DIR_OUT ? ifp : NULL;
139238384Sjkim	args.inp = inp;
140238384Sjkim
141238384Sjkim	ipfw = ipfw_chk(&args);
142238384Sjkim	*m0 = args.m;
143238384Sjkim
144238384Sjkim	KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
145238384Sjkim	    __func__));
146238384Sjkim
147238384Sjkim	/* breaking out of the switch means drop */
148238384Sjkim	ret = 0;	/* default return value for pass */
149238384Sjkim	switch (ipfw) {
150238384Sjkim	case IP_FW_PASS:
151238384Sjkim		/* next_hop may be set by ipfw_chk */
152238384Sjkim		if (args.next_hop == NULL && args.next_hop6 == NULL)
153238384Sjkim			break; /* pass */
154238384Sjkim#if !defined(IPFIREWALL_FORWARD) || (!defined(INET6) && !defined(INET))
155238384Sjkim		ret = EACCES;
156238384Sjkim#else
157238384Sjkim	    {
158238384Sjkim		struct m_tag *fwd_tag;
159238384Sjkim		size_t len;
160238384Sjkim
161238384Sjkim		KASSERT(args.next_hop == NULL || args.next_hop6 == NULL,
162238384Sjkim		    ("%s: both next_hop=%p and next_hop6=%p not NULL", __func__,
163238384Sjkim		     args.next_hop, args.next_hop6));
164238384Sjkim#ifdef INET6
165238384Sjkim		if (args.next_hop6 != NULL)
166238384Sjkim			len = sizeof(struct sockaddr_in6);
167238384Sjkim#endif
168238384Sjkim#ifdef INET
169238384Sjkim		if (args.next_hop != NULL)
170238384Sjkim			len = sizeof(struct sockaddr_in);
171238384Sjkim#endif
172238384Sjkim
173238384Sjkim		/* Incoming packets should not be tagged so we do not
174238384Sjkim		 * m_tag_find. Outgoing packets may be tagged, so we
175238384Sjkim		 * reuse the tag if present.
176238384Sjkim		 */
177238384Sjkim		fwd_tag = (dir == DIR_IN) ? NULL :
178238384Sjkim			m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
179238384Sjkim		if (fwd_tag != NULL) {
180238384Sjkim			m_tag_unlink(*m0, fwd_tag);
181238384Sjkim		} else {
182238384Sjkim			fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
183238384Sjkim			    M_NOWAIT);
184238384Sjkim			if (fwd_tag == NULL) {
185238384Sjkim				ret = EACCES;
186238384Sjkim				break; /* i.e. drop */
187238384Sjkim			}
188238384Sjkim		}
189238384Sjkim#ifdef INET6
190238384Sjkim		if (args.next_hop6 != NULL) {
191238384Sjkim			bcopy(args.next_hop6, (fwd_tag+1), len);
192238384Sjkim			if (in6_localip(&args.next_hop6->sin6_addr))
193238384Sjkim				(*m0)->m_flags |= M_FASTFWD_OURS;
194238384Sjkim		}
195238384Sjkim#endif
196238384Sjkim#ifdef INET
197279264Sdelphij		if (args.next_hop != NULL) {
198279264Sdelphij			bcopy(args.next_hop, (fwd_tag+1), len);
199238384Sjkim			if (in_localip(args.next_hop->sin_addr))
200238384Sjkim				(*m0)->m_flags |= M_FASTFWD_OURS;
201238384Sjkim		}
202238384Sjkim#endif
203238384Sjkim		m_tag_prepend(*m0, fwd_tag);
204279264Sdelphij	    }
205238384Sjkim#endif /* IPFIREWALL_FORWARD */
206279264Sdelphij		break;
207238384Sjkim
208279264Sdelphij	case IP_FW_DENY:
209238384Sjkim		ret = EACCES;
210279264Sdelphij		break; /* i.e. drop */
211279264Sdelphij
212238384Sjkim	case IP_FW_DUMMYNET:
213279264Sdelphij		ret = EACCES;
214238384Sjkim		if (ip_dn_io_ptr == NULL)
215279264Sdelphij			break; /* i.e. drop */
216238384Sjkim		if (mtod(*m0, struct ip *)->ip_v == 4)
217238384Sjkim			ret = ip_dn_io_ptr(m0, dir, &args);
218238384Sjkim		else if (mtod(*m0, struct ip *)->ip_v == 6)
219238384Sjkim			ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
220238384Sjkim		else
221238384Sjkim			break; /* drop it */
222238384Sjkim		/*
223238384Sjkim		 * XXX should read the return value.
224238384Sjkim		 * dummynet normally eats the packet and sets *m0=NULL
225238384Sjkim		 * unless the packet can be sent immediately. In this
226238384Sjkim		 * case args is updated and we should re-run the
227238384Sjkim		 * check without clearing args.
228238384Sjkim		 */
229238384Sjkim		if (*m0 != NULL)
230238384Sjkim			goto again;
231238384Sjkim		break;
232238384Sjkim
233238384Sjkim	case IP_FW_TEE:
234238384Sjkim	case IP_FW_DIVERT:
235238384Sjkim		if (ip_divert_ptr == NULL) {
236238384Sjkim			ret = EACCES;
237238384Sjkim			break; /* i.e. drop */
238238384Sjkim		}
239238384Sjkim		ret = ipfw_divert(m0, dir, &args.rule,
240238384Sjkim			(ipfw == IP_FW_TEE) ? 1 : 0);
241238384Sjkim		/* continue processing for the original packet (tee). */
242238384Sjkim		if (*m0)
243238384Sjkim			goto again;
244238384Sjkim		break;
245238384Sjkim
246238384Sjkim	case IP_FW_NGTEE:
247238384Sjkim	case IP_FW_NETGRAPH:
248238384Sjkim		if (ng_ipfw_input_p == NULL) {
249238384Sjkim			ret = EACCES;
250238384Sjkim			break; /* i.e. drop */
251238384Sjkim		}
252238384Sjkim		ret = ng_ipfw_input_p(m0, dir, &args,
253238384Sjkim			(ipfw == IP_FW_NGTEE) ? 1 : 0);
254238384Sjkim		if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
255			goto again;	/* continue with packet */
256		break;
257
258	case IP_FW_NAT:
259		/* honor one-pass in case of successful nat */
260		if (V_fw_one_pass)
261			break; /* ret is already 0 */
262		goto again;
263
264	case IP_FW_REASS:
265		goto again;		/* continue with packet */
266
267	default:
268		KASSERT(0, ("%s: unknown retval", __func__));
269	}
270
271	if (ret != 0) {
272		if (*m0)
273			FREE_PKT(*m0);
274		*m0 = NULL;
275	}
276	if (*m0 && mtod(*m0, struct ip *)->ip_v == 4)
277		SET_HOST_IPLEN(mtod(*m0, struct ip *));
278	return ret;
279}
280
281/* do the divert, return 1 on error 0 on success */
282static int
283ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
284	int tee)
285{
286	/*
287	 * ipfw_chk() has already tagged the packet with the divert tag.
288	 * If tee is set, copy packet and return original.
289	 * If not tee, consume packet and send it to divert socket.
290	 */
291	struct mbuf *clone;
292	struct ip *ip = mtod(*m0, struct ip *);
293	struct m_tag *tag;
294
295	/* Cloning needed for tee? */
296	if (tee == 0) {
297		clone = *m0;	/* use the original mbuf */
298		*m0 = NULL;
299	} else {
300		clone = m_dup(*m0, M_DONTWAIT);
301		/* If we cannot duplicate the mbuf, we sacrifice the divert
302		 * chain and continue with the tee-ed packet.
303		 */
304		if (clone == NULL)
305			return 1;
306	}
307
308	/*
309	 * Divert listeners can normally handle non-fragmented packets,
310	 * but we can only reass in the non-tee case.
311	 * This means that listeners on a tee rule may get fragments,
312	 * and have to live with that.
313	 * Note that we now have the 'reass' ipfw option so if we care
314	 * we can do it before a 'tee'.
315	 */
316	if (!tee) switch (ip->ip_v) {
317	case IPVERSION:
318	    if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
319		int hlen;
320		struct mbuf *reass;
321
322		SET_HOST_IPLEN(ip); /* ip_reass wants host order */
323		reass = ip_reass(clone); /* Reassemble packet. */
324		if (reass == NULL)
325			return 0; /* not an error */
326		/* if reass = NULL then it was consumed by ip_reass */
327		/*
328		 * IP header checksum fixup after reassembly and leave header
329		 * in network byte order.
330		 */
331		ip = mtod(reass, struct ip *);
332		hlen = ip->ip_hl << 2;
333		SET_NET_IPLEN(ip);
334		ip->ip_sum = 0;
335		if (hlen == sizeof(struct ip))
336			ip->ip_sum = in_cksum_hdr(ip);
337		else
338			ip->ip_sum = in_cksum(reass, hlen);
339		clone = reass;
340	    }
341	    break;
342#ifdef INET6
343	case IPV6_VERSION >> 4:
344	    {
345	    struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
346
347		if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
348			int nxt, off;
349
350			off = sizeof(struct ip6_hdr);
351			nxt = frag6_input(&clone, &off, 0);
352			if (nxt == IPPROTO_DONE)
353				return (0);
354		}
355		break;
356	    }
357#endif
358	}
359
360	/* attach a tag to the packet with the reinject info */
361	tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
362		    sizeof(struct ipfw_rule_ref), M_NOWAIT);
363	if (tag == NULL) {
364		FREE_PKT(clone);
365		return 1;
366	}
367	*((struct ipfw_rule_ref *)(tag+1)) = *rule;
368	m_tag_prepend(clone, tag);
369
370	/* Do the dirty job... */
371	ip_divert_ptr(clone, incoming);
372	return 0;
373}
374
375/*
376 * attach or detach hooks for a given protocol family
377 */
378static int
379ipfw_hook(int onoff, int pf)
380{
381	struct pfil_head *pfh;
382
383	pfh = pfil_head_get(PFIL_TYPE_AF, pf);
384	if (pfh == NULL)
385		return ENOENT;
386
387	(void) (onoff ? pfil_add_hook : pfil_remove_hook)
388	    (ipfw_check_hook, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh);
389
390	return 0;
391}
392
393int
394ipfw_attach_hooks(int arg)
395{
396	int error = 0;
397
398	if (arg == 0) /* detach */
399		ipfw_hook(0, AF_INET);
400	else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
401                error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
402                printf("ipfw_hook() error\n");
403        }
404#ifdef INET6
405	if (arg == 0) /* detach */
406		ipfw_hook(0, AF_INET6);
407	else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
408                error = ENOENT;
409                printf("ipfw6_hook() error\n");
410        }
411#endif
412	return error;
413}
414
415int
416ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
417{
418	int enable;
419	int oldenable;
420	int error;
421	int af;
422
423	if (arg1 == &VNET_NAME(fw_enable)) {
424		enable = V_fw_enable;
425		af = AF_INET;
426	}
427#ifdef INET6
428	else if (arg1 == &VNET_NAME(fw6_enable)) {
429		enable = V_fw6_enable;
430		af = AF_INET6;
431	}
432#endif
433	else
434		return (EINVAL);
435
436	oldenable = enable;
437
438	error = sysctl_handle_int(oidp, &enable, 0, req);
439
440	if (error)
441		return (error);
442
443	enable = (enable) ? 1 : 0;
444
445	if (enable == oldenable)
446		return (0);
447
448	error = ipfw_hook(enable, af);
449	if (error)
450		return (error);
451	if (af == AF_INET)
452		V_fw_enable = enable;
453#ifdef INET6
454	else if (af == AF_INET6)
455		V_fw6_enable = enable;
456#endif
457
458	return (0);
459}
460/* end of file */
461