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 | |
| 58 | static int hnb_read_cb(struct osmo_fd *fd) |
| 59 | { |
| 60 | struct hnb_test *hnb_test = fd->data; |
| 61 | struct sctp_sndrcvinfo sinfo; |
| 62 | struct msgb *msg = msgb_alloc(IUH_MSGB_SIZE, "Iuh rx"); |
| 63 | int flags = 0; |
| 64 | int rc; |
| 65 | |
| 66 | if (!msg) |
| 67 | return -ENOMEM; |
| 68 | |
| 69 | rc = sctp_recvmsg(fd->fd, msgb_data(msg), msgb_tailroom(msg), |
| 70 | NULL, NULL, &sinfo, &flags); |
| 71 | if (rc < 0) { |
| 72 | LOGP(DMAIN, LOGL_ERROR, "Error during sctp_recvmsg()\n"); |
| 73 | /* FIXME: clean up after disappeared HNB */ |
| 74 | return rc; |
| 75 | } else |
| 76 | msgb_put(msg, rc); |
| 77 | |
| 78 | if (flags & MSG_NOTIFICATION) { |
| 79 | LOGP(DMAIN, LOGL_INFO, "Ignoring SCTP notification\n"); |
| 80 | msgb_free(msg); |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | sinfo.sinfo_ppid = ntohl(sinfo.sinfo_ppid); |
| 85 | |
| 86 | switch (sinfo.sinfo_ppid) { |
| 87 | case IUH_PPI_HNBAP: |
| 88 | printf("HNBAP mesage received\n"); |
| 89 | // rc = hnbgw_hnbap_rx(hnb, msg); |
| 90 | break; |
| 91 | case IUH_PPI_RUA: |
| 92 | printf("RUA mesage received\n"); |
| 93 | // rc = hnbgw_rua_rx(hnb, msg); |
| 94 | break; |
| 95 | case IUH_PPI_SABP: |
| 96 | case IUH_PPI_RNA: |
| 97 | case IUH_PPI_PUA: |
| 98 | LOGP(DMAIN, LOGL_ERROR, "Unimplemented SCTP PPID=%u received\n", |
| 99 | sinfo.sinfo_ppid); |
| 100 | rc = 0; |
| 101 | break; |
| 102 | default: |
| 103 | LOGP(DMAIN, LOGL_ERROR, "Unknown SCTP PPID=%u received\n", |
| 104 | sinfo.sinfo_ppid); |
| 105 | rc = 0; |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | msgb_free(msg); |
| 110 | return rc; |
| 111 | } |
| 112 | |
| 113 | static int hnb_write_cb(struct osmo_fd *fd, struct msgb *msg) |
| 114 | { |
| 115 | struct hnb_test *ctx = fd->data; |
| 116 | struct sctp_sndrcvinfo sinfo = { |
| 117 | .sinfo_ppid = htonl(msgb_ppid(msg)), |
| 118 | .sinfo_stream = 0, |
| 119 | }; |
| 120 | int rc; |
| 121 | |
| 122 | rc = sctp_send(fd->fd, msgb_data(msg), msgb_length(msg), |
| 123 | &sinfo, 0); |
| 124 | /* we don't need to msgb_free(), write_queue does this for us */ |
| 125 | return rc; |
| 126 | } |
| 127 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 128 | #include "pdus.h" |
| 129 | |
| 130 | static void hnb_send_register_req(struct hnb_test *hnb_test) |
| 131 | { |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame^] | 132 | HNBRegisterRequest_t request_out; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 133 | struct msgb *msg; |
| 134 | int rc; |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame^] | 135 | uint16_t lac, sac; |
| 136 | uint8_t rac; |
| 137 | uint32_t cid; |
| 138 | uint8_t plmn[] = {0x09, 0xf1, 0x99}; |
| 139 | char identity[50] = "ATestHNB@"; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 140 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame^] | 141 | HNBRegisterRequestIEs_t request; |
| 142 | memset(&request, 0, sizeof(request)); |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 143 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame^] | 144 | lac = 0xc0fe; |
| 145 | sac = 0xabab; |
| 146 | rac = 0x42; |
| 147 | cid = 0xadce00; |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 148 | |
Daniel Willmann | a1e202e | 2015-12-07 17:21:07 +0100 | [diff] [blame^] | 149 | asn1_u16_to_str(&request.lac, &lac, lac); |
| 150 | asn1_u16_to_str(&request.sac, &sac, sac); |
| 151 | asn1_u8_to_str(&request.rac, &rac, rac); |
| 152 | asn1_u32_to_bitstring(&request.cellIdentity, &cid, cid); |
| 153 | request.cellIdentity.bits_unused = 4; |
| 154 | |
| 155 | request.hnB_Identity.hNB_Identity_Info.buf = identity; |
| 156 | request.hnB_Identity.hNB_Identity_Info.size = strlen(identity); |
| 157 | |
| 158 | request.plmNidentity.buf = plmn; |
| 159 | request.plmNidentity.size = 3; |
| 160 | |
| 161 | |
| 162 | |
| 163 | memset(&request_out, 0, sizeof(request_out)); |
| 164 | rc = hnbap_encode_hnbregisterrequesties(&request_out, &request); |
| 165 | if (rc < 0) { |
| 166 | printf("Could not encode HNB register request IEs\n"); |
| 167 | } |
| 168 | |
| 169 | msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister, |
| 170 | Criticality_reject, |
| 171 | &asn_DEF_HNBRegisterRequest, |
| 172 | &request_out); |
| 173 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 174 | |
| 175 | msgb_ppid(msg) = IUH_PPI_HNBAP; |
| 176 | |
| 177 | osmo_wqueue_enqueue(&hnb_test->wqueue, msg); |
| 178 | } |
| 179 | |
| 180 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 181 | static const struct log_info_cat log_cat[] = { |
| 182 | [DMAIN] = { |
| 183 | .name = "DMAIN", .loglevel = LOGL_DEBUG, .enabled = 1, |
| 184 | .color = "", |
| 185 | .description = "Main program", |
| 186 | }, |
| 187 | }; |
| 188 | |
| 189 | static const struct log_info hnb_test_log_info = { |
| 190 | .cat = log_cat, |
| 191 | .num_cat = ARRAY_SIZE(log_cat), |
| 192 | }; |
| 193 | |
| 194 | static struct vty_app_info vty_info = { |
| 195 | .name = "OsmoHNB-Test", |
| 196 | .version = "0", |
| 197 | }; |
| 198 | |
| 199 | int main(int argc, const char *argv) |
| 200 | { |
| 201 | int rc; |
| 202 | |
| 203 | tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context"); |
| 204 | talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context"); |
| 205 | |
| 206 | rc = osmo_init_logging(&hnb_test_log_info); |
| 207 | if (rc < 0) |
| 208 | exit(1); |
| 209 | |
| 210 | vty_init(&vty_info); |
| 211 | |
| 212 | osmo_wqueue_init(&g_hnb_test.wqueue, 16); |
| 213 | g_hnb_test.wqueue.bfd.data = &g_hnb_test; |
| 214 | g_hnb_test.wqueue.read_cb = hnb_read_cb; |
| 215 | g_hnb_test.wqueue.write_cb = hnb_write_cb; |
| 216 | |
| 217 | rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM, |
| 218 | IPPROTO_SCTP, "127.0.0.1", |
| 219 | g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT); |
| 220 | if (rc < 0) { |
| 221 | perror("Error connecting to Iuh port"); |
| 222 | exit(1); |
| 223 | } |
| 224 | // sctp_sock_init(g_hnb_test.conn_fd); |
| 225 | |
Daniel Willmann | 4aeef6c | 2015-12-03 17:02:13 +0100 | [diff] [blame] | 226 | hnb_send_register_req(&g_hnb_test); |
| 227 | |
Daniel Willmann | 97374c0 | 2015-12-03 09:37:58 +0100 | [diff] [blame] | 228 | while (1) { |
| 229 | rc = osmo_select_main(0); |
| 230 | if (rc < 0) |
| 231 | exit(3); |
| 232 | } |
| 233 | |
| 234 | /* not reached */ |
| 235 | exit(0); |
| 236 | } |