blob: 721b7c90c165d4d1ad49ea5dd41e0bd4dfa51fe1 [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
7#include <osmocom/core/select.h>
8#include <osmocom/core/prim.h>
9#include <osmocom/core/talloc.h>
10#include <osmocom/core/logging.h>
11#include <osmocom/core/application.h>
12#include <osmocom/vty/logging.h>
13
14#include <osmocom/sigtran/sua.h>
15#include <osmocom/sigtran/sccp_sap.h>
16
17#include "test_common.h"
18#include "ranap_common_cn.h"
19#include "hnbgw.h"
20
21int asn1_xer_print = 1;
22
Harald Welte2bc20f82015-12-26 23:43:48 +010023struct ue_conn_ctx {
24 struct llist_head list;
25 struct osmo_sua_link *link;
26 uint32_t conn_id;
27};
28
29static LLIST_HEAD(conn_ctx_list);
30
31struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sua_link *link, uint32_t conn_id)
32{
33 struct ue_conn_ctx *ctx = talloc_zero(NULL, struct ue_conn_ctx);
34
35 ctx->link = link;
36 ctx->conn_id = conn_id;
37 llist_add(&ctx->list, &conn_ctx_list);
38
39 return ctx;
40}
41
42struct ue_conn_ctx *ue_conn_ctx_find(struct osmo_sua_link *link, uint32_t conn_id)
43{
44 struct ue_conn_ctx *ctx;
45
46 llist_for_each_entry(ctx, &conn_ctx_list, list) {
47 if (ctx->link == link && ctx->conn_id == conn_id)
48 return ctx;
49 }
50 return NULL;
51}
52
Harald Welte3aad6af2015-12-26 08:43:04 +010053/***********************************************************************
54 * RANAP handling
55 ***********************************************************************/
56
57static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MEssageIEs_t *ies)
58{
59 struct gprs_ra_id ra_id;
60 uint16_t sai;
61 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
62 uint8_t *cur;
63
64 ranap_parse_lai(&ra_id, &ies.lai);
65 sai = asn1str_to_u16(&ies.sai.sAC);
66 cur = msgb_put(msg, ies.nas_pdu.size);
67 memcpy(msg, ies.nas_pdu.buf, ies.nas_pdu.size);
68 /* FIXME: set msgb_gmmh() */
69
70 /* FIXME: Feed into the MM layer */
71 rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id);
72
73 return 0;
74}
75
76static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
77{
78 struct gprs_ra_id _ra_id, *ra_id = NULL;
79 uint16_t _sai, *sai = NULL;
80 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
81 uint8_t *cur;
82
83 if (ies.presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
84 ranap_parse_lai(&_ra_id, &ies.lai);
85 ra_id = &_ra_id;
86 if (ies.presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
87 _ra_id.rac = asn1str_to_u16(&ies.rac);
88 }
89 if (ies.presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
90 _sai = asn1str_to_u16(&ies.sai.sAC);
91 sai = &_sai;
92 }
93 }
94
95 cur = msgb_put(msg, ies.nas_pdu.size);
96 memcpy(msg, ies.nas_pdu.buf, ies.nas_pdu.size);
97 /* FIXME: set msgb_gmmh() */
98
99 /* FIXME: Feed into the MM/CC/SMS-CP layer */
100 rc = gsm0408_gprs_rcvmsg_iu(msg, ra_id, sai, conn_id);
101
102 return 0;
103}
104
105static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
106{
107 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
108 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
109 ranap_cause_str(&ies->cause));
110 else
111 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
112
113 return 0;
114}
115
116/* Entry point for connection-oriented ANAP message */
117int cn_ranap_handle_co(void *ctx, ranap_message *message)
118{
119 int rc = 0;
120
121 switch (message->direction) {
122 case RANAP_RANAP_PDU_PR_initiatingMessage:
123 switch (message->procedureCode) {
124 case RANAP_ProcedureCode_id_InitialUE_Message:
125 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
126 break;
127 case RANAP_ProcedureCode_id_DirectTransfer:
128 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
129 break;
130 case RANAP_ProcedureCode_id_ErrorIndication:
131 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
132 break;
133 }
134 break;
135 case RANAP_RANAP_PDU_PR_successfulOutcome:
136 switch (message->procedureCode) {
137 case RANAP_ProcedureCode_id_RAB_Assignment:
138 /* RAB Assignment Response */
139 break;
140 case RANAP_ProcedureCode_id_SecurityModeControl:
141 /* Security Mode Complete */
142 break;
143 case RANAP_ProcedureCode_id_Iu_Release:
144 /* Iu Release Complete */
145 break;
146 }
147 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
148 case RANAP_RANAP_PDU_PR_outcome:
149 default:
150 rc = -1;
151 break;
152 }
153
154 return rc;
155}
156
157static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
158{
159 /* FIXME: send reset response */
160}
161
162static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
163{
164 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
165 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
166 ranap_cause_str(&ies->cause));
167 else
168 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
169
170 return 0;
171}
172
173/* Entry point for connection-less RANAP message */
174int cn_ranap_handle_cl(void *ctx, ranap_message *message)
175{
176 int rc = 0;
177
178 switch (message->direction) {
179 case RANAP_RANAP_PDU_PR_initiatingMessage:
180 switch (message->procedureCode) {
181 case RANAP_ProcedureCode_id_Reset:
182 /* received reset.req, send reset.resp */
183 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
184 break;
185 case RANAP_ProcedureCode_id_ErrorIndication:
186 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
187 break;
188 }
189 break;
190 case RANAP_RANAP_PDU_PR_successfulOutcome:
191 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
192 case RANAP_RANAP_PDU_PR_outcome:
193 default:
194 rc = -1;
195 break;
196 }
197}
198
199/***********************************************************************
200 *
201 ***********************************************************************/
202
203int tx_unitdata(struct osmo_sua_link *link);
204int tx_conn_req(struct osmo_sua_link *link, uint32_t conn_id);
205
206struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
207struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
208
209struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
210{
211 struct msgb *msg = msgb_alloc(1024, "conn_resp");
212 struct osmo_scu_prim *prim;
213
214 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
215 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
216 OSMO_SCU_PRIM_N_CONNECT,
217 PRIM_OP_RESPONSE, msg);
218 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
219 return &prim->oph;
220}
221
222static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
223{
224 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
225 struct osmo_prim_hdr *resp = NULL;
226 const uint8_t payload[] = { 0xb1, 0xb2, 0xb3 };
227 int rc;
Harald Welte2bc20f82015-12-26 23:43:48 +0100228 struct ue_conn_ctx *ue;
Harald Welte3aad6af2015-12-26 08:43:04 +0100229
230 printf("sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
231
232 switch (OSMO_PRIM_HDR(oph)) {
233 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
234 /* confirmation of outbound connection */
235 break;
236 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
237 /* indication of new inbound connection request*/
238 printf("N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
239 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
240 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
241 LOGP(DMAIN, LOGL_NOTICE, "Received invalid N-CONNECT.ind\n");
242 return 0;
243 }
244 /* FIXME: allocate UE context */
Harald Welte2bc20f82015-12-26 23:43:48 +0100245 ue = ue_conn_ctx_alloc(link, prim->u.connect.conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100246 /* first ensure the local SUA/SCCP socket is ACTIVE */
247 resp = make_conn_resp(&prim->u.connect);
248 osmo_sua_user_link_down(link, resp);
249 /* then handle the RANAP payload */
250 rc = cn_ranap_rx_co(ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
251 break;
252 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
253 /* indication of disconnect */
254 printf("N-DISCONNECT.ind(%u)\n", prim->u.disconnect.conn_id);
255 break;
256 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
257 /* connection-oriented data received */
258 printf("N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
259 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte2bc20f82015-12-26 23:43:48 +0100260 /* resolve UE context */
261 ue = ue_conn_ctx_find(link, prim->u.data.conn_id);
Harald Welte3aad6af2015-12-26 08:43:04 +0100262 rc = cn_ranap_rx_co(ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
263 break;
264 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
265 /* connection-oriented data received */
266 printf("N-UNITDATA.ind(%s)\n",
267 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
268 rc = cn_ranap_rx_cl(link, msgb_l2(oph->msg), msgb_l2len(oph->msg));
269 break;
270 }
271
272 msgb_free(oph->msg);
273 return 0;
274}
275
276int main(int argc, char **argv)
277{
278 struct osmo_sua_user *user;
279 void *ctx = talloc_named_const(NULL, 1, "root");
280 int rc;
281
282 osmo_sua_set_log_area(DSUA);
283
284 test_common_init();
285
Harald Welte2ebe42f2015-12-26 23:38:38 +0100286 user = osmo_sua_user_create(ctx, sccp_sap_up, ctx);
Harald Welte3aad6af2015-12-26 08:43:04 +0100287
288 rc = osmo_sua_server_listen(user, "127.0.0.1", 14001);
289 if (rc < 0) {
290 exit(1);
291 }
292
293 while (1) {
294 osmo_select_main(0);
295 }
296}