blob: 6a1a6676b0c9bce55a91df5bc916ff08b38707c7 [file] [log] [blame]
Daniel Willmann97374c02015-12-03 09:37:58 +01001/* Test HNB */
2
3/* (C) 2015 by Daniel Willmann <dwillmann@sysmocom.de>
4 * (C) 2015 by Sysmocom s.f.m.c. GmbH
5 * 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 Affero General Public License as published by
9 * the Free Software Foundation; either version 3 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 Affero General Public License for more details.
16 *
17 * 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/>.
19 *
20 */
21
22#include <unistd.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <getopt.h>
27#include <errno.h>
28#include <signal.h>
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <netinet/sctp.h>
34#include <arpa/inet.h>
35
36#include <osmocom/core/application.h>
37#include <osmocom/core/talloc.h>
38#include <osmocom/core/select.h>
39#include <osmocom/core/logging.h>
40#include <osmocom/core/socket.h>
41#include <osmocom/core/msgb.h>
42#include <osmocom/core/write_queue.h>
Harald Weltec3851222015-12-24 15:41:21 +010043#include <osmocom/netif/stream.h>
Neels Hofmeyrae937122016-02-29 09:32:00 +010044#include <osmocom/gsm/tlv.h>
45#include <osmocom/gsm/gsm48.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010046
47#include <osmocom/vty/telnet_interface.h>
48#include <osmocom/vty/logging.h>
Harald Weltec3851222015-12-24 15:41:21 +010049#include <osmocom/vty/command.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010050
51#include "hnb-test.h"
Daniel Willmanna1e202e2015-12-07 17:21:07 +010052#include "hnbap_common.h"
53#include "hnbap_ies_defs.h"
Harald Welteb66c5d02016-01-03 18:04:28 +010054#include "rua_msg_factory.h"
Harald Weltec3851222015-12-24 15:41:21 +010055#include "asn1helpers.h"
Neels Hofmeyr96979af2016-01-05 15:19:44 +010056#include <osmocom/ranap/iu_helpers.h>
Harald Welte87ffeb92015-12-25 15:34:22 +010057#include "test_common.h"
Harald Weltec3851222015-12-24 15:41:21 +010058
Neels Hofmeyr96979af2016-01-05 15:19:44 +010059#include <osmocom/ranap/ranap_msg_factory.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010060
Neels Hofmeyr0968a582016-01-11 15:19:38 +010061#include <osmocom/rua/RUA_RUA-PDU.h>
62
Neels Hofmeyr860a1292016-02-18 23:03:15 +010063#include <osmocom/gsm/protocol/gsm_04_08.h>
64
65#include <osmocom/ranap/RANAP_ProcedureCode.h>
66#include <osmocom/ranap/RANAP_Criticality.h>
67#include <osmocom/ranap/RANAP_DirectTransfer.h>
68
Daniel Willmann97374c02015-12-03 09:37:58 +010069static void *tall_hnb_ctx;
Daniel Willmann97374c02015-12-03 09:37:58 +010070
71struct hnb_test g_hnb_test = {
72 .gw_port = IUH_DEFAULT_SCTP_PORT,
73};
74
Harald Weltec3851222015-12-24 15:41:21 +010075struct msgb *rua_new_udt(struct msgb *inmsg);
76
Harald Weltec3851222015-12-24 15:41:21 +010077static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test)
Daniel Willmann19dedbb2015-12-17 11:57:41 +010078{
79 struct msgb *msg;
80 int rc, imsi_len;
81 uint32_t ctx_id;
82
83 UEDe_Register_t dereg;
84 UEDe_RegisterIEs_t dereg_ies;
85 memset(&dereg_ies, 0, sizeof(dereg_ies));
86
87 asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id);
88 dereg_ies.cause.present = Cause_PR_radioNetwork;
89 dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost;
90
91 memset(&dereg, 0, sizeof(dereg));
92 rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies);
93
94 msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register,
95 Criticality_ignore,
96 &asn_DEF_UEDe_Register,
97 &dereg);
98
Harald Weltec3851222015-12-24 15:41:21 +010099 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100100
Harald Weltec3851222015-12-24 15:41:21 +0100101 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100102
103 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
104}
105
Harald Weltec3851222015-12-24 15:41:21 +0100106static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str)
Daniel Willmann479cb302015-12-09 17:54:59 +0100107{
Daniel Willmann4e312502015-12-09 17:59:24 +0100108 struct msgb *msg;
109 int rc, imsi_len;
110
111 char imsi_buf[16];
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100112
Daniel Willmann4e312502015-12-09 17:59:24 +0100113 UERegisterRequest_t request_out;
114 UERegisterRequestIEs_t request;
115 memset(&request, 0, sizeof(request));
116
117 request.uE_Identity.present = UE_Identity_PR_iMSI;
118
Harald Welte056984f2016-01-03 16:31:31 +0100119 imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str);
Harald Weltec3851222015-12-24 15:41:21 +0100120 OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, imsi_buf, imsi_len);
Daniel Willmann4e312502015-12-09 17:59:24 +0100121
122 request.registration_Cause = Registration_Cause_normal;
123 request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6;
124 request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable;
125
126 memset(&request_out, 0, sizeof(request_out));
127 rc = hnbap_encode_ueregisterrequesties(&request_out, &request);
128
129 msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister,
130 Criticality_reject,
131 &asn_DEF_UERegisterRequest,
132 &request_out);
133
Harald Weltec3851222015-12-24 15:41:21 +0100134 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out);
Daniel Willmann4e312502015-12-09 17:59:24 +0100135
Harald Weltec3851222015-12-24 15:41:21 +0100136 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4e312502015-12-09 17:59:24 +0100137
138 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
Daniel Willmann479cb302015-12-09 17:54:59 +0100139}
140
Harald Weltec3851222015-12-24 15:41:21 +0100141static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmann479cb302015-12-09 17:54:59 +0100142{
143 int rc;
144 HNBRegisterAcceptIEs_t accept;
145
146 rc = hnbap_decode_hnbregisteraccepties(&accept, in);
147 if (rc < 0) {
148 }
149
150 hnb->rnc_id = accept.rnc_id;
151 printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id);
152
Daniel Willmann11e912a2016-01-07 13:19:30 +0100153 hnbap_free_hnbregisteraccepties(&accept);
Harald Weltec3851222015-12-24 15:41:21 +0100154 return 0;
Daniel Willmann479cb302015-12-09 17:54:59 +0100155}
156
Harald Weltec3851222015-12-24 15:41:21 +0100157static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmanna7b02402015-12-09 19:05:09 +0100158{
159 int rc;
160 uint32_t ctx_id;
161 UERegisterAcceptIEs_t accept;
162 char imsi[16];
163
164 rc = hnbap_decode_ueregisteraccepties(&accept, in);
165 if (rc < 0) {
166 return rc;
167 }
168
169 if (accept.uE_Identity.present != UE_Identity_PR_iMSI) {
170 printf("Wrong type in UE register accept\n");
171 return -1;
172 }
173
174 ctx_id = asn1bitstr_to_u24(&accept.context_ID);
175
Harald Welte056984f2016-01-03 16:31:31 +0100176 ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf,
Daniel Willmanna7b02402015-12-09 19:05:09 +0100177 accept.uE_Identity.choice.iMSI.size);
178 printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id);
179
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100180 hnb->ctx_id = ctx_id;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100181 hnbap_free_ueregisteraccepties(&accept);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100182
Daniel Willmanna7b02402015-12-09 19:05:09 +0100183 return 0;
184}
185
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100186static struct msgb *gen_nas_id_resp()
187{
188 uint8_t id_resp[] = {
Neels Hofmeyr5c1cc8c2016-02-29 09:28:48 +0100189 GSM48_PDISC_MM,
190 GSM48_MT_MM_ID_RESP,
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100191 /* IMEISV */
192 0x09, /* len */
193 0x03, /* first digit (0000) + even (0) + id IMEISV (011) */
194 0x31, 0x91, 0x06, 0x00, 0x28, 0x47, 0x11, /* digits */
195 0xf2, /* filler (1111) + last digit (0010) */
196 };
197
Neels Hofmeyre1f709f2016-02-28 00:50:45 +0100198 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100199}
200
Neels Hofmeyrae937122016-02-29 09:32:00 +0100201static struct msgb *gen_nas_tmsi_realloc_compl()
202{
203 uint8_t id_resp[] = {
204 GSM48_PDISC_MM,
205 GSM48_MT_MM_TMSI_REALL_COMPL,
206 };
207
208 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
209}
210
211static int hnb_test_nas_tx_dt(struct hnb_test *hnb, struct msgb *txm)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100212{
213 struct hnbtest_chan *chan;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100214 struct msgb *rua;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100215
216 chan = hnb->cs.chan;
217 if (!chan) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100218 printf("hnb_test_nas_tx_tmsi_realloc_compl(): No CS channel established yet.\n");
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100219 return -1;
220 }
221
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100222 rua = rua_new_dt(chan->is_ps, chan->conn_id, txm);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100223 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100224 return 0;
225}
226
Neels Hofmeyrae937122016-02-29 09:32:00 +0100227void hnb_test_nas_rx_lu_accept(struct msgb *rxm)
228{
229 printf(" :D Location Update Accept :D\n");
230 struct gsm48_hdr *gh;
231 struct gsm48_loc_area_id *lai;
232 gh = (struct gsm48_hdr *)msgb_l3(rxm);
233 lai = (struct gsm48_loc_area_id *)&gh->data[0];
234
235 uint16_t mcc, mnc, lac;
236 gsm48_decode_lai(lai, &mcc, &mnc, &lac);
237 printf("LU: mcc %hd mnc %hd lac %hd\n",
238 mcc, mnc, lac);
239
240}
241
242void hnb_test_nas_rx_mm_info(struct msgb *rxm)
243{
244 printf(" :) MM Info :)\n");
245 struct gsm48_hdr *gh;
246 struct tlv_parsed tp;
247 int parse_res;
248 int length = msgb_l3len(rxm);
249
250 if (length < sizeof(*gh)) {
251 printf("GSM48 header does not fit.\n");
252 return;
253 }
254
255 gh = (struct gsm48_hdr *) msgb_l3(rxm);
256 length -= (const char *)&gh->data[0] - (const char *)gh;
257
258 parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], length, 0, 0);
259 if (parse_res <= 0) {
260 printf("Error parsing MM Info message: %d\n", parse_res);
261 return;
262 }
263
264 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_SHORT)) {
265 char name[128] = {0};
266 gsm_7bit_decode_n(name, 127,
267 TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)+1,
268 (TLVP_LEN(&tp, GSM48_IE_NAME_SHORT)-1)*8/7);
269 printf("Info: Short Network Name: %s\n", name);
270 }
271
272 if (TLVP_PRESENT(&tp, GSM48_IE_NAME_LONG)) {
273 char name[128] = {0};
274 gsm_7bit_decode_n(name, 127,
275 TLVP_VAL(&tp, GSM48_IE_NAME_LONG)+1,
276 (TLVP_LEN(&tp, GSM48_IE_NAME_LONG)-1)*8/7);
277 printf("Info: Long Network Name: %s\n", name);
278 }
279
280 return;
281}
282
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100283static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct msgb *rxm)
284{
285 struct hnbtest_chan *chan;
286
287 chan = hnb->cs.chan;
288 if (!chan) {
289 printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n");
290 return -1;
291 }
292
293 OSMO_ASSERT(!chan->is_ps);
294
295 struct gsm48_hdr *gh = msgb_l3(rxm);
296 uint8_t msg_type = gh->msg_type & 0xbf;
297
298 switch (msg_type) {
299 case GSM48_MT_MM_ID_REQ:
Neels Hofmeyrae937122016-02-29 09:32:00 +0100300 return hnb_test_nas_tx_dt(hnb, gen_nas_id_resp());
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100301
Neels Hofmeyrae937122016-02-29 09:32:00 +0100302 case GSM48_MT_MM_LOC_UPD_ACCEPT:
303 hnb_test_nas_rx_lu_accept(rxm);
304 return hnb_test_nas_tx_dt(hnb, gen_nas_tmsi_realloc_compl());
305
306 case GSM48_MT_MM_INFO:
307 hnb_test_nas_rx_mm_info(rxm);
308 return 0;
309
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100310 default:
Neels Hofmeyrae937122016-02-29 09:32:00 +0100311 printf("04.08 message type not handled by hnb-test: 0x%x\n",
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100312 msg_type);
313 return 0;
314 }
315
316}
317
318static int hnb_test_nas_rx_dtap(struct hnb_test *hnb, struct msgb *msg)
319{
320 printf("got %s\n", osmo_hexdump(msg->data, msg->len));
321
322 // nas_pdu == '05 08 12' ==> IMEI Identity request
323 // '05 04 0d' ==> LU reject
324
325 struct gsm48_hdr *gh = msgb_l3(msg);
326 uint8_t pdisc = gh->proto_discr & 0x0f;
327
328 switch (pdisc) {
329 case GSM48_PDISC_MM:
330 return hnb_test_nas_rx_mm(hnb, msg);
331 default:
332 printf("04.08 discriminator not handled by hnb-test: %d\n",
333 pdisc);
334 return 0;
335 }
336
337
338}
339
Daniel Willmann479cb302015-12-09 17:54:59 +0100340int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
341{
342 HNBAP_PDU_t _pdu, *pdu = &_pdu;
343 asn_dec_rval_t dec_ret;
344 int rc;
345
346 memset(pdu, 0, sizeof(*pdu));
347 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
348 msg->data, msgb_length(msg), 0, 0);
349 if (dec_ret.code != RC_OK) {
350 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
351 return rc;
352 }
353
354 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
355 printf("Unexpected HNBAP message received\n");
356 }
357
358 switch (pdu->choice.successfulOutcome.procedureCode) {
359 case ProcedureCode_id_HNBRegister:
360 /* Get HNB id and send UE Register request */
361 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
362 break;
363 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100364 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100365 break;
366 default:
367 break;
368 }
369
370 return rc;
371}
372
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100373extern void direct_transfer_nas_pdu_print(ANY_t *in);
374
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100375int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
376{
377 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
378 asn_dec_rval_t dec_ret;
379 int rc;
380
381 memset(pdu, 0, sizeof(*pdu));
382 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
383 msg->data, msgb_length(msg), 0, 0);
384 if (dec_ret.code != RC_OK) {
385 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
386 return rc;
387 }
388
389 switch (pdu->present) {
390 case RUA_RUA_PDU_PR_successfulOutcome:
391 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
392 break;
393 case RUA_RUA_PDU_PR_initiatingMessage:
394 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
395 break;
396 case RUA_RUA_PDU_PR_NOTHING:
397 printf("RUA_RUA_PDU_PR_NOTHING\n");
398 break;
399 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
400 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
401 break;
402 default:
403 printf("Unexpected RUA message received\n");
404 break;
405 }
406
407 switch (pdu->choice.successfulOutcome.procedureCode) {
408 case RUA_ProcedureCode_id_ConnectionlessTransfer:
409 printf("RUA rx Connectionless Transfer\n");
410 break;
411 case RUA_ProcedureCode_id_Connect:
412 printf("RUA rx Connect\n");
413 break;
414 case RUA_ProcedureCode_id_DirectTransfer:
415 printf("RUA rx DirectTransfer\n");
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100416 {
417 struct msgb *m = msgb_alloc(1500, "direct_transfer_nas_pdu");
418 direct_transfer_nas_pdu_get(&pdu->choice.successfulOutcome.value, m);
419
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100420 hnb_test_nas_rx_dtap(hnb, m);
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100421
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100422 msgb_free(m);
423 }
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100424 break;
425 case RUA_ProcedureCode_id_Disconnect:
426 printf("RUA rx Disconnect\n");
427 break;
428 case RUA_ProcedureCode_id_ErrorIndication:
429 printf("RUA rx ErrorIndication\n");
430 break;
431 case RUA_ProcedureCode_id_privateMessage:
432 printf("RUA rx privateMessage\n");
433 break;
434 default:
435 printf("RUA rx unknown message\n");
436 break;
437 }
438
439 return rc;
440}
441
Daniel Willmann97374c02015-12-03 09:37:58 +0100442static int hnb_read_cb(struct osmo_fd *fd)
443{
444 struct hnb_test *hnb_test = fd->data;
445 struct sctp_sndrcvinfo sinfo;
446 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
447 int flags = 0;
448 int rc;
449
450 if (!msg)
451 return -ENOMEM;
452
453 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
454 NULL, NULL, &sinfo, &flags);
455 if (rc < 0) {
456 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
457 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100458 close(fd->fd);
459 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100460 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100461 } else if (rc == 0) {
462 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
463 close(fd->fd);
464 osmo_fd_unregister(fd);
465 fd->fd = -1;
466
467 return -1;
468 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100469 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100470 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100471
472 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100473 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100474 msgb_free(msg);
475 return 0;
476 }
477
478 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
479
480 switch (sinfo.sinfo_ppid) {
481 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100482 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100483 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100484 break;
485 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100486 printf("RUA message received\n");
487 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100488 break;
489 case IUH_PPI_SABP:
490 case IUH_PPI_RNA:
491 case IUH_PPI_PUA:
492 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
493 sinfo.sinfo_ppid);
494 rc = 0;
495 break;
496 default:
497 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
498 sinfo.sinfo_ppid);
499 rc = 0;
500 break;
501 }
502
503 msgb_free(msg);
504 return rc;
505}
506
507static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
508{
509 struct hnb_test *ctx = fd->data;
510 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100511 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100512 .sinfo_stream = 0,
513 };
514 int rc;
515
516 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
517 &sinfo, 0);
518 /* we don't need to msgb_free(), write_queue does this for us */
519 return rc;
520}
521
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100522static void hnb_send_register_req(struct hnb_test *hnb_test)
523{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100524 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100525 struct msgb *msg;
526 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100527 uint16_t lac, sac;
528 uint8_t rac;
529 uint32_t cid;
530 uint8_t plmn[] = {0x09, 0xf1, 0x99};
531 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100532
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100533 HNBRegisterRequestIEs_t request;
534 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100535
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100536 lac = 0xc0fe;
537 sac = 0xabab;
538 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100539 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100540
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100541 asn1_u16_to_str(&request.lac, &lac, lac);
542 asn1_u16_to_str(&request.sac, &sac, sac);
543 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100544 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100545
546 request.hnB_Identity.hNB_Identity_Info.buf = identity;
547 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
548
549 request.plmNidentity.buf = plmn;
550 request.plmNidentity.size = 3;
551
552
553
554 memset(&request_out, 0, sizeof(request_out));
555 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
556 if (rc < 0) {
557 printf("Could not encode HNB register request IEs\n");
558 }
559
560 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
561 Criticality_reject,
562 &asn_DEF_HNBRegisterRequest,
563 &request_out);
564
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100565
Harald Weltec3851222015-12-24 15:41:21 +0100566 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
567
568 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
569}
570
571static void hnb_send_deregister_req(struct hnb_test *hnb_test)
572{
573 struct msgb *msg;
574 int rc;
575
576 HNBDe_RegisterIEs_t request;
577 memset(&request, 0, sizeof(request));
578
579 request.cause.present = Cause_PR_misc;
580 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
581
582 HNBDe_Register_t request_out;
583 memset(&request_out, 0, sizeof(request_out));
584 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
585 if (rc < 0) {
586 printf("Could not encode HNB deregister request IEs\n");
587 }
588
589 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
590 Criticality_reject,
591 &asn_DEF_HNBDe_Register,
592 &request_out);
593
594 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100595
596 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
597}
598
599
Daniel Willmann97374c02015-12-03 09:37:58 +0100600static const struct log_info_cat log_cat[] = {
601 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100602 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100603 .color = "",
604 .description = "Main program",
605 },
Daniel Willmann32797802015-12-17 12:53:05 +0100606 [DHNBAP] = {
607 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
608 .color = "",
609 .description = "Home Node B Application Part",
610 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100611};
612
613static const struct log_info hnb_test_log_info = {
614 .cat = log_cat,
615 .num_cat = ARRAY_SIZE(log_cat),
616};
617
618static struct vty_app_info vty_info = {
619 .name = "OsmoHNB-Test",
620 .version = "0",
621};
622
Daniel Willmann4abdee02015-12-09 17:57:32 +0100623static int sctp_sock_init(int fd)
624{
625 struct sctp_event_subscribe event;
626 int rc;
627
628 /* subscribe for all events */
629 memset((uint8_t *)&event, 1, sizeof(event));
630 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
631 &event, sizeof(event));
632
633 return rc;
634}
635
Harald Weltec3851222015-12-24 15:41:21 +0100636#define HNBAP_STR "HNBAP related commands\n"
637#define HNB_STR "HomeNodeB commands\n"
638#define UE_STR "User Equipment commands\n"
639#define RANAP_STR "RANAP related commands\n"
640#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
641
642DEFUN(hnb_register, hnb_register_cmd,
643 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
644{
645 hnb_send_register_req(&g_hnb_test);
646
647 return CMD_SUCCESS;
648}
649
650DEFUN(hnb_deregister, hnb_deregister_cmd,
651 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
652{
653 hnb_send_deregister_req(&g_hnb_test);
654
655 return CMD_SUCCESS;
656}
657
658DEFUN(ue_register, ue_register_cmd,
659 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
660{
661 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
662
663 return CMD_SUCCESS;
664}
665
666DEFUN(asn_dbg, asn_dbg_cmd,
667 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
668{
669 asn_debug = atoi(argv[0]);
670
671 return CMD_SUCCESS;
672}
673
674DEFUN(ranap_reset, ranap_reset_cmd,
675 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
676{
677 int is_ps = 0;
678 struct msgb *msg, *rua;
679
680 RANAP_Cause_t cause = {
681 .present = RANAP_Cause_PR_transmissionNetwork,
682 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
683 };
684
685 if (!strcmp(argv[0], "ps"))
686 is_ps = 1;
687
688 msg = ranap_new_msg_reset(is_ps, &cause);
689 rua = rua_new_udt(msg);
690 //msgb_free(msg);
691 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
692
693 return CMD_SUCCESS;
694}
695
696
697enum my_vty_nodes {
698 CHAN_NODE = _LAST_OSMOVTY_NODE,
699};
700
701static struct cmd_node chan_node = {
702 CHAN_NODE,
703 "%s(chan)> ",
704 1,
705};
706
707
Harald Weltec3851222015-12-24 15:41:21 +0100708static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
709{
Neels Hofmeyr5c1cc8c2016-02-29 09:28:48 +0100710 uint8_t lu[] = { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST,
711 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
Neels Hofmeyr32828702016-01-14 13:06:47 +0100712 /* len, IMSI/type, IMSI-------------------------------- */
Harald Weltec3851222015-12-24 15:41:21 +0100713 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
714 0x33, 0x03, 0x57, 0x18 , 0xb2 };
715 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
716 RANAP_GlobalRNC_ID_t rnc_id = {
717 .rNC_ID = 23,
718 .pLMNidentity.buf = plmn_id,
719 .pLMNidentity.size = sizeof(plmn_id),
720 };
Harald Weltec3851222015-12-24 15:41:21 +0100721
722 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100723 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
724 * an even/odd indicator bit encoded in the first octet. So the first
725 * octet looks like this:
726 *
727 * 8 7 6 5 | 4 | 3 2 1
728 * IMSI-digit | even/odd | type
729 *
730 * followed by the remaining IMSI digits.
731 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
732 * (derived from Iu pcap Location Update Request msg and TS 25.413)
733 *
734 * TODO I'm only 90% sure about this
735 */
Harald Weltec3851222015-12-24 15:41:21 +0100736
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100737 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100738}
739
740DEFUN(chan, chan_cmd,
741 "channel (cs|ps) lu imsi IMSI",
742 "Open a new Signalling Connection\n"
743 "To Circuit-Switched CN\n"
744 "To Packet-Switched CN\n"
745 "Performing a Location Update\n"
746 )
747{
748 struct hnbtest_chan *chan;
749 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100750 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100751
752 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
753 if (!strcmp(argv[0], "ps"))
754 chan->is_ps = 1;
755 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100756 chan->conn_id = conn_id;
757 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100758
759 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
760 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
761
762 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
763
764 vty->index = chan;
765 vty->node = CHAN_NODE;
766
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100767 if (!chan->is_ps)
768 g_hnb_test.cs.chan = chan;
769
770
Harald Weltec3851222015-12-24 15:41:21 +0100771 return CMD_SUCCESS;
772}
773
774static void hnbtest_vty_init(void)
775{
776 install_element_ve(&asn_dbg_cmd);
777 install_element_ve(&hnb_register_cmd);
778 install_element_ve(&hnb_deregister_cmd);
779 install_element_ve(&ue_register_cmd);
780 install_element_ve(&ranap_reset_cmd);
781 install_element_ve(&chan_cmd);
782
783 install_node(&chan_node, NULL);
784 vty_install_default(CHAN_NODE);
785}
786
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100787static void handle_options(int argc, char **argv)
788{
789 while (1) {
790 int idx = 0, c;
791 static const struct option long_options[] = {
792 { "ues", 1, 0, 'u' },
793 { 0, 0, 0, 0 },
794 };
795
796 c = getopt_long(argc, argv, "u:", long_options, &idx);
797
798 if (c == -1)
799 break;
800
801 switch (c) {
802 case 'u':
803 g_hnb_test.ues = atoi(optarg);
804 break;
805 }
806 }
807}
808
Harald Weltec3851222015-12-24 15:41:21 +0100809int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100810{
811 int rc;
812
Harald Welte87ffeb92015-12-25 15:34:22 +0100813 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100814
Harald Welte87ffeb92015-12-25 15:34:22 +0100815 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100816
817 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100818 hnbtest_vty_init();
819
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100820 printf("VTY at %s %d\n", vty_get_bind_addr(), 2324);
821 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100822 if (rc < 0) {
823 perror("Error binding VTY port");
824 exit(1);
825 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100826
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100827 handle_options(argc, argv);
828
Daniel Willmann97374c02015-12-03 09:37:58 +0100829 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
830 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
831 g_hnb_test.wqueue.read_cb = hnb_read_cb;
832 g_hnb_test.wqueue.write_cb = hnb_write_cb;
833
834 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
835 IPPROTO_SCTP, "127.0.0.1",
836 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
837 if (rc < 0) {
838 perror("Error connecting to Iuh port");
839 exit(1);
840 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100841 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100842
Harald Weltec3851222015-12-24 15:41:21 +0100843#if 0
844 /* some hard-coded message generation. Doesn't make sense from
845 * a protocol point of view but enables to look at the encoded
846 * results in wireshark for manual verification */
847 {
848 struct msgb *msg, *rua;
849 const uint8_t nas[] = { 0, 1, 2, 3 };
850 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
851
852 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
853 rua = rua_new_udt(msg);
854 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
855
856 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
857 rua = rua_new_udt(msg);
858 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
859
860 msg = ranap_new_msg_iu_rel_cmd()
861 rua = rua_new_udt(msg);
862 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
863
864 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
865 rua = rua_new_udt(msg);
866 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
867
868 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
869 rua = rua_new_udt(msg);
870 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
871
872 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
873 rua = rua_new_udt(msg);
874 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
875 }
876#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100877
Daniel Willmann97374c02015-12-03 09:37:58 +0100878 while (1) {
879 rc = osmo_select_main(0);
880 if (rc < 0)
881 exit(3);
882 }
883
884 /* not reached */
885 exit(0);
886}