blob: 048f3a93ff9601fe2611a5c260b44a945d502612 [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
Daniel Willmann97374c02015-12-03 09:37:58 +010061static void *tall_hnb_ctx;
Daniel Willmann97374c02015-12-03 09:37:58 +010062
63struct hnb_test g_hnb_test = {
64 .gw_port = IUH_DEFAULT_SCTP_PORT,
65};
66
Harald Weltec3851222015-12-24 15:41:21 +010067struct msgb *rua_new_udt(struct msgb *inmsg);
68
Harald Weltec3851222015-12-24 15:41:21 +010069static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test)
Daniel Willmann19dedbb2015-12-17 11:57:41 +010070{
71 struct msgb *msg;
72 int rc, imsi_len;
73 uint32_t ctx_id;
74
75 UEDe_Register_t dereg;
76 UEDe_RegisterIEs_t dereg_ies;
77 memset(&dereg_ies, 0, sizeof(dereg_ies));
78
79 asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id);
80 dereg_ies.cause.present = Cause_PR_radioNetwork;
81 dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost;
82
83 memset(&dereg, 0, sizeof(dereg));
84 rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies);
85
86 msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register,
87 Criticality_ignore,
88 &asn_DEF_UEDe_Register,
89 &dereg);
90
Harald Weltec3851222015-12-24 15:41:21 +010091 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg);
Daniel Willmann19dedbb2015-12-17 11:57:41 +010092
Harald Weltec3851222015-12-24 15:41:21 +010093 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann19dedbb2015-12-17 11:57:41 +010094
95 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
96}
97
Harald Weltec3851222015-12-24 15:41:21 +010098static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str)
Daniel Willmann479cb302015-12-09 17:54:59 +010099{
Daniel Willmann4e312502015-12-09 17:59:24 +0100100 struct msgb *msg;
101 int rc, imsi_len;
102
103 char imsi_buf[16];
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100104
Daniel Willmann4e312502015-12-09 17:59:24 +0100105 UERegisterRequest_t request_out;
106 UERegisterRequestIEs_t request;
107 memset(&request, 0, sizeof(request));
108
109 request.uE_Identity.present = UE_Identity_PR_iMSI;
110
Harald Welte056984f2016-01-03 16:31:31 +0100111 imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str);
Harald Weltec3851222015-12-24 15:41:21 +0100112 OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, imsi_buf, imsi_len);
Daniel Willmann4e312502015-12-09 17:59:24 +0100113
114 request.registration_Cause = Registration_Cause_normal;
115 request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6;
116 request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable;
117
118 memset(&request_out, 0, sizeof(request_out));
119 rc = hnbap_encode_ueregisterrequesties(&request_out, &request);
120
121 msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister,
122 Criticality_reject,
123 &asn_DEF_UERegisterRequest,
124 &request_out);
125
Harald Weltec3851222015-12-24 15:41:21 +0100126 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out);
Daniel Willmann4e312502015-12-09 17:59:24 +0100127
Harald Weltec3851222015-12-24 15:41:21 +0100128 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4e312502015-12-09 17:59:24 +0100129
130 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
Daniel Willmann479cb302015-12-09 17:54:59 +0100131}
132
Harald Weltec3851222015-12-24 15:41:21 +0100133static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmann479cb302015-12-09 17:54:59 +0100134{
135 int rc;
136 HNBRegisterAcceptIEs_t accept;
137
138 rc = hnbap_decode_hnbregisteraccepties(&accept, in);
139 if (rc < 0) {
140 }
141
142 hnb->rnc_id = accept.rnc_id;
143 printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id);
144
Daniel Willmann11e912a2016-01-07 13:19:30 +0100145 hnbap_free_hnbregisteraccepties(&accept);
Harald Weltec3851222015-12-24 15:41:21 +0100146 return 0;
Daniel Willmann479cb302015-12-09 17:54:59 +0100147}
148
Harald Weltec3851222015-12-24 15:41:21 +0100149static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmanna7b02402015-12-09 19:05:09 +0100150{
151 int rc;
152 uint32_t ctx_id;
153 UERegisterAcceptIEs_t accept;
154 char imsi[16];
155
156 rc = hnbap_decode_ueregisteraccepties(&accept, in);
157 if (rc < 0) {
158 return rc;
159 }
160
161 if (accept.uE_Identity.present != UE_Identity_PR_iMSI) {
162 printf("Wrong type in UE register accept\n");
163 return -1;
164 }
165
166 ctx_id = asn1bitstr_to_u24(&accept.context_ID);
167
Harald Welte056984f2016-01-03 16:31:31 +0100168 ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf,
Daniel Willmanna7b02402015-12-09 19:05:09 +0100169 accept.uE_Identity.choice.iMSI.size);
170 printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id);
171
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100172 hnb->ctx_id = ctx_id;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100173 hnbap_free_ueregisteraccepties(&accept);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100174
Daniel Willmanna7b02402015-12-09 19:05:09 +0100175 return 0;
176}
177
Daniel Willmann479cb302015-12-09 17:54:59 +0100178int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
179{
180 HNBAP_PDU_t _pdu, *pdu = &_pdu;
181 asn_dec_rval_t dec_ret;
182 int rc;
183
184 memset(pdu, 0, sizeof(*pdu));
185 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
186 msg->data, msgb_length(msg), 0, 0);
187 if (dec_ret.code != RC_OK) {
188 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
189 return rc;
190 }
191
192 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
193 printf("Unexpected HNBAP message received\n");
194 }
195
196 switch (pdu->choice.successfulOutcome.procedureCode) {
197 case ProcedureCode_id_HNBRegister:
198 /* Get HNB id and send UE Register request */
199 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
200 break;
201 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100202 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100203 break;
204 default:
205 break;
206 }
207
208 return rc;
209}
210
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100211extern void direct_transfer_nas_pdu_print(ANY_t *in);
212
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100213int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
214{
215 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
216 asn_dec_rval_t dec_ret;
217 int rc;
218
219 memset(pdu, 0, sizeof(*pdu));
220 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
221 msg->data, msgb_length(msg), 0, 0);
222 if (dec_ret.code != RC_OK) {
223 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
224 return rc;
225 }
226
227 switch (pdu->present) {
228 case RUA_RUA_PDU_PR_successfulOutcome:
229 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
230 break;
231 case RUA_RUA_PDU_PR_initiatingMessage:
232 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
233 break;
234 case RUA_RUA_PDU_PR_NOTHING:
235 printf("RUA_RUA_PDU_PR_NOTHING\n");
236 break;
237 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
238 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
239 break;
240 default:
241 printf("Unexpected RUA message received\n");
242 break;
243 }
244
245 switch (pdu->choice.successfulOutcome.procedureCode) {
246 case RUA_ProcedureCode_id_ConnectionlessTransfer:
247 printf("RUA rx Connectionless Transfer\n");
248 break;
249 case RUA_ProcedureCode_id_Connect:
250 printf("RUA rx Connect\n");
251 break;
252 case RUA_ProcedureCode_id_DirectTransfer:
253 printf("RUA rx DirectTransfer\n");
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100254 {
255 struct msgb *m = msgb_alloc(1500, "direct_transfer_nas_pdu");
256 direct_transfer_nas_pdu_get(&pdu->choice.successfulOutcome.value, m);
257
258 // evaluate
259
260 printf("got %s\n", osmo_hexdump(m->data, m->len));
261 msgb_free(m);
262 }
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100263 break;
264 case RUA_ProcedureCode_id_Disconnect:
265 printf("RUA rx Disconnect\n");
266 break;
267 case RUA_ProcedureCode_id_ErrorIndication:
268 printf("RUA rx ErrorIndication\n");
269 break;
270 case RUA_ProcedureCode_id_privateMessage:
271 printf("RUA rx privateMessage\n");
272 break;
273 default:
274 printf("RUA rx unknown message\n");
275 break;
276 }
277
278 return rc;
279}
280
Daniel Willmann97374c02015-12-03 09:37:58 +0100281static int hnb_read_cb(struct osmo_fd *fd)
282{
283 struct hnb_test *hnb_test = fd->data;
284 struct sctp_sndrcvinfo sinfo;
285 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
286 int flags = 0;
287 int rc;
288
289 if (!msg)
290 return -ENOMEM;
291
292 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
293 NULL, NULL, &sinfo, &flags);
294 if (rc < 0) {
295 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
296 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100297 close(fd->fd);
298 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100299 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100300 } else if (rc == 0) {
301 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
302 close(fd->fd);
303 osmo_fd_unregister(fd);
304 fd->fd = -1;
305
306 return -1;
307 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100308 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100309 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100310
311 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100312 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100313 msgb_free(msg);
314 return 0;
315 }
316
317 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
318
319 switch (sinfo.sinfo_ppid) {
320 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100321 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100322 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100323 break;
324 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100325 printf("RUA message received\n");
326 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100327 break;
328 case IUH_PPI_SABP:
329 case IUH_PPI_RNA:
330 case IUH_PPI_PUA:
331 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
332 sinfo.sinfo_ppid);
333 rc = 0;
334 break;
335 default:
336 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
337 sinfo.sinfo_ppid);
338 rc = 0;
339 break;
340 }
341
342 msgb_free(msg);
343 return rc;
344}
345
346static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
347{
348 struct hnb_test *ctx = fd->data;
349 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100350 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100351 .sinfo_stream = 0,
352 };
353 int rc;
354
355 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
356 &sinfo, 0);
357 /* we don't need to msgb_free(), write_queue does this for us */
358 return rc;
359}
360
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100361static void hnb_send_register_req(struct hnb_test *hnb_test)
362{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100363 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100364 struct msgb *msg;
365 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100366 uint16_t lac, sac;
367 uint8_t rac;
368 uint32_t cid;
369 uint8_t plmn[] = {0x09, 0xf1, 0x99};
370 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100371
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100372 HNBRegisterRequestIEs_t request;
373 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100374
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100375 lac = 0xc0fe;
376 sac = 0xabab;
377 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100378 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100379
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100380 asn1_u16_to_str(&request.lac, &lac, lac);
381 asn1_u16_to_str(&request.sac, &sac, sac);
382 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100383 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100384
385 request.hnB_Identity.hNB_Identity_Info.buf = identity;
386 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
387
388 request.plmNidentity.buf = plmn;
389 request.plmNidentity.size = 3;
390
391
392
393 memset(&request_out, 0, sizeof(request_out));
394 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
395 if (rc < 0) {
396 printf("Could not encode HNB register request IEs\n");
397 }
398
399 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
400 Criticality_reject,
401 &asn_DEF_HNBRegisterRequest,
402 &request_out);
403
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100404
Harald Weltec3851222015-12-24 15:41:21 +0100405 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
406
407 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
408}
409
410static void hnb_send_deregister_req(struct hnb_test *hnb_test)
411{
412 struct msgb *msg;
413 int rc;
414
415 HNBDe_RegisterIEs_t request;
416 memset(&request, 0, sizeof(request));
417
418 request.cause.present = Cause_PR_misc;
419 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
420
421 HNBDe_Register_t request_out;
422 memset(&request_out, 0, sizeof(request_out));
423 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
424 if (rc < 0) {
425 printf("Could not encode HNB deregister request IEs\n");
426 }
427
428 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
429 Criticality_reject,
430 &asn_DEF_HNBDe_Register,
431 &request_out);
432
433 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100434
435 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
436}
437
438
Daniel Willmann97374c02015-12-03 09:37:58 +0100439static const struct log_info_cat log_cat[] = {
440 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100441 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100442 .color = "",
443 .description = "Main program",
444 },
Daniel Willmann32797802015-12-17 12:53:05 +0100445 [DHNBAP] = {
446 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
447 .color = "",
448 .description = "Home Node B Application Part",
449 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100450};
451
452static const struct log_info hnb_test_log_info = {
453 .cat = log_cat,
454 .num_cat = ARRAY_SIZE(log_cat),
455};
456
457static struct vty_app_info vty_info = {
458 .name = "OsmoHNB-Test",
459 .version = "0",
460};
461
Daniel Willmann4abdee02015-12-09 17:57:32 +0100462static int sctp_sock_init(int fd)
463{
464 struct sctp_event_subscribe event;
465 int rc;
466
467 /* subscribe for all events */
468 memset((uint8_t *)&event, 1, sizeof(event));
469 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
470 &event, sizeof(event));
471
472 return rc;
473}
474
Harald Weltec3851222015-12-24 15:41:21 +0100475#define HNBAP_STR "HNBAP related commands\n"
476#define HNB_STR "HomeNodeB commands\n"
477#define UE_STR "User Equipment commands\n"
478#define RANAP_STR "RANAP related commands\n"
479#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
480
481DEFUN(hnb_register, hnb_register_cmd,
482 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
483{
484 hnb_send_register_req(&g_hnb_test);
485
486 return CMD_SUCCESS;
487}
488
489DEFUN(hnb_deregister, hnb_deregister_cmd,
490 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
491{
492 hnb_send_deregister_req(&g_hnb_test);
493
494 return CMD_SUCCESS;
495}
496
497DEFUN(ue_register, ue_register_cmd,
498 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
499{
500 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
501
502 return CMD_SUCCESS;
503}
504
505DEFUN(asn_dbg, asn_dbg_cmd,
506 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
507{
508 asn_debug = atoi(argv[0]);
509
510 return CMD_SUCCESS;
511}
512
513DEFUN(ranap_reset, ranap_reset_cmd,
514 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
515{
516 int is_ps = 0;
517 struct msgb *msg, *rua;
518
519 RANAP_Cause_t cause = {
520 .present = RANAP_Cause_PR_transmissionNetwork,
521 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
522 };
523
524 if (!strcmp(argv[0], "ps"))
525 is_ps = 1;
526
527 msg = ranap_new_msg_reset(is_ps, &cause);
528 rua = rua_new_udt(msg);
529 //msgb_free(msg);
530 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
531
532 return CMD_SUCCESS;
533}
534
535
536enum my_vty_nodes {
537 CHAN_NODE = _LAST_OSMOVTY_NODE,
538};
539
540static struct cmd_node chan_node = {
541 CHAN_NODE,
542 "%s(chan)> ",
543 1,
544};
545
546
547struct hnbtest_chan {
548 int is_ps;
549 uint32_t conn_id;
550 char *imsi;
551};
552
553static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
554{
555 uint8_t lu[] = { 0x05, 0x08, 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
Neels Hofmeyr32828702016-01-14 13:06:47 +0100556 /* len, IMSI/type, IMSI-------------------------------- */
Harald Weltec3851222015-12-24 15:41:21 +0100557 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
558 0x33, 0x03, 0x57, 0x18 , 0xb2 };
559 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
560 RANAP_GlobalRNC_ID_t rnc_id = {
561 .rNC_ID = 23,
562 .pLMNidentity.buf = plmn_id,
563 .pLMNidentity.size = sizeof(plmn_id),
564 };
565 struct msgb *msg;
566
567 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100568 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
569 * an even/odd indicator bit encoded in the first octet. So the first
570 * octet looks like this:
571 *
572 * 8 7 6 5 | 4 | 3 2 1
573 * IMSI-digit | even/odd | type
574 *
575 * followed by the remaining IMSI digits.
576 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
577 * (derived from Iu pcap Location Update Request msg and TS 25.413)
578 *
579 * TODO I'm only 90% sure about this
580 */
Harald Weltec3851222015-12-24 15:41:21 +0100581
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100582 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100583}
584
585DEFUN(chan, chan_cmd,
586 "channel (cs|ps) lu imsi IMSI",
587 "Open a new Signalling Connection\n"
588 "To Circuit-Switched CN\n"
589 "To Packet-Switched CN\n"
590 "Performing a Location Update\n"
591 )
592{
593 struct hnbtest_chan *chan;
594 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100595 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100596
597 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
598 if (!strcmp(argv[0], "ps"))
599 chan->is_ps = 1;
600 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100601 chan->conn_id = conn_id;
602 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100603
604 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
605 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
606
607 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
608
609 vty->index = chan;
610 vty->node = CHAN_NODE;
611
612 return CMD_SUCCESS;
613}
614
615static void hnbtest_vty_init(void)
616{
617 install_element_ve(&asn_dbg_cmd);
618 install_element_ve(&hnb_register_cmd);
619 install_element_ve(&hnb_deregister_cmd);
620 install_element_ve(&ue_register_cmd);
621 install_element_ve(&ranap_reset_cmd);
622 install_element_ve(&chan_cmd);
623
624 install_node(&chan_node, NULL);
625 vty_install_default(CHAN_NODE);
626}
627
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100628static void handle_options(int argc, char **argv)
629{
630 while (1) {
631 int idx = 0, c;
632 static const struct option long_options[] = {
633 { "ues", 1, 0, 'u' },
634 { 0, 0, 0, 0 },
635 };
636
637 c = getopt_long(argc, argv, "u:", long_options, &idx);
638
639 if (c == -1)
640 break;
641
642 switch (c) {
643 case 'u':
644 g_hnb_test.ues = atoi(optarg);
645 break;
646 }
647 }
648}
649
Harald Weltec3851222015-12-24 15:41:21 +0100650int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100651{
652 int rc;
653
Harald Welte87ffeb92015-12-25 15:34:22 +0100654 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100655
Harald Welte87ffeb92015-12-25 15:34:22 +0100656 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100657
658 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100659 hnbtest_vty_init();
660
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100661 printf("VTY at %s %d\n", vty_get_bind_addr(), 2324);
662 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100663 if (rc < 0) {
664 perror("Error binding VTY port");
665 exit(1);
666 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100667
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100668 handle_options(argc, argv);
669
Daniel Willmann97374c02015-12-03 09:37:58 +0100670 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
671 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
672 g_hnb_test.wqueue.read_cb = hnb_read_cb;
673 g_hnb_test.wqueue.write_cb = hnb_write_cb;
674
675 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
676 IPPROTO_SCTP, "127.0.0.1",
677 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
678 if (rc < 0) {
679 perror("Error connecting to Iuh port");
680 exit(1);
681 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100682 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100683
Harald Weltec3851222015-12-24 15:41:21 +0100684#if 0
685 /* some hard-coded message generation. Doesn't make sense from
686 * a protocol point of view but enables to look at the encoded
687 * results in wireshark for manual verification */
688 {
689 struct msgb *msg, *rua;
690 const uint8_t nas[] = { 0, 1, 2, 3 };
691 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
692
693 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
694 rua = rua_new_udt(msg);
695 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
696
697 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
698 rua = rua_new_udt(msg);
699 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
700
701 msg = ranap_new_msg_iu_rel_cmd()
702 rua = rua_new_udt(msg);
703 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
704
705 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
706 rua = rua_new_udt(msg);
707 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
708
709 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
710 rua = rua_new_udt(msg);
711 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
712
713 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
714 rua = rua_new_udt(msg);
715 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
716 }
717#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100718
Daniel Willmann97374c02015-12-03 09:37:58 +0100719 while (1) {
720 rc = osmo_select_main(0);
721 if (rc < 0)
722 exit(3);
723 }
724
725 /* not reached */
726 exit(0);
727}