blob: e3223143bb2fd2c93073c682190956954e6ad171 [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
Daniel Willmann09129352014-03-20 18:54:57 +01004 * (C) 2014 by Daniel Willmann <dwillmann@sysmocom.de>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +01005 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (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
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +010023#include <osmocom/core/application.h>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010024#include <osmocom/core/logging.h>
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +000025#include <osmocom/core/utils.h>
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010026#include <osmocom/gsm/lapdm.h>
27#include <osmocom/gsm/rsl.h>
28
29#include <errno.h>
30
31#include <string.h>
32
33#define CHECK_RC(rc) \
34 if (rc != 0) { \
35 printf("Operation failed rc=%d on %s:%d\n", rc, __FILE__, __LINE__); \
36 abort(); \
37 }
38
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010039static struct log_info info = {};
Jacob Erlbeckc893c222014-01-28 10:53:59 +010040static int dummy_l1_header_len = 0;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010041
42struct lapdm_polling_state {
43 struct lapdm_channel *bts;
44 int bts_read;
45
46 struct lapdm_channel *ms;
47 int ms_read;
48};
49
50static struct msgb *msgb_from_array(const uint8_t *data, int len)
51{
52 struct msgb *msg = msgb_alloc_headroom(4096, 128, "data");
53 msg->l3h = msgb_put(msg, len);
Harald Welte53e26722017-01-04 11:18:59 +010054 if (data && len)
55 memcpy(msg->l3h, data, len);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010056 return msg;
57}
58
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010059/*
60 * Test data is below...
61 */
62static const uint8_t cm[] = {
63 0x05, 0x24, 0x31, 0x03, 0x50, 0x18, 0x93, 0x08,
64 0x29, 0x47, 0x80, 0x00, 0x00, 0x00, 0x00, 0x80,
65};
66
Andreas Eversberg6e182082013-02-06 14:13:21 +010067static const uint8_t ua[] = {
68 0x01, 0x73, 0x41, 0x05, 0x24, 0x31, 0x03, 0x50,
69 0x18, 0x93, 0x08, 0x29, 0x47, 0x80, 0x00, 0x00,
70 0x00, 0x00, 0x80, 0x2b, 0x2b, 0x2b, 0x2b
71};
72
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010073static const uint8_t mm[] = {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +010074 0x00, 0x0c, 0x00, 0x03, 0x01, 0x01, 0x20, 0x02,
75 0x00, 0x0b, 0x00, 0x03, 0x05, 0x04, 0x0d
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +010076};
77
78static const uint8_t dummy1[] = {
79 0xab, 0x03, 0x30, 0x60, 0x06,
80};
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010081
Daniel Willmanne5233922012-12-25 23:15:50 +010082static const uint8_t rel_req[] = {
83 0x02, 0x07, 0x01, 0x0a, 0x02, 0x40, 0x14, 0x01
84};
85
Jacob Erlbeck90937fe2014-01-24 13:48:19 +010086static const uint8_t est_req_sdcch_sapi3[] = {
87 0x02, 0x04, 0x01, 0x20, 0x02, 0x03
88};
89
90static const uint8_t est_req_sacch_sapi3[] = {
91 0x02, 0x04, 0x01, 0x0b, 0x02, 0x43
92};
93
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010094static struct msgb *create_cm_serv_req(void)
95{
96 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +010097
98 msg = msgb_from_array(cm, sizeof(cm));
99 rsl_rll_push_l3(msg, RSL_MT_EST_REQ, 0, 0, 1);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100100 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100101 return msg;
102}
103
104static struct msgb *create_mm_id_req(void)
105{
106 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100107
108 msg = msgb_from_array(mm, sizeof(mm));
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100109 msg->l2h = msg->data + 3;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000110 OSMO_ASSERT(msgb_l2len(msg) == 12);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100111 msg->l3h = msg->l2h + 6;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000112 OSMO_ASSERT(msgb_l3len(msg) == 6);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100113 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100114
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100115 return msg;
116}
117
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800118static struct msgb *create_empty_msg(void)
119{
120 struct msgb *msg;
121
122 msg = msgb_from_array(NULL, 0);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000123 OSMO_ASSERT(msgb_l3len(msg) == 0);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800124 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100125 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800126 return msg;
127}
128
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100129static struct msgb *create_dummy_data_req(void)
130{
131 struct msgb *msg;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100132
133 msg = msgb_from_array(dummy1, sizeof(dummy1));
134 rsl_rll_push_l3(msg, RSL_MT_DATA_REQ, 0, 0, 1);
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100135 msgb_push(msg, dummy_l1_header_len);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100136 return msg;
137}
138
Daniel Willmanne5233922012-12-25 23:15:50 +0100139static struct msgb *create_rel_req(void)
140{
141 struct msgb *msg;
142
143 msg = msgb_from_array(rel_req, sizeof(rel_req));
144 msg->l2h = msg->data;
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100145 msgb_push(msg, dummy_l1_header_len);
Daniel Willmanne5233922012-12-25 23:15:50 +0100146 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
147 return msg;
148}
149
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100150static struct msgb *create_est_req(const uint8_t *est_req, size_t est_req_size)
151{
152 struct msgb *msg;
153
154 msg = msgb_from_array(est_req, est_req_size);
155 msg->l2h = msg->data;
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100156 msgb_push(msg, dummy_l1_header_len);
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100157 msg->l3h = msg->l2h + sizeof(struct abis_rsl_rll_hdr);
158 return msg;
159}
160
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100161static int send(struct msgb *in_msg, struct lapdm_channel *chan)
162{
163 struct osmo_phsap_prim pp;
164 struct msgb *msg;
165 int rc;
166
167 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
168 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
169 PRIM_OP_INDICATION, msg);
170 /* copy over actual MAC block */
171 msg->l2h = msgb_put(msg, msgb_l2len(in_msg));
172 memcpy(msg->l2h, in_msg->l2h, msgb_l2len(in_msg));
173
174 /* LAPDm requires those... */
175 pp.u.data.chan_nr = 0;
176 pp.u.data.link_id = 0;
177 /* feed into the LAPDm code of libosmogsm */
178 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000179 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100180 return 0;
181}
182
Daniel Willmann09129352014-03-20 18:54:57 +0100183/* Receive from L1 */
184static int send_buf(const uint8_t *buf, size_t len, struct lapdm_channel *chan)
Andreas Eversberg6e182082013-02-06 14:13:21 +0100185{
186 struct osmo_phsap_prim pp;
187 struct msgb *msg;
188 int rc;
189
Daniel Willmann09129352014-03-20 18:54:57 +0100190 msg = msgb_from_array(buf, len);
191 msg->l2h = msg->l3h;
192 msg->l3h = NULL;
193 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
194 PRIM_OP_INDICATION, msg);
195
196 /* LAPDm requires those... */
197 pp.u.data.chan_nr = 0;
198 pp.u.data.link_id = 0;
199 /* feed into the LAPDm code of libosmogsm */
200 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
201 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
202 return 0;
203}
204
205/* Receive from L3 */
206static int enqueue_buf(const uint8_t *buf, size_t len, int sapi, struct lapdm_channel *chan)
207{
208 struct osmo_dlsap_prim dp;
209 struct msgb *msg;
210 int rc;
211 struct lapdm_datalink *dl;
212
213 dl = lapdm_datalink_for_sapi(&chan->lapdm_dcch, sapi);
214 OSMO_ASSERT(dl);
215
216 msg = msgb_from_array(buf, len);
217 osmo_prim_init(&dp.oph, 0, PRIM_DL_DATA, PRIM_OP_REQUEST, msg);
218
219 rc = lapd_recv_dlsap(&dp, &dl->dl.lctx);
220 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
221 return 0;
222}
223
224static int send_sabm(struct lapdm_channel *chan, int sapi, const uint8_t *data, size_t len)
225{
226 struct osmo_phsap_prim pp;
227 struct msgb *msg;
228 int rc;
229
230 OSMO_ASSERT(sapi == 0 || sapi == 3);
231
Andreas Eversberg6e182082013-02-06 14:13:21 +0100232 msg = msgb_alloc_headroom(128, 64, "PH-DATA.ind");
233 osmo_prim_init(&pp.oph, SAP_GSM_PH, PRIM_PH_DATA,
234 PRIM_OP_INDICATION, msg);
235 /* copy over actual MAC block */
Daniel Willmann09129352014-03-20 18:54:57 +0100236 msg->l2h = msgb_put(msg, 3 + len);
237 msg->l2h[0] = 0x01 | (sapi << 2);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100238 msg->l2h[1] = 0x3f;
Daniel Willmann09129352014-03-20 18:54:57 +0100239 msg->l2h[2] = 0x01 | (len << 2);
240
241 if (len > 0)
242 memcpy(msg->l2h + 3, data, len);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100243
244 /* LAPDm requires those... */
245 pp.u.data.chan_nr = 0;
246 pp.u.data.link_id = 0;
247 /* feed into the LAPDm code of libosmogsm */
248 rc = lapdm_phsap_up(&pp.oph, &chan->lapdm_dcch);
249 OSMO_ASSERT(rc == 0 || rc == -EBUSY);
250 return 0;
251}
252
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100253/*
254 * I get called from the LAPDm code when something was sent my way...
255 */
256static int bts_to_ms_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
257{
258 struct lapdm_polling_state *state = _ctx;
259
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100260
261 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100262
263
264 if (state->bts_read == 0) {
265 printf("BTS: Verifying CM request.\n");
Holger Hans Peter Freytherdd34ed52013-06-19 08:16:56 +0200266 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(cm));
267 OSMO_ASSERT(memcmp(in_msg->l3h, cm,
268 ARRAY_SIZE(cm)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100269 } else if (state->bts_read == 1) {
270 printf("BTS: Verifying dummy message.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000271 OSMO_ASSERT(msgb_l3len(in_msg) == ARRAY_SIZE(dummy1));
272 OSMO_ASSERT(memcmp(in_msg->l3h, dummy1,
273 ARRAY_SIZE(dummy1)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100274 } else {
275 printf("BTS: Do not know to verify: %d\n", state->bts_read);
276 }
277
278 state->bts_read += 1;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100279 msgb_free(in_msg);
280
281 return 0;
282}
283
284static int ms_to_bts_l1_cb(struct osmo_prim_hdr *oph, void *_ctx)
285{
286 int rc;
287 struct lapdm_polling_state *state = _ctx;
288 printf("%s: MS(us) -> BTS prim message\n", __func__);
289
290 /* i stuff it into the LAPDm channel of the BTS */
291 rc = send(oph->msg, state->bts);
292 msgb_free(oph->msg);
Holger Hans Peter Freytheraf723a42012-12-26 10:51:00 +0100293 return rc;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100294}
295
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100296static int dequeue_prim(struct lapdm_entity *le, struct osmo_phsap_prim *pp,
297 const char *queue_name)
298{
299 int rc;
300 int l2_header_len;
301 int l3_len = 0;
302
303 /* Take message from queue */
304 rc = lapdm_phsap_dequeue_prim(le, pp);
305
306 fprintf(stderr, "dequeue: got rc %d: %s\n", rc,
307 rc <= 0 ? strerror(-rc) : "-");
308
309 if (rc < 0)
310 return rc;
311
312 l2_header_len = msgb_l2len(pp->oph.msg);
313 if (msgb_l3(pp->oph.msg)) {
314 l3_len = msgb_l3len(pp->oph.msg);
315 l2_header_len -= l3_len;
316 } else
317 fprintf(stderr, "MSGB: L3 is undefined\n");
318
319 if (l2_header_len < 0 || l2_header_len > pp->oph.msg->data_len) {
320 fprintf(stderr,
321 "MSGB inconsistent: data = %p, l2 = %p, l3 = %p, tail = %p\n",
322 pp->oph.msg->data,
323 pp->oph.msg->l2h,
324 pp->oph.msg->l3h,
325 pp->oph.msg->tail);
326 l2_header_len = -1;
327 }
328
329 printf("Took message from %s queue: "
330 "L2 header size %d, L3 size %d, "
331 "SAP %#x, %d/%d, Link 0x%02x\n",
332 queue_name,
333 l2_header_len, l3_len,
334 pp->oph.sap, pp->oph.primitive, pp->oph.operation,
335 pp->u.data.link_id);
336 printf("Message: %s\n", msgb_hexdump(pp->oph.msg));
337
338 return rc;
339}
340
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100341static int ms_to_bts_tx_cb(struct msgb *msg, struct lapdm_entity *le, void *_ctx)
342{
343 struct lapdm_polling_state *state = _ctx;
344
345 printf("%s: BTS->MS(us) message %d\n", __func__, msgb_length(msg));
346
347 if (state->ms_read == 0) {
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100348 struct abis_rsl_rll_hdr hdr;
349
350 printf("MS: Verifying incoming primitive.\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000351 OSMO_ASSERT(msg->len == sizeof(struct abis_rsl_rll_hdr) + 3);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100352
353 /* verify the header */
354 memset(&hdr, 0, sizeof(hdr));
355 rsl_init_rll_hdr(&hdr, RSL_MT_EST_CONF);
356 hdr.c.msg_discr |= ABIS_RSL_MDISC_TRANSP;
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000357 OSMO_ASSERT(memcmp(msg->data, &hdr, sizeof(hdr)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100358
359 /* Verify the added RSL_IE_L3_INFO but we have a bug here */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000360 OSMO_ASSERT(msg->data[6] == RSL_IE_L3_INFO);
Holger Hans Peter Freyther4a075f82011-12-12 00:41:25 +0100361 #warning "RSL_IE_L3_INFO 16 bit length is wrong"
Holger Hans Peter Freyther3cc268c2013-06-19 08:30:59 +0200362 /* This should be okay but it is actually 0x0, 0x9c on ia-32 */
363 /* OSMO_ASSERT(msg->data[7] == 0x0 && msg->data[8] == 0x0); */
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100364 } else if (state->ms_read == 1) {
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100365 printf("MS: Verifying incoming MM message: %d\n", msgb_l3len(msg));
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000366 OSMO_ASSERT(msgb_l3len(msg) == 3);
367 OSMO_ASSERT(memcmp(msg->l3h, &mm[12], msgb_l3len(msg)) == 0);
Holger Hans Peter Freyther15f740c2011-12-12 00:29:50 +0100368 } else {
369 printf("MS: Do not know to verify: %d\n", state->ms_read);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100370 }
371
372 state->ms_read += 1;
373 msgb_free(msg);
374 return 0;
375}
376
377static void test_lapdm_polling()
378{
379 printf("I do some very simple LAPDm test.\n");
380
381 int rc;
382 struct lapdm_polling_state test_state;
383 struct osmo_phsap_prim pp;
384
385 /* Configure LAPDm on both sides */
386 struct lapdm_channel bts_to_ms_channel;
387 struct lapdm_channel ms_to_bts_channel;
388 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
389 memset(&ms_to_bts_channel, 0, sizeof(ms_to_bts_channel));
390
391 memset(&test_state, 0, sizeof(test_state));
392 test_state.bts = &bts_to_ms_channel;
393 test_state.ms = &ms_to_bts_channel;
394
395 /* BTS to MS in polling mode */
396 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
397 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
398 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
399 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
400
401 /* MS to BTS in direct mode */
402 lapdm_channel_init(&ms_to_bts_channel, LAPDM_MODE_MS);
403 lapdm_channel_set_l1(&ms_to_bts_channel, ms_to_bts_l1_cb, &test_state);
404 lapdm_channel_set_l3(&ms_to_bts_channel, ms_to_bts_tx_cb, &test_state);
405
406 /*
407 * We try to send messages from the MS to the BTS to the MS..
408 */
409 /* 1. Start with MS -> BTS, BTS should have a pending message */
410 printf("Establishing link.\n");
411 lapdm_rslms_recvmsg(create_cm_serv_req(), &ms_to_bts_channel);
412
413 /* 2. Poll on the BTS for sending out a confirmation */
414 printf("\nConfirming\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000415 OSMO_ASSERT(test_state.bts_read == 1);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100416 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100417 CHECK_RC(rc);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000418 OSMO_ASSERT(pp.oph.msg->data == pp.oph.msg->l2h);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100419 send(pp.oph.msg, &ms_to_bts_channel);
420 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000421 OSMO_ASSERT(test_state.ms_read == 1);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100422
423 /* 3. Send some data to the MS */
424 printf("\nSending back to MS\n");
425 lapdm_rslms_recvmsg(create_mm_id_req(), &bts_to_ms_channel);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100426 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100427 CHECK_RC(rc);
428 send(pp.oph.msg, &ms_to_bts_channel);
429 msgb_free(pp.oph.msg);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000430 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100431
432 /* verify that there is nothing more to poll */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100433 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000434 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100435
436 /* 3. And back to the BTS */
437 printf("\nSending back to BTS\n");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000438 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100439 lapdm_rslms_recvmsg(create_dummy_data_req(), &ms_to_bts_channel);
440
441
Holger Hans Peter Freyther3a5f08c2012-01-12 23:12:28 +0100442 /* 4. And back to the MS, but let's move data/l2h apart */
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000443 OSMO_ASSERT(test_state.bts_read == 2);
444 OSMO_ASSERT(test_state.ms_read == 2);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100445 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100446 CHECK_RC(rc);
447 send(pp.oph.msg, &ms_to_bts_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000448 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100449 msgb_free(pp.oph.msg);
450
451 /* verify that there is nothing more to poll */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100452 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000453 OSMO_ASSERT(rc < 0);
Jacob Erlbeck7d5f17a2014-02-28 15:54:03 +0100454 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
455 OSMO_ASSERT(rc < 0);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100456
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800457 /* check sending an empty L3 message fails */
458 rc = lapdm_rslms_recvmsg(create_empty_msg(), &bts_to_ms_channel);
Katerina Barone-Adesi008e53b2013-03-03 10:36:52 +0000459 OSMO_ASSERT(rc == -1);
460 OSMO_ASSERT(test_state.ms_read == 2);
Holger Hans Peter Freyther90656db2012-01-13 05:49:29 +0800461
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100462 /* clean up */
463 lapdm_channel_exit(&bts_to_ms_channel);
464 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200465
466 /* Check if exit is idempotent */
467 lapdm_channel_exit(&bts_to_ms_channel);
468 lapdm_channel_exit(&ms_to_bts_channel);
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100469}
470
Andreas Eversberg6e182082013-02-06 14:13:21 +0100471static void test_lapdm_contention_resolution()
472{
473 printf("I test contention resultion by having two mobiles collide and "
474 "first mobile repeating SABM.\n");
475
476 int rc;
477 struct lapdm_polling_state test_state;
478 struct osmo_phsap_prim pp;
Daniel Willmann09129352014-03-20 18:54:57 +0100479 uint8_t *cm2;
Andreas Eversberg6e182082013-02-06 14:13:21 +0100480
481 /* Configure LAPDm on both sides */
482 struct lapdm_channel bts_to_ms_channel;
483 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
484
485 memset(&test_state, 0, sizeof(test_state));
486 test_state.bts = &bts_to_ms_channel;
487
488 /* BTS to MS in polling mode */
489 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
490 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
491 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
492 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
493
494 /* Send SABM MS 1, we must get UA */
Daniel Willmann09129352014-03-20 18:54:57 +0100495 send_sabm(&bts_to_ms_channel, 0, cm, sizeof(cm));
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100496 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100497 CHECK_RC(rc);
498 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
499
500 /* Send SABM MS 2, we must get nothing, due to collision */
Daniel Willmann09129352014-03-20 18:54:57 +0100501 cm2 = malloc(sizeof(cm));
502 memcpy(cm2, cm, sizeof(cm));
503 cm2[0] += 1;
504 send_sabm(&bts_to_ms_channel, 0, cm2, sizeof(cm2));
505 free(cm2);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100506 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100507 OSMO_ASSERT(rc == -ENODEV);
508
509 /* Send SABM MS 1 again, we must get UA gain */
Daniel Willmann09129352014-03-20 18:54:57 +0100510 send_sabm(&bts_to_ms_channel, 0, cm, sizeof(cm));
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100511 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Andreas Eversberg6e182082013-02-06 14:13:21 +0100512 CHECK_RC(rc);
513 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
514
515 /* clean up */
516 lapdm_channel_exit(&bts_to_ms_channel);
Holger Hans Peter Freytherce397de2013-10-26 13:35:15 +0200517
518 /* idempotent */
519 lapdm_channel_exit(&bts_to_ms_channel);
Andreas Eversberg6e182082013-02-06 14:13:21 +0100520}
521
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100522static void test_lapdm_early_release()
523{
524 printf("I test RF channel release of an unestablished channel.\n");
525
526 int rc;
527 struct lapdm_polling_state test_state;
528
529 /* Configure LAPDm on both sides */
530 struct lapdm_channel bts_to_ms_channel;
531 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
532
533 memset(&test_state, 0, sizeof(test_state));
534 test_state.bts = &bts_to_ms_channel;
535
536 /* BTS to MS in polling mode */
537 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
538 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
539 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
540 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
541
542 /* Send the release request */
543 rc = lapdm_rslms_recvmsg(create_rel_req(), &bts_to_ms_channel);
544 OSMO_ASSERT(rc == -EINVAL);
545
546 /* clean up */
547 lapdm_channel_exit(&bts_to_ms_channel);
548
549 /* Check if exit is idempotent */
550 lapdm_channel_exit(&bts_to_ms_channel);
551}
552
553static void lapdm_establish(const uint8_t *est_req, size_t est_req_size)
554{
555 int rc;
556 struct lapdm_polling_state test_state;
557 struct osmo_phsap_prim pp;
558 struct msgb *msg;
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100559
560 /* Configure LAPDm on both sides */
561 struct lapdm_channel bts_to_ms_channel;
562 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
563
564 memset(&test_state, 0, sizeof(test_state));
565 test_state.bts = &bts_to_ms_channel;
566
567 /* BTS to MS in polling mode */
568 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
569 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
570 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, &test_state);
571 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_tx_cb, &test_state);
572
573 /* Send the release request */
574 msg = create_est_req(est_req, est_req_size);
575 rc = lapdm_rslms_recvmsg(msg, &bts_to_ms_channel);
576 fprintf(stderr, "recvmsg: got rc %d: %s\n", rc, rc <= 0 ? strerror(-rc) : "???");
577 OSMO_ASSERT(rc == 0);
578
579 /* Take message from queue */
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100580 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
581 if (rc < 0)
582 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100583
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100584 CHECK_RC(rc);
585
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100586 OSMO_ASSERT(pp.oph.msg->data == msgb_l2(pp.oph.msg));
587
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100588 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100589 OSMO_ASSERT(rc < 0);
Jacob Erlbeck2462cf62014-02-25 10:49:00 +0100590 rc = dequeue_prim(&bts_to_ms_channel.lapdm_acch, &pp, "ACCH");
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100591 OSMO_ASSERT(rc < 0);
592
593 /* clean up */
594 lapdm_channel_exit(&bts_to_ms_channel);
595
596 /* idempotent */
597 lapdm_channel_exit(&bts_to_ms_channel);
598}
599
600static void test_lapdm_establishment()
601{
602 printf("I test RF channel establishment.\n");
603 printf("Testing SAPI3/SDCCH\n");
604 lapdm_establish(est_req_sdcch_sapi3, sizeof(est_req_sdcch_sapi3));
605 printf("Testing SAPI3/SACCH\n");
606 lapdm_establish(est_req_sacch_sapi3, sizeof(est_req_sacch_sapi3));
607}
608
Daniel Willmann09129352014-03-20 18:54:57 +0100609
610const uint8_t cm_chg[] = {
611 0x01, 0x00, 0x49, 0x06, 0x16, 0x03, 0x33,
612 0x59, 0xa6, 0x20, 0x0a, 0x20, 0x04, 0x04,
613 0x2f, 0x65, 0x23, 0x02, 0x00, 0x24, 0x04
614};
615
616const uint8_t cm_chg_ack[] = {
617 0x01, 0x21, 0x01
618};
619
620const uint8_t gprs_susp[] = {
621 0x01, 0x02, 0x35, 0x06, 0x34, 0xe3, 0xd4,
622 0xd2, 0x6f, 0x09, 0xf1, 0x07, 0x00, 0x01, 0x00, 0x02
623};
624
Daniel Willmann09129352014-03-20 18:54:57 +0100625const uint8_t cipher_cmd[] = {
626 0x06, 0x35, 0x01
627};
628
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100629/* The cipher command we send to the MS after updating our N(R) */
Daniel Willmann09129352014-03-20 18:54:57 +0100630const uint8_t cipher_cmd_out[] = {
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100631 0x03, 0x40, 0x0d, 0x06, 0x35, 0x01
Daniel Willmann09129352014-03-20 18:54:57 +0100632};
633
634uint8_t cipher_compl[] = {
635 0x01, 0x24, 0x09, 0x06, 0x32
636};
637
638uint8_t cipher_compl_ack[] = {
639 0x01, 0x61, 0x01
640};
641
642static const uint8_t ua_sms[] = {
643 0x0d, 0x73, 0x01
644};
645
646const uint8_t cp_data_1[] = {
647 0x0d, 0x00, 0x53, 0x59, 0x01, 0x5c, 0x00,
648 0x4c, 0x00, 0x06, 0x91, 0x86, 0x77, 0x07,
649 0x00, 0xf9, 0x51, 0x11, 0x76, 0x05, 0x81, 0x29, 0x32
650};
651
652const uint8_t cp_data_1_ack[] = {
653 0x0d, 0x21, 0x01
654};
655
656static int bts_to_ms_dummy_tx_cb(struct msgb *in_msg, struct lapdm_entity *le, void *_ctx)
657{
658 printf("%s: MS->BTS(us) message %d\n", __func__, msgb_length(in_msg));
659 msgb_free(in_msg);
660
661 return 0;
662}
663
664static void dump_queue(struct llist_head *head)
665{
666 struct msgb* msg;
667
668 printf("\nDumping queue:\n");
669 llist_for_each_entry(msg, head, list)
670 printf("%s\n", msgb_hexdump(msg));
671 printf("\n");
672}
673
674static void test_lapdm_desync()
675{
676 printf("I test if desync problems exist in LAPDm\n");
677
678 int rc;
679 struct osmo_phsap_prim pp;
680
681 /* Configure LAPDm on both sides */
682 struct lapdm_channel bts_to_ms_channel;
683 memset(&bts_to_ms_channel, 0, sizeof(bts_to_ms_channel));
684
685 /* BTS to MS in polling mode */
686 lapdm_channel_init(&bts_to_ms_channel, LAPDM_MODE_BTS);
687 lapdm_channel_set_flags(&bts_to_ms_channel, LAPDM_ENT_F_POLLING_ONLY);
688 lapdm_channel_set_l1(&bts_to_ms_channel, NULL, NULL);
689 lapdm_channel_set_l3(&bts_to_ms_channel, bts_to_ms_dummy_tx_cb, NULL);
690 struct lapdm_datalink *dl = lapdm_datalink_for_sapi(&bts_to_ms_channel.lapdm_dcch, 0);
691 dl->mctx.dl = dl;
692 dl->dl.lctx.dl = &dl->dl;
693
694 /* Send SABM MS 1, we must get UA */
695 printf("\nEstablishing SAPI=0\n");
696 send_sabm(&bts_to_ms_channel, 0, cm, sizeof(cm));
697
698 dump_queue(&dl->dl.tx_queue);
699
700 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
701 CHECK_RC(rc);
702 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua, ARRAY_SIZE(ua)) == 0);
703
704 printf("\nSending Classmark Change\n");
705 send_buf(cm_chg, sizeof(cm_chg), &bts_to_ms_channel);
706 dump_queue(&dl->dl.tx_queue);
707
708 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
709 CHECK_RC(rc);
710 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cm_chg_ack, ARRAY_SIZE(cm_chg_ack)) == 0);
711
712 printf("\nEnqueueing Ciphering Mode Command\n");
713 enqueue_buf(cipher_cmd, sizeof(cipher_cmd), 0, &bts_to_ms_channel);
714 dump_queue(&dl->dl.tx_queue);
715
716 printf("\nSending GPRS Suspend Request\n");
717 send_buf(gprs_susp, sizeof(gprs_susp), &bts_to_ms_channel);
718 dump_queue(&dl->dl.tx_queue);
719
720 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
721 CHECK_RC(rc);
722 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cipher_cmd_out, ARRAY_SIZE(cipher_cmd_out)) == 0);
723
724 printf("\nSending Cipher Mode Complete\n");
725 send_buf(cipher_compl, sizeof(cipher_compl), &bts_to_ms_channel);
726 dump_queue(&dl->dl.tx_queue);
727
728 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
729 CHECK_RC(rc);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100730 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, cipher_compl_ack, ARRAY_SIZE(cipher_compl_ack)) == 0);
Daniel Willmann09129352014-03-20 18:54:57 +0100731
732 printf("\nEstablishing SAPI=3\n");
733 send_sabm(&bts_to_ms_channel, 3, NULL, 0);
734 dump_queue(&dl->dl.tx_queue);
735
736 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
737 CHECK_RC(rc);
738 OSMO_ASSERT(memcmp(pp.oph.msg->l2h, ua_sms, ARRAY_SIZE(ua_sms)) == 0);
739
740 printf("\nSending CP-DATA\n");
741 send_buf(cp_data_1, sizeof(cp_data_1), &bts_to_ms_channel);
742 dump_queue(&dl->dl.tx_queue);
743
744 rc = dequeue_prim(&bts_to_ms_channel.lapdm_dcch, &pp, "DCCH");
745 CHECK_RC(rc);
Daniel Willmann3dc4e162014-03-20 19:24:48 +0100746 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 +0100747
748 /* clean up */
749 lapdm_channel_exit(&bts_to_ms_channel);
750
751 /* Check if exit is idempotent */
752 lapdm_channel_exit(&bts_to_ms_channel);
753}
754
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100755int main(int argc, char **argv)
756{
757 osmo_init_logging(&info);
758
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100759 /* Prevent the test from segfaulting */
760 dummy_l1_header_len = 0;
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100761 test_lapdm_polling();
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100762
763 dummy_l1_header_len = 3;
Daniel Willmanne5233922012-12-25 23:15:50 +0100764 test_lapdm_early_release();
Andreas Eversberg6e182082013-02-06 14:13:21 +0100765 test_lapdm_contention_resolution();
Jacob Erlbeck90937fe2014-01-24 13:48:19 +0100766 test_lapdm_establishment();
Daniel Willmann09129352014-03-20 18:54:57 +0100767 test_lapdm_desync();
Jacob Erlbeckc893c222014-01-28 10:53:59 +0100768
Holger Hans Peter Freyther72bd4eb2011-12-11 20:25:12 +0100769 printf("Success.\n");
770
771 return 0;
772}