alias.h revision 162674
174778Sbrian/* lint -save -library Flexelint comment for external headers */
226026Sbrian
374778Sbrian/*-
485964Sbrian * Copyright (c) 2001 Charles Mott <cm@linktel.net>
577701Sbrian * All rights reserved.
677701Sbrian *
777701Sbrian * Redistribution and use in source and binary forms, with or without
877701Sbrian * modification, are permitted provided that the following conditions
977701Sbrian * are met:
1077701Sbrian * 1. Redistributions of source code must retain the above copyright
1177701Sbrian *    notice, this list of conditions and the following disclaimer.
1277701Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1377701Sbrian *    notice, this list of conditions and the following disclaimer in the
1477701Sbrian *    documentation and/or other materials provided with the distribution.
1577701Sbrian *
1677701Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1777701Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1877701Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1977701Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2077701Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2177701Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2277701Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2377701Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2477701Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2577701Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2677701Sbrian * SUCH DAMAGE.
2777701Sbrian *
2877701Sbrian * $FreeBSD: head/sys/netinet/libalias/alias.h 162674 2006-09-26 23:26:53Z piso $
2977701Sbrian */
3077701Sbrian
31162674Spiso/*
3274778Sbrian * Alias.h defines the outside world interfaces for the packet aliasing
3374778Sbrian * software.
3499207Sbrian *
3574778Sbrian * This software is placed into the public domain with no restrictions on its
3674778Sbrian * distribution.
3774778Sbrian */
3826026Sbrian
3926026Sbrian#ifndef _ALIAS_H_
4074778Sbrian#define	_ALIAS_H_
4126026Sbrian
42162674Spiso#include <netinet/in_systm.h>
43162674Spiso#include <netinet/in.h>
44162674Spiso#include <netinet/ip.h>
45162674Spiso
46162674Spiso#define LIBALIAS_BUF_SIZE 128
47145931Sglebius#ifdef	_KERNEL
48124621Sphk/*
49145931Sglebius * The kernel version of libalias does not support these features.
50145931Sglebius */
51145931Sglebius#define	NO_FW_PUNCH
52145931Sglebius#define	NO_USE_SOCKETS
53145931Sglebius#endif
54145931Sglebius
55145931Sglebius/*
56124621Sphk * The external interface to libalias, the packet aliasing engine.
57124621Sphk *
58124621Sphk * There are two sets of functions:
59124621Sphk *
60124621Sphk * PacketAlias*() the old API which doesn't take an instance pointer
61124621Sphk * and therefore can only have one packet engine at a time.
62124621Sphk *
63127094Sdes * LibAlias*() the new API which takes as first argument a pointer to
64124621Sphk * the instance of the packet aliasing engine.
65124621Sphk *
66124621Sphk * The functions otherwise correspond to each other one for one, except
67127094Sdes * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
68124621Sphk * were misnamed in the old API.
69124621Sphk */
7026026Sbrian
71124621Sphk/*
72124621Sphk * The instance structure
73124621Sphk */
74124621Sphkstruct libalias;
75124621Sphk
76124621Sphk/*
77124621Sphk * An anonymous structure, a pointer to which is returned from
78124621Sphk * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or
79124621Sphk * PacketAliasRedirectProto(), passed to PacketAliasAddServer(),
80124621Sphk * and freed by PacketAliasRedirectDelete().
81124621Sphk */
82127094Sdesstruct alias_link;
83124621Sphk
84124621Sphk
85124621Sphk/* OLD API */
86124621Sphk
8774778Sbrian/* Initialization and control functions. */
88127094Sdesvoid		PacketAliasInit(void);
89127094Sdesvoid		PacketAliasSetAddress(struct in_addr _addr);
90127094Sdesvoid		PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
91127094Sdesvoid		PacketAliasSetSkinnyPort(unsigned int _port);
9274778Sbrianunsigned int
93127094Sdes		PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
94127094Sdesvoid		PacketAliasUninit(void);
9527864Sbrian
9674778Sbrian/* Packet Handling functions. */
97127094Sdesint		PacketAliasIn(char *_ptr, int _maxpacketsize);
98127094Sdesint		PacketAliasOut(char *_ptr, int _maxpacketsize);
99127094Sdesint		PacketUnaliasOut(char *_ptr, int _maxpacketsize);
10027864Sbrian
10174778Sbrian/* Port and address redirection functions. */
10232377Seivind
10327864Sbrian
104127094Sdesint
105131614SdesPacketAliasAddServer(struct alias_link *_lnk,
106127094Sdes    struct in_addr _addr, unsigned short _port);
10774778Sbrianstruct alias_link *
108127094SdesPacketAliasRedirectAddr(struct in_addr _src_addr,
109127094Sdes    struct in_addr _alias_addr);
110131614Sdesint		PacketAliasRedirectDynamic(struct alias_link *_lnk);
111131614Sdesvoid		PacketAliasRedirectDelete(struct alias_link *_lnk);
11274778Sbrianstruct alias_link *
113127094SdesPacketAliasRedirectPort(struct in_addr _src_addr,
114127094Sdes    unsigned short _src_port, struct in_addr _dst_addr,
115127094Sdes    unsigned short _dst_port, struct in_addr _alias_addr,
116127094Sdes    unsigned short _alias_port, unsigned char _proto);
11774778Sbrianstruct alias_link *
118127094SdesPacketAliasRedirectProto(struct in_addr _src_addr,
119127094Sdes    struct in_addr _dst_addr, struct in_addr _alias_addr,
120127094Sdes    unsigned char _proto);
12127864Sbrian
12274778Sbrian/* Fragment Handling functions. */
123127094Sdesvoid		PacketAliasFragmentIn(char *_ptr, char *_ptr_fragment);
124127094Sdeschar           *PacketAliasGetFragment(char *_ptr);
125127094Sdesint		PacketAliasSaveFragment(char *_ptr);
12632377Seivind
12774778Sbrian/* Miscellaneous functions. */
128127094Sdesint		PacketAliasCheckNewLink(void);
12974778Sbrianunsigned short
130127094Sdes		PacketAliasInternetChecksum(unsigned short *_ptr, int _nbytes);
131127094Sdesvoid		PacketAliasSetTarget(struct in_addr _target_addr);
13227864Sbrian
13374778Sbrian/* Transparent proxying routines. */
134127094Sdesint		PacketAliasProxyRule(const char *_cmd);
13527864Sbrian
136124621Sphk/* NEW API */
137124621Sphk
138124621Sphk/* Initialization and control functions. */
139127094Sdesstruct libalias *LibAliasInit(struct libalias *);
140127094Sdesvoid		LibAliasSetAddress(struct libalias *, struct in_addr _addr);
141127094Sdesvoid		LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
142127094Sdesvoid		LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
143124621Sphkunsigned int
144127094Sdes		LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
145127094Sdesvoid		LibAliasUninit(struct libalias *);
146124621Sphk
147124621Sphk/* Packet Handling functions. */
148127094Sdesint		LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
149127094Sdesint		LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
150131566Sphkint		LibAliasOutTry(struct libalias *, char *_ptr, int _maxpacketsize, int _create);
151127094Sdesint		LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
152124621Sphk
153124621Sphk/* Port and address redirection functions. */
154124621Sphk
155127094Sdesint
156131614SdesLibAliasAddServer(struct libalias *, struct alias_link *_lnk,
157127094Sdes    struct in_addr _addr, unsigned short _port);
158124621Sphkstruct alias_link *
159127094SdesLibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
160127094Sdes    struct in_addr _alias_addr);
161131614Sdesint		LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
162131614Sdesvoid		LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
163124621Sphkstruct alias_link *
164127094SdesLibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
165127094Sdes    unsigned short _src_port, struct in_addr _dst_addr,
166127094Sdes    unsigned short _dst_port, struct in_addr _alias_addr,
167127094Sdes    unsigned short _alias_port, unsigned char _proto);
168124621Sphkstruct alias_link *
169127094SdesLibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
170127094Sdes    struct in_addr _dst_addr, struct in_addr _alias_addr,
171127094Sdes    unsigned char _proto);
172124621Sphk
173124621Sphk/* Fragment Handling functions. */
174127094Sdesvoid		LibAliasFragmentIn(struct libalias *, char *_ptr, char *_ptr_fragment);
175127094Sdeschar           *LibAliasGetFragment(struct libalias *, char *_ptr);
176127094Sdesint		LibAliasSaveFragment(struct libalias *, char *_ptr);
177124621Sphk
178124621Sphk/* Miscellaneous functions. */
179127094Sdesint		LibAliasCheckNewLink(struct libalias *);
180124621Sphkunsigned short
181127094Sdes		LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
182127094Sdesvoid		LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
183124621Sphk
184124621Sphk/* Transparent proxying routines. */
185127094Sdesint		LibAliasProxyRule(struct libalias *, const char *_cmd);
186124621Sphk
187162674Spiso/* Module handling API */
188162674Spisoint             LibAliasLoadModule(char *);
189162674Spisoint             LibAliasUnLoadAllModule(void);
190162674Spisoint             LibAliasRefreshModules(void);
191124621Sphk
192124621Sphk/*
193124621Sphk * Mode flags and other constants.
194124621Sphk */
195124621Sphk
196124621Sphk
19774778Sbrian/* Mode flags, set using PacketAliasSetMode() */
19863899Sarchie
19974778Sbrian/*
20074778Sbrian * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
20174778Sbrian * every time a link is created or deleted.  This is useful for debugging.
20274778Sbrian */
20374778Sbrian#define	PKT_ALIAS_LOG			0x01
20427864Sbrian
20574778Sbrian/*
20674778Sbrian * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
20774778Sbrian * telnet or web servers will be prevented by the aliasing mechanism.
20874778Sbrian */
20974778Sbrian#define	PKT_ALIAS_DENY_INCOMING		0x02
21059702Sru
21174778Sbrian/*
21274778Sbrian * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
21374778Sbrian * same port as they originated on.  This allows e.g. rsh to work *99% of the
21474778Sbrian * time*, but _not_ 100% (it will be slightly flakey instead of not working
21574778Sbrian * at all).  This mode bit is set by PacketAliasInit(), so it is a default
21674778Sbrian * mode of operation.
21774778Sbrian */
21874778Sbrian#define	PKT_ALIAS_SAME_PORTS		0x04
21944307Sbrian
22074778Sbrian/*
22174778Sbrian * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
22274778Sbrian * destination port and/or address is zero), the packet aliasing engine will
22374778Sbrian * attempt to allocate a socket for the aliasing port it chooses.  This will
22474778Sbrian * avoid interference with the host machine.  Fully specified links do not
22574778Sbrian * require this.  This bit is set after a call to PacketAliasInit(), so it is
22674778Sbrian * a default mode of operation.
22774778Sbrian */
228145926Sglebius#ifndef	NO_USE_SOCKETS
22974778Sbrian#define	PKT_ALIAS_USE_SOCKETS		0x08
230145926Sglebius#endif
23174778Sbrian/*-
23274778Sbrian * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
23374778Sbrian * unregistered source addresses will be aliased.  Private
23474778Sbrian * addresses are those in the following ranges:
23574778Sbrian *
23674778Sbrian *		10.0.0.0     ->   10.255.255.255
23774778Sbrian *		172.16.0.0   ->   172.31.255.255
23874778Sbrian *		192.168.0.0  ->   192.168.255.255
23974778Sbrian */
24074778Sbrian#define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
24127864Sbrian
24274778Sbrian/*
24374778Sbrian * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
24474778Sbrian * aliasing links will be reset whenever PacketAliasSetAddress() changes the
24574778Sbrian * default aliasing address.  If the default aliasing address is left
24674778Sbrian * unchanged by this function call, then the table of dynamic aliasing links
24774778Sbrian * will be left intact.  This bit is set after a call to PacketAliasInit().
24874778Sbrian */
24974778Sbrian#define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
25027864Sbrian
25135314Sbrian#ifndef NO_FW_PUNCH
25274778Sbrian/*
25374778Sbrian * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
25474778Sbrian * create a 'hole' in the firewall to allow the transfers to work.  The
25574778Sbrian * ipfw rule number that the hole is created with is controlled by
25674778Sbrian * PacketAliasSetFWBase().  The hole will be attached to that
25774778Sbrian * particular alias_link, so when the link goes away the hole is deleted.
25874778Sbrian */
25974778Sbrian#define	PKT_ALIAS_PUNCH_FW		0x100
26035314Sbrian#endif
26132377Seivind
26274778Sbrian/*
26374778Sbrian * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
26474778Sbrian * transparent proxying is performed.
26574778Sbrian */
26674778Sbrian#define	PKT_ALIAS_PROXY_ONLY		0x40
26744307Sbrian
26874778Sbrian/*
26974778Sbrian * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
27074778Sbrian * PacketAliasOut() are reversed.
27174778Sbrian */
27274778Sbrian#define	PKT_ALIAS_REVERSE		0x80
27344307Sbrian
27474778Sbrian/* Function return codes. */
27574778Sbrian#define	PKT_ALIAS_ERROR			-1
27674778Sbrian#define	PKT_ALIAS_OK			1
27774778Sbrian#define	PKT_ALIAS_IGNORED		2
27874778Sbrian#define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
27974778Sbrian#define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
28026026Sbrian
281127094Sdes#endif				/* !_ALIAS_H_ */
28274778Sbrian
28374778Sbrian/* lint -restore */
284