blob: 3ee900c149c678837aa8e054346b314229508339 [file] [log] [blame]
Neels Hofmeyraae68b22017-07-05 14:38:52 +02001/* Common parts of IuCS and IuPS interfaces implementation */
2
3/* (C) 2016-2017 by sysmocom s.f.m.c. GmbH <info@sysmocom.de>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21#include <stdint.h>
22#include <unistd.h>
23#include <stdlib.h>
24#include <stdio.h>
25#include <string.h>
26#include <stdbool.h>
27
Harald Welteda86fe52017-11-21 08:14:37 +010028#include <asn1c/asn1helpers.h>
29
Neels Hofmeyraae68b22017-07-05 14:38:52 +020030#include <osmocom/ranap/iu_client.h>
31
32#include <osmocom/core/logging.h>
33#include <osmocom/crypt/auth.h>
34#include <osmocom/gprs/gprs_msgb.h>
35#include <osmocom/sigtran/sccp_sap.h>
Harald Welteda86fe52017-11-21 08:14:37 +010036#include <osmocom/sigtran/sccp_helpers.h>
37#include <osmocom/ranap/ranap_common_cn.h>
Neels Hofmeyraae68b22017-07-05 14:38:52 +020038#include <osmocom/ranap/ranap_ies_defs.h>
39#include <osmocom/ranap/ranap_msg_factory.h>
40
41/* Parsed global RNC id. See also struct RANAP_GlobalRNC_ID, and note that the
42 * PLMN identity is a BCD representation of the MCC and MNC.
43 * See iu_grnc_id_parse(). */
44struct iu_grnc_id {
45 uint16_t mcc;
46 uint16_t mnc;
47 uint16_t rnc_id;
48};
49
50/* A remote RNC (Radio Network Controller, like BSC but for UMTS) that has
51 * called us and is currently reachable at the given osmo_sccp_addr. So, when we
52 * know a LAC for a subscriber, we can page it at the RNC matching that LAC or
53 * RAC. An HNB-GW typically presents itself as if it were a single RNC, even
54 * though it may have several RNCs in hNodeBs connected to it. Those will then
55 * share the same RNC id, which they actually receive and adopt from the HNB-GW
56 * in the HNBAP HNB REGISTER ACCEPT message. */
57struct ranap_iu_rnc {
58 struct llist_head entry;
59
60 uint16_t rnc_id;
61 uint16_t lac; /* Location Area Code (used for CS and PS) */
62 uint8_t rac; /* Routing Area Code (used for PS only) */
63 struct osmo_sccp_addr sccp_addr;
64};
65
66void *talloc_iu_ctx;
67void *talloc_asn1_ctx;
68
69/* Implement the extern asn_debug from libasn1c to indicate whether to print
70 * asn.1 debug messages (see libasn1c). */
71int asn_debug = 0;
72
73/* Implement the extern asn1_xer_print to indicate whether the ASN.1 binary
74 * code decoded and encoded during Iu communication should be logged to stderr
75 * (see asn.1 generated code in osmo-iuh). */
76int asn1_xer_print = 0;
77
78ranap_iu_recv_cb_t global_iu_recv_cb = NULL;
79ranap_iu_event_cb_t global_iu_event_cb = NULL;
80int iu_log_subsystem = 0;
81
82#define LOGPIU(level, fmt, args...) \
83 LOGP(iu_log_subsystem, level, fmt, ## args)
84
85static LLIST_HEAD(ue_conn_ctx_list);
86static LLIST_HEAD(rnc_list);
87
88static struct osmo_sccp_instance *g_sccp;
89static struct osmo_sccp_user *g_scu;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +020090static struct osmo_sccp_addr g_local_sccp_addr;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020091
92const struct value_string ranap_iu_event_type_names[] = {
93 OSMO_VALUE_STRING(RANAP_IU_EVENT_RAB_ASSIGN),
94 OSMO_VALUE_STRING(RANAP_IU_EVENT_SECURITY_MODE_COMPLETE),
95 OSMO_VALUE_STRING(RANAP_IU_EVENT_IU_RELEASE),
96 OSMO_VALUE_STRING(RANAP_IU_EVENT_LINK_INVALIDATED),
97 { 0, NULL }
98};
99
100static struct ranap_ue_conn_ctx *ue_conn_ctx_alloc(struct ranap_iu_rnc *rnc, uint32_t conn_id)
101{
102 struct ranap_ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ranap_ue_conn_ctx);
103
104 ctx->rnc = rnc;
105 ctx->conn_id = conn_id;
106 llist_add(&ctx->list, &ue_conn_ctx_list);
107
108 return ctx;
109}
110
111static struct ranap_ue_conn_ctx *ue_conn_ctx_find(uint32_t conn_id)
112{
113 struct ranap_ue_conn_ctx *ctx;
114
115 llist_for_each_entry(ctx, &ue_conn_ctx_list, list) {
116 if (ctx->conn_id == conn_id)
117 return ctx;
118 }
119 return NULL;
120}
121
122static struct ranap_iu_rnc *iu_rnc_alloc(uint16_t rnc_id, uint16_t lac, uint8_t rac,
123 struct osmo_sccp_addr *addr)
124{
125 struct ranap_iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct ranap_iu_rnc);
126
127 rnc->rnc_id = rnc_id;
128 rnc->lac = lac;
129 rnc->rac = rac;
130 rnc->sccp_addr = *addr;
131 llist_add(&rnc->entry, &rnc_list);
132
133 LOGPIU(LOGL_NOTICE, "New RNC %d (LAC=%d RAC=%d)\n",
134 rnc->rnc_id, rnc->lac, rnc->rac);
135
136 return rnc;
137}
138
139static struct ranap_iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac,
140 uint8_t rac, struct osmo_sccp_addr *addr)
141{
142 struct ranap_iu_rnc *rnc;
143 llist_for_each_entry(rnc, &rnc_list, entry) {
144 if (rnc->rnc_id != rnc_id)
145 continue;
146
147 /* We have this RNC Id registered already. Make sure that the
148 * details match. */
149
150 /* TODO should a mismatch be an error? */
151 if (rnc->lac != lac || rnc->rac != rac)
152 LOGPIU(LOGL_NOTICE, "RNC %d changes its details:"
153 " LAC=%d RAC=%d --> LAC=%d RAC=%d\n",
154 rnc->rnc_id, rnc->lac, rnc->rac,
155 lac, rac);
156 rnc->lac = lac;
157 rnc->rac = rac;
158
159 if (addr && memcmp(&rnc->sccp_addr, addr, sizeof(*addr)))
160 LOGPIU(LOGL_NOTICE, "RNC %d on New SCCP Addr %s"
161 " (LAC=%d RAC=%d)\n",
162 rnc->rnc_id, osmo_sccp_addr_dump(addr), rnc->lac, rnc->rac);
163 rnc->sccp_addr = *addr;
164 return rnc;
165 }
166
167 /* Not found, make a new one. */
168 return iu_rnc_alloc(rnc_id, lac, rac, addr);
169}
170
171/***********************************************************************
172 * RANAP handling
173 ***********************************************************************/
174
175int ranap_iu_rab_act(struct ranap_ue_conn_ctx *ue_ctx, struct msgb *msg)
176{
177 struct osmo_scu_prim *prim;
178
179 /* wrap RANAP message in SCCP N-DATA.req */
180 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
181 prim->u.data.conn_id = ue_ctx->conn_id;
182 osmo_prim_init(&prim->oph,
183 SCCP_SAP_USER,
184 OSMO_SCU_PRIM_N_DATA,
185 PRIM_OP_REQUEST,
186 msg);
187 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
188}
189
190int ranap_iu_rab_deact(struct ranap_ue_conn_ctx *ue_ctx, uint8_t rab_id)
191{
192 /* FIXME */
193 return -1;
194}
195
196int ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
197 int send_ck, int new_key)
198{
199 struct osmo_scu_prim *prim;
200 struct msgb *msg;
201
202 /* create RANAP message */
203 msg = ranap_new_msg_sec_mod_cmd(vec->ik, send_ck? vec->ck : NULL,
204 new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old);
205 msg->l2h = msg->data;
206 /* wrap RANAP message in SCCP N-DATA.req */
207 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
208 prim->u.data.conn_id = uectx->conn_id;
209 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
210 OSMO_SCU_PRIM_N_DATA,
211 PRIM_OP_REQUEST, msg);
212 osmo_sccp_user_sap_down(g_scu, &prim->oph);
213
214 return 0;
215}
216
217int ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *uectx, const char *imsi)
218{
219 struct msgb *msg;
220 struct osmo_scu_prim *prim;
221
222 LOGPIU(LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n",
223 uectx->conn_id);
224
225 msg = ranap_new_msg_common_id(imsi);
226 msg->l2h = msg->data;
227 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
228 prim->u.data.conn_id = uectx->conn_id;
229 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
230 OSMO_SCU_PRIM_N_DATA,
231 PRIM_OP_REQUEST, msg);
232 osmo_sccp_user_sap_down(g_scu, &prim->oph);
233 return 0;
234}
235
236static int iu_grnc_id_parse(struct iu_grnc_id *dst, struct RANAP_GlobalRNC_ID *src)
237{
238 /* The size is coming from arbitrary sender, check it gracefully */
239 if (src->pLMNidentity.size != 3) {
240 LOGPIU(LOGL_ERROR, "Invalid PLMN Identity size:"
241 " should be 3, is %d\n", src->pLMNidentity.size);
242 return -1;
243 }
244 gsm48_mcc_mnc_from_bcd(&src->pLMNidentity.buf[0],
245 &dst->mcc, &dst->mnc);
246 dst->rnc_id = (uint16_t)src->rNC_ID;
247 return 0;
248}
249
250#if 0
251 -- not used at present --
252static int iu_grnc_id_compose(struct iu_grnc_id *src, struct RANAP_GlobalRNC_ID *dst)
253{
254 /* The caller must ensure proper size */
255 OSMO_ASSERT(dst->pLMNidentity.size == 3);
256 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
257 src->mcc, src->mnc);
258 dst->rNC_ID = src->rnc_id;
259 return 0;
260}
261#endif
262
263struct new_ue_conn_ctx {
264 struct osmo_sccp_addr sccp_addr;
265 uint32_t conn_id;
266};
267
268static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
269{
270 struct new_ue_conn_ctx *new_ctx = ctx;
271 struct gprs_ra_id ra_id;
272 struct iu_grnc_id grnc_id;
273 uint16_t sai;
274 struct ranap_ue_conn_ctx *ue;
275 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
276 struct ranap_iu_rnc *rnc;
277
278 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
279 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
280 return -1;
281 }
282
283 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
284 ra_id.rac = asn1str_to_u8(&ies->rac);
285 }
286
287 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
288 LOGPIU(LOGL_ERROR,
289 "Failed to parse RANAP Global-RNC-ID IE\n");
290 return -1;
291 }
292
293 sai = asn1str_to_u16(&ies->sai.sAC);
294 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
295 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
296
297 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
298 rnc = iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &new_ctx->sccp_addr);
299
300 ue = ue_conn_ctx_alloc(rnc, new_ctx->conn_id);
301 OSMO_ASSERT(ue);
302 ue->ra_id = ra_id;
303
304 /* Feed into the MM layer */
305 msg->dst = ue;
306 global_iu_recv_cb(msg, &ra_id, &sai);
307
308 msgb_free(msg);
309
310 return 0;
311}
312
313static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
314{
315 struct gprs_ra_id _ra_id, *ra_id = NULL;
316 uint16_t _sai, *sai = NULL;
317 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
318
319 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
320 if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) {
321 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
322 return -1;
323 }
324 ra_id = &_ra_id;
325 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
326 _ra_id.rac = asn1str_to_u8(&ies->rac);
327 }
328 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
329 _sai = asn1str_to_u16(&ies->sai.sAC);
330 sai = &_sai;
331 }
332 }
333
334 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
335 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
336
337 /* Feed into the MM/CC/SMS-CP layer */
338 msg->dst = ctx;
339 global_iu_recv_cb(msg, ra_id, sai);
340
341 msgb_free(msg);
342
343 return 0;
344}
345
346static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
347{
348 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
349 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
350 ranap_cause_str(&ies->cause));
351 else
352 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
353
354 return 0;
355}
356
357int ranap_iu_tx(struct msgb *msg_nas, uint8_t sapi)
358{
359 struct ranap_ue_conn_ctx *uectx = msg_nas->dst;
360 struct msgb *msg;
361 struct osmo_scu_prim *prim;
362
363 LOGPIU(LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n",
364 uectx->conn_id);
365
366 msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas));
367 msgb_free(msg_nas);
368 msg->l2h = msg->data;
369 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
370 prim->u.data.conn_id = uectx->conn_id;
371 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
372 OSMO_SCU_PRIM_N_DATA,
373 PRIM_OP_REQUEST, msg);
374 osmo_sccp_user_sap_down(g_scu, &prim->oph);
375 return 0;
376}
377
378/* Send Iu Release for the given UE connection.
379 * If cause is NULL, the standard "No remaining RAB" cause is sent, otherwise
380 * the provided cause. */
381int ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
382{
383 struct msgb *msg;
384 struct osmo_scu_prim *prim;
385 static const struct RANAP_Cause default_cause = {
386 .present = RANAP_Cause_PR_radioNetwork,
387 .choice.radioNetwork = RANAP_CauseRadioNetwork_no_remaining_rab,
388 };
389
390 if (!cause)
391 cause = &default_cause;
392
393 msg = ranap_new_msg_iu_rel_cmd(cause);
394 msg->l2h = msg->data;
395 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
396 prim->u.data.conn_id = ctx->conn_id;
397 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
398 OSMO_SCU_PRIM_N_DATA,
399 PRIM_OP_REQUEST, msg);
400 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
401}
402
403static int ranap_handle_co_iu_rel_req(struct ranap_ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
404{
405 LOGPIU(LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
406 ranap_iu_tx_release(ctx, &ies->cause);
407 return 0;
408}
409
410static int ranap_handle_co_rab_ass_resp(struct ranap_ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
411{
412 int rc = -1;
413
414 LOGPIU(LOGL_INFO,
415 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
416 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
417 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
418 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
419 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
420
421 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
422 if (rc) {
423 LOGPIU(LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
424 return rc;
425 }
426
427 rc = global_iu_event_cb(ctx, RANAP_IU_EVENT_RAB_ASSIGN, &setup_ies);
428
429 ranap_free_rab_setupormodifieditemies(&setup_ies);
430 }
431 /* FIXME: handle RAB Ass failure? */
432
433 return rc;
434}
435
436static void cn_ranap_handle_co_initial(void *ctx, ranap_message *message)
437{
438 int rc;
439
440 LOGPIU(LOGL_NOTICE, "handle_co_initial(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
441
442 if (message->direction != RANAP_RANAP_PDU_PR_initiatingMessage
443 || message->procedureCode != RANAP_ProcedureCode_id_InitialUE_Message) {
444 LOGPIU(LOGL_ERROR, "Expected direction 'InitiatingMessage',"
445 " procedureCode 'InitialUE_Message', instead got %u and %u\n",
446 message->direction, message->procedureCode);
447 rc = -1;
448 }
449 else
450 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
451
452 if (rc) {
453 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
454 /* TODO handling of the error? */
455 }
456}
457
458/* Entry point for connection-oriented RANAP message */
459static void cn_ranap_handle_co(void *ctx, ranap_message *message)
460{
461 int rc;
462
463 LOGPIU(LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
464
465 switch (message->direction) {
466 case RANAP_RANAP_PDU_PR_initiatingMessage:
467 switch (message->procedureCode) {
468 case RANAP_ProcedureCode_id_InitialUE_Message:
469 LOGPIU(LOGL_ERROR, "Got InitialUE_Message but this is not a new conn\n");
470 rc = -1;
471 break;
472 case RANAP_ProcedureCode_id_DirectTransfer:
473 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
474 break;
475 case RANAP_ProcedureCode_id_ErrorIndication:
476 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
477 break;
478 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
479 /* Iu Release Request */
480 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
481 break;
482 default:
483 LOGPIU(LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
484 message->procedureCode);
485 rc = -1;
486 break;
487 }
488 break;
489 case RANAP_RANAP_PDU_PR_successfulOutcome:
490 switch (message->procedureCode) {
491 case RANAP_ProcedureCode_id_SecurityModeControl:
492 /* Security Mode Complete */
493 rc = global_iu_event_cb(ctx, RANAP_IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
494 break;
495 case RANAP_ProcedureCode_id_Iu_Release:
496 /* Iu Release Complete */
497 rc = global_iu_event_cb(ctx, RANAP_IU_EVENT_IU_RELEASE, NULL);
498 if (rc) {
499 LOGPIU(LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
500 rc);
501 }
502 break;
503 default:
504 LOGPIU(LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
505 message->procedureCode);
506 rc = -1;
507 break;
508 }
509 break;
510 case RANAP_RANAP_PDU_PR_outcome:
511 switch (message->procedureCode) {
512 case RANAP_ProcedureCode_id_RAB_Assignment:
513 /* RAB Assignment Response */
514 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
515 break;
516 default:
517 LOGPIU(LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
518 message->procedureCode);
519 rc = -1;
520 break;
521 }
522 break;
523 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
524 default:
525 LOGPIU(LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
526 message->procedureCode);
527 rc = -1;
528 break;
529 }
530
531 if (rc) {
532 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
533 /* TODO handling of the error? */
534 }
535}
536
537static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
538{
539 /* FIXME: send reset response */
540 return -1;
541}
542
543static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
544{
545 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
546 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
547 ranap_cause_str(&ies->cause));
548 else
549 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
550
551 return 0;
552}
553
554/* Entry point for connection-less RANAP message */
555static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
556{
557 int rc;
558
559 switch (message->direction) {
560 case RANAP_RANAP_PDU_PR_initiatingMessage:
561 switch (message->procedureCode) {
562 case RANAP_ProcedureCode_id_Reset:
563 /* received reset.req, send reset.resp */
564 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
565 break;
566 case RANAP_ProcedureCode_id_ErrorIndication:
567 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
568 break;
569 default:
570 rc = -1;
571 break;
572 }
573 break;
574 case RANAP_RANAP_PDU_PR_successfulOutcome:
575 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
576 case RANAP_RANAP_PDU_PR_outcome:
577 default:
578 rc = -1;
579 break;
580 }
581
582 if (rc) {
583 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
584 /* TODO handling of the error? */
585 }
586}
587
588/***********************************************************************
589 * Paging
590 ***********************************************************************/
591
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200592/* Send a paging command down a given SCCP User. tmsi and paging_cause are
593 * optional and may be passed NULL and 0, respectively, to disable their use.
594 * See enum RANAP_PagingCause.
595 *
596 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
597 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
598static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr,
599 const char *imsi, const uint32_t *tmsi,
600 bool is_ps, uint32_t paging_cause)
601{
602 struct msgb *msg;
603 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
604 msg->l2h = msg->data;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200605 osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, called_addr, msg);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200606 return 0;
607}
608
609static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptimsi,
610 uint16_t lac, uint8_t rac, bool is_ps)
611{
612 struct ranap_iu_rnc *rnc;
613 int pagings_sent = 0;
614
615 if (tmsi_or_ptimsi) {
616 LOGPIU(LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s"
617 " (paging will use %s %x)\n",
618 is_ps? "IuPS" : "IuCS",
619 imsi,
620 is_ps? "PTMSI" : "TMSI",
621 *tmsi_or_ptimsi);
622 } else {
623 LOGPIU(LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s"
624 " (paging will use IMSI)\n",
625 is_ps? "IuPS" : "IuCS",
626 imsi
627 );
628 }
629
630 llist_for_each_entry(rnc, &rnc_list, entry) {
631 if (rnc->lac != lac)
632 continue;
633 if (is_ps && rnc->rac != rac)
634 continue;
635
636 /* Found a match! */
637 if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptimsi, is_ps, 0)
638 == 0) {
639 LOGPIU(LOGL_DEBUG,
640 "%s: Paged for IMSI %s on RNC %d, on SCCP addr %s\n",
641 is_ps? "IuPS" : "IuCS",
642 imsi, rnc->rnc_id, osmo_sccp_addr_dump(&rnc->sccp_addr));
643 pagings_sent ++;
644 }
645 }
646
647 /* Some logging... */
648 if (pagings_sent > 0) {
649 LOGPIU(LOGL_DEBUG,
650 "%s: %d RNCs were paged for IMSI %s.\n",
651 is_ps? "IuPS" : "IuCS",
652 pagings_sent, imsi);
653 }
654 else {
655 if (is_ps) {
656 LOGPIU(LOGL_ERROR, "IuPS: Found no RNC to page for"
657 " LAC %d RAC %d (would have paged IMSI %s)\n",
658 lac, rac, imsi);
659 }
660 else {
661 LOGPIU(LOGL_ERROR, "IuCS: Found no RNC to page for"
662 " LAC %d (would have paged IMSI %s)\n",
663 lac, imsi);
664 }
665 }
666
667 return pagings_sent;
668}
669
670int ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
671{
672 return iu_page(imsi, tmsi, lac, 0, false);
673}
674
675int ranap_iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
676{
677 return iu_page(imsi, ptmsi, lac, rac, true);
678}
679
680
681/***********************************************************************
682 *
683 ***********************************************************************/
684
685int tx_unitdata(struct osmo_sccp_user *scu);
686int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id);
687
688struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
689struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
690
691static struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
692{
693 struct msgb *msg = msgb_alloc(1024, "conn_resp");
694 struct osmo_scu_prim *prim;
695
696 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
697 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
698 OSMO_SCU_PRIM_N_CONNECT,
699 PRIM_OP_RESPONSE, msg);
700 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
701 return &prim->oph;
702}
703
704static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
705{
706 struct osmo_sccp_user *scu = _scu;
707 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
708 struct osmo_prim_hdr *resp = NULL;
709 int rc;
710 struct ranap_ue_conn_ctx *ue;
711 struct new_ue_conn_ctx new_ctx = {};
712
713 LOGPIU(LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
714
715 switch (OSMO_PRIM_HDR(oph)) {
716 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
717 /* confirmation of outbound connection */
718 rc = -1;
719 break;
720 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
721 /* indication of new inbound connection request*/
722 LOGPIU(LOGL_DEBUG, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
723 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
724 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
725 LOGPIU(LOGL_NOTICE,
726 "Received invalid N-CONNECT.ind\n");
727 return 0;
728 }
729 new_ctx.sccp_addr = prim->u.connect.calling_addr;
730 new_ctx.conn_id = prim->u.connect.conn_id;
731 /* first ensure the local SCCP socket is ACTIVE */
732 resp = make_conn_resp(&prim->u.connect);
733 osmo_sccp_user_sap_down(scu, resp);
734 /* then handle the RANAP payload */
735 rc = ranap_cn_rx_co(cn_ranap_handle_co_initial, &new_ctx, msgb_l2(oph->msg), msgb_l2len(oph->msg));
736 break;
737 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
738 /* indication of disconnect */
739 LOGPIU(LOGL_DEBUG, "N-DISCONNECT.ind(%u)\n",
740 prim->u.disconnect.conn_id);
741 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
742 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
743 break;
744 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
745 /* connection-oriented data received */
746 LOGPIU(LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
747 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
748 /* resolve UE context */
749 ue = ue_conn_ctx_find(prim->u.data.conn_id);
750 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
751 break;
752 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
753 /* connection-less data received */
754 LOGPIU(LOGL_DEBUG, "N-UNITDATA.ind(%s)\n",
755 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
756 rc = ranap_cn_rx_cl(cn_ranap_handle_cl, scu, msgb_l2(oph->msg), msgb_l2len(oph->msg));
757 break;
758 default:
759 rc = -1;
760 break;
761 }
762
763 msgb_free(oph->msg);
764 return rc;
765}
766
767int ranap_iu_init(void *ctx, int log_subsystem, const char *sccp_user_name, struct osmo_sccp_instance *sccp,
768 ranap_iu_recv_cb_t iu_recv_cb, ranap_iu_event_cb_t iu_event_cb)
769{
770 iu_log_subsystem = log_subsystem;
771 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
772 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
773
774 global_iu_recv_cb = iu_recv_cb;
775 global_iu_event_cb = iu_event_cb;
776 g_sccp = sccp;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200777 osmo_sccp_local_addr_by_instance(&g_local_sccp_addr, sccp, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200778 g_scu = osmo_sccp_user_bind(g_sccp, sccp_user_name, sccp_sap_up, OSMO_SCCP_SSN_RANAP);
779
780 return 0;
781}
782