xref: /freebsd/sys/netinet/libalias/alias_sctp.h (revision 71625ec9ad2a9bc8c09784fbd23b759830e0ee5f)
1619ad9ebSLawrence Stewart /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3fe267a55SPedro F. Giffuni  *
4619ad9ebSLawrence Stewart  * Copyright (c) 2008
567f285a2SLawrence Stewart  * 	Swinburne University of Technology, Melbourne, Australia.
637ce2656SPaolo Pisati  *
737ce2656SPaolo Pisati  *  Redistribution and use in source and binary forms, with or without
837ce2656SPaolo Pisati  *  modification, are permitted provided that the following conditions
937ce2656SPaolo Pisati  *  are met:
1037ce2656SPaolo Pisati  *  1. Redistributions of source code must retain the above copyright
1137ce2656SPaolo Pisati  *     notice, this list of conditions and the following disclaimer.
1237ce2656SPaolo Pisati  *  2. Redistributions in binary form must reproduce the above copyright
1337ce2656SPaolo Pisati  *     notice, this list of conditions and the following disclaimer in the
1437ce2656SPaolo Pisati  *     documentation and/or other materials provided with the distribution.
1537ce2656SPaolo Pisati  *
1637ce2656SPaolo Pisati  *  THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS "AS IS" AND
1737ce2656SPaolo Pisati  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1837ce2656SPaolo Pisati  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1937ce2656SPaolo Pisati  *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
2037ce2656SPaolo Pisati  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2137ce2656SPaolo Pisati  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2237ce2656SPaolo Pisati  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2337ce2656SPaolo Pisati  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2437ce2656SPaolo Pisati  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2537ce2656SPaolo Pisati  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2637ce2656SPaolo Pisati  *  SUCH DAMAGE.
27619ad9ebSLawrence Stewart  */
28619ad9ebSLawrence Stewart 
29619ad9ebSLawrence Stewart /*
3037ce2656SPaolo Pisati  * Alias_sctp forms part of the libalias kernel module to handle
3137ce2656SPaolo Pisati  * Network Address Translation (NAT) for the SCTP protocol.
3237ce2656SPaolo Pisati  *
3337ce2656SPaolo Pisati  *  This software was developed by David A. Hayes
3437ce2656SPaolo Pisati  *  with leadership and advice from Jason But
3537ce2656SPaolo Pisati  *
3637ce2656SPaolo Pisati  * The design is outlined in CAIA technical report number  080618A
3737ce2656SPaolo Pisati  * (D. Hayes and J. But, "Alias_sctp Version 0.1: SCTP NAT implementation in IPFW")
3837ce2656SPaolo Pisati  *
3937ce2656SPaolo Pisati  * Development is part of the CAIA SONATA project,
4037ce2656SPaolo Pisati  * proposed by Jason But and Grenville Armitage:
4137ce2656SPaolo Pisati  * http://caia.swin.edu.au/urp/sonata/
4237ce2656SPaolo Pisati  *
4337ce2656SPaolo Pisati  *
4437ce2656SPaolo Pisati  * This project has been made possible in part by a grant from
4537ce2656SPaolo Pisati  * the Cisco University Research Program Fund at Community
4637ce2656SPaolo Pisati  * Foundation Silicon Valley.
4737ce2656SPaolo Pisati  *
4837ce2656SPaolo Pisati  */
4937ce2656SPaolo Pisati 
5037ce2656SPaolo Pisati 
5137ce2656SPaolo Pisati #ifndef _ALIAS_SCTP_H_
5237ce2656SPaolo Pisati #define _ALIAS_SCTP_H_
5337ce2656SPaolo Pisati 
5437ce2656SPaolo Pisati #include <sys/param.h>
5537ce2656SPaolo Pisati #ifdef _KERNEL
5637ce2656SPaolo Pisati #include <sys/malloc.h>
5737ce2656SPaolo Pisati #include <sys/module.h>
5837ce2656SPaolo Pisati #include <sys/kernel.h>
5937ce2656SPaolo Pisati #include <sys/proc.h>
6037ce2656SPaolo Pisati #include <sys/uio.h>
6137ce2656SPaolo Pisati #include <sys/socketvar.h>
6237ce2656SPaolo Pisati #include <sys/syslog.h>
6337ce2656SPaolo Pisati #endif // #ifdef	_KERNEL
6437ce2656SPaolo Pisati #include <sys/types.h>
6537ce2656SPaolo Pisati 
6637ce2656SPaolo Pisati #include <sys/queue.h>
6737ce2656SPaolo Pisati #include <sys/types.h>
6837ce2656SPaolo Pisati #include <sys/time.h>
6937ce2656SPaolo Pisati 
7037ce2656SPaolo Pisati #include <netinet/in_systm.h>
7137ce2656SPaolo Pisati #include <netinet/in.h>
7237ce2656SPaolo Pisati #include <netinet/ip.h>
7337ce2656SPaolo Pisati 
7437ce2656SPaolo Pisati /**
7537ce2656SPaolo Pisati  * These are defined in sctp_os_bsd.h, but it can't be included due to its local file
7637ce2656SPaolo Pisati  * inclusion, so I'm defining them here.
7737ce2656SPaolo Pisati  *
7837ce2656SPaolo Pisati  */
7937ce2656SPaolo Pisati #include <machine/cpufunc.h>
8037ce2656SPaolo Pisati /* The packed define for 64 bit platforms */
8137ce2656SPaolo Pisati #ifndef SCTP_PACKED
8237ce2656SPaolo Pisati #define SCTP_PACKED __attribute__((packed))
8337ce2656SPaolo Pisati #endif //#ifndef SCTP_PACKED
8437ce2656SPaolo Pisati #ifndef SCTP_UNUSED
8537ce2656SPaolo Pisati #define SCTP_UNUSED __attribute__((unused))
8637ce2656SPaolo Pisati #endif //#ifndef SCTP_UNUSED
8737ce2656SPaolo Pisati 
8837ce2656SPaolo Pisati #include <netinet/sctp.h>
8937ce2656SPaolo Pisati //#include <netinet/sctp_os_bsd.h> --might be needed later for mbuf stuff
9037ce2656SPaolo Pisati #include <netinet/sctp_header.h>
9137ce2656SPaolo Pisati 
9237ce2656SPaolo Pisati #ifndef _KERNEL
9337ce2656SPaolo Pisati #include <stdlib.h>
9437ce2656SPaolo Pisati #include <stdio.h>
9537ce2656SPaolo Pisati #endif //#ifdef _KERNEL
9637ce2656SPaolo Pisati 
9737ce2656SPaolo Pisati #define LINK_SCTP                      IPPROTO_SCTP
9837ce2656SPaolo Pisati 
9937ce2656SPaolo Pisati #define SN_TO_LOCAL              0   /**< packet traveling from global to local */
10037ce2656SPaolo Pisati #define SN_TO_GLOBAL             1   /**< packet traveling from local to global */
10137ce2656SPaolo Pisati #define SN_TO_NODIR             99   /**< used where direction is not important */
10237ce2656SPaolo Pisati 
10337ce2656SPaolo Pisati #define SN_NAT_PKT          0x0000   /**< Network Address Translate packet */
10437ce2656SPaolo Pisati #define SN_DROP_PKT         0x0001   /**< drop packet (don't forward it) */
10537ce2656SPaolo Pisati #define SN_PROCESSING_ERROR 0x0003   /**< Packet processing error */
10637ce2656SPaolo Pisati #define SN_REPLY_ABORT      0x0010   /**< Reply with ABORT to sender (don't forward it) */
10737ce2656SPaolo Pisati #define SN_SEND_ABORT       0x0020   /**< Send ABORT to destination */
10837ce2656SPaolo Pisati #define SN_TX_ABORT         0x0030   /**< mask for transmitting abort */
10937ce2656SPaolo Pisati #define SN_REFLECT_ERROR    0x0100   /**< Reply with ERROR to sender on OOTB packet Tbit set */
11037ce2656SPaolo Pisati #define SN_REPLY_ERROR      0x0200   /**< Reply with ERROR to sender on ASCONF clash */
11137ce2656SPaolo Pisati #define SN_TX_ERROR         0x0300   /**< mask for transmitting error */
11237ce2656SPaolo Pisati 
11337ce2656SPaolo Pisati #define PKT_ALIAS_RESPOND   0x1000   /**< Signal to libalias that there is a response packet to send */
11437ce2656SPaolo Pisati /*
11537ce2656SPaolo Pisati  * Data structures
11637ce2656SPaolo Pisati  */
11737ce2656SPaolo Pisati 
11837ce2656SPaolo Pisati /**
11937ce2656SPaolo Pisati  * @brief sctp association information
12037ce2656SPaolo Pisati  *
12137ce2656SPaolo Pisati  * Structure that contains information about a particular sctp association
12237ce2656SPaolo Pisati  * currently under Network Address Translation.
12337ce2656SPaolo Pisati  * Information is stored in network byte order (as is libalias)***
12437ce2656SPaolo Pisati  */
12537ce2656SPaolo Pisati struct sctp_nat_assoc {
12637ce2656SPaolo Pisati 	uint32_t l_vtag;		/**< local side verification tag */
12737ce2656SPaolo Pisati 	uint16_t l_port;		/**< local side port number */
12837ce2656SPaolo Pisati 	uint32_t g_vtag;		/**< global side verification tag */
12937ce2656SPaolo Pisati 	uint16_t g_port;		/**< global side port number */
13037ce2656SPaolo Pisati 	struct in_addr l_addr;		/**< local ip address */
13137ce2656SPaolo Pisati 	struct in_addr a_addr;		/**< alias ip address */
13237ce2656SPaolo Pisati 	int state;			/**< current state of NAT association */
13337ce2656SPaolo Pisati 	int TableRegister;		/**< stores which look up tables association is registered in */
13437ce2656SPaolo Pisati 	int exp;			/**< timer expiration in seconds from uptime */
13537ce2656SPaolo Pisati 	int exp_loc;			/**< current location in timer_Q */
13637ce2656SPaolo Pisati 	int num_Gaddr;			/**< number of global IP addresses in the list */
13737ce2656SPaolo Pisati 	LIST_HEAD(sctpGlobalAddresshead,sctp_GlobalAddress) Gaddr; /**< List of global addresses */
13837ce2656SPaolo Pisati 	LIST_ENTRY (sctp_nat_assoc) list_L; /**< Linked list of pointers for Local table*/
13937ce2656SPaolo Pisati 	LIST_ENTRY (sctp_nat_assoc) list_G; /**< Linked list of pointers for Global table */
14037ce2656SPaolo Pisati 	LIST_ENTRY (sctp_nat_assoc) timer_Q; /**< Linked list of pointers for timer Q */
14137ce2656SPaolo Pisati 	//Using libalias locking
14237ce2656SPaolo Pisati };
14337ce2656SPaolo Pisati 
14437ce2656SPaolo Pisati struct sctp_GlobalAddress {
14537ce2656SPaolo Pisati 	struct in_addr g_addr;
14637ce2656SPaolo Pisati 	LIST_ENTRY (sctp_GlobalAddress) list_Gaddr; /**< Linked list of pointers for Global table */
14737ce2656SPaolo Pisati };
14837ce2656SPaolo Pisati 
14937ce2656SPaolo Pisati /**
15037ce2656SPaolo Pisati  * @brief SCTP chunk of interest
15137ce2656SPaolo Pisati  *
15237ce2656SPaolo Pisati  * The only chunks whose contents are of any interest are the INIT and ASCONF_AddIP
15337ce2656SPaolo Pisati  */
15437ce2656SPaolo Pisati union sctpChunkOfInt {
15537ce2656SPaolo Pisati 	struct sctp_init *Init;		/**< Pointer to Init Chunk */
15637ce2656SPaolo Pisati 	struct sctp_init_ack *InitAck;	/**< Pointer to Init Chunk */
15737ce2656SPaolo Pisati 	struct sctp_paramhdr *Asconf;	/**< Pointer to ASCONF chunk */
15837ce2656SPaolo Pisati };
15937ce2656SPaolo Pisati 
16037ce2656SPaolo Pisati /**
16137ce2656SPaolo Pisati  * @brief SCTP message
16237ce2656SPaolo Pisati  *
16337ce2656SPaolo Pisati  * Structure containing the relevant information from the SCTP message
16437ce2656SPaolo Pisati  */
16537ce2656SPaolo Pisati struct sctp_nat_msg {
16637ce2656SPaolo Pisati 	uint16_t msg;			/**< one of the key messages defined above */
16737ce2656SPaolo Pisati #ifdef INET6
16837ce2656SPaolo Pisati 	//  struct ip6_hdr *ip_hdr;	/**< pointer to ip packet header */ /*no inet6 support yet*/
16937ce2656SPaolo Pisati #else
17037ce2656SPaolo Pisati 	struct ip *ip_hdr;		/**< pointer to ip packet header */
17137ce2656SPaolo Pisati #endif //#ifdef INET6
17237ce2656SPaolo Pisati 	struct sctphdr *sctp_hdr;	/**< pointer to sctp common header */
17337ce2656SPaolo Pisati 	union sctpChunkOfInt sctpchnk; /**< union of pointers to the chunk of interest */
17437ce2656SPaolo Pisati 	int chunk_length;		/**< length of chunk of interest */
17537ce2656SPaolo Pisati };
17637ce2656SPaolo Pisati 
17737ce2656SPaolo Pisati /**
17837ce2656SPaolo Pisati  * @brief sctp nat timer queue structure
17937ce2656SPaolo Pisati  *
18037ce2656SPaolo Pisati  */
18137ce2656SPaolo Pisati 
18237ce2656SPaolo Pisati struct sctp_nat_timer {
18337ce2656SPaolo Pisati 	int loc_time;			/**< time in seconds for the current location in the queue */
18437ce2656SPaolo Pisati 	int cur_loc;			/**< index of the current location in the circular queue */
18537ce2656SPaolo Pisati 	LIST_HEAD(sctpTimerQ,sctp_nat_assoc) *TimerQ; /**< List of associations at this position in the timer Q */
18637ce2656SPaolo Pisati };
18737ce2656SPaolo Pisati 
18837ce2656SPaolo Pisati #endif //#ifndef _ALIAS_SCTP_H
189