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