blob: 5b122480484e1352eb08046f9dadb39a78495072 [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>
Neels Hofmeyr396f2e62017-09-04 15:13:25 +02006#include <osmocom/sgsn/gprs_sgsn.h>
7#include <osmocom/sgsn/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
Pau Espin Pedrol39aa9ee2019-08-12 16:23:04 +0200113/* 3GPP TS 44.064 § 4.7.1: Logical Link Entity: One per DLCI (TLLI + SAPI) */
Harald Welte2e918a82010-05-18 12:20:12 +0200114struct gprs_llc_lle {
115 struct llist_head list;
116
Harald Welte807a5d82010-06-01 11:53:01 +0200117 uint32_t sapi;
118
Pau Espin Pedrol39aa9ee2019-08-12 16:23:04 +0200119 struct gprs_llc_llme *llme; /* backpointer to the Logical Link Management Entity */
Harald Welte807a5d82010-06-01 11:53:01 +0200120
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 Welteaf779d22019-04-12 16:56:04 +0200148
149 /* Copy of the XID fields we have sent with the last
150 * network originated XID-Request. Since the phone
151 * may strip the optional fields in the confirmation
152 * we need to remeber those fields in order to be
153 * able to create the compression entity. */
154 struct llist_head *xid;
Harald Welte807a5d82010-06-01 11:53:01 +0200155};
156
157#define NUM_SAPIS 16
158
Pau Espin Pedrol39aa9ee2019-08-12 16:23:04 +0200159/* 3GPP TS 44.064 § 4.7.3: Logical Link Management Entity: One per TLLI */
Harald Welte807a5d82010-06-01 11:53:01 +0200160struct gprs_llc_llme {
161 struct llist_head list;
162
163 enum gprs_llc_llme_state state;
164
165 uint32_t tlli;
166 uint32_t old_tlli;
Harald Welte2e918a82010-05-18 12:20:12 +0200167
Harald Welted07b4f92010-06-30 23:07:59 +0200168 /* Crypto parameters */
169 enum gprs_ciph_algo algo;
Max5aa51962016-07-06 11:33:04 +0200170 uint8_t kc[16];
171 uint8_t cksn;
172 /* 3GPP TS 44.064 § 8.9.2: */
173 uint32_t iov_ui;
Harald Welted07b4f92010-06-30 23:07:59 +0200174
Harald Welte2e918a82010-05-18 12:20:12 +0200175 /* over which BSSGP BTS ctx do we need to transmit */
176 uint16_t bvci;
177 uint16_t nsei;
Harald Welte807a5d82010-06-01 11:53:01 +0200178 struct gprs_llc_lle lle[NUM_SAPIS];
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100179
Philippf1f34362016-08-26 17:00:21 +0200180 /* Compression entities */
181 struct {
182 /* In these two list_heads we will store the
183 * data and protocol compression entities,
184 * together with their compression states */
185 struct llist_head *proto;
186 struct llist_head *data;
187 } comp;
188
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100189 /* Internal management */
190 uint32_t age_timestamp;
Harald Welte2e918a82010-05-18 12:20:12 +0200191};
192
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100193#define GPRS_LLME_RESET_AGE (0)
194
Harald Welte807a5d82010-06-01 11:53:01 +0200195extern struct llist_head gprs_llc_llmes;
Harald Welte2e918a82010-05-18 12:20:12 +0200196
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200197/* LLC low level types */
198
199enum gprs_llc_cmd {
200 GPRS_LLC_NULL,
201 GPRS_LLC_RR,
202 GPRS_LLC_ACK,
203 GPRS_LLC_RNR,
204 GPRS_LLC_SACK,
205 GPRS_LLC_DM,
206 GPRS_LLC_DISC,
207 GPRS_LLC_UA,
208 GPRS_LLC_SABM,
209 GPRS_LLC_FRMR,
210 GPRS_LLC_XID,
211 GPRS_LLC_UI,
212};
213
214struct gprs_llc_hdr_parsed {
215 uint8_t sapi;
216 uint8_t is_cmd:1,
217 ack_req:1,
218 is_encrypted:1;
219 uint32_t seq_rx;
220 uint32_t seq_tx;
221 uint32_t fcs;
222 uint32_t fcs_calc;
223 uint8_t *data;
224 uint16_t data_len;
225 uint16_t crc_length;
226 enum gprs_llc_cmd cmd;
227};
228
229
Harald Welte1ae09c72010-05-13 19:22:55 +0200230/* BSSGP-UL-UNITDATA.ind */
Harald Welte9b455bf2010-03-14 15:45:01 +0800231int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv);
Harald Welte1ae09c72010-05-13 19:22:55 +0200232
233/* LL-UNITDATA.req */
Harald Welte56a01452010-05-31 22:12:30 +0200234int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200235 struct sgsn_mm_ctx *mmctx, bool encryptable);
Harald Welte9b455bf2010-03-14 15:45:01 +0800236
Harald Welte0c1a3032011-10-16 18:49:05 +0200237/* Chapter 7.2.1.2 LLGMM-RESET.req */
238int gprs_llgmm_reset(struct gprs_llc_llme *llme);
Maxb997f842016-07-06 15:57:01 +0200239int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
240 struct gprs_llc_llme *llme);
Harald Welte0c1a3032011-10-16 18:49:05 +0200241
Philipp4ac3aee2016-08-10 12:24:09 +0200242/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
243int gprs_ll_xid_req(struct gprs_llc_lle *lle,
244 struct gprs_llc_xid_field *l3_xid_field);
245
Harald Welte807a5d82010-06-01 11:53:01 +0200246/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
247int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +0200248 uint32_t old_tlli, uint32_t new_tlli);
Max39550252016-06-28 17:39:20 +0200249int gprs_llgmm_unassign(struct gprs_llc_llme *llme);
Harald Welte807a5d82010-06-01 11:53:01 +0200250
Harald Welte496aee42010-06-30 19:59:55 +0200251int gprs_llc_init(const char *cipher_plugin_path);
Harald Welte2e918a82010-05-18 12:20:12 +0200252int gprs_llc_vty_init(void);
253
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400254/**
255 * \short Check if N(U) should be considered a retransmit
256 *
257 * Implements the range check as of GSM 04.64 8.4.2
258 * Receipt of unacknowledged information.
259 *
260 * @returns Returns 1 if (V(UR)-32) <= N(U) < V(UR)
261 * @param nu N(U) unconfirmed sequence number of the UI frame
262 * @param vur V(UR) unconfirmend received state variable
263 */
264static inline int gprs_llc_is_retransmit(uint16_t nu, uint16_t vur)
265{
266 int delta = (vur - nu) & 0x1ff;
267 return 0 < delta && delta < 32;
268}
269
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200270/* LLC low level functions */
Max5aa51962016-07-06 11:33:04 +0200271void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme);
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200272
273/* parse a GPRS LLC header, also check for invalid frames */
274int gprs_llc_hdr_parse(struct gprs_llc_hdr_parsed *ghp,
275 uint8_t *llc_hdr, int len);
Max549ebc72016-11-18 14:07:04 +0100276void gprs_llc_hdr_dump(struct gprs_llc_hdr_parsed *gph, struct gprs_llc_lle *lle);
Jacob Erlbeckb492d392014-06-02 10:49:01 +0200277int gprs_llc_fcs(uint8_t *data, unsigned int len);
278
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200279
280/* LLME handling routines */
281struct llist_head *gprs_llme_list(void);
282struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi);
283
284
Harald Welte9b455bf2010-03-14 15:45:01 +0800285#endif