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
|
|
|
*/
|
|
|
|
|
2003-02-05 20:08:39 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
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>
|
2004-07-04 12:53:54 +00:00
|
|
|
#include <sys/queue.h>
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
#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"
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
struct instance {
|
|
|
|
const char *name;
|
|
|
|
struct libalias *la;
|
|
|
|
LIST_ENTRY(instance) list;
|
|
|
|
|
|
|
|
int ifIndex;
|
|
|
|
int assignAliasAddr;
|
|
|
|
char* ifName;
|
|
|
|
int logDropped;
|
|
|
|
u_short inPort;
|
|
|
|
u_short outPort;
|
|
|
|
u_short inOutPort;
|
|
|
|
struct in_addr aliasAddr;
|
|
|
|
int ifMTU;
|
|
|
|
int aliasOverhead;
|
|
|
|
int dropIgnoredIncoming;
|
|
|
|
int divertIn;
|
|
|
|
int divertOut;
|
|
|
|
int divertInOut;
|
|
|
|
};
|
|
|
|
|
|
|
|
static LIST_HEAD(, instance) root = LIST_HEAD_INITIALIZER(&root);
|
|
|
|
|
|
|
|
struct libalias *mla;
|
|
|
|
struct instance *mip;
|
|
|
|
int ninstance = 1;
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2000-06-16 09:41:57 +00:00
|
|
|
static void ParseOption (const char* option, const char* parms);
|
1999-03-24 20:30:48 +00:00
|
|
|
static void ReadConfigFile (const char* fileName);
|
|
|
|
static void SetupPortRedirect (const char* parms);
|
2000-05-03 15:06:45 +00:00
|
|
|
static void SetupProtoRedirect(const char* parms);
|
1999-03-24 20:30:48 +00:00
|
|
|
static void SetupAddressRedirect (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);
|
2000-06-27 15:26:24 +00:00
|
|
|
static void SetupPunchFW(const char *strValue);
|
2003-09-23 07:41:55 +00:00
|
|
|
static void SetupSkinnyPort(const char *strValue);
|
2004-07-04 12:53:54 +00:00
|
|
|
static void NewInstance(const char *name);
|
|
|
|
static void DoGlobal (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;
|
2004-07-04 12:53:54 +00:00
|
|
|
static int logFacility;
|
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
static int dynamicMode;
|
|
|
|
static int icmpSock;
|
2001-11-27 11:00:16 +00:00
|
|
|
static int logIpfwDenied;
|
2003-08-13 13:16:19 +00:00
|
|
|
static char* pidName;
|
2004-07-04 12:53:54 +00:00
|
|
|
static int routeSock;
|
|
|
|
static int globalPort;
|
|
|
|
static int divertGlobal;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
int main (int argc, char** argv)
|
|
|
|
{
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
fd_set readMask;
|
|
|
|
int fdMax;
|
|
|
|
/*
|
|
|
|
* Initialize packet aliasing software.
|
|
|
|
* Done already here to be able to alter option bits
|
|
|
|
* during command line and configuration file processing.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
NewInstance("default");
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Parse options.
|
|
|
|
*/
|
|
|
|
verbose = 0;
|
|
|
|
background = 0;
|
|
|
|
running = 1;
|
|
|
|
dynamicMode = 0;
|
1999-03-07 18:23:56 +00:00
|
|
|
logFacility = LOG_DAEMON;
|
2001-11-27 11:06:02 +00:00
|
|
|
logIpfwDenied = -1;
|
2003-08-13 13:16:19 +00:00
|
|
|
pidName = PIDFILE;
|
2004-07-04 12:53:54 +00:00
|
|
|
routeSock = -1;
|
|
|
|
icmpSock = -1;
|
|
|
|
fdMax = -1;
|
|
|
|
divertGlobal = -1;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
ParseArgs (argc, argv);
|
2001-11-27 11:06:02 +00:00
|
|
|
/*
|
|
|
|
* Log ipfw(8) denied packets by default in verbose mode.
|
|
|
|
*/
|
|
|
|
if (logIpfwDenied == -1)
|
|
|
|
logIpfwDenied = verbose;
|
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);
|
2004-07-04 12:53:54 +00:00
|
|
|
|
|
|
|
LIST_FOREACH(mip, &root, list) {
|
|
|
|
mla = mip->la;
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
2003-06-13 22:15:42 +00:00
|
|
|
* If not doing the transparent proxying only,
|
|
|
|
* check that valid aliasing address has been given.
|
1997-06-22 04:19:08 +00:00
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->aliasAddr.s_addr == INADDR_NONE && mip->ifName == NULL &&
|
|
|
|
!(LibAliasSetMode(mla, 0,0) & PKT_ALIAS_PROXY_ONLY))
|
|
|
|
errx (1, "instance %s: aliasing address not given", mip->name);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->aliasAddr.s_addr != INADDR_NONE && mip->ifName != NULL)
|
|
|
|
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.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->inPort != 0 || mip->outPort != 0)
|
|
|
|
if (mip->inPort == 0 || mip->outPort == 0)
|
|
|
|
errx (1, "both input and output ports are required");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->inPort == 0 && mip->outPort == 0 && mip->inOutPort == 0)
|
|
|
|
ParseOption ("port", DEFAULT_SERVICE);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-09-06 11:14:03 +00:00
|
|
|
/*
|
|
|
|
* Check if ignored packets should be dropped.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->dropIgnoredIncoming = LibAliasSetMode (mla, 0, 0);
|
|
|
|
mip->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.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->inOutPort) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->divertInOut = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (mip->divertInOut == -1)
|
|
|
|
Quit ("Unable to create divert socket.");
|
|
|
|
if (mip->divertInOut > fdMax)
|
|
|
|
fdMax = mip->divertInOut;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->divertIn = -1;
|
|
|
|
mip->divertOut = -1;
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Bind socket.
|
|
|
|
*/
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = mip->inOutPort;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (bind (mip->divertInOut,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind divert socket.");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
mip->divertIn = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (mip->divertIn == -1)
|
|
|
|
Quit ("Unable to create incoming divert socket.");
|
|
|
|
if (mip->divertIn > fdMax)
|
|
|
|
fdMax = mip->divertIn;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->divertOut = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (mip->divertOut == -1)
|
|
|
|
Quit ("Unable to create outgoing divert socket.");
|
|
|
|
if (mip->divertOut > fdMax)
|
|
|
|
fdMax = mip->divertOut;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->divertInOut = -1;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind divert sockets.
|
|
|
|
*/
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = mip->inPort;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (bind (mip->divertIn,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind incoming divert socket.");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = mip->outPort;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (bind (mip->divertOut,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind outgoing divert socket.");
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
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
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->ifName) {
|
|
|
|
if (dynamicMode) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (routeSock == -1)
|
|
|
|
routeSock = socket (PF_ROUTE, SOCK_RAW, 0);
|
|
|
|
if (routeSock == -1)
|
|
|
|
Quit ("Unable to create routing info socket.");
|
|
|
|
if (routeSock > fdMax)
|
|
|
|
fdMax = routeSock;
|
1999-09-28 08:01:46 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->assignAliasAddr = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SetAliasAddressFromIfName (mip->ifName);
|
1999-09-28 08:01:46 +00:00
|
|
|
}
|
2004-07-04 12:53:54 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
if (globalPort) {
|
|
|
|
|
|
|
|
divertGlobal = socket (PF_INET, SOCK_RAW, IPPROTO_DIVERT);
|
|
|
|
if (divertGlobal == -1)
|
|
|
|
Quit ("Unable to create divert socket.");
|
|
|
|
if (divertGlobal > fdMax)
|
|
|
|
fdMax = divertGlobal;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Bind socket.
|
|
|
|
*/
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
|
|
|
addr.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
addr.sin_port = globalPort;
|
|
|
|
|
|
|
|
if (bind (divertGlobal,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr) == -1)
|
|
|
|
Quit ("Unable to bind global divert socket.");
|
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.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
mip = LIST_FIRST(&root); /* XXX: simon */
|
|
|
|
LIST_FOREACH(mip, &root, list) {
|
|
|
|
mla = mip->la;
|
|
|
|
if (mip->aliasAddr.s_addr != INADDR_NONE)
|
|
|
|
LibAliasSetAddress (mla, mip->aliasAddr);
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
while (running) {
|
2004-07-04 12:53:54 +00:00
|
|
|
mip = LIST_FIRST(&root); /* XXX: simon */
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->divertInOut != -1 && !mip->ifName && ninstance == 1) {
|
1997-08-10 21:55:52 +00:00
|
|
|
/*
|
|
|
|
* When using only one socket, just call
|
|
|
|
* DoAliasing repeatedly to process packets.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
DoAliasing (mip->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);
|
|
|
|
/*
|
2002-01-15 17:07:56 +00:00
|
|
|
* Check if new packets are available.
|
1997-08-10 21:55:52 +00:00
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
LIST_FOREACH(mip, &root, list) {
|
|
|
|
if (mip->divertIn != -1)
|
|
|
|
FD_SET (mip->divertIn, &readMask);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->divertOut != -1)
|
|
|
|
FD_SET (mip->divertOut, &readMask);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->divertInOut != -1)
|
|
|
|
FD_SET (mip->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
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (divertGlobal != -1)
|
|
|
|
FD_SET (divertGlobal, &readMask);
|
|
|
|
|
1997-08-10 21:55:52 +00:00
|
|
|
if (select (fdMax + 1,
|
|
|
|
&readMask,
|
2002-01-15 17:07:56 +00:00
|
|
|
NULL,
|
1997-08-10 21:55:52 +00:00
|
|
|
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
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (divertGlobal != -1)
|
|
|
|
if (FD_ISSET (divertGlobal, &readMask))
|
|
|
|
DoGlobal (divertGlobal);
|
|
|
|
LIST_FOREACH(mip, &root, list) {
|
|
|
|
mla = mip->la;
|
|
|
|
if (mip->divertIn != -1)
|
|
|
|
if (FD_ISSET (mip->divertIn, &readMask))
|
|
|
|
DoAliasing (mip->divertIn, INPUT);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->divertOut != -1)
|
|
|
|
if (FD_ISSET (mip->divertOut, &readMask))
|
|
|
|
DoAliasing (mip->divertOut, OUTPUT);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->divertInOut != -1)
|
|
|
|
if (FD_ISSET (mip->divertInOut, &readMask))
|
|
|
|
DoAliasing (mip->divertInOut, DONT_KNOW);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +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)
|
2003-08-13 13:16:19 +00:00
|
|
|
unlink (pidName);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void DaemonMode ()
|
|
|
|
{
|
|
|
|
FILE* pidFile;
|
|
|
|
|
|
|
|
daemon (0, 0);
|
|
|
|
background = 1;
|
|
|
|
|
2003-08-13 13:16:19 +00:00
|
|
|
pidFile = fopen (pidName, "w");
|
1997-06-22 04:19:08 +00:00
|
|
|
if (pidFile) {
|
|
|
|
|
|
|
|
fprintf (pidFile, "%d\n", getpid ());
|
|
|
|
fclose (pidFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ParseArgs (int argc, char** argv)
|
|
|
|
{
|
|
|
|
int arg;
|
|
|
|
char* opt;
|
|
|
|
char parmBuf[256];
|
2000-04-30 20:53:54 +00:00
|
|
|
int len; /* bounds checking */
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
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 ();
|
|
|
|
}
|
|
|
|
|
|
|
|
parmBuf[0] = '\0';
|
2000-04-30 20:53:54 +00:00
|
|
|
len = 0;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
while (arg < argc - 1) {
|
|
|
|
|
|
|
|
if (argv[arg + 1][0] == '-')
|
|
|
|
break;
|
|
|
|
|
2000-06-16 09:41:57 +00:00
|
|
|
if (len) {
|
2000-04-30 20:53:54 +00:00
|
|
|
strncat (parmBuf, " ", sizeof(parmBuf) - (len + 1));
|
|
|
|
len += strlen(parmBuf + len);
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
++arg;
|
2000-04-30 20:53:54 +00:00
|
|
|
strncat (parmBuf, argv[arg], sizeof(parmBuf) - (len + 1));
|
|
|
|
len += strlen(parmBuf + len);
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2000-06-16 09:41:57 +00:00
|
|
|
ParseOption (opt + 1, (len ? parmBuf : NULL));
|
2000-04-30 20:53:54 +00:00
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
static void DoGlobal (int fd)
|
|
|
|
{
|
|
|
|
int bytes;
|
|
|
|
int origBytes;
|
|
|
|
char buf[IP_MAXPACKET];
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int wrote;
|
|
|
|
int addrSize;
|
|
|
|
struct ip* ip;
|
|
|
|
char msgBuf[80];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get packet from socket.
|
|
|
|
*/
|
|
|
|
addrSize = sizeof addr;
|
|
|
|
origBytes = recvfrom (fd,
|
|
|
|
buf,
|
|
|
|
sizeof buf,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
&addrSize);
|
|
|
|
|
|
|
|
if (origBytes == -1) {
|
|
|
|
|
|
|
|
if (errno != EINTR)
|
|
|
|
Warn ("read from divert socket failed");
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
if (mip->assignAliasAddr) {
|
|
|
|
SetAliasAddressFromIfName (mip->ifName);
|
|
|
|
mip->assignAliasAddr = 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* This is an IP packet.
|
|
|
|
*/
|
|
|
|
ip = (struct ip*) buf;
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
/*
|
|
|
|
* Print packet direction and protocol type.
|
|
|
|
*/
|
|
|
|
printf ("Glb ");
|
|
|
|
|
|
|
|
switch (ip->ip_p) {
|
|
|
|
case IPPROTO_TCP:
|
|
|
|
printf ("[TCP] ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPPROTO_UDP:
|
|
|
|
printf ("[UDP] ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IPPROTO_ICMP:
|
|
|
|
printf ("[ICMP] ");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf ("[%d] ", ip->ip_p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Print addresses.
|
|
|
|
*/
|
|
|
|
PrintPacket (ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
LIST_FOREACH(mip, &root, list) {
|
|
|
|
mla = mip->la;
|
|
|
|
if (LibAliasOutTry (mla, buf, IP_MAXPACKET, 0) != PKT_ALIAS_IGNORED)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Length might have changed during aliasing.
|
|
|
|
*/
|
|
|
|
bytes = ntohs (ip->ip_len);
|
|
|
|
/*
|
|
|
|
* Update alias overhead size for outgoing packets.
|
|
|
|
*/
|
|
|
|
if (mip != NULL && bytes - origBytes > mip->aliasOverhead)
|
|
|
|
mip->aliasOverhead = bytes - origBytes;
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print addresses after aliasing.
|
|
|
|
*/
|
|
|
|
printf (" aliased to\n");
|
|
|
|
printf (" ");
|
|
|
|
PrintPacket (ip);
|
|
|
|
printf ("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Put packet back for processing.
|
|
|
|
*/
|
|
|
|
wrote = sendto (fd,
|
|
|
|
buf,
|
|
|
|
bytes,
|
|
|
|
0,
|
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr);
|
|
|
|
|
|
|
|
if (wrote != bytes) {
|
|
|
|
|
|
|
|
if (errno == EMSGSIZE) {
|
|
|
|
|
|
|
|
if (mip->ifMTU != -1)
|
|
|
|
SendNeedFragIcmp (icmpSock,
|
|
|
|
(struct ip*) buf,
|
|
|
|
mip->ifMTU - mip->aliasOverhead);
|
|
|
|
}
|
|
|
|
else if (errno == EACCES && logIpfwDenied) {
|
|
|
|
|
|
|
|
sprintf (msgBuf, "failed to write packet back");
|
|
|
|
Warn (msgBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
2002-01-15 17:07:56 +00:00
|
|
|
char buf[IP_MAXPACKET];
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
int wrote;
|
1997-09-06 11:14:03 +00:00
|
|
|
int status;
|
1997-06-22 04:19:08 +00:00
|
|
|
int addrSize;
|
|
|
|
struct ip* ip;
|
2002-01-15 17:07:56 +00:00
|
|
|
char msgBuf[80];
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->assignAliasAddr) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
SetAliasAddressFromIfName (mip->ifName);
|
|
|
|
mip->assignAliasAddr = 0;
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Get packet from socket.
|
|
|
|
*/
|
2002-01-15 17:07:56 +00:00
|
|
|
addrSize = sizeof addr;
|
1997-06-22 04:19:08 +00:00
|
|
|
origBytes = recvfrom (fd,
|
2002-01-15 17:07:56 +00:00
|
|
|
buf,
|
|
|
|
sizeof buf,
|
1997-06-22 04:19:08 +00:00
|
|
|
0,
|
2002-01-15 17:07:56 +00:00
|
|
|
(struct sockaddr*) &addr,
|
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;
|
|
|
|
}
|
|
|
|
/*
|
2003-01-01 18:49:04 +00:00
|
|
|
* This is an IP packet.
|
1997-06-22 04:19:08 +00:00
|
|
|
*/
|
2002-01-15 17:07:56 +00:00
|
|
|
ip = (struct ip*) buf;
|
1999-04-25 22:33:30 +00:00
|
|
|
if (direction == DONT_KNOW) {
|
2002-01-15 17:07:56 +00:00
|
|
|
if (addr.sin_addr.s_addr == INADDR_ANY)
|
1999-03-07 18:23:56 +00:00
|
|
|
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 ");
|
2004-07-04 12:53:54 +00:00
|
|
|
if (ninstance > 1)
|
|
|
|
printf ("{%s} %08x", mip->name);
|
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.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasOut (mla, buf, 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.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
status = LibAliasIn (mla, buf, IP_MAXPACKET);
|
1997-09-06 11:14:03 +00:00
|
|
|
if (status == PKT_ALIAS_IGNORED &&
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->dropIgnoredIncoming) {
|
1997-09-06 11:14:03 +00:00
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
if (verbose)
|
|
|
|
printf (" dropped.\n");
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->logDropped)
|
1999-03-07 18:23:56 +00:00
|
|
|
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 &&
|
2004-07-04 12:53:54 +00:00
|
|
|
bytes - origBytes > mip->aliasOverhead)
|
|
|
|
mip->aliasOverhead = bytes - origBytes;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Print addresses after aliasing.
|
|
|
|
*/
|
|
|
|
printf (" aliased to\n");
|
|
|
|
printf (" ");
|
|
|
|
PrintPacket (ip);
|
|
|
|
printf ("\n");
|
|
|
|
}
|
1997-08-10 21:55:52 +00:00
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* Put packet back for processing.
|
|
|
|
*/
|
|
|
|
wrote = sendto (fd,
|
2002-01-15 17:07:56 +00:00
|
|
|
buf,
|
|
|
|
bytes,
|
1997-06-22 04:19:08 +00:00
|
|
|
0,
|
2002-01-15 17:07:56 +00:00
|
|
|
(struct sockaddr*) &addr,
|
|
|
|
sizeof addr);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2002-01-15 17:07:56 +00:00
|
|
|
if (wrote != bytes) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
if (errno == EMSGSIZE) {
|
|
|
|
|
2002-01-15 17:07:56 +00:00
|
|
|
if (direction == OUTPUT &&
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->ifMTU != -1)
|
1997-06-22 04:19:08 +00:00
|
|
|
SendNeedFragIcmp (icmpSock,
|
2002-01-15 17:07:56 +00:00
|
|
|
(struct ip*) buf,
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->ifMTU - mip->aliasOverhead);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
2001-11-27 11:00:16 +00:00
|
|
|
else if (errno == EACCES && logIpfwDenied) {
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2001-06-21 12:32:36 +00:00
|
|
|
sprintf (msgBuf, "failed to write packet back");
|
1997-06-22 04:19:08 +00:00
|
|
|
Warn (msgBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
if ((ifMsg.ifm_type == RTM_NEWADDR || ifMsg.ifm_type == RTM_IFINFO)) {
|
|
|
|
LIST_FOREACH(mip, &root, list) {
|
|
|
|
mla = mip->la;
|
|
|
|
if (ifMsg.ifm_index == mip->ifIndex) {
|
|
|
|
if (verbose)
|
|
|
|
printf("Interface address/MTU has probably changed.\n");
|
|
|
|
mip->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.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->ifIndex = 0;
|
1999-10-13 09:00:16 +00:00
|
|
|
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) {
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->ifIndex = ifm->ifm_index;
|
|
|
|
mip->ifMTU = ifm->ifm_data.ifi_mtu;
|
1999-10-13 09:00:16 +00:00
|
|
|
break;
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
}
|
2004-07-04 12:53:54 +00:00
|
|
|
if (!mip->ifIndex)
|
1999-10-13 09:00:16 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
for (i = 1; i < RTA_IFA; i <<= 1)
|
|
|
|
if (ifam->ifam_addrs & i)
|
2004-04-13 11:24:43 +00:00
|
|
|
cp += SA_SIZE((struct sockaddr *)cp);
|
1999-10-13 09:00:16 +00:00
|
|
|
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
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasSetAddress(mla, sin->sin_addr);
|
1999-10-13 09:00:16 +00:00
|
|
|
syslog(LOG_INFO, "Aliasing to %s, mtu %d bytes",
|
2004-07-04 12:53:54 +00:00
|
|
|
inet_ntoa(sin->sin_addr), mip->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
|
2000-07-10 08:14:18 +00:00
|
|
|
warn ("%s", 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
|
|
|
{
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->ifName)
|
|
|
|
mip->assignAliasAddr = 1;
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasOption,
|
|
|
|
Instance,
|
1997-06-22 04:19:08 +00:00
|
|
|
Verbose,
|
|
|
|
InPort,
|
|
|
|
OutPort,
|
|
|
|
Port,
|
2004-07-04 12:53:54 +00:00
|
|
|
GlobalPort,
|
1997-06-22 04:19:08 +00:00
|
|
|
AliasAddress,
|
2000-05-18 10:31:10 +00:00
|
|
|
TargetAddress,
|
1997-06-22 04:19:08 +00:00
|
|
|
InterfaceName,
|
|
|
|
RedirectPort,
|
2000-05-03 15:06:45 +00:00
|
|
|
RedirectProto,
|
1997-06-22 04:19:08 +00:00
|
|
|
RedirectAddress,
|
|
|
|
ConfigFile,
|
1999-03-07 18:23:56 +00:00
|
|
|
DynamicMode,
|
|
|
|
ProxyRule,
|
|
|
|
LogDenied,
|
2000-06-27 15:26:24 +00:00
|
|
|
LogFacility,
|
2001-10-31 16:08:49 +00:00
|
|
|
PunchFW,
|
2003-09-23 07:41:55 +00:00
|
|
|
SkinnyPort,
|
2003-08-13 13:16:19 +00:00
|
|
|
LogIpfwDenied,
|
|
|
|
PidFile
|
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[] = {
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1997-06-22 04:19:08 +00:00
|
|
|
PKT_ALIAS_UNREGISTERED_ONLY,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"alias only unregistered addresses",
|
|
|
|
"unregistered_only",
|
|
|
|
"u" },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1997-06-22 04:19:08 +00:00
|
|
|
PKT_ALIAS_LOG,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"enable logging",
|
|
|
|
"log",
|
|
|
|
"l" },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1999-03-07 18:23:56 +00:00
|
|
|
PKT_ALIAS_PROXY_ONLY,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"proxy only",
|
|
|
|
"proxy_only",
|
|
|
|
NULL },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1999-03-07 18:23:56 +00:00
|
|
|
PKT_ALIAS_REVERSE,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"operate in reverse mode",
|
|
|
|
"reverse",
|
|
|
|
NULL },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1997-06-22 04:19:08 +00:00
|
|
|
PKT_ALIAS_DENY_INCOMING,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"allow incoming connections",
|
|
|
|
"deny_incoming",
|
|
|
|
"d" },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1997-06-22 04:19:08 +00:00
|
|
|
PKT_ALIAS_USE_SOCKETS,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"use sockets to inhibit port conflict",
|
|
|
|
"use_sockets",
|
|
|
|
"s" },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ LibAliasOption,
|
1997-06-22 04:19:08 +00:00
|
|
|
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" },
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
{ GlobalPort,
|
|
|
|
0,
|
|
|
|
Service,
|
|
|
|
"number|service_name",
|
|
|
|
"set globalport",
|
|
|
|
"globalport",
|
|
|
|
NULL },
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
{ AliasAddress,
|
|
|
|
0,
|
|
|
|
Address,
|
|
|
|
"x.x.x.x",
|
|
|
|
"address to use for aliasing",
|
|
|
|
"alias_address",
|
|
|
|
"a" },
|
|
|
|
|
2000-05-18 10:31:10 +00:00
|
|
|
{ TargetAddress,
|
|
|
|
0,
|
|
|
|
Address,
|
|
|
|
"x.x.x.x",
|
|
|
|
"address to use for incoming sessions",
|
|
|
|
"target_address",
|
|
|
|
"t" },
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
{ 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,
|
2000-04-27 17:55:17 +00:00
|
|
|
"tcp|udp local_addr:local_port_range[,...] [public_addr:]public_port_range"
|
1999-03-24 20:30:20 +00:00
|
|
|
" [remote_addr[:remote_port_range]]",
|
|
|
|
"redirect a port (or ports) for incoming traffic",
|
1997-06-22 04:19:08 +00:00
|
|
|
"redirect_port",
|
|
|
|
NULL },
|
|
|
|
|
2000-05-03 15:06:45 +00:00
|
|
|
{ RedirectProto,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"proto local_addr [public_addr] [remote_addr]",
|
|
|
|
"redirect packets of a given proto",
|
|
|
|
"redirect_proto",
|
|
|
|
NULL },
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
{ RedirectAddress,
|
|
|
|
0,
|
|
|
|
String,
|
2000-04-27 17:55:17 +00:00
|
|
|
"local_addr[,...] public_addr",
|
1997-06-22 04:19:08 +00:00
|
|
|
"define mapping between local and public addresses",
|
|
|
|
"redirect_address",
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ 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",
|
2000-06-27 15:26:24 +00:00
|
|
|
NULL },
|
1999-03-07 18:23:56 +00:00
|
|
|
|
2000-06-27 15:26:24 +00:00
|
|
|
{ PunchFW,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"basenumber:count",
|
|
|
|
"punch holes in the firewall for incoming FTP/IRC DCC connections",
|
|
|
|
"punch_fw",
|
2001-10-31 16:08:49 +00:00
|
|
|
NULL },
|
|
|
|
|
2003-09-23 07:41:55 +00:00
|
|
|
{ SkinnyPort,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"port",
|
|
|
|
"set the TCP port for use with the Skinny Station protocol",
|
|
|
|
"skinny_port",
|
|
|
|
NULL },
|
|
|
|
|
2001-10-31 16:08:49 +00:00
|
|
|
{ LogIpfwDenied,
|
|
|
|
0,
|
|
|
|
YesNo,
|
|
|
|
"[yes|no]",
|
|
|
|
"log packets converted by natd, but denied by ipfw",
|
|
|
|
"log_ipfw_denied",
|
|
|
|
NULL },
|
2003-08-13 13:16:19 +00:00
|
|
|
|
|
|
|
{ PidFile,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"file_name",
|
|
|
|
"store PID in an alternate file",
|
|
|
|
"pid_file",
|
|
|
|
"P" },
|
2004-07-04 12:53:54 +00:00
|
|
|
{ Instance,
|
|
|
|
0,
|
|
|
|
String,
|
|
|
|
"instance name",
|
|
|
|
"name of aliasing engine instance",
|
|
|
|
"instance",
|
|
|
|
NULL },
|
1997-06-22 04:19:08 +00:00
|
|
|
};
|
|
|
|
|
2000-06-16 09:41:57 +00:00
|
|
|
static void ParseOption (const char* option, const char* parms)
|
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) {
|
2004-07-04 12:53:54 +00:00
|
|
|
case LibAliasOption:
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
aliasValue = yesNoValue ? info->packetAliasOpt : 0;
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasSetMode (mla, aliasValue, info->packetAliasOpt);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Verbose:
|
|
|
|
verbose = yesNoValue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DynamicMode:
|
|
|
|
dynamicMode = yesNoValue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case InPort:
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->inPort = uNumValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case OutPort:
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->outPort = uNumValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Port:
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->inOutPort = uNumValue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GlobalPort:
|
|
|
|
globalPort = uNumValue;
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case AliasAddress:
|
2004-07-04 12:53:54 +00:00
|
|
|
memcpy (&mip->aliasAddr, &addrValue, sizeof (struct in_addr));
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
2000-05-18 10:31:10 +00:00
|
|
|
case TargetAddress:
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasSetTarget(mla, addrValue);
|
2000-05-18 10:31:10 +00:00
|
|
|
break;
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
case RedirectPort:
|
|
|
|
SetupPortRedirect (strValue);
|
|
|
|
break;
|
|
|
|
|
2000-05-03 15:06:45 +00:00
|
|
|
case RedirectProto:
|
|
|
|
SetupProtoRedirect(strValue);
|
|
|
|
break;
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
case RedirectAddress:
|
|
|
|
SetupAddressRedirect (strValue);
|
|
|
|
break;
|
|
|
|
|
1999-03-07 18:23:56 +00:00
|
|
|
case ProxyRule:
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasProxyRule (mla, strValue);
|
1999-03-07 18:23:56 +00:00
|
|
|
break;
|
|
|
|
|
1997-06-22 04:19:08 +00:00
|
|
|
case InterfaceName:
|
2004-07-04 12:53:54 +00:00
|
|
|
if (mip->ifName)
|
|
|
|
free (mip->ifName);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->ifName = strdup (strValue);
|
1997-06-22 04:19:08 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ConfigFile:
|
|
|
|
ReadConfigFile (strValue);
|
|
|
|
break;
|
1999-03-07 18:23:56 +00:00
|
|
|
|
|
|
|
case LogDenied:
|
2004-07-04 12:53:54 +00:00
|
|
|
mip->logDropped = yesNoValue;
|
1999-03-07 18:23:56 +00:00
|
|
|
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;
|
2000-06-27 15:26:24 +00:00
|
|
|
|
|
|
|
case PunchFW:
|
|
|
|
SetupPunchFW(strValue);
|
|
|
|
break;
|
2001-11-27 11:00:16 +00:00
|
|
|
|
2003-09-23 07:41:55 +00:00
|
|
|
case SkinnyPort:
|
|
|
|
SetupSkinnyPort(strValue);
|
|
|
|
break;
|
|
|
|
|
2001-10-31 16:08:49 +00:00
|
|
|
case LogIpfwDenied:
|
2001-11-27 11:00:16 +00:00
|
|
|
logIpfwDenied = yesNoValue;;
|
|
|
|
break;
|
2003-08-13 13:16:19 +00:00
|
|
|
|
|
|
|
case PidFile:
|
|
|
|
pidName = strdup (strValue);
|
|
|
|
break;
|
2004-07-04 12:53:54 +00:00
|
|
|
case Instance:
|
|
|
|
NewInstance(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;
|
2000-02-25 11:34:38 +00:00
|
|
|
char *buf;
|
|
|
|
size_t len;
|
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");
|
2000-02-25 11:34:38 +00:00
|
|
|
if (!file)
|
|
|
|
err(1, "cannot open config file %s", fileName);
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2000-02-25 11:34:38 +00:00
|
|
|
while ((buf = fgetln(file, &len)) != NULL) {
|
|
|
|
if (buf[len - 1] == '\n')
|
|
|
|
buf[len - 1] = '\0';
|
|
|
|
else
|
|
|
|
errx(1, "config file format error: "
|
|
|
|
"last line should end with newline");
|
1997-06-22 04:19:08 +00:00
|
|
|
|
|
|
|
/*
|
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;
|
|
|
|
|
2000-06-16 09:41:57 +00:00
|
|
|
ParseOption (option, *ptr ? ptr : NULL);
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 SetupPortRedirect (const char* parms)
|
1997-06-22 04:19:08 +00:00
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
2000-04-27 17:55:17 +00:00
|
|
|
char* serverPool;
|
1997-06-22 04:19:08 +00:00
|
|
|
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;
|
2000-04-27 17:55:17 +00:00
|
|
|
struct alias_link *link = NULL;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-05-10 22:33:12 +00:00
|
|
|
strlcpy (buf, parms, sizeof(buf));
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2000-04-27 17:55:17 +00:00
|
|
|
separator = strchr(ptr, ',');
|
|
|
|
if (separator) { /* LSNAT redirection syntax. */
|
|
|
|
localAddr.s_addr = INADDR_NONE;
|
|
|
|
localPort = ~0;
|
|
|
|
numLocalPorts = 1;
|
|
|
|
serverPool = ptr;
|
|
|
|
} else {
|
|
|
|
if ( StrToAddrAndPortRange (ptr, &localAddr, protoName, &portRange) != 0 )
|
|
|
|
errx (1, "redirect_port: invalid local port range");
|
|
|
|
|
|
|
|
localPort = GETLOPORT(portRange);
|
|
|
|
numLocalPorts = GETNUMPORTS(portRange);
|
|
|
|
serverPool = NULL;
|
|
|
|
}
|
1999-03-24 20:30:20 +00:00
|
|
|
|
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;
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
link = LibAliasRedirectPort (mla, localAddr,
|
2000-04-27 17:55:17 +00:00
|
|
|
htons(localPort + i),
|
|
|
|
remoteAddr,
|
|
|
|
htons(remotePortCopy),
|
|
|
|
publicAddr,
|
|
|
|
htons(publicPort + i),
|
|
|
|
proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup LSNAT server pool.
|
|
|
|
*/
|
|
|
|
if (serverPool != NULL && link != NULL) {
|
|
|
|
ptr = strtok(serverPool, ",");
|
|
|
|
while (ptr != NULL) {
|
|
|
|
if (StrToAddrAndPortRange(ptr, &localAddr, protoName, &portRange) != 0)
|
|
|
|
errx(1, "redirect_port: invalid local port range");
|
|
|
|
|
|
|
|
localPort = GETLOPORT(portRange);
|
|
|
|
if (GETNUMPORTS(portRange) != 1)
|
|
|
|
errx(1, "redirect_port: local port must be single in this context");
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasAddServer(mla, link, localAddr, htons(localPort));
|
2000-04-27 17:55:17 +00:00
|
|
|
ptr = strtok(NULL, ",");
|
|
|
|
}
|
1999-03-24 20:30:20 +00:00
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
2000-05-03 15:06:45 +00:00
|
|
|
void
|
|
|
|
SetupProtoRedirect(const char* parms)
|
|
|
|
{
|
|
|
|
char buf[128];
|
|
|
|
char* ptr;
|
|
|
|
struct in_addr localAddr;
|
|
|
|
struct in_addr publicAddr;
|
|
|
|
struct in_addr remoteAddr;
|
|
|
|
int proto;
|
|
|
|
char* protoName;
|
|
|
|
struct protoent *protoent;
|
|
|
|
|
2004-05-10 22:33:12 +00:00
|
|
|
strlcpy (buf, parms, sizeof(buf));
|
2000-05-03 15:06:45 +00:00
|
|
|
/*
|
|
|
|
* Extract protocol.
|
|
|
|
*/
|
|
|
|
protoName = strtok(buf, " \t");
|
|
|
|
if (!protoName)
|
|
|
|
errx(1, "redirect_proto: missing protocol");
|
|
|
|
|
|
|
|
protoent = getprotobyname(protoName);
|
|
|
|
if (protoent == NULL)
|
|
|
|
errx(1, "redirect_proto: unknown protocol %s", protoName);
|
|
|
|
else
|
|
|
|
proto = protoent->p_proto;
|
|
|
|
/*
|
|
|
|
* Extract local address.
|
|
|
|
*/
|
|
|
|
ptr = strtok(NULL, " \t");
|
|
|
|
if (!ptr)
|
|
|
|
errx(1, "redirect_proto: missing local address");
|
|
|
|
else
|
|
|
|
StrToAddr(ptr, &localAddr);
|
|
|
|
/*
|
|
|
|
* Extract optional public address.
|
|
|
|
*/
|
|
|
|
ptr = strtok(NULL, " \t");
|
|
|
|
if (ptr)
|
|
|
|
StrToAddr(ptr, &publicAddr);
|
|
|
|
else
|
|
|
|
publicAddr.s_addr = INADDR_ANY;
|
|
|
|
/*
|
|
|
|
* Extract optional remote address.
|
|
|
|
*/
|
|
|
|
ptr = strtok(NULL, " \t");
|
|
|
|
if (ptr)
|
|
|
|
StrToAddr(ptr, &remoteAddr);
|
|
|
|
else
|
|
|
|
remoteAddr.s_addr = INADDR_ANY;
|
|
|
|
/*
|
|
|
|
* Create aliasing link.
|
|
|
|
*/
|
2004-07-04 12:53:54 +00:00
|
|
|
(void)LibAliasRedirectProto(mla, localAddr, remoteAddr, publicAddr,
|
2000-05-03 15:06:45 +00:00
|
|
|
proto);
|
|
|
|
}
|
|
|
|
|
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;
|
2000-04-27 17:55:17 +00:00
|
|
|
char* separator;
|
1997-06-22 04:19:08 +00:00
|
|
|
struct in_addr localAddr;
|
|
|
|
struct in_addr publicAddr;
|
2000-04-27 17:55:17 +00:00
|
|
|
char* serverPool;
|
|
|
|
struct alias_link *link;
|
1997-06-22 04:19:08 +00:00
|
|
|
|
2004-05-10 22:33:12 +00:00
|
|
|
strlcpy (buf, parms, sizeof(buf));
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2000-04-27 17:55:17 +00:00
|
|
|
separator = strchr(ptr, ',');
|
|
|
|
if (separator) { /* LSNAT redirection syntax. */
|
|
|
|
localAddr.s_addr = INADDR_NONE;
|
|
|
|
serverPool = ptr;
|
|
|
|
} else {
|
|
|
|
StrToAddr (ptr, &localAddr);
|
|
|
|
serverPool = NULL;
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2004-07-04 12:53:54 +00:00
|
|
|
link = LibAliasRedirectAddr(mla, localAddr, publicAddr);
|
2000-04-27 17:55:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Setup LSNAT server pool.
|
|
|
|
*/
|
|
|
|
if (serverPool != NULL && link != NULL) {
|
|
|
|
ptr = strtok(serverPool, ",");
|
|
|
|
while (ptr != NULL) {
|
|
|
|
StrToAddr(ptr, &localAddr);
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasAddServer(mla, link, localAddr, htons(~0));
|
2000-04-27 17:55:17 +00:00
|
|
|
ptr = strtok(NULL, ",");
|
|
|
|
}
|
|
|
|
}
|
1997-06-22 04:19:08 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2003-02-05 20:08:39 +00:00
|
|
|
errx (1, "%s/%s: unknown service", 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. */
|
2003-02-05 20:08:39 +00:00
|
|
|
errx (1, "%s/%s: unknown service", str, proto);
|
1999-03-24 20:30:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
2000-06-27 15:26:24 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
SetupPunchFW(const char *strValue)
|
|
|
|
{
|
|
|
|
unsigned int base, num;
|
|
|
|
|
|
|
|
if (sscanf(strValue, "%u:%u", &base, &num) != 2)
|
|
|
|
errx(1, "punch_fw: basenumber:count parameter required");
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasSetFWBase(mla, base, num);
|
|
|
|
(void)LibAliasSetMode(mla, PKT_ALIAS_PUNCH_FW, PKT_ALIAS_PUNCH_FW);
|
2000-06-27 15:26:24 +00:00
|
|
|
}
|
2003-09-23 07:41:55 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
SetupSkinnyPort(const char *strValue)
|
|
|
|
{
|
|
|
|
unsigned int port;
|
|
|
|
|
|
|
|
if (sscanf(strValue, "%u", &port) != 1)
|
|
|
|
errx(1, "skinny_port: port parameter required");
|
|
|
|
|
2004-07-04 12:53:54 +00:00
|
|
|
LibAliasSetSkinnyPort(mla, port);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NewInstance(const char *name)
|
|
|
|
{
|
|
|
|
struct instance *ip;
|
|
|
|
|
|
|
|
LIST_FOREACH(ip, &root, list) {
|
|
|
|
if (!strcmp(ip->name, name)) {
|
|
|
|
mla = ip->la;
|
|
|
|
mip = ip;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ninstance++;
|
|
|
|
ip = calloc(sizeof *ip, 1);
|
|
|
|
ip->name = strdup(name);
|
|
|
|
ip->la = LibAliasInit (ip->la);
|
|
|
|
ip->assignAliasAddr = 0;
|
|
|
|
ip->ifName = NULL;
|
|
|
|
ip->logDropped = 0;
|
|
|
|
ip->inPort = 0;
|
|
|
|
ip->outPort = 0;
|
|
|
|
ip->inOutPort = 0;
|
|
|
|
ip->aliasAddr.s_addr = INADDR_NONE;
|
|
|
|
ip->ifMTU = -1;
|
|
|
|
ip->aliasOverhead = 12;
|
|
|
|
LIST_INSERT_HEAD(&root, ip, list);
|
|
|
|
mla = ip->la;
|
|
|
|
mip = ip;
|
2003-09-23 07:41:55 +00:00
|
|
|
}
|