2 * Copyright (c) 2004 Andre Oppermann, Internet Business Solutions AG
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: src/sys/netinet/ip_fw_pfil.c,v 1.25.2.2 2008/04/25 10:26:30 oleg Exp $");
30 #if !defined(KLD_MODULE)
35 #error IPFIREWALL requires INET.
37 #endif /* KLD_MODULE */
38 #include "opt_inet6.h"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/kernel.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/sysctl.h>
56 #include <netinet/in.h>
57 #include <netinet/ip.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/ip_fw.h>
60 #include <netinet/ip_divert.h>
61 #include <netinet/ip_dummynet.h>
63 #include <netgraph/ng_ipfw.h>
65 #include <machine/in_cksum.h>
67 VNET_DEFINE(int, fw_enable) = 1;
69 VNET_DEFINE(int, fw6_enable) = 1;
72 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
75 ip_divert_packet_t *ip_divert_ptr = NULL;
78 ng_ipfw_input_t *ng_ipfw_input_p = NULL;
80 /* Forward declarations. */
81 static int ipfw_divert(struct mbuf **, int, int);
86 ipfw_check_in(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
89 struct ip_fw_args args;
90 struct ng_ipfw_tag *ng_tag;
95 #ifdef IPFIREWALL_FORWARD
96 struct m_tag *fwd_tag;
99 KASSERT(dir == PFIL_IN, ("ipfw_check_in wrong direction!"));
101 bzero(&args, sizeof(args));
103 ng_tag = (struct ng_ipfw_tag *)m_tag_locate(*m0, NGM_IPFW_COOKIE, 0,
105 if (ng_tag != NULL) {
106 KASSERT(ng_tag->dir == NG_IPFW_IN,
107 ("ng_ipfw tag with wrong direction"));
108 args.rule = ng_tag->rule;
109 args.rule_id = ng_tag->rule_id;
110 args.chain_id = ng_tag->chain_id;
111 m_tag_delete(*m0, (struct m_tag *)ng_tag);
115 dn_tag = m_tag_find(*m0, PACKET_TAG_DUMMYNET, NULL);
117 struct dn_pkt_tag *dt;
119 dt = (struct dn_pkt_tag *)(dn_tag+1);
120 args.rule = dt->rule;
121 args.rule_id = dt->rule_id;
122 args.chain_id = dt->chain_id;
124 m_tag_delete(*m0, dn_tag);
131 if (V_fw_one_pass == 0 || args.rule == NULL) {
132 ipfw = ipfw_chk(&args);
137 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
142 if (args.next_hop == NULL)
145 #ifdef IPFIREWALL_FORWARD
146 fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
147 sizeof(struct sockaddr_in), M_NOWAIT);
150 bcopy(args.next_hop, (fwd_tag+1), sizeof(struct sockaddr_in));
151 m_tag_prepend(*m0, fwd_tag);
153 if (in_localip(args.next_hop->sin_addr))
154 (*m0)->m_flags |= M_FASTFWD_OURS;
157 break; /* not reached */
161 break; /* not reached */
164 if (ip_dn_io_ptr == NULL)
166 if (mtod(*m0, struct ip *)->ip_v == 4)
167 ip_dn_io_ptr(m0, DN_TO_IP_IN, &args);
168 else if (mtod(*m0, struct ip *)->ip_v == 6)
169 ip_dn_io_ptr(m0, DN_TO_IP6_IN, &args);
172 return 0; /* packet consumed */
179 divert = ipfw_divert(m0, DIV_DIR_IN, tee);
182 return 0; /* packet consumed */
185 goto again; /* continue with packet */
191 (void)ng_ipfw_input_p(m0, NG_IPFW_IN, &args, 1);
192 goto again; /* continue with packet */
197 return ng_ipfw_input_p(m0, NG_IPFW_IN, &args, 0);
200 goto again; /* continue with packet */
206 KASSERT(0, ("%s: unknown retval", __func__));
215 return 0; /* not filtered */
219 ipfw_check_out(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
222 struct ip_fw_args args;
223 struct ng_ipfw_tag *ng_tag;
224 struct m_tag *dn_tag;
228 #ifdef IPFIREWALL_FORWARD
229 struct m_tag *fwd_tag;
232 KASSERT(dir == PFIL_OUT, ("ipfw_check_out wrong direction!"));
234 bzero(&args, sizeof(args));
236 ng_tag = (struct ng_ipfw_tag *)m_tag_locate(*m0, NGM_IPFW_COOKIE, 0,
238 if (ng_tag != NULL) {
239 KASSERT(ng_tag->dir == NG_IPFW_OUT,
240 ("ng_ipfw tag with wrong direction"));
241 args.rule = ng_tag->rule;
242 args.rule_id = ng_tag->rule_id;
243 args.chain_id = ng_tag->chain_id;
244 m_tag_delete(*m0, (struct m_tag *)ng_tag);
248 dn_tag = m_tag_find(*m0, PACKET_TAG_DUMMYNET, NULL);
249 if (dn_tag != NULL) {
250 struct dn_pkt_tag *dt;
252 dt = (struct dn_pkt_tag *)(dn_tag+1);
253 args.rule = dt->rule;
254 args.rule_id = dt->rule_id;
255 args.chain_id = dt->chain_id;
257 m_tag_delete(*m0, dn_tag);
265 if (V_fw_one_pass == 0 || args.rule == NULL) {
266 ipfw = ipfw_chk(&args);
271 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
276 if (args.next_hop == NULL)
278 #ifdef IPFIREWALL_FORWARD
279 /* Overwrite existing tag. */
280 fwd_tag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
281 if (fwd_tag == NULL) {
282 fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
283 sizeof(struct sockaddr_in), M_NOWAIT);
287 m_tag_unlink(*m0, fwd_tag);
288 bcopy(args.next_hop, (fwd_tag+1), sizeof(struct sockaddr_in));
289 m_tag_prepend(*m0, fwd_tag);
291 if (in_localip(args.next_hop->sin_addr))
292 (*m0)->m_flags |= M_FASTFWD_OURS;
295 break; /* not reached */
299 break; /* not reached */
302 if (ip_dn_io_ptr == NULL)
304 if (mtod(*m0, struct ip *)->ip_v == 4)
305 ip_dn_io_ptr(m0, DN_TO_IP_OUT, &args);
306 else if (mtod(*m0, struct ip *)->ip_v == 6)
307 ip_dn_io_ptr(m0, DN_TO_IP6_OUT, &args);
310 return 0; /* packet consumed */
319 divert = ipfw_divert(m0, DIV_DIR_OUT, tee);
322 return 0; /* packet consumed */
325 goto again; /* continue with packet */
331 (void)ng_ipfw_input_p(m0, NG_IPFW_OUT, &args, 1);
332 goto again; /* continue with packet */
337 return ng_ipfw_input_p(m0, NG_IPFW_OUT, &args, 0);
340 goto again; /* continue with packet */
346 KASSERT(0, ("%s: unknown retval", __func__));
355 return 0; /* not filtered */
359 ipfw_divert(struct mbuf **m, int incoming, int tee)
362 * ipfw_chk() has already tagged the packet with the divert tag.
363 * If tee is set, copy packet and return original.
364 * If not tee, consume packet and send it to divert socket.
366 struct mbuf *clone, *reass;
372 /* Is divert module loaded? */
373 if (ip_divert_ptr == NULL)
376 /* Cloning needed for tee? */
378 clone = m_dup(*m, M_DONTWAIT);
382 /* In case m_dup was unable to allocate mbufs. */
387 * Divert listeners can only handle non-fragmented packets.
388 * However when tee is set we will *not* de-fragment the packets;
389 * Doing do would put the reassembly into double-jeopardy. On top
390 * of that someone doing a tee will probably want to get the packet
391 * in its original form.
393 ip = mtod(clone, struct ip *);
394 if (!tee && ip->ip_off & (IP_MF | IP_OFFMASK)) {
396 /* Reassemble packet. */
397 reass = ip_reass(clone);
400 * IP header checksum fixup after reassembly and leave header
401 * in network byte order.
404 ip = mtod(reass, struct ip *);
405 hlen = ip->ip_hl << 2;
406 ip->ip_len = htons(ip->ip_len);
407 ip->ip_off = htons(ip->ip_off);
409 if (hlen == sizeof(struct ip))
410 ip->ip_sum = in_cksum_hdr(ip);
412 ip->ip_sum = in_cksum(reass, hlen);
417 /* Convert header to network byte order. */
418 ip->ip_len = htons(ip->ip_len);
419 ip->ip_off = htons(ip->ip_off);
422 /* Do the dirty job... */
423 if (clone && ip_divert_ptr != NULL)
424 ip_divert_ptr(clone, incoming);
428 * For tee we leave the divert tag attached to original packet.
429 * It will then continue rule evaluation after the tee rule.
434 /* Packet diverted and consumed */
445 struct pfil_head *pfh_inet;
447 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
448 if (pfh_inet == NULL)
451 (void)pfil_add_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
453 (void)pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
462 struct pfil_head *pfh_inet;
464 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
465 if (pfh_inet == NULL)
468 (void)pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
470 (void)pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
480 struct pfil_head *pfh_inet6;
482 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
483 if (pfh_inet6 == NULL)
486 (void)pfil_add_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
488 (void)pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
497 struct pfil_head *pfh_inet6;
499 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
500 if (pfh_inet6 == NULL)
503 (void)pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK,
505 (void)pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK,
513 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
519 if (arg1 == &VNET_NAME(fw_enable)) {
520 enable = V_fw_enable;
523 else if (arg1 == &VNET_NAME(fw6_enable)) {
524 enable = V_fw6_enable;
532 error = sysctl_handle_int(oidp, &enable, 0, req);
537 enable = (enable) ? 1 : 0;
539 if (enable == oldenable)
542 if (arg1 == &VNET_NAME(fw_enable)) {
546 error = ipfw_unhook();
549 V_fw_enable = enable;
552 else if (arg1 == &VNET_NAME(fw6_enable)) {
554 error = ipfw6_hook();
556 error = ipfw6_unhook();
559 V_fw6_enable = enable;
567 ipfw_modevent(module_t mod, int type, void *unused)
573 if ((err = ipfw_init()) != 0) {
574 printf("ipfw_init() error\n");
577 if ((err = ipfw_hook()) != 0) {
578 printf("ipfw_hook() error\n");
582 if ((err = ipfw6_hook()) != 0) {
583 printf("ipfw_hook() error\n");
590 if ((err = ipfw_unhook()) > 0)
593 if ((err = ipfw6_unhook()) > 0)
606 static moduledata_t ipfwmod = {
611 DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY - 256);
612 MODULE_VERSION(ipfw, 2);