alias.h revision 145926
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 145926 2005-05-05 20:25:12Z glebius $
2977701Sbrian */
3077701Sbrian
3177701Sbrian/*-
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
42124621Sphk/*
43124621Sphk * The external interface to libalias, the packet aliasing engine.
44124621Sphk *
45124621Sphk * There are two sets of functions:
46124621Sphk *
47124621Sphk * PacketAlias*() the old API which doesn't take an instance pointer
48124621Sphk * and therefore can only have one packet engine at a time.
49124621Sphk *
50127094Sdes * LibAlias*() the new API which takes as first argument a pointer to
51124621Sphk * the instance of the packet aliasing engine.
52124621Sphk *
53124621Sphk * The functions otherwise correspond to each other one for one, except
54127094Sdes * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
55124621Sphk * were misnamed in the old API.
56124621Sphk */
5726026Sbrian
58124621Sphk/*
59124621Sphk * The instance structure
60124621Sphk */
61124621Sphkstruct libalias;
62124621Sphk
63124621Sphk/*
64124621Sphk * An anonymous structure, a pointer to which is returned from
65124621Sphk * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or
66124621Sphk * PacketAliasRedirectProto(), passed to PacketAliasAddServer(),
67124621Sphk * and freed by PacketAliasRedirectDelete().
68124621Sphk */
69127094Sdesstruct alias_link;
70124621Sphk
71124621Sphk
72124621Sphk/* OLD API */
73124621Sphk
7474778Sbrian/* Initialization and control functions. */
75127094Sdesvoid		PacketAliasInit(void);
76127094Sdesvoid		PacketAliasSetAddress(struct in_addr _addr);
77127094Sdesvoid		PacketAliasSetFWBase(unsigned int _base, unsigned int _num);
78127094Sdesvoid		PacketAliasSetSkinnyPort(unsigned int _port);
7974778Sbrianunsigned int
80127094Sdes		PacketAliasSetMode(unsigned int _flags, unsigned int _mask);
81127094Sdesvoid		PacketAliasUninit(void);
8227864Sbrian
8374778Sbrian/* Packet Handling functions. */
84127094Sdesint		PacketAliasIn(char *_ptr, int _maxpacketsize);
85127094Sdesint		PacketAliasOut(char *_ptr, int _maxpacketsize);
86127094Sdesint		PacketUnaliasOut(char *_ptr, int _maxpacketsize);
8727864Sbrian
8874778Sbrian/* Port and address redirection functions. */
8932377Seivind
9027864Sbrian
91127094Sdesint
92131614SdesPacketAliasAddServer(struct alias_link *_lnk,
93127094Sdes    struct in_addr _addr, unsigned short _port);
9474778Sbrianstruct alias_link *
95127094SdesPacketAliasRedirectAddr(struct in_addr _src_addr,
96127094Sdes    struct in_addr _alias_addr);
97131614Sdesint		PacketAliasRedirectDynamic(struct alias_link *_lnk);
98131614Sdesvoid		PacketAliasRedirectDelete(struct alias_link *_lnk);
9974778Sbrianstruct alias_link *
100127094SdesPacketAliasRedirectPort(struct in_addr _src_addr,
101127094Sdes    unsigned short _src_port, struct in_addr _dst_addr,
102127094Sdes    unsigned short _dst_port, struct in_addr _alias_addr,
103127094Sdes    unsigned short _alias_port, unsigned char _proto);
10474778Sbrianstruct alias_link *
105127094SdesPacketAliasRedirectProto(struct in_addr _src_addr,
106127094Sdes    struct in_addr _dst_addr, struct in_addr _alias_addr,
107127094Sdes    unsigned char _proto);
10827864Sbrian
10974778Sbrian/* Fragment Handling functions. */
110127094Sdesvoid		PacketAliasFragmentIn(char *_ptr, char *_ptr_fragment);
111127094Sdeschar           *PacketAliasGetFragment(char *_ptr);
112127094Sdesint		PacketAliasSaveFragment(char *_ptr);
11332377Seivind
11474778Sbrian/* Miscellaneous functions. */
115127094Sdesint		PacketAliasCheckNewLink(void);
11674778Sbrianunsigned short
117127094Sdes		PacketAliasInternetChecksum(unsigned short *_ptr, int _nbytes);
118127094Sdesvoid		PacketAliasSetTarget(struct in_addr _target_addr);
11927864Sbrian
12074778Sbrian/* Transparent proxying routines. */
121127094Sdesint		PacketAliasProxyRule(const char *_cmd);
12227864Sbrian
123124621Sphk/* NEW API */
124124621Sphk
125124621Sphk/* Initialization and control functions. */
126127094Sdesstruct libalias *LibAliasInit(struct libalias *);
127127094Sdesvoid		LibAliasSetAddress(struct libalias *, struct in_addr _addr);
128127094Sdesvoid		LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
129127094Sdesvoid		LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
130124621Sphkunsigned int
131127094Sdes		LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
132127094Sdesvoid		LibAliasUninit(struct libalias *);
133124621Sphk
134124621Sphk/* Packet Handling functions. */
135127094Sdesint		LibAliasIn (struct libalias *, char *_ptr, int _maxpacketsize);
136127094Sdesint		LibAliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
137131566Sphkint		LibAliasOutTry(struct libalias *, char *_ptr, int _maxpacketsize, int _create);
138127094Sdesint		LibAliasUnaliasOut(struct libalias *, char *_ptr, int _maxpacketsize);
139124621Sphk
140124621Sphk/* Port and address redirection functions. */
141124621Sphk
142127094Sdesint
143131614SdesLibAliasAddServer(struct libalias *, struct alias_link *_lnk,
144127094Sdes    struct in_addr _addr, unsigned short _port);
145124621Sphkstruct alias_link *
146127094SdesLibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
147127094Sdes    struct in_addr _alias_addr);
148131614Sdesint		LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
149131614Sdesvoid		LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
150124621Sphkstruct alias_link *
151127094SdesLibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
152127094Sdes    unsigned short _src_port, struct in_addr _dst_addr,
153127094Sdes    unsigned short _dst_port, struct in_addr _alias_addr,
154127094Sdes    unsigned short _alias_port, unsigned char _proto);
155124621Sphkstruct alias_link *
156127094SdesLibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
157127094Sdes    struct in_addr _dst_addr, struct in_addr _alias_addr,
158127094Sdes    unsigned char _proto);
159124621Sphk
160124621Sphk/* Fragment Handling functions. */
161127094Sdesvoid		LibAliasFragmentIn(struct libalias *, char *_ptr, char *_ptr_fragment);
162127094Sdeschar           *LibAliasGetFragment(struct libalias *, char *_ptr);
163127094Sdesint		LibAliasSaveFragment(struct libalias *, char *_ptr);
164124621Sphk
165124621Sphk/* Miscellaneous functions. */
166127094Sdesint		LibAliasCheckNewLink(struct libalias *);
167124621Sphkunsigned short
168127094Sdes		LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
169127094Sdesvoid		LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
170124621Sphk
171124621Sphk/* Transparent proxying routines. */
172127094Sdesint		LibAliasProxyRule(struct libalias *, const char *_cmd);
173124621Sphk
174124621Sphk
175124621Sphk/*
176124621Sphk * Mode flags and other constants.
177124621Sphk */
178124621Sphk
179124621Sphk
18074778Sbrian/* Mode flags, set using PacketAliasSetMode() */
18163899Sarchie
18274778Sbrian/*
18374778Sbrian * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
18474778Sbrian * every time a link is created or deleted.  This is useful for debugging.
18574778Sbrian */
186145925Sglebius#ifndef	NO_LOGGING
18774778Sbrian#define	PKT_ALIAS_LOG			0x01
188145925Sglebius#endif
18927864Sbrian
19074778Sbrian/*
19174778Sbrian * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
19274778Sbrian * telnet or web servers will be prevented by the aliasing mechanism.
19374778Sbrian */
19474778Sbrian#define	PKT_ALIAS_DENY_INCOMING		0x02
19559702Sru
19674778Sbrian/*
19774778Sbrian * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
19874778Sbrian * same port as they originated on.  This allows e.g. rsh to work *99% of the
19974778Sbrian * time*, but _not_ 100% (it will be slightly flakey instead of not working
20074778Sbrian * at all).  This mode bit is set by PacketAliasInit(), so it is a default
20174778Sbrian * mode of operation.
20274778Sbrian */
20374778Sbrian#define	PKT_ALIAS_SAME_PORTS		0x04
20444307Sbrian
20574778Sbrian/*
20674778Sbrian * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
20774778Sbrian * destination port and/or address is zero), the packet aliasing engine will
20874778Sbrian * attempt to allocate a socket for the aliasing port it chooses.  This will
20974778Sbrian * avoid interference with the host machine.  Fully specified links do not
21074778Sbrian * require this.  This bit is set after a call to PacketAliasInit(), so it is
21174778Sbrian * a default mode of operation.
21274778Sbrian */
213145926Sglebius#ifndef	NO_USE_SOCKETS
21474778Sbrian#define	PKT_ALIAS_USE_SOCKETS		0x08
215145926Sglebius#endif
21674778Sbrian/*-
21774778Sbrian * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
21874778Sbrian * unregistered source addresses will be aliased.  Private
21974778Sbrian * addresses are those in the following ranges:
22074778Sbrian *
22174778Sbrian *		10.0.0.0     ->   10.255.255.255
22274778Sbrian *		172.16.0.0   ->   172.31.255.255
22374778Sbrian *		192.168.0.0  ->   192.168.255.255
22474778Sbrian */
22574778Sbrian#define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
22627864Sbrian
22774778Sbrian/*
22874778Sbrian * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
22974778Sbrian * aliasing links will be reset whenever PacketAliasSetAddress() changes the
23074778Sbrian * default aliasing address.  If the default aliasing address is left
23174778Sbrian * unchanged by this function call, then the table of dynamic aliasing links
23274778Sbrian * will be left intact.  This bit is set after a call to PacketAliasInit().
23374778Sbrian */
23474778Sbrian#define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
23527864Sbrian
23635314Sbrian#ifndef NO_FW_PUNCH
23774778Sbrian/*
23874778Sbrian * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
23974778Sbrian * create a 'hole' in the firewall to allow the transfers to work.  The
24074778Sbrian * ipfw rule number that the hole is created with is controlled by
24174778Sbrian * PacketAliasSetFWBase().  The hole will be attached to that
24274778Sbrian * particular alias_link, so when the link goes away the hole is deleted.
24374778Sbrian */
24474778Sbrian#define	PKT_ALIAS_PUNCH_FW		0x100
24535314Sbrian#endif
24632377Seivind
24774778Sbrian/*
24874778Sbrian * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
24974778Sbrian * transparent proxying is performed.
25074778Sbrian */
25174778Sbrian#define	PKT_ALIAS_PROXY_ONLY		0x40
25244307Sbrian
25374778Sbrian/*
25474778Sbrian * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
25574778Sbrian * PacketAliasOut() are reversed.
25674778Sbrian */
25774778Sbrian#define	PKT_ALIAS_REVERSE		0x80
25844307Sbrian
25974778Sbrian/* Function return codes. */
26074778Sbrian#define	PKT_ALIAS_ERROR			-1
26174778Sbrian#define	PKT_ALIAS_OK			1
26274778Sbrian#define	PKT_ALIAS_IGNORED		2
26374778Sbrian#define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
26474778Sbrian#define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
26526026Sbrian
266127094Sdes#endif				/* !_ALIAS_H_ */
26774778Sbrian
26874778Sbrian/* lint -restore */
269