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