blob: 639e1773da337a5ca00b64e1a9219170eecb2df5 [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
196 return ranap_generate_initiating_message(RANAP_ProcedureCode_id_DirectTransfer,
197 RANAP_Criticality_ignore,
198 &asn_DEF_RANAP_DirectTransfer,
199 &id_resp);
200}
201
202
203static int hnb_test_nas_tx_id_resp(struct hnb_test *hnb)
204{
205 struct hnbtest_chan *chan;
206 struct msgb *txm, *rua;
207
208 chan = hnb->cs.chan;
209 if (!chan) {
210 printf("hnb_test_nas_tx_id_resp(): No CS channel established yet.\n");
211 return -1;
212 }
213
214 txm = gen_nas_id_resp();
215 rua = rua_new_dt(chan->is_ps, chan->conn_id, txm);
216
217 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
218
219 return 0;
220}
221
222static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct msgb *rxm)
223{
224 struct hnbtest_chan *chan;
225
226 chan = hnb->cs.chan;
227 if (!chan) {
228 printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n");
229 return -1;
230 }
231
232 OSMO_ASSERT(!chan->is_ps);
233
234 struct gsm48_hdr *gh = msgb_l3(rxm);
235 uint8_t msg_type = gh->msg_type & 0xbf;
236
237 switch (msg_type) {
238 case GSM48_MT_MM_ID_REQ:
239
240 return hnb_test_nas_tx_id_resp(hnb);
241 default:
242 printf("04.08 message type not handled by hnb-test: %d\n",
243 msg_type);
244 return 0;
245 }
246
247}
248
249static int hnb_test_nas_rx_dtap(struct hnb_test *hnb, struct msgb *msg)
250{
251 printf("got %s\n", osmo_hexdump(msg->data, msg->len));
252
253 // nas_pdu == '05 08 12' ==> IMEI Identity request
254 // '05 04 0d' ==> LU reject
255
256 struct gsm48_hdr *gh = msgb_l3(msg);
257 uint8_t pdisc = gh->proto_discr & 0x0f;
258
259 switch (pdisc) {
260 case GSM48_PDISC_MM:
261 return hnb_test_nas_rx_mm(hnb, msg);
262 default:
263 printf("04.08 discriminator not handled by hnb-test: %d\n",
264 pdisc);
265 return 0;
266 }
267
268
269}
270
Daniel Willmann479cb302015-12-09 17:54:59 +0100271int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
272{
273 HNBAP_PDU_t _pdu, *pdu = &_pdu;
274 asn_dec_rval_t dec_ret;
275 int rc;
276
277 memset(pdu, 0, sizeof(*pdu));
278 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
279 msg->data, msgb_length(msg), 0, 0);
280 if (dec_ret.code != RC_OK) {
281 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
282 return rc;
283 }
284
285 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
286 printf("Unexpected HNBAP message received\n");
287 }
288
289 switch (pdu->choice.successfulOutcome.procedureCode) {
290 case ProcedureCode_id_HNBRegister:
291 /* Get HNB id and send UE Register request */
292 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
293 break;
294 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100295 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100296 break;
297 default:
298 break;
299 }
300
301 return rc;
302}
303
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100304extern void direct_transfer_nas_pdu_print(ANY_t *in);
305
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100306int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
307{
308 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
309 asn_dec_rval_t dec_ret;
310 int rc;
311
312 memset(pdu, 0, sizeof(*pdu));
313 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
314 msg->data, msgb_length(msg), 0, 0);
315 if (dec_ret.code != RC_OK) {
316 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
317 return rc;
318 }
319
320 switch (pdu->present) {
321 case RUA_RUA_PDU_PR_successfulOutcome:
322 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
323 break;
324 case RUA_RUA_PDU_PR_initiatingMessage:
325 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
326 break;
327 case RUA_RUA_PDU_PR_NOTHING:
328 printf("RUA_RUA_PDU_PR_NOTHING\n");
329 break;
330 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
331 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
332 break;
333 default:
334 printf("Unexpected RUA message received\n");
335 break;
336 }
337
338 switch (pdu->choice.successfulOutcome.procedureCode) {
339 case RUA_ProcedureCode_id_ConnectionlessTransfer:
340 printf("RUA rx Connectionless Transfer\n");
341 break;
342 case RUA_ProcedureCode_id_Connect:
343 printf("RUA rx Connect\n");
344 break;
345 case RUA_ProcedureCode_id_DirectTransfer:
346 printf("RUA rx DirectTransfer\n");
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100347 {
348 struct msgb *m = msgb_alloc(1500, "direct_transfer_nas_pdu");
349 direct_transfer_nas_pdu_get(&pdu->choice.successfulOutcome.value, m);
350
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100351 hnb_test_nas_rx_dtap(hnb, m);
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100352
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100353 msgb_free(m);
354 }
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100355 break;
356 case RUA_ProcedureCode_id_Disconnect:
357 printf("RUA rx Disconnect\n");
358 break;
359 case RUA_ProcedureCode_id_ErrorIndication:
360 printf("RUA rx ErrorIndication\n");
361 break;
362 case RUA_ProcedureCode_id_privateMessage:
363 printf("RUA rx privateMessage\n");
364 break;
365 default:
366 printf("RUA rx unknown message\n");
367 break;
368 }
369
370 return rc;
371}
372
Daniel Willmann97374c02015-12-03 09:37:58 +0100373static int hnb_read_cb(struct osmo_fd *fd)
374{
375 struct hnb_test *hnb_test = fd->data;
376 struct sctp_sndrcvinfo sinfo;
377 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
378 int flags = 0;
379 int rc;
380
381 if (!msg)
382 return -ENOMEM;
383
384 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
385 NULL, NULL, &sinfo, &flags);
386 if (rc < 0) {
387 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
388 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100389 close(fd->fd);
390 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100391 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100392 } else if (rc == 0) {
393 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
394 close(fd->fd);
395 osmo_fd_unregister(fd);
396 fd->fd = -1;
397
398 return -1;
399 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100400 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100401 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100402
403 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100404 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100405 msgb_free(msg);
406 return 0;
407 }
408
409 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
410
411 switch (sinfo.sinfo_ppid) {
412 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100413 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100414 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100415 break;
416 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100417 printf("RUA message received\n");
418 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100419 break;
420 case IUH_PPI_SABP:
421 case IUH_PPI_RNA:
422 case IUH_PPI_PUA:
423 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
424 sinfo.sinfo_ppid);
425 rc = 0;
426 break;
427 default:
428 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
429 sinfo.sinfo_ppid);
430 rc = 0;
431 break;
432 }
433
434 msgb_free(msg);
435 return rc;
436}
437
438static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
439{
440 struct hnb_test *ctx = fd->data;
441 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100442 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100443 .sinfo_stream = 0,
444 };
445 int rc;
446
447 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
448 &sinfo, 0);
449 /* we don't need to msgb_free(), write_queue does this for us */
450 return rc;
451}
452
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100453static void hnb_send_register_req(struct hnb_test *hnb_test)
454{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100455 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100456 struct msgb *msg;
457 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100458 uint16_t lac, sac;
459 uint8_t rac;
460 uint32_t cid;
461 uint8_t plmn[] = {0x09, 0xf1, 0x99};
462 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100463
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100464 HNBRegisterRequestIEs_t request;
465 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100466
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100467 lac = 0xc0fe;
468 sac = 0xabab;
469 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100470 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100471
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100472 asn1_u16_to_str(&request.lac, &lac, lac);
473 asn1_u16_to_str(&request.sac, &sac, sac);
474 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100475 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100476
477 request.hnB_Identity.hNB_Identity_Info.buf = identity;
478 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
479
480 request.plmNidentity.buf = plmn;
481 request.plmNidentity.size = 3;
482
483
484
485 memset(&request_out, 0, sizeof(request_out));
486 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
487 if (rc < 0) {
488 printf("Could not encode HNB register request IEs\n");
489 }
490
491 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
492 Criticality_reject,
493 &asn_DEF_HNBRegisterRequest,
494 &request_out);
495
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100496
Harald Weltec3851222015-12-24 15:41:21 +0100497 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
498
499 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
500}
501
502static void hnb_send_deregister_req(struct hnb_test *hnb_test)
503{
504 struct msgb *msg;
505 int rc;
506
507 HNBDe_RegisterIEs_t request;
508 memset(&request, 0, sizeof(request));
509
510 request.cause.present = Cause_PR_misc;
511 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
512
513 HNBDe_Register_t request_out;
514 memset(&request_out, 0, sizeof(request_out));
515 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
516 if (rc < 0) {
517 printf("Could not encode HNB deregister request IEs\n");
518 }
519
520 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
521 Criticality_reject,
522 &asn_DEF_HNBDe_Register,
523 &request_out);
524
525 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100526
527 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
528}
529
530
Daniel Willmann97374c02015-12-03 09:37:58 +0100531static const struct log_info_cat log_cat[] = {
532 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100533 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100534 .color = "",
535 .description = "Main program",
536 },
Daniel Willmann32797802015-12-17 12:53:05 +0100537 [DHNBAP] = {
538 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
539 .color = "",
540 .description = "Home Node B Application Part",
541 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100542};
543
544static const struct log_info hnb_test_log_info = {
545 .cat = log_cat,
546 .num_cat = ARRAY_SIZE(log_cat),
547};
548
549static struct vty_app_info vty_info = {
550 .name = "OsmoHNB-Test",
551 .version = "0",
552};
553
Daniel Willmann4abdee02015-12-09 17:57:32 +0100554static int sctp_sock_init(int fd)
555{
556 struct sctp_event_subscribe event;
557 int rc;
558
559 /* subscribe for all events */
560 memset((uint8_t *)&event, 1, sizeof(event));
561 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
562 &event, sizeof(event));
563
564 return rc;
565}
566
Harald Weltec3851222015-12-24 15:41:21 +0100567#define HNBAP_STR "HNBAP related commands\n"
568#define HNB_STR "HomeNodeB commands\n"
569#define UE_STR "User Equipment commands\n"
570#define RANAP_STR "RANAP related commands\n"
571#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
572
573DEFUN(hnb_register, hnb_register_cmd,
574 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
575{
576 hnb_send_register_req(&g_hnb_test);
577
578 return CMD_SUCCESS;
579}
580
581DEFUN(hnb_deregister, hnb_deregister_cmd,
582 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
583{
584 hnb_send_deregister_req(&g_hnb_test);
585
586 return CMD_SUCCESS;
587}
588
589DEFUN(ue_register, ue_register_cmd,
590 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
591{
592 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
593
594 return CMD_SUCCESS;
595}
596
597DEFUN(asn_dbg, asn_dbg_cmd,
598 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
599{
600 asn_debug = atoi(argv[0]);
601
602 return CMD_SUCCESS;
603}
604
605DEFUN(ranap_reset, ranap_reset_cmd,
606 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
607{
608 int is_ps = 0;
609 struct msgb *msg, *rua;
610
611 RANAP_Cause_t cause = {
612 .present = RANAP_Cause_PR_transmissionNetwork,
613 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
614 };
615
616 if (!strcmp(argv[0], "ps"))
617 is_ps = 1;
618
619 msg = ranap_new_msg_reset(is_ps, &cause);
620 rua = rua_new_udt(msg);
621 //msgb_free(msg);
622 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
623
624 return CMD_SUCCESS;
625}
626
627
628enum my_vty_nodes {
629 CHAN_NODE = _LAST_OSMOVTY_NODE,
630};
631
632static struct cmd_node chan_node = {
633 CHAN_NODE,
634 "%s(chan)> ",
635 1,
636};
637
638
Harald Weltec3851222015-12-24 15:41:21 +0100639static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
640{
641 uint8_t lu[] = { 0x05, 0x08, 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
Neels Hofmeyr32828702016-01-14 13:06:47 +0100642 /* len, IMSI/type, IMSI-------------------------------- */
Harald Weltec3851222015-12-24 15:41:21 +0100643 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
644 0x33, 0x03, 0x57, 0x18 , 0xb2 };
645 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
646 RANAP_GlobalRNC_ID_t rnc_id = {
647 .rNC_ID = 23,
648 .pLMNidentity.buf = plmn_id,
649 .pLMNidentity.size = sizeof(plmn_id),
650 };
Harald Weltec3851222015-12-24 15:41:21 +0100651
652 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100653 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
654 * an even/odd indicator bit encoded in the first octet. So the first
655 * octet looks like this:
656 *
657 * 8 7 6 5 | 4 | 3 2 1
658 * IMSI-digit | even/odd | type
659 *
660 * followed by the remaining IMSI digits.
661 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
662 * (derived from Iu pcap Location Update Request msg and TS 25.413)
663 *
664 * TODO I'm only 90% sure about this
665 */
Harald Weltec3851222015-12-24 15:41:21 +0100666
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100667 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100668}
669
670DEFUN(chan, chan_cmd,
671 "channel (cs|ps) lu imsi IMSI",
672 "Open a new Signalling Connection\n"
673 "To Circuit-Switched CN\n"
674 "To Packet-Switched CN\n"
675 "Performing a Location Update\n"
676 )
677{
678 struct hnbtest_chan *chan;
679 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100680 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100681
682 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
683 if (!strcmp(argv[0], "ps"))
684 chan->is_ps = 1;
685 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100686 chan->conn_id = conn_id;
687 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100688
689 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
690 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
691
692 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
693
694 vty->index = chan;
695 vty->node = CHAN_NODE;
696
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100697 if (!chan->is_ps)
698 g_hnb_test.cs.chan = chan;
699
700
Harald Weltec3851222015-12-24 15:41:21 +0100701 return CMD_SUCCESS;
702}
703
704static void hnbtest_vty_init(void)
705{
706 install_element_ve(&asn_dbg_cmd);
707 install_element_ve(&hnb_register_cmd);
708 install_element_ve(&hnb_deregister_cmd);
709 install_element_ve(&ue_register_cmd);
710 install_element_ve(&ranap_reset_cmd);
711 install_element_ve(&chan_cmd);
712
713 install_node(&chan_node, NULL);
714 vty_install_default(CHAN_NODE);
715}
716
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100717static void handle_options(int argc, char **argv)
718{
719 while (1) {
720 int idx = 0, c;
721 static const struct option long_options[] = {
722 { "ues", 1, 0, 'u' },
723 { 0, 0, 0, 0 },
724 };
725
726 c = getopt_long(argc, argv, "u:", long_options, &idx);
727
728 if (c == -1)
729 break;
730
731 switch (c) {
732 case 'u':
733 g_hnb_test.ues = atoi(optarg);
734 break;
735 }
736 }
737}
738
Harald Weltec3851222015-12-24 15:41:21 +0100739int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100740{
741 int rc;
742
Harald Welte87ffeb92015-12-25 15:34:22 +0100743 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100744
Harald Welte87ffeb92015-12-25 15:34:22 +0100745 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100746
747 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100748 hnbtest_vty_init();
749
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100750 printf("VTY at %s %d\n", vty_get_bind_addr(), 2324);
751 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100752 if (rc < 0) {
753 perror("Error binding VTY port");
754 exit(1);
755 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100756
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100757 handle_options(argc, argv);
758
Daniel Willmann97374c02015-12-03 09:37:58 +0100759 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
760 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
761 g_hnb_test.wqueue.read_cb = hnb_read_cb;
762 g_hnb_test.wqueue.write_cb = hnb_write_cb;
763
764 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
765 IPPROTO_SCTP, "127.0.0.1",
766 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
767 if (rc < 0) {
768 perror("Error connecting to Iuh port");
769 exit(1);
770 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100771 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100772
Harald Weltec3851222015-12-24 15:41:21 +0100773#if 0
774 /* some hard-coded message generation. Doesn't make sense from
775 * a protocol point of view but enables to look at the encoded
776 * results in wireshark for manual verification */
777 {
778 struct msgb *msg, *rua;
779 const uint8_t nas[] = { 0, 1, 2, 3 };
780 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
781
782 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
783 rua = rua_new_udt(msg);
784 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
785
786 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
787 rua = rua_new_udt(msg);
788 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
789
790 msg = ranap_new_msg_iu_rel_cmd()
791 rua = rua_new_udt(msg);
792 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
793
794 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
795 rua = rua_new_udt(msg);
796 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
797
798 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
799 rua = rua_new_udt(msg);
800 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
801
802 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
803 rua = rua_new_udt(msg);
804 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
805 }
806#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100807
Daniel Willmann97374c02015-12-03 09:37:58 +0100808 while (1) {
809 rc = osmo_select_main(0);
810 if (rc < 0)
811 exit(3);
812 }
813
814 /* not reached */
815 exit(0);
816}