blob: 2992b96f9b403bee9372b00c02ff03fe5d781d3a [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>
Daniel Willmann97374c02015-12-03 09:37:58 +010044
45#include <osmocom/vty/telnet_interface.h>
46#include <osmocom/vty/logging.h>
Harald Weltec3851222015-12-24 15:41:21 +010047#include <osmocom/vty/command.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010048
49#include "hnb-test.h"
Daniel Willmanna1e202e2015-12-07 17:21:07 +010050#include "hnbap_common.h"
51#include "hnbap_ies_defs.h"
Harald Welteb66c5d02016-01-03 18:04:28 +010052#include "rua_msg_factory.h"
Harald Weltec3851222015-12-24 15:41:21 +010053#include "asn1helpers.h"
Neels Hofmeyr96979af2016-01-05 15:19:44 +010054#include <osmocom/ranap/iu_helpers.h>
Harald Welte87ffeb92015-12-25 15:34:22 +010055#include "test_common.h"
Harald Weltec3851222015-12-24 15:41:21 +010056
Neels Hofmeyr96979af2016-01-05 15:19:44 +010057#include <osmocom/ranap/ranap_msg_factory.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010058
Neels Hofmeyr0968a582016-01-11 15:19:38 +010059#include <osmocom/rua/RUA_RUA-PDU.h>
60
Neels Hofmeyr860a1292016-02-18 23:03:15 +010061#include <osmocom/gsm/protocol/gsm_04_08.h>
62
63#include <osmocom/ranap/RANAP_ProcedureCode.h>
64#include <osmocom/ranap/RANAP_Criticality.h>
65#include <osmocom/ranap/RANAP_DirectTransfer.h>
66
Daniel Willmann97374c02015-12-03 09:37:58 +010067static void *tall_hnb_ctx;
Daniel Willmann97374c02015-12-03 09:37:58 +010068
69struct hnb_test g_hnb_test = {
70 .gw_port = IUH_DEFAULT_SCTP_PORT,
71};
72
Harald Weltec3851222015-12-24 15:41:21 +010073struct msgb *rua_new_udt(struct msgb *inmsg);
74
Harald Weltec3851222015-12-24 15:41:21 +010075static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test)
Daniel Willmann19dedbb2015-12-17 11:57:41 +010076{
77 struct msgb *msg;
78 int rc, imsi_len;
79 uint32_t ctx_id;
80
81 UEDe_Register_t dereg;
82 UEDe_RegisterIEs_t dereg_ies;
83 memset(&dereg_ies, 0, sizeof(dereg_ies));
84
85 asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id);
86 dereg_ies.cause.present = Cause_PR_radioNetwork;
87 dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost;
88
89 memset(&dereg, 0, sizeof(dereg));
90 rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies);
91
92 msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register,
93 Criticality_ignore,
94 &asn_DEF_UEDe_Register,
95 &dereg);
96
Harald Weltec3851222015-12-24 15:41:21 +010097 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg);
Daniel Willmann19dedbb2015-12-17 11:57:41 +010098
Harald Weltec3851222015-12-24 15:41:21 +010099 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100100
101 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
102}
103
Harald Weltec3851222015-12-24 15:41:21 +0100104static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str)
Daniel Willmann479cb302015-12-09 17:54:59 +0100105{
Daniel Willmann4e312502015-12-09 17:59:24 +0100106 struct msgb *msg;
107 int rc, imsi_len;
108
109 char imsi_buf[16];
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100110
Daniel Willmann4e312502015-12-09 17:59:24 +0100111 UERegisterRequest_t request_out;
112 UERegisterRequestIEs_t request;
113 memset(&request, 0, sizeof(request));
114
115 request.uE_Identity.present = UE_Identity_PR_iMSI;
116
Harald Welte056984f2016-01-03 16:31:31 +0100117 imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str);
Harald Weltec3851222015-12-24 15:41:21 +0100118 OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, imsi_buf, imsi_len);
Daniel Willmann4e312502015-12-09 17:59:24 +0100119
120 request.registration_Cause = Registration_Cause_normal;
121 request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6;
122 request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable;
123
124 memset(&request_out, 0, sizeof(request_out));
125 rc = hnbap_encode_ueregisterrequesties(&request_out, &request);
126
127 msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister,
128 Criticality_reject,
129 &asn_DEF_UERegisterRequest,
130 &request_out);
131
Harald Weltec3851222015-12-24 15:41:21 +0100132 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out);
Daniel Willmann4e312502015-12-09 17:59:24 +0100133
Harald Weltec3851222015-12-24 15:41:21 +0100134 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4e312502015-12-09 17:59:24 +0100135
136 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
Daniel Willmann479cb302015-12-09 17:54:59 +0100137}
138
Harald Weltec3851222015-12-24 15:41:21 +0100139static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmann479cb302015-12-09 17:54:59 +0100140{
141 int rc;
142 HNBRegisterAcceptIEs_t accept;
143
144 rc = hnbap_decode_hnbregisteraccepties(&accept, in);
145 if (rc < 0) {
146 }
147
148 hnb->rnc_id = accept.rnc_id;
149 printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id);
150
Daniel Willmann11e912a2016-01-07 13:19:30 +0100151 hnbap_free_hnbregisteraccepties(&accept);
Harald Weltec3851222015-12-24 15:41:21 +0100152 return 0;
Daniel Willmann479cb302015-12-09 17:54:59 +0100153}
154
Harald Weltec3851222015-12-24 15:41:21 +0100155static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmanna7b02402015-12-09 19:05:09 +0100156{
157 int rc;
158 uint32_t ctx_id;
159 UERegisterAcceptIEs_t accept;
160 char imsi[16];
161
162 rc = hnbap_decode_ueregisteraccepties(&accept, in);
163 if (rc < 0) {
164 return rc;
165 }
166
167 if (accept.uE_Identity.present != UE_Identity_PR_iMSI) {
168 printf("Wrong type in UE register accept\n");
169 return -1;
170 }
171
172 ctx_id = asn1bitstr_to_u24(&accept.context_ID);
173
Harald Welte056984f2016-01-03 16:31:31 +0100174 ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf,
Daniel Willmanna7b02402015-12-09 19:05:09 +0100175 accept.uE_Identity.choice.iMSI.size);
176 printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id);
177
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100178 hnb->ctx_id = ctx_id;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100179 hnbap_free_ueregisteraccepties(&accept);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100180
Daniel Willmanna7b02402015-12-09 19:05:09 +0100181 return 0;
182}
183
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100184static struct msgb *gen_nas_id_resp()
185{
186 uint8_t id_resp[] = {
187 0x05, /* MM discr */
188 0x19, /* DTAP */
189 /* IMEISV */
190 0x09, /* len */
191 0x03, /* first digit (0000) + even (0) + id IMEISV (011) */
192 0x31, 0x91, 0x06, 0x00, 0x28, 0x47, 0x11, /* digits */
193 0xf2, /* filler (1111) + last digit (0010) */
194 };
195
Neels Hofmeyre1f709f2016-02-28 00:50:45 +0100196 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100197}
198
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100199static int hnb_test_nas_tx_id_resp(struct hnb_test *hnb)
200{
201 struct hnbtest_chan *chan;
202 struct msgb *txm, *rua;
203
204 chan = hnb->cs.chan;
205 if (!chan) {
206 printf("hnb_test_nas_tx_id_resp(): No CS channel established yet.\n");
207 return -1;
208 }
209
210 txm = gen_nas_id_resp();
211 rua = rua_new_dt(chan->is_ps, chan->conn_id, txm);
212
213 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
214
215 return 0;
216}
217
218static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct msgb *rxm)
219{
220 struct hnbtest_chan *chan;
221
222 chan = hnb->cs.chan;
223 if (!chan) {
224 printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n");
225 return -1;
226 }
227
228 OSMO_ASSERT(!chan->is_ps);
229
230 struct gsm48_hdr *gh = msgb_l3(rxm);
231 uint8_t msg_type = gh->msg_type & 0xbf;
232
233 switch (msg_type) {
234 case GSM48_MT_MM_ID_REQ:
235
236 return hnb_test_nas_tx_id_resp(hnb);
237 default:
238 printf("04.08 message type not handled by hnb-test: %d\n",
239 msg_type);
240 return 0;
241 }
242
243}
244
245static int hnb_test_nas_rx_dtap(struct hnb_test *hnb, struct msgb *msg)
246{
247 printf("got %s\n", osmo_hexdump(msg->data, msg->len));
248
249 // nas_pdu == '05 08 12' ==> IMEI Identity request
250 // '05 04 0d' ==> LU reject
251
252 struct gsm48_hdr *gh = msgb_l3(msg);
253 uint8_t pdisc = gh->proto_discr & 0x0f;
254
255 switch (pdisc) {
256 case GSM48_PDISC_MM:
257 return hnb_test_nas_rx_mm(hnb, msg);
258 default:
259 printf("04.08 discriminator not handled by hnb-test: %d\n",
260 pdisc);
261 return 0;
262 }
263
264
265}
266
Daniel Willmann479cb302015-12-09 17:54:59 +0100267int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
268{
269 HNBAP_PDU_t _pdu, *pdu = &_pdu;
270 asn_dec_rval_t dec_ret;
271 int rc;
272
273 memset(pdu, 0, sizeof(*pdu));
274 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
275 msg->data, msgb_length(msg), 0, 0);
276 if (dec_ret.code != RC_OK) {
277 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
278 return rc;
279 }
280
281 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
282 printf("Unexpected HNBAP message received\n");
283 }
284
285 switch (pdu->choice.successfulOutcome.procedureCode) {
286 case ProcedureCode_id_HNBRegister:
287 /* Get HNB id and send UE Register request */
288 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
289 break;
290 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100291 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100292 break;
293 default:
294 break;
295 }
296
297 return rc;
298}
299
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100300extern void direct_transfer_nas_pdu_print(ANY_t *in);
301
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100302int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
303{
304 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
305 asn_dec_rval_t dec_ret;
306 int rc;
307
308 memset(pdu, 0, sizeof(*pdu));
309 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
310 msg->data, msgb_length(msg), 0, 0);
311 if (dec_ret.code != RC_OK) {
312 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
313 return rc;
314 }
315
316 switch (pdu->present) {
317 case RUA_RUA_PDU_PR_successfulOutcome:
318 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
319 break;
320 case RUA_RUA_PDU_PR_initiatingMessage:
321 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
322 break;
323 case RUA_RUA_PDU_PR_NOTHING:
324 printf("RUA_RUA_PDU_PR_NOTHING\n");
325 break;
326 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
327 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
328 break;
329 default:
330 printf("Unexpected RUA message received\n");
331 break;
332 }
333
334 switch (pdu->choice.successfulOutcome.procedureCode) {
335 case RUA_ProcedureCode_id_ConnectionlessTransfer:
336 printf("RUA rx Connectionless Transfer\n");
337 break;
338 case RUA_ProcedureCode_id_Connect:
339 printf("RUA rx Connect\n");
340 break;
341 case RUA_ProcedureCode_id_DirectTransfer:
342 printf("RUA rx DirectTransfer\n");
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100343 {
344 struct msgb *m = msgb_alloc(1500, "direct_transfer_nas_pdu");
345 direct_transfer_nas_pdu_get(&pdu->choice.successfulOutcome.value, m);
346
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100347 hnb_test_nas_rx_dtap(hnb, m);
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100348
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100349 msgb_free(m);
350 }
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100351 break;
352 case RUA_ProcedureCode_id_Disconnect:
353 printf("RUA rx Disconnect\n");
354 break;
355 case RUA_ProcedureCode_id_ErrorIndication:
356 printf("RUA rx ErrorIndication\n");
357 break;
358 case RUA_ProcedureCode_id_privateMessage:
359 printf("RUA rx privateMessage\n");
360 break;
361 default:
362 printf("RUA rx unknown message\n");
363 break;
364 }
365
366 return rc;
367}
368
Daniel Willmann97374c02015-12-03 09:37:58 +0100369static int hnb_read_cb(struct osmo_fd *fd)
370{
371 struct hnb_test *hnb_test = fd->data;
372 struct sctp_sndrcvinfo sinfo;
373 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
374 int flags = 0;
375 int rc;
376
377 if (!msg)
378 return -ENOMEM;
379
380 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
381 NULL, NULL, &sinfo, &flags);
382 if (rc < 0) {
383 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
384 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100385 close(fd->fd);
386 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100387 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100388 } else if (rc == 0) {
389 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
390 close(fd->fd);
391 osmo_fd_unregister(fd);
392 fd->fd = -1;
393
394 return -1;
395 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100396 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100397 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100398
399 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100400 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100401 msgb_free(msg);
402 return 0;
403 }
404
405 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
406
407 switch (sinfo.sinfo_ppid) {
408 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100409 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100410 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100411 break;
412 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100413 printf("RUA message received\n");
414 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100415 break;
416 case IUH_PPI_SABP:
417 case IUH_PPI_RNA:
418 case IUH_PPI_PUA:
419 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
420 sinfo.sinfo_ppid);
421 rc = 0;
422 break;
423 default:
424 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
425 sinfo.sinfo_ppid);
426 rc = 0;
427 break;
428 }
429
430 msgb_free(msg);
431 return rc;
432}
433
434static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
435{
436 struct hnb_test *ctx = fd->data;
437 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100438 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100439 .sinfo_stream = 0,
440 };
441 int rc;
442
443 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
444 &sinfo, 0);
445 /* we don't need to msgb_free(), write_queue does this for us */
446 return rc;
447}
448
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100449static void hnb_send_register_req(struct hnb_test *hnb_test)
450{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100451 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100452 struct msgb *msg;
453 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100454 uint16_t lac, sac;
455 uint8_t rac;
456 uint32_t cid;
457 uint8_t plmn[] = {0x09, 0xf1, 0x99};
458 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100459
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100460 HNBRegisterRequestIEs_t request;
461 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100462
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100463 lac = 0xc0fe;
464 sac = 0xabab;
465 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100466 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100467
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100468 asn1_u16_to_str(&request.lac, &lac, lac);
469 asn1_u16_to_str(&request.sac, &sac, sac);
470 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100471 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100472
473 request.hnB_Identity.hNB_Identity_Info.buf = identity;
474 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
475
476 request.plmNidentity.buf = plmn;
477 request.plmNidentity.size = 3;
478
479
480
481 memset(&request_out, 0, sizeof(request_out));
482 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
483 if (rc < 0) {
484 printf("Could not encode HNB register request IEs\n");
485 }
486
487 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
488 Criticality_reject,
489 &asn_DEF_HNBRegisterRequest,
490 &request_out);
491
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100492
Harald Weltec3851222015-12-24 15:41:21 +0100493 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
494
495 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
496}
497
498static void hnb_send_deregister_req(struct hnb_test *hnb_test)
499{
500 struct msgb *msg;
501 int rc;
502
503 HNBDe_RegisterIEs_t request;
504 memset(&request, 0, sizeof(request));
505
506 request.cause.present = Cause_PR_misc;
507 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
508
509 HNBDe_Register_t request_out;
510 memset(&request_out, 0, sizeof(request_out));
511 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
512 if (rc < 0) {
513 printf("Could not encode HNB deregister request IEs\n");
514 }
515
516 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
517 Criticality_reject,
518 &asn_DEF_HNBDe_Register,
519 &request_out);
520
521 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100522
523 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
524}
525
526
Daniel Willmann97374c02015-12-03 09:37:58 +0100527static const struct log_info_cat log_cat[] = {
528 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100529 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100530 .color = "",
531 .description = "Main program",
532 },
Daniel Willmann32797802015-12-17 12:53:05 +0100533 [DHNBAP] = {
534 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
535 .color = "",
536 .description = "Home Node B Application Part",
537 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100538};
539
540static const struct log_info hnb_test_log_info = {
541 .cat = log_cat,
542 .num_cat = ARRAY_SIZE(log_cat),
543};
544
545static struct vty_app_info vty_info = {
546 .name = "OsmoHNB-Test",
547 .version = "0",
548};
549
Daniel Willmann4abdee02015-12-09 17:57:32 +0100550static int sctp_sock_init(int fd)
551{
552 struct sctp_event_subscribe event;
553 int rc;
554
555 /* subscribe for all events */
556 memset((uint8_t *)&event, 1, sizeof(event));
557 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
558 &event, sizeof(event));
559
560 return rc;
561}
562
Harald Weltec3851222015-12-24 15:41:21 +0100563#define HNBAP_STR "HNBAP related commands\n"
564#define HNB_STR "HomeNodeB commands\n"
565#define UE_STR "User Equipment commands\n"
566#define RANAP_STR "RANAP related commands\n"
567#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
568
569DEFUN(hnb_register, hnb_register_cmd,
570 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
571{
572 hnb_send_register_req(&g_hnb_test);
573
574 return CMD_SUCCESS;
575}
576
577DEFUN(hnb_deregister, hnb_deregister_cmd,
578 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
579{
580 hnb_send_deregister_req(&g_hnb_test);
581
582 return CMD_SUCCESS;
583}
584
585DEFUN(ue_register, ue_register_cmd,
586 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
587{
588 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
589
590 return CMD_SUCCESS;
591}
592
593DEFUN(asn_dbg, asn_dbg_cmd,
594 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
595{
596 asn_debug = atoi(argv[0]);
597
598 return CMD_SUCCESS;
599}
600
601DEFUN(ranap_reset, ranap_reset_cmd,
602 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
603{
604 int is_ps = 0;
605 struct msgb *msg, *rua;
606
607 RANAP_Cause_t cause = {
608 .present = RANAP_Cause_PR_transmissionNetwork,
609 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
610 };
611
612 if (!strcmp(argv[0], "ps"))
613 is_ps = 1;
614
615 msg = ranap_new_msg_reset(is_ps, &cause);
616 rua = rua_new_udt(msg);
617 //msgb_free(msg);
618 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
619
620 return CMD_SUCCESS;
621}
622
623
624enum my_vty_nodes {
625 CHAN_NODE = _LAST_OSMOVTY_NODE,
626};
627
628static struct cmd_node chan_node = {
629 CHAN_NODE,
630 "%s(chan)> ",
631 1,
632};
633
634
Harald Weltec3851222015-12-24 15:41:21 +0100635static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
636{
637 uint8_t lu[] = { 0x05, 0x08, 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
Neels Hofmeyr32828702016-01-14 13:06:47 +0100638 /* len, IMSI/type, IMSI-------------------------------- */
Harald Weltec3851222015-12-24 15:41:21 +0100639 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
640 0x33, 0x03, 0x57, 0x18 , 0xb2 };
641 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
642 RANAP_GlobalRNC_ID_t rnc_id = {
643 .rNC_ID = 23,
644 .pLMNidentity.buf = plmn_id,
645 .pLMNidentity.size = sizeof(plmn_id),
646 };
Harald Weltec3851222015-12-24 15:41:21 +0100647
648 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100649 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
650 * an even/odd indicator bit encoded in the first octet. So the first
651 * octet looks like this:
652 *
653 * 8 7 6 5 | 4 | 3 2 1
654 * IMSI-digit | even/odd | type
655 *
656 * followed by the remaining IMSI digits.
657 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
658 * (derived from Iu pcap Location Update Request msg and TS 25.413)
659 *
660 * TODO I'm only 90% sure about this
661 */
Harald Weltec3851222015-12-24 15:41:21 +0100662
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100663 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100664}
665
666DEFUN(chan, chan_cmd,
667 "channel (cs|ps) lu imsi IMSI",
668 "Open a new Signalling Connection\n"
669 "To Circuit-Switched CN\n"
670 "To Packet-Switched CN\n"
671 "Performing a Location Update\n"
672 )
673{
674 struct hnbtest_chan *chan;
675 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100676 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100677
678 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
679 if (!strcmp(argv[0], "ps"))
680 chan->is_ps = 1;
681 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100682 chan->conn_id = conn_id;
683 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100684
685 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
686 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
687
688 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
689
690 vty->index = chan;
691 vty->node = CHAN_NODE;
692
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100693 if (!chan->is_ps)
694 g_hnb_test.cs.chan = chan;
695
696
Harald Weltec3851222015-12-24 15:41:21 +0100697 return CMD_SUCCESS;
698}
699
700static void hnbtest_vty_init(void)
701{
702 install_element_ve(&asn_dbg_cmd);
703 install_element_ve(&hnb_register_cmd);
704 install_element_ve(&hnb_deregister_cmd);
705 install_element_ve(&ue_register_cmd);
706 install_element_ve(&ranap_reset_cmd);
707 install_element_ve(&chan_cmd);
708
709 install_node(&chan_node, NULL);
710 vty_install_default(CHAN_NODE);
711}
712
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100713static void handle_options(int argc, char **argv)
714{
715 while (1) {
716 int idx = 0, c;
717 static const struct option long_options[] = {
718 { "ues", 1, 0, 'u' },
719 { 0, 0, 0, 0 },
720 };
721
722 c = getopt_long(argc, argv, "u:", long_options, &idx);
723
724 if (c == -1)
725 break;
726
727 switch (c) {
728 case 'u':
729 g_hnb_test.ues = atoi(optarg);
730 break;
731 }
732 }
733}
734
Harald Weltec3851222015-12-24 15:41:21 +0100735int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100736{
737 int rc;
738
Harald Welte87ffeb92015-12-25 15:34:22 +0100739 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100740
Harald Welte87ffeb92015-12-25 15:34:22 +0100741 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100742
743 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100744 hnbtest_vty_init();
745
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100746 printf("VTY at %s %d\n", vty_get_bind_addr(), 2324);
747 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100748 if (rc < 0) {
749 perror("Error binding VTY port");
750 exit(1);
751 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100752
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100753 handle_options(argc, argv);
754
Daniel Willmann97374c02015-12-03 09:37:58 +0100755 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
756 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
757 g_hnb_test.wqueue.read_cb = hnb_read_cb;
758 g_hnb_test.wqueue.write_cb = hnb_write_cb;
759
760 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
761 IPPROTO_SCTP, "127.0.0.1",
762 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
763 if (rc < 0) {
764 perror("Error connecting to Iuh port");
765 exit(1);
766 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100767 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100768
Harald Weltec3851222015-12-24 15:41:21 +0100769#if 0
770 /* some hard-coded message generation. Doesn't make sense from
771 * a protocol point of view but enables to look at the encoded
772 * results in wireshark for manual verification */
773 {
774 struct msgb *msg, *rua;
775 const uint8_t nas[] = { 0, 1, 2, 3 };
776 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
777
778 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
779 rua = rua_new_udt(msg);
780 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
781
782 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
783 rua = rua_new_udt(msg);
784 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
785
786 msg = ranap_new_msg_iu_rel_cmd()
787 rua = rua_new_udt(msg);
788 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
789
790 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
791 rua = rua_new_udt(msg);
792 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
793
794 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
795 rua = rua_new_udt(msg);
796 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
797
798 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
799 rua = rua_new_udt(msg);
800 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
801 }
802#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100803
Daniel Willmann97374c02015-12-03 09:37:58 +0100804 while (1) {
805 rc = osmo_select_main(0);
806 if (rc < 0)
807 exit(3);
808 }
809
810 /* not reached */
811 exit(0);
812}