Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 1 | /* 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 Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 43 | #include <osmocom/netif/stream.h> |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 44 | #include <osmocom/gsm/tlv.h> |
| 45 | #include <osmocom/gsm/gsm48.h> |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 46 | |
| 47 | #include <osmocom/vty/telnet_interface.h> |
| 48 | #include <osmocom/vty/logging.h> |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 49 | #include <osmocom/vty/command.h> |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 50 | |
| 51 | #include "hnb-test.h" |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 52 | #include "hnbap_common.h" |
| 53 | #include "hnbap_ies_defs.h" |
Harald Welte | b66c5d0 | 2016-01-03 18:04:28 +0100 | [diff] [blame] | 54 | #include "rua_msg_factory.h" |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 55 | #include "asn1helpers.h" |
Neels Hofmeyr | 96979af | 2016-01-05 15:19:44 +0100 | [diff] [blame] | 56 | #include <osmocom/ranap/iu_helpers.h> |
Harald Welte | 87ffeb9 | 2015-12-25 15:34:22 +0100 | [diff] [blame] | 57 | #include "test_common.h" |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 58 | |
Neels Hofmeyr | 96979af | 2016-01-05 15:19:44 +0100 | [diff] [blame] | 59 | #include <osmocom/ranap/ranap_msg_factory.h> |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 60 | |
Neels Hofmeyr | 0968a58 | 2016-01-11 15:19:38 +0100 | [diff] [blame] | 61 | #include <osmocom/rua/RUA_RUA-PDU.h> |
| 62 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 63 | #include <osmocom/gsm/protocol/gsm_04_08.h> |
| 64 | |
| 65 | #include <osmocom/ranap/RANAP_ProcedureCode.h> |
| 66 | #include <osmocom/ranap/RANAP_Criticality.h> |
| 67 | #include <osmocom/ranap/RANAP_DirectTransfer.h> |
| 68 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 69 | static void *tall_hnb_ctx; |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 70 | |
| 71 | struct hnb_test g_hnb_test = { |
Neels Hofmeyr | 5f9be1e | 2016-02-29 13:33:44 +0100 | [diff] [blame] | 72 | .gw_addr = "127.0.0.1", |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 73 | .gw_port = IUH_DEFAULT_SCTP_PORT, |
| 74 | }; |
| 75 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 76 | struct msgb *rua_new_udt(struct msgb *inmsg); |
| 77 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 78 | static int hnb_test_ue_de_register_tx(struct hnb_test *hnb_test) |
Daniel Willmann | 19dedbb | 2015-12-17 11:57:41 +0100 | [diff] [blame] | 79 | { |
| 80 | struct msgb *msg; |
| 81 | int rc, imsi_len; |
| 82 | uint32_t ctx_id; |
| 83 | |
| 84 | UEDe_Register_t dereg; |
| 85 | UEDe_RegisterIEs_t dereg_ies; |
| 86 | memset(&dereg_ies, 0, sizeof(dereg_ies)); |
| 87 | |
| 88 | asn1_u24_to_bitstring(&dereg_ies.context_ID, &ctx_id, hnb_test->ctx_id); |
| 89 | dereg_ies.cause.present = Cause_PR_radioNetwork; |
| 90 | dereg_ies.cause.choice.radioNetwork = CauseRadioNetwork_connection_with_UE_lost; |
| 91 | |
| 92 | memset(&dereg, 0, sizeof(dereg)); |
| 93 | rc = hnbap_encode_uede_registeries(&dereg, &dereg_ies); |
| 94 | |
| 95 | msg = hnbap_generate_initiating_message(ProcedureCode_id_UEDe_Register, |
| 96 | Criticality_ignore, |
| 97 | &asn_DEF_UEDe_Register, |
| 98 | &dereg); |
| 99 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 100 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UEDe_Register, &dereg); |
Daniel Willmann | 19dedbb | 2015-12-17 11:57:41 +0100 | [diff] [blame] | 101 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 102 | msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; |
Daniel Willmann | 19dedbb | 2015-12-17 11:57:41 +0100 | [diff] [blame] | 103 | |
| 104 | return osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
| 105 | } |
| 106 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 107 | static int hnb_test_ue_register_tx(struct hnb_test *hnb_test, const char *imsi_str) |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 108 | { |
Daniel Willmann | 4e31250 | 2015-12-09 17:59:24 +0100 | [diff] [blame] | 109 | struct msgb *msg; |
| 110 | int rc, imsi_len; |
| 111 | |
| 112 | char imsi_buf[16]; |
Daniel Willmann | 141a0ba | 2015-12-17 18:03:52 +0100 | [diff] [blame] | 113 | |
Daniel Willmann | 4e31250 | 2015-12-09 17:59:24 +0100 | [diff] [blame] | 114 | UERegisterRequest_t request_out; |
| 115 | UERegisterRequestIEs_t request; |
| 116 | memset(&request, 0, sizeof(request)); |
| 117 | |
| 118 | request.uE_Identity.present = UE_Identity_PR_iMSI; |
| 119 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 120 | imsi_len = ranap_imsi_encode(imsi_buf, sizeof(imsi_buf), imsi_str); |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 121 | OCTET_STRING_fromBuf(&request.uE_Identity.choice.iMSI, imsi_buf, imsi_len); |
Daniel Willmann | 4e31250 | 2015-12-09 17:59:24 +0100 | [diff] [blame] | 122 | |
| 123 | request.registration_Cause = Registration_Cause_normal; |
| 124 | request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6; |
| 125 | request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable; |
| 126 | |
| 127 | memset(&request_out, 0, sizeof(request_out)); |
| 128 | rc = hnbap_encode_ueregisterrequesties(&request_out, &request); |
| 129 | |
| 130 | msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister, |
| 131 | Criticality_reject, |
| 132 | &asn_DEF_UERegisterRequest, |
| 133 | &request_out); |
| 134 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 135 | ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterRequest, &request_out); |
Daniel Willmann | 4e31250 | 2015-12-09 17:59:24 +0100 | [diff] [blame] | 136 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 137 | msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; |
Daniel Willmann | 4e31250 | 2015-12-09 17:59:24 +0100 | [diff] [blame] | 138 | |
| 139 | return osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 142 | static int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in) |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 143 | { |
| 144 | int rc; |
| 145 | HNBRegisterAcceptIEs_t accept; |
| 146 | |
| 147 | rc = hnbap_decode_hnbregisteraccepties(&accept, in); |
| 148 | if (rc < 0) { |
| 149 | } |
| 150 | |
| 151 | hnb->rnc_id = accept.rnc_id; |
| 152 | printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id); |
| 153 | |
Daniel Willmann | 11e912a | 2016-01-07 13:19:30 +0100 | [diff] [blame] | 154 | hnbap_free_hnbregisteraccepties(&accept); |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 155 | return 0; |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 156 | } |
| 157 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 158 | static int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in) |
Daniel Willmann | a7b0240 | 2015-12-09 19:05:09 +0100 | [diff] [blame] | 159 | { |
| 160 | int rc; |
| 161 | uint32_t ctx_id; |
| 162 | UERegisterAcceptIEs_t accept; |
| 163 | char imsi[16]; |
| 164 | |
| 165 | rc = hnbap_decode_ueregisteraccepties(&accept, in); |
| 166 | if (rc < 0) { |
| 167 | return rc; |
| 168 | } |
| 169 | |
| 170 | if (accept.uE_Identity.present != UE_Identity_PR_iMSI) { |
| 171 | printf("Wrong type in UE register accept\n"); |
| 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | ctx_id = asn1bitstr_to_u24(&accept.context_ID); |
| 176 | |
Harald Welte | 056984f | 2016-01-03 16:31:31 +0100 | [diff] [blame] | 177 | ranap_bcd_decode(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf, |
Daniel Willmann | a7b0240 | 2015-12-09 19:05:09 +0100 | [diff] [blame] | 178 | accept.uE_Identity.choice.iMSI.size); |
| 179 | printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id); |
| 180 | |
Daniel Willmann | 19dedbb | 2015-12-17 11:57:41 +0100 | [diff] [blame] | 181 | hnb->ctx_id = ctx_id; |
Daniel Willmann | 11e912a | 2016-01-07 13:19:30 +0100 | [diff] [blame] | 182 | hnbap_free_ueregisteraccepties(&accept); |
Daniel Willmann | 19dedbb | 2015-12-17 11:57:41 +0100 | [diff] [blame] | 183 | |
Daniel Willmann | a7b0240 | 2015-12-09 19:05:09 +0100 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 187 | static struct msgb *gen_nas_id_resp() |
| 188 | { |
| 189 | uint8_t id_resp[] = { |
Neels Hofmeyr | 5c1cc8c | 2016-02-29 09:28:48 +0100 | [diff] [blame] | 190 | GSM48_PDISC_MM, |
| 191 | GSM48_MT_MM_ID_RESP, |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 192 | /* IMEISV */ |
| 193 | 0x09, /* len */ |
| 194 | 0x03, /* first digit (0000) + even (0) + id IMEISV (011) */ |
| 195 | 0x31, 0x91, 0x06, 0x00, 0x28, 0x47, 0x11, /* digits */ |
| 196 | 0xf2, /* filler (1111) + last digit (0010) */ |
| 197 | }; |
| 198 | |
Neels Hofmeyr | e1f709f | 2016-02-28 00:50:45 +0100 | [diff] [blame] | 199 | return ranap_new_msg_dt(0, id_resp, sizeof(id_resp)); |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 202 | static struct msgb *gen_nas_tmsi_realloc_compl() |
| 203 | { |
| 204 | uint8_t id_resp[] = { |
| 205 | GSM48_PDISC_MM, |
| 206 | GSM48_MT_MM_TMSI_REALL_COMPL, |
| 207 | }; |
| 208 | |
| 209 | return ranap_new_msg_dt(0, id_resp, sizeof(id_resp)); |
| 210 | } |
| 211 | |
| 212 | static int hnb_test_nas_tx_dt(struct hnb_test *hnb, struct msgb *txm) |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 213 | { |
| 214 | struct hnbtest_chan *chan; |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 215 | struct msgb *rua; |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 216 | |
| 217 | chan = hnb->cs.chan; |
| 218 | if (!chan) { |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 219 | printf("hnb_test_nas_tx_tmsi_realloc_compl(): No CS channel established yet.\n"); |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 220 | return -1; |
| 221 | } |
| 222 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 223 | rua = rua_new_dt(chan->is_ps, chan->conn_id, txm); |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 224 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 228 | void hnb_test_nas_rx_lu_accept(struct msgb *rxm) |
| 229 | { |
| 230 | printf(" :D Location Update Accept :D\n"); |
| 231 | struct gsm48_hdr *gh; |
| 232 | struct gsm48_loc_area_id *lai; |
| 233 | gh = (struct gsm48_hdr *)msgb_l3(rxm); |
| 234 | lai = (struct gsm48_loc_area_id *)&gh->data[0]; |
| 235 | |
| 236 | uint16_t mcc, mnc, lac; |
| 237 | gsm48_decode_lai(lai, &mcc, &mnc, &lac); |
| 238 | printf("LU: mcc %hd mnc %hd lac %hd\n", |
| 239 | mcc, mnc, lac); |
| 240 | |
| 241 | } |
| 242 | |
| 243 | void hnb_test_nas_rx_mm_info(struct msgb *rxm) |
| 244 | { |
| 245 | printf(" :) MM Info :)\n"); |
| 246 | struct gsm48_hdr *gh; |
| 247 | struct tlv_parsed tp; |
| 248 | int parse_res; |
| 249 | int length = msgb_l3len(rxm); |
| 250 | |
| 251 | if (length < sizeof(*gh)) { |
| 252 | printf("GSM48 header does not fit.\n"); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | gh = (struct gsm48_hdr *) msgb_l3(rxm); |
| 257 | length -= (const char *)&gh->data[0] - (const char *)gh; |
| 258 | |
| 259 | parse_res = tlv_parse(&tp, &gsm48_mm_att_tlvdef, &gh->data[0], length, 0, 0); |
| 260 | if (parse_res <= 0) { |
| 261 | printf("Error parsing MM Info message: %d\n", parse_res); |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | if (TLVP_PRESENT(&tp, GSM48_IE_NAME_SHORT)) { |
| 266 | char name[128] = {0}; |
| 267 | gsm_7bit_decode_n(name, 127, |
| 268 | TLVP_VAL(&tp, GSM48_IE_NAME_SHORT)+1, |
| 269 | (TLVP_LEN(&tp, GSM48_IE_NAME_SHORT)-1)*8/7); |
| 270 | printf("Info: Short Network Name: %s\n", name); |
| 271 | } |
| 272 | |
| 273 | if (TLVP_PRESENT(&tp, GSM48_IE_NAME_LONG)) { |
| 274 | char name[128] = {0}; |
| 275 | gsm_7bit_decode_n(name, 127, |
| 276 | TLVP_VAL(&tp, GSM48_IE_NAME_LONG)+1, |
| 277 | (TLVP_LEN(&tp, GSM48_IE_NAME_LONG)-1)*8/7); |
| 278 | printf("Info: Long Network Name: %s\n", name); |
| 279 | } |
| 280 | |
| 281 | return; |
| 282 | } |
| 283 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 284 | static int hnb_test_nas_rx_mm(struct hnb_test *hnb, struct msgb *rxm) |
| 285 | { |
| 286 | struct hnbtest_chan *chan; |
| 287 | |
| 288 | chan = hnb->cs.chan; |
| 289 | if (!chan) { |
| 290 | printf("hnb_test_nas_rx_mm(): No CS channel established yet.\n"); |
| 291 | return -1; |
| 292 | } |
| 293 | |
| 294 | OSMO_ASSERT(!chan->is_ps); |
| 295 | |
| 296 | struct gsm48_hdr *gh = msgb_l3(rxm); |
| 297 | uint8_t msg_type = gh->msg_type & 0xbf; |
| 298 | |
| 299 | switch (msg_type) { |
| 300 | case GSM48_MT_MM_ID_REQ: |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 301 | return hnb_test_nas_tx_dt(hnb, gen_nas_id_resp()); |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 302 | |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 303 | case GSM48_MT_MM_LOC_UPD_ACCEPT: |
| 304 | hnb_test_nas_rx_lu_accept(rxm); |
| 305 | return hnb_test_nas_tx_dt(hnb, gen_nas_tmsi_realloc_compl()); |
| 306 | |
| 307 | case GSM48_MT_MM_INFO: |
| 308 | hnb_test_nas_rx_mm_info(rxm); |
| 309 | return 0; |
| 310 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 311 | default: |
Neels Hofmeyr | ae93712 | 2016-02-29 09:32:00 +0100 | [diff] [blame] | 312 | printf("04.08 message type not handled by hnb-test: 0x%x\n", |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 313 | msg_type); |
| 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | } |
| 318 | |
| 319 | static int hnb_test_nas_rx_dtap(struct hnb_test *hnb, struct msgb *msg) |
| 320 | { |
| 321 | printf("got %s\n", osmo_hexdump(msg->data, msg->len)); |
| 322 | |
| 323 | // nas_pdu == '05 08 12' ==> IMEI Identity request |
| 324 | // '05 04 0d' ==> LU reject |
| 325 | |
| 326 | struct gsm48_hdr *gh = msgb_l3(msg); |
| 327 | uint8_t pdisc = gh->proto_discr & 0x0f; |
| 328 | |
| 329 | switch (pdisc) { |
| 330 | case GSM48_PDISC_MM: |
| 331 | return hnb_test_nas_rx_mm(hnb, msg); |
| 332 | default: |
| 333 | printf("04.08 discriminator not handled by hnb-test: %d\n", |
| 334 | pdisc); |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | } |
| 340 | |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 341 | int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg) |
| 342 | { |
| 343 | HNBAP_PDU_t _pdu, *pdu = &_pdu; |
| 344 | asn_dec_rval_t dec_ret; |
| 345 | int rc; |
| 346 | |
| 347 | memset(pdu, 0, sizeof(*pdu)); |
| 348 | dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu, |
| 349 | msg->data, msgb_length(msg), 0, 0); |
| 350 | if (dec_ret.code != RC_OK) { |
| 351 | LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n"); |
| 352 | return rc; |
| 353 | } |
| 354 | |
| 355 | if (pdu->present != HNBAP_PDU_PR_successfulOutcome) { |
| 356 | printf("Unexpected HNBAP message received\n"); |
| 357 | } |
| 358 | |
| 359 | switch (pdu->choice.successfulOutcome.procedureCode) { |
| 360 | case ProcedureCode_id_HNBRegister: |
| 361 | /* Get HNB id and send UE Register request */ |
| 362 | rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value); |
| 363 | break; |
| 364 | case ProcedureCode_id_UERegister: |
Daniel Willmann | a7b0240 | 2015-12-09 19:05:09 +0100 | [diff] [blame] | 365 | rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value); |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 366 | break; |
| 367 | default: |
| 368 | break; |
| 369 | } |
| 370 | |
| 371 | return rc; |
| 372 | } |
| 373 | |
Neels Hofmeyr | b984f36 | 2016-02-18 01:18:20 +0100 | [diff] [blame] | 374 | extern void direct_transfer_nas_pdu_print(ANY_t *in); |
| 375 | |
Neels Hofmeyr | 0968a58 | 2016-01-11 15:19:38 +0100 | [diff] [blame] | 376 | int hnb_test_rua_rx(struct hnb_test *hnb, struct msgb *msg) |
| 377 | { |
| 378 | RUA_RUA_PDU_t _pdu, *pdu = &_pdu; |
| 379 | asn_dec_rval_t dec_ret; |
| 380 | int rc; |
| 381 | |
| 382 | memset(pdu, 0, sizeof(*pdu)); |
| 383 | dec_ret = aper_decode(NULL, &asn_DEF_RUA_RUA_PDU, (void **) &pdu, |
| 384 | msg->data, msgb_length(msg), 0, 0); |
| 385 | if (dec_ret.code != RC_OK) { |
| 386 | LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n"); |
| 387 | return rc; |
| 388 | } |
| 389 | |
| 390 | switch (pdu->present) { |
| 391 | case RUA_RUA_PDU_PR_successfulOutcome: |
| 392 | printf("RUA_RUA_PDU_PR_successfulOutcome\n"); |
| 393 | break; |
| 394 | case RUA_RUA_PDU_PR_initiatingMessage: |
| 395 | printf("RUA_RUA_PDU_PR_initiatingMessage\n"); |
| 396 | break; |
| 397 | case RUA_RUA_PDU_PR_NOTHING: |
| 398 | printf("RUA_RUA_PDU_PR_NOTHING\n"); |
| 399 | break; |
| 400 | case RUA_RUA_PDU_PR_unsuccessfulOutcome: |
| 401 | printf("RUA_RUA_PDU_PR_unsuccessfulOutcome\n"); |
| 402 | break; |
| 403 | default: |
| 404 | printf("Unexpected RUA message received\n"); |
| 405 | break; |
| 406 | } |
| 407 | |
| 408 | switch (pdu->choice.successfulOutcome.procedureCode) { |
| 409 | case RUA_ProcedureCode_id_ConnectionlessTransfer: |
| 410 | printf("RUA rx Connectionless Transfer\n"); |
| 411 | break; |
| 412 | case RUA_ProcedureCode_id_Connect: |
| 413 | printf("RUA rx Connect\n"); |
| 414 | break; |
| 415 | case RUA_ProcedureCode_id_DirectTransfer: |
| 416 | printf("RUA rx DirectTransfer\n"); |
Neels Hofmeyr | b984f36 | 2016-02-18 01:18:20 +0100 | [diff] [blame] | 417 | { |
| 418 | struct msgb *m = msgb_alloc(1500, "direct_transfer_nas_pdu"); |
| 419 | direct_transfer_nas_pdu_get(&pdu->choice.successfulOutcome.value, m); |
| 420 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 421 | hnb_test_nas_rx_dtap(hnb, m); |
Neels Hofmeyr | b984f36 | 2016-02-18 01:18:20 +0100 | [diff] [blame] | 422 | |
Neels Hofmeyr | b984f36 | 2016-02-18 01:18:20 +0100 | [diff] [blame] | 423 | msgb_free(m); |
| 424 | } |
Neels Hofmeyr | 0968a58 | 2016-01-11 15:19:38 +0100 | [diff] [blame] | 425 | break; |
| 426 | case RUA_ProcedureCode_id_Disconnect: |
| 427 | printf("RUA rx Disconnect\n"); |
| 428 | break; |
| 429 | case RUA_ProcedureCode_id_ErrorIndication: |
| 430 | printf("RUA rx ErrorIndication\n"); |
| 431 | break; |
| 432 | case RUA_ProcedureCode_id_privateMessage: |
| 433 | printf("RUA rx privateMessage\n"); |
| 434 | break; |
| 435 | default: |
| 436 | printf("RUA rx unknown message\n"); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | return rc; |
| 441 | } |
| 442 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 443 | static int hnb_read_cb(struct osmo_fd *fd) |
| 444 | { |
| 445 | struct hnb_test *hnb_test = fd->data; |
| 446 | struct sctp_sndrcvinfo sinfo; |
| 447 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
| 448 | int flags = 0; |
| 449 | int rc; |
| 450 | |
| 451 | if (!msg) |
| 452 | return -ENOMEM; |
| 453 | |
| 454 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 455 | NULL, NULL, &sinfo, &flags); |
| 456 | if (rc < 0) { |
| 457 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
| 458 | /* FIXME: clean up after disappeared HNB */ |
Daniel Willmann | 6637a28 | 2015-12-17 14:47:51 +0100 | [diff] [blame] | 459 | close(fd->fd); |
| 460 | osmo_fd_unregister(fd); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 461 | return rc; |
Daniel Willmann | 6637a28 | 2015-12-17 14:47:51 +0100 | [diff] [blame] | 462 | } else if (rc == 0) { |
| 463 | LOGP(DMAIN, LOGL_INFO, "Connection to HNB closed\n"); |
| 464 | close(fd->fd); |
| 465 | osmo_fd_unregister(fd); |
| 466 | fd->fd = -1; |
| 467 | |
| 468 | return -1; |
| 469 | } else { |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 470 | msgb_put(msg, rc); |
Daniel Willmann | 6637a28 | 2015-12-17 14:47:51 +0100 | [diff] [blame] | 471 | } |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 472 | |
| 473 | if (flags & MSG_NOTIFICATION) { |
Daniel Willmann | 3279780 | 2015-12-17 12:53:05 +0100 | [diff] [blame] | 474 | LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n"); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 475 | msgb_free(msg); |
| 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 480 | |
| 481 | switch (sinfo.sinfo_ppid) { |
| 482 | case IUH_PPI_HNBAP: |
Neels Hofmeyr | 0968a58 | 2016-01-11 15:19:38 +0100 | [diff] [blame] | 483 | printf("HNBAP message received\n"); |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 484 | rc = hnb_test_hnbap_rx(hnb_test, msg); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 485 | break; |
| 486 | case IUH_PPI_RUA: |
Neels Hofmeyr | 0968a58 | 2016-01-11 15:19:38 +0100 | [diff] [blame] | 487 | printf("RUA message received\n"); |
| 488 | rc = hnb_test_rua_rx(hnb_test, msg); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 489 | break; |
| 490 | case IUH_PPI_SABP: |
| 491 | case IUH_PPI_RNA: |
| 492 | case IUH_PPI_PUA: |
| 493 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 494 | sinfo.sinfo_ppid); |
| 495 | rc = 0; |
| 496 | break; |
| 497 | default: |
| 498 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 499 | sinfo.sinfo_ppid); |
| 500 | rc = 0; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | msgb_free(msg); |
| 505 | return rc; |
| 506 | } |
| 507 | |
| 508 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 509 | { |
| 510 | struct hnb_test *ctx = fd->data; |
| 511 | struct sctp_sndrcvinfo sinfo = { |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 512 | .sinfo_ppid = htonl(msgb_sctp_ppid(msg)), |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 513 | .sinfo_stream = 0, |
| 514 | }; |
| 515 | int rc; |
| 516 | |
| 517 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 518 | &sinfo, 0); |
| 519 | /* we don't need to msgb_free(), write_queue does this for us */ |
| 520 | return rc; |
| 521 | } |
| 522 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 523 | static void hnb_send_register_req(struct hnb_test *hnb_test) |
| 524 | { |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 525 | HNBRegisterRequest_t request_out; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 526 | struct msgb *msg; |
| 527 | int rc; |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 528 | uint16_t lac, sac; |
| 529 | uint8_t rac; |
| 530 | uint32_t cid; |
| 531 | uint8_t plmn[] = {0x09, 0xf1, 0x99}; |
| 532 | char identity[50] = "ATestHNB@"; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 533 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 534 | HNBRegisterRequestIEs_t request; |
| 535 | memset(&request, 0, sizeof(request)); |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 536 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 537 | lac = 0xc0fe; |
| 538 | sac = 0xabab; |
| 539 | rac = 0x42; |
Daniel Willmann | d6a45b4 | 2015-12-08 13:55:17 +0100 | [diff] [blame] | 540 | cid = 0xadceaab; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 541 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 542 | asn1_u16_to_str(&request.lac, &lac, lac); |
| 543 | asn1_u16_to_str(&request.sac, &sac, sac); |
| 544 | asn1_u8_to_str(&request.rac, &rac, rac); |
Daniel Willmann | d6a45b4 | 2015-12-08 13:55:17 +0100 | [diff] [blame] | 545 | asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid); |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 546 | |
| 547 | request.hnB_Identity.hNB_Identity_Info.buf = identity; |
| 548 | request.hnB_Identity.hNB_Identity_Info.size = strlen(identity); |
| 549 | |
| 550 | request.plmNidentity.buf = plmn; |
| 551 | request.plmNidentity.size = 3; |
| 552 | |
| 553 | |
| 554 | |
| 555 | memset(&request_out, 0, sizeof(request_out)); |
| 556 | rc = hnbap_encode_hnbregisterrequesties(&request_out, &request); |
| 557 | if (rc < 0) { |
| 558 | printf("Could not encode HNB register request IEs\n"); |
| 559 | } |
| 560 | |
| 561 | msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister, |
| 562 | Criticality_reject, |
| 563 | &asn_DEF_HNBRegisterRequest, |
| 564 | &request_out); |
| 565 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 566 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 567 | msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; |
| 568 | |
| 569 | osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
| 570 | } |
| 571 | |
| 572 | static void hnb_send_deregister_req(struct hnb_test *hnb_test) |
| 573 | { |
| 574 | struct msgb *msg; |
| 575 | int rc; |
| 576 | |
| 577 | HNBDe_RegisterIEs_t request; |
| 578 | memset(&request, 0, sizeof(request)); |
| 579 | |
| 580 | request.cause.present = Cause_PR_misc; |
| 581 | request.cause.choice.misc = CauseMisc_o_and_m_intervention; |
| 582 | |
| 583 | HNBDe_Register_t request_out; |
| 584 | memset(&request_out, 0, sizeof(request_out)); |
| 585 | rc = hnbap_encode_hnbde_registeries(&request_out, &request); |
| 586 | if (rc < 0) { |
| 587 | printf("Could not encode HNB deregister request IEs\n"); |
| 588 | } |
| 589 | |
| 590 | msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBDe_Register, |
| 591 | Criticality_reject, |
| 592 | &asn_DEF_HNBDe_Register, |
| 593 | &request_out); |
| 594 | |
| 595 | msgb_sctp_ppid(msg) = IUH_PPI_HNBAP; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 596 | |
| 597 | osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
| 598 | } |
| 599 | |
| 600 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 601 | static const struct log_info_cat log_cat[] = { |
| 602 | [DMAIN] = { |
Daniel Willmann | 3279780 | 2015-12-17 12:53:05 +0100 | [diff] [blame] | 603 | .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1, |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 604 | .color = "", |
| 605 | .description = "Main program", |
| 606 | }, |
Daniel Willmann | 3279780 | 2015-12-17 12:53:05 +0100 | [diff] [blame] | 607 | [DHNBAP] = { |
| 608 | .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 609 | .color = "", |
| 610 | .description = "Home Node B Application Part", |
| 611 | }, |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 612 | }; |
| 613 | |
| 614 | static const struct log_info hnb_test_log_info = { |
| 615 | .cat = log_cat, |
| 616 | .num_cat = ARRAY_SIZE(log_cat), |
| 617 | }; |
| 618 | |
| 619 | static struct vty_app_info vty_info = { |
| 620 | .name = "OsmoHNB-Test", |
| 621 | .version = "0", |
| 622 | }; |
| 623 | |
Daniel Willmann | 4abdee0 | 2015-12-09 17:57:32 +0100 | [diff] [blame] | 624 | static int sctp_sock_init(int fd) |
| 625 | { |
| 626 | struct sctp_event_subscribe event; |
| 627 | int rc; |
| 628 | |
| 629 | /* subscribe for all events */ |
| 630 | memset((uint8_t *)&event, 1, sizeof(event)); |
| 631 | rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, |
| 632 | &event, sizeof(event)); |
| 633 | |
| 634 | return rc; |
| 635 | } |
| 636 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 637 | #define HNBAP_STR "HNBAP related commands\n" |
| 638 | #define HNB_STR "HomeNodeB commands\n" |
| 639 | #define UE_STR "User Equipment commands\n" |
| 640 | #define RANAP_STR "RANAP related commands\n" |
| 641 | #define CSPS_STR "Circuit Switched\n" "Packet Switched\n" |
| 642 | |
| 643 | DEFUN(hnb_register, hnb_register_cmd, |
| 644 | "hnbap hnb register", HNBAP_STR HNB_STR "Send HNB-REGISTER REQUEST") |
| 645 | { |
| 646 | hnb_send_register_req(&g_hnb_test); |
| 647 | |
| 648 | return CMD_SUCCESS; |
| 649 | } |
| 650 | |
| 651 | DEFUN(hnb_deregister, hnb_deregister_cmd, |
| 652 | "hnbap hnb deregister", HNBAP_STR HNB_STR "Send HNB-DEREGISTER REQUEST") |
| 653 | { |
| 654 | hnb_send_deregister_req(&g_hnb_test); |
| 655 | |
| 656 | return CMD_SUCCESS; |
| 657 | } |
| 658 | |
| 659 | DEFUN(ue_register, ue_register_cmd, |
| 660 | "hnbap ue register IMSI", HNBAP_STR UE_STR "Send UE-REGISTER REQUEST") |
| 661 | { |
| 662 | hnb_test_ue_register_tx(&g_hnb_test, argv[0]); |
| 663 | |
| 664 | return CMD_SUCCESS; |
| 665 | } |
| 666 | |
| 667 | DEFUN(asn_dbg, asn_dbg_cmd, |
| 668 | "asn-debug (1|0)", "Enable or disabel libasn1c debugging") |
| 669 | { |
| 670 | asn_debug = atoi(argv[0]); |
| 671 | |
| 672 | return CMD_SUCCESS; |
| 673 | } |
| 674 | |
| 675 | DEFUN(ranap_reset, ranap_reset_cmd, |
| 676 | "ranap reset (cs|ps)", RANAP_STR "Send RANAP RESET\n" CSPS_STR) |
| 677 | { |
| 678 | int is_ps = 0; |
| 679 | struct msgb *msg, *rua; |
| 680 | |
| 681 | RANAP_Cause_t cause = { |
| 682 | .present = RANAP_Cause_PR_transmissionNetwork, |
| 683 | .choice.transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure, |
| 684 | }; |
| 685 | |
| 686 | if (!strcmp(argv[0], "ps")) |
| 687 | is_ps = 1; |
| 688 | |
| 689 | msg = ranap_new_msg_reset(is_ps, &cause); |
| 690 | rua = rua_new_udt(msg); |
| 691 | //msgb_free(msg); |
| 692 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 693 | |
| 694 | return CMD_SUCCESS; |
| 695 | } |
| 696 | |
| 697 | |
| 698 | enum my_vty_nodes { |
| 699 | CHAN_NODE = _LAST_OSMOVTY_NODE, |
| 700 | }; |
| 701 | |
| 702 | static struct cmd_node chan_node = { |
| 703 | CHAN_NODE, |
| 704 | "%s(chan)> ", |
| 705 | 1, |
| 706 | }; |
| 707 | |
| 708 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 709 | static struct msgb *gen_initue_lu(int is_ps, uint32_t conn_id, const char *imsi) |
| 710 | { |
Neels Hofmeyr | 5c1cc8c | 2016-02-29 09:28:48 +0100 | [diff] [blame] | 711 | uint8_t lu[] = { GSM48_PDISC_MM, GSM48_MT_MM_LOC_UPD_REQUEST, |
| 712 | 0x70, 0x62, 0xf2, 0x30, 0xff, 0xf3, 0x57, |
Neels Hofmeyr | 3282870 | 2016-01-14 13:06:47 +0100 | [diff] [blame] | 713 | /* len, IMSI/type, IMSI-------------------------------- */ |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 714 | 0x08, 0x29, 0x26, 0x24, 0x10, 0x32, 0x54, 0x76, 0x98, |
| 715 | 0x33, 0x03, 0x57, 0x18 , 0xb2 }; |
| 716 | uint8_t plmn_id[] = { 0x09, 0x01, 0x99 }; |
| 717 | RANAP_GlobalRNC_ID_t rnc_id = { |
| 718 | .rNC_ID = 23, |
| 719 | .pLMNidentity.buf = plmn_id, |
| 720 | .pLMNidentity.size = sizeof(plmn_id), |
| 721 | }; |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 722 | |
| 723 | /* FIXME: patch imsi */ |
Neels Hofmeyr | 7b81128 | 2016-01-14 13:05:24 +0100 | [diff] [blame] | 724 | /* Note: the Mobile Identitiy IE's IMSI data has the identity type and |
| 725 | * an even/odd indicator bit encoded in the first octet. So the first |
| 726 | * octet looks like this: |
| 727 | * |
| 728 | * 8 7 6 5 | 4 | 3 2 1 |
| 729 | * IMSI-digit | even/odd | type |
| 730 | * |
| 731 | * followed by the remaining IMSI digits. |
| 732 | * If digit count is even (bit 4 == 0), that first high-nibble is 0xf. |
| 733 | * (derived from Iu pcap Location Update Request msg and TS 25.413) |
| 734 | * |
| 735 | * TODO I'm only 90% sure about this |
| 736 | */ |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 737 | |
Neels Hofmeyr | 6a62e54 | 2016-01-15 03:07:45 +0100 | [diff] [blame] | 738 | return ranap_new_msg_initial_ue(conn_id, is_ps, &rnc_id, lu, sizeof(lu)); |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 739 | } |
| 740 | |
| 741 | DEFUN(chan, chan_cmd, |
| 742 | "channel (cs|ps) lu imsi IMSI", |
| 743 | "Open a new Signalling Connection\n" |
| 744 | "To Circuit-Switched CN\n" |
| 745 | "To Packet-Switched CN\n" |
| 746 | "Performing a Location Update\n" |
| 747 | ) |
| 748 | { |
| 749 | struct hnbtest_chan *chan; |
| 750 | struct msgb *msg, *rua; |
Daniel Willmann | 8592716 | 2016-01-14 15:36:49 +0100 | [diff] [blame] | 751 | static uint16_t conn_id = 42; |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 752 | |
| 753 | chan = talloc_zero(tall_hnb_ctx, struct hnbtest_chan); |
| 754 | if (!strcmp(argv[0], "ps")) |
| 755 | chan->is_ps = 1; |
| 756 | chan->imsi = talloc_strdup(chan, argv[1]); |
Daniel Willmann | 8592716 | 2016-01-14 15:36:49 +0100 | [diff] [blame] | 757 | chan->conn_id = conn_id; |
| 758 | conn_id++; |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 759 | |
| 760 | msg = gen_initue_lu(chan->is_ps, chan->conn_id, chan->imsi); |
| 761 | rua = rua_new_conn(chan->is_ps, chan->conn_id, msg); |
| 762 | |
| 763 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 764 | |
| 765 | vty->index = chan; |
| 766 | vty->node = CHAN_NODE; |
| 767 | |
Neels Hofmeyr | 860a129 | 2016-02-18 23:03:15 +0100 | [diff] [blame] | 768 | if (!chan->is_ps) |
| 769 | g_hnb_test.cs.chan = chan; |
| 770 | |
| 771 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 772 | return CMD_SUCCESS; |
| 773 | } |
| 774 | |
| 775 | static void hnbtest_vty_init(void) |
| 776 | { |
| 777 | install_element_ve(&asn_dbg_cmd); |
| 778 | install_element_ve(&hnb_register_cmd); |
| 779 | install_element_ve(&hnb_deregister_cmd); |
| 780 | install_element_ve(&ue_register_cmd); |
| 781 | install_element_ve(&ranap_reset_cmd); |
| 782 | install_element_ve(&chan_cmd); |
| 783 | |
| 784 | install_node(&chan_node, NULL); |
| 785 | vty_install_default(CHAN_NODE); |
| 786 | } |
| 787 | |
Daniel Willmann | 141a0ba | 2015-12-17 18:03:52 +0100 | [diff] [blame] | 788 | static void handle_options(int argc, char **argv) |
| 789 | { |
| 790 | while (1) { |
| 791 | int idx = 0, c; |
| 792 | static const struct option long_options[] = { |
| 793 | { "ues", 1, 0, 'u' }, |
Neels Hofmeyr | 5f9be1e | 2016-02-29 13:33:44 +0100 | [diff] [blame] | 794 | { "gw-addr", 1, 0, 'g' }, |
Daniel Willmann | 141a0ba | 2015-12-17 18:03:52 +0100 | [diff] [blame] | 795 | { 0, 0, 0, 0 }, |
| 796 | }; |
| 797 | |
Neels Hofmeyr | 5f9be1e | 2016-02-29 13:33:44 +0100 | [diff] [blame] | 798 | c = getopt_long(argc, argv, "u:g:", long_options, &idx); |
Daniel Willmann | 141a0ba | 2015-12-17 18:03:52 +0100 | [diff] [blame] | 799 | |
| 800 | if (c == -1) |
| 801 | break; |
| 802 | |
| 803 | switch (c) { |
| 804 | case 'u': |
| 805 | g_hnb_test.ues = atoi(optarg); |
| 806 | break; |
Neels Hofmeyr | 5f9be1e | 2016-02-29 13:33:44 +0100 | [diff] [blame] | 807 | case 'g': |
| 808 | g_hnb_test.gw_addr = optarg; |
| 809 | break; |
Daniel Willmann | 141a0ba | 2015-12-17 18:03:52 +0100 | [diff] [blame] | 810 | } |
| 811 | } |
| 812 | } |
| 813 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 814 | int main(int argc, char **argv) |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 815 | { |
| 816 | int rc; |
| 817 | |
Harald Welte | 87ffeb9 | 2015-12-25 15:34:22 +0100 | [diff] [blame] | 818 | test_common_init(); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 819 | |
Harald Welte | 87ffeb9 | 2015-12-25 15:34:22 +0100 | [diff] [blame] | 820 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 821 | |
| 822 | vty_init(&vty_info); |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 823 | hnbtest_vty_init(); |
| 824 | |
Neels Hofmeyr | a0d2147 | 2016-02-24 20:50:31 +0100 | [diff] [blame] | 825 | printf("VTY at %s %d\n", vty_get_bind_addr(), 2324); |
| 826 | rc = telnet_init_dynif(NULL, NULL, vty_get_bind_addr(), 2324); |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 827 | if (rc < 0) { |
| 828 | perror("Error binding VTY port"); |
| 829 | exit(1); |
| 830 | } |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 831 | |
Daniel Willmann | 141a0ba | 2015-12-17 18:03:52 +0100 | [diff] [blame] | 832 | handle_options(argc, argv); |
| 833 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 834 | osmo_wqueue_init(&g_hnb_test.wqueue, 16); |
| 835 | g_hnb_test.wqueue.bfd.data = &g_hnb_test; |
| 836 | g_hnb_test.wqueue.read_cb = hnb_read_cb; |
| 837 | g_hnb_test.wqueue.write_cb = hnb_write_cb; |
| 838 | |
| 839 | rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM, |
Neels Hofmeyr | 5f9be1e | 2016-02-29 13:33:44 +0100 | [diff] [blame] | 840 | IPPROTO_SCTP, g_hnb_test.gw_addr, |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 841 | g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT); |
| 842 | if (rc < 0) { |
| 843 | perror("Error connecting to Iuh port"); |
| 844 | exit(1); |
| 845 | } |
Daniel Willmann | 4abdee0 | 2015-12-09 17:57:32 +0100 | [diff] [blame] | 846 | sctp_sock_init(g_hnb_test.wqueue.bfd.fd); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 847 | |
Harald Welte | c385122 | 2015-12-24 15:41:21 +0100 | [diff] [blame] | 848 | #if 0 |
| 849 | /* some hard-coded message generation. Doesn't make sense from |
| 850 | * a protocol point of view but enables to look at the encoded |
| 851 | * results in wireshark for manual verification */ |
| 852 | { |
| 853 | struct msgb *msg, *rua; |
| 854 | const uint8_t nas[] = { 0, 1, 2, 3 }; |
| 855 | const uint8_t ik[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; |
| 856 | |
| 857 | msg = ranap_new_msg_dt(0, nas, sizeof(nas)); |
| 858 | rua = rua_new_udt(msg); |
| 859 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 860 | |
| 861 | msg = ranap_new_msg_sec_mod_cmd(ik, ik); |
| 862 | rua = rua_new_udt(msg); |
| 863 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 864 | |
| 865 | msg = ranap_new_msg_iu_rel_cmd() |
| 866 | rua = rua_new_udt(msg); |
| 867 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 868 | |
| 869 | msg = ranap_new_msg_paging_cmd("901990123456789", NULL, 0, 0); |
| 870 | rua = rua_new_udt(msg); |
| 871 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 872 | |
| 873 | msg = ranap_new_msg_rab_assign_voice(1, 0x01020304, 0x1020); |
| 874 | rua = rua_new_udt(msg); |
| 875 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 876 | |
| 877 | msg = ranap_new_msg_rab_assign_data(2, 0x01020304, 0x11223344); |
| 878 | rua = rua_new_udt(msg); |
| 879 | osmo_wqueue_enqueue(&g_hnb_test.wqueue, rua); |
| 880 | } |
| 881 | #endif |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 882 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 883 | while (1) { |
| 884 | rc = osmo_select_main(0); |
| 885 | if (rc < 0) |
| 886 | exit(3); |
| 887 | } |
| 888 | |
| 889 | /* not reached */ |
| 890 | exit(0); |
| 891 | } |