blob: 6e1456276876104ea6059ea718b014d1e027390e [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
Neels Hofmeyr3d127482016-04-20 11:04:49 +020029const char *cmdline_bind_addr = "127.0.0.1";
Harald Welte3aad6af2015-12-26 08:43:04 +010030
Harald Welte2bc20f82015-12-26 23:43:48 +010031struct ue_conn_ctx {
32 struct llist_head list;
Neels Hofmeyr0a437222016-07-06 15:58:48 +020033 struct osmo_sccp_link *link;
Harald Welte2bc20f82015-12-26 23:43:48 +010034 uint32_t conn_id;
35};
36
37static LLIST_HEAD(conn_ctx_list);
38
Neels Hofmeyr0a437222016-07-06 15:58:48 +020039struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_link *link, uint32_t conn_id)
Harald Welte2bc20f82015-12-26 23:43:48 +010040{
41 struct ue_conn_ctx *ctx = talloc_zero(NULL, struct ue_conn_ctx);
42
43 ctx->link = link;
44 ctx->conn_id = conn_id;
45 llist_add(&ctx->list, &conn_ctx_list);
46
47 return ctx;
48}
49
Neels Hofmeyr0a437222016-07-06 15:58:48 +020050struct ue_conn_ctx *ue_conn_ctx_find(struct osmo_sccp_link *link, uint32_t conn_id)
Harald Welte2bc20f82015-12-26 23:43:48 +010051{
52 struct ue_conn_ctx *ctx;
53
54 llist_for_each_entry(ctx, &conn_ctx_list, list) {
55 if (ctx->link == link && ctx->conn_id == conn_id)
56 return ctx;
57 }
58 return NULL;
59}
60
Harald Welte3aad6af2015-12-26 08:43:04 +010061/***********************************************************************
62 * RANAP handling
63 ***********************************************************************/
64
Daniel Willmannb39bad12016-01-14 15:36:12 +010065static int ranap_handle_co_initial_ue(struct ue_conn_ctx *ctx, RANAP_InitialUE_MessageIEs_t *ies)
Harald Welte3aad6af2015-12-26 08:43:04 +010066{
67 struct gprs_ra_id ra_id;
68 uint16_t sai;
69 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
70 uint8_t *cur;
Daniel Willmannb39bad12016-01-14 15:36:12 +010071 struct osmo_scu_prim *prim;
Harald Welte3aad6af2015-12-26 08:43:04 +010072
Harald Welte1be24f12015-12-26 23:44:28 +010073 ranap_parse_lai(&ra_id, &ies->lai);
74 sai = asn1str_to_u16(&ies->sai.sAC);
75 cur = msgb_put(msg, ies->nas_pdu.size);
76 memcpy(msg, ies->nas_pdu.buf, ies->nas_pdu.size);
Daniel Willmannb39bad12016-01-14 15:36:12 +010077 msgb_free(msg);
Harald Welte3aad6af2015-12-26 08:43:04 +010078 /* FIXME: set msgb_gmmh() */
79
80 /* FIXME: Feed into the MM layer */
Harald Welte1be24f12015-12-26 23:44:28 +010081 //rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +010082
Daniel Willmannb39bad12016-01-14 15:36:12 +010083 msg = ranap_new_msg_dt(0, NULL, 0);
84
85 msg->l2h = msg->data;
86 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
87 prim->u.data.conn_id = ctx->conn_id;
88 osmo_prim_init(&prim->oph, SCCP_SAP_USER, OSMO_SCU_PRIM_N_DATA,
89 PRIM_OP_REQUEST, msg);
90
91 osmo_sua_user_link_down(ctx->link, &prim->oph);
92
Harald Welte3aad6af2015-12-26 08:43:04 +010093 return 0;
94}
95
96static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
97{
98 struct gprs_ra_id _ra_id, *ra_id = NULL;
99 uint16_t _sai, *sai = NULL;
100 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
101 uint8_t *cur;
102
Harald Welte1be24f12015-12-26 23:44:28 +0100103 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
104 ranap_parse_lai(&_ra_id, &ies->lai);
Harald Welte3aad6af2015-12-26 08:43:04 +0100105 ra_id = &_ra_id;
Harald Welte1be24f12015-12-26 23:44:28 +0100106 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
107 _ra_id.rac = asn1str_to_u8(&ies->rac);
Harald Welte3aad6af2015-12-26 08:43:04 +0100108 }
Harald Welte1be24f12015-12-26 23:44:28 +0100109 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
110 _sai = asn1str_to_u16(&ies->sai.sAC);
Harald Welte3aad6af2015-12-26 08:43:04 +0100111 sai = &_sai;
112 }
113 }
114
Harald Welte1be24f12015-12-26 23:44:28 +0100115 cur = msgb_put(msg, ies->nas_pdu.size);
116 memcpy(msg, ies->nas_pdu.buf, ies->nas_pdu.size);
Harald Welte3aad6af2015-12-26 08:43:04 +0100117 /* FIXME: set msgb_gmmh() */
118
119 /* FIXME: Feed into the MM/CC/SMS-CP layer */
Harald Welte1be24f12015-12-26 23:44:28 +0100120 //rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100121
122 return 0;
123}
124
125static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
126{
127 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
128 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
129 ranap_cause_str(&ies->cause));
130 else
131 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
132
133 return 0;
134}
135
Harald Welte0744d6f2015-12-26 23:45:18 +0100136static int ranap_handle_co_iu_rel_req(struct ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
137{
138 struct msgb *msg;
139 struct osmo_scu_prim *prim;
140
141 LOGP(DRANAP, LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
142 msg = ranap_new_msg_iu_rel_cmd(&ies->cause);
143 msg->l2h = msg->data;
144 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
145 prim->u.data.conn_id = ctx->conn_id;
146 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
147 OSMO_SCU_PRIM_N_DATA,
148 PRIM_OP_REQUEST, msg);
149 osmo_sua_user_link_down(ctx->link, &prim->oph);
150 return 0;
151}
152
Harald Welte3aad6af2015-12-26 08:43:04 +0100153/* Entry point for connection-oriented ANAP message */
Harald Welte8fa5d552016-01-05 15:01:53 +0100154static void cn_ranap_handle_co(void *ctx, ranap_message *message)
Harald Welte3aad6af2015-12-26 08:43:04 +0100155{
156 int rc = 0;
157
158 switch (message->direction) {
159 case RANAP_RANAP_PDU_PR_initiatingMessage:
160 switch (message->procedureCode) {
161 case RANAP_ProcedureCode_id_InitialUE_Message:
162 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
163 break;
164 case RANAP_ProcedureCode_id_DirectTransfer:
165 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
166 break;
167 case RANAP_ProcedureCode_id_ErrorIndication:
168 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
169 break;
Harald Welte0744d6f2015-12-26 23:45:18 +0100170 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
171 /* Iu Release Request */
172 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
173 break;
Harald Welte3aad6af2015-12-26 08:43:04 +0100174 }
175 break;
176 case RANAP_RANAP_PDU_PR_successfulOutcome:
177 switch (message->procedureCode) {
178 case RANAP_ProcedureCode_id_RAB_Assignment:
179 /* RAB Assignment Response */
180 break;
181 case RANAP_ProcedureCode_id_SecurityModeControl:
182 /* Security Mode Complete */
183 break;
184 case RANAP_ProcedureCode_id_Iu_Release:
185 /* Iu Release Complete */
186 break;
187 }
188 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
189 case RANAP_RANAP_PDU_PR_outcome:
190 default:
Harald Welte3aad6af2015-12-26 08:43:04 +0100191 break;
192 }
Harald Welte3aad6af2015-12-26 08:43:04 +0100193}
194
195static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
196{
197 /* FIXME: send reset response */
Neels Hofmeyr0968a582016-01-11 15:19:38 +0100198
199 LOGP(DRANAP, LOGL_ERROR, "Rx Reset Request\n");
Harald Welte3aad6af2015-12-26 08:43:04 +0100200}
201
202static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
203{
204 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
205 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
206 ranap_cause_str(&ies->cause));
207 else
208 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
209
210 return 0;
211}
212
213/* Entry point for connection-less RANAP message */
Harald Welte8fa5d552016-01-05 15:01:53 +0100214static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
Harald Welte3aad6af2015-12-26 08:43:04 +0100215{
216 int rc = 0;
217
218 switch (message->direction) {
219 case RANAP_RANAP_PDU_PR_initiatingMessage:
220 switch (message->procedureCode) {
221 case RANAP_ProcedureCode_id_Reset:
222 /* received reset.req, send reset.resp */
223 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
224 break;
225 case RANAP_ProcedureCode_id_ErrorIndication:
226 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
227 break;
228 }
229 break;
230 case RANAP_RANAP_PDU_PR_successfulOutcome:
231 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
232 case RANAP_RANAP_PDU_PR_outcome:
233 default:
Harald Welte3aad6af2015-12-26 08:43:04 +0100234 break;
235 }
236}
237
238/***********************************************************************
239 *
240 ***********************************************************************/
241
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200242int tx_unitdata(struct osmo_sccp_link *link);
243int tx_conn_req(struct osmo_sccp_link *link, uint32_t conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100244
245struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
246struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
247
248struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
249{
250 struct msgb *msg = msgb_alloc(1024, "conn_resp");
251 struct osmo_scu_prim *prim;
252
253 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
254 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
255 OSMO_SCU_PRIM_N_CONNECT,
256 PRIM_OP_RESPONSE, msg);
257 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
258 return &prim->oph;
259}
260
261static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
262{
263 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
264 struct osmo_prim_hdr *resp = NULL;
265 const uint8_t payload[] = { 0xb1, 0xb2, 0xb3 };
266 int rc;
Harald Welte2bc20f82015-12-26 23:43:48 +0100267 struct ue_conn_ctx *ue;
Harald Welte3aad6af2015-12-26 08:43:04 +0100268
269 printf("sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
270
271 switch (OSMO_PRIM_HDR(oph)) {
272 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
273 /* confirmation of outbound connection */
274 break;
275 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
276 /* indication of new inbound connection request*/
277 printf("N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
278 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
279 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
280 LOGP(DMAIN, LOGL_NOTICE, "Received invalid N-CONNECT.ind\n");
281 return 0;
282 }
283 /* FIXME: allocate UE context */
Harald Welte2bc20f82015-12-26 23:43:48 +0100284 ue = ue_conn_ctx_alloc(link, prim->u.connect.conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100285 /* first ensure the local SUA/SCCP socket is ACTIVE */
286 resp = make_conn_resp(&prim->u.connect);
287 osmo_sua_user_link_down(link, resp);
288 /* then handle the RANAP payload */
Harald Welte8fa5d552016-01-05 15:01:53 +0100289 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 +0100290 break;
291 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
292 /* indication of disconnect */
293 printf("N-DISCONNECT.ind(%u)\n", prim->u.disconnect.conn_id);
Harald Welte4e2783f2015-12-26 23:49:52 +0100294 ue = ue_conn_ctx_find(link, prim->u.disconnect.conn_id);
Harald Welte8fa5d552016-01-05 15:01:53 +0100295 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 +0100296 break;
297 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
298 /* connection-oriented data received */
299 printf("N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
300 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte2bc20f82015-12-26 23:43:48 +0100301 /* resolve UE context */
302 ue = ue_conn_ctx_find(link, prim->u.data.conn_id);
Harald Welte8fa5d552016-01-05 15:01:53 +0100303 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 +0100304 break;
305 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
306 /* connection-oriented data received */
Pau Espin Pedrol6f66feb2020-05-18 10:33:50 +0200307 printf("N-UNITDATA.ind(%s)\n",
Harald Welte3aad6af2015-12-26 08:43:04 +0100308 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte8fa5d552016-01-05 15:01:53 +0100309 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 +0100310 break;
311 }
312
313 msgb_free(oph->msg);
314 return 0;
315}
316
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200317static void print_usage()
318{
319 printf("Usage: dummy-cn\n");
320}
321
322static void print_help()
323{
324 printf(" -h --help This text.\n");
325 printf(" -b --bind addr Bind to local IP address (default 127.0.0.1)\n");
326}
327
328static void handle_options(int argc, char **argv)
329{
330 while (1) {
331 int option_index = 0, c;
332 static struct option long_options[] = {
333 {"help", 0, 0, 'h'},
334 {"bind", 1, 0, 'b'},
335 {0, 0, 0, 0}
336 };
337
338 c = getopt_long(argc, argv, "hb:V",
339 long_options, &option_index);
340 if (c == -1)
341 break;
342
343 switch (c) {
344 case 'h':
345 print_usage();
346 print_help();
347 exit(0);
348 case 'b':
349 cmdline_bind_addr = optarg;
350 break;
351 default:
352 printf("Unknown cmdline argument (-h shows help)\n");
353 exit(1);
354 break;
355 }
356 }
357}
358
Harald Welte3aad6af2015-12-26 08:43:04 +0100359int main(int argc, char **argv)
360{
Neels Hofmeyr0a437222016-07-06 15:58:48 +0200361 struct osmo_sccp_user *user;
Harald Welte3aad6af2015-12-26 08:43:04 +0100362 void *ctx = talloc_named_const(NULL, 1, "root");
363 int rc;
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200364 int port = 14001;
Harald Welte3aad6af2015-12-26 08:43:04 +0100365
Pau Espin Pedrol4f356652020-05-18 10:34:43 +0200366 asn1_xer_print = 1;
367
Harald Welte3aad6af2015-12-26 08:43:04 +0100368 osmo_sua_set_log_area(DSUA);
Harald Weltebdf3fd12016-01-03 17:04:09 +0100369 ranap_set_log_area(DRANAP);
Harald Welte3aad6af2015-12-26 08:43:04 +0100370
371 test_common_init();
372
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200373 handle_options(argc, argv);
374
Harald Welte2ebe42f2015-12-26 23:38:38 +0100375 user = osmo_sua_user_create(ctx, sccp_sap_up, ctx);
Harald Welte3aad6af2015-12-26 08:43:04 +0100376
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200377 rc = osmo_sua_server_listen(user, cmdline_bind_addr, port);
Harald Welte3aad6af2015-12-26 08:43:04 +0100378 if (rc < 0) {
379 exit(1);
380 }
381
Neels Hofmeyr3d127482016-04-20 11:04:49 +0200382 printf("dummy-cn listening on %s %d\n", cmdline_bind_addr, port);
383
Harald Welte3aad6af2015-12-26 08:43:04 +0100384 while (1) {
385 osmo_select_main(0);
386 }
387}