blob: acd8b13bc1763d8e0c10c9e299736997e6903e8f [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>
Neels Hofmeyrae937122016-02-29 09:32:00 +010044#include <osmocom/gsm/tlv.h>
45#include <osmocom/gsm/gsm48.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010046
47#include <osmocom/vty/telnet_interface.h>
48#include <osmocom/vty/logging.h>
Harald Weltec3851222015-12-24 15:41:21 +010049#include <osmocom/vty/command.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010050
Neels Hofmeyr26779f82016-04-18 17:04:17 +020051#include <osmocom/crypt/auth.h>
52
Daniel Willmann97374c02015-12-03 09:37:58 +010053#include "hnb-test.h"
Neels Hofmeyr4470f932016-04-19 00:13:53 +020054#include "hnb-test-layers.h"
Neels Hofmeyr83457922016-08-26 23:56:44 +020055#include <osmocom/hnbap/hnbap_common.h>
56#include <osmocom/hnbap/hnbap_ies_defs.h>
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020057#include <osmocom/rua/rua_msg_factory.h>
Harald Weltec3851222015-12-24 15:41:21 +010058#include "asn1helpers.h"
Neels Hofmeyr96979af2016-01-05 15:19:44 +010059#include <osmocom/ranap/iu_helpers.h>
Harald Welte87ffeb92015-12-25 15:34:22 +010060#include "test_common.h"
Harald Weltec3851222015-12-24 15:41:21 +010061
Neels Hofmeyr96979af2016-01-05 15:19:44 +010062#include <osmocom/ranap/ranap_msg_factory.h>
Daniel Willmann97374c02015-12-03 09:37:58 +010063
Neels Hofmeyr0968a582016-01-11 15:19:38 +010064#include <osmocom/rua/RUA_RUA-PDU.h>
65
Neels Hofmeyr860a1292016-02-18 23:03:15 +010066#include <osmocom/gsm/protocol/gsm_04_08.h>
67
68#include <osmocom/ranap/RANAP_ProcedureCode.h>
69#include <osmocom/ranap/RANAP_Criticality.h>
70#include <osmocom/ranap/RANAP_DirectTransfer.h>
71
Daniel Willmann97374c02015-12-03 09:37:58 +010072static void *tall_hnb_ctx;
Daniel Willmann97374c02015-12-03 09:37:58 +010073
74struct hnb_test g_hnb_test = {
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +010075 .gw_addr = "127.0.0.1",
Daniel Willmann97374c02015-12-03 09:37:58 +010076 .gw_port = IUH_DEFAULT_SCTP_PORT,
77};
78
Harald Weltec3851222015-12-24 15:41:21 +010079struct msgb *rua_new_udt(struct msgb *inmsg);
80
Pau Espin Pedrole6342e12021-01-14 11:31:24 +010081#if 0
Harald Weltec3851222015-12-24 15:41:21 +010082static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test)
Daniel Willmann19dedbb2015-12-17 11:57:41 +010083{
84 struct msgb *msg;
Pau Espin Pedrol89f4c802021-01-14 11:13:46 +010085 int rc;
Daniel Willmann19dedbb2015-12-17 11:57:41 +010086 uint32_t ctx_id;
87
88 UEDe_Register_t dereg;
89 UEDe_RegisterIEs_t dereg_ies;
90 memset(&dereg_ies, 0, sizeof(dereg_ies));
91
92 asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id);
93 dereg_ies.cause.present = Cause_PR_radioNetwork;
94 dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost;
95
96 memset(&dereg, 0, sizeof(dereg));
97 rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies);
Pau Espin Pedrol96a94bc2021-01-14 11:17:19 +010098 OSMO_ASSERT(rc == 0);
Daniel Willmann19dedbb2015-12-17 11:57:41 +010099
100 msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register,
101 Criticality_ignore,
102 &asn_DEF_UEDe_Register,
103 &dereg);
104
Harald Weltec3851222015-12-24 15:41:21 +0100105 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100106
Harald Weltec3851222015-12-24 15:41:21 +0100107 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100108
109 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
110}
Pau Espin Pedrole6342e12021-01-14 11:31:24 +0100111#endif
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100112
Harald Weltec3851222015-12-24 15:41:21 +0100113static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str)
Daniel Willmann479cb302015-12-09 17:54:59 +0100114{
Daniel Willmann4e312502015-12-09 17:59:24 +0100115 struct msgb *msg;
116 int rc, imsi_len;
117
Pau Espin Pedrolb4ff1f42021-01-14 11:14:54 +0100118 uint8_t imsi_buf[16];
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100119
Daniel Willmann4e312502015-12-09 17:59:24 +0100120 UERegisterRequest_t request_out;
121 UERegisterRequestIEs_t request;
122 memset(&request, 0, sizeof(request));
123
124 request.uE_Identity.present = UE_Identity_PR_iMSI;
125
Harald Welte056984f2016-01-03 16:31:31 +0100126 imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str);
Pau Espin Pedrolb4ff1f42021-01-14 11:14:54 +0100127 OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, (const char*)imsi_buf, imsi_len);
Daniel Willmann4e312502015-12-09 17:59:24 +0100128
129 request.registration_Cause = Registration_Cause_normal;
130 request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6;
131 request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable;
132
133 memset(&request_out, 0, sizeof(request_out));
134 rc = hnbap_encode_ueregisterrequesties(&request_out, &request);
Pau Espin Pedrol96a94bc2021-01-14 11:17:19 +0100135 OSMO_ASSERT(rc == 0);
Daniel Willmann4e312502015-12-09 17:59:24 +0100136
137 msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister,
138 Criticality_reject,
139 &asn_DEF_UERegisterRequest,
140 &request_out);
141
Harald Weltec3851222015-12-24 15:41:21 +0100142 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out);
Daniel Willmann4e312502015-12-09 17:59:24 +0100143
Harald Weltec3851222015-12-24 15:41:21 +0100144 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4e312502015-12-09 17:59:24 +0100145
146 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
Daniel Willmann479cb302015-12-09 17:54:59 +0100147}
148
Harald Weltec3851222015-12-24 15:41:21 +0100149static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmann479cb302015-12-09 17:54:59 +0100150{
151 int rc;
152 HNBRegisterAcceptIEs_t accept;
153
154 rc = hnbap_decode_hnbregisteraccepties(&accept, in);
155 if (rc < 0) {
156 }
157
158 hnb->rnc_id = accept.rnc_id;
159 printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id);
160
Daniel Willmann11e912a2016-01-07 13:19:30 +0100161 hnbap_free_hnbregisteraccepties(&accept);
Harald Weltec3851222015-12-24 15:41:21 +0100162 return 0;
Daniel Willmann479cb302015-12-09 17:54:59 +0100163}
164
Harald Weltec3851222015-12-24 15:41:21 +0100165static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmanna7b02402015-12-09 19:05:09 +0100166{
167 int rc;
168 uint32_t ctx_id;
169 UERegisterAcceptIEs_t accept;
170 char imsi[16];
171
172 rc = hnbap_decode_ueregisteraccepties(&accept, in);
173 if (rc < 0) {
174 return rc;
175 }
176
177 if (accept.uE_Identity.present != UE_Identity_PR_iMSI) {
178 printf("Wrong type in UE register accept\n");
179 return -1;
180 }
181
182 ctx_id = asn1bitstr_to_u24(&accept.context_ID);
183
Harald Welte056984f2016-01-03 16:31:31 +0100184 ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf,
Daniel Willmanna7b02402015-12-09 19:05:09 +0100185 accept.uE_Identity.choice.iMSI.size);
186 printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id);
187
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100188 hnb->ctx_id = ctx_id;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100189 hnbap_free_ueregisteraccepties(&accept);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100190
Daniel Willmanna7b02402015-12-09 19:05:09 +0100191 return 0;
192}
193
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100194static struct msgb *gen_nas_id_resp()
195{
196 uint8_t id_resp[] = {
Neels Hofmeyr5c1cc8c2016-02-29 09:28:48 +0100197 GSM48_PDISC_MM,
198 GSM48_MT_MM_ID_RESP,
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100199 /* IMEISV */
200 0x09, /* len */
201 0x03, /* first digit (0000) + even (0) + id IMEISV (011) */
202 0x31, 0x91, 0x06, 0x00, 0x28, 0x47, 0x11, /* digits */
203 0xf2, /* filler (1111) + last digit (0010) */
204 };
205
Neels Hofmeyre1f709f2016-02-28 00:50:45 +0100206 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100207}
208
Neels Hofmeyrae937122016-02-29 09:32:00 +0100209static struct msgb *gen_nas_tmsi_realloc_compl()
210{
211 uint8_t id_resp[] = {
212 GSM48_PDISC_MM,
213 GSM48_MT_MM_TMSI_REALL_COMPL,
214 };
215
216 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
217}
218
Neels Hofmeyr26779f82016-04-18 17:04:17 +0200219static struct msgb *gen_nas_auth_resp(uint8_t *sres)
Neels Hofmeyr35888102016-03-09 01:39:56 +0100220{
221 uint8_t id_resp[] = {
222 GSM48_PDISC_MM,
Neels Hofmeyr99872602016-04-05 11:51:15 +0200223 0x80 | GSM48_MT_MM_AUTH_RESP, /* simulate sequence nr 2 */
Neels Hofmeyr35888102016-03-09 01:39:56 +0100224 0x61, 0xb5, 0x69, 0xf5 /* hardcoded SRES */
225 };
226
Neels Hofmeyr26779f82016-04-18 17:04:17 +0200227 memcpy(id_resp + 2, sres, 4);
228
Neels Hofmeyr35888102016-03-09 01:39:56 +0100229 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
230}
231
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200232static int hnb_test_tx_dt(struct hnb_test *hnb, struct msgb *txm)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100233{
234 struct hnbtest_chan *chan;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100235 struct msgb *rua;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100236
237 chan = hnb->cs.chan;
238 if (!chan) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100239 printf("hnb_test_nas_tx_tmsi_realloc_compl(): No CS channel established yet.\n");
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100240 return -1;
241 }
242
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100243 rua = rua_new_dt(chan->is_ps, chan->conn_id, txm);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100244 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100245 return 0;
246}
247
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200248static struct tlv_parsed *parse_mm(struct gsm48_hdr *gh, int len)
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100249{
250 static struct tlv_parsed tp;
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100251 int parse_res;
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100252
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200253 len -= (const char *)&gh->data[0] - (const char *)gh;
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100254
Neels Hofmeyrd7ad0ac2016-04-05 11:52:27 +0200255 OSMO_ASSERT(gsm48_hdr_pdisc(gh) == GSM48_PDISC_MM);
256
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200257 parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], len, 0, 0);
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100258 if (parse_res <= 0) {
Neels Hofmeyr7c28f6f2016-04-05 11:49:53 +0200259 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100260 printf("Error parsing MM message 0x%hhx: %d\n", msg_type, parse_res);
261 return NULL;
262 }
263
264 return &tp;
265}
266
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200267int hnb_test_nas_rx_lu_accept(struct gsm48_hdr *gh, int len, int *sent_tmsi)
Neels Hofmeyrae937122016-02-29 09:32:00 +0100268{
269 printf(" :D Location Update Accept :D\n");
Neels Hofmeyrae937122016-02-29 09:32:00 +0100270 struct gsm48_loc_area_id *lai;
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100271
Neels Hofmeyrae937122016-02-29 09:32:00 +0100272 lai = (struct gsm48_loc_area_id *)&gh->data[0];
273
Neels Hofmeyr30508642018-02-22 04:04:22 +0100274 struct osmo_location_area_id laid;
275 gsm48_decode_lai2(lai, &laid);
276 printf("LU: mcc %s mnc %s lac %hd\n",
277 osmo_mcc_name(laid.plmn.mcc), osmo_mnc_name(laid.plmn.mnc, laid.plmn.mnc_3_digits),
278 laid.lac);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100279
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100280 struct tlv_parsed tp;
281 int parse_res;
282
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200283 len -= (const char *)&gh->data[0] - (const char *)gh;
284 parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], len, 0, 0);
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100285 if (parse_res <= 0) {
286 printf("Error parsing Location Update Accept message: %d\n", parse_res);
287 return -1;
288 }
289
290 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
291 uint8_t type = TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)[0] & 0x0f;
292 if (type == GSM_MI_TYPE_TMSI)
293 *sent_tmsi = 1;
294 else *sent_tmsi = 0;
295 }
296 return 0;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100297}
298
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200299void hnb_test_nas_rx_mm_info(struct gsm48_hdr *gh, int len)
Neels Hofmeyrae937122016-02-29 09:32:00 +0100300{
301 printf(" :) MM Info :)\n");
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200302 struct tlv_parsed *tp = parse_mm(gh, len);
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100303 if (!tp)
Neels Hofmeyrae937122016-02-29 09:32:00 +0100304 return;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100305
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100306 if (TLVP_PRESENT(tp, GSM48_IE_NAME_SHORT)) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100307 char name[128] = {0};
308 gsm_7bit_decode_n(name, 127,
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100309 TLVP_VAL(tp, GSM48_IE_NAME_SHORT)+1,
310 (TLVP_LEN(tp, GSM48_IE_NAME_SHORT)-1)*8/7);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100311 printf("Info: Short Network Name: %s\n", name);
312 }
313
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100314 if (TLVP_PRESENT(tp, GSM48_IE_NAME_LONG)) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100315 char name[128] = {0};
316 gsm_7bit_decode_n(name, 127,
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100317 TLVP_VAL(tp, GSM48_IE_NAME_LONG)+1,
318 (TLVP_LEN(tp, GSM48_IE_NAME_LONG)-1)*8/7);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100319 printf("Info: Long Network Name: %s\n", name);
320 }
Neels Hofmeyrae937122016-02-29 09:32:00 +0100321}
322
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200323static int hnb_test_nas_rx_auth_req(struct hnb_test *hnb, struct gsm48_hdr *gh,
324 int len)
Neels Hofmeyr35888102016-03-09 01:39:56 +0100325{
Neels Hofmeyr35888102016-03-09 01:39:56 +0100326 struct gsm48_auth_req *ar;
Neels Hofmeyr35888102016-03-09 01:39:56 +0100327
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200328 len -= (const char *)&gh->data[0] - (const char *)gh;
Neels Hofmeyr35888102016-03-09 01:39:56 +0100329
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200330 if (len < sizeof(*ar)) {
Neels Hofmeyr35888102016-03-09 01:39:56 +0100331 printf("GSM48 Auth Req does not fit.\n");
Martin Haukec593da52020-02-15 23:01:39 +0100332 return -1;
Neels Hofmeyr35888102016-03-09 01:39:56 +0100333 }
334
335 printf(" :) Authentication Request :)\n");
336
337 ar = (struct gsm48_auth_req*) &gh->data[0];
338 int seq = ar->key_seq;
Neels Hofmeyr26779f82016-04-18 17:04:17 +0200339
340 /* Generate SRES from *HARDCODED* Ki for Iuh testing */
341 struct osmo_auth_vector vec;
342 /* Ki 000102030405060708090a0b0c0d0e0f */
343 struct osmo_sub_auth_data auth = {
344 .type = OSMO_AUTH_TYPE_GSM,
345 .algo = OSMO_AUTH_ALG_COMP128v1,
346 .u.gsm.ki = {
347 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
348 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
349 0x0e, 0x0f
350 },
351 };
352
353 memset(&vec, 0, sizeof(vec));
354 osmo_auth_gen_vec(&vec, &auth, ar->rand);
355
356 printf("seq %d rand %s",
357 seq, osmo_hexdump(ar->rand, sizeof(ar->rand)));
358 printf(" --> sres %s\n",
359 osmo_hexdump(vec.sres, 4));
360
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200361 return hnb_test_tx_dt(hnb, gen_nas_auth_resp(vec.sres));
Neels Hofmeyr35888102016-03-09 01:39:56 +0100362}
363
Neels Hofmeyrbde4d3b2016-04-19 02:38:16 +0200364void hnb_test_tx_iu_release_req(struct hnb_test *hnb)
365{
366 RANAP_Cause_t cause = {
367 .present = RANAP_Cause_PR_radioNetwork,
368 .choice.transmissionNetwork = RANAP_CauseRadioNetwork_release_due_to_UE_generated_signalling_connection_release,
369 };
370 hnb_test_tx_dt(hnb, ranap_new_msg_iu_rel_req(&cause));
371}
372
373void hnb_test_tx_iu_release_compl(struct hnb_test *hnb)
374{
375 hnb_test_tx_dt(hnb, ranap_new_msg_iu_rel_compl());
376}
377
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200378static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct gsm48_hdr *gh, int len)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100379{
380 struct hnbtest_chan *chan;
381
382 chan = hnb->cs.chan;
383 if (!chan) {
384 printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n");
385 return -1;
386 }
387
388 OSMO_ASSERT(!chan->is_ps);
389
Neels Hofmeyr7c28f6f2016-04-05 11:49:53 +0200390 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100391 int sent_tmsi;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100392
393 switch (msg_type) {
394 case GSM48_MT_MM_ID_REQ:
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200395 return hnb_test_tx_dt(hnb, gen_nas_id_resp());
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100396
Neels Hofmeyrae937122016-02-29 09:32:00 +0100397 case GSM48_MT_MM_LOC_UPD_ACCEPT:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200398 if (hnb_test_nas_rx_lu_accept(gh, len, &sent_tmsi))
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100399 return -1;
400 if (sent_tmsi)
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200401 return hnb_test_tx_dt(hnb, gen_nas_tmsi_realloc_compl());
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100402 else
403 return 0;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100404
Neels Hofmeyr5dbb7b22016-03-09 01:38:13 +0100405 case GSM48_MT_MM_LOC_UPD_REJECT:
406 printf("Received Location Update Reject\n");
407 return 0;
408
Neels Hofmeyrae937122016-02-29 09:32:00 +0100409 case GSM48_MT_MM_INFO:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200410 hnb_test_nas_rx_mm_info(gh, len);
Neels Hofmeyrbde4d3b2016-04-19 02:38:16 +0200411 hnb_test_tx_iu_release_req(hnb);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100412 return 0;
413
Neels Hofmeyr35888102016-03-09 01:39:56 +0100414 case GSM48_MT_MM_AUTH_REQ:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200415 return hnb_test_nas_rx_auth_req(hnb, gh, len);
Neels Hofmeyr35888102016-03-09 01:39:56 +0100416
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100417 default:
Neels Hofmeyrae937122016-02-29 09:32:00 +0100418 printf("04.08 message type not handled by hnb-test: 0x%x\n",
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100419 msg_type);
420 return 0;
421 }
422
423}
424
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200425void hnb_test_nas_rx_dtap(struct hnb_test *hnb, void *data, int len)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100426{
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200427 int rc;
428 printf("got %d bytes: %s\n", len, osmo_hexdump(data, len));
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100429
430 // nas_pdu == '05 08 12' ==> IMEI Identity request
431 // '05 04 0d' ==> LU reject
432
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200433 struct gsm48_hdr *gh = data;
434 if (len < sizeof(*gh)) {
435 printf("hnb_test_nas_rx_dtap(): NAS PDU is too short: %d. Ignoring.\n",
436 len);
437 return;
Neels Hofmeyr8c2b4ec2016-04-04 19:27:53 +0200438 }
Neels Hofmeyr7c28f6f2016-04-05 11:49:53 +0200439 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100440
441 switch (pdisc) {
442 case GSM48_PDISC_MM:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200443 rc = hnb_test_nas_rx_mm(hnb, gh, len);
444 if (rc != 0)
445 printf("Error receiving MM message: %d\n", rc);
446 return;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100447 default:
448 printf("04.08 discriminator not handled by hnb-test: %d\n",
449 pdisc);
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200450 return;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100451 }
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100452}
453
Neels Hofmeyr4a0a69a2016-04-19 00:06:28 +0200454void hnb_test_rx_secmode_cmd(struct hnb_test *hnb, long ip_alg)
455{
456 printf(" :) Security Mode Command :)\n");
457 /* not caring about encryption yet, just pass 0 for No Encryption. */
458 hnb_test_tx_dt(hnb, ranap_new_msg_sec_mod_compl(ip_alg, 0));
459}
460
Neels Hofmeyrbde4d3b2016-04-19 02:38:16 +0200461void hnb_test_rx_iu_release(struct hnb_test *hnb)
462{
463 hnb_test_tx_iu_release_compl(hnb);
464}
465
Neels Hofmeyr5ad72b92016-04-19 18:09:05 +0200466void hnb_test_rx_paging(struct hnb_test *hnb, const char *imsi)
467{
468 printf(" :) Paging Request for %s :)\n", imsi);
469 /* TODO reply */
470}
471
Daniel Willmann479cb302015-12-09 17:54:59 +0100472int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
473{
474 HNBAP_PDU_t _pdu, *pdu = &_pdu;
475 asn_dec_rval_t dec_ret;
476 int rc;
477
478 memset(pdu, 0, sizeof(*pdu));
479 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
480 msg->data, msgb_length(msg), 0, 0);
481 if (dec_ret.code != RC_OK) {
482 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
Pau Espin Pedrol84c3e862021-01-14 11:21:09 +0100483 return -EINVAL;
Daniel Willmann479cb302015-12-09 17:54:59 +0100484 }
485
486 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
487 printf("Unexpected HNBAP message received\n");
488 }
489
490 switch (pdu->choice.successfulOutcome.procedureCode) {
491 case ProcedureCode_id_HNBRegister:
492 /* Get HNB id and send UE Register request */
493 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
494 break;
495 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100496 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100497 break;
498 default:
499 break;
500 }
501
502 return rc;
503}
504
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100505extern void direct_transfer_nas_pdu_print(ANY_t *in);
506
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100507int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
508{
509 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
510 asn_dec_rval_t dec_ret;
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100511
512 memset(pdu, 0, sizeof(*pdu));
513 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
514 msg->data, msgb_length(msg), 0, 0);
515 if (dec_ret.code != RC_OK) {
516 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
Pau Espin Pedrol84c3e862021-01-14 11:21:09 +0100517 return -EINVAL;
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100518 }
519
520 switch (pdu->present) {
521 case RUA_RUA_PDU_PR_successfulOutcome:
522 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
523 break;
524 case RUA_RUA_PDU_PR_initiatingMessage:
525 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
526 break;
527 case RUA_RUA_PDU_PR_NOTHING:
528 printf("RUA_RUA_PDU_PR_NOTHING\n");
529 break;
530 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
531 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
532 break;
533 default:
534 printf("Unexpected RUA message received\n");
535 break;
536 }
537
538 switch (pdu->choice.successfulOutcome.procedureCode) {
539 case RUA_ProcedureCode_id_ConnectionlessTransfer:
540 printf("RUA rx Connectionless Transfer\n");
Neels Hofmeyr5ad72b92016-04-19 18:09:05 +0200541 hnb_test_rua_cl_handle(hnb, &pdu->choice.successfulOutcome.value);
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100542 break;
543 case RUA_ProcedureCode_id_Connect:
544 printf("RUA rx Connect\n");
545 break;
546 case RUA_ProcedureCode_id_DirectTransfer:
547 printf("RUA rx DirectTransfer\n");
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200548 hnb_test_rua_dt_handle(hnb, &pdu->choice.successfulOutcome.value);
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100549 break;
550 case RUA_ProcedureCode_id_Disconnect:
551 printf("RUA rx Disconnect\n");
552 break;
553 case RUA_ProcedureCode_id_ErrorIndication:
554 printf("RUA rx ErrorIndication\n");
555 break;
556 case RUA_ProcedureCode_id_privateMessage:
557 printf("RUA rx privateMessage\n");
558 break;
559 default:
560 printf("RUA rx unknown message\n");
561 break;
562 }
563
Pau Espin Pedrol84c3e862021-01-14 11:21:09 +0100564 return 0;
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100565}
566
Daniel Willmann97374c02015-12-03 09:37:58 +0100567static int hnb_read_cb(struct osmo_fd *fd)
568{
569 struct hnb_test *hnb_test = fd->data;
570 struct sctp_sndrcvinfo sinfo;
571 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
572 int flags = 0;
573 int rc;
574
575 if (!msg)
576 return -ENOMEM;
577
578 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
579 NULL, NULL, &sinfo, &flags);
580 if (rc < 0) {
581 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
582 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100583 close(fd->fd);
584 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100585 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100586 } else if (rc == 0) {
587 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
588 close(fd->fd);
589 osmo_fd_unregister(fd);
590 fd->fd = -1;
591
592 return -1;
593 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100594 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100595 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100596
597 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100598 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100599 msgb_free(msg);
600 return 0;
601 }
602
603 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
604
605 switch (sinfo.sinfo_ppid) {
606 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100607 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100608 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100609 break;
610 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100611 printf("RUA message received\n");
612 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100613 break;
614 case IUH_PPI_SABP:
615 case IUH_PPI_RNA:
616 case IUH_PPI_PUA:
617 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
618 sinfo.sinfo_ppid);
619 rc = 0;
620 break;
621 default:
622 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
623 sinfo.sinfo_ppid);
624 rc = 0;
625 break;
626 }
627
628 msgb_free(msg);
629 return rc;
630}
631
632static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
633{
Pau Espin Pedrol89f4c802021-01-14 11:13:46 +0100634 /* struct hnb_test *ctx = fd->data; */
Daniel Willmann97374c02015-12-03 09:37:58 +0100635 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100636 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100637 .sinfo_stream = 0,
638 };
639 int rc;
640
Neels Hofmeyre25faa82016-03-04 02:49:52 +0100641 printf("Sending: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
Daniel Willmann97374c02015-12-03 09:37:58 +0100642 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
643 &sinfo, 0);
644 /* we don't need to msgb_free(), write_queue does this for us */
645 return rc;
646}
647
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100648static void hnb_send_register_req(struct hnb_test *hnb_test)
649{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100650 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100651 struct msgb *msg;
652 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100653 uint16_t lac, sac;
654 uint8_t rac;
655 uint32_t cid;
656 uint8_t plmn[] = {0x09, 0xf1, 0x99};
657 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100658
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100659 HNBRegisterRequestIEs_t request;
660 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100661
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100662 lac = 0xc0fe;
663 sac = 0xabab;
664 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100665 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100666
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100667 asn1_u16_to_str(&request.lac, &lac, lac);
668 asn1_u16_to_str(&request.sac, &sac, sac);
669 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100670 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100671
Pau Espin Pedrol4c928482021-01-14 11:27:34 +0100672 request.hnB_Identity.hNB_Identity_Info.buf = (uint8_t*) identity;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100673 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
674
675 request.plmNidentity.buf = plmn;
676 request.plmNidentity.size = 3;
677
678
679
680 memset(&request_out, 0, sizeof(request_out));
681 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
682 if (rc < 0) {
683 printf("Could not encode HNB register request IEs\n");
684 }
685
686 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
687 Criticality_reject,
688 &asn_DEF_HNBRegisterRequest,
689 &request_out);
690
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100691
Harald Weltec3851222015-12-24 15:41:21 +0100692 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
693
694 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
695}
696
697static void hnb_send_deregister_req(struct hnb_test *hnb_test)
698{
699 struct msgb *msg;
700 int rc;
701
702 HNBDe_RegisterIEs_t request;
703 memset(&request, 0, sizeof(request));
704
705 request.cause.present = Cause_PR_misc;
706 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
707
708 HNBDe_Register_t request_out;
709 memset(&request_out, 0, sizeof(request_out));
710 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
711 if (rc < 0) {
712 printf("Could not encode HNB deregister request IEs\n");
713 }
714
715 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
716 Criticality_reject,
717 &asn_DEF_HNBDe_Register,
718 &request_out);
719
720 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100721
722 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
723}
724
725
Daniel Willmann97374c02015-12-03 09:37:58 +0100726static const struct log_info_cat log_cat[] = {
727 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100728 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100729 .color = "",
730 .description = "Main program",
731 },
Daniel Willmann32797802015-12-17 12:53:05 +0100732 [DHNBAP] = {
733 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
734 .color = "",
735 .description = "Home Node B Application Part",
736 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100737};
738
739static const struct log_info hnb_test_log_info = {
740 .cat = log_cat,
741 .num_cat = ARRAY_SIZE(log_cat),
742};
743
744static struct vty_app_info vty_info = {
745 .name = "OsmoHNB-Test",
746 .version = "0",
747};
748
Daniel Willmann4abdee02015-12-09 17:57:32 +0100749static int sctp_sock_init(int fd)
750{
751 struct sctp_event_subscribe event;
752 int rc;
753
754 /* subscribe for all events */
755 memset((uint8_t *)&event, 1, sizeof(event));
756 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
757 &event, sizeof(event));
758
759 return rc;
760}
761
Harald Weltec3851222015-12-24 15:41:21 +0100762#define HNBAP_STR "HNBAP related commands\n"
763#define HNB_STR "HomeNodeB commands\n"
764#define UE_STR "User Equipment commands\n"
765#define RANAP_STR "RANAP related commands\n"
766#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
767
768DEFUN(hnb_register, hnb_register_cmd,
769 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
770{
771 hnb_send_register_req(&g_hnb_test);
772
773 return CMD_SUCCESS;
774}
775
776DEFUN(hnb_deregister, hnb_deregister_cmd,
777 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
778{
779 hnb_send_deregister_req(&g_hnb_test);
780
781 return CMD_SUCCESS;
782}
783
784DEFUN(ue_register, ue_register_cmd,
785 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
786{
787 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
788
789 return CMD_SUCCESS;
790}
791
792DEFUN(asn_dbg, asn_dbg_cmd,
793 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
794{
795 asn_debug = atoi(argv[0]);
796
797 return CMD_SUCCESS;
798}
799
800DEFUN(ranap_reset, ranap_reset_cmd,
801 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
802{
803 int is_ps = 0;
804 struct msgb *msg, *rua;
805
806 RANAP_Cause_t cause = {
807 .present = RANAP_Cause_PR_transmissionNetwork,
808 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
809 };
810
811 if (!strcmp(argv[0], "ps"))
812 is_ps = 1;
813
814 msg = ranap_new_msg_reset(is_ps, &cause);
815 rua = rua_new_udt(msg);
816 //msgb_free(msg);
817 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
818
819 return CMD_SUCCESS;
820}
821
822
823enum my_vty_nodes {
824 CHAN_NODE = _LAST_OSMOVTY_NODE,
825};
826
827static struct cmd_node chan_node = {
828 CHAN_NODE,
829 "%s(chan)> ",
830 1,
831};
832
833
Harald Weltec3851222015-12-24 15:41:21 +0100834static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
835{
Neels Hofmeyr5c1cc8c2016-02-29 09:28:48 +0100836 uint8_t lu[] = { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST,
837 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
Neels Hofmeyr32828702016-01-14 13:06:47 +0100838 /* len, IMSI/type, IMSI-------------------------------- */
Harald Weltec3851222015-12-24 15:41:21 +0100839 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
840 0x33, 0x03, 0x57, 0x18 , 0xb2 };
841 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
842 RANAP_GlobalRNC_ID_t rnc_id = {
843 .rNC_ID = 23,
844 .pLMNidentity.buf = plmn_id,
845 .pLMNidentity.size = sizeof(plmn_id),
846 };
Harald Weltec3851222015-12-24 15:41:21 +0100847
848 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100849 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
850 * an even/odd indicator bit encoded in the first octet. So the first
851 * octet looks like this:
852 *
853 * 8 7 6 5 | 4 | 3 2 1
854 * IMSI-digit | even/odd | type
855 *
856 * followed by the remaining IMSI digits.
857 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
858 * (derived from Iu pcap Location Update Request msg and TS 25.413)
859 *
860 * TODO I'm only 90% sure about this
861 */
Harald Weltec3851222015-12-24 15:41:21 +0100862
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100863 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100864}
865
866DEFUN(chan, chan_cmd,
867 "channel (cs|ps) lu imsi IMSI",
868 "Open a new Signalling Connection\n"
869 "To Circuit-Switched CN\n"
870 "To Packet-Switched CN\n"
871 "Performing a Location Update\n"
872 )
873{
874 struct hnbtest_chan *chan;
875 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100876 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100877
878 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
879 if (!strcmp(argv[0], "ps"))
880 chan->is_ps = 1;
881 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100882 chan->conn_id = conn_id;
883 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100884
885 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
886 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
887
888 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
889
890 vty->index = chan;
891 vty->node = CHAN_NODE;
892
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100893 if (!chan->is_ps)
894 g_hnb_test.cs.chan = chan;
895
896
Harald Weltec3851222015-12-24 15:41:21 +0100897 return CMD_SUCCESS;
898}
899
900static void hnbtest_vty_init(void)
901{
902 install_element_ve(&asn_dbg_cmd);
903 install_element_ve(&hnb_register_cmd);
904 install_element_ve(&hnb_deregister_cmd);
905 install_element_ve(&ue_register_cmd);
906 install_element_ve(&ranap_reset_cmd);
907 install_element_ve(&chan_cmd);
908
909 install_node(&chan_node, NULL);
Harald Weltec3851222015-12-24 15:41:21 +0100910}
911
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100912static void handle_options(int argc, char **argv)
913{
914 while (1) {
915 int idx = 0, c;
916 static const struct option long_options[] = {
917 { "ues", 1, 0, 'u' },
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100918 { "gw-addr", 1, 0, 'g' },
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100919 { 0, 0, 0, 0 },
920 };
921
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100922 c = getopt_long(argc, argv, "u:g:", long_options, &idx);
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100923
924 if (c == -1)
925 break;
926
927 switch (c) {
928 case 'u':
929 g_hnb_test.ues = atoi(optarg);
930 break;
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100931 case 'g':
932 g_hnb_test.gw_addr = optarg;
933 break;
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100934 }
935 }
936}
937
Harald Weltec3851222015-12-24 15:41:21 +0100938int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100939{
940 int rc;
941
Harald Welte87ffeb92015-12-25 15:34:22 +0100942 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100943
Harald Welte87ffeb92015-12-25 15:34:22 +0100944 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100945
Pau Espin Pedrol86532332021-01-14 11:29:47 +0100946 osmo_init_logging2(tall_hnb_ctx, &hnb_test_log_info);
947
Daniel Willmann97374c02015-12-03 09:37:58 +0100948 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100949 hnbtest_vty_init();
950
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100951 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100952 if (rc < 0) {
953 perror("Error binding VTY port");
954 exit(1);
955 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100956
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100957 handle_options(argc, argv);
958
Daniel Willmann97374c02015-12-03 09:37:58 +0100959 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
960 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
961 g_hnb_test.wqueue.read_cb = hnb_read_cb;
962 g_hnb_test.wqueue.write_cb = hnb_write_cb;
963
964 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100965 IPPROTO_SCTP, g_hnb_test.gw_addr,
Daniel Willmann97374c02015-12-03 09:37:58 +0100966 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
967 if (rc < 0) {
968 perror("Error connecting to Iuh port");
969 exit(1);
970 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100971 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100972
Harald Weltec3851222015-12-24 15:41:21 +0100973#if 0
974 /* some hard-coded message generation. Doesn't make sense from
975 * a protocol point of view but enables to look at the encoded
976 * results in wireshark for manual verification */
977 {
978 struct msgb *msg, *rua;
979 const uint8_t nas[] = { 0, 1, 2, 3 };
980 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
981
982 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
983 rua = rua_new_udt(msg);
984 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
985
Daniel Willmannf44d12c2016-04-20 10:16:37 +0200986 msg = ranap_new_msg_sec_mod_cmd(ik, ik, RANAP_KeyStatus_new);
Harald Weltec3851222015-12-24 15:41:21 +0100987 rua = rua_new_udt(msg);
988 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
989
990 msg = ranap_new_msg_iu_rel_cmd()
991 rua = rua_new_udt(msg);
992 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
993
994 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
995 rua = rua_new_udt(msg);
996 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
997
998 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
999 rua = rua_new_udt(msg);
1000 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
1001
1002 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
1003 rua = rua_new_udt(msg);
1004 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
1005 }
1006#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +01001007
Daniel Willmann97374c02015-12-03 09:37:58 +01001008 while (1) {
1009 rc = osmo_select_main(0);
1010 if (rc < 0)
1011 exit(3);
1012 }
1013
1014 /* not reached */
1015 exit(0);
1016}