blob: 779497745c0e4618147709240239a82bbde50b5a [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
Philipp Maier4b60d072017-04-09 12:32:51 +020039#include <osmocom/sigtran/osmo_ss7.h>
Neels Hofmeyrbfa88782016-05-20 21:38:32 +020040#include <osmocom/sigtran/sccp_sap.h>
41#include <osmocom/sigtran/sccp_helpers.h>
Philipp Maier4b60d072017-04-09 12:32:51 +020042#include <osmocom/sigtran/protocol/m3ua.h>
Neels Hofmeyrbfa88782016-05-20 21:38:32 +020043
44#include <openbsc/gprs_sgsn.h>
45#include <openbsc/iu.h>
46#include <openbsc/debug.h>
47
48#include <pdp.h>
49
50#include <osmocom/ranap/ranap_ies_defs.h>
51#include <osmocom/ranap/ranap_common.h>
52#include <osmocom/ranap/ranap_common_cn.h>
53#include <osmocom/ranap/ranap_msg_factory.h>
54
55#include <asn1c/asn1helpers.h>
56
57/* Parsed global RNC id. See also struct RANAP_GlobalRNC_ID, and note that the
58 * PLMN identity is a BCD representation of the MCC and MNC.
59 * See iu_grnc_id_parse(). */
60struct iu_grnc_id {
61 uint16_t mcc;
62 uint16_t mnc;
63 uint16_t rnc_id;
64};
65
66/* A remote RNC (Radio Network Controller, like BSC but for UMTS) that has
Philipp Maier4b60d072017-04-09 12:32:51 +020067 * called us and is currently reachable at the given osmo_sccp_addr. So, when we
Neels Hofmeyrbfa88782016-05-20 21:38:32 +020068 * know a LAC for a subscriber, we can page it at the RNC matching that LAC or
69 * RAC. An HNB-GW typically presents itself as if it were a single RNC, even
70 * though it may have several RNCs in hNodeBs connected to it. Those will then
71 * share the same RNC id, which they actually receive and adopt from the HNB-GW
72 * in the HNBAP HNB REGISTER ACCEPT message. */
73struct iu_rnc {
74 struct llist_head entry;
75
76 uint16_t rnc_id;
77 uint16_t lac; /* Location Area Code (used for CS and PS) */
78 uint8_t rac; /* Routing Area Code (used for PS only) */
Philipp Maier4b60d072017-04-09 12:32:51 +020079 struct osmo_sccp_addr sccp_addr;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +020080};
81
82void *talloc_iu_ctx;
83
Neels Hofmeyra1756f32016-05-20 21:59:55 +020084/* Implement the extern asn_debug from libasn1c to indicate whether to print
85 * asn.1 debug messages (see libasn1c). */
86int asn_debug = 0;
87
88/* Implement the extern asn1_xer_print to indicate whether the ASN.1 binary
89 * code decoded and encoded during Iu communication should be logged to stderr
90 * (see asn.1 generated code in osmo-iuh). */
91int asn1_xer_print = 0;
92
Neels Hofmeyrbfa88782016-05-20 21:38:32 +020093void *talloc_asn1_ctx;
94
95iu_recv_cb_t global_iu_recv_cb = NULL;
96iu_event_cb_t global_iu_event_cb = NULL;
97
98static LLIST_HEAD(ue_conn_ctx_list);
99static LLIST_HEAD(rnc_list);
100
Philipp Maier4b60d072017-04-09 12:32:51 +0200101static struct osmo_sccp_instance *g_sccp;
102static struct osmo_sccp_user *g_scu;
103
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200104const struct value_string iu_event_type_names[] = {
Neels Hofmeyrb42dc432016-12-16 14:16:53 +0100105 OSMO_VALUE_STRING(IU_EVENT_RAB_ASSIGN),
106 OSMO_VALUE_STRING(IU_EVENT_SECURITY_MODE_COMPLETE),
107 OSMO_VALUE_STRING(IU_EVENT_IU_RELEASE),
108 OSMO_VALUE_STRING(IU_EVENT_LINK_INVALIDATED),
Neels Hofmeyrecdfd6d2016-12-16 14:16:41 +0100109 { 0, NULL }
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200110};
111
Philipp Maier4b60d072017-04-09 12:32:51 +0200112struct ue_conn_ctx *ue_conn_ctx_alloc(struct osmo_sccp_addr *addr, uint32_t conn_id)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200113{
114 struct ue_conn_ctx *ctx = talloc_zero(talloc_iu_ctx, struct ue_conn_ctx);
115
Philipp Maier4b60d072017-04-09 12:32:51 +0200116 ctx->sccp_addr = *addr;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200117 ctx->conn_id = conn_id;
118 llist_add(&ctx->list, &ue_conn_ctx_list);
119
120 return ctx;
121}
122
Philipp Maier4b60d072017-04-09 12:32:51 +0200123struct ue_conn_ctx *ue_conn_ctx_find(uint32_t conn_id)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200124{
125 struct ue_conn_ctx *ctx;
126
127 llist_for_each_entry(ctx, &ue_conn_ctx_list, list) {
Philipp Maier4b60d072017-04-09 12:32:51 +0200128 if (ctx->conn_id == conn_id)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200129 return ctx;
130 }
131 return NULL;
132}
133
134static struct iu_rnc *iu_rnc_alloc(uint16_t rnc_id, uint16_t lac, uint8_t rac,
Philipp Maier4b60d072017-04-09 12:32:51 +0200135 struct osmo_sccp_addr *addr)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200136{
137 struct iu_rnc *rnc = talloc_zero(talloc_iu_ctx, struct iu_rnc);
138
139 rnc->rnc_id = rnc_id;
140 rnc->lac = lac;
141 rnc->rac = rac;
Philipp Maier4b60d072017-04-09 12:32:51 +0200142 rnc->sccp_addr = *addr;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200143 llist_add(&rnc->entry, &rnc_list);
144
145 LOGP(DRANAP, LOGL_NOTICE, "New RNC %d (LAC=%d RAC=%d)\n",
146 rnc->rnc_id, rnc->lac, rnc->rac);
147
148 return rnc;
149}
150
151static struct iu_rnc *iu_rnc_register(uint16_t rnc_id, uint16_t lac,
Philipp Maier4b60d072017-04-09 12:32:51 +0200152 uint8_t rac, struct osmo_sccp_addr *addr)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200153{
154 struct iu_rnc *rnc;
155 llist_for_each_entry(rnc, &rnc_list, entry) {
156 if (rnc->rnc_id != rnc_id)
157 continue;
158
159 /* We have this RNC Id registered already. Make sure that the
160 * details match. */
161
162 /* TODO should a mismatch be an error? */
163 if (rnc->lac != lac || rnc->rac != rac)
164 LOGP(DRANAP, LOGL_NOTICE, "RNC %d changes its details:"
165 " LAC=%d RAC=%d --> LAC=%d RAC=%d\n",
166 rnc->rnc_id, rnc->lac, rnc->rac,
167 lac, rac);
168 rnc->lac = lac;
169 rnc->rac = rac;
170
Philipp Maier4b60d072017-04-09 12:32:51 +0200171 if (addr && memcmp(&rnc->sccp_addr, addr, sizeof(*addr)))
172 LOGP(DRANAP, LOGL_NOTICE, "RNC %d on New SCCP Addr %s"
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200173 " (LAC=%d RAC=%d)\n",
Philipp Maier4b60d072017-04-09 12:32:51 +0200174 rnc->rnc_id, osmo_sccp_addr_dump(addr), rnc->lac, rnc->rac);
175 rnc->sccp_addr = *addr;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200176 return rnc;
177 }
178
179 /* Not found, make a new one. */
Philipp Maier4b60d072017-04-09 12:32:51 +0200180 return iu_rnc_alloc(rnc_id, lac, rac, addr);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200181}
182
183/***********************************************************************
184 * RANAP handling
185 ***********************************************************************/
186
187int iu_rab_act(struct ue_conn_ctx *ue_ctx, struct msgb *msg)
188{
189 struct osmo_scu_prim *prim;
190
191 /* wrap RANAP message in SCCP N-DATA.req */
192 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
193 prim->u.data.conn_id = ue_ctx->conn_id;
194 osmo_prim_init(&prim->oph,
195 SCCP_SAP_USER,
196 OSMO_SCU_PRIM_N_DATA,
197 PRIM_OP_REQUEST,
198 msg);
Philipp Maier4b60d072017-04-09 12:32:51 +0200199 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200200}
201
202int iu_rab_deact(struct ue_conn_ctx *ue_ctx, uint8_t rab_id)
203{
204 /* FIXME */
205 return -1;
206}
207
208int iu_tx_sec_mode_cmd(struct ue_conn_ctx *uectx, struct gsm_auth_tuple *tp,
209 int send_ck, int new_key)
210{
211 struct osmo_scu_prim *prim;
212 struct msgb *msg;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200213
214 /* create RANAP message */
Neels Hofmeyr3eb991e2017-03-04 02:28:19 +0100215 msg = ranap_new_msg_sec_mod_cmd(tp->vec.ik, send_ck? tp->vec.ck : NULL,
216 new_key ? RANAP_KeyStatus_new : RANAP_KeyStatus_old);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200217 msg->l2h = msg->data;
218 /* wrap RANAP message in SCCP N-DATA.req */
219 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
220 prim->u.data.conn_id = uectx->conn_id;
221 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
222 OSMO_SCU_PRIM_N_DATA,
223 PRIM_OP_REQUEST, msg);
Philipp Maier4b60d072017-04-09 12:32:51 +0200224 osmo_sccp_user_sap_down(g_scu, &prim->oph);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200225
226 return 0;
227}
228
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200229int iu_tx_common_id(struct ue_conn_ctx *uectx, const char *imsi)
230{
231 struct msgb *msg;
232 struct osmo_scu_prim *prim;
233
Philipp Maier4b60d072017-04-09 12:32:51 +0200234 LOGP(DRANAP, LOGL_INFO, "Transmitting RANAP CommonID (SCCP conn_id %u)\n",
235 uectx->conn_id);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200236
237 msg = ranap_new_msg_common_id(imsi);
238 msg->l2h = msg->data;
239 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
240 prim->u.data.conn_id = uectx->conn_id;
241 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
242 OSMO_SCU_PRIM_N_DATA,
243 PRIM_OP_REQUEST, msg);
Philipp Maier4b60d072017-04-09 12:32:51 +0200244 osmo_sccp_user_sap_down(g_scu, &prim->oph);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200245 return 0;
246}
247
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200248static int iu_grnc_id_parse(struct iu_grnc_id *dst,
249 struct RANAP_GlobalRNC_ID *src)
250{
251 /* The size is coming from arbitrary sender, check it gracefully */
252 if (src->pLMNidentity.size != 3) {
253 LOGP(DRANAP, LOGL_ERROR, "Invalid PLMN Identity size:"
254 " should be 3, is %d\n", src->pLMNidentity.size);
255 return -1;
256 }
257 gsm48_mcc_mnc_from_bcd(&src->pLMNidentity.buf[0],
258 &dst->mcc, &dst->mnc);
259 dst->rnc_id = (uint16_t)src->rNC_ID;
260 return 0;
261}
262
263#if 0
264 -- not used at present --
265static int iu_grnc_id_compose(struct iu_grnc_id *src,
266 struct RANAP_GlobalRNC_ID *dst)
267{
268 /* The caller must ensure proper size */
269 OSMO_ASSERT(dst->pLMNidentity.size == 3);
270 gsm48_mcc_mnc_to_bcd(&dst->pLMNidentity.buf[0],
271 src->mcc, src->mnc);
272 dst->rNC_ID = src->rnc_id;
273 return 0;
274}
275#endif
276
277static int ranap_handle_co_initial_ue(void *ctx, RANAP_InitialUE_MessageIEs_t *ies)
278{
279 struct ue_conn_ctx *ue_conn = ctx;
280 struct gprs_ra_id ra_id;
281 struct iu_grnc_id grnc_id;
282 uint16_t sai;
283 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
284
285 if (ranap_parse_lai(&ra_id, &ies->lai) != 0) {
286 LOGP(DRANAP, LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
287 return -1;
288 }
289
290 if (ies->presenceMask & INITIALUE_MESSAGEIES_RANAP_RAC_PRESENT) {
291 ra_id.rac = asn1str_to_u8(&ies->rac);
292 }
293
294 if (iu_grnc_id_parse(&grnc_id, &ies->globalRNC_ID) != 0) {
295 LOGP(DRANAP, LOGL_ERROR,
296 "Failed to parse RANAP Global-RNC-ID IE\n");
297 return -1;
298 }
299
300 sai = asn1str_to_u16(&ies->sai.sAC);
301 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
302 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
303
304 /* Make sure we know the RNC Id and LAC+RAC coming in on this connection. */
Philipp Maier4b60d072017-04-09 12:32:51 +0200305 iu_rnc_register(grnc_id.rnc_id, ra_id.lac, ra_id.rac, &ue_conn->sccp_addr);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200306 ue_conn->ra_id = ra_id;
307
308 /* Feed into the MM layer */
309 msg->dst = ctx;
310 global_iu_recv_cb(msg, &ra_id, &sai);
311
312 msgb_free(msg);
313
314 return 0;
315}
316
317static int ranap_handle_co_dt(void *ctx, RANAP_DirectTransferIEs_t *ies)
318{
319 struct gprs_ra_id _ra_id, *ra_id = NULL;
320 uint16_t _sai, *sai = NULL;
321 struct msgb *msg = msgb_alloc(256, "RANAP->NAS");
322
323 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_LAI_PRESENT) {
324 if (ranap_parse_lai(&_ra_id, &ies->lai) != 0) {
325 LOGP(DRANAP, LOGL_ERROR, "Failed to parse RANAP LAI IE\n");
326 return -1;
327 }
328 ra_id = &_ra_id;
329 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_RAC_PRESENT) {
330 _ra_id.rac = asn1str_to_u8(&ies->rac);
331 }
332 if (ies->presenceMask & DIRECTTRANSFERIES_RANAP_SAI_PRESENT) {
333 _sai = asn1str_to_u16(&ies->sai.sAC);
334 sai = &_sai;
335 }
336 }
337
338 msgb_gmmh(msg) = msgb_put(msg, ies->nas_pdu.size);
339 memcpy(msgb_gmmh(msg), ies->nas_pdu.buf, ies->nas_pdu.size);
340
341 /* Feed into the MM/CC/SMS-CP layer */
342 msg->dst = ctx;
343 global_iu_recv_cb(msg, ra_id, sai);
344
345 msgb_free(msg);
346
347 return 0;
348}
349
350static int ranap_handle_co_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
351{
352 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
353 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
354 ranap_cause_str(&ies->cause));
355 else
356 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
357
358 return 0;
359}
360
361int iu_tx(struct msgb *msg_nas, uint8_t sapi)
362{
363 struct ue_conn_ctx *uectx = msg_nas->dst;
364 struct msgb *msg;
365 struct osmo_scu_prim *prim;
366
Philipp Maier4b60d072017-04-09 12:32:51 +0200367 LOGP(DRANAP, LOGL_INFO, "Transmitting L3 Message as RANAP DT (SCCP conn_id %u)\n",
368 uectx->conn_id);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200369
370 msg = ranap_new_msg_dt(sapi, msg_nas->data, msgb_length(msg_nas));
371 msgb_free(msg_nas);
372 msg->l2h = msg->data;
373 prim = (struct osmo_scu_prim *) msgb_push(msg, sizeof(*prim));
374 prim->u.data.conn_id = uectx->conn_id;
375 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
376 OSMO_SCU_PRIM_N_DATA,
377 PRIM_OP_REQUEST, msg);
Philipp Maier4b60d072017-04-09 12:32:51 +0200378 osmo_sccp_user_sap_down(g_scu, &prim->oph);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200379 return 0;
380}
381
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200382/* Send Iu Release for the given UE connection.
383 * If cause is NULL, the standard "No remaining RAB" cause is sent, otherwise
384 * the provided cause. */
385int iu_tx_release(struct ue_conn_ctx *ctx, const struct RANAP_Cause *cause)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200386{
387 struct msgb *msg;
388 struct osmo_scu_prim *prim;
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200389 static const struct RANAP_Cause default_cause = {
390 .present = RANAP_Cause_PR_radioNetwork,
391 .choice.radioNetwork = RANAP_CauseRadioNetwork_no_remaining_rab,
392 };
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200393
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200394 if (!cause)
395 cause = &default_cause;
396
Philipp Maier4b60d072017-04-09 12:32:51 +0200397 LOGP(DRANAP, LOGL_INFO, "Transmitting Iu Release (SCCP conn_id %u)\n",
398 ctx->conn_id);
399
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200400 msg = ranap_new_msg_iu_rel_cmd(cause);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200401 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);
Philipp Maier4b60d072017-04-09 12:32:51 +0200407 return osmo_sccp_user_sap_down(g_scu, &prim->oph);
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200408}
409
410static int ranap_handle_co_iu_rel_req(struct ue_conn_ctx *ctx, RANAP_Iu_ReleaseRequestIEs_t *ies)
411{
412 LOGP(DRANAP, LOGL_INFO, "Received Iu Release Request, Sending Release Command\n");
413 iu_tx_release(ctx, &ies->cause);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200414 return 0;
415}
416
417static int ranap_handle_co_rab_ass_resp(struct ue_conn_ctx *ctx, RANAP_RAB_AssignmentResponseIEs_t *ies)
418{
419 int rc = -1;
420
Neels Hofmeyr87c00562016-10-18 20:19:50 +0200421 LOGP(DRANAP, LOGL_INFO,
422 "Rx RAB Assignment Response for UE conn_id %u\n", ctx->conn_id);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200423 if (ies->presenceMask & RAB_ASSIGNMENTRESPONSEIES_RANAP_RAB_SETUPORMODIFIEDLIST_PRESENT) {
424 /* TODO: Iterate over list of SetupOrModifiedList IEs and handle each one */
425 RANAP_IE_t *ranap_ie = ies->raB_SetupOrModifiedList.raB_SetupOrModifiedList_ies.list.array[0];
426 RANAP_RAB_SetupOrModifiedItemIEs_t setup_ies;
427
428 rc = ranap_decode_rab_setupormodifieditemies_fromlist(&setup_ies, &ranap_ie->value);
429 if (rc) {
430 LOGP(DRANAP, LOGL_ERROR, "Error in ranap_decode_rab_setupormodifieditemies()\n");
431 return rc;
432 }
433
434 rc = global_iu_event_cb(ctx, IU_EVENT_RAB_ASSIGN, &setup_ies);
435
436 ranap_free_rab_setupormodifieditemies(&setup_ies);
437 }
Neels Hofmeyra1756f32016-05-20 21:59:55 +0200438 /* FIXME: handle RAB Ass failure? */
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200439
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200440 return rc;
441}
442
443/* Entry point for connection-oriented RANAP message */
444static void cn_ranap_handle_co(void *ctx, ranap_message *message)
445{
446 int rc;
447
448 LOGP(DRANAP, LOGL_NOTICE, "handle_co(dir=%u, proc=%u)\n", message->direction, message->procedureCode);
449
450 switch (message->direction) {
451 case RANAP_RANAP_PDU_PR_initiatingMessage:
452 switch (message->procedureCode) {
453 case RANAP_ProcedureCode_id_InitialUE_Message:
454 rc = ranap_handle_co_initial_ue(ctx, &message->msg.initialUE_MessageIEs);
455 break;
456 case RANAP_ProcedureCode_id_DirectTransfer:
457 rc = ranap_handle_co_dt(ctx, &message->msg.directTransferIEs);
458 break;
459 case RANAP_ProcedureCode_id_ErrorIndication:
460 rc = ranap_handle_co_err_ind(ctx, &message->msg.errorIndicationIEs);
461 break;
462 case RANAP_ProcedureCode_id_Iu_ReleaseRequest:
463 /* Iu Release Request */
464 rc = ranap_handle_co_iu_rel_req(ctx, &message->msg.iu_ReleaseRequestIEs);
465 break;
466 default:
467 LOGP(DRANAP, LOGL_ERROR, "Received Initiating Message: unknown Procedure Code %d\n",
468 message->procedureCode);
469 rc = -1;
470 break;
471 }
472 break;
473 case RANAP_RANAP_PDU_PR_successfulOutcome:
474 switch (message->procedureCode) {
475 case RANAP_ProcedureCode_id_SecurityModeControl:
476 /* Security Mode Complete */
477 rc = global_iu_event_cb(ctx, IU_EVENT_SECURITY_MODE_COMPLETE, NULL);
478 break;
479 case RANAP_ProcedureCode_id_Iu_Release:
480 /* Iu Release Complete */
481 rc = global_iu_event_cb(ctx, IU_EVENT_IU_RELEASE, NULL);
482 if (rc) {
483 LOGP(DRANAP, LOGL_ERROR, "Iu Release event: Iu Event callback returned %d\n",
484 rc);
485 }
486 break;
487 default:
488 LOGP(DRANAP, LOGL_ERROR, "Received Successful Outcome: unknown Procedure Code %d\n",
489 message->procedureCode);
490 rc = -1;
491 break;
492 }
493 break;
494 case RANAP_RANAP_PDU_PR_outcome:
495 switch (message->procedureCode) {
496 case RANAP_ProcedureCode_id_RAB_Assignment:
497 /* RAB Assignment Response */
498 rc = ranap_handle_co_rab_ass_resp(ctx, &message->msg.raB_AssignmentResponseIEs);
499 break;
500 default:
501 LOGP(DRANAP, LOGL_ERROR, "Received Outcome: unknown Procedure Code %d\n",
502 message->procedureCode);
503 rc = -1;
504 break;
505 }
506 break;
507 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
508 default:
509 LOGP(DRANAP, LOGL_ERROR, "Received Unsuccessful Outcome: Procedure Code %d\n",
510 message->procedureCode);
511 rc = -1;
512 break;
513 }
514
515 if (rc) {
516 LOGP(DRANAP, LOGL_ERROR, "Error in cn_ranap_handle_co (%d)\n",
517 rc);
518 /* TODO handling of the error? */
519 }
520}
521
522static int ranap_handle_cl_reset_req(void *ctx, RANAP_ResetIEs_t *ies)
523{
524 /* FIXME: send reset response */
525 return -1;
526}
527
528static int ranap_handle_cl_err_ind(void *ctx, RANAP_ErrorIndicationIEs_t *ies)
529{
530 if (ies->presenceMask & ERRORINDICATIONIES_RANAP_CAUSE_PRESENT)
531 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication (%s)\n",
532 ranap_cause_str(&ies->cause));
533 else
534 LOGP(DRANAP, LOGL_ERROR, "Rx Error Indication\n");
535
536 return 0;
537}
538
539/* Entry point for connection-less RANAP message */
540static void cn_ranap_handle_cl(void *ctx, ranap_message *message)
541{
542 int rc;
543
544 switch (message->direction) {
545 case RANAP_RANAP_PDU_PR_initiatingMessage:
546 switch (message->procedureCode) {
547 case RANAP_ProcedureCode_id_Reset:
548 /* received reset.req, send reset.resp */
549 rc = ranap_handle_cl_reset_req(ctx, &message->msg.resetIEs);
550 break;
551 case RANAP_ProcedureCode_id_ErrorIndication:
552 rc = ranap_handle_cl_err_ind(ctx, &message->msg.errorIndicationIEs);
553 break;
554 default:
555 rc = -1;
556 break;
557 }
558 break;
559 case RANAP_RANAP_PDU_PR_successfulOutcome:
560 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
561 case RANAP_RANAP_PDU_PR_outcome:
562 default:
563 rc = -1;
564 break;
565 }
566
567 if (rc) {
568 LOGP(DRANAP, LOGL_ERROR, "Error in cn_ranap_handle_cl (%d)\n",
569 rc);
570 /* TODO handling of the error? */
571 }
572}
573
574/***********************************************************************
575 * Paging
576 ***********************************************************************/
577
Philipp Maier4b60d072017-04-09 12:32:51 +0200578struct osmo_sccp_addr local_sccp_addr = {
579 .presence = OSMO_SCCP_ADDR_T_SSN | OSMO_SCCP_ADDR_T_PC,
580 .ri = OSMO_SCCP_RI_SSN_PC,
581 .ssn = OSMO_SCCP_SSN_RANAP,
582 .pc = 1,
583};
584
585/* Send a paging command down a given SCCP User. tmsi and paging_cause are
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200586 * optional and may be passed NULL and 0, respectively, to disable their use.
587 * See enum RANAP_PagingCause.
588 *
589 * If TMSI is given, the IMSI is not sent over the air interface. Nevertheless,
590 * the IMSI is still required for resolution in the HNB-GW and/or(?) RNC. */
Philipp Maier4b60d072017-04-09 12:32:51 +0200591static int iu_tx_paging_cmd(struct osmo_sccp_addr *called_addr,
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200592 const char *imsi, const uint32_t *tmsi,
593 bool is_ps, uint32_t paging_cause)
594{
595 struct msgb *msg;
596 msg = ranap_new_msg_paging_cmd(imsi, tmsi, is_ps? 1 : 0, paging_cause);
597 msg->l2h = msg->data;
Philipp Maier4b60d072017-04-09 12:32:51 +0200598 osmo_sccp_tx_unitdata_msg(g_scu, &local_sccp_addr, called_addr, msg);
599 return 0;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200600}
601
602static int iu_page(const char *imsi, const uint32_t *tmsi_or_ptimsi,
603 uint16_t lac, uint8_t rac, bool is_ps)
604{
605 struct iu_rnc *rnc;
606 int pagings_sent = 0;
607
608 if (tmsi_or_ptimsi) {
609 LOGP(DRANAP, LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s"
610 " (paging will use %s %x)\n",
611 is_ps? "IuPS" : "IuCS",
612 imsi,
613 is_ps? "PTMSI" : "TMSI",
614 *tmsi_or_ptimsi);
615 } else {
616 LOGP(DRANAP, LOGL_DEBUG, "%s: Looking for RNCs to page for IMSI %s"
617 " (paging will use IMSI)\n",
618 is_ps? "IuPS" : "IuCS",
619 imsi
620 );
621 }
622
623 llist_for_each_entry(rnc, &rnc_list, entry) {
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200624 if (rnc->lac != lac)
625 continue;
626 if (is_ps && rnc->rac != rac)
627 continue;
628
629 /* Found a match! */
Philipp Maier4b60d072017-04-09 12:32:51 +0200630 if (iu_tx_paging_cmd(&rnc->sccp_addr, imsi, tmsi_or_ptimsi, is_ps, 0)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200631 == 0) {
632 LOGP(DRANAP, LOGL_DEBUG,
Philipp Maier4b60d072017-04-09 12:32:51 +0200633 "%s: Paged for IMSI %s on RNC %d, on SCCP addr %s\n",
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200634 is_ps? "IuPS" : "IuCS",
Philipp Maier4b60d072017-04-09 12:32:51 +0200635 imsi, rnc->rnc_id, osmo_sccp_addr_dump(&rnc->sccp_addr));
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200636 pagings_sent ++;
637 }
638 }
639
640 /* Some logging... */
641 if (pagings_sent > 0) {
642 LOGP(DRANAP, LOGL_DEBUG,
643 "%s: %d RNCs were paged for IMSI %s.\n",
644 is_ps? "IuPS" : "IuCS",
645 pagings_sent, imsi);
646 }
647 else {
648 if (is_ps) {
649 LOGP(DRANAP, LOGL_ERROR, "IuPS: Found no RNC to page for"
650 " LAC %d RAC %d (would have paged IMSI %s)\n",
651 lac, rac, imsi);
652 }
653 else {
654 LOGP(DRANAP, LOGL_ERROR, "IuCS: Found no RNC to page for"
655 " LAC %d (would have paged IMSI %s)\n",
656 lac, imsi);
657 }
658 }
659
660 return pagings_sent;
661}
662
663int iu_page_cs(const char *imsi, const uint32_t *tmsi, uint16_t lac)
664{
665 return iu_page(imsi, tmsi, lac, 0, false);
666}
667
668int iu_page_ps(const char *imsi, const uint32_t *ptmsi, uint16_t lac, uint8_t rac)
669{
670 return iu_page(imsi, ptmsi, lac, rac, true);
671}
672
673
674/***********************************************************************
675 *
676 ***********************************************************************/
677
Philipp Maier4b60d072017-04-09 12:32:51 +0200678int tx_unitdata(struct osmo_sccp_user *scu);
679int tx_conn_req(struct osmo_sccp_user *scu, uint32_t conn_id);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200680
681struct osmo_prim_hdr *make_conn_req(uint32_t conn_id);
682struct osmo_prim_hdr *make_dt1_req(uint32_t conn_id, const uint8_t *data, unsigned int len);
683
684struct osmo_prim_hdr *make_conn_resp(struct osmo_scu_connect_param *param)
685{
686 struct msgb *msg = msgb_alloc(1024, "conn_resp");
687 struct osmo_scu_prim *prim;
688
689 prim = (struct osmo_scu_prim *) msgb_put(msg, sizeof(*prim));
690 osmo_prim_init(&prim->oph, SCCP_SAP_USER,
691 OSMO_SCU_PRIM_N_CONNECT,
692 PRIM_OP_RESPONSE, msg);
693 memcpy(&prim->u.connect, param, sizeof(prim->u.connect));
694 return &prim->oph;
695}
696
Philipp Maier4b60d072017-04-09 12:32:51 +0200697static int sccp_sap_up(struct osmo_prim_hdr *oph, void *_scu)
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200698{
Philipp Maier4b60d072017-04-09 12:32:51 +0200699 struct osmo_sccp_user *scu = _scu;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200700 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
701 struct osmo_prim_hdr *resp = NULL;
702 int rc;
703 struct ue_conn_ctx *ue;
704
705 DEBUGP(DRANAP, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
706
707 switch (OSMO_PRIM_HDR(oph)) {
708 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
709 /* confirmation of outbound connection */
710 rc = -1;
711 break;
712 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_INDICATION):
713 /* indication of new inbound connection request*/
714 DEBUGP(DRANAP, "N-CONNECT.ind(X->%u)\n", prim->u.connect.conn_id);
715 if (/* prim->u.connect.called_addr.ssn != OSMO_SCCP_SSN_RANAP || */
716 !msgb_l2(oph->msg) || msgb_l2len(oph->msg) == 0) {
717 LOGP(DRANAP, LOGL_NOTICE,
718 "Received invalid N-CONNECT.ind\n");
719 return 0;
720 }
Philipp Maier4b60d072017-04-09 12:32:51 +0200721 ue = ue_conn_ctx_alloc(&prim->u.connect.calling_addr, prim->u.connect.conn_id);
722 /* first ensure the local SCCP socket is ACTIVE */
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200723 resp = make_conn_resp(&prim->u.connect);
Philipp Maier4b60d072017-04-09 12:32:51 +0200724 osmo_sccp_user_sap_down(scu, resp);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200725 /* then handle the RANAP payload */
726 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
727 break;
728 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
729 /* indication of disconnect */
730 DEBUGP(DRANAP, "N-DISCONNECT.ind(%u)\n",
731 prim->u.disconnect.conn_id);
Philipp Maier4b60d072017-04-09 12:32:51 +0200732 ue = ue_conn_ctx_find(prim->u.disconnect.conn_id);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200733 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
734 break;
735 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
736 /* connection-oriented data received */
737 DEBUGP(DRANAP, "N-DATA.ind(%u, %s)\n", prim->u.data.conn_id,
738 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
739 /* resolve UE context */
Philipp Maier4b60d072017-04-09 12:32:51 +0200740 ue = ue_conn_ctx_find(prim->u.data.conn_id);
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200741 rc = ranap_cn_rx_co(cn_ranap_handle_co, ue, msgb_l2(oph->msg), msgb_l2len(oph->msg));
742 break;
743 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
744 /* connection-less data received */
745 DEBUGP(DRANAP, "N-UNITDATA.ind(%s)\n",
746 osmo_hexdump(msgb_l2(oph->msg), msgb_l2len(oph->msg)));
Philipp Maier4b60d072017-04-09 12:32:51 +0200747 rc = ranap_cn_rx_cl(cn_ranap_handle_cl, scu, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200748 break;
749 default:
750 rc = -1;
751 break;
752 }
753
754 msgb_free(oph->msg);
755 return rc;
756}
757
Philipp Maier4b60d072017-04-09 12:32:51 +0200758int iu_init(void *ctx, struct osmo_sccp_instance *sccp,
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200759 iu_recv_cb_t iu_recv_cb, iu_event_cb_t iu_event_cb)
760{
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200761 talloc_iu_ctx = talloc_named_const(ctx, 1, "iu");
762 talloc_asn1_ctx = talloc_named_const(talloc_iu_ctx, 1, "asn1");
763
764 global_iu_recv_cb = iu_recv_cb;
765 global_iu_event_cb = iu_event_cb;
Philipp Maier4b60d072017-04-09 12:32:51 +0200766 g_sccp = sccp;
767 g_scu = osmo_sccp_user_bind(g_sccp, "OsmoMSC-Iu", sccp_sap_up, OSMO_SCCP_SSN_RANAP);
768
769 return 0;
Neels Hofmeyrbfa88782016-05-20 21:38:32 +0200770}
771