blob: 603f63b86d3f09fa7792cd0e273c611cbd6d3484 [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"
Daniel Willmanna1e202e2015-12-07 17:21:07 +010055#include "hnbap_common.h"
56#include "hnbap_ies_defs.h"
Harald Welteb66c5d02016-01-03 18:04:28 +010057#include "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
Harald Weltec3851222015-12-24 15:41:21 +010081static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test)
Daniel Willmann19dedbb2015-12-17 11:57:41 +010082{
83 struct msgb *msg;
84 int rc, imsi_len;
85 uint32_t ctx_id;
86
87 UEDe_Register_t dereg;
88 UEDe_RegisterIEs_t dereg_ies;
89 memset(&dereg_ies, 0, sizeof(dereg_ies));
90
91 asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id);
92 dereg_ies.cause.present = Cause_PR_radioNetwork;
93 dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost;
94
95 memset(&dereg, 0, sizeof(dereg));
96 rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies);
97
98 msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register,
99 Criticality_ignore,
100 &asn_DEF_UEDe_Register,
101 &dereg);
102
Harald Weltec3851222015-12-24 15:41:21 +0100103 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100104
Harald Weltec3851222015-12-24 15:41:21 +0100105 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100106
107 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
108}
109
Harald Weltec3851222015-12-24 15:41:21 +0100110static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str)
Daniel Willmann479cb302015-12-09 17:54:59 +0100111{
Daniel Willmann4e312502015-12-09 17:59:24 +0100112 struct msgb *msg;
113 int rc, imsi_len;
114
115 char imsi_buf[16];
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100116
Daniel Willmann4e312502015-12-09 17:59:24 +0100117 UERegisterRequest_t request_out;
118 UERegisterRequestIEs_t request;
119 memset(&request, 0, sizeof(request));
120
121 request.uE_Identity.present = UE_Identity_PR_iMSI;
122
Harald Welte056984f2016-01-03 16:31:31 +0100123 imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str);
Harald Weltec3851222015-12-24 15:41:21 +0100124 OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, imsi_buf, imsi_len);
Daniel Willmann4e312502015-12-09 17:59:24 +0100125
126 request.registration_Cause = Registration_Cause_normal;
127 request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6;
128 request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable;
129
130 memset(&request_out, 0, sizeof(request_out));
131 rc = hnbap_encode_ueregisterrequesties(&request_out, &request);
132
133 msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister,
134 Criticality_reject,
135 &asn_DEF_UERegisterRequest,
136 &request_out);
137
Harald Weltec3851222015-12-24 15:41:21 +0100138 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out);
Daniel Willmann4e312502015-12-09 17:59:24 +0100139
Harald Weltec3851222015-12-24 15:41:21 +0100140 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4e312502015-12-09 17:59:24 +0100141
142 return osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
Daniel Willmann479cb302015-12-09 17:54:59 +0100143}
144
Harald Weltec3851222015-12-24 15:41:21 +0100145static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmann479cb302015-12-09 17:54:59 +0100146{
147 int rc;
148 HNBRegisterAcceptIEs_t accept;
149
150 rc = hnbap_decode_hnbregisteraccepties(&accept, in);
151 if (rc < 0) {
152 }
153
154 hnb->rnc_id = accept.rnc_id;
155 printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id);
156
Daniel Willmann11e912a2016-01-07 13:19:30 +0100157 hnbap_free_hnbregisteraccepties(&accept);
Harald Weltec3851222015-12-24 15:41:21 +0100158 return 0;
Daniel Willmann479cb302015-12-09 17:54:59 +0100159}
160
Harald Weltec3851222015-12-24 15:41:21 +0100161static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in)
Daniel Willmanna7b02402015-12-09 19:05:09 +0100162{
163 int rc;
164 uint32_t ctx_id;
165 UERegisterAcceptIEs_t accept;
166 char imsi[16];
167
168 rc = hnbap_decode_ueregisteraccepties(&accept, in);
169 if (rc < 0) {
170 return rc;
171 }
172
173 if (accept.uE_Identity.present != UE_Identity_PR_iMSI) {
174 printf("Wrong type in UE register accept\n");
175 return -1;
176 }
177
178 ctx_id = asn1bitstr_to_u24(&accept.context_ID);
179
Harald Welte056984f2016-01-03 16:31:31 +0100180 ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf,
Daniel Willmanna7b02402015-12-09 19:05:09 +0100181 accept.uE_Identity.choice.iMSI.size);
182 printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id);
183
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100184 hnb->ctx_id = ctx_id;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100185 hnbap_free_ueregisteraccepties(&accept);
Daniel Willmann19dedbb2015-12-17 11:57:41 +0100186
Daniel Willmanna7b02402015-12-09 19:05:09 +0100187 return 0;
188}
189
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100190static struct msgb *gen_nas_id_resp()
191{
192 uint8_t id_resp[] = {
Neels Hofmeyr5c1cc8c2016-02-29 09:28:48 +0100193 GSM48_PDISC_MM,
194 GSM48_MT_MM_ID_RESP,
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100195 /* IMEISV */
196 0x09, /* len */
197 0x03, /* first digit (0000) + even (0) + id IMEISV (011) */
198 0x31, 0x91, 0x06, 0x00, 0x28, 0x47, 0x11, /* digits */
199 0xf2, /* filler (1111) + last digit (0010) */
200 };
201
Neels Hofmeyre1f709f2016-02-28 00:50:45 +0100202 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100203}
204
Neels Hofmeyrae937122016-02-29 09:32:00 +0100205static struct msgb *gen_nas_tmsi_realloc_compl()
206{
207 uint8_t id_resp[] = {
208 GSM48_PDISC_MM,
209 GSM48_MT_MM_TMSI_REALL_COMPL,
210 };
211
212 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
213}
214
Neels Hofmeyr26779f82016-04-18 17:04:17 +0200215static struct msgb *gen_nas_auth_resp(uint8_t *sres)
Neels Hofmeyr35888102016-03-09 01:39:56 +0100216{
217 uint8_t id_resp[] = {
218 GSM48_PDISC_MM,
Neels Hofmeyr99872602016-04-05 11:51:15 +0200219 0x80 | GSM48_MT_MM_AUTH_RESP, /* simulate sequence nr 2 */
Neels Hofmeyr35888102016-03-09 01:39:56 +0100220 0x61, 0xb5, 0x69, 0xf5 /* hardcoded SRES */
221 };
222
Neels Hofmeyr26779f82016-04-18 17:04:17 +0200223 memcpy(id_resp + 2, sres, 4);
224
Neels Hofmeyr35888102016-03-09 01:39:56 +0100225 return ranap_new_msg_dt(0, id_resp, sizeof(id_resp));
226}
227
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200228static int hnb_test_tx_dt(struct hnb_test *hnb, struct msgb *txm)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100229{
230 struct hnbtest_chan *chan;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100231 struct msgb *rua;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100232
233 chan = hnb->cs.chan;
234 if (!chan) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100235 printf("hnb_test_nas_tx_tmsi_realloc_compl(): No CS channel established yet.\n");
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100236 return -1;
237 }
238
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100239 rua = rua_new_dt(chan->is_ps, chan->conn_id, txm);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100240 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100241 return 0;
242}
243
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200244static struct tlv_parsed *parse_mm(struct gsm48_hdr *gh, int len)
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100245{
246 static struct tlv_parsed tp;
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100247 int parse_res;
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100248
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200249 len -= (const char *)&gh->data[0] - (const char *)gh;
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100250
Neels Hofmeyrd7ad0ac2016-04-05 11:52:27 +0200251 OSMO_ASSERT(gsm48_hdr_pdisc(gh) == GSM48_PDISC_MM);
252
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200253 parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], len, 0, 0);
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100254 if (parse_res <= 0) {
Neels Hofmeyr7c28f6f2016-04-05 11:49:53 +0200255 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100256 printf("Error parsing MM message 0x%hhx: %d\n", msg_type, parse_res);
257 return NULL;
258 }
259
260 return &tp;
261}
262
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200263int hnb_test_nas_rx_lu_accept(struct gsm48_hdr *gh, int len, int *sent_tmsi)
Neels Hofmeyrae937122016-02-29 09:32:00 +0100264{
265 printf(" :D Location Update Accept :D\n");
Neels Hofmeyrae937122016-02-29 09:32:00 +0100266 struct gsm48_loc_area_id *lai;
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100267
Neels Hofmeyrae937122016-02-29 09:32:00 +0100268 lai = (struct gsm48_loc_area_id *)&gh->data[0];
269
270 uint16_t mcc, mnc, lac;
271 gsm48_decode_lai(lai, &mcc, &mnc, &lac);
272 printf("LU: mcc %hd mnc %hd lac %hd\n",
273 mcc, mnc, lac);
274
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100275 struct tlv_parsed tp;
276 int parse_res;
277
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200278 len -= (const char *)&gh->data[0] - (const char *)gh;
279 parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], len, 0, 0);
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100280 if (parse_res <= 0) {
281 printf("Error parsing Location Update Accept message: %d\n", parse_res);
282 return -1;
283 }
284
285 if (TLVP_PRESENT(&tp, GSM48_IE_MOBILE_ID)) {
286 uint8_t type = TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)[0] & 0x0f;
287 if (type == GSM_MI_TYPE_TMSI)
288 *sent_tmsi = 1;
289 else *sent_tmsi = 0;
290 }
291 return 0;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100292}
293
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200294void hnb_test_nas_rx_mm_info(struct gsm48_hdr *gh, int len)
Neels Hofmeyrae937122016-02-29 09:32:00 +0100295{
296 printf(" :) MM Info :)\n");
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200297 struct tlv_parsed *tp = parse_mm(gh, len);
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100298 if (!tp)
Neels Hofmeyrae937122016-02-29 09:32:00 +0100299 return;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100300
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100301 if (TLVP_PRESENT(tp, GSM48_IE_NAME_SHORT)) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100302 char name[128] = {0};
303 gsm_7bit_decode_n(name, 127,
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100304 TLVP_VAL(tp, GSM48_IE_NAME_SHORT)+1,
305 (TLVP_LEN(tp, GSM48_IE_NAME_SHORT)-1)*8/7);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100306 printf("Info: Short Network Name: %s\n", name);
307 }
308
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100309 if (TLVP_PRESENT(tp, GSM48_IE_NAME_LONG)) {
Neels Hofmeyrae937122016-02-29 09:32:00 +0100310 char name[128] = {0};
311 gsm_7bit_decode_n(name, 127,
Neels Hofmeyrd4598fa2016-03-09 01:37:40 +0100312 TLVP_VAL(tp, GSM48_IE_NAME_LONG)+1,
313 (TLVP_LEN(tp, GSM48_IE_NAME_LONG)-1)*8/7);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100314 printf("Info: Long Network Name: %s\n", name);
315 }
Neels Hofmeyrae937122016-02-29 09:32:00 +0100316}
317
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200318static int hnb_test_nas_rx_auth_req(struct hnb_test *hnb, struct gsm48_hdr *gh,
319 int len)
Neels Hofmeyr35888102016-03-09 01:39:56 +0100320{
Neels Hofmeyr35888102016-03-09 01:39:56 +0100321 struct gsm48_auth_req *ar;
322 int parse_res;
Neels Hofmeyr35888102016-03-09 01:39:56 +0100323
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200324 len -= (const char *)&gh->data[0] - (const char *)gh;
Neels Hofmeyr35888102016-03-09 01:39:56 +0100325
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200326 if (len < sizeof(*ar)) {
Neels Hofmeyr35888102016-03-09 01:39:56 +0100327 printf("GSM48 Auth Req does not fit.\n");
328 return;
329 }
330
331 printf(" :) Authentication Request :)\n");
332
333 ar = (struct gsm48_auth_req*) &gh->data[0];
334 int seq = ar->key_seq;
Neels Hofmeyr26779f82016-04-18 17:04:17 +0200335
336 /* Generate SRES from *HARDCODED* Ki for Iuh testing */
337 struct osmo_auth_vector vec;
338 /* Ki 000102030405060708090a0b0c0d0e0f */
339 struct osmo_sub_auth_data auth = {
340 .type = OSMO_AUTH_TYPE_GSM,
341 .algo = OSMO_AUTH_ALG_COMP128v1,
342 .u.gsm.ki = {
343 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
344 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d,
345 0x0e, 0x0f
346 },
347 };
348
349 memset(&vec, 0, sizeof(vec));
350 osmo_auth_gen_vec(&vec, &auth, ar->rand);
351
352 printf("seq %d rand %s",
353 seq, osmo_hexdump(ar->rand, sizeof(ar->rand)));
354 printf(" --> sres %s\n",
355 osmo_hexdump(vec.sres, 4));
356
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200357 return hnb_test_tx_dt(hnb, gen_nas_auth_resp(vec.sres));
Neels Hofmeyr35888102016-03-09 01:39:56 +0100358}
359
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200360static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct gsm48_hdr *gh, int len)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100361{
362 struct hnbtest_chan *chan;
363
364 chan = hnb->cs.chan;
365 if (!chan) {
366 printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n");
367 return -1;
368 }
369
370 OSMO_ASSERT(!chan->is_ps);
371
Neels Hofmeyr7c28f6f2016-04-05 11:49:53 +0200372 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100373 int sent_tmsi;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100374
375 switch (msg_type) {
376 case GSM48_MT_MM_ID_REQ:
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200377 return hnb_test_tx_dt(hnb, gen_nas_id_resp());
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100378
Neels Hofmeyrae937122016-02-29 09:32:00 +0100379 case GSM48_MT_MM_LOC_UPD_ACCEPT:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200380 if (hnb_test_nas_rx_lu_accept(gh, len, &sent_tmsi))
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100381 return -1;
382 if (sent_tmsi)
Neels Hofmeyrc28ed372016-04-19 01:24:34 +0200383 return hnb_test_tx_dt(hnb, gen_nas_tmsi_realloc_compl());
Neels Hofmeyrc04eb532016-03-04 12:38:43 +0100384 else
385 return 0;
Neels Hofmeyrae937122016-02-29 09:32:00 +0100386
Neels Hofmeyr5dbb7b22016-03-09 01:38:13 +0100387 case GSM48_MT_MM_LOC_UPD_REJECT:
388 printf("Received Location Update Reject\n");
389 return 0;
390
Neels Hofmeyrae937122016-02-29 09:32:00 +0100391 case GSM48_MT_MM_INFO:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200392 hnb_test_nas_rx_mm_info(gh, len);
Neels Hofmeyrae937122016-02-29 09:32:00 +0100393 return 0;
394
Neels Hofmeyr35888102016-03-09 01:39:56 +0100395 case GSM48_MT_MM_AUTH_REQ:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200396 return hnb_test_nas_rx_auth_req(hnb, gh, len);
Neels Hofmeyr35888102016-03-09 01:39:56 +0100397
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100398 default:
Neels Hofmeyrae937122016-02-29 09:32:00 +0100399 printf("04.08 message type not handled by hnb-test: 0x%x\n",
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100400 msg_type);
401 return 0;
402 }
403
404}
405
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200406void hnb_test_nas_rx_dtap(struct hnb_test *hnb, void *data, int len)
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100407{
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200408 int rc;
409 printf("got %d bytes: %s\n", len, osmo_hexdump(data, len));
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100410
411 // nas_pdu == '05 08 12' ==> IMEI Identity request
412 // '05 04 0d' ==> LU reject
413
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200414 struct gsm48_hdr *gh = data;
415 if (len < sizeof(*gh)) {
416 printf("hnb_test_nas_rx_dtap(): NAS PDU is too short: %d. Ignoring.\n",
417 len);
418 return;
Neels Hofmeyr8c2b4ec2016-04-04 19:27:53 +0200419 }
Neels Hofmeyr7c28f6f2016-04-05 11:49:53 +0200420 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100421
422 switch (pdisc) {
423 case GSM48_PDISC_MM:
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200424 rc = hnb_test_nas_rx_mm(hnb, gh, len);
425 if (rc != 0)
426 printf("Error receiving MM message: %d\n", rc);
427 return;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100428 default:
429 printf("04.08 discriminator not handled by hnb-test: %d\n",
430 pdisc);
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200431 return;
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100432 }
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100433}
434
Neels Hofmeyr4a0a69a2016-04-19 00:06:28 +0200435void hnb_test_rx_secmode_cmd(struct hnb_test *hnb, long ip_alg)
436{
437 printf(" :) Security Mode Command :)\n");
438 /* not caring about encryption yet, just pass 0 for No Encryption. */
439 hnb_test_tx_dt(hnb, ranap_new_msg_sec_mod_compl(ip_alg, 0));
440}
441
Daniel Willmann479cb302015-12-09 17:54:59 +0100442int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg)
443{
444 HNBAP_PDU_t _pdu, *pdu = &_pdu;
445 asn_dec_rval_t dec_ret;
446 int rc;
447
448 memset(pdu, 0, sizeof(*pdu));
449 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
450 msg->data, msgb_length(msg), 0, 0);
451 if (dec_ret.code != RC_OK) {
452 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
453 return rc;
454 }
455
456 if (pdu->present != HNBAP_PDU_PR_successfulOutcome) {
457 printf("Unexpected HNBAP message received\n");
458 }
459
460 switch (pdu->choice.successfulOutcome.procedureCode) {
461 case ProcedureCode_id_HNBRegister:
462 /* Get HNB id and send UE Register request */
463 rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value);
464 break;
465 case ProcedureCode_id_UERegister:
Daniel Willmanna7b02402015-12-09 19:05:09 +0100466 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100467 break;
468 default:
469 break;
470 }
471
472 return rc;
473}
474
Neels Hofmeyrb984f362016-02-18 01:18:20 +0100475extern void direct_transfer_nas_pdu_print(ANY_t *in);
476
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100477int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg)
478{
479 RUA_RUA_PDU_t _pdu, *pdu = &_pdu;
480 asn_dec_rval_t dec_ret;
481 int rc;
482
483 memset(pdu, 0, sizeof(*pdu));
484 dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu,
485 msg->data, msgb_length(msg), 0, 0);
486 if (dec_ret.code != RC_OK) {
487 LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n");
488 return rc;
489 }
490
491 switch (pdu->present) {
492 case RUA_RUA_PDU_PR_successfulOutcome:
493 printf("RUA_RUA_PDU_PR_successfulOutcome\n");
494 break;
495 case RUA_RUA_PDU_PR_initiatingMessage:
496 printf("RUA_RUA_PDU_PR_initiatingMessage\n");
497 break;
498 case RUA_RUA_PDU_PR_NOTHING:
499 printf("RUA_RUA_PDU_PR_NOTHING\n");
500 break;
501 case RUA_RUA_PDU_PR_unsuccessfulOutcome:
502 printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n");
503 break;
504 default:
505 printf("Unexpected RUA message received\n");
506 break;
507 }
508
509 switch (pdu->choice.successfulOutcome.procedureCode) {
510 case RUA_ProcedureCode_id_ConnectionlessTransfer:
511 printf("RUA rx Connectionless Transfer\n");
512 break;
513 case RUA_ProcedureCode_id_Connect:
514 printf("RUA rx Connect\n");
515 break;
516 case RUA_ProcedureCode_id_DirectTransfer:
517 printf("RUA rx DirectTransfer\n");
Neels Hofmeyr4470f932016-04-19 00:13:53 +0200518 hnb_test_rua_dt_handle(hnb, &pdu->choice.successfulOutcome.value);
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100519 break;
520 case RUA_ProcedureCode_id_Disconnect:
521 printf("RUA rx Disconnect\n");
522 break;
523 case RUA_ProcedureCode_id_ErrorIndication:
524 printf("RUA rx ErrorIndication\n");
525 break;
526 case RUA_ProcedureCode_id_privateMessage:
527 printf("RUA rx privateMessage\n");
528 break;
529 default:
530 printf("RUA rx unknown message\n");
531 break;
532 }
533
534 return rc;
535}
536
Daniel Willmann97374c02015-12-03 09:37:58 +0100537static int hnb_read_cb(struct osmo_fd *fd)
538{
539 struct hnb_test *hnb_test = fd->data;
540 struct sctp_sndrcvinfo sinfo;
541 struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx");
542 int flags = 0;
543 int rc;
544
545 if (!msg)
546 return -ENOMEM;
547
548 rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg),
549 NULL, NULL, &sinfo, &flags);
550 if (rc < 0) {
551 LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n");
552 /* FIXME: clean up after disappeared HNB */
Daniel Willmann6637a282015-12-17 14:47:51 +0100553 close(fd->fd);
554 osmo_fd_unregister(fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100555 return rc;
Daniel Willmann6637a282015-12-17 14:47:51 +0100556 } else if (rc == 0) {
557 LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n");
558 close(fd->fd);
559 osmo_fd_unregister(fd);
560 fd->fd = -1;
561
562 return -1;
563 } else {
Daniel Willmann97374c02015-12-03 09:37:58 +0100564 msgb_put(msg, rc);
Daniel Willmann6637a282015-12-17 14:47:51 +0100565 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100566
567 if (flags & MSG_NOTIFICATION) {
Daniel Willmann32797802015-12-17 12:53:05 +0100568 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100569 msgb_free(msg);
570 return 0;
571 }
572
573 sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid);
574
575 switch (sinfo.sinfo_ppid) {
576 case IUH_PPI_HNBAP:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100577 printf("HNBAP message received\n");
Daniel Willmann479cb302015-12-09 17:54:59 +0100578 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100579 break;
580 case IUH_PPI_RUA:
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100581 printf("RUA message received\n");
582 rc = hnb_test_rua_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100583 break;
584 case IUH_PPI_SABP:
585 case IUH_PPI_RNA:
586 case IUH_PPI_PUA:
587 LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n",
588 sinfo.sinfo_ppid);
589 rc = 0;
590 break;
591 default:
592 LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n",
593 sinfo.sinfo_ppid);
594 rc = 0;
595 break;
596 }
597
598 msgb_free(msg);
599 return rc;
600}
601
602static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg)
603{
604 struct hnb_test *ctx = fd->data;
605 struct sctp_sndrcvinfo sinfo = {
Harald Weltec3851222015-12-24 15:41:21 +0100606 .sinfo_ppid = htonl(msgb_sctp_ppid(msg)),
Daniel Willmann97374c02015-12-03 09:37:58 +0100607 .sinfo_stream = 0,
608 };
609 int rc;
610
Neels Hofmeyre25faa82016-03-04 02:49:52 +0100611 printf("Sending: %s\n", osmo_hexdump(msgb_data(msg), msgb_length(msg)));
Daniel Willmann97374c02015-12-03 09:37:58 +0100612 rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg),
613 &sinfo, 0);
614 /* we don't need to msgb_free(), write_queue does this for us */
615 return rc;
616}
617
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100618static void hnb_send_register_req(struct hnb_test *hnb_test)
619{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100620 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100621 struct msgb *msg;
622 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100623 uint16_t lac, sac;
624 uint8_t rac;
625 uint32_t cid;
626 uint8_t plmn[] = {0x09, 0xf1, 0x99};
627 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100628
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100629 HNBRegisterRequestIEs_t request;
630 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100631
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100632 lac = 0xc0fe;
633 sac = 0xabab;
634 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100635 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100636
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100637 asn1_u16_to_str(&request.lac, &lac, lac);
638 asn1_u16_to_str(&request.sac, &sac, sac);
639 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100640 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100641
642 request.hnB_Identity.hNB_Identity_Info.buf = identity;
643 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
644
645 request.plmNidentity.buf = plmn;
646 request.plmNidentity.size = 3;
647
648
649
650 memset(&request_out, 0, sizeof(request_out));
651 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
652 if (rc < 0) {
653 printf("Could not encode HNB register request IEs\n");
654 }
655
656 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
657 Criticality_reject,
658 &asn_DEF_HNBRegisterRequest,
659 &request_out);
660
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100661
Harald Weltec3851222015-12-24 15:41:21 +0100662 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
663
664 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
665}
666
667static void hnb_send_deregister_req(struct hnb_test *hnb_test)
668{
669 struct msgb *msg;
670 int rc;
671
672 HNBDe_RegisterIEs_t request;
673 memset(&request, 0, sizeof(request));
674
675 request.cause.present = Cause_PR_misc;
676 request.cause.choice.misc = CauseMisc_o_and_m_intervention;
677
678 HNBDe_Register_t request_out;
679 memset(&request_out, 0, sizeof(request_out));
680 rc = hnbap_encode_hnbde_registeries(&request_out, &request);
681 if (rc < 0) {
682 printf("Could not encode HNB deregister request IEs\n");
683 }
684
685 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register,
686 Criticality_reject,
687 &asn_DEF_HNBDe_Register,
688 &request_out);
689
690 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100691
692 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
693}
694
695
Daniel Willmann97374c02015-12-03 09:37:58 +0100696static const struct log_info_cat log_cat[] = {
697 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100698 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100699 .color = "",
700 .description = "Main program",
701 },
Daniel Willmann32797802015-12-17 12:53:05 +0100702 [DHNBAP] = {
703 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
704 .color = "",
705 .description = "Home Node B Application Part",
706 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100707};
708
709static const struct log_info hnb_test_log_info = {
710 .cat = log_cat,
711 .num_cat = ARRAY_SIZE(log_cat),
712};
713
714static struct vty_app_info vty_info = {
715 .name = "OsmoHNB-Test",
716 .version = "0",
717};
718
Daniel Willmann4abdee02015-12-09 17:57:32 +0100719static int sctp_sock_init(int fd)
720{
721 struct sctp_event_subscribe event;
722 int rc;
723
724 /* subscribe for all events */
725 memset((uint8_t *)&event, 1, sizeof(event));
726 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
727 &event, sizeof(event));
728
729 return rc;
730}
731
Harald Weltec3851222015-12-24 15:41:21 +0100732#define HNBAP_STR "HNBAP related commands\n"
733#define HNB_STR "HomeNodeB commands\n"
734#define UE_STR "User Equipment commands\n"
735#define RANAP_STR "RANAP related commands\n"
736#define CSPS_STR "Circuit Switched\n" "Packet Switched\n"
737
738DEFUN(hnb_register, hnb_register_cmd,
739 "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST")
740{
741 hnb_send_register_req(&g_hnb_test);
742
743 return CMD_SUCCESS;
744}
745
746DEFUN(hnb_deregister, hnb_deregister_cmd,
747 "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST")
748{
749 hnb_send_deregister_req(&g_hnb_test);
750
751 return CMD_SUCCESS;
752}
753
754DEFUN(ue_register, ue_register_cmd,
755 "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST")
756{
757 hnb_test_ue_register_tx(&g_hnb_test, argv[0]);
758
759 return CMD_SUCCESS;
760}
761
762DEFUN(asn_dbg, asn_dbg_cmd,
763 "asn-debug (1|0)", "Enable or disabel libasn1c debugging")
764{
765 asn_debug = atoi(argv[0]);
766
767 return CMD_SUCCESS;
768}
769
770DEFUN(ranap_reset, ranap_reset_cmd,
771 "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR)
772{
773 int is_ps = 0;
774 struct msgb *msg, *rua;
775
776 RANAP_Cause_t cause = {
777 .present = RANAP_Cause_PR_transmissionNetwork,
778 .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
779 };
780
781 if (!strcmp(argv[0], "ps"))
782 is_ps = 1;
783
784 msg = ranap_new_msg_reset(is_ps, &cause);
785 rua = rua_new_udt(msg);
786 //msgb_free(msg);
787 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
788
789 return CMD_SUCCESS;
790}
791
792
793enum my_vty_nodes {
794 CHAN_NODE = _LAST_OSMOVTY_NODE,
795};
796
797static struct cmd_node chan_node = {
798 CHAN_NODE,
799 "%s(chan)> ",
800 1,
801};
802
803
Harald Weltec3851222015-12-24 15:41:21 +0100804static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi)
805{
Neels Hofmeyr5c1cc8c2016-02-29 09:28:48 +0100806 uint8_t lu[] = { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST,
807 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57,
Neels Hofmeyr32828702016-01-14 13:06:47 +0100808 /* len, IMSI/type, IMSI-------------------------------- */
Harald Weltec3851222015-12-24 15:41:21 +0100809 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98,
810 0x33, 0x03, 0x57, 0x18 , 0xb2 };
811 uint8_t plmn_id[] = { 0x09, 0x01, 0x99 };
812 RANAP_GlobalRNC_ID_t rnc_id = {
813 .rNC_ID = 23,
814 .pLMNidentity.buf = plmn_id,
815 .pLMNidentity.size = sizeof(plmn_id),
816 };
Harald Weltec3851222015-12-24 15:41:21 +0100817
818 /* FIXME: patch imsi */
Neels Hofmeyr7b811282016-01-14 13:05:24 +0100819 /* Note: the Mobile Identitiy IE's IMSI data has the identity type and
820 * an even/odd indicator bit encoded in the first octet. So the first
821 * octet looks like this:
822 *
823 * 8 7 6 5 | 4 | 3 2 1
824 * IMSI-digit | even/odd | type
825 *
826 * followed by the remaining IMSI digits.
827 * If digit count is even (bit 4 == 0), that first high-nibble is 0xf.
828 * (derived from Iu pcap Location Update Request msg and TS 25.413)
829 *
830 * TODO I'm only 90% sure about this
831 */
Harald Weltec3851222015-12-24 15:41:21 +0100832
Neels Hofmeyr6a62e542016-01-15 03:07:45 +0100833 return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu));
Harald Weltec3851222015-12-24 15:41:21 +0100834}
835
836DEFUN(chan, chan_cmd,
837 "channel (cs|ps) lu imsi IMSI",
838 "Open a new Signalling Connection\n"
839 "To Circuit-Switched CN\n"
840 "To Packet-Switched CN\n"
841 "Performing a Location Update\n"
842 )
843{
844 struct hnbtest_chan *chan;
845 struct msgb *msg, *rua;
Daniel Willmann85927162016-01-14 15:36:49 +0100846 static uint16_t conn_id = 42;
Harald Weltec3851222015-12-24 15:41:21 +0100847
848 chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan);
849 if (!strcmp(argv[0], "ps"))
850 chan->is_ps = 1;
851 chan->imsi = talloc_strdup(chan, argv[1]);
Daniel Willmann85927162016-01-14 15:36:49 +0100852 chan->conn_id = conn_id;
853 conn_id++;
Harald Weltec3851222015-12-24 15:41:21 +0100854
855 msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi);
856 rua = rua_new_conn(chan->is_ps, chan->conn_id, msg);
857
858 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
859
860 vty->index = chan;
861 vty->node = CHAN_NODE;
862
Neels Hofmeyr860a1292016-02-18 23:03:15 +0100863 if (!chan->is_ps)
864 g_hnb_test.cs.chan = chan;
865
866
Harald Weltec3851222015-12-24 15:41:21 +0100867 return CMD_SUCCESS;
868}
869
870static void hnbtest_vty_init(void)
871{
872 install_element_ve(&asn_dbg_cmd);
873 install_element_ve(&hnb_register_cmd);
874 install_element_ve(&hnb_deregister_cmd);
875 install_element_ve(&ue_register_cmd);
876 install_element_ve(&ranap_reset_cmd);
877 install_element_ve(&chan_cmd);
878
879 install_node(&chan_node, NULL);
880 vty_install_default(CHAN_NODE);
881}
882
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100883static void handle_options(int argc, char **argv)
884{
885 while (1) {
886 int idx = 0, c;
887 static const struct option long_options[] = {
888 { "ues", 1, 0, 'u' },
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100889 { "gw-addr", 1, 0, 'g' },
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100890 { 0, 0, 0, 0 },
891 };
892
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100893 c = getopt_long(argc, argv, "u:g:", long_options, &idx);
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100894
895 if (c == -1)
896 break;
897
898 switch (c) {
899 case 'u':
900 g_hnb_test.ues = atoi(optarg);
901 break;
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100902 case 'g':
903 g_hnb_test.gw_addr = optarg;
904 break;
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100905 }
906 }
907}
908
Harald Weltec3851222015-12-24 15:41:21 +0100909int main(int argc, char **argv)
Daniel Willmann97374c02015-12-03 09:37:58 +0100910{
911 int rc;
912
Harald Welte87ffeb92015-12-25 15:34:22 +0100913 test_common_init();
Daniel Willmann97374c02015-12-03 09:37:58 +0100914
Harald Welte87ffeb92015-12-25 15:34:22 +0100915 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
Daniel Willmann97374c02015-12-03 09:37:58 +0100916
917 vty_init(&vty_info);
Harald Weltec3851222015-12-24 15:41:21 +0100918 hnbtest_vty_init();
919
Neels Hofmeyra0d21472016-02-24 20:50:31 +0100920 printf("VTY at %s %d\n", vty_get_bind_addr(), 2324);
921 rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324);
Harald Weltec3851222015-12-24 15:41:21 +0100922 if (rc < 0) {
923 perror("Error binding VTY port");
924 exit(1);
925 }
Daniel Willmann97374c02015-12-03 09:37:58 +0100926
Daniel Willmann141a0ba2015-12-17 18:03:52 +0100927 handle_options(argc, argv);
928
Daniel Willmann97374c02015-12-03 09:37:58 +0100929 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
930 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
931 g_hnb_test.wqueue.read_cb = hnb_read_cb;
932 g_hnb_test.wqueue.write_cb = hnb_write_cb;
933
934 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
Neels Hofmeyr5f9be1e2016-02-29 13:33:44 +0100935 IPPROTO_SCTP, g_hnb_test.gw_addr,
Daniel Willmann97374c02015-12-03 09:37:58 +0100936 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
937 if (rc < 0) {
938 perror("Error connecting to Iuh port");
939 exit(1);
940 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100941 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100942
Harald Weltec3851222015-12-24 15:41:21 +0100943#if 0
944 /* some hard-coded message generation. Doesn't make sense from
945 * a protocol point of view but enables to look at the encoded
946 * results in wireshark for manual verification */
947 {
948 struct msgb *msg, *rua;
949 const uint8_t nas[] = { 0, 1, 2, 3 };
950 const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
951
952 msg = ranap_new_msg_dt(0, nas, sizeof(nas));
953 rua = rua_new_udt(msg);
954 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
955
956 msg = ranap_new_msg_sec_mod_cmd(ik, ik);
957 rua = rua_new_udt(msg);
958 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
959
960 msg = ranap_new_msg_iu_rel_cmd()
961 rua = rua_new_udt(msg);
962 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
963
964 msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0);
965 rua = rua_new_udt(msg);
966 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
967
968 msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020);
969 rua = rua_new_udt(msg);
970 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
971
972 msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344);
973 rua = rua_new_udt(msg);
974 osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua);
975 }
976#endif
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100977
Daniel Willmann97374c02015-12-03 09:37:58 +0100978 while (1) {
979 rc = osmo_select_main(0);
980 if (rc < 0)
981 exit(3);
982 }
983
984 /* not reached */
985 exit(0);
986}