blob: 1f0685a239347b0cceb8db4ee7f4b7dc7c27149f [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 Hofmeyr0968a582016-01-11 15:19:38 +0100211int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
212{
213 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
214 asn_dec_rval_t dec_ret;
215 int rc;
216
217 memset(pdu, 0, sizeof(*pdu));
218 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
219 msg->data, msgb_length(msg), 0, 0);
220 if (dec_ret.code != RC_OK) {
221 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
222 return rc;
223 }
224
225 switch (pdu->present) {
226 case RUA_RUA_PDU_PR_successfulOutcome:
227 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
228 break;
229 case RUA_RUA_PDU_PR_initiatingMessage:
230 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
231 break;
232 case RUA_RUA_PDU_PR_NOTHING:
233 printf("RUA_RUA_PDU_PR_NOTHING\n");
234 break;
235 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
236 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
237 break;
238 default:
239 printf("Unexpected RUA message received\n");
240 break;
241 }
242
243 switch (pdu->choice.successfulOutcome.procedureCode) {
244 case RUA_ProcedureCode_id_ConnectionlessTransfer:
245 printf("RUA rx Connectionless Transfer\n");
246 break;
247 case RUA_ProcedureCode_id_Connect:
248 printf("RUA rx Connect\n");
249 break;
250 case RUA_ProcedureCode_id_DirectTransfer:
251 printf("RUA rx DirectTransfer\n");
252 break;
253 case RUA_ProcedureCode_id_Disconnect:
254 printf("RUA rx Disconnect\n");
255 break;
256 case RUA_ProcedureCode_id_ErrorIndication:
257 printf("RUA rx ErrorIndication\n");
258 break;
259 case RUA_ProcedureCode_id_privateMessage:
260 printf("RUA rx privateMessage\n");
261 break;
262 default:
263 printf("RUA rx unknown message\n");
264 break;
265 }
266
267 return rc;
268}
269
Daniel Willmann97374c02015-12-03 09:37:58 +0100270static int hnb_read_cb(struct osmo_fd *fd)
271{
272 struct hnb_test *hnb_test = fd->data;
273 struct sctp_sndrcvinfo sinfo;
274 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
275 int flags = 0;
276 int rc;
277
278 if (!msg)
279 return -ENOMEM;
280
281 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
282 NULL, NULL, &sinfo, &flags);
283 if (rc < 0) {
284 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
285 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100286 close(fd->fd);
287 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100288 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100289 } else if (rc == 0) {
290 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
291 close(fd->fd);
292 osmo_fd_unregister(fd);
293 fd->fd = -1;
294
295 return -1;
296 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100297 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100298 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100299
300 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100301 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100302 msgb_free(msg);
303 return 0;
304 }
305
306 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
307
308 switch (sinfo.sinfo_ppid) {
309 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100310 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100311 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100312 break;
313 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100314 printf("RUA message received\n");
315 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100316 break;
317 case IUH_PPI_SABP:
318 case IUH_PPI_RNA:
319 case IUH_PPI_PUA:
320 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
321 sinfo.sinfo_ppid);
322 rc = 0;
323 break;
324 default:
325 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
326 sinfo.sinfo_ppid);
327 rc = 0;
328 break;
329 }
330
331 msgb_free(msg);
332 return rc;
333}
334
335static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
336{
337 struct hnb_test *ctx = fd->data;
338 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100339 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100340 .sinfo_stream = 0,
341 };
342 int rc;
343
344 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
345 &sinfo, 0);
346 /* we don't need to msgb_free(), write_queue does this for us */
347 return rc;
348}
349
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100350static void hnb_send_register_req(struct hnb_test *hnb_test)
351{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100352 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100353 struct msgb *msg;
354 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100355 uint16_t lac, sac;
356 uint8_t rac;
357 uint32_t cid;
358 uint8_t plmn[] = {0x09, 0xf1, 0x99};
359 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100360
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100361 HNBRegisterRequestIEs_t request;
362 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100363
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100364 lac = 0xc0fe;
365 sac = 0xabab;
366 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100367 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100368
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100369 asn1_u16_to_str(&request.lac, &lac, lac);
370 asn1_u16_to_str(&request.sac, &sac, sac);
371 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100372 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100373
374 request.hnB_Identity.hNB_Identity_Info.buf = identity;
375 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
376
377 request.plmNidentity.buf = plmn;
378 request.plmNidentity.size = 3;
379
380
381
382 memset(&request_out, 0, sizeof(request_out));
383 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
384 if (rc < 0) {
385 printf("Could not encode HNB register request IEs\n");
386 }
387
388 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
389 Criticality_reject,
390 &asn_DEF_HNBRegisterRequest,
391 &request_out);
392
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100393
Harald Weltec3851222015-12-24 15:41:21 +0100394 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
395
396 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
397}
398
399static void hnb_send_deregister_req(struct hnb_test *hnb_test)
400{
401 struct msgb *msg;
402 int rc;
403
404 HNBDe_RegisterIEs_t request;
405 memset(&request, 0, sizeof(request));
406
407 request.cause.present = Cause_PR_misc;
408 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
409
410 HNBDe_Register_t request_out;
411 memset(&request_out, 0, sizeof(request_out));
412 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
413 if (rc < 0) {
414 printf("Could not encode HNB deregister request IEs\n");
415 }
416
417 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
418 Criticality_reject,
419 &asn_DEF_HNBDe_Register,
420 &request_out);
421
422 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100423
424 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
425}
426
427
Daniel Willmann97374c02015-12-03 09:37:58 +0100428static const struct log_info_cat log_cat[] = {
429 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100430 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100431 .color = "",
432 .description = "Main program",
433 },
Daniel Willmann32797802015-12-17 12:53:05 +0100434 [DHNBAP] = {
435 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
436 .color = "",
437 .description = "Home Node B Application Part",
438 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100439};
440
441static const struct log_info hnb_test_log_info = {
442 .cat = log_cat,
443 .num_cat = ARRAY_SIZE(log_cat),
444};
445
446static struct vty_app_info vty_info = {
447 .name = "OsmoHNB-Test",
448 .version = "0",
449};
450
Daniel Willmann4abdee02015-12-09 17:57:32 +0100451static int sctp_sock_init(int fd)
452{
453 struct sctp_event_subscribe event;
454 int rc;
455
456 /* subscribe for all events */
457 memset((uint8_t *)&event, 1, sizeof(event));
458 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
459 &event, sizeof(event));
460
461 return rc;
462}
463
Harald Weltec3851222015-12-24 15:41:21 +0100464#define HNBAP_STR "HNBAP related commands\n"
465#define HNB_STR "HomeNodeB commands\n"
466#define UE_STR "User Equipment commands\n"
467#define RANAP_STR "RANAP related commands\n"
468#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
469
470DEFUN(hnb_register, hnb_register_cmd,
471 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
472{
473 hnb_send_register_req(&g_hnb_test);
474
475 return CMD_SUCCESS;
476}
477
478DEFUN(hnb_deregister, hnb_deregister_cmd,
479 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
480{
481 hnb_send_deregister_req(&g_hnb_test);
482
483 return CMD_SUCCESS;
484}
485
486DEFUN(ue_register, ue_register_cmd,
487 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
488{
489 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
490
491 return CMD_SUCCESS;
492}
493
494DEFUN(asn_dbg, asn_dbg_cmd,
495 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
496{
497 asn_debug = atoi(argv[0]);
498
499 return CMD_SUCCESS;
500}
501
502DEFUN(ranap_reset, ranap_reset_cmd,
503 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
504{
505 int is_ps = 0;
506 struct msgb *msg, *rua;
507
508 RANAP_Cause_t cause = {
509 .present = RANAP_Cause_PR_transmissionNetwork,
510 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
511 };
512
513 if (!strcmp(argv[0], "ps"))
514 is_ps = 1;
515
516 msg = ranap_new_msg_reset(is_ps, &cause);
517 rua = rua_new_udt(msg);
518 //msgb_free(msg);
519 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
520
521 return CMD_SUCCESS;
522}
523
524
525enum my_vty_nodes {
526 CHAN_NODE = _LAST_OSMOVTY_NODE,
527};
528
529static struct cmd_node chan_node = {
530 CHAN_NODE,
531 "%s(chan)> ",
532 1,
533};
534
535
536struct hnbtest_chan {
537 int is_ps;
538 uint32_t conn_id;
539 char *imsi;
540};
541
542static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
543{
544 uint8_t lu[] = { 0x05, 0x08, 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
545 /* len- IMSI------------------------------------------ */
546 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
547 0x33, 0x03, 0x57, 0x18 , 0xb2 };
548 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
549 RANAP_GlobalRNC_ID_t rnc_id = {
550 .rNC_ID = 23,
551 .pLMNidentity.buf = plmn_id,
552 .pLMNidentity.size = sizeof(plmn_id),
553 };
554 struct msgb *msg;
555
556 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100557 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
558 * an even/odd indicator bit encoded in the first octet. So the first
559 * octet looks like this:
560 *
561 * 8 7 6 5 | 4 | 3 2 1
562 * IMSI-digit | even/odd | type
563 *
564 * followed by the remaining IMSI digits.
565 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
566 * (derived from Iu pcap Location Update Request msg and TS 25.413)
567 *
568 * TODO I'm only 90% sure about this
569 */
Harald Weltec3851222015-12-24 15:41:21 +0100570
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100571 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100572}
573
574DEFUN(chan, chan_cmd,
575 "channel (cs|ps) lu imsi IMSI",
576 "Open a new Signalling Connection\n"
577 "To Circuit-Switched CN\n"
578 "To Packet-Switched CN\n"
579 "Performing a Location Update\n"
580 )
581{
582 struct hnbtest_chan *chan;
583 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100584 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100585
586 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
587 if (!strcmp(argv[0], "ps"))
588 chan->is_ps = 1;
589 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100590 chan->conn_id = conn_id;
591 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100592
593 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
594 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
595
596 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
597
598 vty->index = chan;
599 vty->node = CHAN_NODE;
600
601 return CMD_SUCCESS;
602}
603
604static void hnbtest_vty_init(void)
605{
606 install_element_ve(&asn_dbg_cmd);
607 install_element_ve(&hnb_register_cmd);
608 install_element_ve(&hnb_deregister_cmd);
609 install_element_ve(&ue_register_cmd);
610 install_element_ve(&ranap_reset_cmd);
611 install_element_ve(&chan_cmd);
612
613 install_node(&chan_node, NULL);
614 vty_install_default(CHAN_NODE);
615}
616
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100617static void handle_options(int argc, char **argv)
618{
619 while (1) {
620 int idx = 0, c;
621 static const struct option long_options[] = {
622 { "ues", 1, 0, 'u' },
623 { 0, 0, 0, 0 },
624 };
625
626 c = getopt_long(argc, argv, "u:", long_options, &idx);
627
628 if (c == -1)
629 break;
630
631 switch (c) {
632 case 'u':
633 g_hnb_test.ues = atoi(optarg);
634 break;
635 }
636 }
637}
638
Harald Weltec3851222015-12-24 15:41:21 +0100639int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100640{
641 int rc;
642
Harald Welte87ffeb92015-12-25 15:34:22 +0100643 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100644
Harald Welte87ffeb92015-12-25 15:34:22 +0100645 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100646
647 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100648 hnbtest_vty_init();
649
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100650 printf("VTY at %s %d\n", vty_get_bind_addr(), 2324);
651 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100652 if (rc < 0) {
653 perror("Error binding VTY port");
654 exit(1);
655 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100656
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100657 handle_options(argc, argv);
658
Daniel Willmann97374c02015-12-03 09:37:58 +0100659 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
660 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
661 g_hnb_test.wqueue.read_cb = hnb_read_cb;
662 g_hnb_test.wqueue.write_cb = hnb_write_cb;
663
664 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
665 IPPROTO_SCTP, "127.0.0.1",
666 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
667 if (rc < 0) {
668 perror("Error connecting to Iuh port");
669 exit(1);
670 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100671 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100672
Harald Weltec3851222015-12-24 15:41:21 +0100673#if 0
674 /* some hard-coded message generation. Doesn't make sense from
675 * a protocol point of view but enables to look at the encoded
676 * results in wireshark for manual verification */
677 {
678 struct msgb *msg, *rua;
679 const uint8_t nas[] = { 0, 1, 2, 3 };
680 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
681
682 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
683 rua = rua_new_udt(msg);
684 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
685
686 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
687 rua = rua_new_udt(msg);
688 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
689
690 msg = ranap_new_msg_iu_rel_cmd()
691 rua = rua_new_udt(msg);
692 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
693
694 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
695 rua = rua_new_udt(msg);
696 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
697
698 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
699 rua = rua_new_udt(msg);
700 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
701
702 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
703 rua = rua_new_udt(msg);
704 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
705 }
706#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100707
Daniel Willmann97374c02015-12-03 09:37:58 +0100708 while (1) {
709 rc = osmo_select_main(0);
710 if (rc < 0)
711 exit(3);
712 }
713
714 /* not reached */
715 exit(0);
716}