blob: eda5139012bdb6d796dc8a2c6eb46ec9046d8758 [file] [log] [blame]
Harald Welte77847ad2015-10-06 22:07:04 +02001/* hnb-gw specific code for HNBAP */
2
3/* (C) 2015 by Harald Welte <laforge@gnumonks.org>
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
Harald Welteba43de42015-08-29 20:33:16 +020021#include <osmocom/core/msgb.h>
Harald Welteb3dae302015-08-30 12:20:09 +020022#include <osmocom/core/utils.h>
Harald Welte10dfc5a2015-09-11 01:34:45 +020023#include <osmocom/gsm/gsm48.h>
Harald Welteffa7c0a2015-12-23 00:03:41 +010024#include <osmocom/netif/stream.h>
Harald Welteba43de42015-08-29 20:33:16 +020025
Harald Welteb3dae302015-08-30 12:20:09 +020026#include <unistd.h>
Harald Welteee77cff2015-08-30 16:57:53 +020027#include <errno.h>
Harald Welteb3dae302015-08-30 12:20:09 +020028#include <string.h>
29
Harald Welte30afef32015-08-30 12:28:29 +020030#include "asn1helpers.h"
Neels Hofmeyr96979af2016-01-05 15:19:44 +010031#include <osmocom/ranap/iu_helpers.h>
Harald Welte30afef32015-08-30 12:28:29 +020032
Harald Welteba43de42015-08-29 20:33:16 +020033#include "hnbgw.h"
Harald Welte27f9c4a2015-08-30 22:47:18 +020034#include "hnbap_common.h"
35#include "hnbap_ies_defs.h"
Harald Welteba43de42015-08-29 20:33:16 +020036
Harald Welteee77cff2015-08-30 16:57:53 +020037#define IU_MSG_NUM_IES 32
38#define IU_MSG_NUM_EXT_IES 32
39
Harald Welte27f9c4a2015-08-30 22:47:18 +020040static int hnbgw_hnbap_tx(struct hnb_context *ctx, struct msgb *msg)
Harald Welteee77cff2015-08-30 16:57:53 +020041{
Harald Welte7b54e322015-09-07 22:41:45 +020042 if (!msg)
43 return -EINVAL;
44
Harald Welteffa7c0a2015-12-23 00:03:41 +010045 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann6480cad2016-01-06 18:06:26 +010046 osmo_stream_srv_send(ctx->conn, msg);
47
48 return 0;
Harald Welte27f9c4a2015-08-30 22:47:18 +020049}
Harald Welteee77cff2015-08-30 16:57:53 +020050
Harald Welte27f9c4a2015-08-30 22:47:18 +020051static int hnbgw_tx_hnb_register_acc(struct hnb_context *ctx)
52{
53 HNBRegisterAccept_t accept_out;
54 struct msgb *msg;
55 int rc;
56
57 /* Single required response IE: RNC-ID */
58 HNBRegisterAcceptIEs_t accept = {
59 .rnc_id = ctx->gw->config.rnc_id
60 };
61
62 /* encode the Information Elements */
Harald Welte2204f9d2015-09-07 21:10:50 +020063 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +020064 rc = hnbap_encode_hnbregisteraccepties(&accept_out, &accept);
65 if (rc < 0) {
66 return rc;
Harald Welteee77cff2015-08-30 16:57:53 +020067 }
68
Harald Welte27f9c4a2015-08-30 22:47:18 +020069 /* generate a successfull outcome PDU */
70 msg = hnbap_generate_successful_outcome(ProcedureCode_id_HNBRegister,
71 Criticality_reject,
72 &asn_DEF_HNBRegisterAccept,
73 &accept_out);
Harald Welteee77cff2015-08-30 16:57:53 +020074
Daniel Willmann59d17d82015-12-17 17:56:56 +010075 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBRegisterAccept, &accept_out);
76
Harald Welte27f9c4a2015-08-30 22:47:18 +020077 return hnbgw_hnbap_tx(ctx, msg);
Harald Welteee77cff2015-08-30 16:57:53 +020078}
79
80
Harald Welte27f9c4a2015-08-30 22:47:18 +020081static int hnbgw_tx_ue_register_acc(struct ue_context *ue)
Harald Welteba43de42015-08-29 20:33:16 +020082{
Harald Welte27f9c4a2015-08-30 22:47:18 +020083 UERegisterAccept_t accept_out;
84 UERegisterAcceptIEs_t accept;
85 struct msgb *msg;
Harald Welte10dfc5a2015-09-11 01:34:45 +020086 uint8_t encoded_imsi[10];
Daniel Willmann92247312015-12-09 19:04:33 +010087 uint32_t ctx_id;
Harald Welte10dfc5a2015-09-11 01:34:45 +020088 size_t encoded_imsi_len;
Harald Welte27f9c4a2015-08-30 22:47:18 +020089 int rc;
Harald Welteba43de42015-08-29 20:33:16 +020090
Harald Welte056984f2016-01-03 16:31:31 +010091 encoded_imsi_len = ranap_imsi_encode(encoded_imsi,
Harald Welte1d2c39d2015-09-11 17:49:37 +020092 sizeof(encoded_imsi), ue->imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +020093
Harald Welte2204f9d2015-09-07 21:10:50 +020094 memset(&accept, 0, sizeof(accept));
Harald Welte10dfc5a2015-09-11 01:34:45 +020095 accept.uE_Identity.present = UE_Identity_PR_iMSI;
Harald Welte1d2c39d2015-09-11 17:49:37 +020096 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.iMSI,
97 (const char *)encoded_imsi, encoded_imsi_len);
Daniel Willmann92247312015-12-09 19:04:33 +010098 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
Harald Welteba43de42015-08-29 20:33:16 +020099
Harald Welte2204f9d2015-09-07 21:10:50 +0200100 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200101 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
102 if (rc < 0) {
103 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200104 }
Harald Welte27f9c4a2015-08-30 22:47:18 +0200105
106 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
107 Criticality_reject,
108 &asn_DEF_UERegisterAccept,
109 &accept_out);
Daniel Willmann541e4292015-12-22 16:25:29 +0100110
111 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING, &accept.uE_Identity.choice.iMSI);
112 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
113
Harald Welte27f9c4a2015-08-30 22:47:18 +0200114 return hnbgw_hnbap_tx(ue->hnb, msg);
Harald Welteba43de42015-08-29 20:33:16 +0200115}
116
Daniel Willmannb433b972016-01-06 18:08:21 +0100117static int hnbgw_rx_hnb_deregister(struct hnb_context *ctx, ANY_t *in)
118{
119 HNBDe_RegisterIEs_t ies;
120 int rc;
121
122 rc = hnbap_decode_hnbde_registeries(&ies, in);
123 if (rc < 0)
124 return rc;
125
126 DEBUGP(DHNBAP, "HNB-DE-REGSITER cause=%ld\n",
127 ies.cause);
128
129 hnb_context_release(ctx);
130
131 return 0;
132}
133
Harald Welte27f9c4a2015-08-30 22:47:18 +0200134static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200135{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200136 HNBRegisterRequestIEs_t ies;
137 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200138
Harald Welte27f9c4a2015-08-30 22:47:18 +0200139 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
140 if (rc < 0)
141 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200142
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200143 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200144 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
145 sizeof(ctx->identity_info));
146 ctx->id.lac = asn1str_to_u16(&ies.lac);
147 ctx->id.sac = asn1str_to_u16(&ies.sac);
148 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100149 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Harald Welteb3dae302015-08-30 12:20:09 +0200150 //ctx->id.mcc FIXME
151 //ctx->id.mnc FIXME
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200152
Daniel Willmannbded9842015-12-17 11:51:17 +0100153 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200154
Harald Welte27f9c4a2015-08-30 22:47:18 +0200155 /* Send HNBRegisterAccept */
156 return hnbgw_tx_hnb_register_acc(ctx);
Harald Welteba43de42015-08-29 20:33:16 +0200157}
158
Harald Welte27f9c4a2015-08-30 22:47:18 +0200159static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200160{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200161 UERegisterRequestIEs_t ies;
162 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200163 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200164 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200165
Harald Welte27f9c4a2015-08-30 22:47:18 +0200166 rc = hnbap_decode_ueregisterrequesties(&ies, in);
167 if (rc < 0)
168 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200169
Harald Welte10dfc5a2015-09-11 01:34:45 +0200170 switch (ies.uE_Identity.present) {
171 case UE_Identity_PR_iMSI:
Harald Welte056984f2016-01-03 16:31:31 +0100172 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200173 ies.uE_Identity.choice.iMSI.size);
174 break;
175 case UE_Identity_PR_iMSIDS41:
Harald Welte056984f2016-01-03 16:31:31 +0100176 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200177 ies.uE_Identity.choice.iMSIDS41.size);
178 break;
179 case UE_Identity_PR_iMSIESN:
Harald Welte056984f2016-01-03 16:31:31 +0100180 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200181 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
182 break;
183 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100184 LOGP(DHNBAP, LOGL_NOTICE, "UE-REGISTER-REQ without IMSI?!?\n");
Harald Welte10dfc5a2015-09-11 01:34:45 +0200185 return -1;
186 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200187
Harald Welte706213a2015-12-25 10:33:09 +0100188 DEBUGP(DHNBAP, "UE-REGISTER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200189 ies.uE_Identity.present, imsi, ies.registration_Cause);
190
Harald Weltec4338de2015-12-24 00:40:52 +0100191 ue = ue_context_by_imsi(ctx->gw, imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200192 if (!ue)
193 ue = ue_context_alloc(ctx, imsi);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200194
Harald Welte27f9c4a2015-08-30 22:47:18 +0200195 /* Send UERegisterAccept */
196 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200197}
198
Daniel Willmannefceb182015-12-15 20:30:12 +0100199static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
200{
201 UEDe_RegisterIEs_t ies;
202 struct ue_context *ue;
203 int rc;
204 uint32_t ctxid;
205
206 rc = hnbap_decode_uede_registeries(&ies, in);
207 if (rc < 0)
208 return rc;
209
210 ctxid = asn1bitstr_to_u24(&ies.context_ID);
211
Harald Welte706213a2015-12-25 10:33:09 +0100212 DEBUGP(DHNBAP, "UE-DE-REGISTER context=%ld cause=%s\n",
Harald Welte2963ee22015-12-25 10:32:37 +0100213 ctxid, hnbap_cause_str(&ies.cause));
Daniel Willmannefceb182015-12-15 20:30:12 +0100214
Harald Weltec4338de2015-12-24 00:40:52 +0100215 ue = ue_context_by_id(ctx->gw, ctxid);
Daniel Willmannefceb182015-12-15 20:30:12 +0100216 if (ue)
217 ue_context_free(ue);
218
219 return 0;
220}
221
Harald Welte3af1db82015-09-11 17:03:16 +0200222static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
223{
224 ErrorIndicationIEs_t ies;
225 int rc;
226
Harald Welte1d2c39d2015-09-11 17:49:37 +0200227 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200228 if (rc < 0)
229 return rc;
230
Daniel Willmannbded9842015-12-17 11:51:17 +0100231 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200232 hnbap_cause_str(&ies.cause));
233
234 return 0;
235}
236
Harald Welte27f9c4a2015-08-30 22:47:18 +0200237static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200238{
239 int rc;
240
Harald Welteb3dae302015-08-30 12:20:09 +0200241 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200242 case ProcedureCode_id_HNBRegister: /* 8.2 */
243 rc = hnbgw_rx_hnb_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200244 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200245 case ProcedureCode_id_HNBDe_Register: /* 8.3 */
Daniel Willmannb433b972016-01-06 18:08:21 +0100246 rc = hnbgw_rx_hnb_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200247 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200248 case ProcedureCode_id_UERegister: /* 8.4 */
249 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200250 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200251 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100252 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200253 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200254 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200255 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
256 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200257 case ProcedureCode_id_TNLUpdate: /* 8.9 */
258 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
259 case ProcedureCode_id_RelocationComplete: /* 8.11 */
260 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
261 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100262 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200263 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200264 break;
265 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100266 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200267 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200268 break;
269 }
270}
271
Harald Welte27f9c4a2015-08-30 22:47:18 +0200272static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200273{
274
275}
276
Harald Welte27f9c4a2015-08-30 22:47:18 +0200277static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200278{
279
280}
281
282
Harald Welte27f9c4a2015-08-30 22:47:18 +0200283static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200284{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100285 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200286
Harald Welteba43de42015-08-29 20:33:16 +0200287 /* it's a bit odd that we can't dispatch on procedure code, but
288 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200289 switch (pdu->present) {
290 case HNBAP_PDU_PR_initiatingMessage:
291 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200292 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200293 case HNBAP_PDU_PR_successfulOutcome:
294 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200295 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200296 case HNBAP_PDU_PR_unsuccessfulOutcome:
297 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200298 break;
299 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100300 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200301 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100302 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200303 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100304
305 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200306}
307
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200308int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200309{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200310 HNBAP_PDU_t _pdu, *pdu = &_pdu;
311 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200312 int rc;
313
314 /* decode and handle to _hnbgw_hnbap_rx() */
315
Harald Welte2204f9d2015-09-07 21:10:50 +0200316 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200317 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
318 msg->data, msgb_length(msg), 0, 0);
319 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100320 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200321 return rc;
322 }
323
324 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200325
Daniel Willmann59d17d82015-12-17 17:56:56 +0100326 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
327
Harald Welteee77cff2015-08-30 16:57:53 +0200328 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200329}
330
331
332int hnbgw_hnbap_init(void)
333{
334
335}