blob: f9b4244e3115f07c12fdfee723829fbb3a85ff61 [file] [log] [blame]
Harald Welte3aad6af2015-12-26 08:43:04 +01001#include <stdint.h>
2#include <unistd.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6
Neels Hofmeyr3d127482016-04-20 11:04:49 +02007#define _GNU_SOURCE
8#include <getopt.h>
9
Harald Welte3aad6af2015-12-26 08:43:04 +010010#include <osmocom/core/select.h>
11#include <osmocom/core/prim.h>
12#include <osmocom/core/talloc.h>
13#include <osmocom/core/logging.h>
14#include <osmocom/core/application.h>
15#include <osmocom/vty/logging.h>
16
Harald Welte1be24f12015-12-26 23:44:28 +010017#include <osmocom/gsm/gsm48.h>
18
Harald Welte3aad6af2015-12-26 08:43:04 +010019#include <osmocom/sigtran/sua.h>
20#include <osmocom/sigtran/sccp_sap.h>
21
22#include "test_common.h"
Harald Welte1be24f12015-12-26 23:44:28 +010023
Neels Hofmeyr96979af2016-01-05 15:19:44 +010024#include <osmocom/ranap/ranap_ies_defs.h>
Daniel Willmannb02d77c2016-01-07 12:59:21 +010025#include <osmocom/ranap/ranap_common_cn.h>
Neels Hofmeyrb9f46a72016-01-13 11:51:34 +010026#include <osmocom/ranap/ranap_msg_factory.h>
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020027#include <osmocom/iuh/hnbgw.h>
Harald Welte3aad6af2015-12-26 08:43:04 +010028
29int asn1_xer_print = 1;
Neels Hofmeyr3d127482016-04-20 11:04:49 +020030const char *cmdline_bind_addr = "127.0.0.1";
Harald Welte3aad6af2015-12-26 08:43:04 +010031
Harald Welte2bc20f82015-12-26 23:43:48 +010032struct ue_conn_ctx {
33 struct llist_head list;
Neels Hofmeyr0a437222016-07-06 15:58:48 +020034 struct osmo_sccp_link *link;
Harald Welte2bc20f82015-12-26 23:43:48 +010035 uint32_t conn_id;
36};
37
38static LLIST_HEAD(conn_ctx_list);
39
Neels Hofmeyr0a437222016-07-06 15:58:48 +020040struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_link *link, uint32_t conn_id)
Harald Welte2bc20f82015-12-26 23:43:48 +010041{
42 struct ue_conn_ctx *ctx = talloc_zero(NULL, struct ue_conn_ctx);
43
44 ctx->link = link;
45 ctx->conn_id = conn_id;
46 llist_add(&ctx->list, &conn_ctx_list);
47
48 return ctx;
49}
50
Neels Hofmeyr0a437222016-07-06 15:58:48 +020051struct ue_conn_ctx *ue_conn_ctx_find(struct osmo_sccp_link *link, uint32_t conn_id)
Harald Welte2bc20f82015-12-26 23:43:48 +010052{
53 struct ue_conn_ctx *ctx;
54
55 llist_for_each_entry(ctx, &conn_ctx_list, list) {
56 if (ctx->link == link && ctx->conn_id == conn_id)
57 return ctx;
58 }
59 return NULL;
60}
61
Harald Welte3aad6af2015-12-26 08:43:04 +010062/***********************************************************************
63 * RANAP handling
64 ***********************************************************************/
65
Daniel Willmannb39bad12016-01-14 15:36:12 +010066static int ranap_handle_co_initial_ue(struct ue_conn_ctx *ctx, RANAP_InitialUE_MessageIEs_t *ies)
Harald Welte3aad6af2015-12-26 08:43:04 +010067{
68 struct gprs_ra_id ra_id;
69 uint16_t sai;
70 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
71 uint8_t *cur;
Daniel Willmannb39bad12016-01-14 15:36:12 +010072 struct osmo_scu_prim *prim;
Harald Welte3aad6af2015-12-26 08:43:04 +010073
Harald Welte1be24f12015-12-26 23:44:28 +010074 ranap_parse_lai(&ra_id, &ies->lai);
75 sai = asn1str_to_u16(&ies->sai.sAC);
76 cur = msgb_put(msg, ies->nas_pdu.size);
77 memcpy(msg, ies->nas_pdu.buf, ies->nas_pdu.size);
Daniel Willmannb39bad12016-01-14 15:36:12 +010078 msgb_free(msg);
Harald Welte3aad6af2015-12-26 08:43:04 +010079 /* FIXME: set msgb_gmmh() */
80
81 /* FIXME: Feed into the MM layer */
Harald Welte1be24f12015-12-26 23:44:28 +010082 //rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +010083
Daniel Willmannb39bad12016-01-14 15:36:12 +010084 msg = ranap_new_msg_dt(0, NULL, 0);
85
86 msg->l2h = msg->data;
87 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
88 prim->u.data.conn_id = ctx->conn_id;
89 osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_DATA,
90 PRIM_OP_REQUEST, msg);
91
92 osmo_sua_user_link_down(ctx->link, &prim->oph);
93
Harald Welte3aad6af2015-12-26 08:43:04 +010094 return 0;
95}
96
97static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
98{
99 struct gprs_ra_id _ra_id, *ra_id = NULL;
100 uint16_t _sai, *sai = NULL;
101 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
102 uint8_t *cur;
103
Harald Welte1be24f12015-12-26 23:44:28 +0100104 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
105 ranap_parse_lai(&_ra_id, &ies->lai);
Harald Welte3aad6af2015-12-26 08:43:04 +0100106 ra_id = &_ra_id;
Harald Welte1be24f12015-12-26 23:44:28 +0100107 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
108 _ra_id.rac = asn1str_to_u8(&ies->rac);
Harald Welte3aad6af2015-12-26 08:43:04 +0100109 }
Harald Welte1be24f12015-12-26 23:44:28 +0100110 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
111 _sai = asn1str_to_u16(&ies->sai.sAC);
Harald Welte3aad6af2015-12-26 08:43:04 +0100112 sai = &_sai;
113 }
114 }
115
Harald Welte1be24f12015-12-26 23:44:28 +0100116 cur = msgb_put(msg, ies->nas_pdu.size);
117 memcpy(msg, ies->nas_pdu.buf, ies->nas_pdu.size);
Harald Welte3aad6af2015-12-26 08:43:04 +0100118 /* FIXME: set msgb_gmmh() */
119
120 /* FIXME: Feed into the MM/CC/SMS-CP layer */
Harald Welte1be24f12015-12-26 23:44:28 +0100121 //rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100122
123 return 0;
124}
125
126static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
127{
128 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
129 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
130 ranap_cause_str(&ies->cause));
131 else
132 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
133
134 return 0;
135}
136
Harald Welte0744d6f2015-12-26 23:45:18 +0100137static int ranap_handle_co_iu_rel_req(struct ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
138{
139 struct msgb *msg;
140 struct osmo_scu_prim *prim;
141
142 LOGP(DRANAP, LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
143 msg = ranap_new_msg_iu_rel_cmd(&ies->cause);
144 msg->l2h = msg->data;
145 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
146 prim->u.data.conn_id = ctx->conn_id;
147 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
148 OSMO_SCU_PRIM_N_DATA,
149 PRIM_OP_REQUEST, msg);
150 osmo_sua_user_link_down(ctx->link, &prim->oph);
151 return 0;
152}
153
Harald Welte3aad6af2015-12-26 08:43:04 +0100154/* Entry point for connection-oriented ANAP message */
Harald Welte8fa5d552016-01-05 15:01:53 +0100155static void cn_ranap_handle_co(void *ctx, ranap_message *message)
Harald Welte3aad6af2015-12-26 08:43:04 +0100156{
157 int rc = 0;
158
159 switch (message->direction) {
160 case RANAP_RANAP_PDU_PR_initiatingMessage:
161 switch (message->procedureCode) {
162 case RANAP_ProcedureCode_id_InitialUE_Message:
163 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
164 break;
165 case RANAP_ProcedureCode_id_DirectTransfer:
166 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
167 break;
168 case RANAP_ProcedureCode_id_ErrorIndication:
169 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
170 break;
Harald Welte0744d6f2015-12-26 23:45:18 +0100171 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
172 /* Iu Release Request */
173 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
174 break;
Harald Welte3aad6af2015-12-26 08:43:04 +0100175 }
176 break;
177 case RANAP_RANAP_PDU_PR_successfulOutcome:
178 switch (message->procedureCode) {
179 case RANAP_ProcedureCode_id_RAB_Assignment:
180 /* RAB Assignment Response */
181 break;
182 case RANAP_ProcedureCode_id_SecurityModeControl:
183 /* Security Mode Complete */
184 break;
185 case RANAP_ProcedureCode_id_Iu_Release:
186 /* Iu Release Complete */
187 break;
188 }
189 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
190 case RANAP_RANAP_PDU_PR_outcome:
191 default:
Harald Welte3aad6af2015-12-26 08:43:04 +0100192 break;
193 }
Harald Welte3aad6af2015-12-26 08:43:04 +0100194}
195
196static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
197{
198 /* FIXME: send reset response */
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100199
200 LOGP(DRANAP, LOGL_ERROR, "Rx Reset Request\n");
Harald Welte3aad6af2015-12-26 08:43:04 +0100201}
202
203static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
204{
205 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
206 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
207 ranap_cause_str(&ies->cause));
208 else
209 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
210
211 return 0;
212}
213
214/* Entry point for connection-less RANAP message */
Harald Welte8fa5d552016-01-05 15:01:53 +0100215static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
Harald Welte3aad6af2015-12-26 08:43:04 +0100216{
217 int rc = 0;
218
219 switch (message->direction) {
220 case RANAP_RANAP_PDU_PR_initiatingMessage:
221 switch (message->procedureCode) {
222 case RANAP_ProcedureCode_id_Reset:
223 /* received reset.req, send reset.resp */
224 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
225 break;
226 case RANAP_ProcedureCode_id_ErrorIndication:
227 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
228 break;
229 }
230 break;
231 case RANAP_RANAP_PDU_PR_successfulOutcome:
232 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
233 case RANAP_RANAP_PDU_PR_outcome:
234 default:
Harald Welte3aad6af2015-12-26 08:43:04 +0100235 break;
236 }
237}
238
239/***********************************************************************
240 *
241 ***********************************************************************/
242
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200243int tx_unitdata(struct osmo_sccp_link *link);
244int tx_conn_req(struct osmo_sccp_link *link, uint32_t conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100245
246struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
247struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
248
249struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
250{
251 struct msgb *msg = msgb_alloc(1024, "conn_resp");
252 struct osmo_scu_prim *prim;
253
254 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
255 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
256 OSMO_SCU_PRIM_N_CONNECT,
257 PRIM_OP_RESPONSE, msg);
258 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
259 return &prim->oph;
260}
261
262static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
263{
264 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
265 struct osmo_prim_hdr *resp = NULL;
266 const uint8_t payload[] = { 0xb1, 0xb2, 0xb3 };
267 int rc;
Harald Welte2bc20f82015-12-26 23:43:48 +0100268 struct ue_conn_ctx *ue;
Harald Welte3aad6af2015-12-26 08:43:04 +0100269
270 printf("sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
271
272 switch (OSMO_PRIM_HDR(oph)) {
273 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
274 /* confirmation of outbound connection */
275 break;
276 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
277 /* indication of new inbound connection request*/
278 printf("N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
279 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
280 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
281 LOGP(DMAIN, LOGL_NOTICE, "Received invalid N-CONNECT.ind\n");
282 return 0;
283 }
284 /* FIXME: allocate UE context */
Harald Welte2bc20f82015-12-26 23:43:48 +0100285 ue = ue_conn_ctx_alloc(link, prim->u.connect.conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100286 /* first ensure the local SUA/SCCP socket is ACTIVE */
287 resp = make_conn_resp(&prim->u.connect);
288 osmo_sua_user_link_down(link, resp);
289 /* then handle the RANAP payload */
Harald Welte8fa5d552016-01-05 15:01:53 +0100290 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Harald Welte3aad6af2015-12-26 08:43:04 +0100291 break;
292 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
293 /* indication of disconnect */
294 printf("N-DISCONNECT.ind(%u)\n", prim->u.disconnect.conn_id);
Harald Welte4e2783f2015-12-26 23:49:52 +0100295 ue = ue_conn_ctx_find(link, prim->u.disconnect.conn_id);
Harald Welte8fa5d552016-01-05 15:01:53 +0100296 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Harald Welte3aad6af2015-12-26 08:43:04 +0100297 break;
298 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
299 /* connection-oriented data received */
300 printf("N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
301 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte2bc20f82015-12-26 23:43:48 +0100302 /* resolve UE context */
303 ue = ue_conn_ctx_find(link, prim->u.data.conn_id);
Harald Welte8fa5d552016-01-05 15:01:53 +0100304 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Harald Welte3aad6af2015-12-26 08:43:04 +0100305 break;
306 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
307 /* connection-oriented data received */
308 printf("N-UNITDATA.ind(%s)\n",
309 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte8fa5d552016-01-05 15:01:53 +0100310 rc = ranap_cn_rx_cl(cn_ranap_handle_cl, link, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Harald Welte3aad6af2015-12-26 08:43:04 +0100311 break;
312 }
313
314 msgb_free(oph->msg);
315 return 0;
316}
317
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200318static void print_usage()
319{
320 printf("Usage: dummy-cn\n");
321}
322
323static void print_help()
324{
325 printf(" -h --help This text.\n");
326 printf(" -b --bind addr Bind to local IP address (default 127.0.0.1)\n");
327}
328
329static void handle_options(int argc, char **argv)
330{
331 while (1) {
332 int option_index = 0, c;
333 static struct option long_options[] = {
334 {"help", 0, 0, 'h'},
335 {"bind", 1, 0, 'b'},
336 {0, 0, 0, 0}
337 };
338
339 c = getopt_long(argc, argv, "hb:V",
340 long_options, &option_index);
341 if (c == -1)
342 break;
343
344 switch (c) {
345 case 'h':
346 print_usage();
347 print_help();
348 exit(0);
349 case 'b':
350 cmdline_bind_addr = optarg;
351 break;
352 default:
353 printf("Unknown cmdline argument (-h shows help)\n");
354 exit(1);
355 break;
356 }
357 }
358}
359
Harald Welte3aad6af2015-12-26 08:43:04 +0100360int main(int argc, char **argv)
361{
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200362 struct osmo_sccp_user *user;
Harald Welte3aad6af2015-12-26 08:43:04 +0100363 void *ctx = talloc_named_const(NULL, 1, "root");
364 int rc;
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200365 int port = 14001;
Harald Welte3aad6af2015-12-26 08:43:04 +0100366
367 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100368 ranap_set_log_area(DRANAP);
Harald Welte3aad6af2015-12-26 08:43:04 +0100369
370 test_common_init();
371
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200372 handle_options(argc, argv);
373
Harald Welte2ebe42f2015-12-26 23:38:38 +0100374 user = osmo_sua_user_create(ctx, sccp_sap_up, ctx);
Harald Welte3aad6af2015-12-26 08:43:04 +0100375
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200376 rc = osmo_sua_server_listen(user, cmdline_bind_addr, port);
Harald Welte3aad6af2015-12-26 08:43:04 +0100377 if (rc < 0) {
378 exit(1);
379 }
380
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200381 printf("dummy-cn listening on %s %d\n", cmdline_bind_addr, port);
382
Harald Welte3aad6af2015-12-26 08:43:04 +0100383 while (1) {
384 osmo_select_main(0);
385 }
386}