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> |
| 43 | |
| 44 | #include <osmocom/vty/telnet_interface.h> |
| 45 | #include <osmocom/vty/logging.h> |
| 46 | |
| 47 | #include "hnb-test.h" |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 48 | #include "hnbap_common.h" |
| 49 | #include "hnbap_ies_defs.h" |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 50 | |
| 51 | static void *tall_hnb_ctx; |
| 52 | void *talloc_asn1_ctx; |
| 53 | |
| 54 | struct hnb_test g_hnb_test = { |
| 55 | .gw_port = IUH_DEFAULT_SCTP_PORT, |
| 56 | }; |
| 57 | |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 58 | int hnb_test_ue_register_tx(struct hnb_test *hnb_test) |
| 59 | { |
Daniel Willmann | 4e31250 | 2015-12-09 17:59:24 +0100 | [diff] [blame] | 60 | struct msgb *msg; |
| 61 | int rc, imsi_len; |
| 62 | |
| 63 | char imsi_buf[16]; |
| 64 | char *imsi_str = "262019876543210"; |
| 65 | |
| 66 | UERegisterRequest_t request_out; |
| 67 | UERegisterRequestIEs_t request; |
| 68 | memset(&request, 0, sizeof(request)); |
| 69 | |
| 70 | request.uE_Identity.present = UE_Identity_PR_iMSI; |
| 71 | |
| 72 | imsi_len = encode_iu_imsi(imsi_buf, sizeof(imsi_buf), imsi_str); |
| 73 | request.uE_Identity.choice.iMSI.buf = imsi_buf; |
| 74 | request.uE_Identity.choice.iMSI.size = imsi_len; |
| 75 | |
| 76 | request.registration_Cause = Registration_Cause_normal; |
| 77 | request.uE_Capabilities.access_stratum_release_indicator = Access_stratum_release_indicator_rel_6; |
| 78 | request.uE_Capabilities.csg_capability = CSG_Capability_not_csg_capable; |
| 79 | |
| 80 | memset(&request_out, 0, sizeof(request_out)); |
| 81 | rc = hnbap_encode_ueregisterrequesties(&request_out, &request); |
| 82 | |
| 83 | msg = hnbap_generate_initiating_message(ProcedureCode_id_UERegister, |
| 84 | Criticality_reject, |
| 85 | &asn_DEF_UERegisterRequest, |
| 86 | &request_out); |
| 87 | |
| 88 | |
| 89 | msgb_ppid(msg) = IUH_PPI_HNBAP; |
| 90 | |
| 91 | return osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int hnb_test_rx_hnb_register_acc(struct hnb_test *hnb, ANY_t *in) |
| 95 | { |
| 96 | int rc; |
| 97 | HNBRegisterAcceptIEs_t accept; |
| 98 | |
| 99 | rc = hnbap_decode_hnbregisteraccepties(&accept, in); |
| 100 | if (rc < 0) { |
| 101 | } |
| 102 | |
| 103 | hnb->rnc_id = accept.rnc_id; |
| 104 | printf("HNB Register accept with RNC ID %u\n", hnb->rnc_id); |
| 105 | |
| 106 | return hnb_test_ue_register_tx(hnb); |
| 107 | } |
| 108 | |
Daniel Willmann | a7b0240 | 2015-12-09 19:05:09 +0100 | [diff] [blame^] | 109 | int hnb_test_rx_ue_register_acc(struct hnb_test *hnb, ANY_t *in) |
| 110 | { |
| 111 | int rc; |
| 112 | uint32_t ctx_id; |
| 113 | UERegisterAcceptIEs_t accept; |
| 114 | char imsi[16]; |
| 115 | |
| 116 | rc = hnbap_decode_ueregisteraccepties(&accept, in); |
| 117 | if (rc < 0) { |
| 118 | return rc; |
| 119 | } |
| 120 | |
| 121 | if (accept.uE_Identity.present != UE_Identity_PR_iMSI) { |
| 122 | printf("Wrong type in UE register accept\n"); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | ctx_id = asn1bitstr_to_u24(&accept.context_ID); |
| 127 | |
| 128 | decode_iu_bcd(imsi, sizeof(imsi), accept.uE_Identity.choice.iMSI.buf, |
| 129 | accept.uE_Identity.choice.iMSI.size); |
| 130 | printf("UE Register accept for IMSI %s, context %u\n", imsi, ctx_id); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 135 | int hnb_test_hnbap_rx(struct hnb_test *hnb, struct msgb *msg) |
| 136 | { |
| 137 | HNBAP_PDU_t _pdu, *pdu = &_pdu; |
| 138 | asn_dec_rval_t dec_ret; |
| 139 | int rc; |
| 140 | |
| 141 | memset(pdu, 0, sizeof(*pdu)); |
| 142 | dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu, |
| 143 | msg->data, msgb_length(msg), 0, 0); |
| 144 | if (dec_ret.code != RC_OK) { |
| 145 | LOGP(DMAIN, LOGL_ERROR, "Error in ASN.1 decode\n"); |
| 146 | return rc; |
| 147 | } |
| 148 | |
| 149 | if (pdu->present != HNBAP_PDU_PR_successfulOutcome) { |
| 150 | printf("Unexpected HNBAP message received\n"); |
| 151 | } |
| 152 | |
| 153 | switch (pdu->choice.successfulOutcome.procedureCode) { |
| 154 | case ProcedureCode_id_HNBRegister: |
| 155 | /* Get HNB id and send UE Register request */ |
| 156 | rc = hnb_test_rx_hnb_register_acc(hnb, &pdu->choice.successfulOutcome.value); |
| 157 | break; |
| 158 | case ProcedureCode_id_UERegister: |
Daniel Willmann | a7b0240 | 2015-12-09 19:05:09 +0100 | [diff] [blame^] | 159 | rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value); |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 160 | break; |
| 161 | default: |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | return rc; |
| 166 | } |
| 167 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 168 | static int hnb_read_cb(struct osmo_fd *fd) |
| 169 | { |
| 170 | struct hnb_test *hnb_test = fd->data; |
| 171 | struct sctp_sndrcvinfo sinfo; |
| 172 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
| 173 | int flags = 0; |
| 174 | int rc; |
| 175 | |
| 176 | if (!msg) |
| 177 | return -ENOMEM; |
| 178 | |
| 179 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 180 | NULL, NULL, &sinfo, &flags); |
| 181 | if (rc < 0) { |
| 182 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
| 183 | /* FIXME: clean up after disappeared HNB */ |
| 184 | return rc; |
| 185 | } else |
| 186 | msgb_put(msg, rc); |
| 187 | |
| 188 | if (flags & MSG_NOTIFICATION) { |
| 189 | LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n"); |
| 190 | msgb_free(msg); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 195 | |
| 196 | switch (sinfo.sinfo_ppid) { |
| 197 | case IUH_PPI_HNBAP: |
| 198 | printf("HNBAP mesage received\n"); |
Daniel Willmann | 479cb30 | 2015-12-09 17:54:59 +0100 | [diff] [blame] | 199 | rc = hnb_test_hnbap_rx(hnb_test, msg); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 200 | break; |
| 201 | case IUH_PPI_RUA: |
| 202 | printf("RUA mesage received\n"); |
| 203 | // rc = hnbgw_rua_rx(hnb, msg); |
| 204 | break; |
| 205 | case IUH_PPI_SABP: |
| 206 | case IUH_PPI_RNA: |
| 207 | case IUH_PPI_PUA: |
| 208 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 209 | sinfo.sinfo_ppid); |
| 210 | rc = 0; |
| 211 | break; |
| 212 | default: |
| 213 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 214 | sinfo.sinfo_ppid); |
| 215 | rc = 0; |
| 216 | break; |
| 217 | } |
| 218 | |
| 219 | msgb_free(msg); |
| 220 | return rc; |
| 221 | } |
| 222 | |
| 223 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 224 | { |
| 225 | struct hnb_test *ctx = fd->data; |
| 226 | struct sctp_sndrcvinfo sinfo = { |
| 227 | .sinfo_ppid = htonl(msgb_ppid(msg)), |
| 228 | .sinfo_stream = 0, |
| 229 | }; |
| 230 | int rc; |
| 231 | |
| 232 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 233 | &sinfo, 0); |
| 234 | /* we don't need to msgb_free(), write_queue does this for us */ |
| 235 | return rc; |
| 236 | } |
| 237 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 238 | #include "pdus.h" |
| 239 | |
| 240 | static void hnb_send_register_req(struct hnb_test *hnb_test) |
| 241 | { |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 242 | HNBRegisterRequest_t request_out; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 243 | struct msgb *msg; |
| 244 | int rc; |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 245 | uint16_t lac, sac; |
| 246 | uint8_t rac; |
| 247 | uint32_t cid; |
| 248 | uint8_t plmn[] = {0x09, 0xf1, 0x99}; |
| 249 | char identity[50] = "ATestHNB@"; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 250 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 251 | HNBRegisterRequestIEs_t request; |
| 252 | memset(&request, 0, sizeof(request)); |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 253 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 254 | lac = 0xc0fe; |
| 255 | sac = 0xabab; |
| 256 | rac = 0x42; |
Daniel Willmann | d6a45b4 | 2015-12-08 13:55:17 +0100 | [diff] [blame] | 257 | cid = 0xadceaab; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 258 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 259 | asn1_u16_to_str(&request.lac, &lac, lac); |
| 260 | asn1_u16_to_str(&request.sac, &sac, sac); |
| 261 | asn1_u8_to_str(&request.rac, &rac, rac); |
Daniel Willmann | d6a45b4 | 2015-12-08 13:55:17 +0100 | [diff] [blame] | 262 | asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid); |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame] | 263 | |
| 264 | request.hnB_Identity.hNB_Identity_Info.buf = identity; |
| 265 | request.hnB_Identity.hNB_Identity_Info.size = strlen(identity); |
| 266 | |
| 267 | request.plmNidentity.buf = plmn; |
| 268 | request.plmNidentity.size = 3; |
| 269 | |
| 270 | |
| 271 | |
| 272 | memset(&request_out, 0, sizeof(request_out)); |
| 273 | rc = hnbap_encode_hnbregisterrequesties(&request_out, &request); |
| 274 | if (rc < 0) { |
| 275 | printf("Could not encode HNB register request IEs\n"); |
| 276 | } |
| 277 | |
| 278 | msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister, |
| 279 | Criticality_reject, |
| 280 | &asn_DEF_HNBRegisterRequest, |
| 281 | &request_out); |
| 282 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 283 | |
| 284 | msgb_ppid(msg) = IUH_PPI_HNBAP; |
| 285 | |
| 286 | osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
| 287 | } |
| 288 | |
| 289 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 290 | static const struct log_info_cat log_cat[] = { |
| 291 | [DMAIN] = { |
| 292 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 293 | .color = "", |
| 294 | .description = "Main program", |
| 295 | }, |
| 296 | }; |
| 297 | |
| 298 | static const struct log_info hnb_test_log_info = { |
| 299 | .cat = log_cat, |
| 300 | .num_cat = ARRAY_SIZE(log_cat), |
| 301 | }; |
| 302 | |
| 303 | static struct vty_app_info vty_info = { |
| 304 | .name = "OsmoHNB-Test", |
| 305 | .version = "0", |
| 306 | }; |
| 307 | |
Daniel Willmann | 4abdee0 | 2015-12-09 17:57:32 +0100 | [diff] [blame] | 308 | static int sctp_sock_init(int fd) |
| 309 | { |
| 310 | struct sctp_event_subscribe event; |
| 311 | int rc; |
| 312 | |
| 313 | /* subscribe for all events */ |
| 314 | memset((uint8_t *)&event, 1, sizeof(event)); |
| 315 | rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, |
| 316 | &event, sizeof(event)); |
| 317 | |
| 318 | return rc; |
| 319 | } |
| 320 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 321 | int main(int argc, const char *argv) |
| 322 | { |
| 323 | int rc; |
| 324 | |
| 325 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
| 326 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
| 327 | |
| 328 | rc = osmo_init_logging(&hnb_test_log_info); |
| 329 | if (rc < 0) |
| 330 | exit(1); |
| 331 | |
| 332 | vty_init(&vty_info); |
| 333 | |
| 334 | osmo_wqueue_init(&g_hnb_test.wqueue, 16); |
| 335 | g_hnb_test.wqueue.bfd.data = &g_hnb_test; |
| 336 | g_hnb_test.wqueue.read_cb = hnb_read_cb; |
| 337 | g_hnb_test.wqueue.write_cb = hnb_write_cb; |
| 338 | |
| 339 | rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM, |
| 340 | IPPROTO_SCTP, "127.0.0.1", |
| 341 | g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT); |
| 342 | if (rc < 0) { |
| 343 | perror("Error connecting to Iuh port"); |
| 344 | exit(1); |
| 345 | } |
Daniel Willmann | 4abdee0 | 2015-12-09 17:57:32 +0100 | [diff] [blame] | 346 | sctp_sock_init(g_hnb_test.wqueue.bfd.fd); |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 347 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 348 | hnb_send_register_req(&g_hnb_test); |
| 349 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 350 | while (1) { |
| 351 | rc = osmo_select_main(0); |
| 352 | if (rc < 0) |
| 353 | exit(3); |
| 354 | } |
| 355 | |
| 356 | /* not reached */ |
| 357 | exit(0); |
| 358 | } |