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