2 * Copyright 2005, Gleb Smirnoff <glebius@FreeBSD.org>
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
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
33 #include <sys/malloc.h>
34 #include <sys/ctype.h>
35 #include <sys/errno.h>
36 #include <sys/syslog.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/ip_var.h>
42 #include <netinet/tcp.h>
43 #include <machine/in_cksum.h>
45 #include <netinet/libalias/alias.h>
46 #include <netinet/libalias/alias_local.h>
48 #include <netgraph/ng_message.h>
49 #include <netgraph/ng_parse.h>
50 #include <netgraph/ng_nat.h>
51 #include <netgraph/netgraph.h>
53 static ng_constructor_t ng_nat_constructor;
54 static ng_rcvmsg_t ng_nat_rcvmsg;
55 static ng_shutdown_t ng_nat_shutdown;
56 static ng_newhook_t ng_nat_newhook;
57 static ng_rcvdata_t ng_nat_rcvdata;
58 static ng_disconnect_t ng_nat_disconnect;
60 static unsigned int ng_nat_translate_flags(unsigned int x);
62 /* Parse type for struct ng_nat_mode. */
63 static const struct ng_parse_struct_field ng_nat_mode_fields[]
65 static const struct ng_parse_type ng_nat_mode_type = {
66 &ng_parse_struct_type,
70 /* Parse type for 'description' field in structs. */
71 static const struct ng_parse_fixedstring_info ng_nat_description_info
72 = { NG_NAT_DESC_LENGTH };
73 static const struct ng_parse_type ng_nat_description_type = {
74 &ng_parse_fixedstring_type,
75 &ng_nat_description_info
78 /* Parse type for struct ng_nat_redirect_port. */
79 static const struct ng_parse_struct_field ng_nat_redirect_port_fields[]
80 = NG_NAT_REDIRECT_PORT_TYPE_INFO(&ng_nat_description_type);
81 static const struct ng_parse_type ng_nat_redirect_port_type = {
82 &ng_parse_struct_type,
83 &ng_nat_redirect_port_fields
86 /* Parse type for struct ng_nat_redirect_addr. */
87 static const struct ng_parse_struct_field ng_nat_redirect_addr_fields[]
88 = NG_NAT_REDIRECT_ADDR_TYPE_INFO(&ng_nat_description_type);
89 static const struct ng_parse_type ng_nat_redirect_addr_type = {
90 &ng_parse_struct_type,
91 &ng_nat_redirect_addr_fields
94 /* Parse type for struct ng_nat_redirect_proto. */
95 static const struct ng_parse_struct_field ng_nat_redirect_proto_fields[]
96 = NG_NAT_REDIRECT_PROTO_TYPE_INFO(&ng_nat_description_type);
97 static const struct ng_parse_type ng_nat_redirect_proto_type = {
98 &ng_parse_struct_type,
99 &ng_nat_redirect_proto_fields
102 /* Parse type for struct ng_nat_add_server. */
103 static const struct ng_parse_struct_field ng_nat_add_server_fields[]
104 = NG_NAT_ADD_SERVER_TYPE_INFO;
105 static const struct ng_parse_type ng_nat_add_server_type = {
106 &ng_parse_struct_type,
107 &ng_nat_add_server_fields
110 /* Parse type for one struct ng_nat_listrdrs_entry. */
111 static const struct ng_parse_struct_field ng_nat_listrdrs_entry_fields[]
112 = NG_NAT_LISTRDRS_ENTRY_TYPE_INFO(&ng_nat_description_type);
113 static const struct ng_parse_type ng_nat_listrdrs_entry_type = {
114 &ng_parse_struct_type,
115 &ng_nat_listrdrs_entry_fields
118 /* Parse type for 'redirects' array in struct ng_nat_list_redirects. */
120 ng_nat_listrdrs_ary_getLength(const struct ng_parse_type *type,
121 const u_char *start, const u_char *buf)
123 const struct ng_nat_list_redirects *lr;
125 lr = (const struct ng_nat_list_redirects *)
126 (buf - offsetof(struct ng_nat_list_redirects, redirects));
127 return lr->total_count;
130 static const struct ng_parse_array_info ng_nat_listrdrs_ary_info = {
131 &ng_nat_listrdrs_entry_type,
132 &ng_nat_listrdrs_ary_getLength,
135 static const struct ng_parse_type ng_nat_listrdrs_ary_type = {
136 &ng_parse_array_type,
137 &ng_nat_listrdrs_ary_info
140 /* Parse type for struct ng_nat_list_redirects. */
141 static const struct ng_parse_struct_field ng_nat_list_redirects_fields[]
142 = NG_NAT_LIST_REDIRECTS_TYPE_INFO(&ng_nat_listrdrs_ary_type);
143 static const struct ng_parse_type ng_nat_list_redirects_type = {
144 &ng_parse_struct_type,
145 &ng_nat_list_redirects_fields
148 /* List of commands and how to convert arguments to/from ASCII. */
149 static const struct ng_cmdlist ng_nat_cmdlist[] = {
154 &ng_parse_ipaddr_type,
168 &ng_parse_ipaddr_type,
173 NGM_NAT_REDIRECT_PORT,
175 &ng_nat_redirect_port_type,
176 &ng_parse_uint32_type
180 NGM_NAT_REDIRECT_ADDR,
182 &ng_nat_redirect_addr_type,
183 &ng_parse_uint32_type
187 NGM_NAT_REDIRECT_PROTO,
189 &ng_nat_redirect_proto_type,
190 &ng_parse_uint32_type
194 NGM_NAT_REDIRECT_DYNAMIC,
196 &ng_parse_uint32_type,
201 NGM_NAT_REDIRECT_DELETE,
203 &ng_parse_uint32_type,
210 &ng_nat_add_server_type,
215 NGM_NAT_LIST_REDIRECTS,
218 &ng_nat_list_redirects_type
224 &ng_parse_string_type,
230 /* Netgraph node type descriptor. */
231 static struct ng_type typestruct = {
232 .version = NG_ABI_VERSION,
233 .name = NG_NAT_NODE_TYPE,
234 .constructor = ng_nat_constructor,
235 .rcvmsg = ng_nat_rcvmsg,
236 .shutdown = ng_nat_shutdown,
237 .newhook = ng_nat_newhook,
238 .rcvdata = ng_nat_rcvdata,
239 .disconnect = ng_nat_disconnect,
240 .cmdlist = ng_nat_cmdlist,
242 NETGRAPH_INIT(nat, &typestruct);
243 MODULE_DEPEND(ng_nat, libalias, 1, 1, 1);
245 /* Element for list of redirects. */
246 struct ng_nat_rdr_lst {
247 STAILQ_ENTRY(ng_nat_rdr_lst) entries;
248 struct alias_link *lnk;
249 struct ng_nat_listrdrs_entry rdr;
251 STAILQ_HEAD(rdrhead, ng_nat_rdr_lst);
253 /* Information we store for each node. */
255 node_p node; /* back pointer to node */
256 hook_p in; /* hook for demasquerading */
257 hook_p out; /* hook for masquerading */
258 struct libalias *lib; /* libalias handler */
259 uint32_t flags; /* status flags */
260 uint32_t rdrcount; /* number or redirects in list */
261 uint32_t nextid; /* for next in turn in list */
262 struct rdrhead redirhead; /* redirect list header */
264 typedef struct ng_nat_priv *priv_p;
266 /* Values of flags */
267 #define NGNAT_CONNECTED 0x1 /* We have both hooks connected */
268 #define NGNAT_ADDR_DEFINED 0x2 /* NGM_NAT_SET_IPADDR happened */
271 ng_nat_constructor(node_p node)
275 /* Initialize private descriptor. */
276 priv = malloc(sizeof(*priv), M_NETGRAPH,
281 /* Init aliasing engine. */
282 priv->lib = LibAliasInit(NULL);
284 /* Set same ports on. */
285 (void )LibAliasSetMode(priv->lib, PKT_ALIAS_SAME_PORTS,
286 PKT_ALIAS_SAME_PORTS);
288 /* Init redirects housekeeping. */
291 STAILQ_INIT(&priv->redirhead);
293 /* Link structs together. */
294 NG_NODE_SET_PRIVATE(node, priv);
298 * libalias is not thread safe, so our node
299 * must be single threaded.
301 NG_NODE_FORCE_WRITER(node);
307 ng_nat_newhook(node_p node, hook_p hook, const char *name)
309 const priv_p priv = NG_NODE_PRIVATE(node);
311 if (strcmp(name, NG_NAT_HOOK_IN) == 0) {
313 } else if (strcmp(name, NG_NAT_HOOK_OUT) == 0) {
318 if (priv->out != NULL &&
320 priv->flags |= NGNAT_CONNECTED;
326 ng_nat_rcvmsg(node_p node, item_p item, hook_p lasthook)
328 const priv_p priv = NG_NODE_PRIVATE(node);
329 struct ng_mesg *resp = NULL;
333 NGI_GET_MSG(item, msg);
335 switch (msg->header.typecookie) {
337 switch (msg->header.cmd) {
338 case NGM_NAT_SET_IPADDR:
340 struct in_addr *const ia = (struct in_addr *)msg->data;
342 if (msg->header.arglen < sizeof(*ia)) {
347 LibAliasSetAddress(priv->lib, *ia);
349 priv->flags |= NGNAT_ADDR_DEFINED;
352 case NGM_NAT_SET_MODE:
354 struct ng_nat_mode *const mode =
355 (struct ng_nat_mode *)msg->data;
357 if (msg->header.arglen < sizeof(*mode)) {
362 if (LibAliasSetMode(priv->lib,
363 ng_nat_translate_flags(mode->flags),
364 ng_nat_translate_flags(mode->mask)) < 0) {
370 case NGM_NAT_SET_TARGET:
372 struct in_addr *const ia = (struct in_addr *)msg->data;
374 if (msg->header.arglen < sizeof(*ia)) {
379 LibAliasSetTarget(priv->lib, *ia);
382 case NGM_NAT_REDIRECT_PORT:
384 struct ng_nat_rdr_lst *entry;
385 struct ng_nat_redirect_port *const rp =
386 (struct ng_nat_redirect_port *)msg->data;
388 if (msg->header.arglen < sizeof(*rp)) {
393 if ((entry = malloc(sizeof(struct ng_nat_rdr_lst),
394 M_NETGRAPH, M_NOWAIT | M_ZERO)) == NULL) {
399 /* Try actual redirect. */
400 entry->lnk = LibAliasRedirectPort(priv->lib,
401 rp->local_addr, htons(rp->local_port),
402 rp->remote_addr, htons(rp->remote_port),
403 rp->alias_addr, htons(rp->alias_port),
406 if (entry->lnk == NULL) {
408 free(entry, M_NETGRAPH);
412 /* Successful, save info in our internal list. */
413 entry->rdr.local_addr = rp->local_addr;
414 entry->rdr.alias_addr = rp->alias_addr;
415 entry->rdr.remote_addr = rp->remote_addr;
416 entry->rdr.local_port = rp->local_port;
417 entry->rdr.alias_port = rp->alias_port;
418 entry->rdr.remote_port = rp->remote_port;
419 entry->rdr.proto = rp->proto;
420 bcopy(rp->description, entry->rdr.description,
423 /* Safety precaution. */
424 entry->rdr.description[NG_NAT_DESC_LENGTH-1] = '\0';
426 entry->rdr.id = priv->nextid++;
429 /* Link to list of redirects. */
430 STAILQ_INSERT_TAIL(&priv->redirhead, entry, entries);
432 /* Response with id of newly added entry. */
433 NG_MKRESPONSE(resp, msg, sizeof(entry->rdr.id), M_NOWAIT);
438 bcopy(&entry->rdr.id, resp->data, sizeof(entry->rdr.id));
441 case NGM_NAT_REDIRECT_ADDR:
443 struct ng_nat_rdr_lst *entry;
444 struct ng_nat_redirect_addr *const ra =
445 (struct ng_nat_redirect_addr *)msg->data;
447 if (msg->header.arglen < sizeof(*ra)) {
452 if ((entry = malloc(sizeof(struct ng_nat_rdr_lst),
453 M_NETGRAPH, M_NOWAIT | M_ZERO)) == NULL) {
458 /* Try actual redirect. */
459 entry->lnk = LibAliasRedirectAddr(priv->lib,
460 ra->local_addr, ra->alias_addr);
462 if (entry->lnk == NULL) {
464 free(entry, M_NETGRAPH);
468 /* Successful, save info in our internal list. */
469 entry->rdr.local_addr = ra->local_addr;
470 entry->rdr.alias_addr = ra->alias_addr;
471 entry->rdr.proto = NG_NAT_REDIRPROTO_ADDR;
472 bcopy(ra->description, entry->rdr.description,
475 /* Safety precaution. */
476 entry->rdr.description[NG_NAT_DESC_LENGTH-1] = '\0';
478 entry->rdr.id = priv->nextid++;
481 /* Link to list of redirects. */
482 STAILQ_INSERT_TAIL(&priv->redirhead, entry, entries);
484 /* Response with id of newly added entry. */
485 NG_MKRESPONSE(resp, msg, sizeof(entry->rdr.id), M_NOWAIT);
490 bcopy(&entry->rdr.id, resp->data, sizeof(entry->rdr.id));
493 case NGM_NAT_REDIRECT_PROTO:
495 struct ng_nat_rdr_lst *entry;
496 struct ng_nat_redirect_proto *const rp =
497 (struct ng_nat_redirect_proto *)msg->data;
499 if (msg->header.arglen < sizeof(*rp)) {
504 if ((entry = malloc(sizeof(struct ng_nat_rdr_lst),
505 M_NETGRAPH, M_NOWAIT | M_ZERO)) == NULL) {
510 /* Try actual redirect. */
511 entry->lnk = LibAliasRedirectProto(priv->lib,
512 rp->local_addr, rp->remote_addr,
513 rp->alias_addr, rp->proto);
515 if (entry->lnk == NULL) {
517 free(entry, M_NETGRAPH);
521 /* Successful, save info in our internal list. */
522 entry->rdr.local_addr = rp->local_addr;
523 entry->rdr.alias_addr = rp->alias_addr;
524 entry->rdr.remote_addr = rp->remote_addr;
525 entry->rdr.proto = rp->proto;
526 bcopy(rp->description, entry->rdr.description,
529 /* Safety precaution. */
530 entry->rdr.description[NG_NAT_DESC_LENGTH-1] = '\0';
532 entry->rdr.id = priv->nextid++;
535 /* Link to list of redirects. */
536 STAILQ_INSERT_TAIL(&priv->redirhead, entry, entries);
538 /* Response with id of newly added entry. */
539 NG_MKRESPONSE(resp, msg, sizeof(entry->rdr.id), M_NOWAIT);
544 bcopy(&entry->rdr.id, resp->data, sizeof(entry->rdr.id));
547 case NGM_NAT_REDIRECT_DYNAMIC:
548 case NGM_NAT_REDIRECT_DELETE:
550 struct ng_nat_rdr_lst *entry;
551 uint32_t *const id = (uint32_t *)msg->data;
553 if (msg->header.arglen < sizeof(*id)) {
558 /* Find entry with supplied id. */
559 STAILQ_FOREACH(entry, &priv->redirhead, entries) {
560 if (entry->rdr.id == *id)
570 if (msg->header.cmd == NGM_NAT_REDIRECT_DYNAMIC) {
571 if (LibAliasRedirectDynamic(priv->lib,
573 error = ENOTTY; /* XXX Something better? */
576 } else { /* NGM_NAT_REDIRECT_DELETE */
577 LibAliasRedirectDelete(priv->lib, entry->lnk);
580 /* Delete entry from our internal list. */
582 STAILQ_REMOVE(&priv->redirhead, entry, ng_nat_rdr_lst, entries);
583 free(entry, M_NETGRAPH);
586 case NGM_NAT_ADD_SERVER:
588 struct ng_nat_rdr_lst *entry;
589 struct ng_nat_add_server *const as =
590 (struct ng_nat_add_server *)msg->data;
592 if (msg->header.arglen < sizeof(*as)) {
597 /* Find entry with supplied id. */
598 STAILQ_FOREACH(entry, &priv->redirhead, entries) {
599 if (entry->rdr.id == as->id)
609 if (LibAliasAddServer(priv->lib, entry->lnk,
610 as->addr, htons(as->port)) == -1) {
618 case NGM_NAT_LIST_REDIRECTS:
620 struct ng_nat_rdr_lst *entry;
621 struct ng_nat_list_redirects *ary;
624 NG_MKRESPONSE(resp, msg, sizeof(*ary) +
625 (priv->rdrcount) * sizeof(*entry), M_NOWAIT);
631 ary = (struct ng_nat_list_redirects *)resp->data;
632 ary->total_count = priv->rdrcount;
634 STAILQ_FOREACH(entry, &priv->redirhead, entries) {
635 bcopy(&entry->rdr, &ary->redirects[i++],
636 sizeof(struct ng_nat_listrdrs_entry));
640 case NGM_NAT_PROXY_RULE:
642 char *cmd = (char *)msg->data;
644 if (msg->header.arglen < 6) {
649 if (LibAliasProxyRule(priv->lib, cmd) != 0)
654 error = EINVAL; /* unknown command */
659 error = EINVAL; /* unknown cookie type */
663 NG_RESPOND_MSG(error, node, item, resp);
669 ng_nat_rcvdata(hook_p hook, item_p item )
671 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
677 /* We have no required hooks. */
678 if (!(priv->flags & NGNAT_CONNECTED)) {
683 /* We have no alias address yet to do anything. */
684 if (!(priv->flags & NGNAT_ADDR_DEFINED))
689 if ((m = m_megapullup(m, m->m_pkthdr.len)) == NULL) {
690 NGI_M(item) = NULL; /* avoid double free */
698 ip = mtod(m, struct ip *);
700 KASSERT(m->m_pkthdr.len == ntohs(ip->ip_len),
701 ("ng_nat: ip_len != m_pkthdr.len"));
704 * We drop packet when:
705 * 1. libalias returns PKT_ALIAS_ERROR;
706 * 2. For incoming packets:
707 * a) for unresolved fragments;
708 * b) libalias returns PKT_ALIAS_IGNORED and
709 * PKT_ALIAS_DENY_INCOMING flag is set.
711 if (hook == priv->in) {
712 rval = LibAliasIn(priv->lib, c, m->m_len + M_TRAILINGSPACE(m));
713 if (rval == PKT_ALIAS_ERROR ||
714 rval == PKT_ALIAS_UNRESOLVED_FRAGMENT ||
715 (rval == PKT_ALIAS_IGNORED &&
716 (priv->lib->packetAliasMode &
717 PKT_ALIAS_DENY_INCOMING) != 0)) {
721 } else if (hook == priv->out) {
722 rval = LibAliasOut(priv->lib, c, m->m_len + M_TRAILINGSPACE(m));
723 if (rval == PKT_ALIAS_ERROR) {
728 panic("ng_nat: unknown hook!\n");
730 if (rval == PKT_ALIAS_RESPOND)
731 m->m_flags |= M_SKIP_FIREWALL;
732 m->m_pkthdr.len = m->m_len = ntohs(ip->ip_len);
734 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 &&
735 ip->ip_p == IPPROTO_TCP) {
736 struct tcphdr *th = (struct tcphdr *)((caddr_t)ip +
740 * Here is our terrible HACK.
742 * Sometimes LibAlias edits contents of TCP packet.
743 * In this case it needs to recompute full TCP
744 * checksum. However, the problem is that LibAlias
745 * doesn't have any idea about checksum offloading
746 * in kernel. To workaround this, we do not do
747 * checksumming in LibAlias, but only mark the
748 * packets in th_x2 field. If we receive a marked
749 * packet, we calculate correct checksum for it
750 * aware of offloading.
752 * Why do I do such a terrible hack instead of
753 * recalculating checksum for each packet?
754 * Because the previous checksum was not checked!
755 * Recalculating checksums for EVERY packet will
756 * hide ALL transmission errors. Yes, marked packets
757 * still suffer from this problem. But, sigh, natd(8)
758 * has this problem, too.
763 ip->ip_len = ntohs(ip->ip_len);
764 th->th_sum = in_pseudo(ip->ip_src.s_addr,
765 ip->ip_dst.s_addr, htons(IPPROTO_TCP +
766 ip->ip_len - (ip->ip_hl << 2)));
768 if ((m->m_pkthdr.csum_flags & CSUM_TCP) == 0) {
769 m->m_pkthdr.csum_data = offsetof(struct tcphdr,
773 ip->ip_len = htons(ip->ip_len);
778 if (hook == priv->in)
779 NG_FWD_ITEM_HOOK(error, item, priv->out);
781 NG_FWD_ITEM_HOOK(error, item, priv->in);
787 ng_nat_shutdown(node_p node)
789 const priv_p priv = NG_NODE_PRIVATE(node);
791 NG_NODE_SET_PRIVATE(node, NULL);
794 /* Free redirects list. */
795 while (!STAILQ_EMPTY(&priv->redirhead)) {
796 struct ng_nat_rdr_lst *entry = STAILQ_FIRST(&priv->redirhead);
797 STAILQ_REMOVE_HEAD(&priv->redirhead, entries);
798 free(entry, M_NETGRAPH);
802 LibAliasUninit(priv->lib);
803 free(priv, M_NETGRAPH);
809 ng_nat_disconnect(hook_p hook)
811 const priv_p priv = NG_NODE_PRIVATE(NG_HOOK_NODE(hook));
813 priv->flags &= ~NGNAT_CONNECTED;
815 if (hook == priv->out)
817 if (hook == priv->in)
820 if (priv->out == NULL && priv->in == NULL)
821 ng_rmnode_self(NG_HOOK_NODE(hook));
827 ng_nat_translate_flags(unsigned int x)
829 unsigned int res = 0;
832 res |= PKT_ALIAS_LOG;
833 if (x & NG_NAT_DENY_INCOMING)
834 res |= PKT_ALIAS_DENY_INCOMING;
835 if (x & NG_NAT_SAME_PORTS)
836 res |= PKT_ALIAS_SAME_PORTS;
837 if (x & NG_NAT_UNREGISTERED_ONLY)
838 res |= PKT_ALIAS_UNREGISTERED_ONLY;
839 if (x & NG_NAT_RESET_ON_ADDR_CHANGE)
840 res |= PKT_ALIAS_RESET_ON_ADDR_CHANGE;
841 if (x & NG_NAT_PROXY_ONLY)
842 res |= PKT_ALIAS_PROXY_ONLY;
843 if (x & NG_NAT_REVERSE)
844 res |= PKT_ALIAS_REVERSE;