2 * Copyright (c) 2008 Paolo Pisati
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 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/condvar.h>
33 #include <sys/eventhandler.h>
34 #include <sys/malloc.h>
36 #include <sys/kernel.h>
39 #include <sys/module.h>
42 #include <sys/rwlock.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/syslog.h>
47 #include <sys/ucred.h>
49 #include <netinet/libalias/alias.h>
50 #include <netinet/libalias/alias_local.h>
52 #define IPFW_INTERNAL /* Access to protected data structures in ip_fw.h. */
55 #include <netinet/in.h>
56 #include <netinet/ip.h>
57 #include <netinet/ip_var.h>
58 #include <netinet/ip_icmp.h>
59 #include <netinet/ip_fw.h>
60 #include <netinet/tcp.h>
61 #include <netinet/tcp_timer.h>
62 #include <netinet/tcp_var.h>
63 #include <netinet/tcpip.h>
64 #include <netinet/udp.h>
65 #include <netinet/udp_var.h>
67 #include <machine/in_cksum.h> /* XXX for in_cksum */
69 MALLOC_DECLARE(M_IPFW);
71 static VNET_DEFINE(eventhandler_tag, ifaddr_event_tag);
72 #define V_ifaddr_event_tag VNET(ifaddr_event_tag)
74 extern ipfw_nat_t *ipfw_nat_ptr;
75 extern ipfw_nat_cfg_t *ipfw_nat_cfg_ptr;
76 extern ipfw_nat_cfg_t *ipfw_nat_del_ptr;
77 extern ipfw_nat_cfg_t *ipfw_nat_get_cfg_ptr;
78 extern ipfw_nat_cfg_t *ipfw_nat_get_log_ptr;
81 ifaddr_change(void *arg __unused, struct ifnet *ifp)
86 IPFW_WLOCK(&V_layer3_chain);
87 /* Check every nat entry... */
88 LIST_FOREACH(ptr, &V_layer3_chain.nat, _next) {
89 /* ...using nic 'ifp->if_xname' as dynamic alias address. */
90 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) == 0) {
92 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
93 if (ifa->ifa_addr == NULL)
95 if (ifa->ifa_addr->sa_family != AF_INET)
97 ptr->ip = ((struct sockaddr_in *)
98 (ifa->ifa_addr))->sin_addr;
99 LibAliasSetAddress(ptr->lib, ptr->ip);
101 if_addr_runlock(ifp);
104 IPFW_WUNLOCK(&V_layer3_chain);
108 flush_nat_ptrs(const int i)
112 IPFW_WLOCK_ASSERT(&V_layer3_chain);
113 for (rule = V_layer3_chain.rules; rule; rule = rule->next) {
114 ipfw_insn_nat *cmd = (ipfw_insn_nat *)ACTION_PTR(rule);
115 if (cmd->o.opcode != O_NAT)
117 if (cmd->nat != NULL && cmd->nat->id == i)
122 #define HOOK_NAT(b, p) do { \
123 IPFW_WLOCK_ASSERT(&V_layer3_chain); \
124 LIST_INSERT_HEAD(b, p, _next); \
127 #define UNHOOK_NAT(p) do { \
128 IPFW_WLOCK_ASSERT(&V_layer3_chain); \
129 LIST_REMOVE(p, _next); \
132 #define HOOK_REDIR(b, p) do { \
133 LIST_INSERT_HEAD(b, p, _next); \
136 #define HOOK_SPOOL(b, p) do { \
137 LIST_INSERT_HEAD(b, p, _next); \
141 del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head)
143 struct cfg_redir *r, *tmp_r;
144 struct cfg_spool *s, *tmp_s;
147 LIST_FOREACH_SAFE(r, head, _next, tmp_r) {
148 num = 1; /* Number of alias_link to delete. */
155 /* Delete all libalias redirect entry. */
156 for (i = 0; i < num; i++)
157 LibAliasRedirectDelete(n->lib, r->alink[i]);
158 /* Del spool cfg if any. */
159 LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) {
160 LIST_REMOVE(s, _next);
163 free(r->alink, M_IPFW);
164 LIST_REMOVE(r, _next);
168 printf("unknown redirect mode: %u\n", r->mode);
169 /* XXX - panic?!?!? */
176 add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
178 struct cfg_redir *r, *ser_r;
179 struct cfg_spool *s, *ser_s;
183 for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) {
184 ser_r = (struct cfg_redir *)&buf[off];
185 r = malloc(SOF_REDIR, M_IPFW, M_WAITOK | M_ZERO);
186 memcpy(r, ser_r, SOF_REDIR);
187 LIST_INIT(&r->spool_chain);
189 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt,
190 M_IPFW, M_WAITOK | M_ZERO);
193 r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr,
197 for (i = 0 ; i < r->pport_cnt; i++) {
198 /* If remotePort is all ports, set it to 0. */
199 u_short remotePortCopy = r->rport + i;
200 if (r->rport_cnt == 1 && r->rport == 0)
202 r->alink[i] = LibAliasRedirectPort(ptr->lib,
203 r->laddr, htons(r->lport + i), r->raddr,
204 htons(remotePortCopy), r->paddr,
205 htons(r->pport + i), r->proto);
206 if (r->alink[i] == NULL) {
213 r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr,
214 r->raddr, r->paddr, r->proto);
217 printf("unknown redirect mode: %u\n", r->mode);
220 if (r->alink[0] == NULL) {
221 panic_err = "LibAliasRedirect* returned NULL";
223 } else /* LSNAT handling. */
224 for (i = 0; i < r->spool_cnt; i++) {
225 ser_s = (struct cfg_spool *)&buf[off];
226 s = malloc(SOF_REDIR, M_IPFW,
228 memcpy(s, ser_s, SOF_SPOOL);
229 LibAliasAddServer(ptr->lib, r->alink[0],
230 s->addr, htons(s->port));
232 /* Hook spool entry. */
233 HOOK_SPOOL(&r->spool_chain, s);
235 /* And finally hook this redir entry. */
236 HOOK_REDIR(&ptr->redir_chain, r);
240 /* something really bad happened: panic! */
241 panic("%s\n", panic_err);
245 ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m)
249 /* XXX - libalias duct tape */
255 if ((mcl = m_megapullup(m, m->m_pkthdr.len)) ==
258 ip = mtod(mcl, struct ip *);
259 if (args->eh == NULL) {
260 ip->ip_len = htons(ip->ip_len);
261 ip->ip_off = htons(ip->ip_off);
265 * XXX - Libalias checksum offload 'duct tape':
267 * locally generated packets have only
268 * pseudo-header checksum calculated
269 * and libalias will screw it[1], so
270 * mark them for later fix. Moreover
271 * there are cases when libalias
272 * modify tcp packet data[2], mark it
275 * [1] libalias was never meant to run
276 * in kernel, so it doesn't have any
277 * knowledge about checksum
278 * offloading, and it expects a packet
279 * with a full internet
280 * checksum. Unfortunately, packets
281 * generated locally will have just the
282 * pseudo header calculated, and when
283 * libalias tries to adjust the
284 * checksum it will actually screw it.
286 * [2] when libalias modify tcp's data
287 * content, full TCP checksum has to
288 * be recomputed: the problem is that
289 * libalias doesn't have any idea
290 * about checksum offloading To
291 * workaround this, we do not do
292 * checksumming in LibAlias, but only
293 * mark the packets in th_x2 field. If
294 * we receive a marked packet, we
295 * calculate correct checksum for it
296 * aware of offloading. Why such a
297 * terrible hack instead of
298 * recalculating checksum for each
299 * packet? Because the previous
300 * checksum was not checked!
301 * Recalculating checksums for EVERY
302 * packet will hide ALL transmission
303 * errors. Yes, marked packets still
304 * suffer from this problem. But,
305 * sigh, natd(8) has this problem,
308 * TODO: -make libalias mbuf aware (so
309 * it can handle delayed checksum and tso)
312 if (mcl->m_pkthdr.rcvif == NULL &&
313 mcl->m_pkthdr.csum_flags &
317 c = mtod(mcl, char *);
318 if (args->oif == NULL)
319 retval = LibAliasIn(t->lib, c,
320 mcl->m_len + M_TRAILINGSPACE(mcl));
322 retval = LibAliasOut(t->lib, c,
323 mcl->m_len + M_TRAILINGSPACE(mcl));
324 if (retval == PKT_ALIAS_RESPOND) {
325 m->m_flags |= M_SKIP_FIREWALL;
326 retval = PKT_ALIAS_OK;
328 if (retval != PKT_ALIAS_OK &&
329 retval != PKT_ALIAS_FOUND_HEADER_FRAGMENT) {
330 /* XXX - should i add some logging? */
336 mcl->m_pkthdr.len = mcl->m_len =
340 * XXX - libalias checksum offload
341 * 'duct tape' (see above)
344 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
345 ip->ip_p == IPPROTO_TCP) {
348 th = (struct tcphdr *)(ip + 1);
358 ip->ip_len = ntohs(ip->ip_len);
362 htons(ip->ip_p + ip->ip_len - (ip->ip_hl << 2))
367 th = (struct tcphdr *)(ip + 1);
369 * Maybe it was set in
374 mcl->m_pkthdr.csum_data =
375 offsetof(struct tcphdr, th_sum);
378 uh = (struct udphdr *)(ip + 1);
380 mcl->m_pkthdr.csum_data =
381 offsetof(struct udphdr, uh_sum);
385 * No hw checksum offloading: do it
388 if ((mcl->m_pkthdr.csum_flags &
389 CSUM_DELAY_DATA) == 0) {
390 in_delayed_cksum(mcl);
391 mcl->m_pkthdr.csum_flags &=
394 ip->ip_len = htons(ip->ip_len);
397 if (args->eh == NULL) {
398 ip->ip_len = ntohs(ip->ip_len);
399 ip->ip_off = ntohs(ip->ip_off);
407 ipfw_nat_cfg(struct sockopt *sopt)
409 struct cfg_nat *ptr, *ser_n;
412 buf = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
413 sooptcopyin(sopt, buf, NAT_BUF_LEN,
414 sizeof(struct cfg_nat));
415 ser_n = (struct cfg_nat *)buf;
418 * Find/create nat rule.
420 IPFW_WLOCK(&V_layer3_chain);
421 LOOKUP_NAT(V_layer3_chain, ser_n->id, ptr);
423 /* New rule: allocate and init new instance. */
424 ptr = malloc(sizeof(struct cfg_nat),
425 M_IPFW, M_NOWAIT | M_ZERO);
427 IPFW_WUNLOCK(&V_layer3_chain);
431 ptr->lib = LibAliasInit(NULL);
432 if (ptr->lib == NULL) {
433 IPFW_WUNLOCK(&V_layer3_chain);
438 LIST_INIT(&ptr->redir_chain);
440 /* Entry already present: temporarly unhook it. */
442 flush_nat_ptrs(ser_n->id);
444 IPFW_WUNLOCK(&V_layer3_chain);
447 * Basic nat configuration.
451 * XXX - what if this rule doesn't nat any ip and just
453 * do we set aliasaddress to 0.0.0.0?
456 ptr->redir_cnt = ser_n->redir_cnt;
457 ptr->mode = ser_n->mode;
458 LibAliasSetMode(ptr->lib, ser_n->mode, ser_n->mode);
459 LibAliasSetAddress(ptr->lib, ptr->ip);
460 memcpy(ptr->if_name, ser_n->if_name, IF_NAMESIZE);
463 * Redir and LSNAT configuration.
465 /* Delete old cfgs. */
466 del_redir_spool_cfg(ptr, &ptr->redir_chain);
467 /* Add new entries. */
468 add_redir_spool_cfg(&buf[(sizeof(struct cfg_nat))], ptr);
470 IPFW_WLOCK(&V_layer3_chain);
471 HOOK_NAT(&V_layer3_chain.nat, ptr);
472 IPFW_WUNLOCK(&V_layer3_chain);
477 ipfw_nat_del(struct sockopt *sopt)
482 sooptcopyin(sopt, &i, sizeof i, sizeof i);
483 IPFW_WLOCK(&V_layer3_chain);
484 LOOKUP_NAT(V_layer3_chain, i, ptr);
486 IPFW_WUNLOCK(&V_layer3_chain);
491 IPFW_WUNLOCK(&V_layer3_chain);
492 del_redir_spool_cfg(ptr, &ptr->redir_chain);
493 LibAliasUninit(ptr->lib);
499 ipfw_nat_get_cfg(struct sockopt *sopt)
508 off = sizeof(nat_cnt);
510 data = malloc(NAT_BUF_LEN, M_IPFW, M_WAITOK | M_ZERO);
511 IPFW_RLOCK(&V_layer3_chain);
512 /* Serialize all the data. */
513 LIST_FOREACH(n, &V_layer3_chain.nat, _next) {
515 if (off + SOF_NAT < NAT_BUF_LEN) {
516 bcopy(n, &data[off], SOF_NAT);
518 LIST_FOREACH(r, &n->redir_chain, _next) {
519 if (off + SOF_REDIR < NAT_BUF_LEN) {
523 LIST_FOREACH(s, &r->spool_chain,
525 if (off + SOF_SPOOL <
539 bcopy(&nat_cnt, data, sizeof(nat_cnt));
540 IPFW_RUNLOCK(&V_layer3_chain);
541 sooptcopyout(sopt, data, NAT_BUF_LEN);
545 IPFW_RUNLOCK(&V_layer3_chain);
546 printf("serialized data buffer not big enough:"
547 "please increase NAT_BUF_LEN\n");
553 ipfw_nat_get_log(struct sockopt *sopt)
557 int i, size, cnt, sof;
560 sof = LIBALIAS_BUF_SIZE;
563 IPFW_RLOCK(&V_layer3_chain);
565 LIST_FOREACH(ptr, &V_layer3_chain.nat, _next) {
566 if (ptr->lib->logDesc == NULL)
569 size = cnt * (sof + sizeof(int));
570 data = realloc(data, size, M_IPFW, M_NOWAIT | M_ZERO);
572 IPFW_RUNLOCK(&V_layer3_chain);
575 bcopy(&ptr->id, &data[i], sizeof(int));
577 bcopy(ptr->lib->logDesc, &data[i], sof);
580 IPFW_RUNLOCK(&V_layer3_chain);
581 sooptcopyout(sopt, data, size);
590 IPFW_WLOCK(&V_layer3_chain);
591 /* init ipfw hooks */
592 ipfw_nat_ptr = ipfw_nat;
593 ipfw_nat_cfg_ptr = ipfw_nat_cfg;
594 ipfw_nat_del_ptr = ipfw_nat_del;
595 ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg;
596 ipfw_nat_get_log_ptr = ipfw_nat_get_log;
597 IPFW_WUNLOCK(&V_layer3_chain);
598 V_ifaddr_event_tag = EVENTHANDLER_REGISTER(ifaddr_event, ifaddr_change,
599 NULL, EVENTHANDLER_PRI_ANY);
603 ipfw_nat_destroy(void)
606 struct cfg_nat *ptr, *ptr_temp;
608 IPFW_WLOCK(&V_layer3_chain);
609 LIST_FOREACH_SAFE(ptr, &V_layer3_chain.nat, _next, ptr_temp) {
610 LIST_REMOVE(ptr, _next);
611 del_redir_spool_cfg(ptr, &ptr->redir_chain);
612 LibAliasUninit(ptr->lib);
615 EVENTHANDLER_DEREGISTER(ifaddr_event, V_ifaddr_event_tag);
616 /* flush all nat ptrs */
617 for (rule = V_layer3_chain.rules; rule; rule = rule->next) {
618 ipfw_insn_nat *cmd = (ipfw_insn_nat *)ACTION_PTR(rule);
619 if (cmd->o.opcode == O_NAT)
622 /* deregister ipfw_nat */
624 IPFW_WUNLOCK(&V_layer3_chain);
628 ipfw_nat_modevent(module_t mod, int type, void *unused)
648 static moduledata_t ipfw_nat_mod = {
654 DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY);
655 MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1);
656 MODULE_DEPEND(ipfw_nat, ipfw, 2, 2, 2);
657 MODULE_VERSION(ipfw_nat, 1);