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