Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 1 | #ifndef _GPRS_LLC_H |
| 2 | #define _GPRS_LLC_H |
| 3 | |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 4 | #include <stdint.h> |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 5 | #include <stdbool.h> |
Neels Hofmeyr | 396f2e6 | 2017-09-04 15:13:25 +0200 | [diff] [blame] | 6 | #include <osmocom/sgsn/gprs_sgsn.h> |
| 7 | #include <osmocom/sgsn/gprs_llc_xid.h> |
Harald Welte | eaa614c | 2010-05-02 11:26:34 +0200 | [diff] [blame] | 8 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 9 | /* Section 4.7 LLC Layer Structure */ |
| 10 | enum gprs_llc_sapi { |
| 11 | GPRS_SAPI_GMM = 1, |
| 12 | GPRS_SAPI_TOM2 = 2, |
| 13 | GPRS_SAPI_SNDCP3 = 3, |
| 14 | GPRS_SAPI_SNDCP5 = 5, |
| 15 | GPRS_SAPI_SMS = 7, |
| 16 | GPRS_SAPI_TOM8 = 8, |
| 17 | GPRS_SAPI_SNDCP9 = 9, |
| 18 | GPRS_SAPI_SNDCP11 = 11, |
| 19 | }; |
| 20 | |
Harald Welte | 10997d0 | 2010-05-03 12:28:12 +0200 | [diff] [blame] | 21 | /* Section 6.4 Commands and Responses */ |
| 22 | enum gprs_llc_u_cmd { |
| 23 | GPRS_LLC_U_DM_RESP = 0x01, |
| 24 | GPRS_LLC_U_DISC_CMD = 0x04, |
| 25 | GPRS_LLC_U_UA_RESP = 0x06, |
| 26 | GPRS_LLC_U_SABM_CMD = 0x07, |
| 27 | GPRS_LLC_U_FRMR_RESP = 0x08, |
| 28 | GPRS_LLC_U_XID = 0x0b, |
| 29 | GPRS_LLC_U_NULL_CMD = 0x00, |
| 30 | }; |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 31 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 32 | /* Section 6.4.1.6 / Table 6 */ |
| 33 | enum gprs_llc_xid_type { |
| 34 | GPRS_LLC_XID_T_VERSION = 0, |
| 35 | GPRS_LLC_XID_T_IOV_UI = 1, |
| 36 | GPRS_LLC_XID_T_IOV_I = 2, |
| 37 | GPRS_LLC_XID_T_T200 = 3, |
| 38 | GPRS_LLC_XID_T_N200 = 4, |
| 39 | GPRS_LLC_XID_T_N201_U = 5, |
| 40 | GPRS_LLC_XID_T_N201_I = 6, |
| 41 | GPRS_LLC_XID_T_mD = 7, |
| 42 | GPRS_LLC_XID_T_mU = 8, |
| 43 | GPRS_LLC_XID_T_kD = 9, |
| 44 | GPRS_LLC_XID_T_kU = 10, |
| 45 | GPRS_LLC_XID_T_L3_PAR = 11, |
| 46 | GPRS_LLC_XID_T_RESET = 12, |
| 47 | }; |
| 48 | |
Max | 549ebc7 | 2016-11-18 14:07:04 +0100 | [diff] [blame] | 49 | extern const struct value_string gprs_llc_xid_type_names[]; |
| 50 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 51 | /* TS 04.64 Section 7.1.2 Table 7: LLC layer primitives (GMM/SNDCP/SMS/TOM) */ |
| 52 | /* TS 04.65 Section 5.1.2 Table 2: Service primitives used by SNDCP */ |
| 53 | enum gprs_llc_primitive { |
| 54 | /* GMM <-> LLME */ |
| 55 | LLGMM_ASSIGN_REQ, /* GMM tells us new TLLI: TLLI old, TLLI new, Kc, CiphAlg */ |
| 56 | LLGMM_RESET_REQ, /* GMM tells us to perform XID negotiation: TLLI */ |
| 57 | LLGMM_RESET_CNF, /* LLC informs GMM that XID has completed: TLLI */ |
| 58 | LLGMM_SUSPEND_REQ, /* GMM tells us MS has suspended: TLLI, Page */ |
| 59 | LLGMM_RESUME_REQ, /* GMM tells us MS has resumed: TLLI */ |
| 60 | LLGMM_PAGE_IND, /* LLC asks GMM to page MS: TLLI */ |
| 61 | LLGMM_IOV_REQ, /* GMM tells us to perform XID: TLLI */ |
| 62 | LLGMM_STATUS_IND, /* LLC informs GMM about error: TLLI, Cause */ |
| 63 | /* LLE <-> (GMM/SNDCP/SMS/TOM) */ |
| 64 | LL_RESET_IND, /* TLLI */ |
| 65 | LL_ESTABLISH_REQ, /* TLLI, XID Req */ |
| 66 | LL_ESTABLISH_IND, /* TLLI, XID Req, N201-I, N201-U */ |
| 67 | LL_ESTABLISH_RESP, /* TLLI, XID Negotiated */ |
| 68 | LL_ESTABLISH_CONF, /* TLLI, XID Neg, N201-i, N201-U */ |
| 69 | LL_RELEASE_REQ, /* TLLI, Local */ |
| 70 | LL_RELEASE_IND, /* TLLI, Cause */ |
| 71 | LL_RELEASE_CONF, /* TLLI */ |
| 72 | LL_XID_REQ, /* TLLI, XID Requested */ |
| 73 | LL_XID_IND, /* TLLI, XID Req, N201-I, N201-U */ |
| 74 | LL_XID_RESP, /* TLLI, XID Negotiated */ |
| 75 | LL_XID_CONF, /* TLLI, XID Neg, N201-I, N201-U */ |
| 76 | LL_DATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */ |
| 77 | LL_DATA_IND, /* TLLI, SN-PDU */ |
| 78 | LL_DATA_CONF, /* TLLI, Ref */ |
| 79 | LL_UNITDATA_REQ, /* TLLI, SN-PDU, Ref, QoS, Radio Prio, Ciph */ |
| 80 | LL_UNITDATA_IND, /* TLLI, SN-PDU */ |
| 81 | LL_STATUS_IND, /* TLLI, Cause */ |
| 82 | }; |
| 83 | |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 84 | /* Section 4.5.2 Logical Link States + Annex C.2 */ |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 85 | enum gprs_llc_lle_state { |
| 86 | GPRS_LLES_UNASSIGNED = 1, /* No TLLI yet */ |
| 87 | GPRS_LLES_ASSIGNED_ADM = 2, /* TLLI assigned */ |
| 88 | GPRS_LLES_LOCAL_EST = 3, /* Local Establishment */ |
| 89 | GPRS_LLES_REMOTE_EST = 4, /* Remote Establishment */ |
| 90 | GPRS_LLES_ABM = 5, |
| 91 | GPRS_LLES_LOCAL_REL = 6, /* Local Release */ |
| 92 | GPRS_LLES_TIMER_REC = 7, /* Timer Recovery */ |
| 93 | }; |
| 94 | |
| 95 | enum gprs_llc_llme_state { |
| 96 | GPRS_LLMS_UNASSIGNED = 1, /* No TLLI yet */ |
| 97 | GPRS_LLMS_ASSIGNED = 2, /* TLLI assigned */ |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 98 | }; |
| 99 | |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 100 | /* Section 8.9.9 LLC layer parameter default values */ |
| 101 | struct gprs_llc_params { |
| 102 | uint16_t iov_i_exp; |
| 103 | uint16_t t200_201; |
| 104 | uint16_t n200; |
| 105 | uint16_t n201_u; |
| 106 | uint16_t n201_i; |
| 107 | uint16_t mD; |
| 108 | uint16_t mU; |
| 109 | uint16_t kD; |
| 110 | uint16_t kU; |
| 111 | }; |
| 112 | |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 113 | /* Section 4.7.1: Logical Link Entity: One per DLCI (TLLI + SAPI) */ |
| 114 | struct gprs_llc_lle { |
| 115 | struct llist_head list; |
| 116 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 117 | uint32_t sapi; |
| 118 | |
| 119 | struct gprs_llc_llme *llme; |
| 120 | |
| 121 | enum gprs_llc_lle_state state; |
| 122 | |
Pablo Neira Ayuso | bf540cb | 2011-05-06 12:11:06 +0200 | [diff] [blame] | 123 | struct osmo_timer_list t200; |
| 124 | struct osmo_timer_list t201; /* wait for acknowledgement */ |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 125 | |
Harald Welte | 6bdee6a | 2010-05-30 21:51:58 +0200 | [diff] [blame] | 126 | uint16_t v_sent; |
| 127 | uint16_t v_ack; |
| 128 | uint16_t v_recv; |
| 129 | |
| 130 | uint16_t vu_send; |
| 131 | uint16_t vu_recv; |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 132 | |
Harald Welte | abadd54 | 2013-06-21 14:06:18 +0200 | [diff] [blame] | 133 | /* non-standard LLC state */ |
| 134 | uint16_t vu_recv_last; |
| 135 | uint16_t vu_recv_duplicates; |
| 136 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 137 | /* Overflow Counter for ABM */ |
| 138 | uint32_t oc_i_send; |
| 139 | uint32_t oc_i_recv; |
| 140 | |
| 141 | /* Overflow Counter for unconfirmed transfer */ |
| 142 | uint32_t oc_ui_send; |
| 143 | uint32_t oc_ui_recv; |
| 144 | |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 145 | unsigned int retrans_ctr; |
Harald Welte | 1d9d944 | 2010-06-03 07:11:04 +0200 | [diff] [blame] | 146 | |
| 147 | struct gprs_llc_params params; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | #define NUM_SAPIS 16 |
| 151 | |
| 152 | struct gprs_llc_llme { |
| 153 | struct llist_head list; |
| 154 | |
| 155 | enum gprs_llc_llme_state state; |
| 156 | |
| 157 | uint32_t tlli; |
| 158 | uint32_t old_tlli; |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 159 | |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 160 | /* Crypto parameters */ |
| 161 | enum gprs_ciph_algo algo; |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 162 | uint8_t kc[16]; |
| 163 | uint8_t cksn; |
| 164 | /* 3GPP TS 44.064 ยง 8.9.2: */ |
| 165 | uint32_t iov_ui; |
Harald Welte | d07b4f9 | 2010-06-30 23:07:59 +0200 | [diff] [blame] | 166 | |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 167 | /* over which BSSGP BTS ctx do we need to transmit */ |
| 168 | uint16_t bvci; |
| 169 | uint16_t nsei; |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 170 | struct gprs_llc_lle lle[NUM_SAPIS]; |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 171 | |
Philipp | 4ac3aee | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 172 | /* Copy of the XID fields we have sent with the last |
| 173 | * network originated XID-Request. Since the phone |
| 174 | * may strip the optional fields in the confirmation |
| 175 | * we need to remeber those fields in order to be |
| 176 | * able to create the compression entity. */ |
| 177 | struct llist_head *xid; |
| 178 | |
Philipp | f1f3436 | 2016-08-26 17:00:21 +0200 | [diff] [blame] | 179 | /* Compression entities */ |
| 180 | struct { |
| 181 | /* In these two list_heads we will store the |
| 182 | * data and protocol compression entities, |
| 183 | * together with their compression states */ |
| 184 | struct llist_head *proto; |
| 185 | struct llist_head *data; |
| 186 | } comp; |
| 187 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 188 | /* Internal management */ |
| 189 | uint32_t age_timestamp; |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 190 | }; |
| 191 | |
Jacob Erlbeck | 81ffb74 | 2015-01-23 11:33:51 +0100 | [diff] [blame] | 192 | #define GPRS_LLME_RESET_AGE (0) |
| 193 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 194 | extern struct llist_head gprs_llc_llmes; |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 195 | |
Jacob Erlbeck | b492d39 | 2014-06-02 10:49:01 +0200 | [diff] [blame] | 196 | /* LLC low level types */ |
| 197 | |
| 198 | enum gprs_llc_cmd { |
| 199 | GPRS_LLC_NULL, |
| 200 | GPRS_LLC_RR, |
| 201 | GPRS_LLC_ACK, |
| 202 | GPRS_LLC_RNR, |
| 203 | GPRS_LLC_SACK, |
| 204 | GPRS_LLC_DM, |
| 205 | GPRS_LLC_DISC, |
| 206 | GPRS_LLC_UA, |
| 207 | GPRS_LLC_SABM, |
| 208 | GPRS_LLC_FRMR, |
| 209 | GPRS_LLC_XID, |
| 210 | GPRS_LLC_UI, |
| 211 | }; |
| 212 | |
| 213 | struct gprs_llc_hdr_parsed { |
| 214 | uint8_t sapi; |
| 215 | uint8_t is_cmd:1, |
| 216 | ack_req:1, |
| 217 | is_encrypted:1; |
| 218 | uint32_t seq_rx; |
| 219 | uint32_t seq_tx; |
| 220 | uint32_t fcs; |
| 221 | uint32_t fcs_calc; |
| 222 | uint8_t *data; |
| 223 | uint16_t data_len; |
| 224 | uint16_t crc_length; |
| 225 | enum gprs_llc_cmd cmd; |
| 226 | }; |
| 227 | |
| 228 | |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 229 | /* BSSGP-UL-UNITDATA.ind */ |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 230 | int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv); |
Harald Welte | 1ae09c7 | 2010-05-13 19:22:55 +0200 | [diff] [blame] | 231 | |
| 232 | /* LL-UNITDATA.req */ |
Harald Welte | 56a0145 | 2010-05-31 22:12:30 +0200 | [diff] [blame] | 233 | int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command, |
Max | 8204010 | 2016-07-06 11:59:18 +0200 | [diff] [blame] | 234 | struct sgsn_mm_ctx *mmctx, bool encryptable); |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 235 | |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 236 | /* Chapter 7.2.1.2 LLGMM-RESET.req */ |
| 237 | int gprs_llgmm_reset(struct gprs_llc_llme *llme); |
Max | b997f84 | 2016-07-06 15:57:01 +0200 | [diff] [blame] | 238 | int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi, |
| 239 | struct gprs_llc_llme *llme); |
Harald Welte | 0c1a303 | 2011-10-16 18:49:05 +0200 | [diff] [blame] | 240 | |
Philipp | 4ac3aee | 2016-08-10 12:24:09 +0200 | [diff] [blame] | 241 | /* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */ |
| 242 | int gprs_ll_xid_req(struct gprs_llc_lle *lle, |
| 243 | struct gprs_llc_xid_field *l3_xid_field); |
| 244 | |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 245 | /* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */ |
| 246 | int gprs_llgmm_assign(struct gprs_llc_llme *llme, |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 247 | uint32_t old_tlli, uint32_t new_tlli); |
Max | 3955025 | 2016-06-28 17:39:20 +0200 | [diff] [blame] | 248 | int gprs_llgmm_unassign(struct gprs_llc_llme *llme); |
Harald Welte | 807a5d8 | 2010-06-01 11:53:01 +0200 | [diff] [blame] | 249 | |
Harald Welte | 496aee4 | 2010-06-30 19:59:55 +0200 | [diff] [blame] | 250 | int gprs_llc_init(const char *cipher_plugin_path); |
Harald Welte | 2e918a8 | 2010-05-18 12:20:12 +0200 | [diff] [blame] | 251 | int gprs_llc_vty_init(void); |
| 252 | |
Holger Hans Peter Freyther | faf1f64 | 2011-06-23 17:53:27 -0400 | [diff] [blame] | 253 | /** |
| 254 | * \short Check if N(U) should be considered a retransmit |
| 255 | * |
| 256 | * Implements the range check as of GSM 04.64 8.4.2 |
| 257 | * Receipt of unacknowledged information. |
| 258 | * |
| 259 | * @returns Returns 1 if (V(UR)-32) <= N(U) < V(UR) |
| 260 | * @param nu N(U) unconfirmed sequence number of the UI frame |
| 261 | * @param vur V(UR) unconfirmend received state variable |
| 262 | */ |
| 263 | static inline int gprs_llc_is_retransmit(uint16_t nu, uint16_t vur) |
| 264 | { |
| 265 | int delta = (vur - nu) & 0x1ff; |
| 266 | return 0 < delta && delta < 32; |
| 267 | } |
| 268 | |
Jacob Erlbeck | b492d39 | 2014-06-02 10:49:01 +0200 | [diff] [blame] | 269 | /* LLC low level functions */ |
Max | 5aa5196 | 2016-07-06 11:33:04 +0200 | [diff] [blame] | 270 | void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme); |
Jacob Erlbeck | b492d39 | 2014-06-02 10:49:01 +0200 | [diff] [blame] | 271 | |
| 272 | /* parse a GPRS LLC header, also check for invalid frames */ |
| 273 | int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp, |
| 274 | uint8_t *llc_hdr, int len); |
Max | 549ebc7 | 2016-11-18 14:07:04 +0100 | [diff] [blame] | 275 | void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph, struct gprs_llc_lle *lle); |
Jacob Erlbeck | b492d39 | 2014-06-02 10:49:01 +0200 | [diff] [blame] | 276 | int gprs_llc_fcs(uint8_t *data, unsigned int len); |
| 277 | |
Holger Hans Peter Freyther | 4299c05 | 2014-10-02 21:27:24 +0200 | [diff] [blame] | 278 | |
| 279 | /* LLME handling routines */ |
| 280 | struct llist_head *gprs_llme_list(void); |
| 281 | struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi); |
| 282 | |
| 283 | |
Harald Welte | 9b455bf | 2010-03-14 15:45:01 +0800 | [diff] [blame] | 284 | #endif |