xref: /freebsd/sys/netinet/libalias/alias.h (revision 61bf830cbb260c2a046cb44421d319184393e028)
13b160b8bSBrian Somers /* lint -save -library Flexelint comment for external headers */
23b160b8bSBrian Somers 
371593f95SBrian Somers /*-
44d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
5fe267a55SPedro F. Giffuni  *
6e83aaae3SBrian Somers  * Copyright (c) 2001 Charles Mott <cm@linktel.net>
7f987e1bdSBrian Somers  * All rights reserved.
8f987e1bdSBrian Somers  *
9f987e1bdSBrian Somers  * Redistribution and use in source and binary forms, with or without
10f987e1bdSBrian Somers  * modification, are permitted provided that the following conditions
11f987e1bdSBrian Somers  * are met:
12f987e1bdSBrian Somers  * 1. Redistributions of source code must retain the above copyright
13f987e1bdSBrian Somers  *    notice, this list of conditions and the following disclaimer.
14f987e1bdSBrian Somers  * 2. Redistributions in binary form must reproduce the above copyright
15f987e1bdSBrian Somers  *    notice, this list of conditions and the following disclaimer in the
16f987e1bdSBrian Somers  *    documentation and/or other materials provided with the distribution.
17f987e1bdSBrian Somers  *
18f987e1bdSBrian Somers  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19f987e1bdSBrian Somers  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20f987e1bdSBrian Somers  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21f987e1bdSBrian Somers  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22f987e1bdSBrian Somers  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23f987e1bdSBrian Somers  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24f987e1bdSBrian Somers  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25f987e1bdSBrian Somers  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26f987e1bdSBrian Somers  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27f987e1bdSBrian Somers  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28f987e1bdSBrian Somers  * SUCH DAMAGE.
29f987e1bdSBrian Somers  */
30f987e1bdSBrian Somers 
31be4f3cd0SPaolo Pisati /*
3271593f95SBrian Somers  * Alias.h defines the outside world interfaces for the packet aliasing
3371593f95SBrian Somers  * software.
3471593f95SBrian Somers  *
3571593f95SBrian Somers  * This software is placed into the public domain with no restrictions on its
3671593f95SBrian Somers  * distribution.
373b160b8bSBrian Somers  */
383b160b8bSBrian Somers 
393b160b8bSBrian Somers #ifndef _ALIAS_H_
403b160b8bSBrian Somers #define	_ALIAS_H_
413b160b8bSBrian Somers 
42be4f3cd0SPaolo Pisati #include <netinet/in_systm.h>
43be4f3cd0SPaolo Pisati #include <netinet/in.h>
44be4f3cd0SPaolo Pisati #include <netinet/ip.h>
45be4f3cd0SPaolo Pisati 
46be4f3cd0SPaolo Pisati #define LIBALIAS_BUF_SIZE 128
47f87fe393SGleb Smirnoff #ifdef _KERNEL
48f87fe393SGleb Smirnoff /*
49f87fe393SGleb Smirnoff  * The kernel version of libalias does not support these features.
50f87fe393SGleb Smirnoff  */
51f87fe393SGleb Smirnoff #define	NO_FW_PUNCH
52f87fe393SGleb Smirnoff #define	NO_USE_SOCKETS
53f87fe393SGleb Smirnoff #endif
54f87fe393SGleb Smirnoff 
555e289f9eSPoul-Henning Kamp /*
565e289f9eSPoul-Henning Kamp  * The external interface to libalias, the packet aliasing engine.
575e289f9eSPoul-Henning Kamp  *
585e289f9eSPoul-Henning Kamp  * There are two sets of functions:
595e289f9eSPoul-Henning Kamp  *
605e289f9eSPoul-Henning Kamp  * PacketAlias*() the old API which doesn't take an instance pointer
615e289f9eSPoul-Henning Kamp  * and therefore can only have one packet engine at a time.
625e289f9eSPoul-Henning Kamp  *
635e289f9eSPoul-Henning Kamp  * LibAlias*() the new API which takes as first argument a pointer to
645e289f9eSPoul-Henning Kamp  * the instance of the packet aliasing engine.
655e289f9eSPoul-Henning Kamp  *
665e289f9eSPoul-Henning Kamp  * The functions otherwise correspond to each other one for one, except
675e289f9eSPoul-Henning Kamp  * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were
685e289f9eSPoul-Henning Kamp  * were misnamed in the old API.
695e289f9eSPoul-Henning Kamp  */
705e289f9eSPoul-Henning Kamp 
715e289f9eSPoul-Henning Kamp /*
725e289f9eSPoul-Henning Kamp  * The instance structure
735e289f9eSPoul-Henning Kamp  */
745e289f9eSPoul-Henning Kamp struct libalias;
755e289f9eSPoul-Henning Kamp 
765e289f9eSPoul-Henning Kamp /*
775e289f9eSPoul-Henning Kamp  * An anonymous structure, a pointer to which is returned from
785e289f9eSPoul-Henning Kamp  * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or
795e289f9eSPoul-Henning Kamp  * PacketAliasRedirectProto(), passed to PacketAliasAddServer(),
805e289f9eSPoul-Henning Kamp  * and freed by PacketAliasRedirectDelete().
815e289f9eSPoul-Henning Kamp  */
825e289f9eSPoul-Henning Kamp struct alias_link;
835e289f9eSPoul-Henning Kamp 
845e289f9eSPoul-Henning Kamp /* Initialization and control functions. */
855e289f9eSPoul-Henning Kamp struct libalias *LibAliasInit(struct libalias *);
865e289f9eSPoul-Henning Kamp void		LibAliasSetAddress(struct libalias *, struct in_addr _addr);
87a08cdb6cSNeel Chauhan void		LibAliasSetAliasPortRange(struct libalias *la, u_short port_low, u_short port_hi);
885e289f9eSPoul-Henning Kamp void		LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num);
895e289f9eSPoul-Henning Kamp void		LibAliasSetSkinnyPort(struct libalias *, unsigned int _port);
90effc8e57SLutz Donnerhacke unsigned int	LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask);
915e289f9eSPoul-Henning Kamp void		LibAliasUninit(struct libalias *);
925e289f9eSPoul-Henning Kamp 
935e289f9eSPoul-Henning Kamp /* Packet Handling functions. */
94bc596e56SAlex Richardson int		LibAliasIn (struct libalias *, void *_ptr, int _maxpacketsize);
95bc596e56SAlex Richardson int		LibAliasOut(struct libalias *, void *_ptr, int _maxpacketsize);
96bc596e56SAlex Richardson int		LibAliasOutTry(struct libalias *, void *_ptr, int _maxpacketsize, int _create);
97bc596e56SAlex Richardson int		LibAliasUnaliasOut(struct libalias *, void *_ptr, int _maxpacketsize);
985e289f9eSPoul-Henning Kamp 
995e289f9eSPoul-Henning Kamp /* Port and address redirection functions. */
1005e289f9eSPoul-Henning Kamp 
101effc8e57SLutz Donnerhacke int		LibAliasAddServer(struct libalias *, struct alias_link *_lnk,
1025e289f9eSPoul-Henning Kamp 		    struct in_addr _addr, unsigned short _port);
103effc8e57SLutz Donnerhacke struct alias_link * LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr,
1045e289f9eSPoul-Henning Kamp 		    struct in_addr _alias_addr);
105ed01a582SDag-Erling Smørgrav int		LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk);
106ed01a582SDag-Erling Smørgrav void		LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk);
107effc8e57SLutz Donnerhacke struct alias_link * LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr,
1085e289f9eSPoul-Henning Kamp 		    unsigned short _src_port, struct in_addr _dst_addr,
1095e289f9eSPoul-Henning Kamp 		    unsigned short _dst_port, struct in_addr _alias_addr,
1105e289f9eSPoul-Henning Kamp 		    unsigned short _alias_port, unsigned char _proto);
111effc8e57SLutz Donnerhacke struct alias_link * LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr,
1125e289f9eSPoul-Henning Kamp 		    struct in_addr _dst_addr, struct in_addr _alias_addr,
1135e289f9eSPoul-Henning Kamp 		    unsigned char _proto);
1145e289f9eSPoul-Henning Kamp 
1155e289f9eSPoul-Henning Kamp /* Fragment Handling functions. */
116bc596e56SAlex Richardson void		LibAliasFragmentIn(struct libalias *, void *_ptr, void *_ptr_fragment);
117bc596e56SAlex Richardson void	       *LibAliasGetFragment(struct libalias *, void *_ptr);
118bc596e56SAlex Richardson int		LibAliasSaveFragment(struct libalias *, void *_ptr);
1195e289f9eSPoul-Henning Kamp 
1205e289f9eSPoul-Henning Kamp /* Miscellaneous functions. */
121effc8e57SLutz Donnerhacke unsigned short	LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes);
1225e289f9eSPoul-Henning Kamp void		LibAliasSetTarget(struct libalias *, struct in_addr _target_addr);
1235e289f9eSPoul-Henning Kamp 
1245e289f9eSPoul-Henning Kamp /* Transparent proxying routines. */
1255e289f9eSPoul-Henning Kamp int		LibAliasProxyRule(struct libalias *, const char *_cmd);
1265e289f9eSPoul-Henning Kamp 
127be4f3cd0SPaolo Pisati /* Module handling API */
128be4f3cd0SPaolo Pisati int		LibAliasLoadModule(char *);
129be4f3cd0SPaolo Pisati int		LibAliasUnLoadAllModule(void);
130be4f3cd0SPaolo Pisati int		LibAliasRefreshModules(void);
1315e289f9eSPoul-Henning Kamp 
132e876228eSPaolo Pisati /* Mbuf helper function. */
133e876228eSPaolo Pisati struct mbuf    *m_megapullup(struct mbuf *, int);
134e876228eSPaolo Pisati 
1355e289f9eSPoul-Henning Kamp /*
1365e289f9eSPoul-Henning Kamp  * Mode flags and other constants.
1375e289f9eSPoul-Henning Kamp  */
1385e289f9eSPoul-Henning Kamp 
13971593f95SBrian Somers /* Mode flags, set using PacketAliasSetMode() */
1403efa11bbSBrian Somers 
14171593f95SBrian Somers /*
14271593f95SBrian Somers  * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log
14371593f95SBrian Somers  * every time a link is created or deleted.  This is useful for debugging.
14471593f95SBrian Somers  */
1453efa11bbSBrian Somers #define	PKT_ALIAS_LOG			0x01
1463b160b8bSBrian Somers 
14771593f95SBrian Somers /*
14871593f95SBrian Somers  * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp,
14971593f95SBrian Somers  * telnet or web servers will be prevented by the aliasing mechanism.
15071593f95SBrian Somers  */
1513efa11bbSBrian Somers #define	PKT_ALIAS_DENY_INCOMING		0x02
1523b160b8bSBrian Somers 
15371593f95SBrian Somers /*
15471593f95SBrian Somers  * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the
15571593f95SBrian Somers  * same port as they originated on.  This allows e.g. rsh to work *99% of the
15671593f95SBrian Somers  * time*, but _not_ 100% (it will be slightly flakey instead of not working
15771593f95SBrian Somers  * at all).  This mode bit is set by PacketAliasInit(), so it is a default
15871593f95SBrian Somers  * mode of operation.
15971593f95SBrian Somers  */
1603efa11bbSBrian Somers #define	PKT_ALIAS_SAME_PORTS		0x04
1613b160b8bSBrian Somers 
16271593f95SBrian Somers /*
16371593f95SBrian Somers  * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g.
16471593f95SBrian Somers  * destination port and/or address is zero), the packet aliasing engine will
16571593f95SBrian Somers  * attempt to allocate a socket for the aliasing port it chooses.  This will
16671593f95SBrian Somers  * avoid interference with the host machine.  Fully specified links do not
16771593f95SBrian Somers  * require this.  This bit is set after a call to PacketAliasInit(), so it is
16871593f95SBrian Somers  * a default mode of operation.
16971593f95SBrian Somers  */
17000fc9a5bSGleb Smirnoff #ifndef NO_USE_SOCKETS
1713efa11bbSBrian Somers #define	PKT_ALIAS_USE_SOCKETS		0x08
17200fc9a5bSGleb Smirnoff #endif
17371593f95SBrian Somers /*-
17471593f95SBrian Somers  * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with
17571593f95SBrian Somers  * unregistered source addresses will be aliased.  Private
17671593f95SBrian Somers  * addresses are those in the following ranges:
17771593f95SBrian Somers  *
17871593f95SBrian Somers  *		10.0.0.0     ->   10.255.255.255
17971593f95SBrian Somers  *		172.16.0.0   ->   172.31.255.255
18071593f95SBrian Somers  *		192.168.0.0  ->   192.168.255.255
18171593f95SBrian Somers  */
1823efa11bbSBrian Somers #define	PKT_ALIAS_UNREGISTERED_ONLY	0x10
1833b160b8bSBrian Somers 
18471593f95SBrian Somers /*
18571593f95SBrian Somers  * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic
18671593f95SBrian Somers  * aliasing links will be reset whenever PacketAliasSetAddress() changes the
18771593f95SBrian Somers  * default aliasing address.  If the default aliasing address is left
18871593f95SBrian Somers  * unchanged by this function call, then the table of dynamic aliasing links
18971593f95SBrian Somers  * will be left intact.  This bit is set after a call to PacketAliasInit().
19071593f95SBrian Somers  */
1913efa11bbSBrian Somers #define	PKT_ALIAS_RESET_ON_ADDR_CHANGE	0x20
1923b160b8bSBrian Somers 
19371593f95SBrian Somers /*
19471593f95SBrian Somers  * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only
19571593f95SBrian Somers  * transparent proxying is performed.
19671593f95SBrian Somers  */
1977d96f4efSBrian Somers #define	PKT_ALIAS_PROXY_ONLY		0x40
1987d96f4efSBrian Somers 
19971593f95SBrian Somers /*
20071593f95SBrian Somers  * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and
20171593f95SBrian Somers  * PacketAliasOut() are reversed.
20271593f95SBrian Somers  */
2037d96f4efSBrian Somers #define	PKT_ALIAS_REVERSE		0x80
2047d96f4efSBrian Somers 
20581a65464SAndrey V. Elsukov #ifndef NO_FW_PUNCH
20681a65464SAndrey V. Elsukov /*
20781a65464SAndrey V. Elsukov  * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will
20881a65464SAndrey V. Elsukov  * create a 'hole' in the firewall to allow the transfers to work.  The
20981a65464SAndrey V. Elsukov  * ipfw rule number that the hole is created with is controlled by
21081a65464SAndrey V. Elsukov  * PacketAliasSetFWBase().  The hole will be attached to that
21181a65464SAndrey V. Elsukov  * particular alias_link, so when the link goes away the hole is deleted.
21281a65464SAndrey V. Elsukov  */
21381a65464SAndrey V. Elsukov #define	PKT_ALIAS_PUNCH_FW		0x100
21481a65464SAndrey V. Elsukov #endif
21581a65464SAndrey V. Elsukov 
2161875bbfeSAndrey V. Elsukov /*
2171875bbfeSAndrey V. Elsukov  * If PKT_ALIAS_SKIP_GLOBAL is set, nat instance is not checked for matching
2181875bbfeSAndrey V. Elsukov  * states in 'ipfw nat global' rule.
2191875bbfeSAndrey V. Elsukov  */
2201875bbfeSAndrey V. Elsukov #define	PKT_ALIAS_SKIP_GLOBAL		0x200
2211875bbfeSAndrey V. Elsukov 
22275b89337SAlexander V. Chernikov /*
22375b89337SAlexander V. Chernikov  * Like PKT_ALIAS_UNREGISTERED_ONLY, but includes the RFC 6598
22475b89337SAlexander V. Chernikov  * (Carrier Grade NAT) address range as follows:
22575b89337SAlexander V. Chernikov  *
22675b89337SAlexander V. Chernikov  *		100.64.0.0   ->   100.127.255.255
22775b89337SAlexander V. Chernikov  */
22875b89337SAlexander V. Chernikov #define	PKT_ALIAS_UNREGISTERED_CGN	0x400
22975b89337SAlexander V. Chernikov 
230*61bf830cSDamjan Jovanovic /*
231*61bf830cSDamjan Jovanovic  * When this bit is set, UDP uses endpoint-independent mapping (EIM), as per
232*61bf830cSDamjan Jovanovic  * RFC 4787 ("full cone" NAT of RFC 3489). All packets from the same internal
233*61bf830cSDamjan Jovanovic  * address:port are mapped to the same NAT address:port, regardless of their
234*61bf830cSDamjan Jovanovic  * destination address:port. If filtering rules allow, and if
235*61bf830cSDamjan Jovanovic  * PKT_ALIAS_DENY_INCOMING is unset, any other external address:port can also
236*61bf830cSDamjan Jovanovic  * send to the internal address:port through its mapped NAT address:port. This
237*61bf830cSDamjan Jovanovic  * is more compatible with applications, and can reduce the need for port
238*61bf830cSDamjan Jovanovic  * forwarding, but less scalable as each NAT address:port can only be
239*61bf830cSDamjan Jovanovic  * concurrently used by at most one internal address:port.
240*61bf830cSDamjan Jovanovic  *
241*61bf830cSDamjan Jovanovic  * When this bit is unset, UDP packets use endpoint-dependent mapping (EDM)
242*61bf830cSDamjan Jovanovic  * ("symmetric" NAT). Each connection from a particular internal address:port
243*61bf830cSDamjan Jovanovic  * to different external addresses:ports is mapped to a random and
244*61bf830cSDamjan Jovanovic  * unpredictable NAT address:port. Two appplications behind EDM NATs can only
245*61bf830cSDamjan Jovanovic  * connect to each other by port forwarding on the NAT, or tunnelling through
246*61bf830cSDamjan Jovanovic  * an in-between server.
247*61bf830cSDamjan Jovanovic  */
248*61bf830cSDamjan Jovanovic #define PKT_ALIAS_UDP_EIM		0x800
249*61bf830cSDamjan Jovanovic 
25071593f95SBrian Somers /* Function return codes. */
2513b160b8bSBrian Somers #define	PKT_ALIAS_ERROR			-1
2523b160b8bSBrian Somers #define	PKT_ALIAS_OK			1
2533b160b8bSBrian Somers #define	PKT_ALIAS_IGNORED		2
2543b160b8bSBrian Somers #define	PKT_ALIAS_UNRESOLVED_FRAGMENT	3
2553b160b8bSBrian Somers #define	PKT_ALIAS_FOUND_HEADER_FRAGMENT	4
2563b160b8bSBrian Somers 
25771593f95SBrian Somers #endif				/* !_ALIAS_H_ */
25871593f95SBrian Somers 
2593b160b8bSBrian Somers /* lint -restore */
260