xref: /freebsd/sys/netpfil/ipfw/ip_fw_pfil.c (revision 28f4385e45a2681c14bd04b83fe1796eaefe8265)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_ipfw.h"
33 #include "opt_inet.h"
34 #include "opt_inet6.h"
35 #ifndef INET
36 #error IPFIREWALL requires INET.
37 #endif /* INET */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/malloc.h>
42 #include <sys/mbuf.h>
43 #include <sys/module.h>
44 #include <sys/kernel.h>
45 #include <sys/lock.h>
46 #include <sys/rwlock.h>
47 #include <sys/socket.h>
48 #include <sys/sysctl.h>
49 
50 #include <net/if.h>
51 #include <net/route.h>
52 #include <net/ethernet.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 #include <netinet6/scope6_var.h>
65 #endif
66 
67 #include <netgraph/ng_ipfw.h>
68 
69 #include <netpfil/ipfw/ip_fw_private.h>
70 
71 #include <machine/in_cksum.h>
72 
73 VNET_DEFINE_STATIC(int, fw_enable) = 1;
74 #define V_fw_enable	VNET(fw_enable)
75 
76 #ifdef INET6
77 VNET_DEFINE_STATIC(int, fw6_enable) = 1;
78 #define V_fw6_enable	VNET(fw6_enable)
79 #endif
80 
81 VNET_DEFINE_STATIC(int, fwlink_enable) = 0;
82 #define V_fwlink_enable	VNET(fwlink_enable)
83 
84 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
85 
86 /* Forward declarations. */
87 static int ipfw_divert(struct mbuf **, int, struct ipfw_rule_ref *, int);
88 int ipfw_check_packet(void *, struct mbuf **, struct ifnet *, int,
89 	struct inpcb *);
90 int ipfw_check_frame(void *, struct mbuf **, struct ifnet *, int,
91 	struct inpcb *);
92 
93 #ifdef SYSCTL_NODE
94 
95 SYSBEGIN(f1)
96 
97 SYSCTL_DECL(_net_inet_ip_fw);
98 SYSCTL_PROC(_net_inet_ip_fw, OID_AUTO, enable,
99     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
100     &VNET_NAME(fw_enable), 0, ipfw_chg_hook, "I", "Enable ipfw");
101 #ifdef INET6
102 SYSCTL_DECL(_net_inet6_ip6_fw);
103 SYSCTL_PROC(_net_inet6_ip6_fw, OID_AUTO, enable,
104     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
105     &VNET_NAME(fw6_enable), 0, ipfw_chg_hook, "I", "Enable ipfw+6");
106 #endif /* INET6 */
107 
108 SYSCTL_DECL(_net_link_ether);
109 SYSCTL_PROC(_net_link_ether, OID_AUTO, ipfw,
110     CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_SECURE3,
111     &VNET_NAME(fwlink_enable), 0, ipfw_chg_hook, "I",
112     "Pass ether pkts through firewall");
113 
114 SYSEND
115 
116 #endif /* SYSCTL_NODE */
117 
118 /*
119  * The pfilter hook to pass packets to ipfw_chk and then to
120  * dummynet, divert, netgraph or other modules.
121  * The packet may be consumed.
122  */
123 int
124 ipfw_check_packet(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
125     struct inpcb *inp)
126 {
127 	struct ip_fw_args args;
128 	struct m_tag *tag;
129 	int ipfw, ret;
130 
131 	/* convert dir to IPFW values */
132 	dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
133 	args.flags = 0;
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 		args.flags |= IPFW_ARGS_REF;
146 	}
147 
148 	args.m = *m0;
149 	args.oif = dir == DIR_OUT ? ifp : NULL;
150 	args.inp = inp;
151 
152 	ipfw = ipfw_chk(&args);
153 	*m0 = args.m;
154 
155 	KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
156 	    __func__));
157 
158 	/* breaking out of the switch means drop */
159 	switch (ipfw) {
160 	case IP_FW_PASS:
161 		/* next_hop may be set by ipfw_chk */
162 		if ((args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR |
163 		    IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) == 0) {
164 			ret = 0;
165 			break;
166 		}
167 #if (!defined(INET6) && !defined(INET))
168 		ret = EACCES;
169 #else
170 	    {
171 		void *psa;
172 		size_t len;
173 #ifdef INET
174 		if (args.flags & (IPFW_ARGS_NH4 | IPFW_ARGS_NH4PTR)) {
175 			MPASS((args.flags & (IPFW_ARGS_NH4 |
176 			    IPFW_ARGS_NH4PTR)) != (IPFW_ARGS_NH4 |
177 			    IPFW_ARGS_NH4PTR));
178 			MPASS((args.flags & (IPFW_ARGS_NH6 |
179 			    IPFW_ARGS_NH6PTR)) == 0);
180 			len = sizeof(struct sockaddr_in);
181 			psa = (args.flags & IPFW_ARGS_NH4) ?
182 			    &args.hopstore : args.next_hop;
183 			if (in_localip(satosin(psa)->sin_addr))
184 				(*m0)->m_flags |= M_FASTFWD_OURS;
185 			(*m0)->m_flags |= M_IP_NEXTHOP;
186 		}
187 #endif /* INET */
188 #ifdef INET6
189 		if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
190 			MPASS((args.flags & (IPFW_ARGS_NH6 |
191 			    IPFW_ARGS_NH6PTR)) != (IPFW_ARGS_NH6 |
192 			    IPFW_ARGS_NH6PTR));
193 			MPASS((args.flags & (IPFW_ARGS_NH4 |
194 			    IPFW_ARGS_NH4PTR)) == 0);
195 			len = sizeof(struct sockaddr_in6);
196 			psa = args.next_hop6;
197 			(*m0)->m_flags |= M_IP6_NEXTHOP;
198 		}
199 #endif /* INET6 */
200 		/*
201 		 * Incoming packets should not be tagged so we do not
202 		 * m_tag_find. Outgoing packets may be tagged, so we
203 		 * reuse the tag if present.
204 		 */
205 		tag = (dir == DIR_IN) ? NULL :
206 			m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
207 		if (tag != NULL) {
208 			m_tag_unlink(*m0, tag);
209 		} else {
210 			tag = m_tag_get(PACKET_TAG_IPFORWARD, len,
211 			    M_NOWAIT);
212 			if (tag == NULL) {
213 				ret = EACCES;
214 				break; /* i.e. drop */
215 			}
216 		}
217 		if ((args.flags & IPFW_ARGS_NH6) == 0)
218 			bcopy(psa, tag + 1, len);
219 		m_tag_prepend(*m0, tag);
220 		ret = 0;
221 #ifdef INET6
222 		/* IPv6 next hop needs additional handling */
223 		if (args.flags & (IPFW_ARGS_NH6 | IPFW_ARGS_NH6PTR)) {
224 			struct sockaddr_in6 *sa6;
225 
226 			sa6 = satosin6(tag + 1);
227 			if (args.flags & IPFW_ARGS_NH6) {
228 				sa6->sin6_family = AF_INET6;
229 				sa6->sin6_len = sizeof(*sa6);
230 				sa6->sin6_addr = args.hopstore6.sin6_addr;
231 				sa6->sin6_port = args.hopstore6.sin6_port;
232 				sa6->sin6_scope_id =
233 				    args.hopstore6.sin6_scope_id;
234 			}
235 			/*
236 			 * If nh6 address is link-local we should convert
237 			 * it to kernel internal form before doing any
238 			 * comparisons.
239 			 */
240 			if (sa6_embedscope(sa6, V_ip6_use_defzone) != 0) {
241 				ret = EACCES;
242 				break;
243 			}
244 			if (in6_localip(&sa6->sin6_addr))
245 				(*m0)->m_flags |= M_FASTFWD_OURS;
246 		}
247 #endif /* INET6 */
248 	    }
249 #endif /* INET || INET6 */
250 		break;
251 
252 	case IP_FW_DENY:
253 		ret = EACCES;
254 		break; /* i.e. drop */
255 
256 	case IP_FW_DUMMYNET:
257 		ret = EACCES;
258 		if (ip_dn_io_ptr == NULL)
259 			break; /* i.e. drop */
260 		MPASS(args.flags & IPFW_ARGS_REF);
261 		if (mtod(*m0, struct ip *)->ip_v == 4)
262 			ret = ip_dn_io_ptr(m0, dir, &args);
263 		else if (mtod(*m0, struct ip *)->ip_v == 6)
264 			ret = ip_dn_io_ptr(m0, dir | PROTO_IPV6, &args);
265 		else
266 			break; /* drop it */
267 		/*
268 		 * XXX should read the return value.
269 		 * dummynet normally eats the packet and sets *m0=NULL
270 		 * unless the packet can be sent immediately. In this
271 		 * case args is updated and we should re-run the
272 		 * check without clearing args.
273 		 */
274 		if (*m0 != NULL)
275 			goto again;
276 		break;
277 
278 	case IP_FW_TEE:
279 	case IP_FW_DIVERT:
280 		if (ip_divert_ptr == NULL) {
281 			ret = EACCES;
282 			break; /* i.e. drop */
283 		}
284 		MPASS(args.flags & IPFW_ARGS_REF);
285 		ret = ipfw_divert(m0, dir, &args.rule,
286 			(ipfw == IP_FW_TEE) ? 1 : 0);
287 		/* continue processing for the original packet (tee). */
288 		if (*m0)
289 			goto again;
290 		break;
291 
292 	case IP_FW_NGTEE:
293 	case IP_FW_NETGRAPH:
294 		if (ng_ipfw_input_p == NULL) {
295 			ret = EACCES;
296 			break; /* i.e. drop */
297 		}
298 		MPASS(args.flags & IPFW_ARGS_REF);
299 		ret = ng_ipfw_input_p(m0, dir, &args,
300 			(ipfw == IP_FW_NGTEE) ? 1 : 0);
301 		if (ipfw == IP_FW_NGTEE) /* ignore errors for NGTEE */
302 			goto again;	/* continue with packet */
303 		break;
304 
305 	case IP_FW_NAT:
306 		/* honor one-pass in case of successful nat */
307 		if (V_fw_one_pass) {
308 			ret = 0;
309 			break;
310 		}
311 		goto again;
312 
313 	case IP_FW_REASS:
314 		goto again;		/* continue with packet */
315 
316 	default:
317 		KASSERT(0, ("%s: unknown retval", __func__));
318 	}
319 
320 	if (ret != 0) {
321 		if (*m0)
322 			FREE_PKT(*m0);
323 		*m0 = NULL;
324 	}
325 
326 	return (ret);
327 }
328 
329 /*
330  * ipfw processing for ethernet packets (in and out).
331  */
332 int
333 ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
334     struct inpcb *inp)
335 {
336 	struct ip_fw_args args;
337 	struct ether_header save_eh;
338 	struct ether_header *eh;
339 	struct m_tag *mtag;
340 	struct mbuf *m;
341 	int i, ret;
342 
343 	args.flags = IPFW_ARGS_ETHER;
344 again:
345 	/* fetch start point from rule, if any.  remove the tag if present. */
346 	mtag = m_tag_locate(*m0, MTAG_IPFW_RULE, 0, NULL);
347 	if (mtag != NULL) {
348 		args.rule = *((struct ipfw_rule_ref *)(mtag+1));
349 		m_tag_delete(*m0, mtag);
350 		if (args.rule.info & IPFW_ONEPASS)
351 			return (0);
352 		args.flags |= IPFW_ARGS_REF;
353 	}
354 
355 	/* I need some amt of data to be contiguous */
356 	m = *m0;
357 	i = min(m->m_pkthdr.len, max_protohdr);
358 	if (m->m_len < i) {
359 		m = m_pullup(m, i);
360 		if (m == NULL) {
361 			*m0 = m;
362 			return (0);
363 		}
364 	}
365 	eh = mtod(m, struct ether_header *);
366 	save_eh = *eh;			/* save copy for restore below */
367 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
368 
369 	args.m = m;		/* the packet we are looking at		*/
370 	args.oif = dir == PFIL_OUT ? ifp: NULL;	/* destination, if any	*/
371 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
372 	args.inp = inp;	/* used by ipfw uid/gid/jail rules	*/
373 	i = ipfw_chk(&args);
374 	m = args.m;
375 	if (m != NULL) {
376 		/*
377 		 * Restore Ethernet header, as needed, in case the
378 		 * mbuf chain was replaced by ipfw.
379 		 */
380 		M_PREPEND(m, ETHER_HDR_LEN, M_NOWAIT);
381 		if (m == NULL) {
382 			*m0 = NULL;
383 			return (0);
384 		}
385 		if (eh != mtod(m, struct ether_header *))
386 			bcopy(&save_eh, mtod(m, struct ether_header *),
387 				ETHER_HDR_LEN);
388 	}
389 	*m0 = m;
390 
391 	ret = 0;
392 	/* Check result of ipfw_chk() */
393 	switch (i) {
394 	case IP_FW_PASS:
395 		break;
396 
397 	case IP_FW_DENY:
398 		ret = EACCES;
399 		break; /* i.e. drop */
400 
401 	case IP_FW_DUMMYNET:
402 		ret = EACCES;
403 
404 		if (ip_dn_io_ptr == NULL)
405 			break; /* i.e. drop */
406 
407 		*m0 = NULL;
408 		dir = (dir == PFIL_IN) ? DIR_IN : DIR_OUT;
409 		MPASS(args.flags & IPFW_ARGS_REF);
410 		ip_dn_io_ptr(&m, dir | PROTO_LAYER2, &args);
411 		return 0;
412 
413 	case IP_FW_NGTEE:
414 	case IP_FW_NETGRAPH:
415 		if (ng_ipfw_input_p == NULL) {
416 			ret = EACCES;
417 			break; /* i.e. drop */
418 		}
419 		MPASS(args.flags & IPFW_ARGS_REF);
420 		ret = ng_ipfw_input_p(m0, (dir == PFIL_IN) ? DIR_IN : DIR_OUT,
421 			&args, (i == IP_FW_NGTEE) ? 1 : 0);
422 		if (i == IP_FW_NGTEE) /* ignore errors for NGTEE */
423 			goto again;	/* continue with packet */
424 		break;
425 
426 	default:
427 		KASSERT(0, ("%s: unknown retval", __func__));
428 	}
429 
430 	if (ret != 0) {
431 		if (*m0)
432 			FREE_PKT(*m0);
433 		*m0 = NULL;
434 	}
435 
436 	return (ret);
437 }
438 
439 /* do the divert, return 1 on error 0 on success */
440 static int
441 ipfw_divert(struct mbuf **m0, int incoming, struct ipfw_rule_ref *rule,
442 	int tee)
443 {
444 	/*
445 	 * ipfw_chk() has already tagged the packet with the divert tag.
446 	 * If tee is set, copy packet and return original.
447 	 * If not tee, consume packet and send it to divert socket.
448 	 */
449 	struct mbuf *clone;
450 	struct ip *ip = mtod(*m0, struct ip *);
451 	struct m_tag *tag;
452 
453 	/* Cloning needed for tee? */
454 	if (tee == 0) {
455 		clone = *m0;	/* use the original mbuf */
456 		*m0 = NULL;
457 	} else {
458 		clone = m_dup(*m0, M_NOWAIT);
459 		/* If we cannot duplicate the mbuf, we sacrifice the divert
460 		 * chain and continue with the tee-ed packet.
461 		 */
462 		if (clone == NULL)
463 			return 1;
464 	}
465 
466 	/*
467 	 * Divert listeners can normally handle non-fragmented packets,
468 	 * but we can only reass in the non-tee case.
469 	 * This means that listeners on a tee rule may get fragments,
470 	 * and have to live with that.
471 	 * Note that we now have the 'reass' ipfw option so if we care
472 	 * we can do it before a 'tee'.
473 	 */
474 	if (!tee) switch (ip->ip_v) {
475 	case IPVERSION:
476 	    if (ntohs(ip->ip_off) & (IP_MF | IP_OFFMASK)) {
477 		int hlen;
478 		struct mbuf *reass;
479 
480 		reass = ip_reass(clone); /* Reassemble packet. */
481 		if (reass == NULL)
482 			return 0; /* not an error */
483 		/* if reass = NULL then it was consumed by ip_reass */
484 		/*
485 		 * IP header checksum fixup after reassembly and leave header
486 		 * in network byte order.
487 		 */
488 		ip = mtod(reass, struct ip *);
489 		hlen = ip->ip_hl << 2;
490 		ip->ip_sum = 0;
491 		if (hlen == sizeof(struct ip))
492 			ip->ip_sum = in_cksum_hdr(ip);
493 		else
494 			ip->ip_sum = in_cksum(reass, hlen);
495 		clone = reass;
496 	    }
497 	    break;
498 #ifdef INET6
499 	case IPV6_VERSION >> 4:
500 	    {
501 	    struct ip6_hdr *const ip6 = mtod(clone, struct ip6_hdr *);
502 
503 		if (ip6->ip6_nxt == IPPROTO_FRAGMENT) {
504 			int nxt, off;
505 
506 			off = sizeof(struct ip6_hdr);
507 			nxt = frag6_input(&clone, &off, 0);
508 			if (nxt == IPPROTO_DONE)
509 				return (0);
510 		}
511 		break;
512 	    }
513 #endif
514 	}
515 
516 	/* attach a tag to the packet with the reinject info */
517 	tag = m_tag_alloc(MTAG_IPFW_RULE, 0,
518 		    sizeof(struct ipfw_rule_ref), M_NOWAIT);
519 	if (tag == NULL) {
520 		FREE_PKT(clone);
521 		return 1;
522 	}
523 	*((struct ipfw_rule_ref *)(tag+1)) = *rule;
524 	m_tag_prepend(clone, tag);
525 
526 	/* Do the dirty job... */
527 	ip_divert_ptr(clone, incoming);
528 	return 0;
529 }
530 
531 /*
532  * attach or detach hooks for a given protocol family
533  */
534 static int
535 ipfw_hook(int onoff, int pf)
536 {
537 	struct pfil_head *pfh;
538 	pfil_func_t hook_func;
539 
540 	pfh = pfil_head_get(PFIL_TYPE_AF, pf);
541 	if (pfh == NULL)
542 		return ENOENT;
543 
544 	hook_func = (pf == AF_LINK) ? ipfw_check_frame : ipfw_check_packet;
545 
546 	(void) (onoff ? pfil_add_hook : pfil_remove_hook)
547 	    (hook_func, NULL, PFIL_IN | PFIL_OUT | PFIL_WAITOK, pfh);
548 
549 	return 0;
550 }
551 
552 int
553 ipfw_attach_hooks(int arg)
554 {
555 	int error = 0;
556 
557 	if (arg == 0) /* detach */
558 		ipfw_hook(0, AF_INET);
559 	else if (V_fw_enable && ipfw_hook(1, AF_INET) != 0) {
560                 error = ENOENT; /* see ip_fw_pfil.c::ipfw_hook() */
561                 printf("ipfw_hook() error\n");
562         }
563 #ifdef INET6
564 	if (arg == 0) /* detach */
565 		ipfw_hook(0, AF_INET6);
566 	else if (V_fw6_enable && ipfw_hook(1, AF_INET6) != 0) {
567                 error = ENOENT;
568                 printf("ipfw6_hook() error\n");
569         }
570 #endif
571 	if (arg == 0) /* detach */
572 		ipfw_hook(0, AF_LINK);
573 	else if (V_fwlink_enable && ipfw_hook(1, AF_LINK) != 0) {
574                 error = ENOENT;
575                 printf("ipfw_link_hook() error\n");
576         }
577 	return error;
578 }
579 
580 int
581 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
582 {
583 	int newval;
584 	int error;
585 	int af;
586 
587 	if (arg1 == &V_fw_enable)
588 		af = AF_INET;
589 #ifdef INET6
590 	else if (arg1 == &V_fw6_enable)
591 		af = AF_INET6;
592 #endif
593 	else if (arg1 == &V_fwlink_enable)
594 		af = AF_LINK;
595 	else
596 		return (EINVAL);
597 
598 	newval = *(int *)arg1;
599 	/* Handle sysctl change */
600 	error = sysctl_handle_int(oidp, &newval, 0, req);
601 
602 	if (error)
603 		return (error);
604 
605 	/* Formalize new value */
606 	newval = (newval) ? 1 : 0;
607 
608 	if (*(int *)arg1 == newval)
609 		return (0);
610 
611 	error = ipfw_hook(newval, af);
612 	if (error)
613 		return (error);
614 	*(int *)arg1 = newval;
615 
616 	return (0);
617 }
618 /* end of file */
619