1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* natd - Network Address Translation Daemon for FreeBSD.
|
|
|
|
*
|
1997-12-27 19:31:11 +00:00
|
|
|
* This software is provided free of charge, with no
|
1997-06-22 04:19:08 +00:00
|
|
|
* warranty of any kind, either expressed or implied.
|
|
|
|
* Use at your own risk.
|
|
|
|
*
|
|
|
|
* You may copy, modify and distribute this software (natd.c) freely.
|
|
|
|
*
|
1997-08-10 21:55:52 +00:00
|
|
|
* Ari Suutari <suutari@iki.fi>
|
1997-06-22 04:19:08 +00:00
|
|
|
*
|
1999-08-28 00:22:10 +00:00
|
|
|
* $FreeBSD$
|
1997-06-22 04:19:08 +00:00
|
|
|
*/
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
#define SYSLOG_NAMES
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
1999-10-13 09:00:16 +00:00
|
|
|
#include <sys/sysctl.h>
|
1997-06-22 04:19:08 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/in_systm.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <machine/in_cksum.h>
|
|
|
|
#include <netinet/tcp.h>
|
1999-03-07 18:23:56 +00:00
|
|
|
#include <netinet/udp.h>
|
|
|
|
#include <netinet/ip_icmp.h>
|
1997-06-22 04:19:08 +00:00
|
|
|
#include <net/if.h>
|
1999-10-13 09:00:16 +00:00
|
|
|
#include <net/if_dl.h>
|
1997-06-22 04:19:08 +00:00
|
|
|
#include <net/route.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include <alias.h>
|
1997-10-02 11:43:33 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <unistd.h>
|
1997-12-10 02:14:57 +00:00
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
#include "natd.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Default values for input and output
|
|
|
|
* divert socket ports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DEFAULT_SERVICE "natd"
|
|
|
|
|
1999-03-24 20:30:20 +00:00
|
|
|
/*
|
|
|
|
* Definition of a port range, and macros to deal with values.
|
|
|
|
* FORMAT: HI 16-bits == first port in range, 0 == all ports.
|
|
|
|
* LO 16-bits == number of ports in range
|
|
|
|
* NOTES: - Port values are not stored in network byte order.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef u_long port_range;
|
|
|
|
|
|
|
|
#define GETLOPORT(x) ((x) >> 0x10)
|
|
|
|
#define GETNUMPORTS(x) ((x) & 0x0000ffff)
|
|
|
|
#define GETHIPORT(x) (GETLOPORT((x)) + GETNUMPORTS((x)))
|
|
|
|
|
|
|
|
/* Set y to be the low-port value in port_range variable x. */
|
|
|
|
#define SETLOPORT(x,y) ((x) = ((x) & 0x0000ffff) | ((y) << 0x10))
|
|
|
|
|
|
|
|
/* Set y to be the number of ports in port_range variable x. */
|
|
|
|
#define SETNUMPORTS(x,y) ((x) = ((x) & 0xffff0000) | (y))
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Function prototypes.
|
|
|
|
*/
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
static void DoAliasing (int fd, int direction);
|
1999-03-24 20:30:48 +00:00
|
|
|
static void DaemonMode (void);
|
1997-12-10 02:14:57 +00:00
|
|
|
static void HandleRoutingInfo (int fd);
|
1999-03-24 20:30:48 +00:00
|
|
|
static void Usage (void);
|
1999-03-07 18:23:56 +00:00
|
|
|
static char* FormatPacket (struct ip*);
|
1997-12-10 02:14:57 +00:00
|
|
|
static void PrintPacket (struct ip*);
|
1999-03-24 20:30:48 +00:00
|
|
|
static void SyslogPacket (struct ip*, int priority, const char *label);
|
1999-10-13 09:00:16 +00:00
|
|
|
static void SetAliasAddressFromIfName (const char *ifName);
|
1999-03-24 20:30:48 +00:00
|
|
|
static void InitiateShutdown (int);
|
|
|
|
static void Shutdown (int);
|
|
|
|
static void RefreshAddr (int);
|
|
|
|
static void ParseOption (const char* option, const char* parms, int cmdLine);
|
|
|
|
static void ReadConfigFile (const char* fileName);
|
|
|
|
static void SetupPortRedirect (const char* parms);
|
|
|
|
static void SetupAddressRedirect (const char* parms);
|
|
|
|
static void SetupPptpAlias (const char* parms);
|
|
|
|
static void StrToAddr (const char* str, struct in_addr* addr);
|
|
|
|
static u_short StrToPort (const char* str, const char* proto);
|
|
|
|
static int StrToPortRange (const char* str, const char* proto, port_range *portRange);
|
|
|
|
static int StrToProto (const char* str);
|
|
|
|
static int StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto, port_range *portRange);
|
1997-12-10 02:14:57 +00:00
|
|
|
static void ParseArgs (int argc, char** argv);
|
|
|
|
static void FlushPacketBuffer (int fd);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Globals.
|
|
|
|
*/
|
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
static int verbose;
|
|
|
|
static int background;
|
|
|
|
static int running;
|
|
|
|
static int assignAliasAddr;
|
|
|
|
static char* ifName;
|
|
|
|
static int ifIndex;
|
1997-12-10 02:14:57 +00:00
|
|
|
static u_short inPort;
|
|
|
|
static u_short outPort;
|
|
|
|
static u_short inOutPort;
|
1997-08-10 21:55:52 +00:00
|
|
|
static struct in_addr aliasAddr;
|
|
|
|
static int dynamicMode;
|
|
|
|
static int ifMTU;
|
|
|
|
static int aliasOverhead;
|
|
|
|
static int icmpSock;
|
|
|
|
static char packetBuf[IP_MAXPACKET];
|
|
|
|
static int packetLen;
|
|
|
|
static struct sockaddr_in packetAddr;
|
|
|
|
static int packetSock;
|
1999-03-07 18:23:56 +00:00
|
|
|
static int packetDirection;
|
1997-09-06 11:14:03 +00:00
|
|
|
static int dropIgnoredIncoming;
|
1999-03-07 18:23:56 +00:00
|
|
|
static int logDropped;
|
|
|
|
static int logFacility;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
int main (int argc, char** argv)
|
|
|
|
{
|
|
|
|
int divertIn;
|
|
|
|
int divertOut;
|
|
|
|
int divertInOut;
|
|
|
|
int routeSock;
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
fd_set readMask;
|
1997-08-10 21:55:52 +00:00
|
|
|
fd_set writeMask;
|
1997-06-22 04:19:08 +00:00
|
|
|
int fdMax;
|
|
|
|
/*
|
|
|
|
* Initialize packet aliasing software.
|
|
|
|
* Done already here to be able to alter option bits
|
|
|
|
* during command line and configuration file processing.
|
|
|
|
*/
|
1997-08-10 21:55:52 +00:00
|
|
|
PacketAliasInit ();
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Parse options.
|
|
|
|
*/
|
|
|
|
inPort = 0;
|
|
|
|
outPort = 0;
|
|
|
|
verbose = 0;
|
|
|
|
inOutPort = 0;
|
|
|
|
ifName = NULL;
|
|
|
|
ifMTU = -1;
|
|
|
|
background = 0;
|
|
|
|
running = 1;
|
|
|
|
assignAliasAddr = 0;
|
|
|
|
aliasAddr.s_addr = INADDR_NONE;
|
|
|
|
aliasOverhead = 12;
|
|
|
|
dynamicMode = 0;
|
1999-03-07 18:23:56 +00:00
|
|
|
logDropped = 0;
|
|
|
|
logFacility = LOG_DAEMON;
|
1997-08-10 21:55:52 +00:00
|
|
|
/*
|
|
|
|
* Mark packet buffer empty.
|
|
|
|
*/
|
|
|
|
packetSock = -1;
|
1999-03-07 18:23:56 +00:00
|
|
|
packetDirection = DONT_KNOW;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
ParseArgs (argc, argv);
|
1999-03-07 18:23:56 +00:00
|
|
|
/*
|
|
|
|
* Open syslog channel.
|
|
|
|
*/
|
1999-10-13 09:00:16 +00:00
|
|
|
openlog ("natd", LOG_CONS | LOG_PID | (verbose ? LOG_PERROR : 0),
|
|
|
|
logFacility);
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Check that valid aliasing address has been given.
|
|
|
|
*/
|
1997-10-02 11:43:33 +00:00
|
|
|
if (aliasAddr.s_addr == INADDR_NONE && ifName == NULL)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "aliasing address not given");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
if (aliasAddr.s_addr != INADDR_NONE && ifName != NULL)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "both alias address and interface "
|
|
|
|
"name are not allowed");
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Check that valid port number is known.
|
|
|
|
*/
|
|
|
|
if (inPort != 0 || outPort != 0)
|
1997-10-02 11:43:33 +00:00
|
|
|
if (inPort == 0 || outPort == 0)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "both input and output ports are required");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
if (inPort == 0 && outPort == 0 && inOutPort == 0)
|
|
|
|
ParseOption ("port", DEFAULT_SERVICE, 0);
|
|
|
|
|
1997-09-06 11:14:03 +00:00
|
|
|
/*
|
|
|
|
* Check if ignored packets should be dropped.
|
|
|
|
*/
|
|
|
|
dropIgnoredIncoming = PacketAliasSetMode (0, 0);
|
|
|
|
dropIgnoredIncoming &= PKT_ALIAS_DENY_INCOMING;
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Create divert sockets. Use only one socket if -p was specified
|
|
|
|
* on command line. Otherwise, create separate sockets for
|
|
|
|
* outgoing and incoming connnections.
|
|
|
|
*/
|
|
|
|
if (inOutPort) {
|
|
|
|
|
|
|
|
divertInOut = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (divertInOut == -1)
|
|
|
|
Quit ("Unable to create divert socket.");
|
|
|
|
|
|
|
|
divertIn = -1;
|
|
|
|
divertOut = -1;
|
|
|
|
/*
|
|
|
|
* Bind socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = inOutPort;
|
|
|
|
|
|
|
|
if (bind (divertInOut,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind divert socket.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
divertIn = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (divertIn == -1)
|
|
|
|
Quit ("Unable to create incoming divert socket.");
|
|
|
|
|
|
|
|
divertOut = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (divertOut == -1)
|
|
|
|
Quit ("Unable to create outgoing divert socket.");
|
|
|
|
|
|
|
|
divertInOut = -1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind divert sockets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = inPort;
|
|
|
|
|
|
|
|
if (bind (divertIn,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind incoming divert socket.");
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = outPort;
|
|
|
|
|
|
|
|
if (bind (divertOut,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind outgoing divert socket.");
|
|
|
|
}
|
|
|
|
/*
|
1999-09-28 08:01:46 +00:00
|
|
|
* Create routing socket if interface name specified and in dynamic mode.
|
1997-06-22 04:19:08 +00:00
|
|
|
*/
|
1999-09-28 08:01:46 +00:00
|
|
|
routeSock = -1;
|
|
|
|
if (ifName) {
|
|
|
|
if (dynamicMode) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-09-28 08:01:46 +00:00
|
|
|
routeSock = socket (PF_ROUTE, SOCK_RAW, 0);
|
|
|
|
if (routeSock == -1)
|
|
|
|
Quit ("Unable to create routing info socket.");
|
|
|
|
|
|
|
|
assignAliasAddr = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetAliasAddressFromIfName (ifName);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Create socket for sending ICMP messages.
|
|
|
|
*/
|
|
|
|
icmpSock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
|
|
|
|
if (icmpSock == -1)
|
|
|
|
Quit ("Unable to create ICMP socket.");
|
1999-03-30 10:11:21 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* And disable reads for the socket, otherwise it slowly fills
|
|
|
|
* up with received icmps which we do not use.
|
|
|
|
*/
|
|
|
|
shutdown(icmpSock, SHUT_RD);
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Become a daemon unless verbose mode was requested.
|
|
|
|
*/
|
|
|
|
if (!verbose)
|
|
|
|
DaemonMode ();
|
|
|
|
/*
|
|
|
|
* Catch signals to manage shutdown and
|
|
|
|
* refresh of interface address.
|
|
|
|
*/
|
1999-09-02 15:17:25 +00:00
|
|
|
siginterrupt(SIGTERM, 1);
|
|
|
|
siginterrupt(SIGHUP, 1);
|
1997-06-22 04:19:08 +00:00
|
|
|
signal (SIGTERM, InitiateShutdown);
|
|
|
|
signal (SIGHUP, RefreshAddr);
|
|
|
|
/*
|
|
|
|
* Set alias address if it has been given.
|
|
|
|
*/
|
|
|
|
if (aliasAddr.s_addr != INADDR_NONE)
|
1997-08-10 21:55:52 +00:00
|
|
|
PacketAliasSetAddress (aliasAddr);
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* We need largest descriptor number for select.
|
|
|
|
*/
|
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
fdMax = -1;
|
|
|
|
|
|
|
|
if (divertIn > fdMax)
|
|
|
|
fdMax = divertIn;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (divertOut > fdMax)
|
|
|
|
fdMax = divertOut;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (divertInOut > fdMax)
|
|
|
|
fdMax = divertInOut;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (routeSock > fdMax)
|
|
|
|
fdMax = routeSock;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
while (running) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (divertInOut != -1 && !ifName && packetSock == -1) {
|
|
|
|
/*
|
|
|
|
* When using only one socket, just call
|
|
|
|
* DoAliasing repeatedly to process packets.
|
|
|
|
*/
|
1999-03-07 18:23:56 +00:00
|
|
|
DoAliasing (divertInOut, DONT_KNOW);
|
1997-08-10 21:55:52 +00:00
|
|
|
continue;
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Build read mask from socket descriptors to select.
|
|
|
|
*/
|
1997-08-10 21:55:52 +00:00
|
|
|
FD_ZERO (&readMask);
|
|
|
|
FD_ZERO (&writeMask);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If there is unsent packet in buffer, use select
|
|
|
|
* to check when socket comes writable again.
|
|
|
|
*/
|
|
|
|
if (packetSock != -1) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
FD_SET (packetSock, &writeMask);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/*
|
|
|
|
* No unsent packet exists - safe to check if
|
|
|
|
* new ones are available.
|
|
|
|
*/
|
1997-06-22 04:19:08 +00:00
|
|
|
if (divertIn != -1)
|
|
|
|
FD_SET (divertIn, &readMask);
|
|
|
|
|
|
|
|
if (divertOut != -1)
|
|
|
|
FD_SET (divertOut, &readMask);
|
|
|
|
|
|
|
|
if (divertInOut != -1)
|
|
|
|
FD_SET (divertInOut, &readMask);
|
1997-08-10 21:55:52 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Routing info is processed always.
|
|
|
|
*/
|
|
|
|
if (routeSock != -1)
|
|
|
|
FD_SET (routeSock, &readMask);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (select (fdMax + 1,
|
|
|
|
&readMask,
|
|
|
|
&writeMask,
|
|
|
|
NULL,
|
|
|
|
NULL) == -1) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (errno == EINTR)
|
|
|
|
continue;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
Quit ("Select failed.");
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (packetSock != -1)
|
|
|
|
if (FD_ISSET (packetSock, &writeMask))
|
|
|
|
FlushPacketBuffer (packetSock);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (divertIn != -1)
|
|
|
|
if (FD_ISSET (divertIn, &readMask))
|
1999-03-07 18:23:56 +00:00
|
|
|
DoAliasing (divertIn, INPUT);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (divertOut != -1)
|
|
|
|
if (FD_ISSET (divertOut, &readMask))
|
1999-03-07 18:23:56 +00:00
|
|
|
DoAliasing (divertOut, OUTPUT);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (divertInOut != -1)
|
|
|
|
if (FD_ISSET (divertInOut, &readMask))
|
1999-03-07 18:23:56 +00:00
|
|
|
DoAliasing (divertInOut, DONT_KNOW);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (routeSock != -1)
|
|
|
|
if (FD_ISSET (routeSock, &readMask))
|
|
|
|
HandleRoutingInfo (routeSock);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (background)
|
|
|
|
unlink (PIDFILE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DaemonMode ()
|
|
|
|
{
|
|
|
|
FILE* pidFile;
|
|
|
|
|
|
|
|
daemon (0, 0);
|
|
|
|
background = 1;
|
|
|
|
|
|
|
|
pidFile = fopen (PIDFILE, "w");
|
|
|
|
if (pidFile) {
|
|
|
|
|
|
|
|
fprintf (pidFile, "%d\n", getpid ());
|
|
|
|
fclose (pidFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ParseArgs (int argc, char** argv)
|
|
|
|
{
|
|
|
|
int arg;
|
|
|
|
char* parm;
|
|
|
|
char* opt;
|
|
|
|
char parmBuf[256];
|
|
|
|
|
|
|
|
for (arg = 1; arg < argc; arg++) {
|
|
|
|
|
|
|
|
opt = argv[arg];
|
|
|
|
if (*opt != '-') {
|
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
warnx ("invalid option %s", opt);
|
1997-06-22 04:19:08 +00:00
|
|
|
Usage ();
|
|
|
|
}
|
|
|
|
|
|
|
|
parm = NULL;
|
|
|
|
parmBuf[0] = '\0';
|
|
|
|
|
|
|
|
while (arg < argc - 1) {
|
|
|
|
|
|
|
|
if (argv[arg + 1][0] == '-')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (parm)
|
|
|
|
strcat (parmBuf, " ");
|
|
|
|
|
|
|
|
++arg;
|
|
|
|
parm = parmBuf;
|
|
|
|
strcat (parmBuf, argv[arg]);
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseOption (opt + 1, parm, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
static void DoAliasing (int fd, int direction)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
int origBytes;
|
1997-09-06 11:14:03 +00:00
|
|
|
int status;
|
1997-06-22 04:19:08 +00:00
|
|
|
int addrSize;
|
|
|
|
struct ip* ip;
|
|
|
|
|
|
|
|
if (assignAliasAddr) {
|
|
|
|
|
|
|
|
SetAliasAddressFromIfName (ifName);
|
|
|
|
assignAliasAddr = 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get packet from socket.
|
|
|
|
*/
|
1997-08-10 21:55:52 +00:00
|
|
|
addrSize = sizeof packetAddr;
|
1997-06-22 04:19:08 +00:00
|
|
|
origBytes = recvfrom (fd,
|
1997-08-10 21:55:52 +00:00
|
|
|
packetBuf,
|
|
|
|
sizeof packetBuf,
|
1997-06-22 04:19:08 +00:00
|
|
|
0,
|
1997-08-10 21:55:52 +00:00
|
|
|
(struct sockaddr*) &packetAddr,
|
1997-06-22 04:19:08 +00:00
|
|
|
&addrSize);
|
|
|
|
|
|
|
|
if (origBytes == -1) {
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
1997-10-02 11:43:33 +00:00
|
|
|
Warn ("read from divert socket failed");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* This is a IP packet.
|
|
|
|
*/
|
1997-08-10 21:55:52 +00:00
|
|
|
ip = (struct ip*) packetBuf;
|
1999-04-25 22:33:30 +00:00
|
|
|
if (direction == DONT_KNOW) {
|
1999-03-07 18:23:56 +00:00
|
|
|
if (packetAddr.sin_addr.s_addr == INADDR_ANY)
|
|
|
|
direction = OUTPUT;
|
|
|
|
else
|
|
|
|
direction = INPUT;
|
1999-04-25 22:33:30 +00:00
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
/*
|
|
|
|
* Print packet direction and protocol type.
|
|
|
|
*/
|
1999-03-07 18:23:56 +00:00
|
|
|
printf (direction == OUTPUT ? "Out " : "In ");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
switch (ip->ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
printf ("[TCP] ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
printf ("[UDP] ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
printf ("[ICMP] ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1999-03-07 18:23:56 +00:00
|
|
|
printf ("[%d] ", ip->ip_p);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Print addresses.
|
|
|
|
*/
|
|
|
|
PrintPacket (ip);
|
|
|
|
}
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
if (direction == OUTPUT) {
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Outgoing packets. Do aliasing.
|
|
|
|
*/
|
1997-08-10 21:55:52 +00:00
|
|
|
PacketAliasOut (packetBuf, IP_MAXPACKET);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
else {
|
1999-03-07 18:23:56 +00:00
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Do aliasing.
|
|
|
|
*/
|
1997-09-06 11:14:03 +00:00
|
|
|
status = PacketAliasIn (packetBuf, IP_MAXPACKET);
|
|
|
|
if (status == PKT_ALIAS_IGNORED &&
|
|
|
|
dropIgnoredIncoming) {
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
if (verbose)
|
|
|
|
printf (" dropped.\n");
|
|
|
|
|
|
|
|
if (logDropped)
|
|
|
|
SyslogPacket (ip, LOG_WARNING, "denied");
|
|
|
|
|
1997-09-06 11:14:03 +00:00
|
|
|
return;
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Length might have changed during aliasing.
|
|
|
|
*/
|
|
|
|
bytes = ntohs (ip->ip_len);
|
|
|
|
/*
|
|
|
|
* Update alias overhead size for outgoing packets.
|
|
|
|
*/
|
1999-03-07 18:23:56 +00:00
|
|
|
if (direction == OUTPUT &&
|
1997-06-22 04:19:08 +00:00
|
|
|
bytes - origBytes > aliasOverhead)
|
|
|
|
aliasOverhead = bytes - origBytes;
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print addresses after aliasing.
|
|
|
|
*/
|
|
|
|
printf (" aliased to\n");
|
|
|
|
printf (" ");
|
|
|
|
PrintPacket (ip);
|
|
|
|
printf ("\n");
|
|
|
|
}
|
1997-08-10 21:55:52 +00:00
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
packetLen = bytes;
|
|
|
|
packetSock = fd;
|
|
|
|
packetDirection = direction;
|
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
FlushPacketBuffer (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void FlushPacketBuffer (int fd)
|
|
|
|
{
|
|
|
|
int wrote;
|
|
|
|
char msgBuf[80];
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Put packet back for processing.
|
|
|
|
*/
|
|
|
|
wrote = sendto (fd,
|
1997-08-10 21:55:52 +00:00
|
|
|
packetBuf,
|
|
|
|
packetLen,
|
1997-06-22 04:19:08 +00:00
|
|
|
0,
|
1997-08-10 21:55:52 +00:00
|
|
|
(struct sockaddr*) &packetAddr,
|
|
|
|
sizeof packetAddr);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (wrote != packetLen) {
|
|
|
|
/*
|
|
|
|
* If buffer space is not available,
|
|
|
|
* just return. Main loop will take care of
|
|
|
|
* retrying send when space becomes available.
|
|
|
|
*/
|
|
|
|
if (errno == ENOBUFS)
|
|
|
|
return;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
if (errno == EMSGSIZE) {
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
if (packetDirection == OUTPUT &&
|
1997-06-22 04:19:08 +00:00
|
|
|
ifMTU != -1)
|
|
|
|
SendNeedFragIcmp (icmpSock,
|
1997-08-10 21:55:52 +00:00
|
|
|
(struct ip*) packetBuf,
|
1997-06-22 04:19:08 +00:00
|
|
|
ifMTU - aliasOverhead);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
sprintf (msgBuf, "failed to write packet back");
|
1997-06-22 04:19:08 +00:00
|
|
|
Warn (msgBuf);
|
|
|
|
}
|
|
|
|
}
|
1997-08-10 21:55:52 +00:00
|
|
|
|
|
|
|
packetSock = -1;
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void HandleRoutingInfo (int fd)
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
struct if_msghdr ifMsg;
|
|
|
|
/*
|
|
|
|
* Get packet from socket.
|
|
|
|
*/
|
|
|
|
bytes = read (fd, &ifMsg, sizeof ifMsg);
|
|
|
|
if (bytes == -1) {
|
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
Warn ("read from routing socket failed");
|
1997-06-22 04:19:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ifMsg.ifm_version != RTM_VERSION) {
|
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
Warn ("unexpected packet read from routing socket");
|
1997-06-22 04:19:08 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verbose)
|
2000-01-25 12:24:06 +00:00
|
|
|
printf ("Routing message %#x received.\n", ifMsg.ifm_type);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2000-01-25 12:24:06 +00:00
|
|
|
if ((ifMsg.ifm_type == RTM_NEWADDR || ifMsg.ifm_type == RTM_IFINFO) &&
|
|
|
|
ifMsg.ifm_index == ifIndex) {
|
|
|
|
if (verbose)
|
|
|
|
printf("Interface address/MTU has probably changed.\n");
|
1997-06-22 04:19:08 +00:00
|
|
|
assignAliasAddr = 1;
|
2000-01-25 12:24:06 +00:00
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void PrintPacket (struct ip* ip)
|
|
|
|
{
|
1999-03-07 18:23:56 +00:00
|
|
|
printf ("%s", FormatPacket (ip));
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
static void SyslogPacket (struct ip* ip, int priority, const char *label)
|
1999-03-07 18:23:56 +00:00
|
|
|
{
|
|
|
|
syslog (priority, "%s %s", label, FormatPacket (ip));
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* FormatPacket (struct ip* ip)
|
|
|
|
{
|
|
|
|
static char buf[256];
|
1997-06-22 04:19:08 +00:00
|
|
|
struct tcphdr* tcphdr;
|
1999-03-07 18:23:56 +00:00
|
|
|
struct udphdr* udphdr;
|
|
|
|
struct icmp* icmphdr;
|
|
|
|
char src[20];
|
|
|
|
char dst[20];
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
strcpy (src, inet_ntoa (ip->ip_src));
|
|
|
|
strcpy (dst, inet_ntoa (ip->ip_dst));
|
|
|
|
|
|
|
|
switch (ip->ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
1997-06-22 04:19:08 +00:00
|
|
|
tcphdr = (struct tcphdr*) ((char*) ip + (ip->ip_hl << 2));
|
1999-03-07 18:23:56 +00:00
|
|
|
sprintf (buf, "[TCP] %s:%d -> %s:%d",
|
|
|
|
src,
|
|
|
|
ntohs (tcphdr->th_sport),
|
|
|
|
dst,
|
|
|
|
ntohs (tcphdr->th_dport));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
udphdr = (struct udphdr*) ((char*) ip + (ip->ip_hl << 2));
|
|
|
|
sprintf (buf, "[UDP] %s:%d -> %s:%d",
|
|
|
|
src,
|
|
|
|
ntohs (udphdr->uh_sport),
|
|
|
|
dst,
|
|
|
|
ntohs (udphdr->uh_dport));
|
|
|
|
break;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
case IPPROTO_ICMP:
|
|
|
|
icmphdr = (struct icmp*) ((char*) ip + (ip->ip_hl << 2));
|
1999-03-11 09:24:52 +00:00
|
|
|
sprintf (buf, "[ICMP] %s -> %s %u(%u)",
|
1999-03-07 18:23:56 +00:00
|
|
|
src,
|
|
|
|
dst,
|
1999-03-11 09:24:52 +00:00
|
|
|
icmphdr->icmp_type,
|
|
|
|
icmphdr->icmp_code);
|
1999-03-07 18:23:56 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
sprintf (buf, "[%d] %s -> %s ", ip->ip_p, src, dst);
|
|
|
|
break;
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
return buf;
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-10-13 09:00:16 +00:00
|
|
|
static void
|
|
|
|
SetAliasAddressFromIfName(const char *ifn)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
1999-10-13 09:00:16 +00:00
|
|
|
size_t needed;
|
|
|
|
int mib[6];
|
|
|
|
char *buf, *lim, *next;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct ifa_msghdr *ifam;
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
struct sockaddr_in *sin;
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
mib[1] = PF_ROUTE;
|
|
|
|
mib[2] = 0;
|
|
|
|
mib[3] = AF_INET; /* Only IP addresses please */
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
mib[5] = 0; /* ifIndex??? */
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Get interface data.
|
|
|
|
*/
|
1999-10-13 09:00:16 +00:00
|
|
|
if (sysctl(mib, 6, NULL, &needed, NULL, 0) == -1)
|
|
|
|
err(1, "iflist-sysctl-estimate");
|
|
|
|
if ((buf = malloc(needed)) == NULL)
|
|
|
|
errx(1, "malloc failed");
|
|
|
|
if (sysctl(mib, 6, buf, &needed, NULL, 0) == -1)
|
|
|
|
err(1, "iflist-sysctl-get");
|
|
|
|
lim = buf + needed;
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Loop through interfaces until one with
|
|
|
|
* given name is found. This is done to
|
|
|
|
* find correct interface index for routing
|
|
|
|
* message processing.
|
|
|
|
*/
|
1999-10-13 09:00:16 +00:00
|
|
|
ifIndex = 0;
|
|
|
|
next = buf;
|
|
|
|
while (next < lim) {
|
|
|
|
ifm = (struct if_msghdr *)next;
|
|
|
|
next += ifm->ifm_msglen;
|
|
|
|
if (ifm->ifm_version != RTM_VERSION) {
|
|
|
|
if (verbose)
|
|
|
|
warnx("routing message version %d "
|
|
|
|
"not understood", ifm->ifm_version);
|
|
|
|
continue;
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
1999-10-13 09:00:16 +00:00
|
|
|
if (ifm->ifm_type == RTM_IFINFO) {
|
|
|
|
sdl = (struct sockaddr_dl *)(ifm + 1);
|
|
|
|
if (strlen(ifn) == sdl->sdl_nlen &&
|
|
|
|
strncmp(ifn, sdl->sdl_data, sdl->sdl_nlen) == 0) {
|
|
|
|
ifIndex = ifm->ifm_index;
|
|
|
|
ifMTU = ifm->ifm_data.ifi_mtu;
|
|
|
|
break;
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
1999-10-13 09:00:16 +00:00
|
|
|
if (!ifIndex)
|
|
|
|
errx(1, "unknown interface name %s", ifn);
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Get interface address.
|
|
|
|
*/
|
1999-10-13 09:00:16 +00:00
|
|
|
sin = NULL;
|
|
|
|
while (next < lim) {
|
|
|
|
ifam = (struct ifa_msghdr *)next;
|
|
|
|
next += ifam->ifam_msglen;
|
|
|
|
if (ifam->ifam_version != RTM_VERSION) {
|
|
|
|
if (verbose)
|
|
|
|
warnx("routing message version %d "
|
|
|
|
"not understood", ifam->ifam_version);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ifam->ifam_type != RTM_NEWADDR)
|
|
|
|
break;
|
|
|
|
if (ifam->ifam_addrs & RTA_IFA) {
|
|
|
|
int i;
|
|
|
|
char *cp = (char *)(ifam + 1);
|
|
|
|
|
|
|
|
#define ROUNDUP(a) \
|
|
|
|
((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
|
|
|
|
#define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
|
|
|
|
|
|
|
|
for (i = 1; i < RTA_IFA; i <<= 1)
|
|
|
|
if (ifam->ifam_addrs & i)
|
|
|
|
ADVANCE(cp, (struct sockaddr *)cp);
|
|
|
|
if (((struct sockaddr *)cp)->sa_family == AF_INET) {
|
|
|
|
sin = (struct sockaddr_in *)cp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (sin == NULL)
|
|
|
|
errx(1, "%s: cannot get interface address", ifn);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-10-13 09:00:16 +00:00
|
|
|
PacketAliasSetAddress(sin->sin_addr);
|
|
|
|
syslog(LOG_INFO, "Aliasing to %s, mtu %d bytes",
|
|
|
|
inet_ntoa(sin->sin_addr), ifMTU);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-10-13 09:00:16 +00:00
|
|
|
free(buf);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void Quit (const char* msg)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
Warn (msg);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void Warn (const char* msg)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
if (background)
|
|
|
|
syslog (LOG_ALERT, "%s (%m)", msg);
|
|
|
|
else
|
1997-10-02 11:43:33 +00:00
|
|
|
warn (msg);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
static void RefreshAddr (int sig)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
if (ifName)
|
|
|
|
assignAliasAddr = 1;
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
static void InitiateShutdown (int sig)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Start timer to allow kernel gracefully
|
|
|
|
* shutdown existing connections when system
|
|
|
|
* is shut down.
|
|
|
|
*/
|
1999-09-02 15:17:25 +00:00
|
|
|
siginterrupt(SIGALRM, 1);
|
1997-06-22 04:19:08 +00:00
|
|
|
signal (SIGALRM, Shutdown);
|
|
|
|
alarm (10);
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
static void Shutdown (int sig)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
running = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Different options recognized by this program.
|
|
|
|
*/
|
|
|
|
|
|
|
|
enum Option {
|
|
|
|
|
|
|
|
PacketAliasOption,
|
|
|
|
Verbose,
|
|
|
|
InPort,
|
|
|
|
OutPort,
|
|
|
|
Port,
|
|
|
|
AliasAddress,
|
|
|
|
InterfaceName,
|
|
|
|
RedirectPort,
|
|
|
|
RedirectAddress,
|
|
|
|
ConfigFile,
|
1999-03-07 18:23:56 +00:00
|
|
|
DynamicMode,
|
|
|
|
PptpAlias,
|
|
|
|
ProxyRule,
|
|
|
|
LogDenied,
|
|
|
|
LogFacility
|
1997-06-22 04:19:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum Param {
|
|
|
|
|
|
|
|
YesNo,
|
|
|
|
Numeric,
|
|
|
|
String,
|
|
|
|
None,
|
|
|
|
Address,
|
|
|
|
Service
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Option information structure (used by ParseOption).
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct OptionInfo {
|
|
|
|
|
|
|
|
enum Option type;
|
|
|
|
int packetAliasOpt;
|
|
|
|
enum Param parm;
|
1999-03-24 20:30:48 +00:00
|
|
|
const char* parmDescription;
|
|
|
|
const char* description;
|
|
|
|
const char* name;
|
|
|
|
const char* shortName;
|
1997-06-22 04:19:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Table of known options.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static struct OptionInfo optionTable[] = {
|
|
|
|
|
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_UNREGISTERED_ONLY,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"alias only unregistered addresses",
|
|
|
|
"unregistered_only",
|
|
|
|
"u" },
|
|
|
|
|
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_LOG,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"enable logging",
|
|
|
|
"log",
|
|
|
|
"l" },
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_PROXY_ONLY,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"proxy only",
|
|
|
|
"proxy_only",
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_REVERSE,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"operate in reverse mode",
|
|
|
|
"reverse",
|
|
|
|
NULL },
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_DENY_INCOMING,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"allow incoming connections",
|
|
|
|
"deny_incoming",
|
|
|
|
"d" },
|
|
|
|
|
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_USE_SOCKETS,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"use sockets to inhibit port conflict",
|
|
|
|
"use_sockets",
|
|
|
|
"s" },
|
|
|
|
|
|
|
|
{ PacketAliasOption,
|
|
|
|
PKT_ALIAS_SAME_PORTS,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"try to keep original port numbers for connections",
|
|
|
|
"same_ports",
|
|
|
|
"m" },
|
|
|
|
|
|
|
|
{ Verbose,
|
|
|
|
0,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"verbose mode, dump packet information",
|
|
|
|
"verbose",
|
|
|
|
"v" },
|
|
|
|
|
|
|
|
{ DynamicMode,
|
|
|
|
0,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"dynamic mode, automatically detect interface address changes",
|
|
|
|
"dynamic",
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ InPort,
|
|
|
|
0,
|
|
|
|
Service,
|
|
|
|
"number|service_name",
|
|
|
|
"set port for incoming packets",
|
|
|
|
"in_port",
|
|
|
|
"i" },
|
|
|
|
|
|
|
|
{ OutPort,
|
|
|
|
0,
|
|
|
|
Service,
|
|
|
|
"number|service_name",
|
|
|
|
"set port for outgoing packets",
|
|
|
|
"out_port",
|
|
|
|
"o" },
|
|
|
|
|
|
|
|
{ Port,
|
|
|
|
0,
|
|
|
|
Service,
|
|
|
|
"number|service_name",
|
|
|
|
"set port (defaults to natd/divert)",
|
|
|
|
"port",
|
|
|
|
"p" },
|
|
|
|
|
|
|
|
{ AliasAddress,
|
|
|
|
0,
|
|
|
|
Address,
|
|
|
|
"x.x.x.x",
|
|
|
|
"address to use for aliasing",
|
|
|
|
"alias_address",
|
|
|
|
"a" },
|
|
|
|
|
|
|
|
{ InterfaceName,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"network_if_name",
|
|
|
|
"take aliasing address from interface",
|
|
|
|
"interface",
|
|
|
|
"n" },
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
{ ProxyRule,
|
1997-06-22 04:19:08 +00:00
|
|
|
0,
|
|
|
|
String,
|
1999-03-07 18:23:56 +00:00
|
|
|
"[type encode_ip_hdr|encode_tcp_stream] port xxxx server "
|
|
|
|
"a.b.c.d:yyyy",
|
|
|
|
"add transparent proxying / destination NAT",
|
|
|
|
"proxy_rule",
|
1997-06-22 04:19:08 +00:00
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ RedirectPort,
|
|
|
|
0,
|
|
|
|
String,
|
1999-03-24 20:30:20 +00:00
|
|
|
"tcp|udp local_addr:local_port_range [public_addr:]public_port_range"
|
|
|
|
" [remote_addr[:remote_port_range]]",
|
|
|
|
"redirect a port (or ports) for incoming traffic",
|
1997-06-22 04:19:08 +00:00
|
|
|
"redirect_port",
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ RedirectAddress,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"local_addr public_addr",
|
|
|
|
"define mapping between local and public addresses",
|
|
|
|
"redirect_address",
|
|
|
|
NULL },
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
{ PptpAlias,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"src",
|
|
|
|
"define inside machine for PPTP traffic",
|
|
|
|
"pptpalias",
|
|
|
|
NULL },
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
{ ConfigFile,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"file_name",
|
|
|
|
"read options from configuration file",
|
|
|
|
"config",
|
1999-03-07 18:23:56 +00:00
|
|
|
"f" },
|
|
|
|
|
|
|
|
{ LogDenied,
|
|
|
|
0,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"enable logging of denied incoming packets",
|
|
|
|
"log_denied",
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ LogFacility,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"facility",
|
|
|
|
"name of syslog facility to use for logging",
|
|
|
|
"log_facility",
|
|
|
|
NULL }
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
};
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
static void ParseOption (const char* option, const char* parms, int cmdLine)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct OptionInfo* info;
|
|
|
|
int yesNoValue;
|
|
|
|
int aliasValue;
|
|
|
|
int numValue;
|
1997-12-10 02:14:57 +00:00
|
|
|
u_short uNumValue;
|
1999-03-24 20:30:48 +00:00
|
|
|
const char* strValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
struct in_addr addrValue;
|
|
|
|
int max;
|
|
|
|
char* end;
|
1999-03-07 18:23:56 +00:00
|
|
|
CODE* fac_record = NULL;
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Find option from table.
|
|
|
|
*/
|
|
|
|
max = sizeof (optionTable) / sizeof (struct OptionInfo);
|
|
|
|
for (i = 0, info = optionTable; i < max; i++, info++) {
|
|
|
|
|
|
|
|
if (!strcmp (info->name, option))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (info->shortName)
|
|
|
|
if (!strcmp (info->shortName, option))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= max) {
|
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
warnx ("unknown option %s", option);
|
1997-06-22 04:19:08 +00:00
|
|
|
Usage ();
|
|
|
|
}
|
|
|
|
|
1997-12-10 02:14:57 +00:00
|
|
|
uNumValue = 0;
|
1997-06-22 04:19:08 +00:00
|
|
|
yesNoValue = 0;
|
|
|
|
numValue = 0;
|
|
|
|
strValue = NULL;
|
|
|
|
/*
|
|
|
|
* Check parameters.
|
|
|
|
*/
|
|
|
|
switch (info->parm) {
|
|
|
|
case YesNo:
|
|
|
|
if (!parms)
|
|
|
|
parms = "yes";
|
|
|
|
|
|
|
|
if (!strcmp (parms, "yes"))
|
|
|
|
yesNoValue = 1;
|
|
|
|
else
|
|
|
|
if (!strcmp (parms, "no"))
|
|
|
|
yesNoValue = 0;
|
1997-10-02 11:43:33 +00:00
|
|
|
else
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s needs yes/no parameter", option);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Service:
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!parms)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s needs service name or "
|
|
|
|
"port number parameter",
|
|
|
|
option);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-12-10 02:14:57 +00:00
|
|
|
uNumValue = StrToPort (parms, "divert");
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Numeric:
|
|
|
|
if (parms)
|
|
|
|
numValue = strtol (parms, &end, 10);
|
|
|
|
else
|
1999-03-24 20:30:48 +00:00
|
|
|
end = NULL;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-10-02 11:43:33 +00:00
|
|
|
if (end == parms)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s needs numeric parameter", option);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case String:
|
|
|
|
strValue = parms;
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!strValue)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s needs parameter", option);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case None:
|
1997-10-02 11:43:33 +00:00
|
|
|
if (parms)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s does not take parameters", option);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Address:
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!parms)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s needs address/host parameter", option);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
StrToAddr (parms, &addrValue);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (info->type) {
|
|
|
|
case PacketAliasOption:
|
|
|
|
|
|
|
|
aliasValue = yesNoValue ? info->packetAliasOpt : 0;
|
1997-08-10 21:55:52 +00:00
|
|
|
PacketAliasSetMode (aliasValue, info->packetAliasOpt);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Verbose:
|
|
|
|
verbose = yesNoValue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DynamicMode:
|
|
|
|
dynamicMode = yesNoValue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InPort:
|
1997-12-10 02:14:57 +00:00
|
|
|
inPort = uNumValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OutPort:
|
1997-12-10 02:14:57 +00:00
|
|
|
outPort = uNumValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Port:
|
1997-12-10 02:14:57 +00:00
|
|
|
inOutPort = uNumValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AliasAddress:
|
|
|
|
memcpy (&aliasAddr, &addrValue, sizeof (struct in_addr));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RedirectPort:
|
|
|
|
SetupPortRedirect (strValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RedirectAddress:
|
|
|
|
SetupAddressRedirect (strValue);
|
|
|
|
break;
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
case PptpAlias:
|
|
|
|
SetupPptpAlias (strValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ProxyRule:
|
|
|
|
PacketAliasProxyRule (strValue);
|
|
|
|
break;
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
case InterfaceName:
|
|
|
|
if (ifName)
|
|
|
|
free (ifName);
|
|
|
|
|
|
|
|
ifName = strdup (strValue);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ConfigFile:
|
|
|
|
ReadConfigFile (strValue);
|
|
|
|
break;
|
1999-03-07 18:23:56 +00:00
|
|
|
|
|
|
|
case LogDenied:
|
|
|
|
logDropped = 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LogFacility:
|
|
|
|
|
|
|
|
fac_record = facilitynames;
|
|
|
|
while (fac_record->c_name != NULL) {
|
|
|
|
|
|
|
|
if (!strcmp (fac_record->c_name, strValue)) {
|
|
|
|
|
|
|
|
logFacility = fac_record->c_val;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
fac_record++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(fac_record->c_name == NULL)
|
|
|
|
errx(1, "Unknown log facility name: %s", strValue);
|
|
|
|
|
|
|
|
break;
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void ReadConfigFile (const char* fileName)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
FILE* file;
|
|
|
|
char buf[128];
|
1999-09-07 15:34:12 +00:00
|
|
|
char *ptr, *p;
|
1997-06-22 04:19:08 +00:00
|
|
|
char* option;
|
|
|
|
|
|
|
|
file = fopen (fileName, "r");
|
|
|
|
if (!file) {
|
|
|
|
|
|
|
|
sprintf (buf, "Cannot open config file %s.\n", fileName);
|
|
|
|
Quit (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (fgets (buf, sizeof (buf), file)) {
|
|
|
|
|
|
|
|
ptr = strchr (buf, '\n');
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "config line too long: %s", buf);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
|
|
|
|
/*
|
1999-09-07 15:34:12 +00:00
|
|
|
* Check for comments, strip off trailing spaces.
|
1997-06-22 04:19:08 +00:00
|
|
|
*/
|
1999-09-07 15:34:12 +00:00
|
|
|
if ((ptr = strchr(buf, '#')))
|
|
|
|
*ptr = '\0';
|
|
|
|
for (ptr = buf; isspace(*ptr); ++ptr)
|
|
|
|
continue;
|
1997-06-22 04:19:08 +00:00
|
|
|
if (*ptr == '\0')
|
|
|
|
continue;
|
1999-09-07 15:34:12 +00:00
|
|
|
for (p = strchr(buf, '\0'); isspace(*--p);)
|
|
|
|
continue;
|
|
|
|
*++p = '\0';
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Extract option name.
|
|
|
|
*/
|
|
|
|
option = ptr;
|
|
|
|
while (*ptr && !isspace (*ptr))
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
if (*ptr != '\0') {
|
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
++ptr;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Skip white space between name and parms.
|
|
|
|
*/
|
|
|
|
while (*ptr && isspace (*ptr))
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
ParseOption (option, *ptr ? ptr : NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose (file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Usage ()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int max;
|
|
|
|
struct OptionInfo* info;
|
|
|
|
|
|
|
|
fprintf (stderr, "Recognized options:\n\n");
|
|
|
|
|
|
|
|
max = sizeof (optionTable) / sizeof (struct OptionInfo);
|
|
|
|
for (i = 0, info = optionTable; i < max; i++, info++) {
|
|
|
|
|
|
|
|
fprintf (stderr, "-%-20s %s\n", info->name,
|
|
|
|
info->parmDescription);
|
|
|
|
|
|
|
|
if (info->shortName)
|
|
|
|
fprintf (stderr, "-%-20s %s\n", info->shortName,
|
|
|
|
info->parmDescription);
|
|
|
|
|
|
|
|
fprintf (stderr, " %s\n\n", info->description);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void SetupPptpAlias (const char* parms)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
|
|
|
struct in_addr srcAddr;
|
|
|
|
|
|
|
|
strcpy (buf, parms);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extract source address.
|
|
|
|
*/
|
1999-03-07 18:23:56 +00:00
|
|
|
ptr = strtok (buf, " \t");
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1999-03-07 18:23:56 +00:00
|
|
|
errx(1, "pptpalias: missing src address");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
StrToAddr (ptr, &srcAddr);
|
|
|
|
PacketAliasPptp (srcAddr);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void SetupPortRedirect (const char* parms)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
|
|
|
struct in_addr localAddr;
|
|
|
|
struct in_addr publicAddr;
|
|
|
|
struct in_addr remoteAddr;
|
1999-03-24 20:30:20 +00:00
|
|
|
port_range portRange;
|
|
|
|
u_short localPort = 0;
|
|
|
|
u_short publicPort = 0;
|
|
|
|
u_short remotePort = 0;
|
|
|
|
u_short numLocalPorts = 0;
|
|
|
|
u_short numPublicPorts = 0;
|
|
|
|
u_short numRemotePorts = 0;
|
1997-06-22 04:19:08 +00:00
|
|
|
int proto;
|
|
|
|
char* protoName;
|
|
|
|
char* separator;
|
1999-03-24 20:30:20 +00:00
|
|
|
int i;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
strcpy (buf, parms);
|
|
|
|
/*
|
|
|
|
* Extract protocol.
|
|
|
|
*/
|
|
|
|
protoName = strtok (buf, " \t");
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!protoName)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "redirect_port: missing protocol");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
proto = StrToProto (protoName);
|
|
|
|
/*
|
|
|
|
* Extract local address.
|
|
|
|
*/
|
|
|
|
ptr = strtok (NULL, " \t");
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "redirect_port: missing local address");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1999-03-24 20:30:20 +00:00
|
|
|
if ( StrToAddrAndPortRange (ptr, &localAddr, protoName, &portRange) != 0 )
|
|
|
|
errx (1, "redirect_port: invalid local port range");
|
|
|
|
|
|
|
|
localPort = GETLOPORT(portRange);
|
|
|
|
numLocalPorts = GETNUMPORTS(portRange);
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
1999-05-13 16:58:31 +00:00
|
|
|
* Extract public port and optionally address.
|
1997-06-22 04:19:08 +00:00
|
|
|
*/
|
|
|
|
ptr = strtok (NULL, " \t");
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "redirect_port: missing public port");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
separator = strchr (ptr, ':');
|
1999-03-24 20:30:20 +00:00
|
|
|
if (separator) {
|
|
|
|
if (StrToAddrAndPortRange (ptr, &publicAddr, protoName, &portRange) != 0 )
|
|
|
|
errx (1, "redirect_port: invalid public port range");
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
else {
|
|
|
|
publicAddr.s_addr = INADDR_ANY;
|
1999-03-24 20:30:20 +00:00
|
|
|
if (StrToPortRange (ptr, protoName, &portRange) != 0)
|
|
|
|
errx (1, "redirect_port: invalid public port range");
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:20 +00:00
|
|
|
publicPort = GETLOPORT(portRange);
|
|
|
|
numPublicPorts = GETNUMPORTS(portRange);
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Extract remote address and optionally port.
|
|
|
|
*/
|
|
|
|
ptr = strtok (NULL, " \t");
|
|
|
|
if (ptr) {
|
|
|
|
separator = strchr (ptr, ':');
|
1999-04-25 22:33:30 +00:00
|
|
|
if (separator) {
|
1999-03-24 20:30:20 +00:00
|
|
|
if (StrToAddrAndPortRange (ptr, &remoteAddr, protoName, &portRange) != 0)
|
|
|
|
errx (1, "redirect_port: invalid remote port range");
|
1999-04-25 22:33:30 +00:00
|
|
|
} else {
|
1999-03-24 20:30:20 +00:00
|
|
|
SETLOPORT(portRange, 0);
|
|
|
|
SETNUMPORTS(portRange, 1);
|
1997-06-22 04:19:08 +00:00
|
|
|
StrToAddr (ptr, &remoteAddr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1999-03-24 20:30:20 +00:00
|
|
|
SETLOPORT(portRange, 0);
|
|
|
|
SETNUMPORTS(portRange, 1);
|
1997-06-22 04:19:08 +00:00
|
|
|
remoteAddr.s_addr = INADDR_ANY;
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:20 +00:00
|
|
|
remotePort = GETLOPORT(portRange);
|
|
|
|
numRemotePorts = GETNUMPORTS(portRange);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure port ranges match up, then add the redirect ports.
|
|
|
|
*/
|
|
|
|
if (numLocalPorts != numPublicPorts)
|
|
|
|
errx (1, "redirect_port: port ranges must be equal in size");
|
|
|
|
|
|
|
|
/* Remote port range is allowed to be '0' which means all ports. */
|
1999-05-13 17:09:44 +00:00
|
|
|
if (numRemotePorts != numLocalPorts && (numRemotePorts != 1 || remotePort != 0))
|
1999-03-24 20:30:20 +00:00
|
|
|
errx (1, "redirect_port: remote port must be 0 or equal to local port range in size");
|
|
|
|
|
|
|
|
for (i = 0 ; i < numPublicPorts ; ++i) {
|
|
|
|
/* If remotePort is all ports, set it to 0. */
|
|
|
|
u_short remotePortCopy = remotePort + i;
|
|
|
|
if (numRemotePorts == 1 && remotePort == 0)
|
|
|
|
remotePortCopy = 0;
|
|
|
|
|
|
|
|
PacketAliasRedirectPort (localAddr,
|
|
|
|
htons(localPort + i),
|
|
|
|
remoteAddr,
|
|
|
|
htons(remotePortCopy),
|
|
|
|
publicAddr,
|
|
|
|
htons(publicPort + i),
|
|
|
|
proto);
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void SetupAddressRedirect (const char* parms)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
|
|
|
struct in_addr localAddr;
|
|
|
|
struct in_addr publicAddr;
|
|
|
|
|
|
|
|
strcpy (buf, parms);
|
|
|
|
/*
|
|
|
|
* Extract local address.
|
|
|
|
*/
|
|
|
|
ptr = strtok (buf, " \t");
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "redirect_address: missing local address");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
StrToAddr (ptr, &localAddr);
|
|
|
|
/*
|
|
|
|
* Extract public address.
|
|
|
|
*/
|
|
|
|
ptr = strtok (NULL, " \t");
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "redirect_address: missing public address");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
StrToAddr (ptr, &publicAddr);
|
|
|
|
PacketAliasRedirectAddr (localAddr, publicAddr);
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
void StrToAddr (const char* str, struct in_addr* addr)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
struct hostent* hp;
|
|
|
|
|
|
|
|
if (inet_aton (str, addr))
|
|
|
|
return;
|
|
|
|
|
|
|
|
hp = gethostbyname (str);
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!hp)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "unknown host %s", str);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
memcpy (addr, hp->h_addr, sizeof (struct in_addr));
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
u_short StrToPort (const char* str, const char* proto)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
1997-12-10 02:14:57 +00:00
|
|
|
u_short port;
|
1997-06-22 04:19:08 +00:00
|
|
|
struct servent* sp;
|
|
|
|
char* end;
|
|
|
|
|
|
|
|
port = strtol (str, &end, 10);
|
|
|
|
if (end != str)
|
1997-12-06 12:00:32 +00:00
|
|
|
return htons (port);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
sp = getservbyname (str, proto);
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!sp)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "unknown service %s/%s", str, proto);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
return sp->s_port;
|
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
int StrToPortRange (const char* str, const char* proto, port_range *portRange)
|
1999-03-24 20:30:20 +00:00
|
|
|
{
|
|
|
|
char* sep;
|
|
|
|
struct servent* sp;
|
|
|
|
char* end;
|
|
|
|
u_short loPort;
|
|
|
|
u_short hiPort;
|
|
|
|
|
|
|
|
/* First see if this is a service, return corresponding port if so. */
|
|
|
|
sp = getservbyname (str,proto);
|
|
|
|
if (sp) {
|
|
|
|
SETLOPORT(*portRange, ntohs(sp->s_port));
|
|
|
|
SETNUMPORTS(*portRange, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Not a service, see if it's a single port or port range. */
|
|
|
|
sep = strchr (str, '-');
|
|
|
|
if (sep == NULL) {
|
|
|
|
SETLOPORT(*portRange, strtol(str, &end, 10));
|
|
|
|
if (end != str) {
|
|
|
|
/* Single port. */
|
|
|
|
SETNUMPORTS(*portRange, 1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Error in port range field. */
|
|
|
|
errx (1, "unknown service %s/%s", str, proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Port range, get the values and sanity check. */
|
|
|
|
sscanf (str, "%hu-%hu", &loPort, &hiPort);
|
|
|
|
SETLOPORT(*portRange, loPort);
|
|
|
|
SETNUMPORTS(*portRange, 0); /* Error by default */
|
|
|
|
if (loPort <= hiPort)
|
|
|
|
SETNUMPORTS(*portRange, hiPort - loPort + 1);
|
|
|
|
|
|
|
|
if (GETNUMPORTS(*portRange) == 0)
|
|
|
|
errx (1, "invalid port range %s", str);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
int StrToProto (const char* str)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
if (!strcmp (str, "tcp"))
|
|
|
|
return IPPROTO_TCP;
|
|
|
|
|
|
|
|
if (!strcmp (str, "udp"))
|
|
|
|
return IPPROTO_UDP;
|
|
|
|
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "unknown protocol %s. Expected tcp or udp", str);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
1999-03-24 20:30:48 +00:00
|
|
|
int StrToAddrAndPortRange (const char* str, struct in_addr* addr, char* proto, port_range *portRange)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
char* ptr;
|
|
|
|
|
|
|
|
ptr = strchr (str, ':');
|
1997-10-02 11:43:33 +00:00
|
|
|
if (!ptr)
|
1997-12-10 02:14:57 +00:00
|
|
|
errx (1, "%s is missing port number", str);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
*ptr = '\0';
|
|
|
|
++ptr;
|
|
|
|
|
|
|
|
StrToAddr (str, addr);
|
1999-03-24 20:30:20 +00:00
|
|
|
return StrToPortRange (ptr, proto, portRange);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|