blob: 971e97ecea2c7f49760757bbda00bd6fde843261 [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
Daniel Willmannd10002c2016-01-07 12:27:41 +0100129 hnbap_free_hnbde_registeries(&ies);
Daniel Willmannb433b972016-01-06 18:08:21 +0100130 hnb_context_release(ctx);
131
132 return 0;
133}
134
Harald Welte27f9c4a2015-08-30 22:47:18 +0200135static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200136{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200137 HNBRegisterRequestIEs_t ies;
138 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200139
Harald Welte27f9c4a2015-08-30 22:47:18 +0200140 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
141 if (rc < 0)
142 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200143
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200144 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200145 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
146 sizeof(ctx->identity_info));
147 ctx->id.lac = asn1str_to_u16(&ies.lac);
148 ctx->id.sac = asn1str_to_u16(&ies.sac);
149 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100150 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Harald Welteb3dae302015-08-30 12:20:09 +0200151 //ctx->id.mcc FIXME
152 //ctx->id.mnc FIXME
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200153
Daniel Willmannbded9842015-12-17 11:51:17 +0100154 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200155
Harald Welte27f9c4a2015-08-30 22:47:18 +0200156 /* Send HNBRegisterAccept */
Daniel Willmannd10002c2016-01-07 12:27:41 +0100157 rc = hnbgw_tx_hnb_register_acc(ctx);
158 hnbap_free_hnbregisterrequesties(&ies);
159 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200160}
161
Harald Welte27f9c4a2015-08-30 22:47:18 +0200162static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200163{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200164 UERegisterRequestIEs_t ies;
165 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200166 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200167 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200168
Harald Welte27f9c4a2015-08-30 22:47:18 +0200169 rc = hnbap_decode_ueregisterrequesties(&ies, in);
170 if (rc < 0)
171 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200172
Harald Welte10dfc5a2015-09-11 01:34:45 +0200173 switch (ies.uE_Identity.present) {
174 case UE_Identity_PR_iMSI:
Harald Welte056984f2016-01-03 16:31:31 +0100175 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200176 ies.uE_Identity.choice.iMSI.size);
177 break;
178 case UE_Identity_PR_iMSIDS41:
Harald Welte056984f2016-01-03 16:31:31 +0100179 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200180 ies.uE_Identity.choice.iMSIDS41.size);
181 break;
182 case UE_Identity_PR_iMSIESN:
Harald Welte056984f2016-01-03 16:31:31 +0100183 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200184 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
185 break;
186 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100187 LOGP(DHNBAP, LOGL_NOTICE, "UE-REGISTER-REQ without IMSI?!?\n");
Harald Welte10dfc5a2015-09-11 01:34:45 +0200188 return -1;
189 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200190
Harald Welte706213a2015-12-25 10:33:09 +0100191 DEBUGP(DHNBAP, "UE-REGISTER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200192 ies.uE_Identity.present, imsi, ies.registration_Cause);
193
Harald Weltec4338de2015-12-24 00:40:52 +0100194 ue = ue_context_by_imsi(ctx->gw, imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200195 if (!ue)
196 ue = ue_context_alloc(ctx, imsi);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200197
Daniel Willmannd10002c2016-01-07 12:27:41 +0100198 hnbap_free_ueregisterrequesties(&ies);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200199 /* Send UERegisterAccept */
200 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200201}
202
Daniel Willmannefceb182015-12-15 20:30:12 +0100203static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
204{
205 UEDe_RegisterIEs_t ies;
206 struct ue_context *ue;
207 int rc;
208 uint32_t ctxid;
209
210 rc = hnbap_decode_uede_registeries(&ies, in);
211 if (rc < 0)
212 return rc;
213
214 ctxid = asn1bitstr_to_u24(&ies.context_ID);
215
Harald Welte706213a2015-12-25 10:33:09 +0100216 DEBUGP(DHNBAP, "UE-DE-REGISTER context=%ld cause=%s\n",
Harald Welte2963ee22015-12-25 10:32:37 +0100217 ctxid, hnbap_cause_str(&ies.cause));
Daniel Willmannefceb182015-12-15 20:30:12 +0100218
Harald Weltec4338de2015-12-24 00:40:52 +0100219 ue = ue_context_by_id(ctx->gw, ctxid);
Daniel Willmannefceb182015-12-15 20:30:12 +0100220 if (ue)
221 ue_context_free(ue);
222
Daniel Willmannd10002c2016-01-07 12:27:41 +0100223 hnbap_free_uede_registeries(&ies);
Daniel Willmannefceb182015-12-15 20:30:12 +0100224 return 0;
225}
226
Harald Welte3af1db82015-09-11 17:03:16 +0200227static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
228{
229 ErrorIndicationIEs_t ies;
230 int rc;
231
Harald Welte1d2c39d2015-09-11 17:49:37 +0200232 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200233 if (rc < 0)
234 return rc;
235
Daniel Willmannbded9842015-12-17 11:51:17 +0100236 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200237 hnbap_cause_str(&ies.cause));
238
Daniel Willmannd10002c2016-01-07 12:27:41 +0100239 hnbap_free_errorindicationies(&ies);
Harald Welte3af1db82015-09-11 17:03:16 +0200240 return 0;
241}
242
Harald Welte27f9c4a2015-08-30 22:47:18 +0200243static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200244{
245 int rc;
246
Harald Welteb3dae302015-08-30 12:20:09 +0200247 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200248 case ProcedureCode_id_HNBRegister: /* 8.2 */
249 rc = hnbgw_rx_hnb_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_HNBDe_Register: /* 8.3 */
Daniel Willmannb433b972016-01-06 18:08:21 +0100252 rc = hnbgw_rx_hnb_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200253 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200254 case ProcedureCode_id_UERegister: /* 8.4 */
255 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200256 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200257 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100258 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200259 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200260 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200261 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
262 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200263 case ProcedureCode_id_TNLUpdate: /* 8.9 */
264 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
265 case ProcedureCode_id_RelocationComplete: /* 8.11 */
266 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
267 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100268 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200269 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200270 break;
271 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100272 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200273 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200274 break;
275 }
276}
277
Harald Welte27f9c4a2015-08-30 22:47:18 +0200278static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200279{
280
281}
282
Harald Welte27f9c4a2015-08-30 22:47:18 +0200283static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200284{
285
286}
287
288
Harald Welte27f9c4a2015-08-30 22:47:18 +0200289static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200290{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100291 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200292
Harald Welteba43de42015-08-29 20:33:16 +0200293 /* it's a bit odd that we can't dispatch on procedure code, but
294 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200295 switch (pdu->present) {
296 case HNBAP_PDU_PR_initiatingMessage:
297 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200298 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200299 case HNBAP_PDU_PR_successfulOutcome:
300 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200301 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200302 case HNBAP_PDU_PR_unsuccessfulOutcome:
303 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200304 break;
305 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100306 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200307 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100308 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200309 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100310
311 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200312}
313
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200314int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200315{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200316 HNBAP_PDU_t _pdu, *pdu = &_pdu;
317 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200318 int rc;
319
320 /* decode and handle to _hnbgw_hnbap_rx() */
321
Harald Welte2204f9d2015-09-07 21:10:50 +0200322 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200323 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
324 msg->data, msgb_length(msg), 0, 0);
325 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100326 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200327 return rc;
328 }
329
330 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200331
Daniel Willmann59d17d82015-12-17 17:56:56 +0100332 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
333
Harald Welteee77cff2015-08-30 16:57:53 +0200334 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200335}
336
337
338int hnbgw_hnbap_init(void)
339{
340
341}