blob: d6c0495312463b2092839d30c6d00db6bd007ace [file] [log] [blame]
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +01001/*
2 * (C) 2011 by Holger Hans Peter Freyther
3 * (C) 2011 by On-Waves
Harald Weltee08da972017-11-13 01:00:26 +09004 * (C) 2014 by sysmocom - s.f.m.c. GmbH, Author: Daniel Willmann <dwillmann@sysmocom.de>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +01005 * All Rights Reserved
6 *
Harald Weltee08da972017-11-13 01:00:26 +09007 * SPDX-License-Identifier: GPL-2.0+
8 *
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +01009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +010025#include <osmocom/core/application.h>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010026#include <osmocom/core/logging.h>
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000027#include <osmocom/core/utils.h>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010028#include <osmocom/gsm/lapdm.h>
29#include <osmocom/gsm/rsl.h>
30
31#include <errno.h>
32
33#include <string.h>
34
35#define CHECK_RC(rc) \
36 if (rc != 0) { \
37 printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \
38 abort(); \
39 }
40
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010041static struct log_info info = {};
Jacob Erlbeckc893c222014-01-28 10:53:59 +010042static int dummy_l1_header_len = 0;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010043
44struct lapdm_polling_state {
45 struct lapdm_channel *bts;
46 int bts_read;
47
48 struct lapdm_channel *ms;
49 int ms_read;
50};
51
52static struct msgb *msgb_from_array(const uint8_t *data, int len)
53{
54 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
55 msg->l3h = msgb_put(msg, len);
Harald Welte53e26722017-01-04 11:18:59 +010056 if (data && len)
57 memcpy(msg->l3h, data, len);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010058 return msg;
59}
60
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010061/*
62 * Test data is below...
63 */
64static const uint8_t cm[] = {
65 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
66 0x29, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80,
67};
68
Andreas Eversberg6e182082013-02-06 14:13:21 +010069static const uint8_t ua[] = {
70 0x01, 0x73, 0x41, 0x05, 0x24, 0x31, 0x03, 0x50,
71 0x18, 0x93, 0x08, 0x29, 0x47, 0x80, 0x00, 0x00,
72 0x00, 0x00, 0x80, 0x2b, 0x2b, 0x2b, 0x2b
73};
74
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010075static const uint8_t mm[] = {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010076 0x00, 0x0c, 0x00, 0x03, 0x01, 0x01, 0x20, 0x02,
77 0x00, 0x0b, 0x00, 0x03, 0x05, 0x04, 0x0d
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010078};
79
80static const uint8_t dummy1[] = {
81 0xab, 0x03, 0x30, 0x60, 0x06,
82};
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010083
Daniel Willmanne5233922012-12-25 23:15:50 +010084static const uint8_t rel_req[] = {
85 0x02, 0x07, 0x01, 0x0a, 0x02, 0x40, 0x14, 0x01
86};
87
Jacob Erlbeck90937fe2014-01-24 13:48:19 +010088static const uint8_t est_req_sdcch_sapi3[] = {
89 0x02, 0x04, 0x01, 0x20, 0x02, 0x03
90};
91
92static const uint8_t est_req_sacch_sapi3[] = {
93 0x02, 0x04, 0x01, 0x0b, 0x02, 0x43
94};
95
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010096static struct msgb *create_cm_serv_req(void)
97{
98 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010099
100 msg = msgb_from_array(cm, sizeof(cm));
101 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100102 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100103 return msg;
104}
105
106static struct msgb *create_mm_id_req(void)
107{
108 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100109
110 msg = msgb_from_array(mm, sizeof(mm));
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100111 msg->l2h = msg->data + 3;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000112 OSMO_ASSERT(msgb_l2len(msg) == 12);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100113 msg->l3h = msg->l2h + 6;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000114 OSMO_ASSERT(msgb_l3len(msg) == 6);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100115 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100116
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100117 return msg;
118}
119
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800120static struct msgb *create_empty_msg(void)
121{
122 struct msgb *msg;
123
124 msg = msgb_from_array(NULL, 0);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000125 OSMO_ASSERT(msgb_l3len(msg) == 0);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800126 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100127 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800128 return msg;
129}
130
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100131static struct msgb *create_dummy_data_req(void)
132{
133 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100134
135 msg = msgb_from_array(dummy1, sizeof(dummy1));
136 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100137 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100138 return msg;
139}
140
Daniel Willmanne5233922012-12-25 23:15:50 +0100141static struct msgb *create_rel_req(void)
142{
143 struct msgb *msg;
144
145 msg = msgb_from_array(rel_req, sizeof(rel_req));
146 msg->l2h = msg->data;
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100147 msgb_push(msg, dummy_l1_header_len);
Daniel Willmanne5233922012-12-25 23:15:50 +0100148 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
149 return msg;
150}
151
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100152static struct msgb *create_est_req(const uint8_t *est_req, size_t est_req_size)
153{
154 struct msgb *msg;
155
156 msg = msgb_from_array(est_req, est_req_size);
157 msg->l2h = msg->data;
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100158 msgb_push(msg, dummy_l1_header_len);
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100159 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
160 return msg;
161}
162
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100163static int send(struct msgb *in_msg, struct lapdm_channel *chan)
164{
165 struct osmo_phsap_prim pp;
166 struct msgb *msg;
167 int rc;
168
169 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
170 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
171 PRIM_OP_INDICATION, msg);
172 /* copy over actual MAC block */
173 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
174 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
175
176 /* LAPDm requires those... */
177 pp.u.data.chan_nr = 0;
178 pp.u.data.link_id = 0;
179 /* feed into the LAPDm code of libosmogsm */
180 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000181 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100182 return 0;
183}
184
Daniel Willmann09129352014-03-20 18:54:57 +0100185/* Receive from L1 */
186static int send_buf(const uint8_t *buf, size_t len, struct lapdm_channel *chan)
Andreas Eversberg6e182082013-02-06 14:13:21 +0100187{
188 struct osmo_phsap_prim pp;
189 struct msgb *msg;
190 int rc;
191
Daniel Willmann09129352014-03-20 18:54:57 +0100192 msg = msgb_from_array(buf, len);
193 msg->l2h = msg->l3h;
194 msg->l3h = NULL;
195 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
196 PRIM_OP_INDICATION, msg);
197
198 /* LAPDm requires those... */
199 pp.u.data.chan_nr = 0;
200 pp.u.data.link_id = 0;
201 /* feed into the LAPDm code of libosmogsm */
202 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
203 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
204 return 0;
205}
206
207/* Receive from L3 */
208static int enqueue_buf(const uint8_t *buf, size_t len, int sapi, struct lapdm_channel *chan)
209{
210 struct osmo_dlsap_prim dp;
211 struct msgb *msg;
212 int rc;
213 struct lapdm_datalink *dl;
214
215 dl = lapdm_datalink_for_sapi(&chan->lapdm_dcch, sapi);
216 OSMO_ASSERT(dl);
217
218 msg = msgb_from_array(buf, len);
219 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
220
221 rc = lapd_recv_dlsap(&dp, &dl->dl.lctx);
222 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
223 return 0;
224}
225
226static int send_sabm(struct lapdm_channel *chan, int sapi, const uint8_t *data, size_t len)
227{
228 struct osmo_phsap_prim pp;
229 struct msgb *msg;
230 int rc;
231
232 OSMO_ASSERT(sapi == 0 || sapi == 3);
233
Andreas Eversberg6e182082013-02-06 14:13:21 +0100234 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
235 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
236 PRIM_OP_INDICATION, msg);
237 /* copy over actual MAC block */
Daniel Willmann09129352014-03-20 18:54:57 +0100238 msg->l2h = msgb_put(msg, 3 + len);
239 msg->l2h[0] = 0x01 | (sapi << 2);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100240 msg->l2h[1] = 0x3f;
Daniel Willmann09129352014-03-20 18:54:57 +0100241 msg->l2h[2] = 0x01 | (len << 2);
242
243 if (len > 0)
244 memcpy(msg->l2h + 3, data, len);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100245
246 /* LAPDm requires those... */
247 pp.u.data.chan_nr = 0;
248 pp.u.data.link_id = 0;
249 /* feed into the LAPDm code of libosmogsm */
250 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
251 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
252 return 0;
253}
254
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100255/*
256 * I get called from the LAPDm code when something was sent my way...
257 */
258static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
259{
260 struct lapdm_polling_state *state = _ctx;
261
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100262
263 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100264
265
266 if (state->bts_read == 0) {
267 printf("BTS: Verifying CM request.\n");
Holger Hans Peter Freytherdd34ed52013-06-19 08:16:56 +0200268 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm));
269 OSMO_ASSERT(memcmp(in_msg->l3h, cm,
270 ARRAY_SIZE(cm)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100271 } else if (state->bts_read == 1) {
272 printf("BTS: Verifying dummy message.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000273 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1));
274 OSMO_ASSERT(memcmp(in_msg->l3h, dummy1,
275 ARRAY_SIZE(dummy1)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100276 } else {
277 printf("BTS: Do not know to verify: %d\n", state->bts_read);
278 }
279
280 state->bts_read += 1;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100281 msgb_free(in_msg);
282
283 return 0;
284}
285
286static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
287{
288 int rc;
289 struct lapdm_polling_state *state = _ctx;
290 printf("%s: MS(us) -> BTS prim message\n", __func__);
291
292 /* i stuff it into the LAPDm channel of the BTS */
293 rc = send(oph->msg, state->bts);
294 msgb_free(oph->msg);
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +0100295 return rc;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100296}
297
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100298static int dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp,
299 const char *queue_name)
300{
301 int rc;
302 int l2_header_len;
303 int l3_len = 0;
304
305 /* Take message from queue */
306 rc = lapdm_phsap_dequeue_prim(le, pp);
307
308 fprintf(stderr, "dequeue: got rc %d: %s\n", rc,
309 rc <= 0 ? strerror(-rc) : "-");
310
311 if (rc < 0)
312 return rc;
313
314 l2_header_len = msgb_l2len(pp->oph.msg);
315 if (msgb_l3(pp->oph.msg)) {
316 l3_len = msgb_l3len(pp->oph.msg);
317 l2_header_len -= l3_len;
318 } else
319 fprintf(stderr, "MSGB: L3 is undefined\n");
320
321 if (l2_header_len < 0 || l2_header_len > pp->oph.msg->data_len) {
322 fprintf(stderr,
323 "MSGB inconsistent: data = %p, l2 = %p, l3 = %p, tail = %p\n",
324 pp->oph.msg->data,
325 pp->oph.msg->l2h,
326 pp->oph.msg->l3h,
327 pp->oph.msg->tail);
328 l2_header_len = -1;
329 }
330
331 printf("Took message from %s queue: "
332 "L2 header size %d, L3 size %d, "
333 "SAP %#x, %d/%d, Link 0x%02x\n",
334 queue_name,
335 l2_header_len, l3_len,
336 pp->oph.sap, pp->oph.primitive, pp->oph.operation,
337 pp->u.data.link_id);
338 printf("Message: %s\n", msgb_hexdump(pp->oph.msg));
339
340 return rc;
341}
342
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100343static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
344{
345 struct lapdm_polling_state *state = _ctx;
346
347 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
348
349 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100350 struct abis_rsl_rll_hdr hdr;
351
352 printf("MS: Verifying incoming primitive.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000353 OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100354
355 /* verify the header */
356 memset(&hdr, 0, sizeof(hdr));
357 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
358 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000359 OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100360
361 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000362 OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Pau Espin Pedrol35c6fb32017-06-18 10:58:10 +0200363 #pragma message ("RSL_IE_L3_INFO 16 bit length is wrong")
Holger Hans Peter Freyther3cc268c2013-06-19 08:30:59 +0200364 /* This should be okay but it is actually 0x0, 0x9c on ia-32 */
365 /* OSMO_ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x0); */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100366 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100367 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000368 OSMO_ASSERT(msgb_l3len(msg) == 3);
369 OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100370 } else {
371 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100372 }
373
374 state->ms_read += 1;
375 msgb_free(msg);
376 return 0;
377}
378
379static void test_lapdm_polling()
380{
381 printf("I do some very simple LAPDm test.\n");
382
383 int rc;
384 struct lapdm_polling_state test_state;
385 struct osmo_phsap_prim pp;
386
387 /* Configure LAPDm on both sides */
388 struct lapdm_channel bts_to_ms_channel;
389 struct lapdm_channel ms_to_bts_channel;
390 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
391 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
392
393 memset(&test_state, 0, sizeof(test_state));
394 test_state.bts = &bts_to_ms_channel;
395 test_state.ms = &ms_to_bts_channel;
396
397 /* BTS to MS in polling mode */
398 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
399 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
400 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
401 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
402
403 /* MS to BTS in direct mode */
404 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
405 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
406 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
407
408 /*
409 * We try to send messages from the MS to the BTS to the MS..
410 */
411 /* 1. Start with MS -> BTS, BTS should have a pending message */
412 printf("Establishing link.\n");
413 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
414
415 /* 2. Poll on the BTS for sending out a confirmation */
416 printf("\nConfirming\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000417 OSMO_ASSERT(test_state.bts_read == 1);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100418 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100419 CHECK_RC(rc);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000420 OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100421 send(pp.oph.msg, &ms_to_bts_channel);
422 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000423 OSMO_ASSERT(test_state.ms_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100424
425 /* 3. Send some data to the MS */
426 printf("\nSending back to MS\n");
427 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100428 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100429 CHECK_RC(rc);
430 send(pp.oph.msg, &ms_to_bts_channel);
431 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000432 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100433
434 /* verify that there is nothing more to poll */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100435 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000436 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100437
438 /* 3. And back to the BTS */
439 printf("\nSending back to BTS\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000440 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100441 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
442
443
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100444 /* 4. And back to the MS, but let's move data/l2h apart */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000445 OSMO_ASSERT(test_state.bts_read == 2);
446 OSMO_ASSERT(test_state.ms_read == 2);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100447 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100448 CHECK_RC(rc);
449 send(pp.oph.msg, &ms_to_bts_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000450 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100451 msgb_free(pp.oph.msg);
452
453 /* verify that there is nothing more to poll */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100454 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000455 OSMO_ASSERT(rc < 0);
Jacob Erlbeck7d5f17a2014-02-28 15:54:03 +0100456 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
457 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100458
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800459 /* check sending an empty L3 message fails */
460 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000461 OSMO_ASSERT(rc == -1);
462 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800463
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100464 /* clean up */
465 lapdm_channel_exit(&bts_to_ms_channel);
466 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200467
468 /* Check if exit is idempotent */
469 lapdm_channel_exit(&bts_to_ms_channel);
470 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100471}
472
Andreas Eversberg6e182082013-02-06 14:13:21 +0100473static void test_lapdm_contention_resolution()
474{
475 printf("I test contention resultion by having two mobiles collide and "
476 "first mobile repeating SABM.\n");
477
478 int rc;
479 struct lapdm_polling_state test_state;
480 struct osmo_phsap_prim pp;
Daniel Willmann09129352014-03-20 18:54:57 +0100481 uint8_t *cm2;
Andreas Eversberg6e182082013-02-06 14:13:21 +0100482
483 /* Configure LAPDm on both sides */
484 struct lapdm_channel bts_to_ms_channel;
485 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
486
487 memset(&test_state, 0, sizeof(test_state));
488 test_state.bts = &bts_to_ms_channel;
489
490 /* BTS to MS in polling mode */
491 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
492 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
493 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
494 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
495
496 /* Send SABM MS 1, we must get UA */
Daniel Willmann09129352014-03-20 18:54:57 +0100497 send_sabm(&bts_to_ms_channel, 0, cm, sizeof(cm));
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100498 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100499 CHECK_RC(rc);
500 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
501
502 /* Send SABM MS 2, we must get nothing, due to collision */
Daniel Willmann09129352014-03-20 18:54:57 +0100503 cm2 = malloc(sizeof(cm));
504 memcpy(cm2, cm, sizeof(cm));
505 cm2[0] += 1;
506 send_sabm(&bts_to_ms_channel, 0, cm2, sizeof(cm2));
507 free(cm2);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100508 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100509 OSMO_ASSERT(rc == -ENODEV);
510
511 /* Send SABM MS 1 again, we must get UA gain */
Daniel Willmann09129352014-03-20 18:54:57 +0100512 send_sabm(&bts_to_ms_channel, 0, cm, sizeof(cm));
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100513 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100514 CHECK_RC(rc);
515 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
516
517 /* clean up */
518 lapdm_channel_exit(&bts_to_ms_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200519
520 /* idempotent */
521 lapdm_channel_exit(&bts_to_ms_channel);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100522}
523
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100524static void test_lapdm_early_release()
525{
526 printf("I test RF channel release of an unestablished channel.\n");
527
528 int rc;
529 struct lapdm_polling_state test_state;
530
531 /* Configure LAPDm on both sides */
532 struct lapdm_channel bts_to_ms_channel;
533 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
534
535 memset(&test_state, 0, sizeof(test_state));
536 test_state.bts = &bts_to_ms_channel;
537
538 /* BTS to MS in polling mode */
539 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
540 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
541 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
542 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
543
544 /* Send the release request */
545 rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel);
546 OSMO_ASSERT(rc == -EINVAL);
547
548 /* clean up */
549 lapdm_channel_exit(&bts_to_ms_channel);
550
551 /* Check if exit is idempotent */
552 lapdm_channel_exit(&bts_to_ms_channel);
553}
554
555static void lapdm_establish(const uint8_t *est_req, size_t est_req_size)
556{
557 int rc;
558 struct lapdm_polling_state test_state;
559 struct osmo_phsap_prim pp;
560 struct msgb *msg;
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100561
562 /* Configure LAPDm on both sides */
563 struct lapdm_channel bts_to_ms_channel;
564 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
565
566 memset(&test_state, 0, sizeof(test_state));
567 test_state.bts = &bts_to_ms_channel;
568
569 /* BTS to MS in polling mode */
570 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
571 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
572 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
573 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
574
575 /* Send the release request */
576 msg = create_est_req(est_req, est_req_size);
577 rc = lapdm_rslms_recvmsg(msg, &bts_to_ms_channel);
578 fprintf(stderr, "recvmsg: got rc %d: %s\n", rc, rc <= 0 ? strerror(-rc) : "???");
579 OSMO_ASSERT(rc == 0);
580
581 /* Take message from queue */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100582 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
583 if (rc < 0)
584 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100585
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100586 CHECK_RC(rc);
587
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100588 OSMO_ASSERT(pp.oph.msg->data == msgb_l2(pp.oph.msg));
589
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100590 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100591 OSMO_ASSERT(rc < 0);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100592 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100593 OSMO_ASSERT(rc < 0);
594
595 /* clean up */
596 lapdm_channel_exit(&bts_to_ms_channel);
597
598 /* idempotent */
599 lapdm_channel_exit(&bts_to_ms_channel);
600}
601
602static void test_lapdm_establishment()
603{
604 printf("I test RF channel establishment.\n");
605 printf("Testing SAPI3/SDCCH\n");
606 lapdm_establish(est_req_sdcch_sapi3, sizeof(est_req_sdcch_sapi3));
607 printf("Testing SAPI3/SACCH\n");
608 lapdm_establish(est_req_sacch_sapi3, sizeof(est_req_sacch_sapi3));
609}
610
Daniel Willmann09129352014-03-20 18:54:57 +0100611
612const uint8_t cm_chg[] = {
613 0x01, 0x00, 0x49, 0x06, 0x16, 0x03, 0x33,
614 0x59, 0xa6, 0x20, 0x0a, 0x20, 0x04, 0x04,
615 0x2f, 0x65, 0x23, 0x02, 0x00, 0x24, 0x04
616};
617
618const uint8_t cm_chg_ack[] = {
619 0x01, 0x21, 0x01
620};
621
622const uint8_t gprs_susp[] = {
623 0x01, 0x02, 0x35, 0x06, 0x34, 0xe3, 0xd4,
624 0xd2, 0x6f, 0x09, 0xf1, 0x07, 0x00, 0x01, 0x00, 0x02
625};
626
Daniel Willmann09129352014-03-20 18:54:57 +0100627const uint8_t cipher_cmd[] = {
628 0x06, 0x35, 0x01
629};
630
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100631/* The cipher command we send to the MS after updating our N(R) */
Daniel Willmann09129352014-03-20 18:54:57 +0100632const uint8_t cipher_cmd_out[] = {
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100633 0x03, 0x40, 0x0d, 0x06, 0x35, 0x01
Daniel Willmann09129352014-03-20 18:54:57 +0100634};
635
636uint8_t cipher_compl[] = {
637 0x01, 0x24, 0x09, 0x06, 0x32
638};
639
640uint8_t cipher_compl_ack[] = {
641 0x01, 0x61, 0x01
642};
643
644static const uint8_t ua_sms[] = {
645 0x0d, 0x73, 0x01
646};
647
648const uint8_t cp_data_1[] = {
649 0x0d, 0x00, 0x53, 0x59, 0x01, 0x5c, 0x00,
650 0x4c, 0x00, 0x06, 0x91, 0x86, 0x77, 0x07,
651 0x00, 0xf9, 0x51, 0x11, 0x76, 0x05, 0x81, 0x29, 0x32
652};
653
654const uint8_t cp_data_1_ack[] = {
655 0x0d, 0x21, 0x01
656};
657
658static int bts_to_ms_dummy_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
659{
660 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
661 msgb_free(in_msg);
662
663 return 0;
664}
665
666static void dump_queue(struct llist_head *head)
667{
668 struct msgb* msg;
669
670 printf("\nDumping queue:\n");
671 llist_for_each_entry(msg, head, list)
672 printf("%s\n", msgb_hexdump(msg));
673 printf("\n");
674}
675
676static void test_lapdm_desync()
677{
678 printf("I test if desync problems exist in LAPDm\n");
679
680 int rc;
681 struct osmo_phsap_prim pp;
682
683 /* Configure LAPDm on both sides */
684 struct lapdm_channel bts_to_ms_channel;
685 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
686
687 /* BTS to MS in polling mode */
688 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
689 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
690 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, NULL);
691 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_dummy_tx_cb, NULL);
692 struct lapdm_datalink *dl = lapdm_datalink_for_sapi(&bts_to_ms_channel.lapdm_dcch, 0);
693 dl->mctx.dl = dl;
694 dl->dl.lctx.dl = &dl->dl;
695
696 /* Send SABM MS 1, we must get UA */
697 printf("\nEstablishing SAPI=0\n");
698 send_sabm(&bts_to_ms_channel, 0, cm, sizeof(cm));
699
700 dump_queue(&dl->dl.tx_queue);
701
702 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
703 CHECK_RC(rc);
704 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
705
706 printf("\nSending Classmark Change\n");
707 send_buf(cm_chg, sizeof(cm_chg), &bts_to_ms_channel);
708 dump_queue(&dl->dl.tx_queue);
709
710 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
711 CHECK_RC(rc);
712 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cm_chg_ack, ARRAY_SIZE(cm_chg_ack)) == 0);
713
714 printf("\nEnqueueing Ciphering Mode Command\n");
715 enqueue_buf(cipher_cmd, sizeof(cipher_cmd), 0, &bts_to_ms_channel);
716 dump_queue(&dl->dl.tx_queue);
717
718 printf("\nSending GPRS Suspend Request\n");
719 send_buf(gprs_susp, sizeof(gprs_susp), &bts_to_ms_channel);
720 dump_queue(&dl->dl.tx_queue);
721
722 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
723 CHECK_RC(rc);
724 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cipher_cmd_out, ARRAY_SIZE(cipher_cmd_out)) == 0);
725
726 printf("\nSending Cipher Mode Complete\n");
727 send_buf(cipher_compl, sizeof(cipher_compl), &bts_to_ms_channel);
728 dump_queue(&dl->dl.tx_queue);
729
730 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
731 CHECK_RC(rc);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100732 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cipher_compl_ack, ARRAY_SIZE(cipher_compl_ack)) == 0);
Daniel Willmann09129352014-03-20 18:54:57 +0100733
734 printf("\nEstablishing SAPI=3\n");
735 send_sabm(&bts_to_ms_channel, 3, NULL, 0);
736 dump_queue(&dl->dl.tx_queue);
737
738 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
739 CHECK_RC(rc);
740 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua_sms, ARRAY_SIZE(ua_sms)) == 0);
741
742 printf("\nSending CP-DATA\n");
743 send_buf(cp_data_1, sizeof(cp_data_1), &bts_to_ms_channel);
744 dump_queue(&dl->dl.tx_queue);
745
746 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
747 CHECK_RC(rc);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100748 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cp_data_1_ack, ARRAY_SIZE(cp_data_1_ack)) == 0);
Daniel Willmann09129352014-03-20 18:54:57 +0100749
750 /* clean up */
751 lapdm_channel_exit(&bts_to_ms_channel);
752
753 /* Check if exit is idempotent */
754 lapdm_channel_exit(&bts_to_ms_channel);
755}
756
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100757int main(int argc, char **argv)
758{
759 osmo_init_logging(&info);
760
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100761 /* Prevent the test from segfaulting */
762 dummy_l1_header_len = 0;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100763 test_lapdm_polling();
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100764
765 dummy_l1_header_len = 3;
Daniel Willmanne5233922012-12-25 23:15:50 +0100766 test_lapdm_early_release();
Andreas Eversberg6e182082013-02-06 14:13:21 +0100767 test_lapdm_contention_resolution();
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100768 test_lapdm_establishment();
Daniel Willmann09129352014-03-20 18:54:57 +0100769 test_lapdm_desync();
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100770
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100771 printf("Success.\n");
772
773 return 0;
774}