blob: 0b8e3e44f993add3391642130d93d1986ab21670 [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 Welteba43de42015-08-29 20:33:16 +020024
Harald Welteb3dae302015-08-30 12:20:09 +020025#include <unistd.h>
Harald Welteee77cff2015-08-30 16:57:53 +020026#include <errno.h>
Harald Welteb3dae302015-08-30 12:20:09 +020027#include <string.h>
28
Harald Welte30afef32015-08-30 12:28:29 +020029#include "asn1helpers.h"
Harald Welte1d2c39d2015-09-11 17:49:37 +020030#include "iu_helpers.h"
Harald Welte30afef32015-08-30 12:28:29 +020031
Harald Welteba43de42015-08-29 20:33:16 +020032#include "hnbgw.h"
Harald Welte27f9c4a2015-08-30 22:47:18 +020033#include "hnbap_common.h"
34#include "hnbap_ies_defs.h"
Harald Welteba43de42015-08-29 20:33:16 +020035
Harald Welteee77cff2015-08-30 16:57:53 +020036#define IU_MSG_NUM_IES 32
37#define IU_MSG_NUM_EXT_IES 32
38
Harald Welte27f9c4a2015-08-30 22:47:18 +020039static int hnbgw_hnbap_tx(struct hnb_context *ctx, struct msgb *msg)
Harald Welteee77cff2015-08-30 16:57:53 +020040{
Harald Welte7b54e322015-09-07 22:41:45 +020041 if (!msg)
42 return -EINVAL;
43
Harald Welte3f712562015-09-07 21:53:25 +020044 msgb_ppid(msg) = IUH_PPI_HNBAP;
45 return osmo_wqueue_enqueue(&ctx->wqueue, msg);
Harald Welte27f9c4a2015-08-30 22:47:18 +020046}
Harald Welteee77cff2015-08-30 16:57:53 +020047
Harald Welte27f9c4a2015-08-30 22:47:18 +020048static int hnbgw_tx_hnb_register_acc(struct hnb_context *ctx)
49{
50 HNBRegisterAccept_t accept_out;
51 struct msgb *msg;
52 int rc;
53
54 /* Single required response IE: RNC-ID */
55 HNBRegisterAcceptIEs_t accept = {
56 .rnc_id = ctx->gw->config.rnc_id
57 };
58
59 /* encode the Information Elements */
Harald Welte2204f9d2015-09-07 21:10:50 +020060 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +020061 rc = hnbap_encode_hnbregisteraccepties(&accept_out, &accept);
62 if (rc < 0) {
63 return rc;
Harald Welteee77cff2015-08-30 16:57:53 +020064 }
65
Harald Welte27f9c4a2015-08-30 22:47:18 +020066 /* generate a successfull outcome PDU */
67 msg = hnbap_generate_successful_outcome(ProcedureCode_id_HNBRegister,
68 Criticality_reject,
69 &asn_DEF_HNBRegisterAccept,
70 &accept_out);
Harald Welteee77cff2015-08-30 16:57:53 +020071
Daniel Willmann59d17d82015-12-17 17:56:56 +010072 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBRegisterAccept, &accept_out);
73
Harald Welte27f9c4a2015-08-30 22:47:18 +020074 return hnbgw_hnbap_tx(ctx, msg);
Harald Welteee77cff2015-08-30 16:57:53 +020075}
76
77
Harald Welte27f9c4a2015-08-30 22:47:18 +020078static int hnbgw_tx_ue_register_acc(struct ue_context *ue)
Harald Welteba43de42015-08-29 20:33:16 +020079{
Harald Welte27f9c4a2015-08-30 22:47:18 +020080 UERegisterAccept_t accept_out;
81 UERegisterAcceptIEs_t accept;
82 struct msgb *msg;
Harald Welte10dfc5a2015-09-11 01:34:45 +020083 uint8_t encoded_imsi[10];
Daniel Willmann92247312015-12-09 19:04:33 +010084 uint32_t ctx_id;
Harald Welte10dfc5a2015-09-11 01:34:45 +020085 size_t encoded_imsi_len;
Harald Welte27f9c4a2015-08-30 22:47:18 +020086 int rc;
Harald Welteba43de42015-08-29 20:33:16 +020087
Harald Welte1d2c39d2015-09-11 17:49:37 +020088 encoded_imsi_len = encode_iu_imsi(encoded_imsi,
89 sizeof(encoded_imsi), ue->imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +020090
Harald Welte2204f9d2015-09-07 21:10:50 +020091 memset(&accept, 0, sizeof(accept));
Harald Welte10dfc5a2015-09-11 01:34:45 +020092 accept.uE_Identity.present = UE_Identity_PR_iMSI;
Harald Welte1d2c39d2015-09-11 17:49:37 +020093 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.iMSI,
94 (const char *)encoded_imsi, encoded_imsi_len);
Daniel Willmann92247312015-12-09 19:04:33 +010095 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
Harald Welteba43de42015-08-29 20:33:16 +020096
Harald Welte2204f9d2015-09-07 21:10:50 +020097 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +020098 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
99 if (rc < 0) {
100 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200101 }
Harald Welte27f9c4a2015-08-30 22:47:18 +0200102
103 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
104 Criticality_reject,
105 &asn_DEF_UERegisterAccept,
106 &accept_out);
Daniel Willmann541e4292015-12-22 16:25:29 +0100107
108 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING, &accept.uE_Identity.choice.iMSI);
109 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
110
Harald Welte27f9c4a2015-08-30 22:47:18 +0200111 return hnbgw_hnbap_tx(ue->hnb, msg);
Harald Welteba43de42015-08-29 20:33:16 +0200112}
113
Harald Welte27f9c4a2015-08-30 22:47:18 +0200114static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200115{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200116 HNBRegisterRequestIEs_t ies;
117 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200118
Harald Welte27f9c4a2015-08-30 22:47:18 +0200119 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
120 if (rc < 0)
121 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200122
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200123 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200124 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
125 sizeof(ctx->identity_info));
126 ctx->id.lac = asn1str_to_u16(&ies.lac);
127 ctx->id.sac = asn1str_to_u16(&ies.sac);
128 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100129 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Harald Welteb3dae302015-08-30 12:20:09 +0200130 //ctx->id.mcc FIXME
131 //ctx->id.mnc FIXME
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200132
Daniel Willmannbded9842015-12-17 11:51:17 +0100133 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200134
Harald Welte27f9c4a2015-08-30 22:47:18 +0200135 /* Send HNBRegisterAccept */
136 return hnbgw_tx_hnb_register_acc(ctx);
Harald Welteba43de42015-08-29 20:33:16 +0200137}
138
Harald Welte27f9c4a2015-08-30 22:47:18 +0200139static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200140{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200141 UERegisterRequestIEs_t ies;
142 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200143 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200144 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200145
Harald Welte27f9c4a2015-08-30 22:47:18 +0200146 rc = hnbap_decode_ueregisterrequesties(&ies, in);
147 if (rc < 0)
148 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200149
Harald Welte10dfc5a2015-09-11 01:34:45 +0200150 switch (ies.uE_Identity.present) {
151 case UE_Identity_PR_iMSI:
152 decode_iu_bcd(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
153 ies.uE_Identity.choice.iMSI.size);
154 break;
155 case UE_Identity_PR_iMSIDS41:
156 decode_iu_bcd(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
157 ies.uE_Identity.choice.iMSIDS41.size);
158 break;
159 case UE_Identity_PR_iMSIESN:
160 decode_iu_bcd(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
161 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
162 break;
163 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100164 LOGP(DHNBAP, LOGL_NOTICE, "UE-REGISTER-REQ without IMSI?!?\n");
Harald Welte10dfc5a2015-09-11 01:34:45 +0200165 return -1;
166 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200167
Daniel Willmannbded9842015-12-17 11:51:17 +0100168 DEBUGP(DHNBAP, "UE-REGSITER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200169 ies.uE_Identity.present, imsi, ies.registration_Cause);
170
171 ue = ue_context_by_imsi(imsi);
172 if (!ue)
173 ue = ue_context_alloc(ctx, imsi);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200174
Harald Welte27f9c4a2015-08-30 22:47:18 +0200175 /* Send UERegisterAccept */
176 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200177}
178
Daniel Willmannefceb182015-12-15 20:30:12 +0100179static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
180{
181 UEDe_RegisterIEs_t ies;
182 struct ue_context *ue;
183 int rc;
184 uint32_t ctxid;
185
186 rc = hnbap_decode_uede_registeries(&ies, in);
187 if (rc < 0)
188 return rc;
189
190 ctxid = asn1bitstr_to_u24(&ies.context_ID);
191
Daniel Willmannbded9842015-12-17 11:51:17 +0100192 DEBUGP(DHNBAP, "UE-DE-REGSITER context=%ld cause=%ld\n",
Daniel Willmannefceb182015-12-15 20:30:12 +0100193 ctxid, ies.cause);
194
195 ue = ue_context_by_id(ctxid);
196 if (ue)
197 ue_context_free(ue);
198
199 return 0;
200}
201
Harald Welte3af1db82015-09-11 17:03:16 +0200202static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
203{
204 ErrorIndicationIEs_t ies;
205 int rc;
206
Harald Welte1d2c39d2015-09-11 17:49:37 +0200207 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200208 if (rc < 0)
209 return rc;
210
Daniel Willmannbded9842015-12-17 11:51:17 +0100211 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200212 hnbap_cause_str(&ies.cause));
213
214 return 0;
215}
216
Harald Welte27f9c4a2015-08-30 22:47:18 +0200217static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200218{
219 int rc;
220
Harald Welteb3dae302015-08-30 12:20:09 +0200221 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200222 case ProcedureCode_id_HNBRegister: /* 8.2 */
223 rc = hnbgw_rx_hnb_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200224 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200225 case ProcedureCode_id_HNBDe_Register: /* 8.3 */
Harald Welteba43de42015-08-29 20:33:16 +0200226 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200227 case ProcedureCode_id_UERegister: /* 8.4 */
228 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200229 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200230 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100231 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200232 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200233 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200234 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
235 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200236 case ProcedureCode_id_TNLUpdate: /* 8.9 */
237 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
238 case ProcedureCode_id_RelocationComplete: /* 8.11 */
239 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
240 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100241 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200242 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200243 break;
244 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100245 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200246 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200247 break;
248 }
249}
250
Harald Welte27f9c4a2015-08-30 22:47:18 +0200251static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200252{
253
254}
255
Harald Welte27f9c4a2015-08-30 22:47:18 +0200256static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200257{
258
259}
260
261
Harald Welte27f9c4a2015-08-30 22:47:18 +0200262static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200263{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100264 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200265
Harald Welteba43de42015-08-29 20:33:16 +0200266 /* it's a bit odd that we can't dispatch on procedure code, but
267 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200268 switch (pdu->present) {
269 case HNBAP_PDU_PR_initiatingMessage:
270 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200271 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200272 case HNBAP_PDU_PR_successfulOutcome:
273 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200274 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200275 case HNBAP_PDU_PR_unsuccessfulOutcome:
276 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200277 break;
278 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100279 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200280 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100281 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200282 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100283
284 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200285}
286
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200287int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200288{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200289 HNBAP_PDU_t _pdu, *pdu = &_pdu;
290 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200291 int rc;
292
293 /* decode and handle to _hnbgw_hnbap_rx() */
294
Harald Welte2204f9d2015-09-07 21:10:50 +0200295 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200296 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
297 msg->data, msgb_length(msg), 0, 0);
298 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100299 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200300 return rc;
301 }
302
303 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200304
Daniel Willmann59d17d82015-12-17 17:56:56 +0100305 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
306
Harald Welteee77cff2015-08-30 16:57:53 +0200307 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200308}
309
310
311int hnbgw_hnbap_init(void)
312{
313
314}