xref: /freebsd/sys/netpfil/ipfw/ip_fw_pfil.c (revision ff0ba87247820afbdfdc1b307c803f7923d0e4d3)
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$");
29 
30 #include "opt_ipfw.h"
31 #include "opt_inet.h"
32 #include "opt_inet6.h"
33 #ifndef INET
34 #error IPFIREWALL requires INET.
35 #endif /* INET */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/mbuf.h>
41 #include <sys/module.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/rwlock.h>
45 #include <sys/socket.h>
46 #include <sys/sysctl.h>
47 
48 #include <net/if.h>
49 #include <net/route.h>
50 #include <net/ethernet.h>
51 #include <net/pfil.h>
52 #include <net/vnet.h>
53 
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/ip_fw.h>
59 #ifdef INET6
60 #include <netinet/ip6.h>
61 #include <netinet6/ip6_var.h>
62 #endif
63 
64 #include <netgraph/ng_ipfw.h>
65 
66 #include <netpfil/ipfw/ip_fw_private.h>
67 
68 #include <machine/in_cksum.h>
69 
70 static VNET_DEFINE(int, fw_enable) = 1;
71 #define V_fw_enable	VNET(fw_enable)
72 
73 #ifdef INET6
74 static VNET_DEFINE(int, fw6_enable) = 1;
75 #define V_fw6_enable	VNET(fw6_enable)
76 #endif
77 
78 static VNET_DEFINE(int, fwlink_enable) = 0;
79 #define V_fwlink_enable	VNET(fwlink_enable)
80 
81 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
82 
83 /* Forward declarations. */
84 static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
85 int ipfw_check_packet(void *, struct mbuf **, struct ifnet *, int,
86 	struct inpcb *);
87 int ipfw_check_frame(void *, struct mbuf **, struct ifnet *, int,
88 	struct inpcb *);
89 
90 #ifdef SYSCTL_NODE
91 
92 SYSBEGIN(f1)
93 
94 SYSCTL_DECL(_net_inet_ip_fw);
95 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, enable,
96     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
97     &VNET_NAME(fw_enable), 0, ipfw_chg_hook, "I", "Enable ipfw");
98 #ifdef INET6
99 SYSCTL_DECL(_net_inet6_ip6_fw);
100 SYSCTL_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
101     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
102     &VNET_NAME(fw6_enable), 0, ipfw_chg_hook, "I", "Enable ipfw+6");
103 #endif /* INET6 */
104 
105 SYSCTL_DECL(_net_link_ether);
106 SYSCTL_PROC(_net_link_ether, OID_AUTO, ipfw,
107     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
108     &VNET_NAME(fwlink_enable), 0, ipfw_chg_hook, "I",
109     "Pass ether pkts through firewall");
110 
111 SYSEND
112 
113 #endif /* SYSCTL_NODE */
114 
115 /*
116  * The pfilter hook to pass packets to ipfw_chk and then to
117  * dummynet, divert, netgraph or other modules.
118  * The packet may be consumed.
119  */
120 int
121 ipfw_check_packet(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
122     struct inpcb *inp)
123 {
124 	struct ip_fw_args args;
125 	struct m_tag *tag;
126 	int ipfw;
127 	int ret;
128 
129 	/* convert dir to IPFW values */
130 	dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
131 	bzero(&args, sizeof(args));
132 
133 again:
134 	/*
135 	 * extract and remove the tag if present. If we are left
136 	 * with onepass, optimize the outgoing path.
137 	 */
138 	tag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
139 	if (tag != NULL) {
140 		args.rule = *((struct ipfw_rule_ref *)(tag+1));
141 		m_tag_delete(*m0, tag);
142 		if (args.rule.info & IPFW_ONEPASS)
143 			return (0);
144 	}
145 
146 	args.m = *m0;
147 	args.oif = dir == DIR_OUT ? ifp : NULL;
148 	args.inp = inp;
149 
150 	ipfw = ipfw_chk(&args);
151 	*m0 = args.m;
152 
153 	KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
154 	    __func__));
155 
156 	/* breaking out of the switch means drop */
157 	ret = 0;	/* default return value for pass */
158 	switch (ipfw) {
159 	case IP_FW_PASS:
160 		/* next_hop may be set by ipfw_chk */
161 		if (args.next_hop == NULL && args.next_hop6 == NULL)
162 			break; /* pass */
163 #if (!defined(INET6) && !defined(INET))
164 		ret = EACCES;
165 #else
166 	    {
167 		struct m_tag *fwd_tag;
168 		size_t len;
169 
170 		KASSERT(args.next_hop == NULL || args.next_hop6 == NULL,
171 		    ("%s: both next_hop=%p and next_hop6=%p not NULL", __func__,
172 		     args.next_hop, args.next_hop6));
173 #ifdef INET6
174 		if (args.next_hop6 != NULL)
175 			len = sizeof(struct sockaddr_in6);
176 #endif
177 #ifdef INET
178 		if (args.next_hop != NULL)
179 			len = sizeof(struct sockaddr_in);
180 #endif
181 
182 		/* Incoming packets should not be tagged so we do not
183 		 * m_tag_find. Outgoing packets may be tagged, so we
184 		 * reuse the tag if present.
185 		 */
186 		fwd_tag = (dir == DIR_IN) ? NULL :
187 			m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
188 		if (fwd_tag != NULL) {
189 			m_tag_unlink(*m0, fwd_tag);
190 		} else {
191 			fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
192 			    M_NOWAIT);
193 			if (fwd_tag == NULL) {
194 				ret = EACCES;
195 				break; /* i.e. drop */
196 			}
197 		}
198 #ifdef INET6
199 		if (args.next_hop6 != NULL) {
200 			bcopy(args.next_hop6, (fwd_tag+1), len);
201 			if (in6_localip(&args.next_hop6->sin6_addr))
202 				(*m0)->m_flags |= M_FASTFWD_OURS;
203 			(*m0)->m_flags |= M_IP6_NEXTHOP;
204 		}
205 #endif
206 #ifdef INET
207 		if (args.next_hop != NULL) {
208 			bcopy(args.next_hop, (fwd_tag+1), len);
209 			if (in_localip(args.next_hop->sin_addr))
210 				(*m0)->m_flags |= M_FASTFWD_OURS;
211 			(*m0)->m_flags |= M_IP_NEXTHOP;
212 		}
213 #endif
214 		m_tag_prepend(*m0, fwd_tag);
215 	    }
216 #endif /* INET || INET6 */
217 		break;
218 
219 	case IP_FW_DENY:
220 		ret = EACCES;
221 		break; /* i.e. drop */
222 
223 	case IP_FW_DUMMYNET:
224 		ret = EACCES;
225 		if (ip_dn_io_ptr == NULL)
226 			break; /* i.e. drop */
227 		if (mtod(*m0, struct ip *)->ip_v == 4)
228 			ret = ip_dn_io_ptr(m0, dir, &args);
229 		else if (mtod(*m0, struct ip *)->ip_v == 6)
230 			ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
231 		else
232 			break; /* drop it */
233 		/*
234 		 * XXX should read the return value.
235 		 * dummynet normally eats the packet and sets *m0=NULL
236 		 * unless the packet can be sent immediately. In this
237 		 * case args is updated and we should re-run the
238 		 * check without clearing args.
239 		 */
240 		if (*m0 != NULL)
241 			goto again;
242 		break;
243 
244 	case IP_FW_TEE:
245 	case IP_FW_DIVERT:
246 		if (ip_divert_ptr == NULL) {
247 			ret = EACCES;
248 			break; /* i.e. drop */
249 		}
250 		ret = ipfw_divert(m0, dir, &args.rule,
251 			(ipfw == IP_FW_TEE) ? 1 : 0);
252 		/* continue processing for the original packet (tee). */
253 		if (*m0)
254 			goto again;
255 		break;
256 
257 	case IP_FW_NGTEE:
258 	case IP_FW_NETGRAPH:
259 		if (ng_ipfw_input_p == NULL) {
260 			ret = EACCES;
261 			break; /* i.e. drop */
262 		}
263 		ret = ng_ipfw_input_p(m0, dir, &args,
264 			(ipfw == IP_FW_NGTEE) ? 1 : 0);
265 		if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
266 			goto again;	/* continue with packet */
267 		break;
268 
269 	case IP_FW_NAT:
270 		/* honor one-pass in case of successful nat */
271 		if (V_fw_one_pass)
272 			break; /* ret is already 0 */
273 		goto again;
274 
275 	case IP_FW_REASS:
276 		goto again;		/* continue with packet */
277 
278 	default:
279 		KASSERT(0, ("%s: unknown retval", __func__));
280 	}
281 
282 	if (ret != 0) {
283 		if (*m0)
284 			FREE_PKT(*m0);
285 		*m0 = NULL;
286 	}
287 
288 	return ret;
289 }
290 
291 /*
292  * ipfw processing for ethernet packets (in and out).
293  * Inteface is NULL from ether_demux, and ifp from
294  * ether_output_frame.
295  */
296 int
297 ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *dst, int dir,
298     struct inpcb *inp)
299 {
300 	struct ether_header *eh;
301 	struct ether_header save_eh;
302 	struct mbuf *m;
303 	int i, ret;
304 	struct ip_fw_args args;
305 	struct m_tag *mtag;
306 
307 	/* fetch start point from rule, if any */
308 	mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
309 	if (mtag == NULL) {
310 		args.rule.slot = 0;
311 	} else {
312 		/* dummynet packet, already partially processed */
313 		struct ipfw_rule_ref *r;
314 
315 		/* XXX can we free it after use ? */
316 		mtag->m_tag_id = PACKET_TAG_NONE;
317 		r = (struct ipfw_rule_ref *)(mtag + 1);
318 		if (r->info & IPFW_ONEPASS)
319 			return (0);
320 		args.rule = *r;
321 	}
322 
323 	/* I need some amt of data to be contiguous */
324 	m = *m0;
325 	i = min(m->m_pkthdr.len, max_protohdr);
326 	if (m->m_len < i) {
327 		m = m_pullup(m, i);
328 		if (m == NULL) {
329 			*m0 = m;
330 			return (0);
331 		}
332 	}
333 	eh = mtod(m, struct ether_header *);
334 	save_eh = *eh;			/* save copy for restore below */
335 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
336 
337 	args.m = m;		/* the packet we are looking at		*/
338 	args.oif = dir == PFIL_OUT ? dst: NULL;	/* destination, if any	*/
339 	args.next_hop = NULL;	/* we do not support forward yet	*/
340 	args.next_hop6 = NULL;	/* we do not support forward yet	*/
341 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
342 	args.inp = NULL;	/* used by ipfw uid/gid/jail rules	*/
343 	i = ipfw_chk(&args);
344 	m = args.m;
345 	if (m != NULL) {
346 		/*
347 		 * Restore Ethernet header, as needed, in case the
348 		 * mbuf chain was replaced by ipfw.
349 		 */
350 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
351 		if (m == NULL) {
352 			*m0 = NULL;
353 			return (0);
354 		}
355 		if (eh != mtod(m, struct ether_header *))
356 			bcopy(&save_eh, mtod(m, struct ether_header *),
357 				ETHER_HDR_LEN);
358 	}
359 	*m0 = m;
360 
361 	ret = 0;
362 	/* Check result of ipfw_chk() */
363 	switch (i) {
364 	case IP_FW_PASS:
365 		break;
366 
367 	case IP_FW_DENY:
368 		ret = EACCES;
369 		break; /* i.e. drop */
370 
371 	case IP_FW_DUMMYNET:
372 		ret = EACCES;
373 		int dir;
374 
375 		if (ip_dn_io_ptr == NULL)
376 			break; /* i.e. drop */
377 
378 		*m0 = NULL;
379 		dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN);
380 		ip_dn_io_ptr(&m, dir, &args);
381 		return 0;
382 
383 	default:
384 		KASSERT(0, ("%s: unknown retval", __func__));
385 	}
386 
387 	if (ret != 0) {
388 		if (*m0)
389 			FREE_PKT(*m0);
390 		*m0 = NULL;
391 	}
392 
393 	return ret;
394 }
395 
396 /* do the divert, return 1 on error 0 on success */
397 static int
398 ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
399 	int tee)
400 {
401 	/*
402 	 * ipfw_chk() has already tagged the packet with the divert tag.
403 	 * If tee is set, copy packet and return original.
404 	 * If not tee, consume packet and send it to divert socket.
405 	 */
406 	struct mbuf *clone;
407 	struct ip *ip = mtod(*m0, struct ip *);
408 	struct m_tag *tag;
409 
410 	/* Cloning needed for tee? */
411 	if (tee == 0) {
412 		clone = *m0;	/* use the original mbuf */
413 		*m0 = NULL;
414 	} else {
415 		clone = m_dup(*m0, M_NOWAIT);
416 		/* If we cannot duplicate the mbuf, we sacrifice the divert
417 		 * chain and continue with the tee-ed packet.
418 		 */
419 		if (clone == NULL)
420 			return 1;
421 	}
422 
423 	/*
424 	 * Divert listeners can normally handle non-fragmented packets,
425 	 * but we can only reass in the non-tee case.
426 	 * This means that listeners on a tee rule may get fragments,
427 	 * and have to live with that.
428 	 * Note that we now have the 'reass' ipfw option so if we care
429 	 * we can do it before a 'tee'.
430 	 */
431 	if (!tee) switch (ip->ip_v) {
432 	case IPVERSION:
433 	    if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
434 		int hlen;
435 		struct mbuf *reass;
436 
437 		reass = ip_reass(clone); /* Reassemble packet. */
438 		if (reass == NULL)
439 			return 0; /* not an error */
440 		/* if reass = NULL then it was consumed by ip_reass */
441 		/*
442 		 * IP header checksum fixup after reassembly and leave header
443 		 * in network byte order.
444 		 */
445 		ip = mtod(reass, struct ip *);
446 		hlen = ip->ip_hl << 2;
447 		ip->ip_sum = 0;
448 		if (hlen == sizeof(struct ip))
449 			ip->ip_sum = in_cksum_hdr(ip);
450 		else
451 			ip->ip_sum = in_cksum(reass, hlen);
452 		clone = reass;
453 	    }
454 	    break;
455 #ifdef INET6
456 	case IPV6_VERSION >> 4:
457 	    {
458 	    struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
459 
460 		if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
461 			int nxt, off;
462 
463 			off = sizeof(struct ip6_hdr);
464 			nxt = frag6_input(&clone, &off, 0);
465 			if (nxt == IPPROTO_DONE)
466 				return (0);
467 		}
468 		break;
469 	    }
470 #endif
471 	}
472 
473 	/* attach a tag to the packet with the reinject info */
474 	tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
475 		    sizeof(struct ipfw_rule_ref), M_NOWAIT);
476 	if (tag == NULL) {
477 		FREE_PKT(clone);
478 		return 1;
479 	}
480 	*((struct ipfw_rule_ref *)(tag+1)) = *rule;
481 	m_tag_prepend(clone, tag);
482 
483 	/* Do the dirty job... */
484 	ip_divert_ptr(clone, incoming);
485 	return 0;
486 }
487 
488 /*
489  * attach or detach hooks for a given protocol family
490  */
491 static int
492 ipfw_hook(int onoff, int pf)
493 {
494 	struct pfil_head *pfh;
495 	void *hook_func;
496 
497 	pfh = pfil_head_get(PFIL_TYPE_AF, pf);
498 	if (pfh == NULL)
499 		return ENOENT;
500 
501 	hook_func = (pf == AF_LINK) ? ipfw_check_frame : ipfw_check_packet;
502 
503 	(void) (onoff ? pfil_add_hook : pfil_remove_hook)
504 	    (hook_func, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh);
505 
506 	return 0;
507 }
508 
509 int
510 ipfw_attach_hooks(int arg)
511 {
512 	int error = 0;
513 
514 	if (arg == 0) /* detach */
515 		ipfw_hook(0, AF_INET);
516 	else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
517                 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
518                 printf("ipfw_hook() error\n");
519         }
520 #ifdef INET6
521 	if (arg == 0) /* detach */
522 		ipfw_hook(0, AF_INET6);
523 	else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
524                 error = ENOENT;
525                 printf("ipfw6_hook() error\n");
526         }
527 #endif
528 	if (arg == 0) /* detach */
529 		ipfw_hook(0, AF_LINK);
530 	else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) {
531                 error = ENOENT;
532                 printf("ipfw_link_hook() error\n");
533         }
534 	return error;
535 }
536 
537 int
538 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
539 {
540 	int newval;
541 	int error;
542 	int af;
543 
544 	if (arg1 == &V_fw_enable)
545 		af = AF_INET;
546 #ifdef INET6
547 	else if (arg1 == &V_fw6_enable)
548 		af = AF_INET6;
549 #endif
550 	else if (arg1 == &V_fwlink_enable)
551 		af = AF_LINK;
552 	else
553 		return (EINVAL);
554 
555 	newval = *(int *)arg1;
556 	/* Handle sysctl change */
557 	error = sysctl_handle_int(oidp, &newval, 0, req);
558 
559 	if (error)
560 		return (error);
561 
562 	/* Formalize new value */
563 	newval = (newval) ? 1 : 0;
564 
565 	if (*(int *)arg1 == newval)
566 		return (0);
567 
568 	error = ipfw_hook(newval, af);
569 	if (error)
570 		return (error);
571 	*(int *)arg1 = newval;
572 
573 	return (0);
574 }
575 /* end of file */
576