2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
3 * Brian Somers <brian@Awfulhak.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
34 #include <netinet/in_systm.h>
35 #include <netinet/in.h>
36 #include <netinet/ip.h>
37 #include <sys/socket.h>
57 #include "descriptor.h"
61 #include "slcompress.h"
62 #include "throughput.h"
83 #define NAT_EXTRABUF (13)
85 static int StrToAddr(const char *, struct in_addr *);
86 static int StrToPortRange(const char *, u_short *, u_short *, const char *);
87 static int StrToAddrAndPort(const char *, struct in_addr *, u_short *,
88 u_short *, const char *);
91 lowhigh(u_short *a, u_short *b)
103 nat_RedirectPort(struct cmdargs const *arg)
105 if (!arg->bundle->NatEnabled) {
106 prompt_Printf(arg->prompt, "Alias not enabled\n");
108 } else if (arg->argc == arg->argn + 3 || arg->argc == arg->argn + 4) {
111 struct in_addr localaddr;
112 u_short hlocalport, llocalport;
113 struct in_addr aliasaddr;
114 u_short haliasport, laliasport;
115 struct in_addr remoteaddr;
116 u_short hremoteport, lremoteport;
117 struct alias_link *link;
120 proto = arg->argv[arg->argn];
121 if (strcmp(proto, "tcp") == 0) {
122 proto_constant = IPPROTO_TCP;
123 } else if (strcmp(proto, "udp") == 0) {
124 proto_constant = IPPROTO_UDP;
126 prompt_Printf(arg->prompt, "port redirect: protocol must be"
131 error = StrToAddrAndPort(arg->argv[arg->argn+1], &localaddr, &llocalport,
134 prompt_Printf(arg->prompt, "nat port: error reading localaddr:port\n");
138 error = StrToPortRange(arg->argv[arg->argn+2], &laliasport, &haliasport,
141 prompt_Printf(arg->prompt, "nat port: error reading alias port\n");
144 aliasaddr.s_addr = INADDR_ANY;
146 if (arg->argc == arg->argn + 4) {
147 error = StrToAddrAndPort(arg->argv[arg->argn+3], &remoteaddr,
148 &lremoteport, &hremoteport, proto);
150 prompt_Printf(arg->prompt, "nat port: error reading "
151 "remoteaddr:port\n");
155 remoteaddr.s_addr = INADDR_ANY;
156 lremoteport = hremoteport = 0;
159 lowhigh(&llocalport, &hlocalport);
160 lowhigh(&laliasport, &haliasport);
161 lowhigh(&lremoteport, &hremoteport);
163 if (haliasport - laliasport != hlocalport - llocalport) {
164 prompt_Printf(arg->prompt, "nat port: local & alias port ranges "
169 if (hremoteport && hremoteport - lremoteport != hlocalport - llocalport) {
170 prompt_Printf(arg->prompt, "nat port: local & remote port ranges "
175 while (laliasport <= haliasport) {
176 link = PacketAliasRedirectPort(localaddr, htons(llocalport),
177 remoteaddr, htons(lremoteport),
178 aliasaddr, htons(laliasport),
182 prompt_Printf(arg->prompt, "nat port: %d: error %d\n", laliasport,
200 nat_RedirectAddr(struct cmdargs const *arg)
202 if (!arg->bundle->NatEnabled) {
203 prompt_Printf(arg->prompt, "nat not enabled\n");
205 } else if (arg->argc == arg->argn+2) {
207 struct in_addr localaddr, aliasaddr;
208 struct alias_link *link;
210 error = StrToAddr(arg->argv[arg->argn], &localaddr);
212 prompt_Printf(arg->prompt, "address redirect: invalid local address\n");
215 error = StrToAddr(arg->argv[arg->argn+1], &aliasaddr);
217 prompt_Printf(arg->prompt, "address redirect: invalid alias address\n");
218 prompt_Printf(arg->prompt, "Usage: nat %s %s\n", arg->cmd->name,
222 link = PacketAliasRedirectAddr(localaddr, aliasaddr);
224 prompt_Printf(arg->prompt, "address redirect: packet aliasing"
226 prompt_Printf(arg->prompt, "Usage: nat %s %s\n", arg->cmd->name,
237 nat_RedirectProto(struct cmdargs const *arg)
239 if (!arg->bundle->NatEnabled) {
240 prompt_Printf(arg->prompt, "nat not enabled\n");
242 } else if (arg->argc >= arg->argn + 2 && arg->argc <= arg->argn + 4) {
243 struct in_addr localIP, publicIP, remoteIP;
244 struct alias_link *link;
248 len = strlen(arg->argv[arg->argn]);
250 prompt_Printf(arg->prompt, "proto redirect: invalid protocol\n");
253 if (strspn(arg->argv[arg->argn], "01234567") == len)
254 pe = getprotobynumber(atoi(arg->argv[arg->argn]));
256 pe = getprotobyname(arg->argv[arg->argn]);
258 prompt_Printf(arg->prompt, "proto redirect: invalid protocol\n");
262 error = StrToAddr(arg->argv[arg->argn + 1], &localIP);
264 prompt_Printf(arg->prompt, "proto redirect: invalid src address\n");
268 if (arg->argc >= arg->argn + 3) {
269 error = StrToAddr(arg->argv[arg->argn + 2], &publicIP);
271 prompt_Printf(arg->prompt, "proto redirect: invalid alias address\n");
272 prompt_Printf(arg->prompt, "Usage: nat %s %s\n", arg->cmd->name,
277 publicIP.s_addr = INADDR_ANY;
279 if (arg->argc == arg->argn + 4) {
280 error = StrToAddr(arg->argv[arg->argn + 2], &remoteIP);
282 prompt_Printf(arg->prompt, "proto redirect: invalid dst address\n");
283 prompt_Printf(arg->prompt, "Usage: nat %s %s\n", arg->cmd->name,
288 remoteIP.s_addr = INADDR_ANY;
290 link = PacketAliasRedirectProto(localIP, remoteIP, publicIP, pe->p_proto);
292 prompt_Printf(arg->prompt, "proto redirect: packet aliasing"
294 prompt_Printf(arg->prompt, "Usage: nat %s %s\n", arg->cmd->name,
305 StrToAddr(const char *str, struct in_addr *addr)
309 if (inet_aton(str, addr))
312 hp = gethostbyname(str);
314 log_Printf(LogWARN, "StrToAddr: Unknown host %s.\n", str);
317 *addr = *((struct in_addr *) hp->h_addr);
323 StrToPort(const char *str, u_short *port, const char *proto)
328 *port = strtol(str, &end, 10);
330 sp = getservbyname(str, proto);
332 log_Printf(LogWARN, "StrToAddr: Unknown port or service %s/%s.\n",
336 *port = ntohs(sp->s_port);
343 StrToPortRange(const char *str, u_short *low, u_short *high, const char *proto)
348 minus = strchr(str, '-');
350 *minus = '\0'; /* Cheat the const-ness ! */
352 res = StrToPort(str, low, proto);
355 *minus = '-'; /* Cheat the const-ness ! */
359 res = StrToPort(minus + 1, high, proto);
368 StrToAddrAndPort(const char *str, struct in_addr *addr, u_short *low,
369 u_short *high, const char *proto)
374 colon = strchr(str, ':');
376 log_Printf(LogWARN, "StrToAddrAndPort: %s is missing port number.\n", str);
380 *colon = '\0'; /* Cheat the const-ness ! */
381 res = StrToAddr(str, addr);
382 *colon = ':'; /* Cheat the const-ness ! */
386 return StrToPortRange(colon + 1, low, high, proto);
390 nat_ProxyRule(struct cmdargs const *arg)
396 if (arg->argn >= arg->argc)
399 for (f = arg->argn, pos = 0; f < arg->argc; f++) {
400 len = strlen(arg->argv[f]);
401 if (sizeof cmd - pos < len + (len ? 1 : 0))
405 strcpy(cmd + pos, arg->argv[f]);
409 return PacketAliasProxyRule(cmd);
413 nat_SetTarget(struct cmdargs const *arg)
417 if (arg->argc == arg->argn) {
418 addr.s_addr = INADDR_ANY;
419 PacketAliasSetTarget(addr);
423 if (arg->argc != arg->argn + 1)
426 if (!strcasecmp(arg->argv[arg->argn], "MYADDR")) {
427 addr.s_addr = INADDR_ANY;
428 PacketAliasSetTarget(addr);
432 addr = GetIpAddr(arg->argv[arg->argn]);
433 if (addr.s_addr == INADDR_NONE) {
434 log_Printf(LogWARN, "%s: invalid address\n", arg->argv[arg->argn]);
438 PacketAliasSetTarget(addr);
444 nat_PunchFW(struct cmdargs const *arg)
449 if (arg->argc == arg->argn) {
450 PacketAliasSetMode(0, PKT_ALIAS_PUNCH_FW);
454 if (arg->argc != arg->argn + 2)
457 base = strtol(arg->argv[arg->argn], &end, 10);
458 if (*end != '\0' || base < 0)
461 count = strtol(arg->argv[arg->argn + 1], &end, 10);
462 if (*end != '\0' || count < 0)
465 PacketAliasSetFWBase(base, count);
466 PacketAliasSetMode(PKT_ALIAS_PUNCH_FW, PKT_ALIAS_PUNCH_FW);
473 nat_LayerPush(struct bundle *bundle, struct link *l, struct mbuf *bp,
474 int pri, u_short *proto)
476 if (!bundle->NatEnabled || *proto != PROTO_IP)
479 log_Printf(LogDEBUG, "nat_LayerPush: PROTO_IP -> PROTO_IP\n");
480 m_settype(bp, MB_NATOUT);
481 /* Ensure there's a bit of extra buffer for the NAT code... */
482 bp = m_pullup(m_append(bp, NULL, NAT_EXTRABUF));
483 PacketAliasOut(MBUF_CTOP(bp), bp->m_len);
484 bp->m_len = ntohs(((struct ip *)MBUF_CTOP(bp))->ip_len);
490 nat_LayerPull(struct bundle *bundle, struct link *l, struct mbuf *bp,
494 int ret, len, nfrags;
498 if (!bundle->NatEnabled || *proto != PROTO_IP)
501 log_Printf(LogDEBUG, "nat_LayerPull: PROTO_IP -> PROTO_IP\n");
502 m_settype(bp, MB_NATIN);
503 /* Ensure there's a bit of extra buffer for the NAT code... */
504 bp = m_pullup(m_append(bp, NULL, NAT_EXTRABUF));
505 ret = PacketAliasIn(MBUF_CTOP(bp), bp->m_len);
507 bp->m_len = ntohs(((struct ip *)MBUF_CTOP(bp))->ip_len);
508 if (bp->m_len > MAX_MRU) {
509 log_Printf(LogWARN, "nat_LayerPull: Problem with IP header length (%lu)\n",
510 (unsigned long)bp->m_len);
519 case PKT_ALIAS_UNRESOLVED_FRAGMENT:
520 /* Save the data for later */
521 fptr = malloc(bp->m_len);
522 bp = mbuf_Read(bp, fptr, bp->m_len);
523 PacketAliasSaveFragment(fptr);
524 log_Printf(LogDEBUG, "Store another frag (%lu) - now %d\n",
525 (unsigned long)((struct ip *)fptr)->ip_id, ++gfrags);
528 case PKT_ALIAS_FOUND_HEADER_FRAGMENT:
529 /* Fetch all the saved fragments and chain them on the end of `bp' */
530 last = &bp->m_nextpkt;
532 while ((fptr = PacketAliasGetFragment(MBUF_CTOP(bp))) != NULL) {
534 PacketAliasFragmentIn(MBUF_CTOP(bp), fptr);
535 len = ntohs(((struct ip *)fptr)->ip_len);
536 *last = m_get(len, MB_NATIN);
537 memcpy(MBUF_CTOP(*last), fptr, len);
539 last = &(*last)->m_nextpkt;
542 log_Printf(LogDEBUG, "Found a frag header (%lu) - plus %d more frags (no"
543 "w %d)\n", (unsigned long)((struct ip *)MBUF_CTOP(bp))->ip_id,
547 case PKT_ALIAS_IGNORED:
548 if (PacketAliasSetMode(0, 0) & PKT_ALIAS_DENY_INCOMING) {
549 log_Printf(LogTCPIP, "NAT engine denied data:\n");
552 } else if (log_IsKept(LogTCPIP)) {
553 log_Printf(LogTCPIP, "NAT engine ignored data:\n");
554 PacketCheck(bundle, AF_INET, MBUF_CTOP(bp), bp->m_len, NULL,
560 log_Printf(LogWARN, "nat_LayerPull: Dropped a packet (%d)....\n", ret);
569 struct layer natlayer =
570 { LAYER_NAT, "nat", nat_LayerPush, nat_LayerPull };