blob: 932b217189cda31af8411e0594fb24668a027333 [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
83int asn1_xer_print = 1;
84void *talloc_asn1_ctx;
85
86iu_recv_cb_t global_iu_recv_cb = NULL;
87iu_event_cb_t global_iu_event_cb = NULL;
88
89static LLIST_HEAD(ue_conn_ctx_list);
90static LLIST_HEAD(rnc_list);
91
92const struct value_string iu_event_type_names[] = {
Neels Hofmeyrb42dc432016-12-16 14:16:53 +010093 OSMO_VALUE_STRING(IU_EVENT_RAB_ASSIGN),
94 OSMO_VALUE_STRING(IU_EVENT_SECURITY_MODE_COMPLETE),
95 OSMO_VALUE_STRING(IU_EVENT_IU_RELEASE),
96 OSMO_VALUE_STRING(IU_EVENT_LINK_INVALIDATED),
Neels Hofmeyrecdfd6d2016-12-16 14:16:41 +010097 { 0, NULL }
Neels Hofmeyrbfa88782016-05-20 21:38:32 +020098};
99
100struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_link *link, uint32_t conn_id)
101{
102 struct ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ue_conn_ctx);
103
104 ctx->link = link;
105 ctx->conn_id = conn_id;
106 llist_add(&ctx->list, &ue_conn_ctx_list);
107
108 return ctx;
109}
110
111struct ue_conn_ctx *ue_conn_ctx_find(struct osmo_sccp_link *link,
112 uint32_t conn_id)
113{
114 struct ue_conn_ctx *ctx;
115
116 llist_for_each_entry(ctx, &ue_conn_ctx_list, list) {
117 if (ctx->link == link && ctx->conn_id == conn_id)
118 return ctx;
119 }
120 return NULL;
121}
122
123static struct iu_rnc *iu_rnc_alloc(uint16_t rnc_id, uint16_t lac, uint8_t rac,
124 struct osmo_sccp_link *link)
125{
126 struct iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct iu_rnc);
127
128 rnc->rnc_id = rnc_id;
129 rnc->lac = lac;
130 rnc->rac = rac;
131 rnc->link = link;
132 llist_add(&rnc->entry, &rnc_list);
133
134 LOGP(DRANAP, LOGL_NOTICE, "New RNC %d (LAC=%d RAC=%d)\n",
135 rnc->rnc_id, rnc->lac, rnc->rac);
136
137 return rnc;
138}
139
140static struct iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac,
141 uint8_t rac, struct osmo_sccp_link *link)
142{
143 struct iu_rnc *rnc;
144 llist_for_each_entry(rnc, &rnc_list, entry) {
145 if (rnc->rnc_id != rnc_id)
146 continue;
147
148 /* We have this RNC Id registered already. Make sure that the
149 * details match. */
150
151 /* TODO should a mismatch be an error? */
152 if (rnc->lac != lac || rnc->rac != rac)
153 LOGP(DRANAP, LOGL_NOTICE, "RNC %d changes its details:"
154 " LAC=%d RAC=%d --> LAC=%d RAC=%d\n",
155 rnc->rnc_id, rnc->lac, rnc->rac,
156 lac, rac);
157 rnc->lac = lac;
158 rnc->rac = rac;
159
160 if (link && rnc->link != link)
161 LOGP(DRANAP, LOGL_NOTICE, "RNC %d on new link"
162 " (LAC=%d RAC=%d)\n",
163 rnc->rnc_id, rnc->lac, rnc->rac);
164 rnc->link = link;
165 return rnc;
166 }
167
168 /* Not found, make a new one. */
169 return iu_rnc_alloc(rnc_id, lac, rac, link);
170}
171
172/* Discard/invalidate all ue_conn_ctx and iu_rnc entries that reference the
173 * given link, since this link is invalid and about to be deallocated. For
174 * each ue_conn_ctx, invoke the iu_event_cb_t with IU_EVENT_LINK_INVALIDATED.
175 */
176void iu_link_del(struct osmo_sccp_link *link)
177{
178 struct iu_rnc *rnc, *rnc_next;
179 llist_for_each_entry_safe(rnc, rnc_next, &rnc_list, entry) {
180 if (!rnc->link)
181 continue;
182 if (rnc->link != link)
183 continue;
184 rnc->link = NULL;
185 llist_del(&rnc->entry);
186 talloc_free(rnc);
187 }
188
189 struct ue_conn_ctx *uec, *uec_next;
190 llist_for_each_entry_safe(uec, uec_next, &ue_conn_ctx_list, list) {
191 if (uec->link != link)
192 continue;
193 uec->link = NULL;
194 global_iu_event_cb(uec, IU_EVENT_LINK_INVALIDATED, NULL);
195 }
196}
197
198/***********************************************************************
199 * RANAP handling
200 ***********************************************************************/
201
202int iu_rab_act(struct ue_conn_ctx *ue_ctx, struct msgb *msg)
203{
204 struct osmo_scu_prim *prim;
205
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 = ue_ctx->conn_id;
209 osmo_prim_init(&prim->oph,
210 SCCP_SAP_USER,
211 OSMO_SCU_PRIM_N_DATA,
212 PRIM_OP_REQUEST,
213 msg);
214 return osmo_sua_user_link_down(ue_ctx->link, &prim->oph);
215}
216
217int iu_rab_deact(struct ue_conn_ctx *ue_ctx, uint8_t rab_id)
218{
219 /* FIXME */
220 return -1;
221}
222
223int iu_tx_sec_mode_cmd(struct ue_conn_ctx *uectx, struct gsm_auth_tuple *tp,
224 int send_ck, int new_key)
225{
226 struct osmo_scu_prim *prim;
227 struct msgb *msg;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200228
229 /* create RANAP message */
Neels Hofmeyrcbafa252017-03-04 02:28:19 +0100230 msg = ranap_new_msg_sec_mod_cmd(tp->vec.ik, send_ck? tp->vec.ck : NULL,
231 new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200232 msg->l2h = msg->data;
233 /* wrap RANAP message in SCCP N-DATA.req */
234 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
235 prim->u.data.conn_id = uectx->conn_id;
236 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
237 OSMO_SCU_PRIM_N_DATA,
238 PRIM_OP_REQUEST, msg);
239 osmo_sua_user_link_down(uectx->link, &prim->oph);
240
241 return 0;
242}
243
244static int iu_grnc_id_parse(struct iu_grnc_id *dst,
245 struct RANAP_GlobalRNC_ID *src)
246{
247 /* The size is coming from arbitrary sender, check it gracefully */
248 if (src->pLMNidentity.size != 3) {
249 LOGP(DRANAP, LOGL_ERROR, "Invalid PLMN Identity size:"
250 " should be 3, is %d\n", src->pLMNidentity.size);
251 return -1;
252 }
253 gsm48_mcc_mnc_from_bcd(&src->pLMNidentity.buf[0],
254 &dst->mcc, &dst->mnc);
255 dst->rnc_id = (uint16_t)src->rNC_ID;
256 return 0;
257}
258
259#if 0
260 -- not used at present --
261static int iu_grnc_id_compose(struct iu_grnc_id *src,
262 struct RANAP_GlobalRNC_ID *dst)
263{
264 /* The caller must ensure proper size */
265 OSMO_ASSERT(dst->pLMNidentity.size == 3);
266 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
267 src->mcc, src->mnc);
268 dst->rNC_ID = src->rnc_id;
269 return 0;
270}
271#endif
272
273static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
274{
275 struct ue_conn_ctx *ue_conn = ctx;
276 struct gprs_ra_id ra_id;
277 struct iu_grnc_id grnc_id;
278 uint16_t sai;
279 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
280
281 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
282 LOGP(DRANAP, LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
283 return -1;
284 }
285
286 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
287 ra_id.rac = asn1str_to_u8(&ies->rac);
288 }
289
290 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
291 LOGP(DRANAP, LOGL_ERROR,
292 "Failed to parse RANAP Global-RNC-ID IE\n");
293 return -1;
294 }
295
296 sai = asn1str_to_u16(&ies->sai.sAC);
297 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
298 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
299
300 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
301 iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, ue_conn->link);
302 ue_conn->ra_id = ra_id;
303
304 /* Feed into the MM layer */
305 msg->dst = ctx;
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 LOGP(DRANAP, 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 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
350 ranap_cause_str(&ies->cause));
351 else
352 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
353
354 return 0;
355}
356
357int iu_tx(struct msgb *msg_nas, uint8_t sapi)
358{
359 struct ue_conn_ctx *uectx = msg_nas->dst;
360 struct msgb *msg;
361 struct osmo_scu_prim *prim;
362
363 LOGP(DRANAP, LOGL_INFO, "Transmitting L3 Message as RANAP DT (SUA link %p conn_id %u)\n",
364 uectx->link, 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_sua_user_link_down(uectx->link, &prim->oph);
375 return 0;
376}
377
378static int ranap_handle_co_iu_rel_req(struct ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
379{
380 struct msgb *msg;
381 struct osmo_scu_prim *prim;
382
383 LOGP(DRANAP, LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
384 msg = ranap_new_msg_iu_rel_cmd(&ies->cause);
385 msg->l2h = msg->data;
386 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
387 prim->u.data.conn_id = ctx->conn_id;
388 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
389 OSMO_SCU_PRIM_N_DATA,
390 PRIM_OP_REQUEST, msg);
391 osmo_sua_user_link_down(ctx->link, &prim->oph);
392 return 0;
393}
394
395static int ranap_handle_co_rab_ass_resp(struct ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
396{
397 int rc = -1;
398
Neels Hofmeyr87c00562016-10-18 20:19:50 +0200399 LOGP(DRANAP, LOGL_INFO,
400 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200401 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
402 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
403 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
404 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
405
406 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
407 if (rc) {
408 LOGP(DRANAP, LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
409 return rc;
410 }
411
412 rc = global_iu_event_cb(ctx, IU_EVENT_RAB_ASSIGN, &setup_ies);
413
414 ranap_free_rab_setupormodifieditemies(&setup_ies);
415 }
416
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200417 return rc;
418}
419
420/* Entry point for connection-oriented RANAP message */
421static void cn_ranap_handle_co(void *ctx, ranap_message *message)
422{
423 int rc;
424
425 LOGP(DRANAP, LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
426
427 switch (message->direction) {
428 case RANAP_RANAP_PDU_PR_initiatingMessage:
429 switch (message->procedureCode) {
430 case RANAP_ProcedureCode_id_InitialUE_Message:
431 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
432 break;
433 case RANAP_ProcedureCode_id_DirectTransfer:
434 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
435 break;
436 case RANAP_ProcedureCode_id_ErrorIndication:
437 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
438 break;
439 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
440 /* Iu Release Request */
441 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
442 break;
443 default:
444 LOGP(DRANAP, LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
445 message->procedureCode);
446 rc = -1;
447 break;
448 }
449 break;
450 case RANAP_RANAP_PDU_PR_successfulOutcome:
451 switch (message->procedureCode) {
452 case RANAP_ProcedureCode_id_SecurityModeControl:
453 /* Security Mode Complete */
454 rc = global_iu_event_cb(ctx, IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
455 break;
456 case RANAP_ProcedureCode_id_Iu_Release:
457 /* Iu Release Complete */
458 rc = global_iu_event_cb(ctx, IU_EVENT_IU_RELEASE, NULL);
459 if (rc) {
460 LOGP(DRANAP, LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
461 rc);
462 }
463 break;
464 default:
465 LOGP(DRANAP, LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
466 message->procedureCode);
467 rc = -1;
468 break;
469 }
470 break;
471 case RANAP_RANAP_PDU_PR_outcome:
472 switch (message->procedureCode) {
473 case RANAP_ProcedureCode_id_RAB_Assignment:
474 /* RAB Assignment Response */
475 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
476 break;
477 default:
478 LOGP(DRANAP, LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
479 message->procedureCode);
480 rc = -1;
481 break;
482 }
483 break;
484 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
485 default:
486 LOGP(DRANAP, LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
487 message->procedureCode);
488 rc = -1;
489 break;
490 }
491
492 if (rc) {
493 LOGP(DRANAP, LOGL_ERROR, "Error in cn_ranap_handle_co (%d)\n",
494 rc);
495 /* TODO handling of the error? */
496 }
497}
498
499static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
500{
501 /* FIXME: send reset response */
502 return -1;
503}
504
505static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
506{
507 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
508 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
509 ranap_cause_str(&ies->cause));
510 else
511 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
512
513 return 0;
514}
515
516/* Entry point for connection-less RANAP message */
517static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
518{
519 int rc;
520
521 switch (message->direction) {
522 case RANAP_RANAP_PDU_PR_initiatingMessage:
523 switch (message->procedureCode) {
524 case RANAP_ProcedureCode_id_Reset:
525 /* received reset.req, send reset.resp */
526 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
527 break;
528 case RANAP_ProcedureCode_id_ErrorIndication:
529 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
530 break;
531 default:
532 rc = -1;
533 break;
534 }
535 break;
536 case RANAP_RANAP_PDU_PR_successfulOutcome:
537 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
538 case RANAP_RANAP_PDU_PR_outcome:
539 default:
540 rc = -1;
541 break;
542 }
543
544 if (rc) {
545 LOGP(DRANAP, LOGL_ERROR, "Error in cn_ranap_handle_cl (%d)\n",
546 rc);
547 /* TODO handling of the error? */
548 }
549}
550
551/***********************************************************************
552 * Paging
553 ***********************************************************************/
554
555/* Send a paging command down a given SUA link. tmsi and paging_cause are
556 * optional and may be passed NULL and 0, respectively, to disable their use.
557 * See enum RANAP_PagingCause.
558 *
559 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
560 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
561static int iu_tx_paging_cmd(struct osmo_sccp_link *link,
562 const char *imsi, const uint32_t *tmsi,
563 bool is_ps, uint32_t paging_cause)
564{
565 struct msgb *msg;
566 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
567 msg->l2h = msg->data;
568 return osmo_sccp_tx_unitdata_ranap(link, 1, 2, msg->data,
569 msgb_length(msg));
570}
571
572static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptimsi,
573 uint16_t lac, uint8_t rac, bool is_ps)
574{
575 struct iu_rnc *rnc;
576 int pagings_sent = 0;
577
578 if (tmsi_or_ptimsi) {
579 LOGP(DRANAP, LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s"
580 " (paging will use %s %x)\n",
581 is_ps? "IuPS" : "IuCS",
582 imsi,
583 is_ps? "PTMSI" : "TMSI",
584 *tmsi_or_ptimsi);
585 } else {
586 LOGP(DRANAP, LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s"
587 " (paging will use IMSI)\n",
588 is_ps? "IuPS" : "IuCS",
589 imsi
590 );
591 }
592
593 llist_for_each_entry(rnc, &rnc_list, entry) {
594 if (!rnc->link) {
595 /* Not actually connected, don't count it. */
596 continue;
597 }
598 if (rnc->lac != lac)
599 continue;
600 if (is_ps && rnc->rac != rac)
601 continue;
602
603 /* Found a match! */
604 if (iu_tx_paging_cmd(rnc->link, imsi, tmsi_or_ptimsi, is_ps, 0)
605 == 0) {
606 LOGP(DRANAP, LOGL_DEBUG,
607 "%s: Paged for IMSI %s on RNC %d, on SUA link %p\n",
608 is_ps? "IuPS" : "IuCS",
609 imsi, rnc->rnc_id, rnc->link);
610 pagings_sent ++;
611 }
612 }
613
614 /* Some logging... */
615 if (pagings_sent > 0) {
616 LOGP(DRANAP, LOGL_DEBUG,
617 "%s: %d RNCs were paged for IMSI %s.\n",
618 is_ps? "IuPS" : "IuCS",
619 pagings_sent, imsi);
620 }
621 else {
622 if (is_ps) {
623 LOGP(DRANAP, LOGL_ERROR, "IuPS: Found no RNC to page for"
624 " LAC %d RAC %d (would have paged IMSI %s)\n",
625 lac, rac, imsi);
626 }
627 else {
628 LOGP(DRANAP, LOGL_ERROR, "IuCS: Found no RNC to page for"
629 " LAC %d (would have paged IMSI %s)\n",
630 lac, imsi);
631 }
632 }
633
634 return pagings_sent;
635}
636
637int iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
638{
639 return iu_page(imsi, tmsi, lac, 0, false);
640}
641
642int iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
643{
644 return iu_page(imsi, ptmsi, lac, rac, true);
645}
646
647
648/***********************************************************************
649 *
650 ***********************************************************************/
651
652int tx_unitdata(struct osmo_sccp_link *link);
653int tx_conn_req(struct osmo_sccp_link *link, uint32_t conn_id);
654
655struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
656struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
657
658struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
659{
660 struct msgb *msg = msgb_alloc(1024, "conn_resp");
661 struct osmo_scu_prim *prim;
662
663 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
664 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
665 OSMO_SCU_PRIM_N_CONNECT,
666 PRIM_OP_RESPONSE, msg);
667 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
668 return &prim->oph;
669}
670
671static int sccp_sap_up(struct osmo_prim_hdr *oph, void *link)
672{
673 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
674 struct osmo_prim_hdr *resp = NULL;
675 int rc;
676 struct ue_conn_ctx *ue;
677
678 DEBUGP(DRANAP, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
679
680 switch (OSMO_PRIM_HDR(oph)) {
681 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
682 /* confirmation of outbound connection */
683 rc = -1;
684 break;
685 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
686 /* indication of new inbound connection request*/
687 DEBUGP(DRANAP, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
688 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
689 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
690 LOGP(DRANAP, LOGL_NOTICE,
691 "Received invalid N-CONNECT.ind\n");
692 return 0;
693 }
694 ue = ue_conn_ctx_alloc(link, prim->u.connect.conn_id);
695 /* first ensure the local SUA/SCCP socket is ACTIVE */
696 resp = make_conn_resp(&prim->u.connect);
697 osmo_sua_user_link_down(link, resp);
698 /* then handle the RANAP payload */
699 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
700 break;
701 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
702 /* indication of disconnect */
703 DEBUGP(DRANAP, "N-DISCONNECT.ind(%u)\n",
704 prim->u.disconnect.conn_id);
705 ue = ue_conn_ctx_find(link, prim->u.disconnect.conn_id);
706 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
707 break;
708 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
709 /* connection-oriented data received */
710 DEBUGP(DRANAP, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
711 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
712 /* resolve UE context */
713 ue = ue_conn_ctx_find(link, prim->u.data.conn_id);
714 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
715 break;
716 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
717 /* connection-less data received */
718 DEBUGP(DRANAP, "N-UNITDATA.ind(%s)\n",
719 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
720 rc = ranap_cn_rx_cl(cn_ranap_handle_cl, link, msgb_l2(oph->msg), msgb_l2len(oph->msg));
721 break;
722 default:
723 rc = -1;
724 break;
725 }
726
727 msgb_free(oph->msg);
728 return rc;
729}
730
731int iu_init(void *ctx, const char *listen_addr, uint16_t listen_port,
732 iu_recv_cb_t iu_recv_cb, iu_event_cb_t iu_event_cb)
733{
734 struct osmo_sccp_user *user;
735 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
736 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
737
738 global_iu_recv_cb = iu_recv_cb;
739 global_iu_event_cb = iu_event_cb;
740 osmo_sua_set_log_area(DSUA);
741 user = osmo_sua_user_create(talloc_iu_ctx, sccp_sap_up, talloc_iu_ctx);
742 return osmo_sua_server_listen(user, listen_addr, listen_port);
743}
744