Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file gprs_ns.h */ |
| 2 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 3 | #pragma once |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 4 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 5 | #include <stdint.h> |
| 6 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 7 | /* Our Implementation */ |
| 8 | #include <netinet/in.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 9 | #include <osmocom/core/linuxlist.h> |
| 10 | #include <osmocom/core/msgb.h> |
| 11 | #include <osmocom/core/timer.h> |
| 12 | #include <osmocom/core/select.h> |
Harald Welte | 605ac5d | 2012-06-16 16:09:52 +0800 | [diff] [blame] | 13 | #include <osmocom/gprs/gprs_msgb.h> |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 14 | |
Harald Welte | 8648e49 | 2012-06-17 13:12:51 +0800 | [diff] [blame] | 15 | #include <osmocom/gprs/protocol/gsm_08_16.h> |
| 16 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 17 | #define NS_TIMERS_COUNT 7 |
| 18 | #define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries)" |
| 19 | #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 Freyther | 887934e | 2011-11-05 15:14:59 +0100 | [diff] [blame] | 25 | "Alive Timer (Tns-alive) timeout\n" \ |
| 26 | "Alive Timer (Tns-alive) number of retries\n" |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 27 | |
Daniel Willmann | ba34a2f | 2016-09-02 15:44:12 +0200 | [diff] [blame] | 28 | /* Educated guess - LLC user payload is 1500 bytes plus possible headers */ |
| 29 | #define NS_ALLOC_SIZE 3072 |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 30 | #define NS_ALLOC_HEADROOM 20 |
| 31 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 32 | enum ns_timeout { |
| 33 | NS_TOUT_TNS_BLOCK, |
| 34 | NS_TOUT_TNS_BLOCK_RETRIES, |
| 35 | NS_TOUT_TNS_RESET, |
| 36 | NS_TOUT_TNS_RESET_RETRIES, |
| 37 | NS_TOUT_TNS_TEST, |
| 38 | NS_TOUT_TNS_ALIVE, |
| 39 | NS_TOUT_TNS_ALIVE_RETRIES, |
| 40 | }; |
| 41 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 42 | #define NSE_S_BLOCKED 0x0001 |
| 43 | #define NSE_S_ALIVE 0x0002 |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 44 | #define NSE_S_RESET 0x0004 |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 45 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! Osmocom NS link layer types */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 47 | enum gprs_ns_ll { |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 48 | GPRS_NS_LL_UDP, /*!< NS/UDP/IP */ |
| 49 | GPRS_NS_LL_E1, /*!< NS/E1 */ |
| 50 | GPRS_NS_LL_FR_GRE, /*!< NS/FR/GRE/IP */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 53 | /*! Osmoco NS events */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 54 | enum gprs_ns_evt { |
| 55 | GPRS_NS_EVT_UNIT_DATA, |
| 56 | }; |
| 57 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 58 | /*! Osmocom NS VC create status */ |
Jacob Erlbeck | 84cdc70 | 2013-10-08 12:04:44 +0200 | [diff] [blame] | 59 | enum gprs_ns_cs { |
| 60 | GPRS_NS_CS_CREATED, /*!< A NSVC object has been created */ |
| 61 | GPRS_NS_CS_FOUND, /*!< A NSVC object has been found */ |
| 62 | GPRS_NS_CS_REJECTED, /*!< Rejected and answered message */ |
| 63 | GPRS_NS_CS_SKIPPED, /*!< Skipped message */ |
| 64 | GPRS_NS_CS_ERROR, /*!< Failed to process message */ |
| 65 | }; |
| 66 | |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 67 | struct gprs_nsvc; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | /*! Osmocom GPRS callback function type */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 69 | typedef int gprs_ns_cb_t(enum gprs_ns_evt event, struct gprs_nsvc *nsvc, |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 70 | struct msgb *msg, uint16_t bvci); |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 71 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 72 | /*! An instance of the NS protocol stack */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 73 | struct gprs_ns_inst { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 74 | /*! callback to the user for incoming UNIT DATA IND */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 75 | gprs_ns_cb_t *cb; |
| 76 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 77 | /*! linked lists of all NSVC in this instance */ |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 78 | struct llist_head gprs_nsvcs; |
| 79 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 80 | /*! a NSVC object that's needed to deal with packets for |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 81 | * unknown NSVC */ |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 82 | struct gprs_nsvc *unknown_nsvc; |
| 83 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 84 | uint16_t timeout[NS_TIMERS_COUNT]; |
| 85 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 86 | /*! NS-over-IP specific bits */ |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 87 | struct { |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 88 | struct osmo_fd fd; |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 89 | uint32_t local_ip; |
| 90 | uint16_t local_port; |
Holger Hans Peter Freyther | 2c3393d | 2013-03-25 11:59:58 +0100 | [diff] [blame] | 91 | int dscp; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 92 | } nsip; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 93 | /*! NS-over-FR-over-GRE-over-IP specific bits */ |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 94 | struct { |
Pablo Neira Ayuso | bfadfb7 | 2011-05-06 12:11:23 +0200 | [diff] [blame] | 95 | struct osmo_fd fd; |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 96 | uint32_t local_ip; |
Holger Hans Peter Freyther | 1e57eb6 | 2012-03-02 14:14:33 +0100 | [diff] [blame] | 97 | unsigned int enabled:1; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 98 | } frgre; |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 101 | enum nsvc_timer_mode { |
| 102 | /* standard timers */ |
| 103 | NSVC_TIMER_TNS_TEST, |
| 104 | NSVC_TIMER_TNS_ALIVE, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 105 | NSVC_TIMER_TNS_RESET, |
| 106 | _NSVC_TIMER_NR, |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 109 | /*! Structure representing a single NS-VC */ |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 110 | struct gprs_nsvc { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 111 | /*! list of NS-VCs within NS Instance */ |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 112 | struct llist_head list; |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 113 | /*! pointer to NS Instance */ |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 114 | struct gprs_ns_inst *nsi; |
| 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 116 | uint16_t nsei; /*! end-to-end significance */ |
| 117 | uint16_t nsvci; /*! uniquely identifies NS-VC at SGSN */ |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 118 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 119 | uint32_t state; |
| 120 | uint32_t remote_state; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 121 | |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 122 | struct osmo_timer_list timer; |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 123 | enum nsvc_timer_mode timer_mode; |
Jacob Erlbeck | 0a1400f | 2015-10-06 15:23:25 +0200 | [diff] [blame] | 124 | struct timeval timer_started; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 125 | int alive_retries; |
| 126 | |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 127 | unsigned int remote_end_is_sgsn:1; |
| 128 | unsigned int persistent:1; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 129 | unsigned int nsvci_is_valid:1; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 130 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 131 | struct rate_ctr_group *ctrg; |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 132 | struct osmo_stat_item_group *statg; |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 133 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 134 | /*! which link-layer are we based on? */ |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 135 | enum gprs_ns_ll ll; |
| 136 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 137 | union { |
| 138 | struct { |
| 139 | struct sockaddr_in bts_addr; |
| 140 | } ip; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 141 | struct { |
| 142 | struct sockaddr_in bts_addr; |
| 143 | } frgre; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 144 | }; |
| 145 | }; |
| 146 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 147 | /* Create a new NS protocol instance */ |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 148 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb, void *ctx); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 149 | |
Holger Hans Peter Freyther | c62a1bf | 2013-07-02 09:10:11 +0200 | [diff] [blame] | 150 | /* Close a NS protocol instance */ |
| 151 | void gprs_ns_close(struct gprs_ns_inst *nsi); |
| 152 | |
| 153 | /* Close and Destroy a NS protocol instance */ |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 154 | void gprs_ns_destroy(struct gprs_ns_inst *nsi); |
| 155 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 156 | /* Listen for incoming GPRS packets via NS/UDP */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 157 | int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 158 | |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 159 | /* Establish a connection (from the BSS) to the SGSN */ |
| 160 | struct gprs_nsvc *gprs_ns_nsip_connect(struct gprs_ns_inst *nsi, |
| 161 | struct sockaddr_in *dest, |
| 162 | uint16_t nsei, uint16_t nsvci); |
| 163 | |
| 164 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 165 | struct sockaddr_in; |
| 166 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 167 | /* main function for higher layers (BSSGP) to send NS messages */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 168 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 169 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 170 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause); |
| 171 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause); |
| 172 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc); |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 173 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 174 | /* Listen for incoming GPRS packets via NS/FR/GRE */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 175 | int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi); |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 176 | |
Harald Welte | f543036 | 2012-06-17 12:25:53 +0800 | [diff] [blame] | 177 | struct gprs_nsvc *gprs_nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci); |
| 178 | void gprs_nsvc_delete(struct gprs_nsvc *nsvc); |
| 179 | struct gprs_nsvc *gprs_nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei); |
| 180 | struct gprs_nsvc *gprs_nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 181 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 182 | /* Initiate a RESET procedure (including timer start, ...)*/ |
Jacob Erlbeck | 0540d83 | 2014-10-08 11:47:36 +0200 | [diff] [blame] | 183 | int gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause); |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 184 | |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 185 | /* Add NS-specific VTY stuff */ |
| 186 | int gprs_ns_vty_init(struct gprs_ns_inst *nsi); |
| 187 | |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 188 | /* Resturn peer info as string (NOTE: the buffer is allocated statically) */ |
Jacob Erlbeck | 96550e0 | 2013-10-14 22:06:47 +0200 | [diff] [blame] | 189 | const char *gprs_ns_ll_str(struct gprs_nsvc *nsvc); |
| 190 | |
| 191 | /* Copy the link layer info from other into nsvc */ |
| 192 | void gprs_ns_ll_copy(struct gprs_nsvc *nsvc, struct gprs_nsvc *other); |
| 193 | |
| 194 | /* Clear the link layer info (will never match a real link then) */ |
| 195 | void gprs_ns_ll_clear(struct gprs_nsvc *nsvc); |
Jacob Erlbeck | 34fc470 | 2013-10-09 11:27:04 +0200 | [diff] [blame] | 196 | |
Jacob Erlbeck | 8d192d7 | 2015-04-07 17:52:45 +0200 | [diff] [blame] | 197 | struct msgb *gprs_ns_msgb_alloc(void); |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 198 | |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 199 | enum signal_ns { |
| 200 | S_NS_RESET, |
| 201 | S_NS_BLOCK, |
| 202 | S_NS_UNBLOCK, |
| 203 | S_NS_ALIVE_EXP, /* Tns-alive expired more than N times */ |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 204 | S_NS_REPLACED, /* nsvc object is replaced (sets old_nsvc) */ |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 205 | S_NS_MISMATCH, /* got an unexpected IE (sets msg, pdu_type, ie_type) */ |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 206 | }; |
| 207 | |
Max | 54ca30c | 2017-05-22 14:48:27 +0200 | [diff] [blame] | 208 | extern const struct value_string gprs_ns_signal_ns_names[]; |
| 209 | |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 210 | struct ns_signal_data { |
| 211 | struct gprs_nsvc *nsvc; |
Jacob Erlbeck | 5e6d679 | 2013-10-14 22:06:48 +0200 | [diff] [blame] | 212 | struct gprs_nsvc *old_nsvc; |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 213 | uint8_t cause; |
Jacob Erlbeck | 5405a10 | 2013-10-24 01:33:23 +0200 | [diff] [blame] | 214 | uint8_t pdu_type; |
| 215 | uint8_t ie_type; |
| 216 | struct msgb *msg; |
Harald Welte | 4fcdd76 | 2012-06-16 16:40:42 +0800 | [diff] [blame] | 217 | }; |
| 218 | |
Harald Welte | cca4963 | 2012-06-16 17:45:59 +0800 | [diff] [blame] | 219 | void gprs_ns_set_log_ss(int ss); |
| 220 | |
Harald Welte | a9f23c8 | 2011-11-23 15:01:31 +0100 | [diff] [blame] | 221 | /*! }@ */ |