blob: f79799288a7d8bfa4c8eddbdc2b70bfc505cf749 [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"
Harald Welte1d2c39d2015-09-11 17:49:37 +020031#include "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;
Harald Welte3f712562015-09-07 21:53:25 +020046 return osmo_wqueue_enqueue(&ctx->wqueue, msg);
Harald Welte27f9c4a2015-08-30 22:47:18 +020047}
Harald Welteee77cff2015-08-30 16:57:53 +020048
Harald Welte27f9c4a2015-08-30 22:47:18 +020049static int hnbgw_tx_hnb_register_acc(struct hnb_context *ctx)
50{
51 HNBRegisterAccept_t accept_out;
52 struct msgb *msg;
53 int rc;
54
55 /* Single required response IE: RNC-ID */
56 HNBRegisterAcceptIEs_t accept = {
57 .rnc_id = ctx->gw->config.rnc_id
58 };
59
60 /* encode the Information Elements */
Harald Welte2204f9d2015-09-07 21:10:50 +020061 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +020062 rc = hnbap_encode_hnbregisteraccepties(&accept_out, &accept);
63 if (rc < 0) {
64 return rc;
Harald Welteee77cff2015-08-30 16:57:53 +020065 }
66
Harald Welte27f9c4a2015-08-30 22:47:18 +020067 /* generate a successfull outcome PDU */
68 msg = hnbap_generate_successful_outcome(ProcedureCode_id_HNBRegister,
69 Criticality_reject,
70 &asn_DEF_HNBRegisterAccept,
71 &accept_out);
Harald Welteee77cff2015-08-30 16:57:53 +020072
Daniel Willmann59d17d82015-12-17 17:56:56 +010073 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBRegisterAccept, &accept_out);
74
Harald Welte27f9c4a2015-08-30 22:47:18 +020075 return hnbgw_hnbap_tx(ctx, msg);
Harald Welteee77cff2015-08-30 16:57:53 +020076}
77
78
Harald Welte27f9c4a2015-08-30 22:47:18 +020079static int hnbgw_tx_ue_register_acc(struct ue_context *ue)
Harald Welteba43de42015-08-29 20:33:16 +020080{
Harald Welte27f9c4a2015-08-30 22:47:18 +020081 UERegisterAccept_t accept_out;
82 UERegisterAcceptIEs_t accept;
83 struct msgb *msg;
Harald Welte10dfc5a2015-09-11 01:34:45 +020084 uint8_t encoded_imsi[10];
Daniel Willmann92247312015-12-09 19:04:33 +010085 uint32_t ctx_id;
Harald Welte10dfc5a2015-09-11 01:34:45 +020086 size_t encoded_imsi_len;
Harald Welte27f9c4a2015-08-30 22:47:18 +020087 int rc;
Harald Welteba43de42015-08-29 20:33:16 +020088
Harald Welte1d2c39d2015-09-11 17:49:37 +020089 encoded_imsi_len = encode_iu_imsi(encoded_imsi,
90 sizeof(encoded_imsi), ue->imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +020091
Harald Welte2204f9d2015-09-07 21:10:50 +020092 memset(&accept, 0, sizeof(accept));
Harald Welte10dfc5a2015-09-11 01:34:45 +020093 accept.uE_Identity.present = UE_Identity_PR_iMSI;
Harald Welte1d2c39d2015-09-11 17:49:37 +020094 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.iMSI,
95 (const char *)encoded_imsi, encoded_imsi_len);
Daniel Willmann92247312015-12-09 19:04:33 +010096 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
Harald Welteba43de42015-08-29 20:33:16 +020097
Harald Welte2204f9d2015-09-07 21:10:50 +020098 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +020099 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
100 if (rc < 0) {
101 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200102 }
Harald Welte27f9c4a2015-08-30 22:47:18 +0200103
104 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
105 Criticality_reject,
106 &asn_DEF_UERegisterAccept,
107 &accept_out);
Daniel Willmann541e4292015-12-22 16:25:29 +0100108
109 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING, &accept.uE_Identity.choice.iMSI);
110 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
111
Harald Welte27f9c4a2015-08-30 22:47:18 +0200112 return hnbgw_hnbap_tx(ue->hnb, msg);
Harald Welteba43de42015-08-29 20:33:16 +0200113}
114
Harald Welte27f9c4a2015-08-30 22:47:18 +0200115static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200116{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200117 HNBRegisterRequestIEs_t ies;
118 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200119
Harald Welte27f9c4a2015-08-30 22:47:18 +0200120 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
121 if (rc < 0)
122 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200123
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200124 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200125 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
126 sizeof(ctx->identity_info));
127 ctx->id.lac = asn1str_to_u16(&ies.lac);
128 ctx->id.sac = asn1str_to_u16(&ies.sac);
129 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100130 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Harald Welteb3dae302015-08-30 12:20:09 +0200131 //ctx->id.mcc FIXME
132 //ctx->id.mnc FIXME
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200133
Daniel Willmannbded9842015-12-17 11:51:17 +0100134 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200135
Harald Welte27f9c4a2015-08-30 22:47:18 +0200136 /* Send HNBRegisterAccept */
137 return hnbgw_tx_hnb_register_acc(ctx);
Harald Welteba43de42015-08-29 20:33:16 +0200138}
139
Harald Welte27f9c4a2015-08-30 22:47:18 +0200140static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200141{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200142 UERegisterRequestIEs_t ies;
143 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200144 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200145 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200146
Harald Welte27f9c4a2015-08-30 22:47:18 +0200147 rc = hnbap_decode_ueregisterrequesties(&ies, in);
148 if (rc < 0)
149 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200150
Harald Welte10dfc5a2015-09-11 01:34:45 +0200151 switch (ies.uE_Identity.present) {
152 case UE_Identity_PR_iMSI:
153 decode_iu_bcd(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
154 ies.uE_Identity.choice.iMSI.size);
155 break;
156 case UE_Identity_PR_iMSIDS41:
157 decode_iu_bcd(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
158 ies.uE_Identity.choice.iMSIDS41.size);
159 break;
160 case UE_Identity_PR_iMSIESN:
161 decode_iu_bcd(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
162 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
163 break;
164 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100165 LOGP(DHNBAP, LOGL_NOTICE, "UE-REGISTER-REQ without IMSI?!?\n");
Harald Welte10dfc5a2015-09-11 01:34:45 +0200166 return -1;
167 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200168
Daniel Willmannbded9842015-12-17 11:51:17 +0100169 DEBUGP(DHNBAP, "UE-REGSITER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200170 ies.uE_Identity.present, imsi, ies.registration_Cause);
171
Harald Weltec4338de2015-12-24 00:40:52 +0100172 ue = ue_context_by_imsi(ctx->gw, imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200173 if (!ue)
174 ue = ue_context_alloc(ctx, imsi);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200175
Harald Welte27f9c4a2015-08-30 22:47:18 +0200176 /* Send UERegisterAccept */
177 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200178}
179
Daniel Willmannefceb182015-12-15 20:30:12 +0100180static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
181{
182 UEDe_RegisterIEs_t ies;
183 struct ue_context *ue;
184 int rc;
185 uint32_t ctxid;
186
187 rc = hnbap_decode_uede_registeries(&ies, in);
188 if (rc < 0)
189 return rc;
190
191 ctxid = asn1bitstr_to_u24(&ies.context_ID);
192
Daniel Willmannbded9842015-12-17 11:51:17 +0100193 DEBUGP(DHNBAP, "UE-DE-REGSITER context=%ld cause=%ld\n",
Daniel Willmannefceb182015-12-15 20:30:12 +0100194 ctxid, ies.cause);
195
Harald Weltec4338de2015-12-24 00:40:52 +0100196 ue = ue_context_by_id(ctx->gw, ctxid);
Daniel Willmannefceb182015-12-15 20:30:12 +0100197 if (ue)
198 ue_context_free(ue);
199
200 return 0;
201}
202
Harald Welte3af1db82015-09-11 17:03:16 +0200203static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
204{
205 ErrorIndicationIEs_t ies;
206 int rc;
207
Harald Welte1d2c39d2015-09-11 17:49:37 +0200208 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200209 if (rc < 0)
210 return rc;
211
Daniel Willmannbded9842015-12-17 11:51:17 +0100212 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200213 hnbap_cause_str(&ies.cause));
214
215 return 0;
216}
217
Harald Welte27f9c4a2015-08-30 22:47:18 +0200218static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200219{
220 int rc;
221
Harald Welteb3dae302015-08-30 12:20:09 +0200222 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200223 case ProcedureCode_id_HNBRegister: /* 8.2 */
224 rc = hnbgw_rx_hnb_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200225 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200226 case ProcedureCode_id_HNBDe_Register: /* 8.3 */
Harald Welteba43de42015-08-29 20:33:16 +0200227 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200228 case ProcedureCode_id_UERegister: /* 8.4 */
229 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200230 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200231 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100232 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200233 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200234 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200235 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
236 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200237 case ProcedureCode_id_TNLUpdate: /* 8.9 */
238 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
239 case ProcedureCode_id_RelocationComplete: /* 8.11 */
240 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
241 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100242 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200243 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200244 break;
245 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100246 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200247 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200248 break;
249 }
250}
251
Harald Welte27f9c4a2015-08-30 22:47:18 +0200252static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200253{
254
255}
256
Harald Welte27f9c4a2015-08-30 22:47:18 +0200257static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200258{
259
260}
261
262
Harald Welte27f9c4a2015-08-30 22:47:18 +0200263static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200264{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100265 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200266
Harald Welteba43de42015-08-29 20:33:16 +0200267 /* it's a bit odd that we can't dispatch on procedure code, but
268 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200269 switch (pdu->present) {
270 case HNBAP_PDU_PR_initiatingMessage:
271 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200272 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200273 case HNBAP_PDU_PR_successfulOutcome:
274 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200275 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200276 case HNBAP_PDU_PR_unsuccessfulOutcome:
277 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200278 break;
279 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100280 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200281 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100282 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200283 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100284
285 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200286}
287
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200288int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200289{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200290 HNBAP_PDU_t _pdu, *pdu = &_pdu;
291 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200292 int rc;
293
294 /* decode and handle to _hnbgw_hnbap_rx() */
295
Harald Welte2204f9d2015-09-07 21:10:50 +0200296 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200297 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
298 msg->data, msgb_length(msg), 0, 0);
299 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100300 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Harald Welteee77cff2015-08-30 16:57:53 +0200301 return rc;
302 }
303
304 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200305
Daniel Willmann59d17d82015-12-17 17:56:56 +0100306 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
307
Harald Welteee77cff2015-08-30 16:57:53 +0200308 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200309}
310
311
312int hnbgw_hnbap_init(void)
313{
314
315}