blob: 5908716e3df831f9fc6dd3826d7bc75c99bd8ed4 [file] [log] [blame]
Daniel Willmann97374c02015-12-03 09:37:58 +01001/* 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 Willmanna1e202e2015-12-07 17:21:07 +010048#include "hnbap_common.h"
49#include "hnbap_ies_defs.h"
Daniel Willmann97374c02015-12-03 09:37:58 +010050
51static void *tall_hnb_ctx;
52void *talloc_asn1_ctx;
53
54struct hnb_test g_hnb_test = {
55 .gw_port = IUH_DEFAULT_SCTP_PORT,
56};
57
Daniel Willmann479cb302015-12-09 17:54:59 +010058int hnb_test_ue_register_tx(struct hnb_test *hnb_test)
59{
Daniel Willmann4e312502015-12-09 17:59:24 +010060 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 Willmann479cb302015-12-09 17:54:59 +010092}
93
94int 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 Willmanna7b02402015-12-09 19:05:09 +0100109int 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 Willmann479cb302015-12-09 17:54:59 +0100135int 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 Willmanna7b02402015-12-09 19:05:09 +0100159 rc = hnb_test_rx_ue_register_acc(hnb, &pdu->choice.successfulOutcome.value);
Daniel Willmann479cb302015-12-09 17:54:59 +0100160 break;
161 default:
162 break;
163 }
164
165 return rc;
166}
167
Daniel Willmann97374c02015-12-03 09:37:58 +0100168static 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) {
Daniel Willmann32797802015-12-17 12:53:05 +0100189 LOGP(DMAIN, LOGL_DEBUG, "Ignoring SCTP notification\n");
Daniel Willmann97374c02015-12-03 09:37:58 +0100190 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 Willmann479cb302015-12-09 17:54:59 +0100199 rc = hnb_test_hnbap_rx(hnb_test, msg);
Daniel Willmann97374c02015-12-03 09:37:58 +0100200 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
223static 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 Willmann4aeef6c2015-12-03 17:02:13 +0100238static void hnb_send_register_req(struct hnb_test *hnb_test)
239{
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100240 HNBRegisterRequest_t request_out;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100241 struct msgb *msg;
242 int rc;
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100243 uint16_t lac, sac;
244 uint8_t rac;
245 uint32_t cid;
246 uint8_t plmn[] = {0x09, 0xf1, 0x99};
247 char identity[50] = "ATestHNB@";
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100248
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100249 HNBRegisterRequestIEs_t request;
250 memset(&request, 0, sizeof(request));
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100251
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100252 lac = 0xc0fe;
253 sac = 0xabab;
254 rac = 0x42;
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100255 cid = 0xadceaab;
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100256
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100257 asn1_u16_to_str(&request.lac, &lac, lac);
258 asn1_u16_to_str(&request.sac, &sac, sac);
259 asn1_u8_to_str(&request.rac, &rac, rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100260 asn1_u28_to_bitstring(&request.cellIdentity, &cid, cid);
Daniel Willmanna1e202e2015-12-07 17:21:07 +0100261
262 request.hnB_Identity.hNB_Identity_Info.buf = identity;
263 request.hnB_Identity.hNB_Identity_Info.size = strlen(identity);
264
265 request.plmNidentity.buf = plmn;
266 request.plmNidentity.size = 3;
267
268
269
270 memset(&request_out, 0, sizeof(request_out));
271 rc = hnbap_encode_hnbregisterrequesties(&request_out, &request);
272 if (rc < 0) {
273 printf("Could not encode HNB register request IEs\n");
274 }
275
276 msg = hnbap_generate_initiating_message(ProcedureCode_id_HNBRegister,
277 Criticality_reject,
278 &asn_DEF_HNBRegisterRequest,
279 &request_out);
280
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100281
282 msgb_ppid(msg) = IUH_PPI_HNBAP;
283
284 osmo_wqueue_enqueue(&hnb_test->wqueue, msg);
285}
286
287
Daniel Willmann97374c02015-12-03 09:37:58 +0100288static const struct log_info_cat log_cat[] = {
289 [DMAIN] = {
Daniel Willmann32797802015-12-17 12:53:05 +0100290 .name = "DMAIN", .loglevel = LOGL_INFO, .enabled = 1,
Daniel Willmann97374c02015-12-03 09:37:58 +0100291 .color = "",
292 .description = "Main program",
293 },
Daniel Willmann32797802015-12-17 12:53:05 +0100294 [DHNBAP] = {
295 .name = "DHNBAP", .loglevel = LOGL_DEBUG, .enabled = 1,
296 .color = "",
297 .description = "Home Node B Application Part",
298 },
Daniel Willmann97374c02015-12-03 09:37:58 +0100299};
300
301static const struct log_info hnb_test_log_info = {
302 .cat = log_cat,
303 .num_cat = ARRAY_SIZE(log_cat),
304};
305
306static struct vty_app_info vty_info = {
307 .name = "OsmoHNB-Test",
308 .version = "0",
309};
310
Daniel Willmann4abdee02015-12-09 17:57:32 +0100311static int sctp_sock_init(int fd)
312{
313 struct sctp_event_subscribe event;
314 int rc;
315
316 /* subscribe for all events */
317 memset((uint8_t *)&event, 1, sizeof(event));
318 rc = setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS,
319 &event, sizeof(event));
320
321 return rc;
322}
323
Daniel Willmann97374c02015-12-03 09:37:58 +0100324int main(int argc, const char *argv)
325{
326 int rc;
327
328 tall_hnb_ctx = talloc_named_const(NULL, 0, "hnb_context");
329 talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
330
331 rc = osmo_init_logging(&hnb_test_log_info);
332 if (rc < 0)
333 exit(1);
334
335 vty_init(&vty_info);
336
337 osmo_wqueue_init(&g_hnb_test.wqueue, 16);
338 g_hnb_test.wqueue.bfd.data = &g_hnb_test;
339 g_hnb_test.wqueue.read_cb = hnb_read_cb;
340 g_hnb_test.wqueue.write_cb = hnb_write_cb;
341
342 rc = osmo_sock_init_ofd(&g_hnb_test.wqueue.bfd, AF_INET, SOCK_STREAM,
343 IPPROTO_SCTP, "127.0.0.1",
344 g_hnb_test.gw_port, OSMO_SOCK_F_CONNECT);
345 if (rc < 0) {
346 perror("Error connecting to Iuh port");
347 exit(1);
348 }
Daniel Willmann4abdee02015-12-09 17:57:32 +0100349 sctp_sock_init(g_hnb_test.wqueue.bfd.fd);
Daniel Willmann97374c02015-12-03 09:37:58 +0100350
Daniel Willmann4aeef6c2015-12-03 17:02:13 +0100351 hnb_send_register_req(&g_hnb_test);
352
Daniel Willmann97374c02015-12-03 09:37:58 +0100353 while (1) {
354 rc = osmo_select_main(0);
355 if (rc < 0)
356 exit(3);
357 }
358
359 /* not reached */
360 exit(0);
361}