2001-06-04 15:09:51 +00:00
|
|
|
/*-
|
2001-11-03 11:34:09 +00:00
|
|
|
* Copyright (c) 2001 Charles Mott <cm@linktel.net>
|
2001-06-04 15:09:51 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2001-09-30 21:03:33 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
1999-02-27 02:16:01 +00:00
|
|
|
/* file: alias_proxy.c
|
|
|
|
|
|
|
|
This file encapsulates special operations related to transparent
|
|
|
|
proxy redirection. This is where packets with a particular destination,
|
|
|
|
usually tcp port 80, are redirected to a proxy server.
|
|
|
|
|
|
|
|
When packets are proxied, the destination address and port are
|
|
|
|
modified. In certain cases, it is necessary to somehow encode
|
|
|
|
the original address/port info into the packet. Two methods are
|
|
|
|
presently supported: addition of a [DEST addr port] string at the
|
2003-01-01 18:49:04 +00:00
|
|
|
beginning of a tcp stream, or inclusion of an optional field
|
1999-02-27 02:16:01 +00:00
|
|
|
in the IP header.
|
2002-07-01 11:19:40 +00:00
|
|
|
|
1999-02-27 02:16:01 +00:00
|
|
|
There is one public API function:
|
|
|
|
|
2004-07-05 10:53:28 +00:00
|
|
|
PacketAliasProxyRule() -- Adds and deletes proxy
|
|
|
|
rules.
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
Rules are stored in a linear linked list, so lookup efficiency
|
|
|
|
won't be too good for large lists.
|
|
|
|
|
|
|
|
|
|
|
|
Initial development: April, 1998 (cjm)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* System includes */
|
2005-05-05 19:27:32 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/ctype.h>
|
|
|
|
#include <sys/libkern.h>
|
|
|
|
#include <sys/limits.h>
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
1999-02-27 02:16:01 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <netdb.h>
|
Summer of Code 2005: improve libalias - part 1 of 2
With the first part of my previous Summer of Code work, we get:
-made libalias modular:
-support for 'particular' protocols (like ftp/irc/etcetc) is no more
hardcoded inside libalias, but it's available through external
modules loadable at runtime
-modules are available both in kernel (/boot/kernel/alias_*.ko) and
user land (/lib/libalias_*)
-protocols/applications modularized are: cuseeme, ftp, irc, nbt, pptp,
skinny and smedia
-added logging support for kernel side
-cleanup
After a buildworld, do a 'mergemaster -i' to install the file libalias.conf
in /etc or manually copy it.
During startup (and after every HUP signal) user land applications running
the new libalias will try to read a file in /etc called libalias.conf:
that file contains the list of modules to load.
User land applications affected by this commit are ppp and natd:
if libalias.conf is present in /etc you won't notice any difference.
The only kernel land bit affected by this commit is ng_nat:
if you are using ng_nat, and it doesn't correctly handle
ftp/irc/etcetc sessions anymore, remember to kldload
the correspondent module (i.e. kldload alias_ftp).
General information and details about the inner working are available
in the libalias man page under the section 'MODULAR ARCHITECTURE
(AND ipfw(4) SUPPORT)'.
NOTA BENE: this commit affects _ONLY_ libalias, ipfw in-kernel nat
support will be part of the next libalias-related commit.
Approved by: glebius
Reviewed by: glebius, ru
2006-09-26 23:26:53 +00:00
|
|
|
#include <string.h>
|
2005-05-05 19:27:32 +00:00
|
|
|
#endif
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
|
2005-05-05 19:27:32 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
#include <netinet/libalias/alias.h>
|
2005-05-05 21:55:17 +00:00
|
|
|
#include <netinet/libalias/alias_local.h>
|
Summer of Code 2005: improve libalias - part 1 of 2
With the first part of my previous Summer of Code work, we get:
-made libalias modular:
-support for 'particular' protocols (like ftp/irc/etcetc) is no more
hardcoded inside libalias, but it's available through external
modules loadable at runtime
-modules are available both in kernel (/boot/kernel/alias_*.ko) and
user land (/lib/libalias_*)
-protocols/applications modularized are: cuseeme, ftp, irc, nbt, pptp,
skinny and smedia
-added logging support for kernel side
-cleanup
After a buildworld, do a 'mergemaster -i' to install the file libalias.conf
in /etc or manually copy it.
During startup (and after every HUP signal) user land applications running
the new libalias will try to read a file in /etc called libalias.conf:
that file contains the list of modules to load.
User land applications affected by this commit are ppp and natd:
if libalias.conf is present in /etc you won't notice any difference.
The only kernel land bit affected by this commit is ng_nat:
if you are using ng_nat, and it doesn't correctly handle
ftp/irc/etcetc sessions anymore, remember to kldload
the correspondent module (i.e. kldload alias_ftp).
General information and details about the inner working are available
in the libalias man page under the section 'MODULAR ARCHITECTURE
(AND ipfw(4) SUPPORT)'.
NOTA BENE: this commit affects _ONLY_ libalias, ipfw in-kernel nat
support will be part of the next libalias-related commit.
Approved by: glebius
Reviewed by: glebius, ru
2006-09-26 23:26:53 +00:00
|
|
|
#include <netinet/libalias/alias_mod.h>
|
2005-05-05 19:27:32 +00:00
|
|
|
#else
|
Summer of Code 2005: improve libalias - part 1 of 2
With the first part of my previous Summer of Code work, we get:
-made libalias modular:
-support for 'particular' protocols (like ftp/irc/etcetc) is no more
hardcoded inside libalias, but it's available through external
modules loadable at runtime
-modules are available both in kernel (/boot/kernel/alias_*.ko) and
user land (/lib/libalias_*)
-protocols/applications modularized are: cuseeme, ftp, irc, nbt, pptp,
skinny and smedia
-added logging support for kernel side
-cleanup
After a buildworld, do a 'mergemaster -i' to install the file libalias.conf
in /etc or manually copy it.
During startup (and after every HUP signal) user land applications running
the new libalias will try to read a file in /etc called libalias.conf:
that file contains the list of modules to load.
User land applications affected by this commit are ppp and natd:
if libalias.conf is present in /etc you won't notice any difference.
The only kernel land bit affected by this commit is ng_nat:
if you are using ng_nat, and it doesn't correctly handle
ftp/irc/etcetc sessions anymore, remember to kldload
the correspondent module (i.e. kldload alias_ftp).
General information and details about the inner working are available
in the libalias man page under the section 'MODULAR ARCHITECTURE
(AND ipfw(4) SUPPORT)'.
NOTA BENE: this commit affects _ONLY_ libalias, ipfw in-kernel nat
support will be part of the next libalias-related commit.
Approved by: glebius
Reviewed by: glebius, ru
2006-09-26 23:26:53 +00:00
|
|
|
#include <arpa/inet.h>
|
2004-03-16 21:30:41 +00:00
|
|
|
#include "alias.h" /* Public API functions for libalias */
|
2005-05-05 21:55:17 +00:00
|
|
|
#include "alias_local.h" /* Functions used by alias*.c */
|
2005-05-05 19:27:32 +00:00
|
|
|
#endif
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Data structures
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A linked list of arbitrary length, based on struct proxy_entry is
|
|
|
|
* used to store proxy rules.
|
|
|
|
*/
|
2004-03-16 21:30:41 +00:00
|
|
|
struct proxy_entry {
|
|
|
|
struct libalias *la;
|
1999-02-27 02:16:01 +00:00
|
|
|
#define PROXY_TYPE_ENCODE_NONE 1
|
|
|
|
#define PROXY_TYPE_ENCODE_TCPSTREAM 2
|
|
|
|
#define PROXY_TYPE_ENCODE_IPHDR 3
|
2004-03-16 21:30:41 +00:00
|
|
|
int rule_index;
|
|
|
|
int proxy_type;
|
|
|
|
u_char proto;
|
|
|
|
u_short proxy_port;
|
|
|
|
u_short server_port;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
struct in_addr server_addr;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
struct in_addr src_addr;
|
|
|
|
struct in_addr src_mask;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
struct in_addr dst_addr;
|
|
|
|
struct in_addr dst_mask;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
struct proxy_entry *next;
|
|
|
|
struct proxy_entry *last;
|
1999-02-27 02:16:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
File scope variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Local (static) functions:
|
|
|
|
|
|
|
|
IpMask() -- Utility function for creating IP
|
2004-07-05 10:53:28 +00:00
|
|
|
masks from integer (1-32) specification.
|
1999-02-27 02:16:01 +00:00
|
|
|
IpAddr() -- Utility function for converting string
|
2004-07-05 10:53:28 +00:00
|
|
|
to IP address
|
1999-02-27 02:16:01 +00:00
|
|
|
IpPort() -- Utility function for converting string
|
2004-07-05 10:53:28 +00:00
|
|
|
to port number
|
1999-02-27 02:16:01 +00:00
|
|
|
RuleAdd() -- Adds an element to the rule list.
|
|
|
|
RuleDelete() -- Removes an element from the rule list.
|
|
|
|
RuleNumberDelete() -- Removes all elements from the rule list
|
2004-07-05 10:53:28 +00:00
|
|
|
having a certain rule number.
|
1999-02-27 02:16:01 +00:00
|
|
|
ProxyEncodeTcpStream() -- Adds [DEST x.x.x.x xxxx] to the beginning
|
2004-07-05 10:53:28 +00:00
|
|
|
of a TCP stream.
|
1999-02-27 02:16:01 +00:00
|
|
|
ProxyEncodeIpHeader() -- Adds an IP option indicating the true
|
2004-07-05 10:53:28 +00:00
|
|
|
destination of a proxied IP packet
|
1999-02-27 02:16:01 +00:00
|
|
|
*/
|
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
static int IpMask(int, struct in_addr *);
|
|
|
|
static int IpAddr(char *, struct in_addr *);
|
|
|
|
static int IpPort(char *, int, int *);
|
|
|
|
static void RuleAdd(struct libalias *la, struct proxy_entry *);
|
|
|
|
static void RuleDelete(struct proxy_entry *);
|
|
|
|
static int RuleNumberDelete(struct libalias *la, int);
|
|
|
|
static void ProxyEncodeTcpStream(struct alias_link *, struct ip *, int);
|
|
|
|
static void ProxyEncodeIpHeader(struct ip *, int);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
IpMask(int nbits, struct in_addr *mask)
|
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int i;
|
|
|
|
u_int imask;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
if (nbits < 0 || nbits > 32)
|
2004-07-05 10:55:23 +00:00
|
|
|
return (-1);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
imask = 0;
|
|
|
|
for (i = 0; i < nbits; i++)
|
|
|
|
imask = (imask >> 1) + 0x80000000;
|
|
|
|
mask->s_addr = htonl(imask);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-07-05 10:55:23 +00:00
|
|
|
return (0);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
IpAddr(char *s, struct in_addr *addr)
|
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
if (inet_aton(s, addr) == 0)
|
2004-07-05 10:55:23 +00:00
|
|
|
return (-1);
|
2004-03-16 21:30:41 +00:00
|
|
|
else
|
2004-07-05 10:55:23 +00:00
|
|
|
return (0);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
IpPort(char *s, int proto, int *port)
|
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int n;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
n = sscanf(s, "%d", port);
|
2005-05-05 22:00:32 +00:00
|
|
|
if (n != 1)
|
|
|
|
#ifndef _KERNEL /* XXX: we accept only numeric ports in kernel */
|
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
struct servent *se;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
if (proto == IPPROTO_TCP)
|
|
|
|
se = getservbyname(s, "tcp");
|
|
|
|
else if (proto == IPPROTO_UDP)
|
|
|
|
se = getservbyname(s, "udp");
|
|
|
|
else
|
2004-07-05 10:55:23 +00:00
|
|
|
return (-1);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
if (se == NULL)
|
2004-07-05 10:55:23 +00:00
|
|
|
return (-1);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
*port = (u_int) ntohs(se->s_port);
|
|
|
|
}
|
2005-05-05 22:00:32 +00:00
|
|
|
#else
|
|
|
|
return (-1);
|
|
|
|
#endif
|
2004-07-05 10:55:23 +00:00
|
|
|
return (0);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-01-17 10:52:21 +00:00
|
|
|
RuleAdd(struct libalias *la, struct proxy_entry *entry)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int rule_index;
|
|
|
|
struct proxy_entry *ptr;
|
|
|
|
struct proxy_entry *ptr_last;
|
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK_ASSERT(la);
|
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
if (la->proxyList == NULL) {
|
|
|
|
la->proxyList = entry;
|
|
|
|
entry->last = NULL;
|
|
|
|
entry->next = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
entry->la = la;
|
|
|
|
|
|
|
|
rule_index = entry->rule_index;
|
|
|
|
ptr = la->proxyList;
|
|
|
|
ptr_last = NULL;
|
|
|
|
while (ptr != NULL) {
|
|
|
|
if (ptr->rule_index >= rule_index) {
|
|
|
|
if (ptr_last == NULL) {
|
|
|
|
entry->next = la->proxyList;
|
|
|
|
entry->last = NULL;
|
|
|
|
la->proxyList->last = entry;
|
|
|
|
la->proxyList = entry;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr_last->next = entry;
|
|
|
|
ptr->last = entry;
|
|
|
|
entry->last = ptr->last;
|
|
|
|
entry->next = ptr;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ptr_last = ptr;
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
ptr_last->next = entry;
|
|
|
|
entry->last = ptr_last;
|
|
|
|
entry->next = NULL;
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
RuleDelete(struct proxy_entry *entry)
|
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
struct libalias *la;
|
|
|
|
|
|
|
|
la = entry->la;
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK_ASSERT(la);
|
2004-03-16 21:30:41 +00:00
|
|
|
if (entry->last != NULL)
|
|
|
|
entry->last->next = entry->next;
|
|
|
|
else
|
|
|
|
la->proxyList = entry->next;
|
|
|
|
|
|
|
|
if (entry->next != NULL)
|
|
|
|
entry->next->last = entry->last;
|
|
|
|
|
|
|
|
free(entry);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-01-17 10:52:21 +00:00
|
|
|
RuleNumberDelete(struct libalias *la, int rule_index)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int err;
|
|
|
|
struct proxy_entry *ptr;
|
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK_ASSERT(la);
|
2004-03-16 21:30:41 +00:00
|
|
|
err = -1;
|
|
|
|
ptr = la->proxyList;
|
|
|
|
while (ptr != NULL) {
|
|
|
|
struct proxy_entry *ptr_next;
|
|
|
|
|
|
|
|
ptr_next = ptr->next;
|
|
|
|
if (ptr->rule_index == rule_index) {
|
|
|
|
err = 0;
|
|
|
|
RuleDelete(ptr);
|
|
|
|
}
|
|
|
|
ptr = ptr_next;
|
|
|
|
}
|
|
|
|
|
2004-07-05 10:55:23 +00:00
|
|
|
return (err);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-07-05 11:10:57 +00:00
|
|
|
ProxyEncodeTcpStream(struct alias_link *lnk,
|
2004-03-16 21:30:41 +00:00
|
|
|
struct ip *pip,
|
|
|
|
int maxpacketsize)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
int slen;
|
|
|
|
char buffer[40];
|
|
|
|
struct tcphdr *tc;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Compute pointer to tcp header */
|
2004-07-06 12:13:28 +00:00
|
|
|
tc = (struct tcphdr *)ip_next(pip);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Don't modify if once already modified */
|
|
|
|
|
2004-07-05 11:10:57 +00:00
|
|
|
if (GetAckModified(lnk))
|
2004-03-16 21:30:41 +00:00
|
|
|
return;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Translate destination address and port to string form */
|
2004-03-16 21:30:41 +00:00
|
|
|
snprintf(buffer, sizeof(buffer) - 2, "[DEST %s %d]",
|
2004-07-05 11:10:57 +00:00
|
|
|
inet_ntoa(GetProxyAddress(lnk)), (u_int) ntohs(GetProxyPort(lnk)));
|
2002-07-01 11:19:40 +00:00
|
|
|
|
1999-02-27 02:16:01 +00:00
|
|
|
/* Pad string out to a multiple of two in length */
|
2004-03-16 21:30:41 +00:00
|
|
|
slen = strlen(buffer);
|
|
|
|
switch (slen % 2) {
|
|
|
|
case 0:
|
|
|
|
strcat(buffer, " \n");
|
|
|
|
slen += 2;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
strcat(buffer, "\n");
|
|
|
|
slen += 1;
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Check for packet overflow */
|
2004-07-05 11:10:57 +00:00
|
|
|
if ((int)(ntohs(pip->ip_len) + strlen(buffer)) > maxpacketsize)
|
2004-03-16 21:30:41 +00:00
|
|
|
return;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Shift existing TCP data and insert destination string */
|
2004-03-16 21:30:41 +00:00
|
|
|
{
|
|
|
|
int dlen;
|
|
|
|
int hlen;
|
2007-04-04 03:10:42 +00:00
|
|
|
char *p;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
hlen = (pip->ip_hl + tc->th_off) << 2;
|
|
|
|
dlen = ntohs(pip->ip_len) - hlen;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Modify first packet that has data in it */
|
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
if (dlen == 0)
|
|
|
|
return;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
p = (char *)pip;
|
|
|
|
p += hlen;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2005-05-05 21:40:49 +00:00
|
|
|
bcopy(p, p + slen, dlen);
|
2004-03-16 21:30:41 +00:00
|
|
|
memcpy(p, buffer, slen);
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Save information about modfied sequence number */
|
2004-03-16 21:30:41 +00:00
|
|
|
{
|
|
|
|
int delta;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-07-05 11:10:57 +00:00
|
|
|
SetAckModified(lnk);
|
2008-03-06 21:50:41 +00:00
|
|
|
tc = (struct tcphdr *)ip_next(pip);
|
|
|
|
delta = GetDeltaSeqOut(tc->th_seq, lnk);
|
|
|
|
AddSeq(lnk, delta + slen, pip->ip_hl, pip->ip_len, tc->th_seq,
|
|
|
|
tc->th_off);
|
2004-03-16 21:30:41 +00:00
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Update IP header packet length and checksum */
|
2004-03-16 21:30:41 +00:00
|
|
|
{
|
|
|
|
int accumulate;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
accumulate = pip->ip_len;
|
|
|
|
pip->ip_len = htons(ntohs(pip->ip_len) + slen);
|
|
|
|
accumulate -= pip->ip_len;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
ADJUST_CHECKSUM(accumulate, pip->ip_sum);
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Update TCP checksum, Use TcpChecksum since so many things have
|
|
|
|
already changed. */
|
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
tc->th_sum = 0;
|
2005-06-27 07:36:02 +00:00
|
|
|
#ifdef _KERNEL
|
|
|
|
tc->th_x2 = 1;
|
|
|
|
#else
|
2004-03-16 21:30:41 +00:00
|
|
|
tc->th_sum = TcpChecksum(pip);
|
2005-06-27 07:36:02 +00:00
|
|
|
#endif
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ProxyEncodeIpHeader(struct ip *pip,
|
2004-03-16 21:30:41 +00:00
|
|
|
int maxpacketsize)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
|
|
|
#define OPTION_LEN_BYTES 8
|
|
|
|
#define OPTION_LEN_INT16 4
|
|
|
|
#define OPTION_LEN_INT32 2
|
2004-03-16 21:30:41 +00:00
|
|
|
u_char option[OPTION_LEN_BYTES];
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2005-05-06 11:07:49 +00:00
|
|
|
#ifdef LIBALIAS_DEBUG
|
2004-03-16 21:30:41 +00:00
|
|
|
fprintf(stdout, " ip cksum 1 = %x\n", (u_int) IpChecksum(pip));
|
|
|
|
fprintf(stdout, "tcp cksum 1 = %x\n", (u_int) TcpChecksum(pip));
|
1999-03-09 23:44:00 +00:00
|
|
|
#endif
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-07-05 11:10:57 +00:00
|
|
|
(void)maxpacketsize;
|
|
|
|
|
1999-02-27 02:16:01 +00:00
|
|
|
/* Check to see that there is room to add an IP option */
|
2004-03-16 21:30:41 +00:00
|
|
|
if (pip->ip_hl > (0x0f - OPTION_LEN_INT32))
|
|
|
|
return;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Build option and copy into packet */
|
2004-03-16 21:30:41 +00:00
|
|
|
{
|
|
|
|
u_char *ptr;
|
|
|
|
struct tcphdr *tc;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
ptr = (u_char *) pip;
|
|
|
|
ptr += 20;
|
|
|
|
memcpy(ptr + OPTION_LEN_BYTES, ptr, ntohs(pip->ip_len) - 20);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
option[0] = 0x64; /* class: 3 (reserved), option 4 */
|
|
|
|
option[1] = OPTION_LEN_BYTES;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
memcpy(&option[2], (u_char *) & pip->ip_dst, 4);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-07-06 12:13:28 +00:00
|
|
|
tc = (struct tcphdr *)ip_next(pip);
|
2004-03-16 21:30:41 +00:00
|
|
|
memcpy(&option[6], (u_char *) & tc->th_sport, 2);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
memcpy(ptr, option, 8);
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Update checksum, header length and packet length */
|
2004-03-16 21:30:41 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int accumulate;
|
|
|
|
u_short *sptr;
|
|
|
|
|
|
|
|
sptr = (u_short *) option;
|
|
|
|
accumulate = 0;
|
|
|
|
for (i = 0; i < OPTION_LEN_INT16; i++)
|
|
|
|
accumulate -= *(sptr++);
|
|
|
|
|
|
|
|
sptr = (u_short *) pip;
|
|
|
|
accumulate += *sptr;
|
|
|
|
pip->ip_hl += OPTION_LEN_INT32;
|
|
|
|
accumulate -= *sptr;
|
|
|
|
|
|
|
|
accumulate += pip->ip_len;
|
|
|
|
pip->ip_len = htons(ntohs(pip->ip_len) + OPTION_LEN_BYTES);
|
|
|
|
accumulate -= pip->ip_len;
|
|
|
|
|
|
|
|
ADJUST_CHECKSUM(accumulate, pip->ip_sum);
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
#undef OPTION_LEN_BYTES
|
|
|
|
#undef OPTION_LEN_INT16
|
|
|
|
#undef OPTION_LEN_INT32
|
2005-05-06 11:07:49 +00:00
|
|
|
#ifdef LIBALIAS_DEBUG
|
2004-03-16 21:30:41 +00:00
|
|
|
fprintf(stdout, " ip cksum 2 = %x\n", (u_int) IpChecksum(pip));
|
|
|
|
fprintf(stdout, "tcp cksum 2 = %x\n", (u_int) TcpChecksum(pip));
|
1999-03-09 23:44:00 +00:00
|
|
|
#endif
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Functions by other packet alias source files
|
|
|
|
|
|
|
|
ProxyCheck() -- Checks whether an outgoing packet should
|
2004-07-05 10:53:28 +00:00
|
|
|
be proxied.
|
1999-02-27 02:16:01 +00:00
|
|
|
ProxyModify() -- Encodes the original destination address/port
|
2004-07-05 10:53:28 +00:00
|
|
|
for a packet which is to be redirected to
|
|
|
|
a proxy server.
|
1999-02-27 02:16:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2008-03-06 21:50:41 +00:00
|
|
|
ProxyCheck(struct libalias *la, struct in_addr *proxy_server_addr,
|
|
|
|
u_short * proxy_server_port, struct in_addr src_addr,
|
|
|
|
struct in_addr dst_addr, u_short dst_port, u_char ip_p)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
2004-03-16 21:30:41 +00:00
|
|
|
struct proxy_entry *ptr;
|
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK_ASSERT(la);
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
ptr = la->proxyList;
|
|
|
|
while (ptr != NULL) {
|
|
|
|
u_short proxy_port;
|
|
|
|
|
|
|
|
proxy_port = ptr->proxy_port;
|
|
|
|
if ((dst_port == proxy_port || proxy_port == 0)
|
2008-03-06 21:50:41 +00:00
|
|
|
&& ip_p == ptr->proto
|
2004-03-16 21:30:41 +00:00
|
|
|
&& src_addr.s_addr != ptr->server_addr.s_addr) {
|
|
|
|
struct in_addr src_addr_masked;
|
|
|
|
struct in_addr dst_addr_masked;
|
|
|
|
|
|
|
|
src_addr_masked.s_addr = src_addr.s_addr & ptr->src_mask.s_addr;
|
|
|
|
dst_addr_masked.s_addr = dst_addr.s_addr & ptr->dst_mask.s_addr;
|
|
|
|
|
|
|
|
if ((src_addr_masked.s_addr == ptr->src_addr.s_addr)
|
|
|
|
&& (dst_addr_masked.s_addr == ptr->dst_addr.s_addr)) {
|
|
|
|
if ((*proxy_server_port = ptr->server_port) == 0)
|
|
|
|
*proxy_server_port = dst_port;
|
|
|
|
*proxy_server_addr = ptr->server_addr;
|
2004-07-05 10:55:23 +00:00
|
|
|
return (ptr->proxy_type);
|
2004-03-16 21:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr = ptr->next;
|
|
|
|
}
|
|
|
|
|
2004-07-05 10:55:23 +00:00
|
|
|
return (0);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2004-07-05 11:10:57 +00:00
|
|
|
ProxyModify(struct libalias *la, struct alias_link *lnk,
|
2004-03-16 21:30:41 +00:00
|
|
|
struct ip *pip,
|
|
|
|
int maxpacketsize,
|
|
|
|
int proxy_type)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
2004-07-05 11:10:57 +00:00
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK_ASSERT(la);
|
2004-07-05 11:10:57 +00:00
|
|
|
(void)la;
|
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
switch (proxy_type) {
|
|
|
|
case PROXY_TYPE_ENCODE_IPHDR:
|
|
|
|
ProxyEncodeIpHeader(pip, maxpacketsize);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PROXY_TYPE_ENCODE_TCPSTREAM:
|
2004-07-05 11:10:57 +00:00
|
|
|
ProxyEncodeTcpStream(lnk, pip, maxpacketsize);
|
2004-03-16 21:30:41 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Public API functions
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
2004-01-17 10:52:21 +00:00
|
|
|
LibAliasProxyRule(struct libalias *la, const char *cmd)
|
1999-02-27 02:16:01 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This function takes command strings of the form:
|
|
|
|
*
|
|
|
|
* server <addr>[:<port>]
|
|
|
|
* [port <port>]
|
|
|
|
* [rule n]
|
|
|
|
* [proto tcp|udp]
|
|
|
|
* [src <addr>[/n]]
|
|
|
|
* [dst <addr>[/n]]
|
|
|
|
* [type encode_tcp_stream|encode_ip_hdr|no_encode]
|
|
|
|
*
|
|
|
|
* delete <rule number>
|
|
|
|
*
|
|
|
|
* Subfields can be in arbitrary order. Port numbers and addresses
|
|
|
|
* must be in either numeric or symbolic form. An optional rule number
|
|
|
|
* is used to control the order in which rules are searched. If two
|
|
|
|
* rules have the same number, then search order cannot be guaranteed,
|
|
|
|
* and the rules should be disjoint. If no rule number is specified,
|
|
|
|
* then 0 is used, and group 0 rules are always checked before any
|
|
|
|
* others.
|
|
|
|
*/
|
2006-12-15 12:50:06 +00:00
|
|
|
int i, n, len, ret;
|
2004-03-16 21:30:41 +00:00
|
|
|
int cmd_len;
|
|
|
|
int token_count;
|
|
|
|
int state;
|
|
|
|
char *token;
|
|
|
|
char buffer[256];
|
|
|
|
char str_port[sizeof(buffer)];
|
|
|
|
char str_server_port[sizeof(buffer)];
|
|
|
|
char *res = buffer;
|
|
|
|
|
|
|
|
int rule_index;
|
|
|
|
int proto;
|
|
|
|
int proxy_type;
|
|
|
|
int proxy_port;
|
|
|
|
int server_port;
|
|
|
|
struct in_addr server_addr;
|
|
|
|
struct in_addr src_addr, src_mask;
|
|
|
|
struct in_addr dst_addr, dst_mask;
|
|
|
|
struct proxy_entry *proxy_entry;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
LIBALIAS_LOCK(la);
|
|
|
|
ret = 0;
|
1999-02-27 02:16:01 +00:00
|
|
|
/* Copy command line into a buffer */
|
2004-03-16 21:30:41 +00:00
|
|
|
cmd += strspn(cmd, " \t");
|
|
|
|
cmd_len = strlen(cmd);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (cmd_len > (int)(sizeof(buffer) - 1)) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
strcpy(buffer, cmd);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Convert to lower case */
|
2004-03-16 21:30:41 +00:00
|
|
|
len = strlen(buffer);
|
|
|
|
for (i = 0; i < len; i++)
|
|
|
|
buffer[i] = tolower((unsigned char)buffer[i]);
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Set default proxy type */
|
|
|
|
|
|
|
|
/* Set up default values */
|
2004-03-16 21:30:41 +00:00
|
|
|
rule_index = 0;
|
|
|
|
proxy_type = PROXY_TYPE_ENCODE_NONE;
|
|
|
|
proto = IPPROTO_TCP;
|
|
|
|
proxy_port = 0;
|
|
|
|
server_addr.s_addr = 0;
|
|
|
|
server_port = 0;
|
|
|
|
src_addr.s_addr = 0;
|
|
|
|
IpMask(0, &src_mask);
|
|
|
|
dst_addr.s_addr = 0;
|
|
|
|
IpMask(0, &dst_mask);
|
|
|
|
|
|
|
|
str_port[0] = 0;
|
|
|
|
str_server_port[0] = 0;
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Parse command string with state machine */
|
|
|
|
#define STATE_READ_KEYWORD 0
|
|
|
|
#define STATE_READ_TYPE 1
|
|
|
|
#define STATE_READ_PORT 2
|
|
|
|
#define STATE_READ_SERVER 3
|
|
|
|
#define STATE_READ_RULE 4
|
|
|
|
#define STATE_READ_DELETE 5
|
|
|
|
#define STATE_READ_PROTO 6
|
|
|
|
#define STATE_READ_SRC 7
|
|
|
|
#define STATE_READ_DST 8
|
2004-03-16 21:30:41 +00:00
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
token = strsep(&res, " \t");
|
|
|
|
token_count = 0;
|
|
|
|
while (token != NULL) {
|
|
|
|
token_count++;
|
|
|
|
switch (state) {
|
|
|
|
case STATE_READ_KEYWORD:
|
|
|
|
if (strcmp(token, "type") == 0)
|
|
|
|
state = STATE_READ_TYPE;
|
|
|
|
else if (strcmp(token, "port") == 0)
|
|
|
|
state = STATE_READ_PORT;
|
|
|
|
else if (strcmp(token, "server") == 0)
|
|
|
|
state = STATE_READ_SERVER;
|
|
|
|
else if (strcmp(token, "rule") == 0)
|
|
|
|
state = STATE_READ_RULE;
|
|
|
|
else if (strcmp(token, "delete") == 0)
|
|
|
|
state = STATE_READ_DELETE;
|
|
|
|
else if (strcmp(token, "proto") == 0)
|
|
|
|
state = STATE_READ_PROTO;
|
|
|
|
else if (strcmp(token, "src") == 0)
|
|
|
|
state = STATE_READ_SRC;
|
|
|
|
else if (strcmp(token, "dst") == 0)
|
|
|
|
state = STATE_READ_DST;
|
2006-12-15 12:50:06 +00:00
|
|
|
else {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_READ_TYPE:
|
|
|
|
if (strcmp(token, "encode_ip_hdr") == 0)
|
|
|
|
proxy_type = PROXY_TYPE_ENCODE_IPHDR;
|
|
|
|
else if (strcmp(token, "encode_tcp_stream") == 0)
|
|
|
|
proxy_type = PROXY_TYPE_ENCODE_TCPSTREAM;
|
|
|
|
else if (strcmp(token, "no_encode") == 0)
|
|
|
|
proxy_type = PROXY_TYPE_ENCODE_NONE;
|
2006-12-15 12:50:06 +00:00
|
|
|
else {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_READ_PORT:
|
|
|
|
strcpy(str_port, token);
|
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_READ_SERVER:
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char *p;
|
|
|
|
char s[sizeof(buffer)];
|
|
|
|
|
|
|
|
p = token;
|
|
|
|
while (*p != ':' && *p != 0)
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p != ':') {
|
|
|
|
err = IpAddr(token, &server_addr);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
} else {
|
|
|
|
*p = ' ';
|
|
|
|
|
|
|
|
n = sscanf(token, "%s %s", s, str_server_port);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (n != 2) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
err = IpAddr(s, &server_addr);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_READ_RULE:
|
|
|
|
n = sscanf(token, "%d", &rule_index);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (n != 1 || rule_index < 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_READ_DELETE:
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
int rule_to_delete;
|
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
if (token_count != 2) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
n = sscanf(token, "%d", &rule_to_delete);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (n != 1) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
err = RuleNumberDelete(la, rule_to_delete);
|
|
|
|
if (err)
|
2006-12-15 12:50:06 +00:00
|
|
|
ret = -1;
|
|
|
|
ret = 0;
|
|
|
|
goto getout;
|
2004-03-16 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case STATE_READ_PROTO:
|
|
|
|
if (strcmp(token, "tcp") == 0)
|
|
|
|
proto = IPPROTO_TCP;
|
|
|
|
else if (strcmp(token, "udp") == 0)
|
|
|
|
proto = IPPROTO_UDP;
|
2006-12-15 12:50:06 +00:00
|
|
|
else {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_READ_SRC:
|
|
|
|
case STATE_READ_DST:
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
char *p;
|
|
|
|
struct in_addr mask;
|
|
|
|
struct in_addr addr;
|
|
|
|
|
|
|
|
p = token;
|
|
|
|
while (*p != '/' && *p != 0)
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p != '/') {
|
|
|
|
IpMask(32, &mask);
|
|
|
|
err = IpAddr(token, &addr);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
} else {
|
|
|
|
int nbits;
|
|
|
|
char s[sizeof(buffer)];
|
|
|
|
|
|
|
|
*p = ' ';
|
|
|
|
n = sscanf(token, "%s %d", s, &nbits);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (n != 2) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
err = IpAddr(s, &addr);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
err = IpMask(nbits, &mask);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == STATE_READ_SRC) {
|
|
|
|
src_addr = addr;
|
|
|
|
src_mask = mask;
|
|
|
|
} else {
|
|
|
|
dst_addr = addr;
|
|
|
|
dst_mask = mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state = STATE_READ_KEYWORD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2006-12-15 12:50:06 +00:00
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
2004-03-16 21:30:41 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
token = strsep(&res, " \t");
|
|
|
|
} while (token != NULL && !*token);
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
#undef STATE_READ_KEYWORD
|
|
|
|
#undef STATE_READ_TYPE
|
|
|
|
#undef STATE_READ_PORT
|
|
|
|
#undef STATE_READ_SERVER
|
|
|
|
#undef STATE_READ_RULE
|
|
|
|
#undef STATE_READ_DELETE
|
|
|
|
#undef STATE_READ_PROTO
|
|
|
|
#undef STATE_READ_SRC
|
|
|
|
#undef STATE_READ_DST
|
|
|
|
|
|
|
|
/* Convert port strings to numbers. This needs to be done after
|
|
|
|
the string is parsed, because the prototype might not be designated
|
|
|
|
before the ports (which might be symbolic entries in /etc/services) */
|
|
|
|
|
2004-03-16 21:30:41 +00:00
|
|
|
if (strlen(str_port) != 0) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = IpPort(str_port, proto, &proxy_port);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
} else {
|
|
|
|
proxy_port = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(str_server_port) != 0) {
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = IpPort(str_server_port, proto, &server_port);
|
2006-12-15 12:50:06 +00:00
|
|
|
if (err) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
} else {
|
|
|
|
server_port = 0;
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Check that at least the server address has been defined */
|
2006-12-15 12:50:06 +00:00
|
|
|
if (server_addr.s_addr == 0) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
1999-02-27 02:16:01 +00:00
|
|
|
|
|
|
|
/* Add to linked list */
|
2004-03-16 21:30:41 +00:00
|
|
|
proxy_entry = malloc(sizeof(struct proxy_entry));
|
2006-12-15 12:50:06 +00:00
|
|
|
if (proxy_entry == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto getout;
|
|
|
|
}
|
2004-03-16 21:30:41 +00:00
|
|
|
|
|
|
|
proxy_entry->proxy_type = proxy_type;
|
|
|
|
proxy_entry->rule_index = rule_index;
|
|
|
|
proxy_entry->proto = proto;
|
|
|
|
proxy_entry->proxy_port = htons(proxy_port);
|
|
|
|
proxy_entry->server_port = htons(server_port);
|
|
|
|
proxy_entry->server_addr = server_addr;
|
|
|
|
proxy_entry->src_addr.s_addr = src_addr.s_addr & src_mask.s_addr;
|
|
|
|
proxy_entry->dst_addr.s_addr = dst_addr.s_addr & dst_mask.s_addr;
|
|
|
|
proxy_entry->src_mask = src_mask;
|
|
|
|
proxy_entry->dst_mask = dst_mask;
|
|
|
|
|
|
|
|
RuleAdd(la, proxy_entry);
|
|
|
|
|
2006-12-15 12:50:06 +00:00
|
|
|
getout:
|
|
|
|
LIBALIAS_UNLOCK(la);
|
|
|
|
return (ret);
|
1999-02-27 02:16:01 +00:00
|
|
|
}
|