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