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$");
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>
49 #include <sys/ucred.h>
52 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in_var.h>
58 #include <netinet/ip.h>
59 #include <netinet/ip_var.h>
60 #include <netinet/ip_fw.h>
61 #include <netinet/ip_divert.h>
62 #include <netinet/ip_dummynet.h>
64 #include <netgraph/ng_ipfw.h>
66 #include <machine/in_cksum.h>
73 int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
76 ip_dn_ruledel_t *ip_dn_ruledel_ptr = NULL;
79 ip_divert_packet_t *ip_divert_ptr = NULL;
82 ng_ipfw_input_t *ng_ipfw_input_p = NULL;
84 /* Forward declarations. */
85 static int ipfw_divert(struct mbuf **, int, int);
90 ipfw_check_in(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
93 struct ip_fw_args args;
94 struct ng_ipfw_tag *ng_tag;
99 #ifdef IPFIREWALL_FORWARD
100 struct m_tag *fwd_tag;
103 KASSERT(dir == PFIL_IN, ("ipfw_check_in wrong direction!"));
105 bzero(&args, sizeof(args));
107 dn_tag = m_tag_find(*m0, PACKET_TAG_DUMMYNET, NULL);
109 struct dn_pkt_tag *dt;
111 dt = (struct dn_pkt_tag *)(dn_tag+1);
112 args.rule = dt->rule;
114 m_tag_delete(*m0, dn_tag);
117 ng_tag = (struct ng_ipfw_tag *)m_tag_locate(*m0, NGM_IPFW_COOKIE, 0,
119 if (ng_tag != NULL) {
120 KASSERT(ng_tag->dir == NG_IPFW_IN,
121 ("ng_ipfw tag with wrong direction"));
122 args.rule = ng_tag->rule;
123 m_tag_delete(*m0, (struct m_tag *)ng_tag);
129 ipfw = ipfw_chk(&args);
133 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
138 if (args.next_hop == NULL)
141 #ifdef IPFIREWALL_FORWARD
142 fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
143 sizeof(struct sockaddr_in), M_NOWAIT);
146 bcopy(args.next_hop, (fwd_tag+1), sizeof(struct sockaddr_in));
147 m_tag_prepend(*m0, fwd_tag);
149 if (in_localip(args.next_hop->sin_addr))
150 (*m0)->m_flags |= M_FASTFWD_OURS;
153 break; /* not reached */
157 break; /* not reached */
160 if (!DUMMYNET_LOADED)
162 if (mtod(*m0, struct ip *)->ip_v == 4)
163 ip_dn_io_ptr(*m0, DN_TO_IP_IN, &args);
164 else if (mtod(*m0, struct ip *)->ip_v == 6)
165 ip_dn_io_ptr(*m0, DN_TO_IP6_IN, &args);
167 return 0; /* packet consumed */
174 divert = ipfw_divert(m0, DIV_DIR_IN, tee);
177 return 0; /* packet consumed */
180 goto again; /* continue with packet */
186 (void)ng_ipfw_input_p(m0, NG_IPFW_IN, &args, 1);
187 goto again; /* continue with packet */
192 return ng_ipfw_input_p(m0, NG_IPFW_IN, &args, 0);
195 goto again; /* continue with packet */
198 KASSERT(0, ("%s: unknown retval", __func__));
207 return 0; /* not filtered */
211 ipfw_check_out(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
214 struct ip_fw_args args;
215 struct ng_ipfw_tag *ng_tag;
216 struct m_tag *dn_tag;
220 #ifdef IPFIREWALL_FORWARD
221 struct m_tag *fwd_tag;
224 KASSERT(dir == PFIL_OUT, ("ipfw_check_out wrong direction!"));
226 bzero(&args, sizeof(args));
228 dn_tag = m_tag_find(*m0, PACKET_TAG_DUMMYNET, NULL);
229 if (dn_tag != NULL) {
230 struct dn_pkt_tag *dt;
232 dt = (struct dn_pkt_tag *)(dn_tag+1);
233 args.rule = dt->rule;
235 m_tag_delete(*m0, dn_tag);
238 ng_tag = (struct ng_ipfw_tag *)m_tag_locate(*m0, NGM_IPFW_COOKIE, 0,
240 if (ng_tag != NULL) {
241 KASSERT(ng_tag->dir == NG_IPFW_OUT,
242 ("ng_ipfw tag with wrong direction"));
243 args.rule = ng_tag->rule;
244 m_tag_delete(*m0, (struct m_tag *)ng_tag);
251 ipfw = ipfw_chk(&args);
255 KASSERT(*m0 != NULL || ipfw == IP_FW_DENY, ("%s: m0 is NULL",
260 if (args.next_hop == NULL)
262 #ifdef IPFIREWALL_FORWARD
263 /* Overwrite existing tag. */
264 fwd_tag = m_tag_find(*m0, PACKET_TAG_IPFORWARD, NULL);
265 if (fwd_tag == NULL) {
266 fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
267 sizeof(struct sockaddr_in), M_NOWAIT);
271 m_tag_unlink(*m0, fwd_tag);
272 bcopy(args.next_hop, (fwd_tag+1), sizeof(struct sockaddr_in));
273 m_tag_prepend(*m0, fwd_tag);
275 if (in_localip(args.next_hop->sin_addr))
276 (*m0)->m_flags |= M_FASTFWD_OURS;
279 break; /* not reached */
283 break; /* not reached */
286 if (!DUMMYNET_LOADED)
288 if (mtod(*m0, struct ip *)->ip_v == 4)
289 ip_dn_io_ptr(*m0, DN_TO_IP_OUT, &args);
290 else if (mtod(*m0, struct ip *)->ip_v == 6)
291 ip_dn_io_ptr(*m0, DN_TO_IP6_OUT, &args);
293 return 0; /* packet consumed */
302 divert = ipfw_divert(m0, DIV_DIR_OUT, tee);
305 return 0; /* packet consumed */
308 goto again; /* continue with packet */
314 (void)ng_ipfw_input_p(m0, NG_IPFW_OUT, &args, 1);
315 goto again; /* continue with packet */
320 return ng_ipfw_input_p(m0, NG_IPFW_OUT, &args, 0);
323 goto again; /* continue with packet */
326 KASSERT(0, ("%s: unknown retval", __func__));
335 return 0; /* not filtered */
339 ipfw_divert(struct mbuf **m, int incoming, int tee)
342 * ipfw_chk() has already tagged the packet with the divert tag.
343 * If tee is set, copy packet and return original.
344 * If not tee, consume packet and send it to divert socket.
346 struct mbuf *clone, *reass;
352 /* Is divert module loaded? */
353 if (ip_divert_ptr == NULL)
356 /* Cloning needed for tee? */
358 clone = m_dup(*m, M_DONTWAIT);
362 /* In case m_dup was unable to allocate mbufs. */
367 * Divert listeners can only handle non-fragmented packets.
368 * However when tee is set we will *not* de-fragment the packets;
369 * Doing do would put the reassembly into double-jeopardy. On top
370 * of that someone doing a tee will probably want to get the packet
371 * in its original form.
373 ip = mtod(clone, struct ip *);
374 if (!tee && ip->ip_off & (IP_MF | IP_OFFMASK)) {
376 /* Reassemble packet. */
377 reass = ip_reass(clone);
380 * IP header checksum fixup after reassembly and leave header
381 * in network byte order.
384 ip = mtod(reass, struct ip *);
385 hlen = ip->ip_hl << 2;
386 ip->ip_len = htons(ip->ip_len);
387 ip->ip_off = htons(ip->ip_off);
389 if (hlen == sizeof(struct ip))
390 ip->ip_sum = in_cksum_hdr(ip);
392 ip->ip_sum = in_cksum(reass, hlen);
397 /* Convert header to network byte order. */
398 ip->ip_len = htons(ip->ip_len);
399 ip->ip_off = htons(ip->ip_off);
402 /* Do the dirty job... */
403 if (clone && ip_divert_ptr != NULL)
404 ip_divert_ptr(clone, incoming);
408 * For tee we leave the divert tag attached to original packet.
409 * It will then continue rule evaluation after the tee rule.
414 /* Packet diverted and consumed */
425 struct pfil_head *pfh_inet;
427 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
428 if (pfh_inet == NULL)
431 pfil_add_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
432 pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
440 struct pfil_head *pfh_inet;
442 pfh_inet = pfil_head_get(PFIL_TYPE_AF, AF_INET);
443 if (pfh_inet == NULL)
446 pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet);
447 pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet);
456 struct pfil_head *pfh_inet6;
458 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
459 if (pfh_inet6 == NULL)
462 pfil_add_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
463 pfil_add_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
471 struct pfil_head *pfh_inet6;
473 pfh_inet6 = pfil_head_get(PFIL_TYPE_AF, AF_INET6);
474 if (pfh_inet6 == NULL)
477 pfil_remove_hook(ipfw_check_in, NULL, PFIL_IN | PFIL_WAITOK, pfh_inet6);
478 pfil_remove_hook(ipfw_check_out, NULL, PFIL_OUT | PFIL_WAITOK, pfh_inet6);
485 ipfw_chg_hook(SYSCTL_HANDLER_ARGS)
487 int enable = *(int *)arg1;
490 error = sysctl_handle_int(oidp, &enable, 0, req);
494 enable = (enable) ? 1 : 0;
496 if (enable == *(int *)arg1)
499 if (arg1 == &fw_enable) {
503 error = ipfw_unhook();
506 if (arg1 == &fw6_enable) {
508 error = ipfw6_hook();
510 error = ipfw6_unhook();
517 *(int *)arg1 = enable;
523 ipfw_modevent(module_t mod, int type, void *unused)
529 if ((err = ipfw_init()) != 0) {
530 printf("ipfw_init() error\n");
533 if ((err = ipfw_hook()) != 0) {
534 printf("ipfw_hook() error\n");
538 if ((err = ipfw6_hook()) != 0) {
539 printf("ipfw_hook() error\n");
546 if ((err = ipfw_unhook()) > 0)
549 if ((err = ipfw6_unhook()) > 0)
562 static moduledata_t ipfwmod = {
567 DECLARE_MODULE(ipfw, ipfwmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
568 MODULE_VERSION(ipfw, 2);