blob: 6bc09d9c2420a70bf1fe7a8a9b11616100032061 [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
Harald Welte27f9c4a2015-08-30 22:47:18 +0200117static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200118{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200119 HNBRegisterRequestIEs_t ies;
120 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200121
Harald Welte27f9c4a2015-08-30 22:47:18 +0200122 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
123 if (rc < 0)
124 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200125
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200126 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200127 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
128 sizeof(ctx->identity_info));
129 ctx->id.lac = asn1str_to_u16(&ies.lac);
130 ctx->id.sac = asn1str_to_u16(&ies.sac);
131 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100132 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Harald Welteb3dae302015-08-30 12:20:09 +0200133 //ctx->id.mcc FIXME
134 //ctx->id.mnc FIXME
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200135
Daniel Willmannbded9842015-12-17 11:51:17 +0100136 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200137
Harald Welte27f9c4a2015-08-30 22:47:18 +0200138 /* Send HNBRegisterAccept */
139 return hnbgw_tx_hnb_register_acc(ctx);
Harald Welteba43de42015-08-29 20:33:16 +0200140}
141
Harald Welte27f9c4a2015-08-30 22:47:18 +0200142static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200143{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200144 UERegisterRequestIEs_t ies;
145 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200146 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200147 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200148
Harald Welte27f9c4a2015-08-30 22:47:18 +0200149 rc = hnbap_decode_ueregisterrequesties(&ies, in);
150 if (rc < 0)
151 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200152
Harald Welte10dfc5a2015-09-11 01:34:45 +0200153 switch (ies.uE_Identity.present) {
154 case UE_Identity_PR_iMSI:
Harald Welte056984f2016-01-03 16:31:31 +0100155 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200156 ies.uE_Identity.choice.iMSI.size);
157 break;
158 case UE_Identity_PR_iMSIDS41:
Harald Welte056984f2016-01-03 16:31:31 +0100159 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200160 ies.uE_Identity.choice.iMSIDS41.size);
161 break;
162 case UE_Identity_PR_iMSIESN:
Harald Welte056984f2016-01-03 16:31:31 +0100163 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200164 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
165 break;
166 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100167 LOGP(DHNBAP, LOGL_NOTICE, "UE-REGISTER-REQ without IMSI?!?\n");
Harald Welte10dfc5a2015-09-11 01:34:45 +0200168 return -1;
169 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200170
Harald Welte706213a2015-12-25 10:33:09 +0100171 DEBUGP(DHNBAP, "UE-REGISTER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200172 ies.uE_Identity.present, imsi, ies.registration_Cause);
173
Harald Weltec4338de2015-12-24 00:40:52 +0100174 ue = ue_context_by_imsi(ctx->gw, imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200175 if (!ue)
176 ue = ue_context_alloc(ctx, imsi);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200177
Harald Welte27f9c4a2015-08-30 22:47:18 +0200178 /* Send UERegisterAccept */
179 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200180}
181
Daniel Willmannefceb182015-12-15 20:30:12 +0100182static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
183{
184 UEDe_RegisterIEs_t ies;
185 struct ue_context *ue;
186 int rc;
187 uint32_t ctxid;
188
189 rc = hnbap_decode_uede_registeries(&ies, in);
190 if (rc < 0)
191 return rc;
192
193 ctxid = asn1bitstr_to_u24(&ies.context_ID);
194
Harald Welte706213a2015-12-25 10:33:09 +0100195 DEBUGP(DHNBAP, "UE-DE-REGISTER context=%ld cause=%s\n",
Harald Welte2963ee22015-12-25 10:32:37 +0100196 ctxid, hnbap_cause_str(&ies.cause));
Daniel Willmannefceb182015-12-15 20:30:12 +0100197
Harald Weltec4338de2015-12-24 00:40:52 +0100198 ue = ue_context_by_id(ctx->gw, ctxid);
Daniel Willmannefceb182015-12-15 20:30:12 +0100199 if (ue)
200 ue_context_free(ue);
201
202 return 0;
203}
204
Harald Welte3af1db82015-09-11 17:03:16 +0200205static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
206{
207 ErrorIndicationIEs_t ies;
208 int rc;
209
Harald Welte1d2c39d2015-09-11 17:49:37 +0200210 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200211 if (rc < 0)
212 return rc;
213
Daniel Willmannbded9842015-12-17 11:51:17 +0100214 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200215 hnbap_cause_str(&ies.cause));
216
217 return 0;
218}
219
Harald Welte27f9c4a2015-08-30 22:47:18 +0200220static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200221{
222 int rc;
223
Harald Welteb3dae302015-08-30 12:20:09 +0200224 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200225 case ProcedureCode_id_HNBRegister: /* 8.2 */
226 rc = hnbgw_rx_hnb_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200227 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200228 case ProcedureCode_id_HNBDe_Register: /* 8.3 */
Harald Welteba43de42015-08-29 20:33:16 +0200229 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200230 case ProcedureCode_id_UERegister: /* 8.4 */
231 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200232 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200233 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100234 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200235 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200236 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200237 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
238 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200239 case ProcedureCode_id_TNLUpdate: /* 8.9 */
240 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
241 case ProcedureCode_id_RelocationComplete: /* 8.11 */
242 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
243 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100244 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200245 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200246 break;
247 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100248 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200249 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200250 break;
251 }
252}
253
Harald Welte27f9c4a2015-08-30 22:47:18 +0200254static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200255{
256
257}
258
Harald Welte27f9c4a2015-08-30 22:47:18 +0200259static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200260{
261
262}
263
264
Harald Welte27f9c4a2015-08-30 22:47:18 +0200265static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200266{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100267 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200268
Harald Welteba43de42015-08-29 20:33:16 +0200269 /* it's a bit odd that we can't dispatch on procedure code, but
270 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200271 switch (pdu->present) {
272 case HNBAP_PDU_PR_initiatingMessage:
273 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200274 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200275 case HNBAP_PDU_PR_successfulOutcome:
276 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200277 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200278 case HNBAP_PDU_PR_unsuccessfulOutcome:
279 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200280 break;
281 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100282 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200283 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100284 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200285 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100286
287 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200288}
289
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200290int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200291{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200292 HNBAP_PDU_t _pdu, *pdu = &_pdu;
293 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200294 int rc;
295
296 /* decode and handle to _hnbgw_hnbap_rx() */
297
Harald Welte2204f9d2015-09-07 21:10:50 +0200298 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200299 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
300 msg->data, msgb_length(msg), 0, 0);
301 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100302 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200303 return rc;
304 }
305
306 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200307
Daniel Willmann59d17d82015-12-17 17:56:56 +0100308 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
309
Harald Welteee77cff2015-08-30 16:57:53 +0200310 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200311}
312
313
314int hnbgw_hnbap_init(void)
315{
316
317}