blob: d16068be249783b382263de41080ec1c24b95196 [file] [log] [blame]
Harald Welte9ba50052010-03-14 15:45:01 +08001#ifndef _GPRS_NS_H
2#define _GPRS_NS_H
3
Harald Welte8f9a3ee2010-05-02 11:26:34 +02004#include <stdint.h>
5
Harald Welte3771d092010-04-30 20:26:32 +02006/* Our Implementation */
7#include <netinet/in.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +01008#include <osmocom/core/linuxlist.h>
9#include <osmocom/core/msgb.h>
10#include <osmocom/core/timer.h>
11#include <osmocom/core/select.h>
Harald Welte605ac5d2012-06-16 16:09:52 +080012#include <osmocom/gprs/gprs_msgb.h>
Harald Welte3771d092010-04-30 20:26:32 +020013
Harald Welte8648e492012-06-17 13:12:51 +080014#include <osmocom/gprs/protocol/gsm_08_16.h>
15
Harald Weltefe4ab902010-05-12 17:19:53 +000016#define NS_TIMERS_COUNT 7
17#define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries)"
18#define NS_TIMERS_HELP \
19 "(un)blocking Timer (Tns-block) timeout\n" \
20 "(un)blocking Timer (Tns-block) number of retries\n" \
21 "Reset Timer (Tns-reset) timeout\n" \
22 "Reset Timer (Tns-reset) number of retries\n" \
23 "Test Timer (Tns-test) timeout\n" \
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +010024 "Alive Timer (Tns-alive) timeout\n" \
25 "Alive Timer (Tns-alive) number of retries\n"
Harald Weltefe4ab902010-05-12 17:19:53 +000026
27enum ns_timeout {
28 NS_TOUT_TNS_BLOCK,
29 NS_TOUT_TNS_BLOCK_RETRIES,
30 NS_TOUT_TNS_RESET,
31 NS_TOUT_TNS_RESET_RETRIES,
32 NS_TOUT_TNS_TEST,
33 NS_TOUT_TNS_ALIVE,
34 NS_TOUT_TNS_ALIVE_RETRIES,
35};
36
Harald Welte3771d092010-04-30 20:26:32 +020037#define NSE_S_BLOCKED 0x0001
38#define NSE_S_ALIVE 0x0002
39
Harald Weltea9f23c82011-11-23 15:01:31 +010040/*! \brief Osmocom NS link layer types */
Harald Welte1203de32010-05-01 11:28:43 +020041enum gprs_ns_ll {
Harald Weltea9f23c82011-11-23 15:01:31 +010042 GPRS_NS_LL_UDP, /*!< NS/UDP/IP */
43 GPRS_NS_LL_E1, /*!< NS/E1 */
44 GPRS_NS_LL_FR_GRE, /*!< NS/FR/GRE/IP */
Harald Welte1203de32010-05-01 11:28:43 +020045};
46
Harald Weltea9f23c82011-11-23 15:01:31 +010047/*! \brief Osmoco NS events */
Harald Welte1203de32010-05-01 11:28:43 +020048enum gprs_ns_evt {
49 GPRS_NS_EVT_UNIT_DATA,
50};
51
Jacob Erlbeck84cdc702013-10-08 12:04:44 +020052/*! \brief Osmocom NS VC create status */
53enum gprs_ns_cs {
54 GPRS_NS_CS_CREATED, /*!< A NSVC object has been created */
55 GPRS_NS_CS_FOUND, /*!< A NSVC object has been found */
56 GPRS_NS_CS_REJECTED, /*!< Rejected and answered message */
57 GPRS_NS_CS_SKIPPED, /*!< Skipped message */
58 GPRS_NS_CS_ERROR, /*!< Failed to process message */
59};
60
Harald Welte1203de32010-05-01 11:28:43 +020061struct gprs_nsvc;
Harald Weltea9f23c82011-11-23 15:01:31 +010062/*! \brief Osmocom GPRS callback function type */
Harald Welte1203de32010-05-01 11:28:43 +020063typedef int gprs_ns_cb_t(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020064 struct msgb *msg, uint16_t bvci);
Harald Welte1203de32010-05-01 11:28:43 +020065
Harald Weltea9f23c82011-11-23 15:01:31 +010066/*! \brief An instance of the NS protocol stack */
Harald Welte1203de32010-05-01 11:28:43 +020067struct gprs_ns_inst {
Harald Weltea9f23c82011-11-23 15:01:31 +010068 /*! \brief callback to the user for incoming UNIT DATA IND */
Harald Welte1203de32010-05-01 11:28:43 +020069 gprs_ns_cb_t *cb;
70
Harald Weltea9f23c82011-11-23 15:01:31 +010071 /*! \brief linked lists of all NSVC in this instance */
Harald Welte1203de32010-05-01 11:28:43 +020072 struct llist_head gprs_nsvcs;
73
Harald Weltea9f23c82011-11-23 15:01:31 +010074 /*! \brief a NSVC object that's needed to deal with packets for
75 * unknown NSVC */
Harald Weltedd1c83c2010-05-13 13:58:08 +020076 struct gprs_nsvc *unknown_nsvc;
77
Harald Weltefe4ab902010-05-12 17:19:53 +000078 uint16_t timeout[NS_TIMERS_COUNT];
79
Harald Weltea9f23c82011-11-23 15:01:31 +010080 /*! \brief NS-over-IP specific bits */
Harald Welteb3ee2652010-05-19 14:38:50 +020081 struct {
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +020082 struct osmo_fd fd;
Harald Welte7fb05232010-05-19 15:09:09 +020083 uint32_t local_ip;
84 uint16_t local_port;
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +010085 int dscp;
Harald Welteb3ee2652010-05-19 14:38:50 +020086 } nsip;
Harald Weltea9f23c82011-11-23 15:01:31 +010087 /*! \brief NS-over-FR-over-GRE-over-IP specific bits */
Harald Welteb3ee2652010-05-19 14:38:50 +020088 struct {
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +020089 struct osmo_fd fd;
Harald Welte7fb05232010-05-19 15:09:09 +020090 uint32_t local_ip;
Holger Hans Peter Freyther1e57eb62012-03-02 14:14:33 +010091 unsigned int enabled:1;
Harald Welteb3ee2652010-05-19 14:38:50 +020092 } frgre;
Harald Welte1203de32010-05-01 11:28:43 +020093};
94
Harald Welte80405452010-05-03 20:16:13 +020095enum nsvc_timer_mode {
96 /* standard timers */
97 NSVC_TIMER_TNS_TEST,
98 NSVC_TIMER_TNS_ALIVE,
Harald Welte69a4cf22010-05-03 20:55:10 +020099 NSVC_TIMER_TNS_RESET,
100 _NSVC_TIMER_NR,
Harald Welte80405452010-05-03 20:16:13 +0200101};
102
Harald Weltea9f23c82011-11-23 15:01:31 +0100103/*! \brief Structure representing a single NS-VC */
Harald Welte3771d092010-04-30 20:26:32 +0200104struct gprs_nsvc {
Harald Weltea9f23c82011-11-23 15:01:31 +0100105 /*! \brief list of NS-VCs within NS Instance */
Harald Welte3771d092010-04-30 20:26:32 +0200106 struct llist_head list;
Harald Weltea9f23c82011-11-23 15:01:31 +0100107 /*! \brief pointer to NS Instance */
Harald Welte3771d092010-04-30 20:26:32 +0200108 struct gprs_ns_inst *nsi;
109
Harald Weltea9f23c82011-11-23 15:01:31 +0100110 uint16_t nsei; /*! \brief end-to-end significance */
111 uint16_t nsvci; /*! \brief uniquely identifies NS-VC at SGSN */
Harald Welte3771d092010-04-30 20:26:32 +0200112
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200113 uint32_t state;
114 uint32_t remote_state;
Harald Welte3771d092010-04-30 20:26:32 +0200115
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200116 struct osmo_timer_list timer;
Harald Welte80405452010-05-03 20:16:13 +0200117 enum nsvc_timer_mode timer_mode;
Harald Welte3771d092010-04-30 20:26:32 +0200118 int alive_retries;
119
Harald Welte2bffac52010-05-12 15:55:23 +0000120 unsigned int remote_end_is_sgsn:1;
121 unsigned int persistent:1;
Harald Welte3771d092010-04-30 20:26:32 +0200122
Harald Welte144e0292010-05-13 11:45:07 +0200123 struct rate_ctr_group *ctrg;
124
Harald Weltea9f23c82011-11-23 15:01:31 +0100125 /*! \brief which link-layer are we based on? */
Harald Welteb3ee2652010-05-19 14:38:50 +0200126 enum gprs_ns_ll ll;
127
Harald Welte3771d092010-04-30 20:26:32 +0200128 union {
129 struct {
130 struct sockaddr_in bts_addr;
131 } ip;
Harald Welteb3ee2652010-05-19 14:38:50 +0200132 struct {
133 struct sockaddr_in bts_addr;
134 } frgre;
Harald Welte3771d092010-04-30 20:26:32 +0200135 };
136};
137
Harald Weltef030b212010-04-26 19:18:54 +0200138/* Create a new NS protocol instance */
Harald Welte4fcdd762012-06-16 16:40:42 +0800139struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800140
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +0200141/* Close a NS protocol instance */
142void gprs_ns_close(struct gprs_ns_inst *nsi);
143
144/* Close and Destroy a NS protocol instance */
Harald Weltef030b212010-04-26 19:18:54 +0200145void gprs_ns_destroy(struct gprs_ns_inst *nsi);
146
Harald Welteb3ee2652010-05-19 14:38:50 +0200147/* Listen for incoming GPRS packets via NS/UDP */
Harald Welte7fb05232010-05-19 15:09:09 +0200148int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi);
Harald Weltef030b212010-04-26 19:18:54 +0200149
Harald Weltef5430362012-06-17 12:25:53 +0800150/* Establish a connection (from the BSS) to the SGSN */
151struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
152 struct sockaddr_in *dest,
153 uint16_t nsei, uint16_t nsvci);
154
155
Harald Weltef030b212010-04-26 19:18:54 +0200156struct sockaddr_in;
157
Harald Weltef030b212010-04-26 19:18:54 +0200158/* main function for higher layers (BSSGP) to send NS messages */
Harald Welte24a655f2010-04-30 19:54:29 +0200159int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200160
Harald Welte834f26d2010-05-11 06:20:54 +0200161int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause);
162int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause);
163int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc);
Harald Welte3771d092010-04-30 20:26:32 +0200164
Harald Welteb3ee2652010-05-19 14:38:50 +0200165/* Listen for incoming GPRS packets via NS/FR/GRE */
Harald Welte7fb05232010-05-19 15:09:09 +0200166int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi);
Harald Welte3771d092010-04-30 20:26:32 +0200167
Harald Weltef5430362012-06-17 12:25:53 +0800168struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci);
169void gprs_nsvc_delete(struct gprs_nsvc *nsvc);
170struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei);
171struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci);
Harald Welte144e0292010-05-13 11:45:07 +0200172
Harald Welte731d1fc2010-05-14 11:53:08 +0000173/* Initiate a RESET procedure (including timer start, ...)*/
Holger Hans Peter Freyther5617d992010-05-23 21:18:01 +0800174void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause);
Harald Welte731d1fc2010-05-14 11:53:08 +0000175
Harald Welte2bffac52010-05-12 15:55:23 +0000176/* Add NS-specific VTY stuff */
177int gprs_ns_vty_init(struct gprs_ns_inst *nsi);
178
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200179/* Resturn peer info as string (NOTE: the buffer is allocated statically) */
180const char *gprs_ns_format_peer(struct gprs_nsvc *nsvc);
181
Holger Hans Peter Freyther4a90d222010-06-14 22:11:40 +0800182#define NS_ALLOC_SIZE 2048
Harald Welteba4c6662010-05-19 15:38:10 +0200183#define NS_ALLOC_HEADROOM 20
184static inline struct msgb *gprs_ns_msgb_alloc(void)
185{
186 return msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, "GPRS/NS");
187}
Harald Welteb3ee2652010-05-19 14:38:50 +0200188
Harald Welte4fcdd762012-06-16 16:40:42 +0800189enum signal_ns {
190 S_NS_RESET,
191 S_NS_BLOCK,
192 S_NS_UNBLOCK,
193 S_NS_ALIVE_EXP, /* Tns-alive expired more than N times */
194};
195
196struct ns_signal_data {
197 struct gprs_nsvc *nsvc;
198 uint8_t cause;
199};
200
Harald Weltecca49632012-06-16 17:45:59 +0800201void gprs_ns_set_log_ss(int ss);
202
Harald Weltea9f23c82011-11-23 15:01:31 +0100203/*! }@ */
204
Harald Welte9ba50052010-03-14 15:45:01 +0800205#endif