13b160b8bSBrian Somers /* lint -save -library Flexelint comment for external headers */ 23b160b8bSBrian Somers 371593f95SBrian Somers /*- 4fe267a55SPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 * $FreeBSD$ 31f987e1bdSBrian Somers */ 32f987e1bdSBrian Somers 33be4f3cd0SPaolo Pisati /* 3471593f95SBrian Somers * Alias.h defines the outside world interfaces for the packet aliasing 3571593f95SBrian Somers * software. 3671593f95SBrian Somers * 3771593f95SBrian Somers * This software is placed into the public domain with no restrictions on its 3871593f95SBrian Somers * distribution. 393b160b8bSBrian Somers */ 403b160b8bSBrian Somers 413b160b8bSBrian Somers #ifndef _ALIAS_H_ 423b160b8bSBrian Somers #define _ALIAS_H_ 433b160b8bSBrian Somers 44be4f3cd0SPaolo Pisati #include <netinet/in_systm.h> 45be4f3cd0SPaolo Pisati #include <netinet/in.h> 46be4f3cd0SPaolo Pisati #include <netinet/ip.h> 47be4f3cd0SPaolo Pisati 48be4f3cd0SPaolo Pisati #define LIBALIAS_BUF_SIZE 128 49f87fe393SGleb Smirnoff #ifdef _KERNEL 50f87fe393SGleb Smirnoff /* 51f87fe393SGleb Smirnoff * The kernel version of libalias does not support these features. 52f87fe393SGleb Smirnoff */ 53f87fe393SGleb Smirnoff #define NO_FW_PUNCH 54f87fe393SGleb Smirnoff #define NO_USE_SOCKETS 55f87fe393SGleb Smirnoff #endif 56f87fe393SGleb Smirnoff 575e289f9eSPoul-Henning Kamp /* 585e289f9eSPoul-Henning Kamp * The external interface to libalias, the packet aliasing engine. 595e289f9eSPoul-Henning Kamp * 605e289f9eSPoul-Henning Kamp * There are two sets of functions: 615e289f9eSPoul-Henning Kamp * 625e289f9eSPoul-Henning Kamp * PacketAlias*() the old API which doesn't take an instance pointer 635e289f9eSPoul-Henning Kamp * and therefore can only have one packet engine at a time. 645e289f9eSPoul-Henning Kamp * 655e289f9eSPoul-Henning Kamp * LibAlias*() the new API which takes as first argument a pointer to 665e289f9eSPoul-Henning Kamp * the instance of the packet aliasing engine. 675e289f9eSPoul-Henning Kamp * 685e289f9eSPoul-Henning Kamp * The functions otherwise correspond to each other one for one, except 695e289f9eSPoul-Henning Kamp * for the LibAliasUnaliasOut()/PacketUnaliasOut() function which were 705e289f9eSPoul-Henning Kamp * were misnamed in the old API. 715e289f9eSPoul-Henning Kamp */ 725e289f9eSPoul-Henning Kamp 735e289f9eSPoul-Henning Kamp /* 745e289f9eSPoul-Henning Kamp * The instance structure 755e289f9eSPoul-Henning Kamp */ 765e289f9eSPoul-Henning Kamp struct libalias; 775e289f9eSPoul-Henning Kamp 785e289f9eSPoul-Henning Kamp /* 795e289f9eSPoul-Henning Kamp * An anonymous structure, a pointer to which is returned from 805e289f9eSPoul-Henning Kamp * PacketAliasRedirectAddr(), PacketAliasRedirectPort() or 815e289f9eSPoul-Henning Kamp * PacketAliasRedirectProto(), passed to PacketAliasAddServer(), 825e289f9eSPoul-Henning Kamp * and freed by PacketAliasRedirectDelete(). 835e289f9eSPoul-Henning Kamp */ 845e289f9eSPoul-Henning Kamp struct alias_link; 855e289f9eSPoul-Henning Kamp 865e289f9eSPoul-Henning Kamp /* Initialization and control functions. */ 875e289f9eSPoul-Henning Kamp struct libalias *LibAliasInit(struct libalias *); 885e289f9eSPoul-Henning Kamp void LibAliasSetAddress(struct libalias *, struct in_addr _addr); 89*a08cdb6cSNeel Chauhan void LibAliasSetAliasPortRange(struct libalias *la, u_short port_low, u_short port_hi); 905e289f9eSPoul-Henning Kamp void LibAliasSetFWBase(struct libalias *, unsigned int _base, unsigned int _num); 915e289f9eSPoul-Henning Kamp void LibAliasSetSkinnyPort(struct libalias *, unsigned int _port); 925e289f9eSPoul-Henning Kamp unsigned int 935e289f9eSPoul-Henning Kamp LibAliasSetMode(struct libalias *, unsigned int _flags, unsigned int _mask); 945e289f9eSPoul-Henning Kamp void LibAliasUninit(struct libalias *); 955e289f9eSPoul-Henning Kamp 965e289f9eSPoul-Henning Kamp /* Packet Handling functions. */ 97bc596e56SAlex Richardson int LibAliasIn (struct libalias *, void *_ptr, int _maxpacketsize); 98bc596e56SAlex Richardson int LibAliasOut(struct libalias *, void *_ptr, int _maxpacketsize); 99bc596e56SAlex Richardson int LibAliasOutTry(struct libalias *, void *_ptr, int _maxpacketsize, int _create); 100bc596e56SAlex Richardson int LibAliasUnaliasOut(struct libalias *, void *_ptr, int _maxpacketsize); 1015e289f9eSPoul-Henning Kamp 1025e289f9eSPoul-Henning Kamp /* Port and address redirection functions. */ 1035e289f9eSPoul-Henning Kamp 104f0f93429SDag-Erling Smørgrav int 105ed01a582SDag-Erling Smørgrav LibAliasAddServer(struct libalias *, struct alias_link *_lnk, 1065e289f9eSPoul-Henning Kamp struct in_addr _addr, unsigned short _port); 1075e289f9eSPoul-Henning Kamp struct alias_link * 1085e289f9eSPoul-Henning Kamp LibAliasRedirectAddr(struct libalias *, struct in_addr _src_addr, 1095e289f9eSPoul-Henning Kamp struct in_addr _alias_addr); 110ed01a582SDag-Erling Smørgrav int LibAliasRedirectDynamic(struct libalias *, struct alias_link *_lnk); 111ed01a582SDag-Erling Smørgrav void LibAliasRedirectDelete(struct libalias *, struct alias_link *_lnk); 1125e289f9eSPoul-Henning Kamp struct alias_link * 1135e289f9eSPoul-Henning Kamp LibAliasRedirectPort(struct libalias *, struct in_addr _src_addr, 1145e289f9eSPoul-Henning Kamp unsigned short _src_port, struct in_addr _dst_addr, 1155e289f9eSPoul-Henning Kamp unsigned short _dst_port, struct in_addr _alias_addr, 1165e289f9eSPoul-Henning Kamp unsigned short _alias_port, unsigned char _proto); 1175e289f9eSPoul-Henning Kamp struct alias_link * 1185e289f9eSPoul-Henning Kamp LibAliasRedirectProto(struct libalias *, struct in_addr _src_addr, 1195e289f9eSPoul-Henning Kamp struct in_addr _dst_addr, struct in_addr _alias_addr, 1205e289f9eSPoul-Henning Kamp unsigned char _proto); 1215e289f9eSPoul-Henning Kamp 1225e289f9eSPoul-Henning Kamp /* Fragment Handling functions. */ 123bc596e56SAlex Richardson void LibAliasFragmentIn(struct libalias *, void *_ptr, void *_ptr_fragment); 124bc596e56SAlex Richardson void *LibAliasGetFragment(struct libalias *, void *_ptr); 125bc596e56SAlex Richardson int LibAliasSaveFragment(struct libalias *, void *_ptr); 1265e289f9eSPoul-Henning Kamp 1275e289f9eSPoul-Henning Kamp /* Miscellaneous functions. */ 1285e289f9eSPoul-Henning Kamp int LibAliasCheckNewLink(struct libalias *); 1295e289f9eSPoul-Henning Kamp unsigned short 1305e289f9eSPoul-Henning Kamp LibAliasInternetChecksum(struct libalias *, unsigned short *_ptr, int _nbytes); 1315e289f9eSPoul-Henning Kamp void LibAliasSetTarget(struct libalias *, struct in_addr _target_addr); 1325e289f9eSPoul-Henning Kamp 1335e289f9eSPoul-Henning Kamp /* Transparent proxying routines. */ 1345e289f9eSPoul-Henning Kamp int LibAliasProxyRule(struct libalias *, const char *_cmd); 1355e289f9eSPoul-Henning Kamp 136be4f3cd0SPaolo Pisati /* Module handling API */ 137be4f3cd0SPaolo Pisati int LibAliasLoadModule(char *); 138be4f3cd0SPaolo Pisati int LibAliasUnLoadAllModule(void); 139be4f3cd0SPaolo Pisati int LibAliasRefreshModules(void); 1405e289f9eSPoul-Henning Kamp 141e876228eSPaolo Pisati /* Mbuf helper function. */ 142e876228eSPaolo Pisati struct mbuf *m_megapullup(struct mbuf *, int); 143e876228eSPaolo Pisati 1445e289f9eSPoul-Henning Kamp /* 1455e289f9eSPoul-Henning Kamp * Mode flags and other constants. 1465e289f9eSPoul-Henning Kamp */ 1475e289f9eSPoul-Henning Kamp 14871593f95SBrian Somers /* Mode flags, set using PacketAliasSetMode() */ 1493efa11bbSBrian Somers 15071593f95SBrian Somers /* 15171593f95SBrian Somers * If PKT_ALIAS_LOG is set, a message will be printed to /var/log/alias.log 15271593f95SBrian Somers * every time a link is created or deleted. This is useful for debugging. 15371593f95SBrian Somers */ 1543efa11bbSBrian Somers #define PKT_ALIAS_LOG 0x01 1553b160b8bSBrian Somers 15671593f95SBrian Somers /* 15771593f95SBrian Somers * If PKT_ALIAS_DENY_INCOMING is set, then incoming connections (e.g. to ftp, 15871593f95SBrian Somers * telnet or web servers will be prevented by the aliasing mechanism. 15971593f95SBrian Somers */ 1603efa11bbSBrian Somers #define PKT_ALIAS_DENY_INCOMING 0x02 1613b160b8bSBrian Somers 16271593f95SBrian Somers /* 16371593f95SBrian Somers * If PKT_ALIAS_SAME_PORTS is set, packets will be attempted sent from the 16471593f95SBrian Somers * same port as they originated on. This allows e.g. rsh to work *99% of the 16571593f95SBrian Somers * time*, but _not_ 100% (it will be slightly flakey instead of not working 16671593f95SBrian Somers * at all). This mode bit is set by PacketAliasInit(), so it is a default 16771593f95SBrian Somers * mode of operation. 16871593f95SBrian Somers */ 1693efa11bbSBrian Somers #define PKT_ALIAS_SAME_PORTS 0x04 1703b160b8bSBrian Somers 17171593f95SBrian Somers /* 17271593f95SBrian Somers * If PKT_ALIAS_USE_SOCKETS is set, then when partially specified links (e.g. 17371593f95SBrian Somers * destination port and/or address is zero), the packet aliasing engine will 17471593f95SBrian Somers * attempt to allocate a socket for the aliasing port it chooses. This will 17571593f95SBrian Somers * avoid interference with the host machine. Fully specified links do not 17671593f95SBrian Somers * require this. This bit is set after a call to PacketAliasInit(), so it is 17771593f95SBrian Somers * a default mode of operation. 17871593f95SBrian Somers */ 17900fc9a5bSGleb Smirnoff #ifndef NO_USE_SOCKETS 1803efa11bbSBrian Somers #define PKT_ALIAS_USE_SOCKETS 0x08 18100fc9a5bSGleb Smirnoff #endif 18271593f95SBrian Somers /*- 18371593f95SBrian Somers * If PKT_ALIAS_UNREGISTERED_ONLY is set, then only packets with 18471593f95SBrian Somers * unregistered source addresses will be aliased. Private 18571593f95SBrian Somers * addresses are those in the following ranges: 18671593f95SBrian Somers * 18771593f95SBrian Somers * 10.0.0.0 -> 10.255.255.255 18871593f95SBrian Somers * 172.16.0.0 -> 172.31.255.255 18971593f95SBrian Somers * 192.168.0.0 -> 192.168.255.255 19071593f95SBrian Somers */ 1913efa11bbSBrian Somers #define PKT_ALIAS_UNREGISTERED_ONLY 0x10 1923b160b8bSBrian Somers 19371593f95SBrian Somers /* 19471593f95SBrian Somers * If PKT_ALIAS_RESET_ON_ADDR_CHANGE is set, then the table of dynamic 19571593f95SBrian Somers * aliasing links will be reset whenever PacketAliasSetAddress() changes the 19671593f95SBrian Somers * default aliasing address. If the default aliasing address is left 19771593f95SBrian Somers * unchanged by this function call, then the table of dynamic aliasing links 19871593f95SBrian Somers * will be left intact. This bit is set after a call to PacketAliasInit(). 19971593f95SBrian Somers */ 2003efa11bbSBrian Somers #define PKT_ALIAS_RESET_ON_ADDR_CHANGE 0x20 2013b160b8bSBrian Somers 20271593f95SBrian Somers /* 20371593f95SBrian Somers * If PKT_ALIAS_PROXY_ONLY is set, then NAT will be disabled and only 20471593f95SBrian Somers * transparent proxying is performed. 20571593f95SBrian Somers */ 2067d96f4efSBrian Somers #define PKT_ALIAS_PROXY_ONLY 0x40 2077d96f4efSBrian Somers 20871593f95SBrian Somers /* 20971593f95SBrian Somers * If PKT_ALIAS_REVERSE is set, the actions of PacketAliasIn() and 21071593f95SBrian Somers * PacketAliasOut() are reversed. 21171593f95SBrian Somers */ 2127d96f4efSBrian Somers #define PKT_ALIAS_REVERSE 0x80 2137d96f4efSBrian Somers 21481a65464SAndrey V. Elsukov #ifndef NO_FW_PUNCH 21581a65464SAndrey V. Elsukov /* 21681a65464SAndrey V. Elsukov * If PKT_ALIAS_PUNCH_FW is set, active FTP and IRC DCC connections will 21781a65464SAndrey V. Elsukov * create a 'hole' in the firewall to allow the transfers to work. The 21881a65464SAndrey V. Elsukov * ipfw rule number that the hole is created with is controlled by 21981a65464SAndrey V. Elsukov * PacketAliasSetFWBase(). The hole will be attached to that 22081a65464SAndrey V. Elsukov * particular alias_link, so when the link goes away the hole is deleted. 22181a65464SAndrey V. Elsukov */ 22281a65464SAndrey V. Elsukov #define PKT_ALIAS_PUNCH_FW 0x100 22381a65464SAndrey V. Elsukov #endif 22481a65464SAndrey V. Elsukov 2251875bbfeSAndrey V. Elsukov /* 2261875bbfeSAndrey V. Elsukov * If PKT_ALIAS_SKIP_GLOBAL is set, nat instance is not checked for matching 2271875bbfeSAndrey V. Elsukov * states in 'ipfw nat global' rule. 2281875bbfeSAndrey V. Elsukov */ 2291875bbfeSAndrey V. Elsukov #define PKT_ALIAS_SKIP_GLOBAL 0x200 2301875bbfeSAndrey V. Elsukov 23175b89337SAlexander V. Chernikov /* 23275b89337SAlexander V. Chernikov * Like PKT_ALIAS_UNREGISTERED_ONLY, but includes the RFC 6598 23375b89337SAlexander V. Chernikov * (Carrier Grade NAT) address range as follows: 23475b89337SAlexander V. Chernikov * 23575b89337SAlexander V. Chernikov * 100.64.0.0 -> 100.127.255.255 23675b89337SAlexander V. Chernikov */ 23775b89337SAlexander V. Chernikov #define PKT_ALIAS_UNREGISTERED_CGN 0x400 23875b89337SAlexander V. Chernikov 23971593f95SBrian Somers /* Function return codes. */ 2403b160b8bSBrian Somers #define PKT_ALIAS_ERROR -1 2413b160b8bSBrian Somers #define PKT_ALIAS_OK 1 2423b160b8bSBrian Somers #define PKT_ALIAS_IGNORED 2 2433b160b8bSBrian Somers #define PKT_ALIAS_UNRESOLVED_FRAGMENT 3 2443b160b8bSBrian Somers #define PKT_ALIAS_FOUND_HEADER_FRAGMENT 4 2453b160b8bSBrian Somers 24671593f95SBrian Somers #endif /* !_ALIAS_H_ */ 24771593f95SBrian Somers 2483b160b8bSBrian Somers /* lint -restore */ 249