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