blob: e4eb83e9049c83454cdb31ee8f45bbe95cbba894 [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;
Alexander Couzensff796992019-09-10 17:46:54 +0200132 osmo_timer_setup(&ctx->release_timeout,
133 (void *)(void *) ranap_iu_free_ue,
134 ctx);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200135 llist_add(&ctx->list, &ue_conn_ctx_list);
136
137 return ctx;
138}
139
140static struct ranap_ue_conn_ctx *ue_conn_ctx_find(uint32_t conn_id)
141{
142 struct ranap_ue_conn_ctx *ctx;
143
144 llist_for_each_entry(ctx, &ue_conn_ctx_list, list) {
145 if (ctx->conn_id == conn_id)
146 return ctx;
147 }
148 return NULL;
149}
150
Alexander Couzens609994d2019-08-12 16:48:56 +0200151void ranap_iu_free_ue(struct ranap_ue_conn_ctx *ue_ctx)
152{
153 if (!ue_ctx)
154 return;
155
Alexander Couzensff796992019-09-10 17:46:54 +0200156 osmo_timer_del(&ue_ctx->release_timeout);
Alexander Couzens609994d2019-08-12 16:48:56 +0200157 osmo_sccp_tx_disconn(g_scu, ue_ctx->conn_id, NULL, 0);
158 llist_del(&ue_ctx->list);
159 talloc_free(ue_ctx);
160}
161
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100162static struct ranap_iu_rnc *iu_rnc_alloc(uint16_t rnc_id, struct osmo_sccp_addr *addr)
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200163{
164 struct ranap_iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct ranap_iu_rnc);
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100165 OSMO_ASSERT(rnc);
166
167 INIT_LLIST_HEAD(&rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200168
169 rnc->rnc_id = rnc_id;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200170 rnc->sccp_addr = *addr;
171 llist_add(&rnc->entry, &rnc_list);
172
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100173 LOGPIU(LOGL_NOTICE, "New RNC %d at %s\n", rnc->rnc_id, osmo_sccp_addr_dump(addr));
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200174
175 return rnc;
176}
177
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100178/* Find a match for the given LAC (and RAC). For CS, pass rac as 0.
179 * If rnc and lre pointers are not NULL, *rnc / *lre are set to NULL if no match is found, or to the
180 * match if a match is found. Return true if a match is found. */
181static bool iu_rnc_lac_rac_find(struct ranap_iu_rnc **rnc, struct iu_lac_rac_entry **lre,
182 uint16_t lac, uint8_t rac)
183{
184 struct ranap_iu_rnc *r;
185 struct iu_lac_rac_entry *e;
186
187 if (rnc)
188 *rnc = NULL;
189 if (lre)
190 *lre = NULL;
191
192 llist_for_each_entry(r, &rnc_list, entry) {
193 llist_for_each_entry(e, &r->lac_rac_list, entry) {
194 if (e->lac == lac && e->rac == rac) {
195 if (rnc)
196 *rnc = r;
197 if (lre)
198 *lre = e;
199 return true;
200 }
201 }
202 }
203 return false;
204}
205
206static struct ranap_iu_rnc *iu_rnc_id_find(uint16_t rnc_id)
207{
208 struct ranap_iu_rnc *rnc;
209 llist_for_each_entry(rnc, &rnc_list, entry) {
210 if (rnc->rnc_id == rnc_id)
211 return rnc;
212 }
213 return NULL;
214}
215
216static bool same_sccp_addr(struct osmo_sccp_addr *a, struct osmo_sccp_addr *b)
217{
218 char buf[256];
219 osmo_strlcpy(buf, osmo_sccp_addr_dump(a), sizeof(buf));
220 return !strcmp(buf, osmo_sccp_addr_dump(b));
221}
222
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200223static struct ranap_iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac,
224 uint8_t rac, struct osmo_sccp_addr *addr)
225{
226 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100227 struct ranap_iu_rnc *old_rnc;
228 struct iu_lac_rac_entry *lre;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200229
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100230 /* Make sure we know this rnc_id and that this SCCP address is in our records */
231 rnc = iu_rnc_id_find(rnc_id);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200232
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100233 if (rnc) {
234 if (!same_sccp_addr(&rnc->sccp_addr, addr)) {
235 LOGPIU(LOGL_NOTICE, "RNC %u changed its SCCP addr to %s (LAC=%u RAC=%u)\n",
236 rnc_id, osmo_sccp_addr_dump(addr), lac, rac);
237 rnc->sccp_addr = *addr;
238 }
239 } else
240 rnc = iu_rnc_alloc(rnc_id, addr);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200241
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100242 /* Detect whether the LAC,RAC is already recorded in another RNC */
243 iu_rnc_lac_rac_find(&old_rnc, &lre, lac, rac);
244
245 if (old_rnc && old_rnc != rnc) {
246 /* LAC,RAC already exists in a different RNC */
247 LOGPIU(LOGL_NOTICE, "LAC %u RAC %u moved from RNC %u %s",
248 lac, rac, old_rnc->rnc_id, osmo_sccp_addr_dump(&old_rnc->sccp_addr));
249 LOGPIUC(LOGL_NOTICE, " to RNC %u %s\n",
250 rnc->rnc_id, osmo_sccp_addr_dump(&rnc->sccp_addr));
251
252 llist_del(&lre->entry);
253 llist_add(&lre->entry, &rnc->lac_rac_list);
254 } else if (!old_rnc) {
255 /* LAC,RAC not recorded yet */
256 LOGPIU(LOGL_NOTICE, "RNC %u: new LAC %u RAC %u\n", rnc_id, lac, rac);
257 lre = talloc_zero(rnc, struct iu_lac_rac_entry);
258 lre->lac = lac;
259 lre->rac = rac;
260 llist_add(&lre->entry, &rnc->lac_rac_list);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200261 }
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100262 /* else, LAC,RAC already recorded with the current RNC. */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200263
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100264 return rnc;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200265}
266
267/***********************************************************************
268 * RANAP handling
269 ***********************************************************************/
270
271int ranap_iu_rab_act(struct ranap_ue_conn_ctx *ue_ctx, struct msgb *msg)
272{
273 struct osmo_scu_prim *prim;
274
275 /* wrap RANAP message in SCCP N-DATA.req */
276 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
277 prim->u.data.conn_id = ue_ctx->conn_id;
278 osmo_prim_init(&prim->oph,
279 SCCP_SAP_USER,
280 OSMO_SCU_PRIM_N_DATA,
281 PRIM_OP_REQUEST,
282 msg);
283 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
284}
285
286int ranap_iu_rab_deact(struct ranap_ue_conn_ctx *ue_ctx, uint8_t rab_id)
287{
288 /* FIXME */
289 return -1;
290}
291
292int ranap_iu_tx_sec_mode_cmd(struct ranap_ue_conn_ctx *uectx, struct osmo_auth_vector *vec,
293 int send_ck, int new_key)
294{
295 struct osmo_scu_prim *prim;
296 struct msgb *msg;
297
298 /* create RANAP message */
299 msg = ranap_new_msg_sec_mod_cmd(vec->ik, send_ck? vec->ck : NULL,
300 new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old);
301 msg->l2h = msg->data;
302 /* wrap RANAP message in SCCP N-DATA.req */
303 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
304 prim->u.data.conn_id = uectx->conn_id;
305 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
306 OSMO_SCU_PRIM_N_DATA,
307 PRIM_OP_REQUEST, msg);
308 osmo_sccp_user_sap_down(g_scu, &prim->oph);
309
310 return 0;
311}
312
313int ranap_iu_tx_common_id(struct ranap_ue_conn_ctx *uectx, const char *imsi)
314{
315 struct msgb *msg;
316 struct osmo_scu_prim *prim;
317
318 LOGPIU(LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n",
319 uectx->conn_id);
320
321 msg = ranap_new_msg_common_id(imsi);
322 msg->l2h = msg->data;
323 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
324 prim->u.data.conn_id = uectx->conn_id;
325 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
326 OSMO_SCU_PRIM_N_DATA,
327 PRIM_OP_REQUEST, msg);
328 osmo_sccp_user_sap_down(g_scu, &prim->oph);
329 return 0;
330}
331
332static int iu_grnc_id_parse(struct iu_grnc_id *dst, struct RANAP_GlobalRNC_ID *src)
333{
334 /* The size is coming from arbitrary sender, check it gracefully */
335 if (src->pLMNidentity.size != 3) {
336 LOGPIU(LOGL_ERROR, "Invalid PLMN Identity size:"
337 " should be 3, is %d\n", src->pLMNidentity.size);
338 return -1;
339 }
Neels Hofmeyrcf5fbea2018-03-21 15:50:41 +0100340 osmo_plmn_from_bcd(&src->pLMNidentity.buf[0], &dst->plmn);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200341 dst->rnc_id = (uint16_t)src->rNC_ID;
342 return 0;
343}
344
345#if 0
346 -- not used at present --
347static int iu_grnc_id_compose(struct iu_grnc_id *src, struct RANAP_GlobalRNC_ID *dst)
348{
349 /* The caller must ensure proper size */
350 OSMO_ASSERT(dst->pLMNidentity.size == 3);
351 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
352 src->mcc, src->mnc);
353 dst->rNC_ID = src->rnc_id;
354 return 0;
355}
356#endif
357
358struct new_ue_conn_ctx {
359 struct osmo_sccp_addr sccp_addr;
360 uint32_t conn_id;
361};
362
363static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
364{
365 struct new_ue_conn_ctx *new_ctx = ctx;
366 struct gprs_ra_id ra_id;
367 struct iu_grnc_id grnc_id;
368 uint16_t sai;
369 struct ranap_ue_conn_ctx *ue;
370 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
371 struct ranap_iu_rnc *rnc;
372
373 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
374 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
375 return -1;
376 }
377
378 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
379 ra_id.rac = asn1str_to_u8(&ies->rac);
380 }
381
382 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
383 LOGPIU(LOGL_ERROR,
384 "Failed to parse RANAP Global-RNC-ID IE\n");
385 return -1;
386 }
387
388 sai = asn1str_to_u16(&ies->sai.sAC);
389 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
390 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
391
392 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
393 rnc = iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &new_ctx->sccp_addr);
394
395 ue = ue_conn_ctx_alloc(rnc, new_ctx->conn_id);
396 OSMO_ASSERT(ue);
397 ue->ra_id = ra_id;
398
399 /* Feed into the MM layer */
400 msg->dst = ue;
401 global_iu_recv_cb(msg, &ra_id, &sai);
402
403 msgb_free(msg);
404
405 return 0;
406}
407
408static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
409{
410 struct gprs_ra_id _ra_id, *ra_id = NULL;
411 uint16_t _sai, *sai = NULL;
412 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
413
414 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
415 if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) {
416 LOGPIU(LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
417 return -1;
418 }
419 ra_id = &_ra_id;
420 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
421 _ra_id.rac = asn1str_to_u8(&ies->rac);
422 }
423 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
424 _sai = asn1str_to_u16(&ies->sai.sAC);
425 sai = &_sai;
426 }
427 }
428
429 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
430 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
431
432 /* Feed into the MM/CC/SMS-CP layer */
433 msg->dst = ctx;
434 global_iu_recv_cb(msg, ra_id, sai);
435
436 msgb_free(msg);
437
438 return 0;
439}
440
441static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
442{
443 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
444 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
445 ranap_cause_str(&ies->cause));
446 else
447 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
448
449 return 0;
450}
451
452int ranap_iu_tx(struct msgb *msg_nas, uint8_t sapi)
453{
454 struct ranap_ue_conn_ctx *uectx = msg_nas->dst;
455 struct msgb *msg;
456 struct osmo_scu_prim *prim;
457
458 LOGPIU(LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n",
459 uectx->conn_id);
460
461 msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas));
462 msgb_free(msg_nas);
463 msg->l2h = msg->data;
464 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
465 prim->u.data.conn_id = uectx->conn_id;
466 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
467 OSMO_SCU_PRIM_N_DATA,
468 PRIM_OP_REQUEST, msg);
469 osmo_sccp_user_sap_down(g_scu, &prim->oph);
470 return 0;
471}
472
473/* Send Iu Release for the given UE connection.
474 * If cause is NULL, the standard "No remaining RAB" cause is sent, otherwise
475 * the provided cause. */
476int ranap_iu_tx_release(struct ranap_ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
477{
478 struct msgb *msg;
479 struct osmo_scu_prim *prim;
480 static const struct RANAP_Cause default_cause = {
481 .present = RANAP_Cause_PR_radioNetwork,
482 .choice.radioNetwork = RANAP_CauseRadioNetwork_no_remaining_rab,
483 };
484
485 if (!cause)
486 cause = &default_cause;
487
488 msg = ranap_new_msg_iu_rel_cmd(cause);
489 msg->l2h = msg->data;
490 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
491 prim->u.data.conn_id = ctx->conn_id;
492 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
493 OSMO_SCU_PRIM_N_DATA,
494 PRIM_OP_REQUEST, msg);
495 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
496}
497
Alexander Couzensff796992019-09-10 17:46:54 +0200498void ranap_iu_tx_release_free(struct ranap_ue_conn_ctx *ctx,
499 const struct RANAP_Cause *cause,
500 int timeout)
501{
502 ctx->notification = false;
503 int ret = ranap_iu_tx_release(ctx, cause);
504 if (ret) {
505 ranap_iu_free_ue(ctx);
506 return;
507 }
508
509 osmo_timer_schedule(&ctx->release_timeout, timeout, 0);
510}
511
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200512static int ranap_handle_co_iu_rel_req(struct ranap_ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
513{
514 LOGPIU(LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
515 ranap_iu_tx_release(ctx, &ies->cause);
516 return 0;
517}
518
519static int ranap_handle_co_rab_ass_resp(struct ranap_ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
520{
521 int rc = -1;
522
523 LOGPIU(LOGL_INFO,
524 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
525 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
526 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
527 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
528 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
529
530 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
531 if (rc) {
532 LOGPIU(LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
533 return rc;
534 }
535
Alexander Couzens790212f2019-09-10 18:23:37 +0200536 rc = global_iu_event(ctx, RANAP_IU_EVENT_RAB_ASSIGN, &setup_ies);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200537
538 ranap_free_rab_setupormodifieditemies(&setup_ies);
539 }
540 /* FIXME: handle RAB Ass failure? */
541
542 return rc;
543}
544
545static void cn_ranap_handle_co_initial(void *ctx, ranap_message *message)
546{
547 int rc;
548
549 LOGPIU(LOGL_NOTICE, "handle_co_initial(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
550
551 if (message->direction != RANAP_RANAP_PDU_PR_initiatingMessage
552 || message->procedureCode != RANAP_ProcedureCode_id_InitialUE_Message) {
553 LOGPIU(LOGL_ERROR, "Expected direction 'InitiatingMessage',"
554 " procedureCode 'InitialUE_Message', instead got %u and %u\n",
555 message->direction, message->procedureCode);
556 rc = -1;
557 }
558 else
559 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
560
561 if (rc) {
562 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
563 /* TODO handling of the error? */
564 }
565}
566
567/* Entry point for connection-oriented RANAP message */
568static void cn_ranap_handle_co(void *ctx, ranap_message *message)
569{
570 int rc;
571
572 LOGPIU(LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
573
574 switch (message->direction) {
575 case RANAP_RANAP_PDU_PR_initiatingMessage:
576 switch (message->procedureCode) {
577 case RANAP_ProcedureCode_id_InitialUE_Message:
578 LOGPIU(LOGL_ERROR, "Got InitialUE_Message but this is not a new conn\n");
579 rc = -1;
580 break;
581 case RANAP_ProcedureCode_id_DirectTransfer:
582 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
583 break;
584 case RANAP_ProcedureCode_id_ErrorIndication:
585 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
586 break;
587 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
588 /* Iu Release Request */
589 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
590 break;
591 default:
592 LOGPIU(LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
593 message->procedureCode);
594 rc = -1;
595 break;
596 }
597 break;
598 case RANAP_RANAP_PDU_PR_successfulOutcome:
599 switch (message->procedureCode) {
600 case RANAP_ProcedureCode_id_SecurityModeControl:
601 /* Security Mode Complete */
Alexander Couzens790212f2019-09-10 18:23:37 +0200602 rc = global_iu_event(ctx, RANAP_IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200603 break;
604 case RANAP_ProcedureCode_id_Iu_Release:
605 /* Iu Release Complete */
Alexander Couzens790212f2019-09-10 18:23:37 +0200606 rc = global_iu_event(ctx, RANAP_IU_EVENT_IU_RELEASE, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200607 if (rc) {
608 LOGPIU(LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
609 rc);
610 }
611 break;
612 default:
613 LOGPIU(LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
614 message->procedureCode);
615 rc = -1;
616 break;
617 }
618 break;
619 case RANAP_RANAP_PDU_PR_outcome:
620 switch (message->procedureCode) {
621 case RANAP_ProcedureCode_id_RAB_Assignment:
622 /* RAB Assignment Response */
623 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
624 break;
625 default:
626 LOGPIU(LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
627 message->procedureCode);
628 rc = -1;
629 break;
630 }
631 break;
632 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
633 default:
634 LOGPIU(LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
635 message->procedureCode);
636 rc = -1;
637 break;
638 }
639
640 if (rc) {
641 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
642 /* TODO handling of the error? */
643 }
644}
645
646static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
647{
Harald Welte8fa35b82019-04-20 20:13:31 +0200648 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) ctx;
649 struct osmo_scu_unitdata_param *ud_prim = &prim->u.unitdata;
650 RANAP_GlobalRNC_ID_t *grnc_id = NULL;
651 struct msgb *resp;
652
653 OSMO_ASSERT(prim->oph.primitive == OSMO_SCU_PRIM_N_UNITDATA);
654
655 /* FIXME: verify ies.cN_DomainIndicator */
656
657 if (ies->presenceMask & RESETIES_RANAP_GLOBALRNC_ID_PRESENT)
658 grnc_id = &ies->globalRNC_ID;
659
660 /* send reset response */
661 resp = ranap_new_msg_reset_ack(ies->cN_DomainIndicator, grnc_id);
662 if (!resp)
663 return -ENOMEM;
664 resp->l2h = resp->data;
665 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, &ud_prim->calling_addr, resp);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200666}
667
668static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
669{
670 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
671 LOGPIU(LOGL_ERROR, "Rx Error Indication (%s)\n",
672 ranap_cause_str(&ies->cause));
673 else
674 LOGPIU(LOGL_ERROR, "Rx Error Indication\n");
675
676 return 0;
677}
678
679/* Entry point for connection-less RANAP message */
680static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
681{
682 int rc;
683
684 switch (message->direction) {
685 case RANAP_RANAP_PDU_PR_initiatingMessage:
686 switch (message->procedureCode) {
687 case RANAP_ProcedureCode_id_Reset:
688 /* received reset.req, send reset.resp */
689 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
690 break;
691 case RANAP_ProcedureCode_id_ErrorIndication:
692 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
693 break;
694 default:
695 rc = -1;
696 break;
697 }
698 break;
699 case RANAP_RANAP_PDU_PR_successfulOutcome:
700 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
701 case RANAP_RANAP_PDU_PR_outcome:
702 default:
703 rc = -1;
704 break;
705 }
706
707 if (rc) {
708 LOGPIU(LOGL_ERROR, "Error in %s (%d)\n", __func__, rc);
709 /* TODO handling of the error? */
710 }
711}
712
713/***********************************************************************
714 * Paging
715 ***********************************************************************/
716
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200717/* Send a paging command down a given SCCP User. tmsi and paging_cause are
718 * optional and may be passed NULL and 0, respectively, to disable their use.
719 * See enum RANAP_PagingCause.
720 *
721 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
722 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
723static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr,
724 const char *imsi, const uint32_t *tmsi,
725 bool is_ps, uint32_t paging_cause)
726{
727 struct msgb *msg;
728 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
729 msg->l2h = msg->data;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100730 return osmo_sccp_tx_unitdata_msg(g_scu, &g_local_sccp_addr, called_addr, msg);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200731}
732
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100733static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptmsi,
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200734 uint16_t lac, uint8_t rac, bool is_ps)
735{
736 struct ranap_iu_rnc *rnc;
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100737 const char *log_msg;
738 int log_level;
739 int paged = 0;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200740
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100741 iu_rnc_lac_rac_find(&rnc, NULL, lac, rac);
742 if (rnc) {
743 if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptmsi, is_ps, 0) == 0) {
744 log_msg = "Paging";
745 log_level = LOGL_DEBUG;
746 paged = 1;
747 } else {
748 log_msg = "Paging failed";
749 log_level = LOGL_ERROR;
750 }
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200751 } else {
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100752 log_msg = "Found no RNC to Page";
753 log_level = LOGL_ERROR;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200754 }
755
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100756 if (is_ps)
757 LOGPIU(log_level, "IuPS: %s on LAC %d RAC %d", log_msg, lac, rac);
758 else
759 LOGPIU(log_level, "IuCS: %s on LAC %d", log_msg, lac);
760 if (rnc)
761 LOGPIUC(log_level, " at SCCP-addr %s", osmo_sccp_addr_dump(&rnc->sccp_addr));
762 if (tmsi_or_ptmsi)
763 LOGPIUC(log_level, ", for %s %08x\n", is_ps? "PTMSI" : "TMSI", *tmsi_or_ptmsi);
764 else
765 LOGPIUC(log_level, ", for IMSI %s\n", imsi);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200766
Neels Hofmeyr69888c82017-12-15 02:54:45 +0100767 return paged;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200768}
769
770int ranap_iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
771{
772 return iu_page(imsi, tmsi, lac, 0, false);
773}
774
775int ranap_iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
776{
777 return iu_page(imsi, ptmsi, lac, rac, true);
778}
779
780
781/***********************************************************************
782 *
783 ***********************************************************************/
784
785int tx_unitdata(struct osmo_sccp_user *scu);
786int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id);
787
788struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
789struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
790
791static struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
792{
793 struct msgb *msg = msgb_alloc(1024, "conn_resp");
794 struct osmo_scu_prim *prim;
795
796 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
797 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
798 OSMO_SCU_PRIM_N_CONNECT,
799 PRIM_OP_RESPONSE, msg);
800 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
801 return &prim->oph;
802}
803
804static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
805{
806 struct osmo_sccp_user *scu = _scu;
807 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
808 struct osmo_prim_hdr *resp = NULL;
Alexander Couzens80307922019-08-13 17:13:59 +0200809 int rc = -1;
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200810 struct ranap_ue_conn_ctx *ue;
811 struct new_ue_conn_ctx new_ctx = {};
812
813 LOGPIU(LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
814
815 switch (OSMO_PRIM_HDR(oph)) {
816 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
817 /* confirmation of outbound connection */
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200818 break;
819 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
820 /* indication of new inbound connection request*/
821 LOGPIU(LOGL_DEBUG, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
822 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
823 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
824 LOGPIU(LOGL_NOTICE,
825 "Received invalid N-CONNECT.ind\n");
826 return 0;
827 }
828 new_ctx.sccp_addr = prim->u.connect.calling_addr;
829 new_ctx.conn_id = prim->u.connect.conn_id;
830 /* first ensure the local SCCP socket is ACTIVE */
831 resp = make_conn_resp(&prim->u.connect);
832 osmo_sccp_user_sap_down(scu, resp);
833 /* then handle the RANAP payload */
834 rc = ranap_cn_rx_co(cn_ranap_handle_co_initial, &new_ctx, msgb_l2(oph->msg), msgb_l2len(oph->msg));
835 break;
836 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
837 /* indication of disconnect */
838 LOGPIU(LOGL_DEBUG, "N-DISCONNECT.ind(%u)\n",
839 prim->u.disconnect.conn_id);
840 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200841 if (!ue)
842 break;
843
Alexander Couzens4ae261b2019-08-13 17:14:31 +0200844 rc = 0;
845 if (msgb_l2len(oph->msg) > 0)
846 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 +0200847
848 /* A Iu Release event might be used to free the UE in cn_ranap_handle_co. */
849 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
850 if (!ue)
851 break;
852
Alexander Couzens790212f2019-09-10 18:23:37 +0200853 global_iu_event(ue, RANAP_IU_EVENT_LINK_INVALIDATED, NULL);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200854 break;
855 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
856 /* connection-oriented data received */
857 LOGPIU(LOGL_DEBUG, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
858 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
859 /* resolve UE context */
860 ue = ue_conn_ctx_find(prim->u.data.conn_id);
Alexander Couzens87482b32019-08-13 17:06:17 +0200861 if (!ue)
862 break;
863
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200864 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
865 break;
866 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
867 /* connection-less data received */
868 LOGPIU(LOGL_DEBUG, "N-UNITDATA.ind(%s)\n",
869 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Harald Welte8fa35b82019-04-20 20:13:31 +0200870 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 +0200871 break;
872 default:
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200873 break;
874 }
875
876 msgb_free(oph->msg);
877 return rc;
878}
879
880int ranap_iu_init(void *ctx, int log_subsystem, const char *sccp_user_name, struct osmo_sccp_instance *sccp,
881 ranap_iu_recv_cb_t iu_recv_cb, ranap_iu_event_cb_t iu_event_cb)
882{
883 iu_log_subsystem = log_subsystem;
884 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
885 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
886
887 global_iu_recv_cb = iu_recv_cb;
888 global_iu_event_cb = iu_event_cb;
889 g_sccp = sccp;
Neels Hofmeyr1aef9a62017-08-12 18:02:44 +0200890 osmo_sccp_local_addr_by_instance(&g_local_sccp_addr, sccp, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyraae68b22017-07-05 14:38:52 +0200891 g_scu = osmo_sccp_user_bind(g_sccp, sccp_user_name, sccp_sap_up, OSMO_SCCP_SSN_RANAP);
892
893 return 0;
894}