blob: 8bc226781c32493f5b2558a5b8582aa74cb666a0 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001#ifndef _GPRS_LLC_H
2#define _GPRS_LLC_H
3
Harald Welteeaa614c2010-05-02 11:26:34 +02004#include <stdint.h>
Max82040102016-07-06 11:59:18 +02005#include <stdbool.h>
Harald Welte807a5d82010-06-01 11:53:01 +02006#include <openbsc/gprs_sgsn.h>
Philipp4ac3aee2016-08-10 12:24:09 +02007#include <openbsc/gprs_llc_xid.h>
Harald Welteeaa614c2010-05-02 11:26:34 +02008
Harald Welte9b455bf2010-03-14 15:45:01 +08009/* Section 4.7 LLC Layer Structure */
10enum 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 Welte10997d02010-05-03 12:28:12 +020021/* Section 6.4 Commands and Responses */
22enum 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 Welte9b455bf2010-03-14 15:45:01 +080031
Harald Welte0c1a3032011-10-16 18:49:05 +020032/* Section 6.4.1.6 / Table 6 */
33enum 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
Max549ebc72016-11-18 14:07:04 +010049extern const struct value_string gprs_llc_xid_type_names[];
50
Harald Welte1ae09c72010-05-13 19:22:55 +020051/* 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 */
53enum 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 Welte2e918a82010-05-18 12:20:12 +020084/* Section 4.5.2 Logical Link States + Annex C.2 */
Harald Welte807a5d82010-06-01 11:53:01 +020085enum 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
95enum gprs_llc_llme_state {
96 GPRS_LLMS_UNASSIGNED = 1, /* No TLLI yet */
97 GPRS_LLMS_ASSIGNED = 2, /* TLLI assigned */
Harald Welte2e918a82010-05-18 12:20:12 +020098};
99
Harald Welte1d9d9442010-06-03 07:11:04 +0200100/* Section 8.9.9 LLC layer parameter default values */
101struct 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 Welte2e918a82010-05-18 12:20:12 +0200113/* Section 4.7.1: Logical Link Entity: One per DLCI (TLLI + SAPI) */
114struct gprs_llc_lle {
115 struct llist_head list;
116
Harald Welte807a5d82010-06-01 11:53:01 +0200117 uint32_t sapi;
118
119 struct gprs_llc_llme *llme;
120
121 enum gprs_llc_lle_state state;
122
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200123 struct osmo_timer_list t200;
124 struct osmo_timer_list t201; /* wait for acknowledgement */
Harald Welte2e918a82010-05-18 12:20:12 +0200125
Harald Welte6bdee6a2010-05-30 21:51:58 +0200126 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 Welte2e918a82010-05-18 12:20:12 +0200132
Harald Welteabadd542013-06-21 14:06:18 +0200133 /* non-standard LLC state */
134 uint16_t vu_recv_last;
135 uint16_t vu_recv_duplicates;
136
Harald Welted07b4f92010-06-30 23:07:59 +0200137 /* 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 Welte2e918a82010-05-18 12:20:12 +0200145 unsigned int retrans_ctr;
Harald Welte1d9d9442010-06-03 07:11:04 +0200146
147 struct gprs_llc_params params;
Harald Welte807a5d82010-06-01 11:53:01 +0200148};
149
150#define NUM_SAPIS 16
151
152struct 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 Welte2e918a82010-05-18 12:20:12 +0200159
Harald Welted07b4f92010-06-30 23:07:59 +0200160 /* Crypto parameters */
161 enum gprs_ciph_algo algo;
Max5aa51962016-07-06 11:33:04 +0200162 uint8_t kc[16];
163 uint8_t cksn;
164 /* 3GPP TS 44.064 ยง 8.9.2: */
165 uint32_t iov_ui;
Harald Welted07b4f92010-06-30 23:07:59 +0200166
Harald Welte2e918a82010-05-18 12:20:12 +0200167 /* over which BSSGP BTS ctx do we need to transmit */
168 uint16_t bvci;
169 uint16_t nsei;
Harald Welte807a5d82010-06-01 11:53:01 +0200170 struct gprs_llc_lle lle[NUM_SAPIS];
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100171
Philipp4ac3aee2016-08-10 12:24:09 +0200172 /* 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
Philippf1f34362016-08-26 17:00:21 +0200179 /* 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 Erlbeck81ffb742015-01-23 11:33:51 +0100188 /* Internal management */
189 uint32_t age_timestamp;
Harald Welte2e918a82010-05-18 12:20:12 +0200190};
191
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100192#define GPRS_LLME_RESET_AGE (0)
193
Harald Welte807a5d82010-06-01 11:53:01 +0200194extern struct llist_head gprs_llc_llmes;
Harald Welte2e918a82010-05-18 12:20:12 +0200195
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200196/* LLC low level types */
197
198enum 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
213struct 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 Welte1ae09c72010-05-13 19:22:55 +0200229/* BSSGP-UL-UNITDATA.ind */
Harald Welte9b455bf2010-03-14 15:45:01 +0800230int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv);
Harald Welte1ae09c72010-05-13 19:22:55 +0200231
232/* LL-UNITDATA.req */
Harald Welte56a01452010-05-31 22:12:30 +0200233int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200234 struct sgsn_mm_ctx *mmctx, bool encryptable);
Harald Welte9b455bf2010-03-14 15:45:01 +0800235
Harald Welte0c1a3032011-10-16 18:49:05 +0200236/* Chapter 7.2.1.2 LLGMM-RESET.req */
237int gprs_llgmm_reset(struct gprs_llc_llme *llme);
Maxb997f842016-07-06 15:57:01 +0200238int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
239 struct gprs_llc_llme *llme);
Harald Welte0c1a3032011-10-16 18:49:05 +0200240
Philipp4ac3aee2016-08-10 12:24:09 +0200241/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
242int gprs_ll_xid_req(struct gprs_llc_lle *lle,
243 struct gprs_llc_xid_field *l3_xid_field);
244
Harald Welte807a5d82010-06-01 11:53:01 +0200245/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
246int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +0200247 uint32_t old_tlli, uint32_t new_tlli);
Max39550252016-06-28 17:39:20 +0200248int gprs_llgmm_unassign(struct gprs_llc_llme *llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200249
Harald Welte496aee42010-06-30 19:59:55 +0200250int gprs_llc_init(const char *cipher_plugin_path);
Harald Welte2e918a82010-05-18 12:20:12 +0200251int gprs_llc_vty_init(void);
252
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400253/**
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 */
263static 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 Erlbeckb492d392014-06-02 10:49:01 +0200269/* LLC low level functions */
Max5aa51962016-07-06 11:33:04 +0200270void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme);
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200271
272/* parse a GPRS LLC header, also check for invalid frames */
273int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
274 uint8_t *llc_hdr, int len);
Max549ebc72016-11-18 14:07:04 +0100275void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph, struct gprs_llc_lle *lle);
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200276int gprs_llc_fcs(uint8_t *data, unsigned int len);
277
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200278
279/* LLME handling routines */
280struct llist_head *gprs_llme_list(void);
281struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi);
282
283
Harald Welte9b455bf2010-03-14 15:45:01 +0800284#endif