blob: af25825eedd68cb0cc7e3660f2cb48da73e28e03 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file gprs_ns.h */
2
Sylvain Munaut12ba7782014-06-16 10:13:40 +02003#pragma once
Harald Welte9ba50052010-03-14 15:45:01 +08004
Harald Welte8f9a3ee2010-05-02 11:26:34 +02005#include <stdint.h>
6
Harald Welte3771d092010-04-30 20:26:32 +02007/* Our Implementation */
8#include <netinet/in.h>
Pablo Neira Ayusoff663232011-03-22 16:47:59 +01009#include <osmocom/core/linuxlist.h>
10#include <osmocom/core/msgb.h>
11#include <osmocom/core/timer.h>
12#include <osmocom/core/select.h>
Harald Welte605ac5d2012-06-16 16:09:52 +080013#include <osmocom/gprs/gprs_msgb.h>
Harald Welte3771d092010-04-30 20:26:32 +020014
Harald Welte8648e492012-06-17 13:12:51 +080015#include <osmocom/gprs/protocol/gsm_08_16.h>
16
Harald Welte047f3872018-07-01 21:04:45 +020017#define NS_TIMERS_COUNT 8
18#define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries|tsns-prov)"
Harald Weltefe4ab902010-05-12 17:19:53 +000019#define NS_TIMERS_HELP \
20 "(un)blocking Timer (Tns-block) timeout\n" \
21 "(un)blocking Timer (Tns-block) number of retries\n" \
22 "Reset Timer (Tns-reset) timeout\n" \
23 "Reset Timer (Tns-reset) number of retries\n" \
24 "Test Timer (Tns-test) timeout\n" \
Holger Hans Peter Freyther887934e2011-11-05 15:14:59 +010025 "Alive Timer (Tns-alive) timeout\n" \
Harald Welte047f3872018-07-01 21:04:45 +020026 "Alive Timer (Tns-alive) number of retries\n" \
27 "SNS Provision Timer (Tsns-prov) timeout\n"
Harald Weltefe4ab902010-05-12 17:19:53 +000028
Daniel Willmannba34a2f2016-09-02 15:44:12 +020029/* Educated guess - LLC user payload is 1500 bytes plus possible headers */
30#define NS_ALLOC_SIZE 3072
Jacob Erlbeck8d192d72015-04-07 17:52:45 +020031#define NS_ALLOC_HEADROOM 20
32
Harald Weltefe4ab902010-05-12 17:19:53 +000033enum ns_timeout {
34 NS_TOUT_TNS_BLOCK,
35 NS_TOUT_TNS_BLOCK_RETRIES,
36 NS_TOUT_TNS_RESET,
37 NS_TOUT_TNS_RESET_RETRIES,
38 NS_TOUT_TNS_TEST,
39 NS_TOUT_TNS_ALIVE,
40 NS_TOUT_TNS_ALIVE_RETRIES,
Harald Welte047f3872018-07-01 21:04:45 +020041 NS_TOUT_TSNS_PROV,
Harald Weltefe4ab902010-05-12 17:19:53 +000042};
43
Harald Welte3771d092010-04-30 20:26:32 +020044#define NSE_S_BLOCKED 0x0001
45#define NSE_S_ALIVE 0x0002
Jacob Erlbeck5405a102013-10-24 01:33:23 +020046#define NSE_S_RESET 0x0004
Harald Welte3771d092010-04-30 20:26:32 +020047
Max4ce24c42017-10-23 15:09:23 +020048#define NS_DESC_B(st) ((st) & NSE_S_BLOCKED ? "BLOCKED" : "UNBLOCKED")
49#define NS_DESC_A(st) ((st) & NSE_S_ALIVE ? "ALIVE" : "DEAD")
50#define NS_DESC_R(st) ((st) & NSE_S_RESET ? "RESET" : "UNRESET")
51
Neels Hofmeyr87e45502017-06-20 00:17:59 +020052/*! Osmocom NS link layer types */
Harald Welte1203de32010-05-01 11:28:43 +020053enum gprs_ns_ll {
Harald Weltea9f23c82011-11-23 15:01:31 +010054 GPRS_NS_LL_UDP, /*!< NS/UDP/IP */
55 GPRS_NS_LL_E1, /*!< NS/E1 */
56 GPRS_NS_LL_FR_GRE, /*!< NS/FR/GRE/IP */
Harald Welte1203de32010-05-01 11:28:43 +020057};
58
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059/*! Osmoco NS events */
Harald Welte1203de32010-05-01 11:28:43 +020060enum gprs_ns_evt {
61 GPRS_NS_EVT_UNIT_DATA,
62};
63
Neels Hofmeyr87e45502017-06-20 00:17:59 +020064/*! Osmocom NS VC create status */
Jacob Erlbeck84cdc702013-10-08 12:04:44 +020065enum gprs_ns_cs {
66 GPRS_NS_CS_CREATED, /*!< A NSVC object has been created */
67 GPRS_NS_CS_FOUND, /*!< A NSVC object has been found */
68 GPRS_NS_CS_REJECTED, /*!< Rejected and answered message */
69 GPRS_NS_CS_SKIPPED, /*!< Skipped message */
70 GPRS_NS_CS_ERROR, /*!< Failed to process message */
71};
72
Harald Welte1203de32010-05-01 11:28:43 +020073struct gprs_nsvc;
Neels Hofmeyr87e45502017-06-20 00:17:59 +020074/*! Osmocom GPRS callback function type */
Harald Welte1203de32010-05-01 11:28:43 +020075typedef int gprs_ns_cb_t(enum gprs_ns_evt event, struct gprs_nsvc *nsvc,
Harald Welte8f9a3ee2010-05-02 11:26:34 +020076 struct msgb *msg, uint16_t bvci);
Harald Welte1203de32010-05-01 11:28:43 +020077
Neels Hofmeyr87e45502017-06-20 00:17:59 +020078/*! An instance of the NS protocol stack */
Harald Welte1203de32010-05-01 11:28:43 +020079struct gprs_ns_inst {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020080 /*! callback to the user for incoming UNIT DATA IND */
Harald Welte1203de32010-05-01 11:28:43 +020081 gprs_ns_cb_t *cb;
82
Neels Hofmeyr87e45502017-06-20 00:17:59 +020083 /*! linked lists of all NSVC in this instance */
Harald Welte1203de32010-05-01 11:28:43 +020084 struct llist_head gprs_nsvcs;
85
Neels Hofmeyr87e45502017-06-20 00:17:59 +020086 /*! a NSVC object that's needed to deal with packets for
Harald Weltea9f23c82011-11-23 15:01:31 +010087 * unknown NSVC */
Harald Weltedd1c83c2010-05-13 13:58:08 +020088 struct gprs_nsvc *unknown_nsvc;
89
Harald Weltefe4ab902010-05-12 17:19:53 +000090 uint16_t timeout[NS_TIMERS_COUNT];
91
Neels Hofmeyr87e45502017-06-20 00:17:59 +020092 /*! NS-over-IP specific bits */
Harald Welteb3ee2652010-05-19 14:38:50 +020093 struct {
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +020094 struct osmo_fd fd;
Harald Welte7fb05232010-05-19 15:09:09 +020095 uint32_t local_ip;
96 uint16_t local_port;
Philipp Maier224a6dd2017-10-17 15:23:53 +020097 uint32_t remote_ip;
98 uint16_t remote_port;
Holger Hans Peter Freyther2c3393d2013-03-25 11:59:58 +010099 int dscp;
Harald Welte057122e2020-06-06 22:04:20 +0200100 /*! IPA compatibility: NS-RESET/BLOCK/UNBLOCK even on IP-SNS */
101 bool use_reset_block_unblock;
Harald Welteb3ee2652010-05-19 14:38:50 +0200102 } nsip;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200103 /*! NS-over-FR-over-GRE-over-IP specific bits */
Harald Welteb3ee2652010-05-19 14:38:50 +0200104 struct {
Pablo Neira Ayusobfadfb72011-05-06 12:11:23 +0200105 struct osmo_fd fd;
Harald Welte7fb05232010-05-19 15:09:09 +0200106 uint32_t local_ip;
Holger Hans Peter Freyther1e57eb62012-03-02 14:14:33 +0100107 unsigned int enabled:1;
Harald Welteb3ee2652010-05-19 14:38:50 +0200108 } frgre;
Harald Welte047f3872018-07-01 21:04:45 +0200109
110 struct osmo_fsm_inst *bss_sns_fi;
Harald Welte1203de32010-05-01 11:28:43 +0200111};
112
Harald Welte80405452010-05-03 20:16:13 +0200113enum nsvc_timer_mode {
114 /* standard timers */
115 NSVC_TIMER_TNS_TEST,
116 NSVC_TIMER_TNS_ALIVE,
Harald Welte69a4cf22010-05-03 20:55:10 +0200117 NSVC_TIMER_TNS_RESET,
118 _NSVC_TIMER_NR,
Harald Welte80405452010-05-03 20:16:13 +0200119};
120
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200121/*! Structure representing a single NS-VC */
Harald Welte3771d092010-04-30 20:26:32 +0200122struct gprs_nsvc {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200123 /*! list of NS-VCs within NS Instance */
Harald Welte3771d092010-04-30 20:26:32 +0200124 struct llist_head list;
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200125 /*! pointer to NS Instance */
Harald Welte3771d092010-04-30 20:26:32 +0200126 struct gprs_ns_inst *nsi;
127
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200128 uint16_t nsei; /*! end-to-end significance */
129 uint16_t nsvci; /*! uniquely identifies NS-VC at SGSN */
Harald Welte3771d092010-04-30 20:26:32 +0200130
Harald Welte8f9a3ee2010-05-02 11:26:34 +0200131 uint32_t state;
132 uint32_t remote_state;
Harald Welte3771d092010-04-30 20:26:32 +0200133
Pablo Neira Ayuso2b159522011-05-06 12:11:06 +0200134 struct osmo_timer_list timer;
Harald Welte80405452010-05-03 20:16:13 +0200135 enum nsvc_timer_mode timer_mode;
Jacob Erlbeck0a1400f2015-10-06 15:23:25 +0200136 struct timeval timer_started;
Harald Welte3771d092010-04-30 20:26:32 +0200137 int alive_retries;
138
Harald Welte2bffac52010-05-12 15:55:23 +0000139 unsigned int remote_end_is_sgsn:1;
140 unsigned int persistent:1;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200141 unsigned int nsvci_is_valid:1;
Harald Welte3771d092010-04-30 20:26:32 +0200142
Harald Welte144e0292010-05-13 11:45:07 +0200143 struct rate_ctr_group *ctrg;
Jacob Erlbeckfc9533d2015-10-29 00:55:58 +0100144 struct osmo_stat_item_group *statg;
Harald Welte144e0292010-05-13 11:45:07 +0200145
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200146 /*! which link-layer are we based on? */
Harald Welteb3ee2652010-05-19 14:38:50 +0200147 enum gprs_ns_ll ll;
148
Max95308592017-10-24 15:54:28 +0200149 /*! make sure to always keep bts_addr as first struct member to not break the assumption
150 that those structs are similar */
Harald Welte3771d092010-04-30 20:26:32 +0200151 union {
152 struct {
153 struct sockaddr_in bts_addr;
154 } ip;
Harald Welteb3ee2652010-05-19 14:38:50 +0200155 struct {
156 struct sockaddr_in bts_addr;
157 } frgre;
Harald Welte3771d092010-04-30 20:26:32 +0200158 };
Harald Welte047f3872018-07-01 21:04:45 +0200159 /*! signalling weight. 0 = don't use for signalling (BVCI == 0)*/
160 uint8_t sig_weight;
161 /*! signaling weight. 0 = don't use for user data (BVCI != 0) */
162 uint8_t data_weight;
Harald Welte3771d092010-04-30 20:26:32 +0200163};
164
Harald Weltef030b212010-04-26 19:18:54 +0200165/* Create a new NS protocol instance */
Harald Welte4fcdd762012-06-16 16:40:42 +0800166struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx);
Harald Welte9ba50052010-03-14 15:45:01 +0800167
Holger Hans Peter Freytherc62a1bf2013-07-02 09:10:11 +0200168/* Close a NS protocol instance */
169void gprs_ns_close(struct gprs_ns_inst *nsi);
170
171/* Close and Destroy a NS protocol instance */
Harald Weltef030b212010-04-26 19:18:54 +0200172void gprs_ns_destroy(struct gprs_ns_inst *nsi);
173
Harald Welteb3ee2652010-05-19 14:38:50 +0200174/* Listen for incoming GPRS packets via NS/UDP */
Harald Welte7fb05232010-05-19 15:09:09 +0200175int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi);
Harald Weltef030b212010-04-26 19:18:54 +0200176
Harald Weltef5430362012-06-17 12:25:53 +0800177/* Establish a connection (from the BSS) to the SGSN */
178struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi,
179 struct sockaddr_in *dest,
180 uint16_t nsei, uint16_t nsvci);
181
Harald Welte047f3872018-07-01 21:04:45 +0200182/* Establish a connection (from the BSS) to the SGSN using IP SNS */
183struct gprs_nsvc *gprs_ns_nsip_connect_sns(struct gprs_ns_inst *nsi, struct sockaddr_in *dest,
184 uint16_t nsei, uint16_t nsvci);
Harald Weltef5430362012-06-17 12:25:53 +0800185
Harald Weltef030b212010-04-26 19:18:54 +0200186struct sockaddr_in;
187
Harald Weltef030b212010-04-26 19:18:54 +0200188/* main function for higher layers (BSSGP) to send NS messages */
Harald Welte24a655f2010-04-30 19:54:29 +0200189int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg);
Harald Weltef030b212010-04-26 19:18:54 +0200190
Alexander Couzensecbf5a12020-06-13 20:01:18 +0200191/* Receive incoming NS message from underlying transport layer */
192int gprs_ns_rcvmsg(struct gprs_ns_inst *nsi, struct msgb *msg,
193 struct sockaddr_in *saddr, enum gprs_ns_ll ll);
194
195
Harald Welte7dd0f9c2019-02-19 11:41:36 +0100196int gprs_ns_tx_alive(struct gprs_nsvc *nsvc);
197int gprs_ns_tx_alive_ack(struct gprs_nsvc *nsvc);
Harald Welte834f26d2010-05-11 06:20:54 +0200198int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause);
199int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause);
200int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc);
Harald Welte3771d092010-04-30 20:26:32 +0200201
Harald Welteb3ee2652010-05-19 14:38:50 +0200202/* Listen for incoming GPRS packets via NS/FR/GRE */
Harald Welte7fb05232010-05-19 15:09:09 +0200203int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi);
Harald Welte3771d092010-04-30 20:26:32 +0200204
Harald Welte047f3872018-07-01 21:04:45 +0200205struct gprs_nsvc *gprs_nsvc_create2(struct gprs_ns_inst *nsi, uint16_t nsvci,
206 uint8_t sig_weight, uint8_t data_weight);
Harald Weltef5430362012-06-17 12:25:53 +0800207void gprs_nsvc_delete(struct gprs_nsvc *nsvc);
208struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei);
209struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci);
Harald Welte047f3872018-07-01 21:04:45 +0200210struct gprs_nsvc *gprs_nsvc_by_rem_addr(struct gprs_ns_inst *nsi, const struct sockaddr_in *sin);
Harald Welte144e0292010-05-13 11:45:07 +0200211
Harald Welte731d1fc2010-05-14 11:53:08 +0000212/* Initiate a RESET procedure (including timer start, ...)*/
Jacob Erlbeck0540d832014-10-08 11:47:36 +0200213int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause);
Harald Welte731d1fc2010-05-14 11:53:08 +0000214
Harald Welte2bffac52010-05-12 15:55:23 +0000215/* Add NS-specific VTY stuff */
216int gprs_ns_vty_init(struct gprs_ns_inst *nsi);
217
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200218/* Resturn peer info as string (NOTE: the buffer is allocated statically) */
Maxcbf5cdf2017-10-24 18:18:55 +0200219const char *gprs_ns_ll_str(const struct gprs_nsvc *nsvc);
Harald Welte4a62eda2019-03-18 18:27:00 +0100220/* Return peer info in user-supplied buffer */
221char *gprs_ns_ll_str_buf(char *buf, size_t buf_len, const struct gprs_nsvc *nsvc);
Harald Welte179f3572019-03-18 18:38:47 +0100222char *gprs_ns_ll_str_c(const void *ctx, const struct gprs_nsvc *nsvc);
Jacob Erlbeck96550e02013-10-14 22:06:47 +0200223
224/* Copy the link layer info from other into nsvc */
225void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other);
226
227/* Clear the link layer info (will never match a real link then) */
228void gprs_ns_ll_clear(struct gprs_nsvc *nsvc);
Jacob Erlbeck34fc4702013-10-09 11:27:04 +0200229
Jacob Erlbeck8d192d72015-04-07 17:52:45 +0200230struct msgb *gprs_ns_msgb_alloc(void);
Harald Welteb3ee2652010-05-19 14:38:50 +0200231
Harald Welte4fcdd762012-06-16 16:40:42 +0800232enum signal_ns {
233 S_NS_RESET,
234 S_NS_BLOCK,
235 S_NS_UNBLOCK,
236 S_NS_ALIVE_EXP, /* Tns-alive expired more than N times */
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200237 S_NS_REPLACED, /* nsvc object is replaced (sets old_nsvc) */
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200238 S_NS_MISMATCH, /* got an unexpected IE (sets msg, pdu_type, ie_type) */
Harald Welte047f3872018-07-01 21:04:45 +0200239 S_SNS_CONFIGURED, /* IP-SNS configuration completed */
Harald Welte4fcdd762012-06-16 16:40:42 +0800240};
241
Max54ca30c2017-05-22 14:48:27 +0200242extern const struct value_string gprs_ns_signal_ns_names[];
Harald Welte83361bd2018-07-05 02:59:28 +0200243const char *gprs_ns_cause_str(enum ns_cause cause);
Max54ca30c2017-05-22 14:48:27 +0200244
Harald Welte4fcdd762012-06-16 16:40:42 +0800245struct ns_signal_data {
246 struct gprs_nsvc *nsvc;
Jacob Erlbeck5e6d6792013-10-14 22:06:48 +0200247 struct gprs_nsvc *old_nsvc;
Harald Welte4fcdd762012-06-16 16:40:42 +0800248 uint8_t cause;
Jacob Erlbeck5405a102013-10-24 01:33:23 +0200249 uint8_t pdu_type;
250 uint8_t ie_type;
251 struct msgb *msg;
Harald Welte4fcdd762012-06-16 16:40:42 +0800252};
253
Harald Weltecca49632012-06-16 17:45:59 +0800254void gprs_ns_set_log_ss(int ss);
255
Daniel Willmannf1318fe2018-05-29 20:55:18 +0200256char *gprs_nsvc_state_append(char *s, struct gprs_nsvc *nsvc);
257
Neels Hofmeyrb525b9e2017-10-16 16:46:43 +0200258/*! @} */