Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | #ifndef _GPRS_NS_H |
| 2 | #define _GPRS_NS_H |
| 3 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 4 | #include <stdint.h> |
| 5 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 6 | /* GPRS Networks Service (NS) messages on the Gb interface |
| 7 | * 3GPP TS 08.16 version 8.0.1 Release 1999 / ETSI TS 101 299 V8.0.1 (2002-05) |
| 8 | * 3GPP TS 48.016 version 6.5.0 Release 6 / ETSI TS 148 016 V6.5.0 (2005-11) */ |
| 9 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 10 | struct gprs_ns_hdr { |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 11 | uint8_t pdu_type; |
| 12 | uint8_t data[0]; |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 13 | } __attribute__((packed)); |
| 14 | |
| 15 | /* TS 08.16, Section 10.3.7, Table 14 */ |
| 16 | enum ns_pdu_type { |
| 17 | NS_PDUT_UNITDATA = 0x00, |
| 18 | NS_PDUT_RESET = 0x02, |
| 19 | NS_PDUT_RESET_ACK = 0x03, |
| 20 | NS_PDUT_BLOCK = 0x04, |
| 21 | NS_PDUT_BLOCK_ACK = 0x05, |
| 22 | NS_PDUT_UNBLOCK = 0x06, |
| 23 | NS_PDUT_UNBLOCK_ACK = 0x07, |
| 24 | NS_PDUT_STATUS = 0x08, |
| 25 | NS_PDUT_ALIVE = 0x0a, |
| 26 | NS_PDUT_ALIVE_ACK = 0x0b, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 27 | /* TS 48.016 Section 10.3.7, Table 10.3.7.1 */ |
| 28 | SNS_PDUT_ACK = 0x0c, |
| 29 | SNS_PDUT_ADD = 0x0d, |
| 30 | SNS_PDUT_CHANGE_WEIGHT = 0x0e, |
| 31 | SNS_PDUT_CONFIG = 0x0f, |
| 32 | SNS_PDUT_CONFIG_ACK = 0x10, |
| 33 | SNS_PDUT_DELETE = 0x11, |
| 34 | SNS_PDUT_SIZE = 0x12, |
| 35 | SNS_PDUT_SIZE_ACK = 0x13, |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 36 | }; |
| 37 | |
| 38 | /* TS 08.16, Section 10.3, Table 12 */ |
| 39 | enum ns_ctrl_ie { |
| 40 | NS_IE_CAUSE = 0x00, |
| 41 | NS_IE_VCI = 0x01, |
| 42 | NS_IE_PDU = 0x02, |
| 43 | NS_IE_BVCI = 0x03, |
| 44 | NS_IE_NSEI = 0x04, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 45 | /* TS 48.016 Section 10.3, Table 10.3.1 */ |
| 46 | NS_IE_IPv4_LIST = 0x05, |
| 47 | NS_IE_IPv6_LIST = 0x06, |
| 48 | NS_IE_MAX_NR_NSVC = 0x07, |
| 49 | NS_IE_IPv4_EP_NR = 0x08, |
| 50 | NS_IE_IPv6_EP_NR = 0x09, |
| 51 | NS_IE_RESET_FLAG = 0x0a, |
| 52 | NS_IE_IP_ADDR = 0x0b, |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* TS 08.16, Section 10.3.2, Table 13 */ |
| 56 | enum ns_cause { |
| 57 | NS_CAUSE_TRANSIT_FAIL = 0x00, |
| 58 | NS_CAUSE_OM_INTERVENTION = 0x01, |
| 59 | NS_CAUSE_EQUIP_FAIL = 0x02, |
| 60 | NS_CAUSE_NSVC_BLOCKED = 0x03, |
| 61 | NS_CAUSE_NSVC_UNKNOWN = 0x04, |
| 62 | NS_CAUSE_BVCI_UNKNOWN = 0x05, |
| 63 | NS_CAUSE_SEM_INCORR_PDU = 0x08, |
| 64 | NS_CAUSE_PDU_INCOMP_PSTATE = 0x0a, |
| 65 | NS_CAUSE_PROTO_ERR_UNSPEC = 0x0b, |
| 66 | NS_CAUSE_INVAL_ESSENT_IE = 0x0c, |
| 67 | NS_CAUSE_MISSING_ESSENT_IE = 0x0d, |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 68 | /* TS 48.016 Section 10.3.2, Table 10.3.2.1 */ |
| 69 | NS_CAUSE_INVAL_NR_IPv4_EP = 0x0e, |
| 70 | NS_CAUSE_INVAL_NR_IPv6_EP = 0x0f, |
| 71 | NS_CAUSE_INVAL_NR_NS_VC = 0x10, |
| 72 | NS_CAUSE_INVAL_WEIGH = 0x11, |
| 73 | NS_CAUSE_UNKN_IP_EP = 0x12, |
| 74 | NS_CAUSE_UNKN_IP_ADDR = 0x13, |
| 75 | NS_CAUSE_UNKN_IP_TEST_FAILED = 0x14, |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 76 | }; |
| 77 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 78 | /* Our Implementation */ |
| 79 | #include <netinet/in.h> |
Pablo Neira Ayuso | ff66323 | 2011-03-22 16:47:59 +0100 | [diff] [blame] | 80 | #include <osmocom/core/linuxlist.h> |
| 81 | #include <osmocom/core/msgb.h> |
| 82 | #include <osmocom/core/timer.h> |
| 83 | #include <osmocom/core/select.h> |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 84 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 85 | #define NS_TIMERS_COUNT 7 |
| 86 | #define NS_TIMERS "(tns-block|tns-block-retries|tns-reset|tns-reset-retries|tns-test|tns-alive|tns-alive-retries)" |
| 87 | #define NS_TIMERS_HELP \ |
| 88 | "(un)blocking Timer (Tns-block) timeout\n" \ |
| 89 | "(un)blocking Timer (Tns-block) number of retries\n" \ |
| 90 | "Reset Timer (Tns-reset) timeout\n" \ |
| 91 | "Reset Timer (Tns-reset) number of retries\n" \ |
| 92 | "Test Timer (Tns-test) timeout\n" \ |
| 93 | |
| 94 | enum ns_timeout { |
| 95 | NS_TOUT_TNS_BLOCK, |
| 96 | NS_TOUT_TNS_BLOCK_RETRIES, |
| 97 | NS_TOUT_TNS_RESET, |
| 98 | NS_TOUT_TNS_RESET_RETRIES, |
| 99 | NS_TOUT_TNS_TEST, |
| 100 | NS_TOUT_TNS_ALIVE, |
| 101 | NS_TOUT_TNS_ALIVE_RETRIES, |
| 102 | }; |
| 103 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 104 | #define NSE_S_BLOCKED 0x0001 |
| 105 | #define NSE_S_ALIVE 0x0002 |
| 106 | |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 107 | enum gprs_ns_ll { |
| 108 | GPRS_NS_LL_UDP, |
| 109 | GPRS_NS_LL_E1, |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 110 | GPRS_NS_LL_FR_GRE, |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | enum gprs_ns_evt { |
| 114 | GPRS_NS_EVT_UNIT_DATA, |
| 115 | }; |
| 116 | |
| 117 | struct gprs_nsvc; |
| 118 | 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] | 119 | struct msgb *msg, uint16_t bvci); |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 120 | |
| 121 | /* An instance of the NS protocol stack */ |
| 122 | struct gprs_ns_inst { |
| 123 | /* callback to the user for incoming UNIT DATA IND */ |
| 124 | gprs_ns_cb_t *cb; |
| 125 | |
| 126 | /* linked lists of all NSVC in this instance */ |
| 127 | struct llist_head gprs_nsvcs; |
| 128 | |
Harald Welte | dd1c83c | 2010-05-13 13:58:08 +0200 | [diff] [blame] | 129 | /* a NSVC object that's needed to deal with packets for unknown NSVC */ |
| 130 | struct gprs_nsvc *unknown_nsvc; |
| 131 | |
Harald Welte | fe4ab90 | 2010-05-12 17:19:53 +0000 | [diff] [blame] | 132 | uint16_t timeout[NS_TIMERS_COUNT]; |
| 133 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 134 | /* NS-over-IP specific bits */ |
| 135 | struct { |
| 136 | struct bsc_fd fd; |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 137 | uint32_t local_ip; |
| 138 | uint16_t local_port; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 139 | } nsip; |
| 140 | /* NS-over-FR-over-GRE-over-IP specific bits */ |
| 141 | struct { |
| 142 | struct bsc_fd fd; |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 143 | uint32_t local_ip; |
| 144 | int enabled:1; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 145 | } frgre; |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 146 | }; |
| 147 | |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 148 | enum nsvc_timer_mode { |
| 149 | /* standard timers */ |
| 150 | NSVC_TIMER_TNS_TEST, |
| 151 | NSVC_TIMER_TNS_ALIVE, |
Harald Welte | 69a4cf2 | 2010-05-03 20:55:10 +0200 | [diff] [blame] | 152 | NSVC_TIMER_TNS_RESET, |
| 153 | _NSVC_TIMER_NR, |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 154 | }; |
| 155 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 156 | struct gprs_nsvc { |
| 157 | struct llist_head list; |
| 158 | struct gprs_ns_inst *nsi; |
| 159 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 160 | uint16_t nsei; /* end-to-end significance */ |
| 161 | uint16_t nsvci; /* uniquely identifies NS-VC at SGSN */ |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 162 | |
Harald Welte | 8f9a3ee | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 163 | uint32_t state; |
| 164 | uint32_t remote_state; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 165 | |
Pablo Neira Ayuso | 2b15952 | 2011-05-06 12:11:06 +0200 | [diff] [blame^] | 166 | struct osmo_timer_list timer; |
Harald Welte | 8040545 | 2010-05-03 20:16:13 +0200 | [diff] [blame] | 167 | enum nsvc_timer_mode timer_mode; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 168 | int alive_retries; |
| 169 | |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 170 | unsigned int remote_end_is_sgsn:1; |
| 171 | unsigned int persistent:1; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 172 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 173 | struct rate_ctr_group *ctrg; |
| 174 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 175 | /* which link-layer are we based on? */ |
| 176 | enum gprs_ns_ll ll; |
| 177 | |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 178 | union { |
| 179 | struct { |
| 180 | struct sockaddr_in bts_addr; |
| 181 | } ip; |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 182 | struct { |
| 183 | struct sockaddr_in bts_addr; |
| 184 | } frgre; |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 185 | }; |
| 186 | }; |
| 187 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 188 | /* Create a new NS protocol instance */ |
| 189 | struct gprs_ns_inst *gprs_ns_instantiate(gprs_ns_cb_t *cb); |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 190 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 191 | /* Destroy a NS protocol instance */ |
| 192 | void gprs_ns_destroy(struct gprs_ns_inst *nsi); |
| 193 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 194 | /* Listen for incoming GPRS packets via NS/UDP */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 195 | int gprs_ns_nsip_listen(struct gprs_ns_inst *nsi); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 196 | |
| 197 | struct sockaddr_in; |
| 198 | |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 199 | /* main function for higher layers (BSSGP) to send NS messages */ |
Harald Welte | 24a655f | 2010-04-30 19:54:29 +0200 | [diff] [blame] | 200 | int gprs_ns_sendmsg(struct gprs_ns_inst *nsi, struct msgb *msg); |
Harald Welte | f030b21 | 2010-04-26 19:18:54 +0200 | [diff] [blame] | 201 | |
Harald Welte | 834f26d | 2010-05-11 06:20:54 +0200 | [diff] [blame] | 202 | int gprs_ns_tx_reset(struct gprs_nsvc *nsvc, uint8_t cause); |
| 203 | int gprs_ns_tx_block(struct gprs_nsvc *nsvc, uint8_t cause); |
| 204 | int gprs_ns_tx_unblock(struct gprs_nsvc *nsvc); |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 205 | |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 206 | /* Listen for incoming GPRS packets via NS/FR/GRE */ |
Harald Welte | 7fb0523 | 2010-05-19 15:09:09 +0200 | [diff] [blame] | 207 | int gprs_ns_frgre_listen(struct gprs_ns_inst *nsi); |
Harald Welte | 3771d09 | 2010-04-30 20:26:32 +0200 | [diff] [blame] | 208 | |
| 209 | /* Establish a connection (from the BSS) to the SGSN */ |
| 210 | struct gprs_nsvc *nsip_connect(struct gprs_ns_inst *nsi, |
Harald Welte | 1203de3 | 2010-05-01 11:28:43 +0200 | [diff] [blame] | 211 | struct sockaddr_in *dest, uint16_t nsei, |
| 212 | uint16_t nsvci); |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 213 | |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 214 | struct gprs_nsvc *nsvc_create(struct gprs_ns_inst *nsi, uint16_t nsvci); |
| 215 | void nsvc_delete(struct gprs_nsvc *nsvc); |
| 216 | struct gprs_nsvc *nsvc_by_nsei(struct gprs_ns_inst *nsi, uint16_t nsei); |
Harald Welte | 90af194 | 2010-05-15 23:02:24 +0200 | [diff] [blame] | 217 | struct gprs_nsvc *nsvc_by_nsvci(struct gprs_ns_inst *nsi, uint16_t nsvci); |
Harald Welte | 144e029 | 2010-05-13 11:45:07 +0200 | [diff] [blame] | 218 | |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 219 | /* Initiate a RESET procedure (including timer start, ...)*/ |
Holger Hans Peter Freyther | 5617d99 | 2010-05-23 21:18:01 +0800 | [diff] [blame] | 220 | void gprs_nsvc_reset(struct gprs_nsvc *nsvc, uint8_t cause); |
Harald Welte | 731d1fc | 2010-05-14 11:53:08 +0000 | [diff] [blame] | 221 | |
Harald Welte | 2bffac5 | 2010-05-12 15:55:23 +0000 | [diff] [blame] | 222 | /* Add NS-specific VTY stuff */ |
| 223 | int gprs_ns_vty_init(struct gprs_ns_inst *nsi); |
| 224 | |
Holger Hans Peter Freyther | 4a90d22 | 2010-06-14 22:11:40 +0800 | [diff] [blame] | 225 | #define NS_ALLOC_SIZE 2048 |
Harald Welte | ba4c666 | 2010-05-19 15:38:10 +0200 | [diff] [blame] | 226 | #define NS_ALLOC_HEADROOM 20 |
| 227 | static inline struct msgb *gprs_ns_msgb_alloc(void) |
| 228 | { |
| 229 | return msgb_alloc_headroom(NS_ALLOC_SIZE, NS_ALLOC_HEADROOM, "GPRS/NS"); |
| 230 | } |
Harald Welte | b3ee265 | 2010-05-19 14:38:50 +0200 | [diff] [blame] | 231 | |
Harald Welte | 9ba5005 | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 232 | #endif |