blob: 38c8a1df594dad6d427a70ee303b421d2f3cc77c [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 {
Neels Hofmeyrcf5fbea2018-03-21 15:50:41 +010045 struct osmo_plmn_id plmn;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020046 uint16_t rnc_id;
47};
48
Neels Hofmeyr69888c82017-12-15 02:54:45 +010049struct iu_lac_rac_entry {
50 struct llist_head entry;
51
52 /* LAC: Location Area Code (for CS and PS) */
53 uint16_t lac;
54 /* RAC: Routing Area Code (for PS only) */
55 uint8_t rac;
56};
57
Neels Hofmeyraae68b22017-07-05 14:38:52 +020058/* A remote RNC (Radio Network Controller, like BSC but for UMTS) that has
59 * called us and is currently reachable at the given osmo_sccp_addr. So, when we
60 * know a LAC for a subscriber, we can page it at the RNC matching that LAC or
61 * RAC. An HNB-GW typically presents itself as if it were a single RNC, even
62 * though it may have several RNCs in hNodeBs connected to it. Those will then
63 * share the same RNC id, which they actually receive and adopt from the HNB-GW
64 * in the HNBAP HNB REGISTER ACCEPT message. */
65struct ranap_iu_rnc {
66 struct llist_head entry;
67
68 uint16_t rnc_id;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020069 struct osmo_sccp_addr sccp_addr;
Neels Hofmeyr69888c82017-12-15 02:54:45 +010070
71 /* A list of struct iu_lac_rac_entry */
72 struct llist_head lac_rac_list;
Neels Hofmeyraae68b22017-07-05 14:38:52 +020073};
74
75void *talloc_iu_ctx;
76void *talloc_asn1_ctx;
77
78/* Implement the extern asn_debug from libasn1c to indicate whether to print
79 * asn.1 debug messages (see libasn1c). */
80int asn_debug = 0;
81
82/* Implement the extern asn1_xer_print to indicate whether the ASN.1 binary
83 * code decoded and encoded during Iu communication should be logged to stderr
84 * (see asn.1 generated code in osmo-iuh). */
85int asn1_xer_print = 0;
86
87ranap_iu_recv_cb_t global_iu_recv_cb = NULL;
88ranap_iu_event_cb_t global_iu_event_cb = NULL;
89int iu_log_subsystem = 0;
90
91#define LOGPIU(level, fmt, args...) \
92 LOGP(iu_log_subsystem, level, fmt, ## args)
93
Neels Hofmeyr69888c82017-12-15 02:54:45 +010094#define LOGPIUC(level, fmt, args...) \
95 LOGPC(iu_log_subsystem, level, fmt, ## args)
96
Neels Hofmeyraae68b22017-07-05 14:38:52 +020097static LLIST_HEAD(ue_conn_ctx_list);
98static LLIST_HEAD(rnc_list);
99
100static struct osmo_sccp_instance *g_sccp;
101static struct osmo_sccp_user *g_scu;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200102static struct osmo_sccp_addr g_local_sccp_addr;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200103
104const struct value_string ranap_iu_event_type_names[] = {
105 OSMO_VALUE_STRING(RANAP_IU_EVENT_RAB_ASSIGN),
106 OSMO_VALUE_STRING(RANAP_IU_EVENT_SECURITY_MODE_COMPLETE),
107 OSMO_VALUE_STRING(RANAP_IU_EVENT_IU_RELEASE),
108 OSMO_VALUE_STRING(RANAP_IU_EVENT_LINK_INVALIDATED),
109 { 0, NULL }
110};
111
Alexander Couzens790212f2019-09-10 18:23:37 +0200112static int global_iu_event(struct ranap_ue_conn_ctx *ue_ctx,
113 enum ranap_iu_event_type type,
114 void *data)
115{
116 if (!global_iu_event_cb)
117 return 0;
118
Alexander Couzens8e2a0ee2019-09-10 18:28:49 +0200119 if (!ue_ctx->notification)
120 return 0;
121
Alexander Couzens790212f2019-09-10 18:23:37 +0200122 return global_iu_event_cb(ue_ctx, type, data);
123}
124
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200125static struct ranap_ue_conn_ctx *ue_conn_ctx_alloc(struct ranap_iu_rnc *rnc, uint32_t conn_id)
126{
127 struct ranap_ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ranap_ue_conn_ctx);
128
129 ctx->rnc = rnc;
130 ctx->conn_id = conn_id;
Alexander Couzens8e2a0ee2019-09-10 18:28:49 +0200131 ctx->notification = true;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200132 llist_add(&ctx->list, &ue_conn_ctx_list);
133
134 return ctx;
135}
136
137static struct ranap_ue_conn_ctx *ue_conn_ctx_find(uint32_t conn_id)
138{
139 struct ranap_ue_conn_ctx *ctx;
140
141 llist_for_each_entry(ctx, &ue_conn_ctx_list, list) {
142 if (ctx->conn_id == conn_id)
143 return ctx;
144 }
145 return NULL;
146}
147
Alexander Couzens609994d2019-08-12 16:48:56 +0200148void ranap_iu_free_ue(struct ranap_ue_conn_ctx *ue_ctx)
149{
150 if (!ue_ctx)
151 return;
152
153 osmo_sccp_tx_disconn(g_scu, ue_ctx->conn_id, NULL, 0);
154 llist_del(&ue_ctx->list);
155 talloc_free(ue_ctx);
156}
157
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100158static struct ranap_iu_rnc *iu_rnc_alloc(uint16_t rnc_id, struct osmo_sccp_addr *addr)
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200159{
160 struct ranap_iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct ranap_iu_rnc);
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100161 OSMO_ASSERT(rnc);
162
163 INIT_LLIST_HEAD(&rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200164
165 rnc->rnc_id = rnc_id;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200166 rnc->sccp_addr = *addr;
167 llist_add(&rnc->entry, &rnc_list);
168
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100169 LOGPIU(LOGL_NOTICE, "New RNC %d at %s\n", rnc->rnc_id, osmo_sccp_addr_dump(addr));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200170
171 return rnc;
172}
173
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100174/* Find a match for the given LAC (and RAC). For CS, pass rac as 0.
175 * If rnc and lre pointers are not NULL, *rnc / *lre are set to NULL if no match is found, or to the
176 * match if a match is found. Return true if a match is found. */
177static bool iu_rnc_lac_rac_find(struct ranap_iu_rnc **rnc, struct iu_lac_rac_entry **lre,
178 uint16_t lac, uint8_t rac)
179{
180 struct ranap_iu_rnc *r;
181 struct iu_lac_rac_entry *e;
182
183 if (rnc)
184 *rnc = NULL;
185 if (lre)
186 *lre = NULL;
187
188 llist_for_each_entry(r, &rnc_list, entry) {
189 llist_for_each_entry(e, &r->lac_rac_list, entry) {
190 if (e->lac == lac && e->rac == rac) {
191 if (rnc)
192 *rnc = r;
193 if (lre)
194 *lre = e;
195 return true;
196 }
197 }
198 }
199 return false;
200}
201
202static struct ranap_iu_rnc *iu_rnc_id_find(uint16_t rnc_id)
203{
204 struct ranap_iu_rnc *rnc;
205 llist_for_each_entry(rnc, &rnc_list, entry) {
206 if (rnc->rnc_id == rnc_id)
207 return rnc;
208 }
209 return NULL;
210}
211
212static bool same_sccp_addr(struct osmo_sccp_addr *a, struct osmo_sccp_addr *b)
213{
214 char buf[256];
215 osmo_strlcpy(buf, osmo_sccp_addr_dump(a), sizeof(buf));
216 return !strcmp(buf, osmo_sccp_addr_dump(b));
217}
218
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200219static struct ranap_iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac,
220 uint8_t rac, struct osmo_sccp_addr *addr)
221{
222 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100223 struct ranap_iu_rnc *old_rnc;
224 struct iu_lac_rac_entry *lre;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200225
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100226 /* Make sure we know this rnc_id and that this SCCP address is in our records */
227 rnc = iu_rnc_id_find(rnc_id);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200228
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100229 if (rnc) {
230 if (!same_sccp_addr(&rnc->sccp_addr, addr)) {
231 LOGPIU(LOGL_NOTICE, "RNC %u changed its SCCP addr to %s (LAC=%u RAC=%u)\n",
232 rnc_id, osmo_sccp_addr_dump(addr), lac, rac);
233 rnc->sccp_addr = *addr;
234 }
235 } else
236 rnc = iu_rnc_alloc(rnc_id, addr);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200237
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100238 /* Detect whether the LAC,RAC is already recorded in another RNC */
239 iu_rnc_lac_rac_find(&old_rnc, &lre, lac, rac);
240
241 if (old_rnc && old_rnc != rnc) {
242 /* LAC,RAC already exists in a different RNC */
243 LOGPIU(LOGL_NOTICE, "LAC %u RAC %u moved from RNC %u %s",
244 lac, rac, old_rnc->rnc_id, osmo_sccp_addr_dump(&old_rnc->sccp_addr));
245 LOGPIUC(LOGL_NOTICE, " to RNC %u %s\n",
246 rnc->rnc_id, osmo_sccp_addr_dump(&rnc->sccp_addr));
247
248 llist_del(&lre->entry);
249 llist_add(&lre->entry, &rnc->lac_rac_list);
250 } else if (!old_rnc) {
251 /* LAC,RAC not recorded yet */
252 LOGPIU(LOGL_NOTICE, "RNC %u: new LAC %u RAC %u\n", rnc_id, lac, rac);
253 lre = talloc_zero(rnc, struct iu_lac_rac_entry);
254 lre->lac = lac;
255 lre->rac = rac;
256 llist_add(&lre->entry, &rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200257 }
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100258 /* else, LAC,RAC already recorded with the current RNC. */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200259
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100260 return rnc;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200261}
262
263/***********************************************************************
264 * RANAP handling
265 ***********************************************************************/
266
267int ranap_iu_rab_act(struct ranap_ue_conn_ctx *ue_ctx, struct msgb *msg)
268{
269 struct osmo_scu_prim *prim;
270
271 /* wrap RANAP message in SCCP N-DATA.req */
272 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
273 prim->u.data.conn_id = ue_ctx->conn_id;
274 osmo_prim_init(&prim->oph,
275 SCCP_SAP_USER,
276 OSMO_SCU_PRIM_N_DATA,
277 PRIM_OP_REQUEST,
278 msg);
279 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
280}
281
282int ranap_iu_rab_deact(struct ranap_ue_conn_ctx *ue_ctx, uint8_t rab_id)
283{
284 /* FIXME */
285 return -1;
286}
287
288int ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
289 int send_ck, int new_key)
290{
291 struct osmo_scu_prim *prim;
292 struct msgb *msg;
293
294 /* create RANAP message */
295 msg = ranap_new_msg_sec_mod_cmd(vec->ik, send_ck? vec->ck : NULL,
296 new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old);
297 msg->l2h = msg->data;
298 /* wrap RANAP message in SCCP N-DATA.req */
299 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
300 prim->u.data.conn_id = uectx->conn_id;
301 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
302 OSMO_SCU_PRIM_N_DATA,
303 PRIM_OP_REQUEST, msg);
304 osmo_sccp_user_sap_down(g_scu, &prim->oph);
305
306 return 0;
307}
308
309int ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *uectx, const char *imsi)
310{
311 struct msgb *msg;
312 struct osmo_scu_prim *prim;
313
314 LOGPIU(LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n",
315 uectx->conn_id);
316
317 msg = ranap_new_msg_common_id(imsi);
318 msg->l2h = msg->data;
319 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
320 prim->u.data.conn_id = uectx->conn_id;
321 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
322 OSMO_SCU_PRIM_N_DATA,
323 PRIM_OP_REQUEST, msg);
324 osmo_sccp_user_sap_down(g_scu, &prim->oph);
325 return 0;
326}
327
328static int iu_grnc_id_parse(struct iu_grnc_id *dst, struct RANAP_GlobalRNC_ID *src)
329{
330 /* The size is coming from arbitrary sender, check it gracefully */
331 if (src->pLMNidentity.size != 3) {
332 LOGPIU(LOGL_ERROR, "Invalid PLMN Identity size:"
333 " should be 3, is %d\n", src->pLMNidentity.size);
334 return -1;
335 }
Neels Hofmeyrcf5fbea2018-03-21 15:50:41 +0100336 osmo_plmn_from_bcd(&src->pLMNidentity.buf[0], &dst->plmn);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200337 dst->rnc_id = (uint16_t)src->rNC_ID;
338 return 0;
339}
340
341#if 0
342 -- not used at present --
343static int iu_grnc_id_compose(struct iu_grnc_id *src, struct RANAP_GlobalRNC_ID *dst)
344{
345 /* The caller must ensure proper size */
346 OSMO_ASSERT(dst->pLMNidentity.size == 3);
347 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
348 src->mcc, src->mnc);
349 dst->rNC_ID = src->rnc_id;
350 return 0;
351}
352#endif
353
354struct new_ue_conn_ctx {
355 struct osmo_sccp_addr sccp_addr;
356 uint32_t conn_id;
357};
358
359static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
360{
361 struct new_ue_conn_ctx *new_ctx = ctx;
362 struct gprs_ra_id ra_id;
363 struct iu_grnc_id grnc_id;
364 uint16_t sai;
365 struct ranap_ue_conn_ctx *ue;
366 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
367 struct ranap_iu_rnc *rnc;
368
369 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
370 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
371 return -1;
372 }
373
374 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
375 ra_id.rac = asn1str_to_u8(&ies->rac);
376 }
377
378 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
379 LOGPIU(LOGL_ERROR,
380 "Failed to parse RANAP Global-RNC-ID IE\n");
381 return -1;
382 }
383
384 sai = asn1str_to_u16(&ies->sai.sAC);
385 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
386 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
387
388 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
389 rnc = iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &new_ctx->sccp_addr);
390
391 ue = ue_conn_ctx_alloc(rnc, new_ctx->conn_id);
392 OSMO_ASSERT(ue);
393 ue->ra_id = ra_id;
394
395 /* Feed into the MM layer */
396 msg->dst = ue;
397 global_iu_recv_cb(msg, &ra_id, &sai);
398
399 msgb_free(msg);
400
401 return 0;
402}
403
404static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
405{
406 struct gprs_ra_id _ra_id, *ra_id = NULL;
407 uint16_t _sai, *sai = NULL;
408 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
409
410 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
411 if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) {
412 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
413 return -1;
414 }
415 ra_id = &_ra_id;
416 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
417 _ra_id.rac = asn1str_to_u8(&ies->rac);
418 }
419 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
420 _sai = asn1str_to_u16(&ies->sai.sAC);
421 sai = &_sai;
422 }
423 }
424
425 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
426 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
427
428 /* Feed into the MM/CC/SMS-CP layer */
429 msg->dst = ctx;
430 global_iu_recv_cb(msg, ra_id, sai);
431
432 msgb_free(msg);
433
434 return 0;
435}
436
437static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
438{
439 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
440 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
441 ranap_cause_str(&ies->cause));
442 else
443 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
444
445 return 0;
446}
447
448int ranap_iu_tx(struct msgb *msg_nas, uint8_t sapi)
449{
450 struct ranap_ue_conn_ctx *uectx = msg_nas->dst;
451 struct msgb *msg;
452 struct osmo_scu_prim *prim;
453
454 LOGPIU(LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n",
455 uectx->conn_id);
456
457 msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas));
458 msgb_free(msg_nas);
459 msg->l2h = msg->data;
460 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
461 prim->u.data.conn_id = uectx->conn_id;
462 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
463 OSMO_SCU_PRIM_N_DATA,
464 PRIM_OP_REQUEST, msg);
465 osmo_sccp_user_sap_down(g_scu, &prim->oph);
466 return 0;
467}
468
469/* Send Iu Release for the given UE connection.
470 * If cause is NULL, the standard "No remaining RAB" cause is sent, otherwise
471 * the provided cause. */
472int ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
473{
474 struct msgb *msg;
475 struct osmo_scu_prim *prim;
476 static const struct RANAP_Cause default_cause = {
477 .present = RANAP_Cause_PR_radioNetwork,
478 .choice.radioNetwork = RANAP_CauseRadioNetwork_no_remaining_rab,
479 };
480
481 if (!cause)
482 cause = &default_cause;
483
484 msg = ranap_new_msg_iu_rel_cmd(cause);
485 msg->l2h = msg->data;
486 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
487 prim->u.data.conn_id = ctx->conn_id;
488 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
489 OSMO_SCU_PRIM_N_DATA,
490 PRIM_OP_REQUEST, msg);
491 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
492}
493
494static int ranap_handle_co_iu_rel_req(struct ranap_ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
495{
496 LOGPIU(LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
497 ranap_iu_tx_release(ctx, &ies->cause);
498 return 0;
499}
500
501static int ranap_handle_co_rab_ass_resp(struct ranap_ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
502{
503 int rc = -1;
504
505 LOGPIU(LOGL_INFO,
506 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
507 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
508 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
509 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
510 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
511
512 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
513 if (rc) {
514 LOGPIU(LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
515 return rc;
516 }
517
Alexander Couzens790212f2019-09-10 18:23:37 +0200518 rc = global_iu_event(ctx, RANAP_IU_EVENT_RAB_ASSIGN, &setup_ies);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200519
520 ranap_free_rab_setupormodifieditemies(&setup_ies);
521 }
522 /* FIXME: handle RAB Ass failure? */
523
524 return rc;
525}
526
527static void cn_ranap_handle_co_initial(void *ctx, ranap_message *message)
528{
529 int rc;
530
531 LOGPIU(LOGL_NOTICE, "handle_co_initial(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
532
533 if (message->direction != RANAP_RANAP_PDU_PR_initiatingMessage
534 || message->procedureCode != RANAP_ProcedureCode_id_InitialUE_Message) {
535 LOGPIU(LOGL_ERROR, "Expected direction 'InitiatingMessage',"
536 " procedureCode 'InitialUE_Message', instead got %u and %u\n",
537 message->direction, message->procedureCode);
538 rc = -1;
539 }
540 else
541 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
542
543 if (rc) {
544 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
545 /* TODO handling of the error? */
546 }
547}
548
549/* Entry point for connection-oriented RANAP message */
550static void cn_ranap_handle_co(void *ctx, ranap_message *message)
551{
552 int rc;
553
554 LOGPIU(LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
555
556 switch (message->direction) {
557 case RANAP_RANAP_PDU_PR_initiatingMessage:
558 switch (message->procedureCode) {
559 case RANAP_ProcedureCode_id_InitialUE_Message:
560 LOGPIU(LOGL_ERROR, "Got InitialUE_Message but this is not a new conn\n");
561 rc = -1;
562 break;
563 case RANAP_ProcedureCode_id_DirectTransfer:
564 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
565 break;
566 case RANAP_ProcedureCode_id_ErrorIndication:
567 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
568 break;
569 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
570 /* Iu Release Request */
571 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
572 break;
573 default:
574 LOGPIU(LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
575 message->procedureCode);
576 rc = -1;
577 break;
578 }
579 break;
580 case RANAP_RANAP_PDU_PR_successfulOutcome:
581 switch (message->procedureCode) {
582 case RANAP_ProcedureCode_id_SecurityModeControl:
583 /* Security Mode Complete */
Alexander Couzens790212f2019-09-10 18:23:37 +0200584 rc = global_iu_event(ctx, RANAP_IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200585 break;
586 case RANAP_ProcedureCode_id_Iu_Release:
587 /* Iu Release Complete */
Alexander Couzens790212f2019-09-10 18:23:37 +0200588 rc = global_iu_event(ctx, RANAP_IU_EVENT_IU_RELEASE, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200589 if (rc) {
590 LOGPIU(LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
591 rc);
592 }
593 break;
594 default:
595 LOGPIU(LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
596 message->procedureCode);
597 rc = -1;
598 break;
599 }
600 break;
601 case RANAP_RANAP_PDU_PR_outcome:
602 switch (message->procedureCode) {
603 case RANAP_ProcedureCode_id_RAB_Assignment:
604 /* RAB Assignment Response */
605 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
606 break;
607 default:
608 LOGPIU(LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
609 message->procedureCode);
610 rc = -1;
611 break;
612 }
613 break;
614 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
615 default:
616 LOGPIU(LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
617 message->procedureCode);
618 rc = -1;
619 break;
620 }
621
622 if (rc) {
623 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
624 /* TODO handling of the error? */
625 }
626}
627
628static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
629{
Harald Welte8fa35b82019-04-20 20:13:31 +0200630 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) ctx;
631 struct osmo_scu_unitdata_param *ud_prim = &prim->u.unitdata;
632 RANAP_GlobalRNC_ID_t *grnc_id = NULL;
633 struct msgb *resp;
634
635 OSMO_ASSERT(prim->oph.primitive == OSMO_SCU_PRIM_N_UNITDATA);
636
637 /* FIXME: verify ies.cN_DomainIndicator */
638
639 if (ies->presenceMask & RESETIES_RANAP_GLOBALRNC_ID_PRESENT)
640 grnc_id = &ies->globalRNC_ID;
641
642 /* send reset response */
643 resp = ranap_new_msg_reset_ack(ies->cN_DomainIndicator, grnc_id);
644 if (!resp)
645 return -ENOMEM;
646 resp->l2h = resp->data;
647 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, &ud_prim->calling_addr, resp);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200648}
649
650static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
651{
652 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
653 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
654 ranap_cause_str(&ies->cause));
655 else
656 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
657
658 return 0;
659}
660
661/* Entry point for connection-less RANAP message */
662static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
663{
664 int rc;
665
666 switch (message->direction) {
667 case RANAP_RANAP_PDU_PR_initiatingMessage:
668 switch (message->procedureCode) {
669 case RANAP_ProcedureCode_id_Reset:
670 /* received reset.req, send reset.resp */
671 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
672 break;
673 case RANAP_ProcedureCode_id_ErrorIndication:
674 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
675 break;
676 default:
677 rc = -1;
678 break;
679 }
680 break;
681 case RANAP_RANAP_PDU_PR_successfulOutcome:
682 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
683 case RANAP_RANAP_PDU_PR_outcome:
684 default:
685 rc = -1;
686 break;
687 }
688
689 if (rc) {
690 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
691 /* TODO handling of the error? */
692 }
693}
694
695/***********************************************************************
696 * Paging
697 ***********************************************************************/
698
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200699/* Send a paging command down a given SCCP User. tmsi and paging_cause are
700 * optional and may be passed NULL and 0, respectively, to disable their use.
701 * See enum RANAP_PagingCause.
702 *
703 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
704 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
705static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr,
706 const char *imsi, const uint32_t *tmsi,
707 bool is_ps, uint32_t paging_cause)
708{
709 struct msgb *msg;
710 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
711 msg->l2h = msg->data;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100712 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, called_addr, msg);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200713}
714
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100715static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptmsi,
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200716 uint16_t lac, uint8_t rac, bool is_ps)
717{
718 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100719 const char *log_msg;
720 int log_level;
721 int paged = 0;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200722
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100723 iu_rnc_lac_rac_find(&rnc, NULL, lac, rac);
724 if (rnc) {
725 if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptmsi, is_ps, 0) == 0) {
726 log_msg = "Paging";
727 log_level = LOGL_DEBUG;
728 paged = 1;
729 } else {
730 log_msg = "Paging failed";
731 log_level = LOGL_ERROR;
732 }
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200733 } else {
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100734 log_msg = "Found no RNC to Page";
735 log_level = LOGL_ERROR;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200736 }
737
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100738 if (is_ps)
739 LOGPIU(log_level, "IuPS: %s on LAC %d RAC %d", log_msg, lac, rac);
740 else
741 LOGPIU(log_level, "IuCS: %s on LAC %d", log_msg, lac);
742 if (rnc)
743 LOGPIUC(log_level, " at SCCP-addr %s", osmo_sccp_addr_dump(&rnc->sccp_addr));
744 if (tmsi_or_ptmsi)
745 LOGPIUC(log_level, ", for %s %08x\n", is_ps? "PTMSI" : "TMSI", *tmsi_or_ptmsi);
746 else
747 LOGPIUC(log_level, ", for IMSI %s\n", imsi);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200748
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100749 return paged;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200750}
751
752int ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
753{
754 return iu_page(imsi, tmsi, lac, 0, false);
755}
756
757int ranap_iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
758{
759 return iu_page(imsi, ptmsi, lac, rac, true);
760}
761
762
763/***********************************************************************
764 *
765 ***********************************************************************/
766
767int tx_unitdata(struct osmo_sccp_user *scu);
768int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id);
769
770struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
771struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
772
773static struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
774{
775 struct msgb *msg = msgb_alloc(1024, "conn_resp");
776 struct osmo_scu_prim *prim;
777
778 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
779 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
780 OSMO_SCU_PRIM_N_CONNECT,
781 PRIM_OP_RESPONSE, msg);
782 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
783 return &prim->oph;
784}
785
786static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
787{
788 struct osmo_sccp_user *scu = _scu;
789 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
790 struct osmo_prim_hdr *resp = NULL;
Alexander Couzens80307922019-08-13 17:13:59 +0200791 int rc = -1;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200792 struct ranap_ue_conn_ctx *ue;
793 struct new_ue_conn_ctx new_ctx = {};
794
795 LOGPIU(LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
796
797 switch (OSMO_PRIM_HDR(oph)) {
798 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
799 /* confirmation of outbound connection */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200800 break;
801 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
802 /* indication of new inbound connection request*/
803 LOGPIU(LOGL_DEBUG, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
804 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
805 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
806 LOGPIU(LOGL_NOTICE,
807 "Received invalid N-CONNECT.ind\n");
808 return 0;
809 }
810 new_ctx.sccp_addr = prim->u.connect.calling_addr;
811 new_ctx.conn_id = prim->u.connect.conn_id;
812 /* first ensure the local SCCP socket is ACTIVE */
813 resp = make_conn_resp(&prim->u.connect);
814 osmo_sccp_user_sap_down(scu, resp);
815 /* then handle the RANAP payload */
816 rc = ranap_cn_rx_co(cn_ranap_handle_co_initial, &new_ctx, msgb_l2(oph->msg), msgb_l2len(oph->msg));
817 break;
818 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
819 /* indication of disconnect */
820 LOGPIU(LOGL_DEBUG, "N-DISCONNECT.ind(%u)\n",
821 prim->u.disconnect.conn_id);
822 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200823 if (!ue)
824 break;
825
Alexander Couzens4ae261b2019-08-13 17:14:31 +0200826 rc = 0;
827 if (msgb_l2len(oph->msg) > 0)
828 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Alexander Couzensefdd80d2019-08-13 17:42:50 +0200829
830 /* A Iu Release event might be used to free the UE in cn_ranap_handle_co. */
831 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
832 if (!ue)
833 break;
834
Alexander Couzens790212f2019-09-10 18:23:37 +0200835 global_iu_event(ue, RANAP_IU_EVENT_LINK_INVALIDATED, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200836 break;
837 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
838 /* connection-oriented data received */
839 LOGPIU(LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
840 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
841 /* resolve UE context */
842 ue = ue_conn_ctx_find(prim->u.data.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200843 if (!ue)
844 break;
845
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200846 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
847 break;
848 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
849 /* connection-less data received */
850 LOGPIU(LOGL_DEBUG, "N-UNITDATA.ind(%s)\n",
851 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte8fa35b82019-04-20 20:13:31 +0200852 rc = ranap_cn_rx_cl(cn_ranap_handle_cl, prim, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200853 break;
854 default:
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200855 break;
856 }
857
858 msgb_free(oph->msg);
859 return rc;
860}
861
862int ranap_iu_init(void *ctx, int log_subsystem, const char *sccp_user_name, struct osmo_sccp_instance *sccp,
863 ranap_iu_recv_cb_t iu_recv_cb, ranap_iu_event_cb_t iu_event_cb)
864{
865 iu_log_subsystem = log_subsystem;
866 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
867 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
868
869 global_iu_recv_cb = iu_recv_cb;
870 global_iu_event_cb = iu_event_cb;
871 g_sccp = sccp;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200872 osmo_sccp_local_addr_by_instance(&g_local_sccp_addr, sccp, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200873 g_scu = osmo_sccp_user_bind(g_sccp, sccp_user_name, sccp_sap_up, OSMO_SCCP_SSN_RANAP);
874
875 return 0;
876}