blob: 0f794b7ba5b25b9ee216412c9c460c0eccd482b3 [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
59static void *tall_hnb_ctx;
Daniel Willmann97374c02015-12-03 09:37:58 +010060
61struct hnb_test g_hnb_test = {
62 .gw_port = IUH_DEFAULT_SCTP_PORT,
63};
64
Harald Weltec3851222015-12-24 15:41:21 +010065struct msgb *rua_new_udt(struct msgb *inmsg);
66
Harald Weltec3851222015-12-24 15:41:21 +010067static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test)
Daniel Willmann19dedbb2015-12-17 11:57:41 +010068{
69 struct msgb *msg;
70 int rc, imsi_len;
71 uint32_t ctx_id;
72
73 UEDe_Register_t dereg;
74 UEDe_RegisterIEs_t dereg_ies;
75 memset(&dereg_ies, 0, sizeof(dereg_ies));
76
77 asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id);
78 dereg_ies.cause.present = Cause_PR_radioNetwork;
79 dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost;
80
81 memset(&dereg, 0, sizeof(dereg));
82 rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies);
83
84 msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register,
85 Criticality_ignore,
86 &asn_DEF_UEDe_Register,
87 &dereg);
88
Harald Weltec3851222015-12-24 15:41:21 +010089 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg);
Daniel Willmann19dedbb2015-12-17 11:57:41 +010090
Harald Weltec3851222015-12-24 15:41:21 +010091 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann19dedbb2015-12-17 11:57:41 +010092
93 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
94}
95
Harald Weltec3851222015-12-24 15:41:21 +010096static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str)
Daniel Willmann479cb302015-12-09 17:54:59 +010097{
Daniel Willmann4e312502015-12-09 17:59:24 +010098 struct msgb *msg;
99 int rc, imsi_len;
100
101 char imsi_buf[16];
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100102
Daniel Willmann4e312502015-12-09 17:59:24 +0100103 UERegisterRequest_t request_out;
104 UERegisterRequestIEs_t request;
105 memset(&request, 0, sizeof(request));
106
107 request.uE_Identity.present = UE_Identity_PR_iMSI;
108
Harald Welte056984f2016-01-03 16:31:31 +0100109 imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str);
Harald Weltec3851222015-12-24 15:41:21 +0100110 OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, imsi_buf, imsi_len);
Daniel Willmann4e312502015-12-09 17:59:24 +0100111
112 request.registration_Cause = Registration_Cause_normal;
113 request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6;
114 request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable;
115
116 memset(&request_out, 0, sizeof(request_out));
117 rc = hnbap_encode_ueregisterrequesties(&request_out, &request);
118
119 msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister,
120 Criticality_reject,
121 &asn_DEF_UERegisterRequest,
122 &request_out);
123
Harald Weltec3851222015-12-24 15:41:21 +0100124 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out);
Daniel Willmann4e312502015-12-09 17:59:24 +0100125
Harald Weltec3851222015-12-24 15:41:21 +0100126 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4e312502015-12-09 17:59:24 +0100127
128 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
Daniel Willmann479cb302015-12-09 17:54:59 +0100129}
130
Harald Weltec3851222015-12-24 15:41:21 +0100131static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmann479cb302015-12-09 17:54:59 +0100132{
133 int rc;
134 HNBRegisterAcceptIEs_t accept;
135
136 rc = hnbap_decode_hnbregisteraccepties(&accept, in);
137 if (rc < 0) {
138 }
139
140 hnb->rnc_id = accept.rnc_id;
141 printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id);
142
Daniel Willmann11e912a2016-01-07 13:19:30 +0100143 hnbap_free_hnbregisteraccepties(&accept);
Harald Weltec3851222015-12-24 15:41:21 +0100144 return 0;
Daniel Willmann479cb302015-12-09 17:54:59 +0100145}
146
Harald Weltec3851222015-12-24 15:41:21 +0100147static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmanna7b02402015-12-09 19:05:09 +0100148{
149 int rc;
150 uint32_t ctx_id;
151 UERegisterAcceptIEs_t accept;
152 char imsi[16];
153
154 rc = hnbap_decode_ueregisteraccepties(&accept, in);
155 if (rc < 0) {
156 return rc;
157 }
158
159 if (accept.uE_Identity.present != UE_Identity_PR_iMSI) {
160 printf("Wrong type in UE register accept\n");
161 return -1;
162 }
163
164 ctx_id = asn1bitstr_to_u24(&accept.context_ID);
165
Harald Welte056984f2016-01-03 16:31:31 +0100166 ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf,
Daniel Willmanna7b02402015-12-09 19:05:09 +0100167 accept.uE_Identity.choice.iMSI.size);
168 printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id);
169
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100170 hnb->ctx_id = ctx_id;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100171 hnbap_free_ueregisteraccepties(&accept);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100172
Daniel Willmanna7b02402015-12-09 19:05:09 +0100173 return 0;
174}
175
Daniel Willmann479cb302015-12-09 17:54:59 +0100176int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
177{
178 HNBAP_PDU_t _pdu, *pdu = &_pdu;
179 asn_dec_rval_t dec_ret;
180 int rc;
181
182 memset(pdu, 0, sizeof(*pdu));
183 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
184 msg->data, msgb_length(msg), 0, 0);
185 if (dec_ret.code != RC_OK) {
186 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
187 return rc;
188 }
189
190 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
191 printf("Unexpected HNBAP message received\n");
192 }
193
194 switch (pdu->choice.successfulOutcome.procedureCode) {
195 case ProcedureCode_id_HNBRegister:
196 /* Get HNB id and send UE Register request */
197 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
198 break;
199 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100200 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100201 break;
202 default:
203 break;
204 }
205
206 return rc;
207}
208
Daniel Willmann97374c02015-12-03 09:37:58 +0100209static int hnb_read_cb(struct osmo_fd *fd)
210{
211 struct hnb_test *hnb_test = fd->data;
212 struct sctp_sndrcvinfo sinfo;
213 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
214 int flags = 0;
215 int rc;
216
217 if (!msg)
218 return -ENOMEM;
219
220 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
221 NULL, NULL, &sinfo, &flags);
222 if (rc < 0) {
223 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
224 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100225 close(fd->fd);
226 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100227 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100228 } else if (rc == 0) {
229 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
230 close(fd->fd);
231 osmo_fd_unregister(fd);
232 fd->fd = -1;
233
234 return -1;
235 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100236 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100237 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100238
239 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100240 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100241 msgb_free(msg);
242 return 0;
243 }
244
245 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
246
247 switch (sinfo.sinfo_ppid) {
248 case IUH_PPI_HNBAP:
249 printf("HNBAP mesage received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100250 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100251 break;
252 case IUH_PPI_RUA:
253 printf("RUA mesage received\n");
254// rc = hnbgw_rua_rx(hnb, msg);
255 break;
256 case IUH_PPI_SABP:
257 case IUH_PPI_RNA:
258 case IUH_PPI_PUA:
259 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
260 sinfo.sinfo_ppid);
261 rc = 0;
262 break;
263 default:
264 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
265 sinfo.sinfo_ppid);
266 rc = 0;
267 break;
268 }
269
270 msgb_free(msg);
271 return rc;
272}
273
274static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
275{
276 struct hnb_test *ctx = fd->data;
277 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100278 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100279 .sinfo_stream = 0,
280 };
281 int rc;
282
283 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
284 &sinfo, 0);
285 /* we don't need to msgb_free(), write_queue does this for us */
286 return rc;
287}
288
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100289static void hnb_send_register_req(struct hnb_test *hnb_test)
290{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100291 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100292 struct msgb *msg;
293 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100294 uint16_t lac, sac;
295 uint8_t rac;
296 uint32_t cid;
297 uint8_t plmn[] = {0x09, 0xf1, 0x99};
298 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100299
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100300 HNBRegisterRequestIEs_t request;
301 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100302
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100303 lac = 0xc0fe;
304 sac = 0xabab;
305 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100306 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100307
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100308 asn1_u16_to_str(&request.lac, &lac, lac);
309 asn1_u16_to_str(&request.sac, &sac, sac);
310 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100311 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100312
313 request.hnB_Identity.hNB_Identity_Info.buf = identity;
314 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
315
316 request.plmNidentity.buf = plmn;
317 request.plmNidentity.size = 3;
318
319
320
321 memset(&request_out, 0, sizeof(request_out));
322 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
323 if (rc < 0) {
324 printf("Could not encode HNB register request IEs\n");
325 }
326
327 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
328 Criticality_reject,
329 &asn_DEF_HNBRegisterRequest,
330 &request_out);
331
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100332
Harald Weltec3851222015-12-24 15:41:21 +0100333 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
334
335 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
336}
337
338static void hnb_send_deregister_req(struct hnb_test *hnb_test)
339{
340 struct msgb *msg;
341 int rc;
342
343 HNBDe_RegisterIEs_t request;
344 memset(&request, 0, sizeof(request));
345
346 request.cause.present = Cause_PR_misc;
347 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
348
349 HNBDe_Register_t request_out;
350 memset(&request_out, 0, sizeof(request_out));
351 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
352 if (rc < 0) {
353 printf("Could not encode HNB deregister request IEs\n");
354 }
355
356 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
357 Criticality_reject,
358 &asn_DEF_HNBDe_Register,
359 &request_out);
360
361 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100362
363 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
364}
365
366
Daniel Willmann97374c02015-12-03 09:37:58 +0100367static const struct log_info_cat log_cat[] = {
368 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100369 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100370 .color = "",
371 .description = "Main program",
372 },
Daniel Willmann32797802015-12-17 12:53:05 +0100373 [DHNBAP] = {
374 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
375 .color = "",
376 .description = "Home Node B Application Part",
377 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100378};
379
380static const struct log_info hnb_test_log_info = {
381 .cat = log_cat,
382 .num_cat = ARRAY_SIZE(log_cat),
383};
384
385static struct vty_app_info vty_info = {
386 .name = "OsmoHNB-Test",
387 .version = "0",
388};
389
Daniel Willmann4abdee02015-12-09 17:57:32 +0100390static int sctp_sock_init(int fd)
391{
392 struct sctp_event_subscribe event;
393 int rc;
394
395 /* subscribe for all events */
396 memset((uint8_t *)&event, 1, sizeof(event));
397 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
398 &event, sizeof(event));
399
400 return rc;
401}
402
Harald Weltec3851222015-12-24 15:41:21 +0100403#define HNBAP_STR "HNBAP related commands\n"
404#define HNB_STR "HomeNodeB commands\n"
405#define UE_STR "User Equipment commands\n"
406#define RANAP_STR "RANAP related commands\n"
407#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
408
409DEFUN(hnb_register, hnb_register_cmd,
410 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
411{
412 hnb_send_register_req(&g_hnb_test);
413
414 return CMD_SUCCESS;
415}
416
417DEFUN(hnb_deregister, hnb_deregister_cmd,
418 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
419{
420 hnb_send_deregister_req(&g_hnb_test);
421
422 return CMD_SUCCESS;
423}
424
425DEFUN(ue_register, ue_register_cmd,
426 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
427{
428 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
429
430 return CMD_SUCCESS;
431}
432
433DEFUN(asn_dbg, asn_dbg_cmd,
434 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
435{
436 asn_debug = atoi(argv[0]);
437
438 return CMD_SUCCESS;
439}
440
441DEFUN(ranap_reset, ranap_reset_cmd,
442 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
443{
444 int is_ps = 0;
445 struct msgb *msg, *rua;
446
447 RANAP_Cause_t cause = {
448 .present = RANAP_Cause_PR_transmissionNetwork,
449 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
450 };
451
452 if (!strcmp(argv[0], "ps"))
453 is_ps = 1;
454
455 msg = ranap_new_msg_reset(is_ps, &cause);
456 rua = rua_new_udt(msg);
457 //msgb_free(msg);
458 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
459
460 return CMD_SUCCESS;
461}
462
463
464enum my_vty_nodes {
465 CHAN_NODE = _LAST_OSMOVTY_NODE,
466};
467
468static struct cmd_node chan_node = {
469 CHAN_NODE,
470 "%s(chan)> ",
471 1,
472};
473
474
475struct hnbtest_chan {
476 int is_ps;
477 uint32_t conn_id;
478 char *imsi;
479};
480
481static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
482{
483 uint8_t lu[] = { 0x05, 0x08, 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
484 /* len- IMSI------------------------------------------ */
485 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
486 0x33, 0x03, 0x57, 0x18 , 0xb2 };
487 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
488 RANAP_GlobalRNC_ID_t rnc_id = {
489 .rNC_ID = 23,
490 .pLMNidentity.buf = plmn_id,
491 .pLMNidentity.size = sizeof(plmn_id),
492 };
493 struct msgb *msg;
494
495 /* FIXME: patch imsi */
496
497 return ranap_new_msg_initial_ue(is_ps, conn_id, &rnc_id, lu, sizeof(lu));
498}
499
500DEFUN(chan, chan_cmd,
501 "channel (cs|ps) lu imsi IMSI",
502 "Open a new Signalling Connection\n"
503 "To Circuit-Switched CN\n"
504 "To Packet-Switched CN\n"
505 "Performing a Location Update\n"
506 )
507{
508 struct hnbtest_chan *chan;
509 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100510 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100511
512 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
513 if (!strcmp(argv[0], "ps"))
514 chan->is_ps = 1;
515 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100516 chan->conn_id = conn_id;
517 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100518
519 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
520 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
521
522 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
523
524 vty->index = chan;
525 vty->node = CHAN_NODE;
526
527 return CMD_SUCCESS;
528}
529
530static void hnbtest_vty_init(void)
531{
532 install_element_ve(&asn_dbg_cmd);
533 install_element_ve(&hnb_register_cmd);
534 install_element_ve(&hnb_deregister_cmd);
535 install_element_ve(&ue_register_cmd);
536 install_element_ve(&ranap_reset_cmd);
537 install_element_ve(&chan_cmd);
538
539 install_node(&chan_node, NULL);
540 vty_install_default(CHAN_NODE);
541}
542
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100543static void handle_options(int argc, char **argv)
544{
545 while (1) {
546 int idx = 0, c;
547 static const struct option long_options[] = {
548 { "ues", 1, 0, 'u' },
549 { 0, 0, 0, 0 },
550 };
551
552 c = getopt_long(argc, argv, "u:", long_options, &idx);
553
554 if (c == -1)
555 break;
556
557 switch (c) {
558 case 'u':
559 g_hnb_test.ues = atoi(optarg);
560 break;
561 }
562 }
563}
564
Harald Weltec3851222015-12-24 15:41:21 +0100565int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100566{
567 int rc;
568
Harald Welte87ffeb92015-12-25 15:34:22 +0100569 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100570
Harald Welte87ffeb92015-12-25 15:34:22 +0100571 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100572
573 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100574 hnbtest_vty_init();
575
576 rc = telnet_init(NULL, NULL, 2324);
577 if (rc < 0) {
578 perror("Error binding VTY port");
579 exit(1);
580 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100581
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100582 handle_options(argc, argv);
583
Daniel Willmann97374c02015-12-03 09:37:58 +0100584 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
585 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
586 g_hnb_test.wqueue.read_cb = hnb_read_cb;
587 g_hnb_test.wqueue.write_cb = hnb_write_cb;
588
589 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
590 IPPROTO_SCTP, "127.0.0.1",
591 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
592 if (rc < 0) {
593 perror("Error connecting to Iuh port");
594 exit(1);
595 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100596 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100597
Harald Weltec3851222015-12-24 15:41:21 +0100598#if 0
599 /* some hard-coded message generation. Doesn't make sense from
600 * a protocol point of view but enables to look at the encoded
601 * results in wireshark for manual verification */
602 {
603 struct msgb *msg, *rua;
604 const uint8_t nas[] = { 0, 1, 2, 3 };
605 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
606
607 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
608 rua = rua_new_udt(msg);
609 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
610
611 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
612 rua = rua_new_udt(msg);
613 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
614
615 msg = ranap_new_msg_iu_rel_cmd()
616 rua = rua_new_udt(msg);
617 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
618
619 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
620 rua = rua_new_udt(msg);
621 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
622
623 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
624 rua = rua_new_udt(msg);
625 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
626
627 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
628 rua = rua_new_udt(msg);
629 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
630 }
631#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100632
Daniel Willmann97374c02015-12-03 09:37:58 +0100633 while (1) {
634 rc = osmo_select_main(0);
635 if (rc < 0)
636 exit(3);
637 }
638
639 /* not reached */
640 exit(0);
641}