blob: 453ded5db29f4801987c7f9d821dcf90fd4e5720 [file] [log] [blame]
Harald Welte9b455bf2010-03-14 15:45:01 +08001/* GPRS LLC protocol implementation as per 3GPP TS 04.64 */
2
Harald Weltea2665542010-05-02 09:28:11 +02003/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
Harald Welte9b455bf2010-03-14 15:45:01 +08004 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +01008 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
Harald Welte9b455bf2010-03-14 15:45:01 +080010 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010015 * GNU Affero General Public License for more details.
Harald Welte9b455bf2010-03-14 15:45:01 +080016 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte9b455bf2010-03-14 15:45:01 +080019 *
20 */
21
22#include <errno.h>
Harald Welteeaa614c2010-05-02 11:26:34 +020023#include <stdint.h>
Max82040102016-07-06 11:59:18 +020024#include <stdbool.h>
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +010025#include <inttypes.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080026
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010027#include <osmocom/core/msgb.h>
28#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/timer.h>
30#include <osmocom/core/talloc.h>
Alexander Couzens4e699a92016-07-05 11:04:27 +020031#include <osmocom/core/rate_ctr.h>
Harald Welteea34a4e2012-06-16 14:59:56 +080032#include <osmocom/gprs/gprs_bssgp.h>
Neels Hofmeyree6cfdc2017-07-13 02:03:50 +020033#include <osmocom/gsm/gsm_utils.h>
Harald Weltea2665542010-05-02 09:28:11 +020034
Neels Hofmeyr396f2e62017-09-04 15:13:25 +020035#include <osmocom/sgsn/debug.h>
36#include <osmocom/sgsn/gprs_sgsn.h>
37#include <osmocom/sgsn/gprs_gmm.h>
38#include <osmocom/sgsn/gprs_llc.h>
39#include <osmocom/sgsn/crc24.h>
40#include <osmocom/sgsn/sgsn.h>
41#include <osmocom/sgsn/gprs_llc_xid.h>
42#include <osmocom/sgsn/gprs_sndcp_comp.h>
43#include <osmocom/sgsn/gprs_sndcp.h>
Harald Welte9b455bf2010-03-14 15:45:01 +080044
Eric2f898262021-05-19 18:57:50 +020045#include <osmocom/crypt/kdf.h>
46
Pau Espin Pedrol5b6c4b82019-08-14 16:08:15 +020047const struct value_string gprs_llc_llme_state_names[] = {
48 { GPRS_LLMS_UNASSIGNED, "UNASSIGNED" },
49 { GPRS_LLMS_ASSIGNED, "ASSIGNED" },
50 { 0, NULL }
51};
52
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +020053static struct gprs_llc_llme *llme_alloc(uint32_t tlli);
Philipp4ac3aee2016-08-10 12:24:09 +020054static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
55 int command);
Harald Welte1f60bbe2019-04-23 23:21:14 +020056static int gprs_llc_tx_dm(struct gprs_llc_lle *lle);
Philipp4ac3aee2016-08-10 12:24:09 +020057static int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi,
58 int command, enum gprs_llc_u_cmd u_cmd, int pf_bit);
59
60/* BEGIN XID RELATED */
61
62/* Generate XID message */
63static int gprs_llc_generate_xid(uint8_t *bytes, int bytes_len,
64 struct gprs_llc_xid_field *l3_xid_field,
Harald Welteaf779d22019-04-12 16:56:04 +020065 struct gprs_llc_lle *lle)
Philipp4ac3aee2016-08-10 12:24:09 +020066{
67 /* Note: Called by gprs_ll_xid_req() */
68
69 LLIST_HEAD(xid_fields);
70
71 struct gprs_llc_xid_field xid_version;
72 struct gprs_llc_xid_field xid_n201u;
73 struct gprs_llc_xid_field xid_n201i;
Harald Welte41461212019-04-12 17:01:32 +020074 uint16_t n201_u, n201_i;
Philipp4ac3aee2016-08-10 12:24:09 +020075
76 xid_version.type = GPRS_LLC_XID_T_VERSION;
77 xid_version.data = (uint8_t *) "\x00";
78 xid_version.data_len = 1;
79
Harald Welte41461212019-04-12 17:01:32 +020080 n201_u = htons(lle->params.n201_u);
Philipp4ac3aee2016-08-10 12:24:09 +020081 xid_n201u.type = GPRS_LLC_XID_T_N201_U;
Harald Welte41461212019-04-12 17:01:32 +020082 xid_n201u.data = (uint8_t *) &n201_u;
Philipp4ac3aee2016-08-10 12:24:09 +020083 xid_n201u.data_len = 2;
84
Harald Welte41461212019-04-12 17:01:32 +020085 n201_i = htons(lle->params.n201_i);
Philipp4ac3aee2016-08-10 12:24:09 +020086 xid_n201i.type = GPRS_LLC_XID_T_N201_I;
Harald Welte41461212019-04-12 17:01:32 +020087 xid_n201i.data = (uint8_t *) &n201_i;
Philipp4ac3aee2016-08-10 12:24:09 +020088 xid_n201i.data_len = 2;
89
90 /* Add locally managed XID Fields */
Philipp4ac3aee2016-08-10 12:24:09 +020091 llist_add(&xid_version.list, &xid_fields);
Philippf788d932016-12-05 12:44:19 +010092 llist_add(&xid_n201u.list, &xid_fields);
93 llist_add(&xid_n201i.list, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +020094
95 /* Append layer 3 XID field (if present) */
96 if (l3_xid_field) {
97 /* Enforce layer 3 XID type (just to be sure) */
98 l3_xid_field->type = GPRS_LLC_XID_T_L3_PAR;
99
100 /* Add Layer 3 XID field to the list */
101 llist_add(&l3_xid_field->list, &xid_fields);
102 }
103
104 /* Store generated XID for later reference */
Harald Welteaf779d22019-04-12 16:56:04 +0200105 talloc_free(lle->xid);
106 lle->xid = gprs_llc_copy_xid(lle->llme, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +0200107
108 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
109}
110
111/* Generate XID message that will cause the GMM to reset */
112static int gprs_llc_generate_xid_for_gmm_reset(uint8_t *bytes,
113 int bytes_len, uint32_t iov_ui,
Harald Welteaf779d22019-04-12 16:56:04 +0200114 struct gprs_llc_lle *lle)
Philipp4ac3aee2016-08-10 12:24:09 +0200115{
116 /* Called by gprs_llgmm_reset() and
117 * gprs_llgmm_reset_oldmsg() */
118
119 LLIST_HEAD(xid_fields);
120
121 struct gprs_llc_xid_field xid_reset;
122 struct gprs_llc_xid_field xid_iovui;
123
124 /* First XID component must be RESET */
125 xid_reset.type = GPRS_LLC_XID_T_RESET;
126 xid_reset.data = NULL;
127 xid_reset.data_len = 0;
128
129 /* Add new IOV-UI */
130 xid_iovui.type = GPRS_LLC_XID_T_IOV_UI;
131 xid_iovui.data = (uint8_t *) & iov_ui;
132 xid_iovui.data_len = 4;
133
134 /* Add locally managed XID Fields */
135 llist_add(&xid_iovui.list, &xid_fields);
136 llist_add(&xid_reset.list, &xid_fields);
137
138 /* Store generated XID for later reference */
Harald Welteaf779d22019-04-12 16:56:04 +0200139 talloc_free(lle->xid);
140 lle->xid = gprs_llc_copy_xid(lle->llme, &xid_fields);
Philipp4ac3aee2016-08-10 12:24:09 +0200141
142 return gprs_llc_compile_xid(bytes, bytes_len, &xid_fields);
143}
144
145/* Process an incoming XID confirmation */
146static int gprs_llc_process_xid_conf(uint8_t *bytes, int bytes_len,
147 struct gprs_llc_lle *lle)
148{
149 /* Note: This function handles the response of a network originated
Philipp532480a2016-12-23 11:05:11 +0100150 * XID-Request. There XID messages reflected by the MS are analyzed
Philipp4ac3aee2016-08-10 12:24:09 +0200151 * and processed here. The caller is called by rx_llc_xid(). */
152
153 struct llist_head *xid_fields;
154 struct gprs_llc_xid_field *xid_field;
Philippf1f34362016-08-26 17:00:21 +0200155 struct gprs_llc_xid_field *xid_field_request;
156 struct gprs_llc_xid_field *xid_field_request_l3 = NULL;
157
158 /* Pick layer3 XID from the XID request we have sent last */
Harald Welteaf779d22019-04-12 16:56:04 +0200159 if (lle->xid) {
160 llist_for_each_entry(xid_field_request, lle->xid, list) {
Philippf1f34362016-08-26 17:00:21 +0200161 if (xid_field_request->type == GPRS_LLC_XID_T_L3_PAR)
162 xid_field_request_l3 = xid_field_request;
163 }
164 }
Philipp4ac3aee2016-08-10 12:24:09 +0200165
166 /* Parse and analyze XID-Response */
167 xid_fields = gprs_llc_parse_xid(NULL, bytes, bytes_len);
168
169 if (xid_fields) {
170
171 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
172 llist_for_each_entry(xid_field, xid_fields, list) {
173
174 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
Philippf1f34362016-08-26 17:00:21 +0200175 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR &&
176 xid_field_request_l3) {
177 sndcp_sn_xid_conf(xid_field,
178 xid_field_request_l3, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200179 }
180
181 /* Process LLC-XID fields: */
182 else {
183
184 /* FIXME: Do something more useful with the
185 * echoed XID-Information. Currently we
186 * just ignore the response completely and
187 * by doing so we blindly accept any changes
188 * the MS might have done to the our XID
189 * inquiry. There is a remainig risk of
190 * malfunction! */
191 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100192 "Ignoring XID-Field: XID: type %s, data_len=%d, data=%s\n",
193 get_value_string(gprs_llc_xid_type_names,
194 xid_field->type),
195 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200196 osmo_hexdump_nospc(xid_field->data,
197 xid_field->data_len));
198 }
199 }
200 talloc_free(xid_fields);
201 }
202
203 /* Flush pending XID fields */
Harald Welteaf779d22019-04-12 16:56:04 +0200204 talloc_free(lle->xid);
205 lle->xid = NULL;
Philipp4ac3aee2016-08-10 12:24:09 +0200206
207 return 0;
208}
209
210/* Process an incoming XID indication and generate an appropiate response */
211static int gprs_llc_process_xid_ind(uint8_t *bytes_request,
212 int bytes_request_len,
213 uint8_t *bytes_response,
214 int bytes_response_maxlen,
215 struct gprs_llc_lle *lle)
216{
217 /* Note: This function computes the response that is sent back to the
Philipp532480a2016-12-23 11:05:11 +0100218 * MS when a mobile originated XID is received. The function is
Philipp4ac3aee2016-08-10 12:24:09 +0200219 * called by rx_llc_xid() */
220
221 int rc = -EINVAL;
222
223 struct llist_head *xid_fields;
224 struct llist_head *xid_fields_response;
225
226 struct gprs_llc_xid_field *xid_field;
227 struct gprs_llc_xid_field *xid_field_response;
228
Philipp4ac3aee2016-08-10 12:24:09 +0200229 /* Parse and analyze XID-Request */
230 xid_fields =
231 gprs_llc_parse_xid(lle->llme, bytes_request, bytes_request_len);
232 if (xid_fields) {
233 xid_fields_response = talloc_zero(lle->llme, struct llist_head);
234 INIT_LLIST_HEAD(xid_fields_response);
235 gprs_llc_dump_xid_fields(xid_fields, LOGL_DEBUG);
236
237 /* Process LLC-XID fields: */
238 llist_for_each_entry(xid_field, xid_fields, list) {
239
240 if (xid_field->type != GPRS_LLC_XID_T_L3_PAR) {
241 /* FIXME: Check the incoming XID parameters for
242 * for validity. Currently we just blindly
243 * accept all XID fields by just echoing them.
244 * There is a remaining risk of malfunction
Philipp532480a2016-12-23 11:05:11 +0100245 * when a MS submits values which defer from
Philipp4ac3aee2016-08-10 12:24:09 +0200246 * the default! */
247 LOGP(DLLC, LOGL_NOTICE,
Max549ebc72016-11-18 14:07:04 +0100248 "Echoing XID-Field: XID: type %s, data_len=%d, data=%s\n",
249 get_value_string(gprs_llc_xid_type_names,
250 xid_field->type),
251 xid_field->data_len,
Philipp4ac3aee2016-08-10 12:24:09 +0200252 osmo_hexdump_nospc(xid_field->data,
253 xid_field->data_len));
254 xid_field_response =
255 gprs_llc_dup_xid_field
256 (lle->llme, xid_field);
257 llist_add(&xid_field_response->list,
258 xid_fields_response);
259 }
260 }
261
Philippf1f34362016-08-26 17:00:21 +0200262 /* Forward SNDCP-XID fields to Layer 3 (SNDCP) */
263 llist_for_each_entry(xid_field, xid_fields, list) {
264 if (xid_field->type == GPRS_LLC_XID_T_L3_PAR) {
265
266 xid_field_response =
267 talloc_zero(lle->llme,
268 struct gprs_llc_xid_field);
269 rc = sndcp_sn_xid_ind(xid_field,
270 xid_field_response, lle);
271 if (rc == 0)
272 llist_add(&xid_field_response->list,
273 xid_fields_response);
274 else
275 talloc_free(xid_field_response);
276 }
277 }
278
Philipp4ac3aee2016-08-10 12:24:09 +0200279 rc = gprs_llc_compile_xid(bytes_response,
280 bytes_response_maxlen,
281 xid_fields_response);
282 talloc_free(xid_fields_response);
283 talloc_free(xid_fields);
284 }
285
286 return rc;
287}
288
Philipp532480a2016-12-23 11:05:11 +0100289/* Dispatch XID indications and responses comming from the MS */
Philipp4ac3aee2016-08-10 12:24:09 +0200290static void rx_llc_xid(struct gprs_llc_lle *lle,
291 struct gprs_llc_hdr_parsed *gph)
292{
293 uint8_t response[1024];
294 int response_len;
295
296 /* FIXME: 8.5.3.3: check if XID is invalid */
297 if (gph->is_cmd) {
298 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100299 "Received XID indication from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200300
301 struct msgb *resp;
302 uint8_t *xid;
303 resp = msgb_alloc_headroom(4096, 1024, "LLC_XID");
304
305 response_len =
306 gprs_llc_process_xid_ind(gph->data, gph->data_len,
307 response, sizeof(response),
308 lle);
Philippf1f34362016-08-26 17:00:21 +0200309 if (response_len < 0) {
310 LOGP(DLLC, LOGL_ERROR,
311 "invalid XID indication received!\n");
312 } else {
313 xid = msgb_put(resp, response_len);
314 memcpy(xid, response, response_len);
315 }
Philipp4ac3aee2016-08-10 12:24:09 +0200316 gprs_llc_tx_xid(lle, resp, 0);
317 } else {
318 LOGP(DLLC, LOGL_NOTICE,
Philipp532480a2016-12-23 11:05:11 +0100319 "Received XID confirmation from MS.\n");
Philipp4ac3aee2016-08-10 12:24:09 +0200320 gprs_llc_process_xid_conf(gph->data, gph->data_len, lle);
321 /* FIXME: if we had sent a XID reset, send
322 * LLGMM-RESET.conf to GMM */
323 }
324}
325
Philipp4ac3aee2016-08-10 12:24:09 +0200326/* Set of LL-XID negotiation (See also: TS 101 351, Section 7.2.2.4) */
327int gprs_ll_xid_req(struct gprs_llc_lle *lle,
328 struct gprs_llc_xid_field *l3_xid_field)
329{
330 /* Note: This functions is calle from gprs_sndcp.c */
331
332 uint8_t xid_bytes[1024];;
333 int xid_bytes_len;
334 uint8_t *xid;
335 struct msgb *msg;
Max549ebc72016-11-18 14:07:04 +0100336 const char *ftype;
Philipp4ac3aee2016-08-10 12:24:09 +0200337
338 /* Generate XID */
339 xid_bytes_len =
Harald Welteaf779d22019-04-12 16:56:04 +0200340 gprs_llc_generate_xid(xid_bytes, sizeof(xid_bytes), l3_xid_field, lle);
Philipp4ac3aee2016-08-10 12:24:09 +0200341
342 /* Only perform XID sending if the XID message contains something */
343 if (xid_bytes_len > 0) {
344 /* Transmit XID bytes */
345 msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
346 xid = msgb_put(msg, xid_bytes_len);
347 memcpy(xid, xid_bytes, xid_bytes_len);
Max549ebc72016-11-18 14:07:04 +0100348 if (l3_xid_field)
349 ftype = get_value_string(gprs_llc_xid_type_names,
350 l3_xid_field->type);
351 else
352 ftype = "NULL";
353 LOGP(DLLC, LOGL_NOTICE, "Sending XID type %s (%d bytes) request"
Philipp532480a2016-12-23 11:05:11 +0100354 " to MS...\n", ftype, xid_bytes_len);
Philipp4ac3aee2016-08-10 12:24:09 +0200355 gprs_llc_tx_xid(lle, msg, 1);
356 } else {
357 LOGP(DLLC, LOGL_ERROR,
358 "XID-Message generation failed, XID not sent!\n");
359 return -EINVAL;
360 }
361
362 return 0;
363}
364/* END XID RELATED */
365
366
367
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200368
Harald Weltefaa70ff2012-06-17 09:31:16 +0800369/* Entry function from upper level (LLC), asking us to transmit a BSSGP PDU
370 * to a remote MS (identified by TLLI) at a BTS identified by its BVCI and NSEI */
371static int _bssgp_tx_dl_ud(struct msgb *msg, struct sgsn_mm_ctx *mmctx)
372{
373 struct bssgp_dl_ud_par dup;
374 const uint8_t qos_profile_default[3] = { 0x00, 0x00, 0x20 };
375
Harald Welte8c004962012-07-04 21:53:12 +0200376 memset(&dup, 0, sizeof(dup));
377 /* before we have received some identity from the MS, we might
378 * not yet have a MMC context (e.g. XID negotiation of primarly
Philipp4ac3aee2016-08-10 12:24:09 +0200379 * LLC connection from GMM sapi). */
Harald Welte8c004962012-07-04 21:53:12 +0200380 if (mmctx) {
Alexander Couzensd3c3dde2020-09-18 18:28:33 +0200381 /* In rare cases the LLME is NULL in those cases don't
382 * use the mm radio capabilities */
Harald Welte8c004962012-07-04 21:53:12 +0200383 dup.imsi = mmctx->imsi;
Alexander Couzensd3c3dde2020-09-18 18:28:33 +0200384 if (mmctx->gb.llme) {
385 dup.drx_parms = mmctx->drx_parms;
386 dup.ms_ra_cap.len = mmctx->ms_radio_access_capa.len;
387 dup.ms_ra_cap.v = mmctx->ms_radio_access_capa.buf;
Holger Hans Peter Freyther7e0fec12013-07-29 10:09:12 +0200388
Alexander Couzensd3c3dde2020-09-18 18:28:33 +0200389 /* make sure we only send it to the right llme */
390 if (!(msgb_tlli(msg) == mmctx->gb.llme->tlli
391 || msgb_tlli(msg) == mmctx->gb.llme->old_tlli)) {
392 LOGP(DLLC, LOGL_ERROR,
393 "_bssgp_tx_dl_ud(): Attempt to send Downlink Unitdata to wrong LLME:"
394 " msgb_tlli=0x%x mmctx->gb.llme->tlli=0x%x ->old_tlli=0x%x\n",
395 msgb_tlli(msg), mmctx->gb.llme->tlli, mmctx->gb.llme->old_tlli);
396 msgb_free(msg);
397 return -EINVAL;
398 }
Neels Hofmeyr188a91c2017-12-27 17:29:04 +0100399 }
Harald Welte8c004962012-07-04 21:53:12 +0200400 }
Harald Weltefaa70ff2012-06-17 09:31:16 +0800401 memcpy(&dup.qos_profile, qos_profile_default,
402 sizeof(qos_profile_default));
403
Harald Weltece95b272012-06-17 13:04:02 +0800404 return bssgp_tx_dl_ud(msg, 1000, &dup);
Harald Weltefaa70ff2012-06-17 09:31:16 +0800405}
406
407
Harald Welte1d9d9442010-06-03 07:11:04 +0200408/* Section 8.9.9 LLC layer parameter default values */
Daniel Willmann46d13262014-06-27 17:05:48 +0200409static const struct gprs_llc_params llc_default_params[NUM_SAPIS] = {
Harald Welte1d9d9442010-06-03 07:11:04 +0200410 [1] = {
411 .t200_201 = 5,
412 .n200 = 3,
413 .n201_u = 400,
414 },
415 [2] = {
416 .t200_201 = 5,
417 .n200 = 3,
418 .n201_u = 270,
419 },
420 [3] = {
421 .iov_i_exp = 27,
422 .t200_201 = 5,
423 .n200 = 3,
424 .n201_u = 500,
425 .n201_i = 1503,
426 .mD = 1520,
427 .mU = 1520,
428 .kD = 16,
429 .kU = 16,
430 },
431 [5] = {
432 .iov_i_exp = 27,
433 .t200_201 = 10,
434 .n200 = 3,
435 .n201_u = 500,
436 .n201_i = 1503,
437 .mD = 760,
438 .mU = 760,
439 .kD = 8,
440 .kU = 8,
441 },
442 [7] = {
443 .t200_201 = 20,
444 .n200 = 3,
445 .n201_u = 270,
446 },
447 [8] = {
448 .t200_201 = 20,
449 .n200 = 3,
450 .n201_u = 270,
451 },
452 [9] = {
453 .iov_i_exp = 27,
454 .t200_201 = 20,
455 .n200 = 3,
456 .n201_u = 500,
457 .n201_i = 1503,
458 .mD = 380,
459 .mU = 380,
460 .kD = 4,
461 .kU = 4,
462 },
463 [11] = {
464 .iov_i_exp = 27,
465 .t200_201 = 40,
466 .n200 = 3,
467 .n201_u = 500,
468 .n201_i = 1503,
469 .mD = 190,
470 .mU = 190,
471 .kD = 2,
472 .kU = 2,
473 },
474};
475
Harald Welte807a5d82010-06-01 11:53:01 +0200476LLIST_HEAD(gprs_llc_llmes);
Harald Weltea2665542010-05-02 09:28:11 +0200477void *llc_tall_ctx;
478
479/* lookup LLC Entity based on DLCI (TLLI+SAPI tuple) */
Holger Hans Peter Freyther012a7ee2013-07-29 09:06:46 +0200480static struct gprs_llc_lle *lle_by_tlli_sapi(const uint32_t tlli, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200481{
Harald Welte807a5d82010-06-01 11:53:01 +0200482 struct gprs_llc_llme *llme;
Harald Weltea2665542010-05-02 09:28:11 +0200483
Harald Welte807a5d82010-06-01 11:53:01 +0200484 llist_for_each_entry(llme, &gprs_llc_llmes, list) {
485 if (llme->tlli == tlli || llme->old_tlli == tlli)
486 return &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200487 }
488 return NULL;
489}
490
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200491struct gprs_llc_lle *gprs_lle_get_or_create(const uint32_t tlli, uint8_t sapi)
492{
493 struct gprs_llc_llme *llme;
494 struct gprs_llc_lle *lle;
495
496 lle = lle_by_tlli_sapi(tlli, sapi);
497 if (lle)
498 return lle;
499
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200500 LOGP(DLLC, LOGL_NOTICE, "LLC: unknown TLLI 0x%08x, "
501 "creating LLME on the fly\n", tlli);
502 llme = llme_alloc(tlli);
503 lle = &llme->lle[sapi];
504 return lle;
505}
506
507struct llist_head *gprs_llme_list(void)
508{
509 return &gprs_llc_llmes;
510}
511
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200512/* lookup LLC Entity for RX based on DLCI (TLLI+SAPI tuple) */
513static struct gprs_llc_lle *lle_for_rx_by_tlli_sapi(const uint32_t tlli,
514 uint8_t sapi, enum gprs_llc_cmd cmd)
515{
516 struct gprs_llc_lle *lle;
517
518 /* We already know about this TLLI */
519 lle = lle_by_tlli_sapi(tlli, sapi);
520 if (lle)
521 return lle;
522
523 /* Maybe it is a routing area update but we already know this sapi? */
524 if (gprs_tlli_type(tlli) == TLLI_FOREIGN) {
Jacob Erlbeck3fbf0a32016-01-04 18:43:32 +0100525 lle = lle_by_tlli_sapi(tlli, sapi);
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200526 if (lle) {
527 LOGP(DLLC, LOGL_NOTICE,
528 "LLC RX: Found a local entry for TLLI 0x%08x\n",
529 tlli);
530 return lle;
531 }
532 }
533
534 /* 7.2.1.1 LLC belonging to unassigned TLLI+SAPI shall be discarded,
535 * except UID and XID frames with SAPI=1 */
536 if (sapi == GPRS_SAPI_GMM &&
537 (cmd == GPRS_LLC_XID || cmd == GPRS_LLC_UI)) {
538 struct gprs_llc_llme *llme;
539 /* FIXME: don't use the TLLI but the 0xFFFF unassigned? */
540 llme = llme_alloc(tlli);
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +0100541 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLC RX: unknown TLLI 0x%08x, "
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200542 "creating LLME on the fly\n", tlli);
543 lle = &llme->lle[sapi];
544 return lle;
545 }
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +0200546
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200547 LOGP(DLLC, LOGL_NOTICE,
548 "unknown TLLI(0x%08x)/SAPI(%d): Silently dropping\n",
549 tlli, sapi);
550 return NULL;
551}
552
Harald Welte1d9d9442010-06-03 07:11:04 +0200553static void lle_init(struct gprs_llc_llme *llme, uint8_t sapi)
Harald Weltea2665542010-05-02 09:28:11 +0200554{
Harald Welte807a5d82010-06-01 11:53:01 +0200555 struct gprs_llc_lle *lle = &llme->lle[sapi];
Harald Weltea2665542010-05-02 09:28:11 +0200556
Harald Welte807a5d82010-06-01 11:53:01 +0200557 lle->llme = llme;
558 lle->sapi = sapi;
559 lle->state = GPRS_LLES_UNASSIGNED;
560
Harald Welte1d9d9442010-06-03 07:11:04 +0200561 /* Initialize according to parameters */
562 memcpy(&lle->params, &llc_default_params[sapi], sizeof(lle->params));
Harald Welte807a5d82010-06-01 11:53:01 +0200563}
564
565static struct gprs_llc_llme *llme_alloc(uint32_t tlli)
566{
567 struct gprs_llc_llme *llme;
568 uint32_t i;
569
570 llme = talloc_zero(llc_tall_ctx, struct gprs_llc_llme);
571 if (!llme)
Harald Weltea2665542010-05-02 09:28:11 +0200572 return NULL;
573
Harald Welte807a5d82010-06-01 11:53:01 +0200574 llme->tlli = tlli;
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +0200575 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte807a5d82010-06-01 11:53:01 +0200576 llme->state = GPRS_LLMS_UNASSIGNED;
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100577 llme->age_timestamp = GPRS_LLME_RESET_AGE;
Max5aa51962016-07-06 11:33:04 +0200578 llme->cksn = GSM_KEY_SEQ_INVAL;
Harald Weltea2665542010-05-02 09:28:11 +0200579
Harald Welte807a5d82010-06-01 11:53:01 +0200580 for (i = 0; i < ARRAY_SIZE(llme->lle); i++)
581 lle_init(llme, i);
582
583 llist_add(&llme->list, &gprs_llc_llmes);
584
Philippf1f34362016-08-26 17:00:21 +0200585 llme->comp.proto = gprs_sndcp_comp_alloc(llme);
586 llme->comp.data = gprs_sndcp_comp_alloc(llme);
587
Harald Welte807a5d82010-06-01 11:53:01 +0200588 return llme;
Harald Weltea2665542010-05-02 09:28:11 +0200589}
590
Harald Weltef7fef482010-06-28 22:18:26 +0200591static void llme_free(struct gprs_llc_llme *llme)
592{
Oliver Smithf7642852021-12-07 13:16:17 +0100593 gprs_sndcp_sm_deactivate_ind_by_llme(llme);
Philippf1f34362016-08-26 17:00:21 +0200594 gprs_sndcp_comp_free(llme->comp.proto);
595 gprs_sndcp_comp_free(llme->comp.data);
Harald Weltef7fef482010-06-28 22:18:26 +0200596 llist_del(&llme->list);
597 talloc_free(llme);
598}
599
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200600#if 0
601/* FIXME: Unused code... */
Harald Welte9b455bf2010-03-14 15:45:01 +0800602static void t200_expired(void *data)
603{
604 struct gprs_llc_lle *lle = data;
605
606 /* 8.5.1.3: Expiry of T200 */
607
Harald Welte1d9d9442010-06-03 07:11:04 +0200608 if (lle->retrans_ctr >= lle->params.n200) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800609 /* FIXME: LLGM-STATUS-IND, LL-RELEASE-IND/CNF */
Harald Welte807a5d82010-06-01 11:53:01 +0200610 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800611 }
612
613 switch (lle->state) {
Harald Welte807a5d82010-06-01 11:53:01 +0200614 case GPRS_LLES_LOCAL_EST:
Harald Welte1ae09c72010-05-13 19:22:55 +0200615 /* FIXME: retransmit SABM */
616 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800617 lle->retrans_ctr++;
618 break;
Harald Welte807a5d82010-06-01 11:53:01 +0200619 case GPRS_LLES_LOCAL_REL:
Harald Welte1ae09c72010-05-13 19:22:55 +0200620 /* FIXME: retransmit DISC */
621 /* FIXME: re-start T200 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800622 lle->retrans_ctr++;
623 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200624 default:
625 LOGP(DLLC, LOGL_ERROR, "LLC unhandled state: %d\n", lle->state);
626 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800627 }
628
629}
630
631static void t201_expired(void *data)
632{
633 struct gprs_llc_lle *lle = data;
634
Harald Welte1d9d9442010-06-03 07:11:04 +0200635 if (lle->retrans_ctr < lle->params.n200) {
Harald Welte1ae09c72010-05-13 19:22:55 +0200636 /* FIXME: transmit apropriate supervisory frame (8.6.4.1) */
637 /* FIXME: set timer T201 */
Harald Welte9b455bf2010-03-14 15:45:01 +0800638 lle->retrans_ctr++;
639 }
640}
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200641#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800642
Harald Welte10997d02010-05-03 12:28:12 +0200643int gprs_llc_tx_u(struct msgb *msg, uint8_t sapi, int command,
644 enum gprs_llc_u_cmd u_cmd, int pf_bit)
645{
646 uint8_t *fcs, *llch;
647 uint8_t addr, ctrl;
648 uint32_t fcs_calc;
649
650 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
651
652 /* Address Field */
653 addr = sapi & 0xf;
654 if (command)
655 addr |= 0x40;
656
657 /* 6.3 Figure 8 */
658 ctrl = 0xe0 | u_cmd;
659 if (pf_bit)
660 ctrl |= 0x10;
661
662 /* prepend LLC UI header */
663 llch = msgb_push(msg, 2);
664 llch[0] = addr;
665 llch[1] = ctrl;
666
667 /* append FCS to end of frame */
668 fcs = msgb_put(msg, 3);
669 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
670 fcs[0] = fcs_calc & 0xff;
671 fcs[1] = (fcs_calc >> 8) & 0xff;
672 fcs[2] = (fcs_calc >> 16) & 0xff;
673
674 /* Identifiers passed down: (BVCI, NSEI) */
675
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200676 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_LLC_DL_PACKETS));
677 rate_ctr_add(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_LLC_DL_BYTES), msg->len);
Alexander Couzens4e699a92016-07-05 11:04:27 +0200678
Harald Welte1ae09c72010-05-13 19:22:55 +0200679 /* Send BSSGP-DL-UNITDATA.req */
Harald Welteb1fd9022012-06-17 12:16:31 +0800680 return _bssgp_tx_dl_ud(msg, NULL);
Harald Welte10997d02010-05-03 12:28:12 +0200681}
682
683/* Send XID response to LLE */
Harald Welte0c1a3032011-10-16 18:49:05 +0200684static int gprs_llc_tx_xid(struct gprs_llc_lle *lle, struct msgb *msg,
685 int command)
Harald Welte10997d02010-05-03 12:28:12 +0200686{
687 /* copy identifiers from LLE to ensure lower layers can route */
Harald Welte807a5d82010-06-01 11:53:01 +0200688 msgb_tlli(msg) = lle->llme->tlli;
689 msgb_bvci(msg) = lle->llme->bvci;
690 msgb_nsei(msg) = lle->llme->nsei;
Harald Welte10997d02010-05-03 12:28:12 +0200691
Harald Welte0c1a3032011-10-16 18:49:05 +0200692 return gprs_llc_tx_u(msg, lle->sapi, command, GPRS_LLC_U_XID, 1);
Harald Welte10997d02010-05-03 12:28:12 +0200693}
694
Harald Welte1f60bbe2019-04-23 23:21:14 +0200695static int gprs_llc_tx_dm(struct gprs_llc_lle *lle)
696{
697 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_DM");
698
699 /* copy identifiers from LLE to ensure lower layers can route */
700 msgb_tlli(msg) = lle->llme->tlli;
701 msgb_bvci(msg) = lle->llme->bvci;
702 msgb_nsei(msg) = lle->llme->nsei;
703
704 return gprs_llc_tx_u(msg, lle->sapi, 0, GPRS_LLC_U_DM_RESP, 1);
705}
706
Max1de15912016-07-11 12:42:12 +0200707/* encrypt information field + FCS, if needed! */
708static int apply_gea(struct gprs_llc_lle *lle, uint16_t crypt_len, uint16_t nu,
709 uint32_t oc, uint8_t sapi, uint8_t *fcs, uint8_t *data)
710{
711 uint8_t cipher_out[GSM0464_CIPH_MAX_BLOCK];
712
713 if (lle->llme->algo == GPRS_ALGO_GEA0)
714 return -EINVAL;
715
716 /* Compute the 'Input' Paraemeter */
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200717 uint32_t fcs_calc, iv = gprs_cipher_gen_input_ui(lle->llme->iov_ui, sapi,
Max1de15912016-07-11 12:42:12 +0200718 nu, oc);
719 /* Compute gamma that we need to XOR with the data */
720 int r = gprs_cipher_run(cipher_out, crypt_len, lle->llme->algo,
721 lle->llme->kc, iv,
722 fcs ? GPRS_CIPH_SGSN2MS : GPRS_CIPH_MS2SGSN);
723 if (r < 0) {
724 LOGP(DLLC, LOGL_ERROR, "Error producing %s gamma for UI "
725 "frame: %d\n", get_value_string(gprs_cipher_names,
726 lle->llme->algo), r);
727 return -ENOMSG;
728 }
729
730 if (fcs) {
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200731 /* Mark frame as encrypted and update FCS */
732 data[2] |= 0x02;
733 fcs_calc = gprs_llc_fcs(data, fcs - data);
734 fcs[0] = fcs_calc & 0xff;
735 fcs[1] = (fcs_calc >> 8) & 0xff;
736 fcs[2] = (fcs_calc >> 16) & 0xff;
Max1de15912016-07-11 12:42:12 +0200737 data += 3;
738 }
739
740 /* XOR the cipher output with the data */
741 for (r = 0; r < crypt_len; r++)
742 *(data + r) ^= cipher_out[r];
743
744 return 0;
745}
746
Max82040102016-07-06 11:59:18 +0200747/* Transmit a UI frame over the given SAPI:
748 'encryptable' indicates whether particular message can be encrypted according
749 to 3GPP TS 24.008 § 4.7.1.2
750 */
Harald Welte56a01452010-05-31 22:12:30 +0200751int gprs_llc_tx_ui(struct msgb *msg, uint8_t sapi, int command,
Max82040102016-07-06 11:59:18 +0200752 struct sgsn_mm_ctx *mmctx, bool encryptable)
Harald Welte9b455bf2010-03-14 15:45:01 +0800753{
Harald Weltee6afd602010-05-02 11:19:37 +0200754 struct gprs_llc_lle *lle;
Harald Welteeaa614c2010-05-02 11:26:34 +0200755 uint8_t *fcs, *llch;
756 uint8_t addr, ctrl[2];
757 uint32_t fcs_calc;
758 uint16_t nu = 0;
Harald Welted07b4f92010-06-30 23:07:59 +0200759 uint32_t oc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800760
Harald Weltee6afd602010-05-02 11:19:37 +0200761 /* Identifiers from UP: (TLLI, SAPI) + (BVCI, NSEI) */
762
763 /* look-up or create the LL Entity for this (TLLI, SAPI) tuple */
Holger Hans Peter Freyther4299c052014-10-02 21:27:24 +0200764 lle = gprs_lle_get_or_create(msgb_tlli(msg), sapi);
Harald Welte1d9d9442010-06-03 07:11:04 +0200765
766 if (msg->len > lle->params.n201_u) {
767 LOGP(DLLC, LOGL_ERROR, "Cannot Tx %u bytes (N201-U=%u)\n",
768 msg->len, lle->params.n201_u);
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200769 msgb_free(msg);
Harald Welte1d9d9442010-06-03 07:11:04 +0200770 return -EFBIG;
771 }
772
Max5aa51962016-07-06 11:33:04 +0200773 gprs_llme_copy_key(mmctx, lle->llme);
774
Harald Weltee6afd602010-05-02 11:19:37 +0200775 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200776 lle->llme->bvci = msgb_bvci(msg);
777 lle->llme->nsei = msgb_nsei(msg);
Harald Weltee6afd602010-05-02 11:19:37 +0200778
Harald Welted07b4f92010-06-30 23:07:59 +0200779 /* Obtain current values for N(u) and OC */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200780 nu = lle->vu_send;
Harald Welted07b4f92010-06-30 23:07:59 +0200781 oc = lle->oc_ui_send;
782 /* Increment V(U) */
Harald Welte6bdee6a2010-05-30 21:51:58 +0200783 lle->vu_send = (lle->vu_send + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200784 /* Increment Overflow Counter, if needed */
785 if ((lle->vu_send + 1) / 512)
786 lle->oc_ui_send += 512;
Harald Welte6bdee6a2010-05-30 21:51:58 +0200787
Harald Welte9b455bf2010-03-14 15:45:01 +0800788 /* Address Field */
789 addr = sapi & 0xf;
790 if (command)
791 addr |= 0x40;
792
793 /* Control Field */
794 ctrl[0] = 0xc0;
795 ctrl[0] |= nu >> 6;
796 ctrl[1] = (nu << 2) & 0xfc;
797 ctrl[1] |= 0x01; /* Protected Mode */
798
799 /* prepend LLC UI header */
800 llch = msgb_push(msg, 3);
801 llch[0] = addr;
802 llch[1] = ctrl[0];
803 llch[2] = ctrl[1];
804
805 /* append FCS to end of frame */
806 fcs = msgb_put(msg, 3);
807 fcs_calc = gprs_llc_fcs(llch, fcs - llch);
808 fcs[0] = fcs_calc & 0xff;
809 fcs[1] = (fcs_calc >> 8) & 0xff;
810 fcs[2] = (fcs_calc >> 16) & 0xff;
811
Max82040102016-07-06 11:59:18 +0200812 if (lle->llme->algo != GPRS_ALGO_GEA0 && encryptable) {
Max1de15912016-07-11 12:42:12 +0200813 int rc = apply_gea(lle, fcs - llch, nu, oc, sapi, fcs, llch);
Harald Welted07b4f92010-06-30 23:07:59 +0200814 if (rc < 0) {
Holger Hans Peter Freytherf9ffd1f2014-10-10 17:35:54 +0200815 msgb_free(msg);
Harald Welted07b4f92010-06-30 23:07:59 +0200816 return rc;
817 }
Harald Welted07b4f92010-06-30 23:07:59 +0200818 }
819
Pau Espin Pedrola33f0062021-06-04 17:27:03 +0200820 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_LLC_DL_PACKETS));
821 rate_ctr_add(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_LLC_DL_BYTES), msg->len);
Alexander Couzens33163972016-10-04 17:53:21 +0200822
Harald Weltee6afd602010-05-02 11:19:37 +0200823 /* Identifiers passed down: (BVCI, NSEI) */
824
Harald Welte1ae09c72010-05-13 19:22:55 +0200825 /* Send BSSGP-DL-UNITDATA.req */
Harald Weltefaa70ff2012-06-17 09:31:16 +0800826 return _bssgp_tx_dl_ud(msg, mmctx);
Harald Welte9b455bf2010-03-14 15:45:01 +0800827}
828
Harald Welte9b455bf2010-03-14 15:45:01 +0800829static int gprs_llc_hdr_rx(struct gprs_llc_hdr_parsed *gph,
830 struct gprs_llc_lle *lle)
831{
832 switch (gph->cmd) {
Harald Welte1f60bbe2019-04-23 23:21:14 +0200833#if 0
834 /* we don't fully imoplement ABM, so refuse it properly (OS#3953) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800835 case GPRS_LLC_SABM: /* Section 6.4.1.1 */
836 lle->v_sent = lle->v_ack = lle->v_recv = 0;
Harald Welte807a5d82010-06-01 11:53:01 +0200837 if (lle->state == GPRS_LLES_ASSIGNED_ADM) {
Harald Welte9b455bf2010-03-14 15:45:01 +0800838 /* start re-establishment (8.7.1) */
839 }
Harald Welte807a5d82010-06-01 11:53:01 +0200840 lle->state = GPRS_LLES_REMOTE_EST;
Harald Welte9b455bf2010-03-14 15:45:01 +0800841 /* FIXME: Send UA */
Harald Welte807a5d82010-06-01 11:53:01 +0200842 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800843 /* FIXME: process data */
844 break;
845 case GPRS_LLC_DISC: /* Section 6.4.1.2 */
846 /* FIXME: Send UA */
847 /* terminate ABM */
Harald Welte807a5d82010-06-01 11:53:01 +0200848 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800849 break;
850 case GPRS_LLC_UA: /* Section 6.4.1.3 */
Harald Welte807a5d82010-06-01 11:53:01 +0200851 if (lle->state == GPRS_LLES_LOCAL_EST)
852 lle->state = GPRS_LLES_ABM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800853 break;
854 case GPRS_LLC_DM: /* Section 6.4.1.4: ABM cannot be performed */
Harald Welte807a5d82010-06-01 11:53:01 +0200855 if (lle->state == GPRS_LLES_LOCAL_EST)
856 lle->state = GPRS_LLES_ASSIGNED_ADM;
Harald Welte9b455bf2010-03-14 15:45:01 +0800857 break;
858 case GPRS_LLC_FRMR: /* Section 6.4.1.5 */
859 break;
Harald Welte1f60bbe2019-04-23 23:21:14 +0200860#else
861 case GPRS_LLC_SABM:
862 case GPRS_LLC_DISC:
863 /* send DM to properly signal we don't do ABM */
864 gprs_llc_tx_dm(lle);
865 break;
866#endif
Harald Welte9b455bf2010-03-14 15:45:01 +0800867 case GPRS_LLC_XID: /* Section 6.4.1.6 */
Harald Welte0c1a3032011-10-16 18:49:05 +0200868 rx_llc_xid(lle, gph);
Harald Welte9b455bf2010-03-14 15:45:01 +0800869 break;
Harald Welteebabdea2010-06-01 18:28:10 +0200870 case GPRS_LLC_UI:
Holger Hans Peter Freytherfaf1f642011-06-23 17:53:27 -0400871 if (gprs_llc_is_retransmit(gph->seq_tx, lle->vu_recv)) {
872 LOGP(DLLC, LOGL_NOTICE,
873 "TLLI=%08x dropping UI, N(U=%d) not in window V(URV(UR:%d).\n",
Holger Hans Peter Freyther2788b962010-06-23 09:48:25 +0800874 lle->llme ? lle->llme->tlli : -1,
Harald Welteebabdea2010-06-01 18:28:10 +0200875 gph->seq_tx, lle->vu_recv);
Harald Welteabadd542013-06-21 14:06:18 +0200876
877 /* HACK: non-standard recovery handling. If remote LLE
878 * is re-transmitting the same sequence number for
Harald Welte649e1ff2013-07-21 17:41:46 +0800879 * three times, don't discard the frame but pass it on
Harald Welteabadd542013-06-21 14:06:18 +0200880 * and 'learn' the new sequence number */
881 if (gph->seq_tx != lle->vu_recv_last) {
882 lle->vu_recv_last = gph->seq_tx;
883 lle->vu_recv_duplicates = 0;
884 } else {
885 lle->vu_recv_duplicates++;
886 if (lle->vu_recv_duplicates < 3)
887 return -EIO;
888 LOGP(DLLC, LOGL_NOTICE, "TLLI=%08x recovering "
889 "N(U=%d) after receiving %u duplicates\n",
890 lle->llme ? lle->llme->tlli : -1,
891 gph->seq_tx, lle->vu_recv_duplicates);
892 }
Harald Welteebabdea2010-06-01 18:28:10 +0200893 }
894 /* Increment the sequence number that we expect in the next frame */
895 lle->vu_recv = (gph->seq_tx + 1) % 512;
Harald Welted07b4f92010-06-30 23:07:59 +0200896 /* Increment Overflow Counter */
897 if ((gph->seq_tx + 1) / 512)
898 lle->oc_ui_recv += 512;
Harald Welteebabdea2010-06-01 18:28:10 +0200899 break;
Harald Welte3a0b7722019-04-23 22:48:26 +0200900 case GPRS_LLC_NULL:
901 LOGP(DLLC, LOGL_DEBUG, "TLLI=%08x sends us LLC NULL\n", lle->llme ? lle->llme->tlli : -1);
902 break;
Holger Hans Peter Freyther744568b2014-04-04 12:47:32 +0200903 default:
904 LOGP(DLLC, LOGL_NOTICE, "Unhandled command: %d\n", gph->cmd);
905 break;
Harald Welte9b455bf2010-03-14 15:45:01 +0800906 }
907
908 return 0;
909}
910
Harald Weltea2665542010-05-02 09:28:11 +0200911/* receive an incoming LLC PDU (BSSGP-UL-UNITDATA-IND, 7.2.4.2) */
Harald Welte9b455bf2010-03-14 15:45:01 +0800912int gprs_llc_rcvmsg(struct msgb *msg, struct tlv_parsed *tv)
913{
Holger Hans Peter Freyther3dccda52011-10-14 23:42:13 +0200914 struct gprs_llc_hdr *lh = (struct gprs_llc_hdr *) msgb_llch(msg);
Harald Welte9b455bf2010-03-14 15:45:01 +0800915 struct gprs_llc_hdr_parsed llhp;
Max549ebc72016-11-18 14:07:04 +0100916 struct gprs_llc_lle *lle = NULL;
Max82040102016-07-06 11:59:18 +0200917 bool drop_cipherable = false;
Harald Weltea2665542010-05-02 09:28:11 +0200918 int rc = 0;
Harald Welte9b455bf2010-03-14 15:45:01 +0800919
Harald Welte11d7c102010-05-02 11:54:55 +0200920 /* Identifiers from DOWN: NSEI, BVCI, TLLI */
921
Holger Hans Peter Freyther4752e0c2010-05-23 21:33:57 +0800922 memset(&llhp, 0, sizeof(llhp));
Holger Hans Peter Freytherfa848d42010-05-23 21:43:57 +0800923 rc = gprs_llc_hdr_parse(&llhp, (uint8_t *) lh, TLVP_LEN(tv, BSSGP_IE_LLC_PDU));
Harald Welte1ae09c72010-05-13 19:22:55 +0200924 if (rc < 0) {
Harald Welte1b170d12010-05-13 19:49:06 +0200925 LOGP(DLLC, LOGL_NOTICE, "Error during LLC header parsing\n");
Harald Welte1ae09c72010-05-13 19:22:55 +0200926 return rc;
927 }
928
Harald Welte807a5d82010-06-01 11:53:01 +0200929 switch (gprs_tlli_type(msgb_tlli(msg))) {
930 case TLLI_LOCAL:
931 case TLLI_FOREIGN:
932 case TLLI_RANDOM:
933 case TLLI_AUXILIARY:
934 break;
935 default:
936 LOGP(DLLC, LOGL_ERROR,
937 "Discarding frame with strange TLLI type\n");
938 break;
939 }
940
Harald Weltea2665542010-05-02 09:28:11 +0200941 /* find the LLC Entity for this TLLI+SAPI tuple */
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200942 lle = lle_for_rx_by_tlli_sapi(msgb_tlli(msg), llhp.sapi, llhp.cmd);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200943 if (!lle) {
944 switch (llhp.sapi) {
945 case GPRS_SAPI_SNDCP3:
946 case GPRS_SAPI_SNDCP5:
947 case GPRS_SAPI_SNDCP9:
948 case GPRS_SAPI_SNDCP11:
949 /* Ask an upper layer for help. */
Alexander Couzens58f446c2016-08-30 18:51:50 +0200950 return gsm0408_gprs_force_reattach_oldmsg(msg, NULL);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200951 default:
952 break;
953 }
Holger Hans Peter Freyther964a9b32013-07-30 09:29:27 +0200954 return 0;
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +0200955 }
Max549ebc72016-11-18 14:07:04 +0100956 gprs_llc_hdr_dump(&llhp, lle);
Jacob Erlbeck81ffb742015-01-23 11:33:51 +0100957 /* reset age computation */
958 lle->llme->age_timestamp = GPRS_LLME_RESET_AGE;
959
Harald Welted07b4f92010-06-30 23:07:59 +0200960 /* decrypt information field + FCS, if needed! */
961 if (llhp.is_encrypted) {
Max1de15912016-07-11 12:42:12 +0200962 if (lle->llme->algo != GPRS_ALGO_GEA0) {
963 rc = apply_gea(lle, llhp.data_len + 3, llhp.seq_tx,
964 lle->oc_ui_recv, lle->sapi, NULL,
965 llhp.data);
966 if (rc < 0)
967 return rc;
Dieter Spaarb572d7c2016-07-11 12:48:07 +0200968 llhp.fcs = *(llhp.data + llhp.data_len);
969 llhp.fcs |= *(llhp.data + llhp.data_len + 1) << 8;
970 llhp.fcs |= *(llhp.data + llhp.data_len + 2) << 16;
Max1de15912016-07-11 12:42:12 +0200971 } else {
Harald Welted07b4f92010-06-30 23:07:59 +0200972 LOGP(DLLC, LOGL_NOTICE, "encrypted frame for LLC that "
973 "has no KC/Algo! Dropping.\n");
974 return 0;
975 }
Harald Welted07b4f92010-06-30 23:07:59 +0200976 } else {
Max82040102016-07-06 11:59:18 +0200977 if (lle->llme->algo != GPRS_ALGO_GEA0 &&
978 lle->llme->cksn != GSM_KEY_SEQ_INVAL)
979 drop_cipherable = true;
Harald Welted07b4f92010-06-30 23:07:59 +0200980 }
981
982 /* We have to do the FCS check _after_ decryption */
Harald Welte1b8827a2010-06-30 23:15:57 +0200983 llhp.fcs_calc = gprs_llc_fcs((uint8_t *)lh, llhp.crc_length);
Harald Welted07b4f92010-06-30 23:07:59 +0200984 if (llhp.fcs != llhp.fcs_calc) {
985 LOGP(DLLC, LOGL_INFO, "Dropping frame with invalid FCS\n");
986 return -EIO;
987 }
988
Harald Welte10997d02010-05-03 12:28:12 +0200989 /* Update LLE's (BVCI, NSEI) tuple */
Harald Welte807a5d82010-06-01 11:53:01 +0200990 lle->llme->bvci = msgb_bvci(msg);
991 lle->llme->nsei = msgb_nsei(msg);
Harald Welte10997d02010-05-03 12:28:12 +0200992
Harald Welte1ae09c72010-05-13 19:22:55 +0200993 /* Receive and Process the actual LLC frame */
Harald Welte9b455bf2010-03-14 15:45:01 +0800994 rc = gprs_llc_hdr_rx(&llhp, lle);
Harald Welte1ae09c72010-05-13 19:22:55 +0200995 if (rc < 0)
996 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +0800997
Harald Welte37bd0212019-04-23 22:46:43 +0200998 /* there are many frame types that don't carry user information
999 * and which hence have llhp.data = NULL */
1000 if (llhp.data) {
1001 /* set l3 layer & remove the fcs */
1002 msg->l3h = llhp.data;
1003 msgb_l3trim(msg, llhp.data_len);
1004 }
1005
Pau Espin Pedrola33f0062021-06-04 17:27:03 +02001006 rate_ctr_inc(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_LLC_UL_PACKETS));
1007 rate_ctr_add(rate_ctr_group_get_ctr(sgsn->rate_ctrs, CTR_LLC_UL_BYTES), msg->len);
Alexander Couzens4e699a92016-07-05 11:04:27 +02001008
Harald Welte1ae09c72010-05-13 19:22:55 +02001009 /* llhp.data is only set when we need to send LL_[UNIT]DATA_IND up */
Harald Welte22df4ac2015-08-16 15:23:32 +02001010 if (llhp.cmd == GPRS_LLC_UI && llhp.data && llhp.data_len) {
Harald Welte9b455bf2010-03-14 15:45:01 +08001011 switch (llhp.sapi) {
1012 case GPRS_SAPI_GMM:
Harald Welte1ae09c72010-05-13 19:22:55 +02001013 /* send LL_UNITDATA_IND to GMM */
Max82040102016-07-06 11:59:18 +02001014 rc = gsm0408_gprs_rcvmsg_gb(msg, lle->llme,
1015 drop_cipherable);
Harald Weltea2665542010-05-02 09:28:11 +02001016 break;
Harald Weltea2665542010-05-02 09:28:11 +02001017 case GPRS_SAPI_SNDCP3:
1018 case GPRS_SAPI_SNDCP5:
1019 case GPRS_SAPI_SNDCP9:
1020 case GPRS_SAPI_SNDCP11:
Harald Welteebabdea2010-06-01 18:28:10 +02001021 /* send LL_DATA_IND/LL_UNITDATA_IND to SNDCP */
1022 rc = sndcp_llunitdata_ind(msg, lle, llhp.data, llhp.data_len);
1023 break;
Harald Weltea2665542010-05-02 09:28:11 +02001024 case GPRS_SAPI_SMS:
1025 /* FIXME */
Harald Welteebabdea2010-06-01 18:28:10 +02001026 case GPRS_SAPI_TOM2:
1027 case GPRS_SAPI_TOM8:
1028 /* FIXME: send LL_DATA_IND/LL_UNITDATA_IND to TOM */
Harald Weltea2665542010-05-02 09:28:11 +02001029 default:
Harald Weltec6ecafe2010-05-13 19:47:50 +02001030 LOGP(DLLC, LOGL_NOTICE, "Unsupported SAPI %u\n", llhp.sapi);
Harald Weltea2665542010-05-02 09:28:11 +02001031 rc = -EINVAL;
1032 break;
Harald Welte9b455bf2010-03-14 15:45:01 +08001033 }
1034 }
1035
Harald Weltea2665542010-05-02 09:28:11 +02001036 return rc;
Harald Welte9b455bf2010-03-14 15:45:01 +08001037}
Harald Welte807a5d82010-06-01 11:53:01 +02001038
Max5aa51962016-07-06 11:33:04 +02001039/* Propagate crypto parameters MM -> LLME */
1040void gprs_llme_copy_key(struct sgsn_mm_ctx *mm, struct gprs_llc_llme *llme)
1041{
1042 if (!mm)
1043 return;
1044 if (mm->ciph_algo != GPRS_ALGO_GEA0) {
1045 llme->algo = mm->ciph_algo;
1046 if (llme->cksn != mm->auth_triplet.key_seq &&
1047 mm->auth_triplet.key_seq != GSM_KEY_SEQ_INVAL) {
Eric2f898262021-05-19 18:57:50 +02001048
1049 /* gea4 needs kc128 */
1050 if (mm->ciph_algo == GPRS_ALGO_GEA4)
1051 osmo_kdf_kc128(mm->auth_triplet.vec.ck, mm->auth_triplet.vec.ik, llme->kc);
1052 else
1053 memcpy(llme->kc, mm->auth_triplet.vec.kc, gprs_cipher_key_length(mm->ciph_algo));
1054
Max5aa51962016-07-06 11:33:04 +02001055 llme->cksn = mm->auth_triplet.key_seq;
1056 }
1057 } else
1058 llme->cksn = GSM_KEY_SEQ_INVAL;
1059}
1060
Harald Welte807a5d82010-06-01 11:53:01 +02001061/* 04.64 Chapter 7.2.1.1 LLGMM-ASSIGN */
1062int gprs_llgmm_assign(struct gprs_llc_llme *llme,
Max5aa51962016-07-06 11:33:04 +02001063 uint32_t old_tlli, uint32_t new_tlli)
Harald Welte807a5d82010-06-01 11:53:01 +02001064{
1065 unsigned int i;
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001066 bool free = false;
1067
1068 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Assign pre (%08x => %08x)\n",
1069 old_tlli, new_tlli);
Harald Welte807a5d82010-06-01 11:53:01 +02001070
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001071 if (old_tlli == TLLI_UNASSIGNED && new_tlli != TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001072 /* TLLI Assignment 8.3.1 */
1073 /* New TLLI shall be assigned and used when (re)transmitting LLC frames */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001074 /* If old TLLI != TLLI_UNASSIGNED was assigned to LLME, then TLLI
Harald Welte807a5d82010-06-01 11:53:01 +02001075 * old is unassigned. Only TLLI new shall be accepted when
1076 * received from peer. */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001077 if (llme->old_tlli != TLLI_UNASSIGNED) {
1078 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte875840c2010-07-01 11:54:31 +02001079 llme->tlli = new_tlli;
1080 } else {
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001081 /* If TLLI old == TLLI_UNASSIGNED was assigned to LLME, then this is
Harald Welte875840c2010-07-01 11:54:31 +02001082 * TLLI assignmemt according to 8.3.1 */
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001083 llme->old_tlli = TLLI_UNASSIGNED;
Harald Welte875840c2010-07-01 11:54:31 +02001084 llme->tlli = new_tlli;
1085 llme->state = GPRS_LLMS_ASSIGNED;
1086 /* 8.5.3.1 For all LLE's */
1087 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1088 struct gprs_llc_lle *l = &llme->lle[i];
1089 l->vu_send = l->vu_recv = 0;
1090 l->retrans_ctr = 0;
1091 l->state = GPRS_LLES_ASSIGNED_ADM;
1092 /* FIXME Set parameters according to table 9 */
1093 }
Harald Welte807a5d82010-06-01 11:53:01 +02001094 }
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001095 } else if (old_tlli != TLLI_UNASSIGNED && new_tlli != TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001096 /* TLLI Change 8.3.2 */
1097 /* Both TLLI Old and TLLI New are assigned; use New when
Holger Hans Peter Freyther92aa6bb2013-07-28 20:13:01 +02001098 * (re)transmitting. Accept both Old and New on Rx */
Holger Hans Peter Freytheraa93bac2013-07-31 11:20:37 +02001099 llme->old_tlli = old_tlli;
Harald Welte807a5d82010-06-01 11:53:01 +02001100 llme->tlli = new_tlli;
1101 llme->state = GPRS_LLMS_ASSIGNED;
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001102 } else if (old_tlli != TLLI_UNASSIGNED && new_tlli == TLLI_UNASSIGNED) {
Harald Welte807a5d82010-06-01 11:53:01 +02001103 /* TLLI Unassignment 8.3.3) */
1104 llme->tlli = llme->old_tlli = 0;
1105 llme->state = GPRS_LLMS_UNASSIGNED;
1106 for (i = 0; i < ARRAY_SIZE(llme->lle); i++) {
1107 struct gprs_llc_lle *l = &llme->lle[i];
1108 l->state = GPRS_LLES_UNASSIGNED;
1109 }
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001110 free = true;
Harald Welte807a5d82010-06-01 11:53:01 +02001111 } else
1112 return -EINVAL;
1113
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001114 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Assign post (%08x => %08x)\n",
1115 old_tlli, new_tlli);
1116
1117 if (free)
1118 llme_free(llme);
1119
Harald Welte807a5d82010-06-01 11:53:01 +02001120 return 0;
1121}
Harald Welte496aee42010-06-30 19:59:55 +02001122
Max39550252016-06-28 17:39:20 +02001123/* TLLI unassignment */
1124int gprs_llgmm_unassign(struct gprs_llc_llme *llme)
1125{
Pau Espin Pedrol404d9b82019-08-12 17:49:09 +02001126 return gprs_llgmm_assign(llme, llme->tlli, TLLI_UNASSIGNED);
Max39550252016-06-28 17:39:20 +02001127}
1128
Harald Welte0c1a3032011-10-16 18:49:05 +02001129/* Chapter 7.2.1.2 LLGMM-RESET.req */
1130int gprs_llgmm_reset(struct gprs_llc_llme *llme)
1131{
1132 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Pau Espin Pedrolb71d2c52019-11-21 16:15:52 +01001133 struct gprs_llc_lle *lle = &llme->lle[GPRS_SAPI_GMM];
Philipp4ac3aee2016-08-10 12:24:09 +02001134 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001135 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001136 uint8_t *xid;
Harald Welte0c1a3032011-10-16 18:49:05 +02001137
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001138 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Reset\n");
Max3b6332f2017-11-01 13:28:38 +01001139
1140 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1141 if (rc < 0) {
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001142 LOGGBP(llme, DLLC, LOGL_ERROR,
1143 "osmo_get_rand_id() failed for LLC XID reset: %s\n",
1144 strerror(-rc));
Max3b6332f2017-11-01 13:28:38 +01001145 return rc;
Maxb997f842016-07-06 15:57:01 +02001146 }
1147
Philipp4ac3aee2016-08-10 12:24:09 +02001148 /* Generate XID message */
Harald Welteaf779d22019-04-12 16:56:04 +02001149 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, sizeof(xid_bytes),
1150 llme->iov_ui, lle);
Harald Welte7e5bb622016-09-28 08:20:58 +08001151 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001152 return -EINVAL;
1153 xid = msgb_put(msg, xid_bytes_len);
1154 memcpy(xid, xid_bytes, xid_bytes_len);
Harald Welte0c1a3032011-10-16 18:49:05 +02001155
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001156 /* Reset some of the LLC parameters. See GSM 04.64, 8.5.3.1 */
1157 lle->vu_recv = 0;
1158 lle->vu_send = 0;
1159 lle->oc_ui_send = 0;
1160 lle->oc_ui_recv = 0;
1161
Harald Welte0c1a3032011-10-16 18:49:05 +02001162 /* FIXME: Start T200, wait for XID response */
Jacob Erlbeck25ad52c2014-09-11 14:20:53 +02001163 return gprs_llc_tx_xid(lle, msg, 1);
Harald Welte0c1a3032011-10-16 18:49:05 +02001164}
1165
Maxb997f842016-07-06 15:57:01 +02001166int gprs_llgmm_reset_oldmsg(struct msgb* oldmsg, uint8_t sapi,
1167 struct gprs_llc_llme *llme)
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001168{
1169 struct msgb *msg = msgb_alloc_headroom(4096, 1024, "LLC_XID");
Harald Welteaf779d22019-04-12 16:56:04 +02001170 struct gprs_llc_lle *lle = &llme->lle[sapi];
Philipp4ac3aee2016-08-10 12:24:09 +02001171 uint8_t xid_bytes[1024];
Max3b6332f2017-11-01 13:28:38 +01001172 int xid_bytes_len, rc;
Philipp4ac3aee2016-08-10 12:24:09 +02001173 uint8_t *xid;
Maxb997f842016-07-06 15:57:01 +02001174
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001175 LOGGBP(llme, DLLC, LOGL_NOTICE, "LLGM Reset (SAPI=%" PRIu8 ")\n", sapi);
Max3b6332f2017-11-01 13:28:38 +01001176
1177 rc = osmo_get_rand_id((uint8_t *) &llme->iov_ui, 4);
1178 if (rc < 0) {
Pau Espin Pedrol029a70e2019-11-21 13:58:39 +01001179 LOGGBP(llme, DLLC, LOGL_ERROR,
1180 "osmo_get_rand_id() failed for LLC XID reset: %s\n",
1181 strerror(-rc));
Max3b6332f2017-11-01 13:28:38 +01001182 return rc;
Maxb997f842016-07-06 15:57:01 +02001183 }
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001184
Philipp4ac3aee2016-08-10 12:24:09 +02001185 /* Generate XID message */
Harald Welteaf779d22019-04-12 16:56:04 +02001186 xid_bytes_len = gprs_llc_generate_xid_for_gmm_reset(xid_bytes, sizeof(xid_bytes),
1187 llme->iov_ui, lle);
Harald Welte7e5bb622016-09-28 08:20:58 +08001188 if (xid_bytes_len < 0)
Philipp4ac3aee2016-08-10 12:24:09 +02001189 return -EINVAL;
1190 xid = msgb_put(msg, xid_bytes_len);
1191 memcpy(xid, xid_bytes, xid_bytes_len);
Jacob Erlbeck78ecaf02014-09-05 14:32:36 +02001192
1193 /* FIXME: Start T200, wait for XID response */
1194
1195 msgb_tlli(msg) = msgb_tlli(oldmsg);
1196 msgb_bvci(msg) = msgb_bvci(oldmsg);
1197 msgb_nsei(msg) = msgb_nsei(oldmsg);
1198
1199 return gprs_llc_tx_u(msg, sapi, 1, GPRS_LLC_U_XID, 1);
1200}
1201
Harald Welte496aee42010-06-30 19:59:55 +02001202int gprs_llc_init(const char *cipher_plugin_path)
1203{
1204 return gprs_cipher_load(cipher_plugin_path);
1205}