blob: 42e0d884180210efda552ff30eb4f6256e9d1597 [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welte9ba50052010-03-14 15:45:01 +08002
Harald Welte8f9a3ee2010-05-02 11:26:34 +02003#include <stdint.h>
4
Harald Welte3771d092010-04-30 20:26:32 +02005/* Our Implementation */
6#include <netinet/in.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +01007#include <osmocom/core/linuxlist.h>
8#include <osmocom/core/msgb.h>
9#include <osmocom/core/timer.h>
10#include <osmocom/core/select.h>
Harald Welte605ac5d2012-06-16 16:09:52 +080011#include <osmocom/gprs/gprs_msgb.h>
Harald Welte3771d092010-04-30 20:26:32 +020012
Harald Welte8648e492012-06-17 13:12:51 +080013#include <osmocom/gprs/protocol/gsm_08_16.h>
14
Harald Weltefe4ab902010-05-12 17:19:53 +000015#define NS_TIMERS_COUNT 7
16#define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries)"
17#define NS_TIMERS_HELP \
18 "(un)blocking Timer (Tns-block) timeout\n" \
19 "(un)blocking Timer (Tns-block) number of retries\n" \
20 "Reset Timer (Tns-reset) timeout\n" \
21 "Reset Timer (Tns-reset) number of retries\n" \
22 "Test Timer (Tns-test) timeout\n" \
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +010023 "Alive Timer (Tns-alive) timeout\n" \
24 "Alive Timer (Tns-alive) number of retries\n"
Harald Weltefe4ab902010-05-12 17:19:53 +000025
Jacob Erlbeck8d192d72015-04-07 17:52:45 +020026#define NS_ALLOC_SIZE 2048
27#define NS_ALLOC_HEADROOM 20
28
Harald Weltefe4ab902010-05-12 17:19:53 +000029enum ns_timeout {
30 NS_TOUT_TNS_BLOCK,
31 NS_TOUT_TNS_BLOCK_RETRIES,
32 NS_TOUT_TNS_RESET,
33 NS_TOUT_TNS_RESET_RETRIES,
34 NS_TOUT_TNS_TEST,
35 NS_TOUT_TNS_ALIVE,
36 NS_TOUT_TNS_ALIVE_RETRIES,
37};
38
Harald Welte3771d092010-04-30 20:26:32 +020039#define NSE_S_BLOCKED 0x0001
40#define NSE_S_ALIVE 0x0002
Jacob Erlbeck5405a102013-10-24 01:33:23 +020041#define NSE_S_RESET 0x0004
Harald Welte3771d092010-04-30 20:26:32 +020042
Harald Weltea9f23c82011-11-23 15:01:31 +010043/*! \brief Osmocom NS link layer types */
Harald Welte1203de32010-05-01 11:28:43 +020044enum gprs_ns_ll {
Harald Weltea9f23c82011-11-23 15:01:31 +010045 GPRS_NS_LL_UDP, /*!< NS/UDP/IP */
46 GPRS_NS_LL_E1, /*!< NS/E1 */
47 GPRS_NS_LL_FR_GRE, /*!< NS/FR/GRE/IP */
Harald Welte1203de32010-05-01 11:28:43 +020048};
49
Harald Weltea9f23c82011-11-23 15:01:31 +010050/*! \brief Osmoco NS events */
Harald Welte1203de32010-05-01 11:28:43 +020051enum gprs_ns_evt {
52 GPRS_NS_EVT_UNIT_DATA,
53};
54
Jacob Erlbeck84cdc702013-10-08 12:04:44 +020055/*! \brief Osmocom NS VC create status */
56enum gprs_ns_cs {
57 GPRS_NS_CS_CREATED, /*!< A NSVC object has been created */
58 GPRS_NS_CS_FOUND, /*!< A NSVC object has been found */
59 GPRS_NS_CS_REJECTED, /*!< Rejected and answered message */
60 GPRS_NS_CS_SKIPPED, /*!< Skipped message */
61 GPRS_NS_CS_ERROR, /*!< Failed to process message */
62};
63
Harald Welte1203de32010-05-01 11:28:43 +020064struct gprs_nsvc;
Harald Weltea9f23c82011-11-23 15:01:31 +010065/*! \brief Osmocom GPRS callback function type */
Harald Welte1203de32010-05-01 11:28:43 +020066typedef int gprs_ns_cb_t(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020067 struct msgb *msg, uint16_t bvci);
Harald Welte1203de32010-05-01 11:28:43 +020068
Harald Weltea9f23c82011-11-23 15:01:31 +010069/*! \brief An instance of the NS protocol stack */
Harald Welte1203de32010-05-01 11:28:43 +020070struct gprs_ns_inst {
Harald Weltea9f23c82011-11-23 15:01:31 +010071 /*! \brief callback to the user for incoming UNIT DATA IND */
Harald Welte1203de32010-05-01 11:28:43 +020072 gprs_ns_cb_t *cb;
73
Harald Weltea9f23c82011-11-23 15:01:31 +010074 /*! \brief linked lists of all NSVC in this instance */
Harald Welte1203de32010-05-01 11:28:43 +020075 struct llist_head gprs_nsvcs;
76
Harald Weltea9f23c82011-11-23 15:01:31 +010077 /*! \brief a NSVC object that's needed to deal with packets for
78 * unknown NSVC */
Harald Weltedd1c83c2010-05-13 13:58:08 +020079 struct gprs_nsvc *unknown_nsvc;
80
Harald Weltefe4ab902010-05-12 17:19:53 +000081 uint16_t timeout[NS_TIMERS_COUNT];
82
Harald Weltea9f23c82011-11-23 15:01:31 +010083 /*! \brief NS-over-IP specific bits */
Harald Welteb3ee2652010-05-19 14:38:50 +020084 struct {
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +020085 struct osmo_fd fd;
Harald Welte7fb05232010-05-19 15:09:09 +020086 uint32_t local_ip;
87 uint16_t local_port;
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +010088 int dscp;
Harald Welteb3ee2652010-05-19 14:38:50 +020089 } nsip;
Harald Weltea9f23c82011-11-23 15:01:31 +010090 /*! \brief NS-over-FR-over-GRE-over-IP specific bits */
Harald Welteb3ee2652010-05-19 14:38:50 +020091 struct {
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +020092 struct osmo_fd fd;
Harald Welte7fb05232010-05-19 15:09:09 +020093 uint32_t local_ip;
Holger Hans Peter Freyther1e57eb62012-03-02 14:14:33 +010094 unsigned int enabled:1;
Harald Welteb3ee2652010-05-19 14:38:50 +020095 } frgre;
Harald Welte1203de32010-05-01 11:28:43 +020096};
97
Harald Welte80405452010-05-03 20:16:13 +020098enum nsvc_timer_mode {
99 /* standard timers */
100 NSVC_TIMER_TNS_TEST,
101 NSVC_TIMER_TNS_ALIVE,
Harald Welte69a4cf22010-05-03 20:55:10 +0200102 NSVC_TIMER_TNS_RESET,
103 _NSVC_TIMER_NR,
Harald Welte80405452010-05-03 20:16:13 +0200104};
105
Harald Weltea9f23c82011-11-23 15:01:31 +0100106/*! \brief Structure representing a single NS-VC */
Harald Welte3771d092010-04-30 20:26:32 +0200107struct gprs_nsvc {
Harald Weltea9f23c82011-11-23 15:01:31 +0100108 /*! \brief list of NS-VCs within NS Instance */
Harald Welte3771d092010-04-30 20:26:32 +0200109 struct llist_head list;
Harald Weltea9f23c82011-11-23 15:01:31 +0100110 /*! \brief pointer to NS Instance */
Harald Welte3771d092010-04-30 20:26:32 +0200111 struct gprs_ns_inst *nsi;
112
Harald Weltea9f23c82011-11-23 15:01:31 +0100113 uint16_t nsei; /*! \brief end-to-end significance */
114 uint16_t nsvci; /*! \brief uniquely identifies NS-VC at SGSN */
Harald Welte3771d092010-04-30 20:26:32 +0200115
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200116 uint32_t state;
117 uint32_t remote_state;
Harald Welte3771d092010-04-30 20:26:32 +0200118
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200119 struct osmo_timer_list timer;
Harald Welte80405452010-05-03 20:16:13 +0200120 enum nsvc_timer_mode timer_mode;
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200121 struct timeval timer_started;
Harald Welte3771d092010-04-30 20:26:32 +0200122 int alive_retries;
123
Harald Welte2bffac52010-05-12 15:55:23 +0000124 unsigned int remote_end_is_sgsn:1;
125 unsigned int persistent:1;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200126 unsigned int nsvci_is_valid:1;
Harald Welte3771d092010-04-30 20:26:32 +0200127
Harald Welte144e0292010-05-13 11:45:07 +0200128 struct rate_ctr_group *ctrg;
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200129 struct stat_item_group *statg;
Harald Welte144e0292010-05-13 11:45:07 +0200130
Harald Weltea9f23c82011-11-23 15:01:31 +0100131 /*! \brief which link-layer are we based on? */
Harald Welteb3ee2652010-05-19 14:38:50 +0200132 enum gprs_ns_ll ll;
133
Harald Welte3771d092010-04-30 20:26:32 +0200134 union {
135 struct {
136 struct sockaddr_in bts_addr;
137 } ip;
Harald Welteb3ee2652010-05-19 14:38:50 +0200138 struct {
139 struct sockaddr_in bts_addr;
140 } frgre;
Harald Welte3771d092010-04-30 20:26:32 +0200141 };
142};
143
Harald Weltef030b212010-04-26 19:18:54 +0200144/* Create a new NS protocol instance */
Harald Welte4fcdd762012-06-16 16:40:42 +0800145struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800146
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +0200147/* Close a NS protocol instance */
148void gprs_ns_close(struct gprs_ns_inst *nsi);
149
150/* Close and Destroy a NS protocol instance */
Harald Weltef030b212010-04-26 19:18:54 +0200151void gprs_ns_destroy(struct gprs_ns_inst *nsi);
152
Harald Welteb3ee2652010-05-19 14:38:50 +0200153/* Listen for incoming GPRS packets via NS/UDP */
Harald Welte7fb05232010-05-19 15:09:09 +0200154int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi);
Harald Weltef030b212010-04-26 19:18:54 +0200155
Harald Weltef5430362012-06-17 12:25:53 +0800156/* Establish a connection (from the BSS) to the SGSN */
157struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
158 struct sockaddr_in *dest,
159 uint16_t nsei, uint16_t nsvci);
160
161
Harald Weltef030b212010-04-26 19:18:54 +0200162struct sockaddr_in;
163
Harald Weltef030b212010-04-26 19:18:54 +0200164/* main function for higher layers (BSSGP) to send NS messages */
Harald Welte24a655f2010-04-30 19:54:29 +0200165int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200166
Harald Welte834f26d2010-05-11 06:20:54 +0200167int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause);
168int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause);
169int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc);
Harald Welte3771d092010-04-30 20:26:32 +0200170
Harald Welteb3ee2652010-05-19 14:38:50 +0200171/* Listen for incoming GPRS packets via NS/FR/GRE */
Harald Welte7fb05232010-05-19 15:09:09 +0200172int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi);
Harald Welte3771d092010-04-30 20:26:32 +0200173
Harald Weltef5430362012-06-17 12:25:53 +0800174struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci);
175void gprs_nsvc_delete(struct gprs_nsvc *nsvc);
176struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei);
177struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci);
Harald Welte144e0292010-05-13 11:45:07 +0200178
Harald Welte731d1fc2010-05-14 11:53:08 +0000179/* Initiate a RESET procedure (including timer start, ...)*/
Jacob Erlbeck0540d832014-10-08 11:47:36 +0200180int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause);
Harald Welte731d1fc2010-05-14 11:53:08 +0000181
Harald Welte2bffac52010-05-12 15:55:23 +0000182/* Add NS-specific VTY stuff */
183int gprs_ns_vty_init(struct gprs_ns_inst *nsi);
184
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200185/* Resturn peer info as string (NOTE: the buffer is allocated statically) */
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200186const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc);
187
188/* Copy the link layer info from other into nsvc */
189void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other);
190
191/* Clear the link layer info (will never match a real link then) */
192void gprs_ns_ll_clear(struct gprs_nsvc *nsvc);
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200193
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200194struct msgb *gprs_ns_msgb_alloc(void);
Harald Welteb3ee2652010-05-19 14:38:50 +0200195
Harald Welte4fcdd762012-06-16 16:40:42 +0800196enum signal_ns {
197 S_NS_RESET,
198 S_NS_BLOCK,
199 S_NS_UNBLOCK,
200 S_NS_ALIVE_EXP, /* Tns-alive expired more than N times */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200201 S_NS_REPLACED, /* nsvc object is replaced (sets old_nsvc) */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200202 S_NS_MISMATCH, /* got an unexpected IE (sets msg, pdu_type, ie_type) */
Harald Welte4fcdd762012-06-16 16:40:42 +0800203};
204
205struct ns_signal_data {
206 struct gprs_nsvc *nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200207 struct gprs_nsvc *old_nsvc;
Harald Welte4fcdd762012-06-16 16:40:42 +0800208 uint8_t cause;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200209 uint8_t pdu_type;
210 uint8_t ie_type;
211 struct msgb *msg;
Harald Welte4fcdd762012-06-16 16:40:42 +0800212};
213
Harald Weltecca49632012-06-16 17:45:59 +0800214void gprs_ns_set_log_ss(int ss);
215
Harald Weltea9f23c82011-11-23 15:01:31 +0100216/*! }@ */