blob: 2d67219c93b94d02a588eeed950f6482b845c4ef [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>
Stefan Sperlingc964a2c2018-02-19 17:02:12 +010023#include <osmocom/core/socket.h>
Harald Welte10dfc5a2015-09-11 01:34:45 +020024#include <osmocom/gsm/gsm48.h>
Harald Welteffa7c0a2015-12-23 00:03:41 +010025#include <osmocom/netif/stream.h>
Harald Welteba43de42015-08-29 20:33:16 +020026
Harald Welteb3dae302015-08-30 12:20:09 +020027#include <unistd.h>
Harald Welteee77cff2015-08-30 16:57:53 +020028#include <errno.h>
Harald Welteb3dae302015-08-30 12:20:09 +020029#include <string.h>
30
Harald Welte30afef32015-08-30 12:28:29 +020031#include "asn1helpers.h"
Neels Hofmeyr83457922016-08-26 23:56:44 +020032#include <osmocom/hnbap/hnbap_common.h>
Neels Hofmeyr96979af2016-01-05 15:19:44 +010033#include <osmocom/ranap/iu_helpers.h>
Harald Welte30afef32015-08-30 12:28:29 +020034
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020035#include <osmocom/iuh/hnbgw.h>
Neels Hofmeyr83457922016-08-26 23:56:44 +020036#include <osmocom/hnbap/hnbap_ies_defs.h>
Harald Welteba43de42015-08-29 20:33:16 +020037
Harald Welteee77cff2015-08-30 16:57:53 +020038#define IU_MSG_NUM_IES 32
39#define IU_MSG_NUM_EXT_IES 32
40
Harald Welte27f9c4a2015-08-30 22:47:18 +020041static int hnbgw_hnbap_tx(struct hnb_context *ctx, struct msgb *msg)
Harald Welteee77cff2015-08-30 16:57:53 +020042{
Harald Welte7b54e322015-09-07 22:41:45 +020043 if (!msg)
44 return -EINVAL;
45
Harald Welteffa7c0a2015-12-23 00:03:41 +010046 msgb_sctp_ppid(msg) = IUH_PPI_HNBAP;
Daniel Willmann6480cad2016-01-06 18:06:26 +010047 osmo_stream_srv_send(ctx->conn, msg);
48
49 return 0;
Harald Welte27f9c4a2015-08-30 22:47:18 +020050}
Harald Welteee77cff2015-08-30 16:57:53 +020051
Stefan Sperlingc964a2c2018-02-19 17:02:12 +010052static int hnbgw_tx_hnb_register_rej(struct hnb_context *ctx)
53{
54 HNBRegisterReject_t reject_out;
55 HNBRegisterRejectIEs_t reject;
56 struct msgb *msg;
57 int rc;
58
59 reject.presenceMask = 0,
60 reject.cause.present = Cause_PR_radioNetwork;
61 reject.cause.choice.radioNetwork = CauseRadioNetwork_unspecified;
62
63 /* encode the Information Elements */
64 memset(&reject_out, 0, sizeof(reject_out));
65 rc = hnbap_encode_hnbregisterrejecties(&reject_out, &reject);
66 if (rc < 0) {
67 LOGP(DHNBAP, LOGL_ERROR, "Failure to encode HNB-REGISTER-REJECT to %s: rc=%d\n",
68 ctx->identity_info, rc);
69 return rc;
70 }
71
72 /* generate a successfull outcome PDU */
73 msg = hnbap_generate_unsuccessful_outcome(ProcedureCode_id_HNBRegister,
74 Criticality_reject,
75 &asn_DEF_HNBRegisterReject,
76 &reject_out);
77
78 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBRegisterReject, &reject_out);
79
80 rc = hnbgw_hnbap_tx(ctx, msg);
81 if (rc == 0) {
82 /* Tell libosmo-netif to destroy this connection when it is done
83 * sending our HNB-REGISTER-REJECT response. */
84 osmo_stream_srv_set_flush_and_destroy(ctx->conn);
85 } else {
86 /* The message was not queued. Destroy the connection right away. */
Alexander Couzensad4ea3b2018-07-24 19:04:47 +020087 hnb_context_release(ctx);
Harald Welte9aad1852018-10-21 11:45:26 +020088 return rc;
Stefan Sperlingc964a2c2018-02-19 17:02:12 +010089 }
Harald Welte9aad1852018-10-21 11:45:26 +020090
91 return 0;
Stefan Sperlingc964a2c2018-02-19 17:02:12 +010092}
93
Harald Welte27f9c4a2015-08-30 22:47:18 +020094static int hnbgw_tx_hnb_register_acc(struct hnb_context *ctx)
95{
96 HNBRegisterAccept_t accept_out;
97 struct msgb *msg;
98 int rc;
99
100 /* Single required response IE: RNC-ID */
101 HNBRegisterAcceptIEs_t accept = {
102 .rnc_id = ctx->gw->config.rnc_id
103 };
104
105 /* encode the Information Elements */
Harald Welte2204f9d2015-09-07 21:10:50 +0200106 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200107 rc = hnbap_encode_hnbregisteraccepties(&accept_out, &accept);
108 if (rc < 0) {
Neels Hofmeyr2293df02018-01-18 19:01:09 +0100109 LOGP(DHNBAP, LOGL_ERROR, "Failure to encode HNB-REGISTER-ACCEPT to %s: rc=%d\n",
110 ctx->identity_info, rc);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200111 return rc;
Harald Welteee77cff2015-08-30 16:57:53 +0200112 }
113
Harald Welte27f9c4a2015-08-30 22:47:18 +0200114 /* generate a successfull outcome PDU */
115 msg = hnbap_generate_successful_outcome(ProcedureCode_id_HNBRegister,
116 Criticality_reject,
117 &asn_DEF_HNBRegisterAccept,
118 &accept_out);
Harald Welteee77cff2015-08-30 16:57:53 +0200119
Daniel Willmann59d17d82015-12-17 17:56:56 +0100120 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBRegisterAccept, &accept_out);
121
Neels Hofmeyr39d7b5c2019-03-04 22:37:59 +0100122 LOGP(DHNBAP, LOGL_NOTICE, "Accepting HNB-REGISTER-REQ from %s\n", ctx->identity_info);
123
Harald Welte27f9c4a2015-08-30 22:47:18 +0200124 return hnbgw_hnbap_tx(ctx, msg);
Harald Welteee77cff2015-08-30 16:57:53 +0200125}
126
127
Harald Welte27f9c4a2015-08-30 22:47:18 +0200128static int hnbgw_tx_ue_register_acc(struct ue_context *ue)
Harald Welteba43de42015-08-29 20:33:16 +0200129{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200130 UERegisterAccept_t accept_out;
131 UERegisterAcceptIEs_t accept;
132 struct msgb *msg;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200133 uint8_t encoded_imsi[10];
Daniel Willmann92247312015-12-09 19:04:33 +0100134 uint32_t ctx_id;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200135 size_t encoded_imsi_len;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200136 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200137
Harald Welte056984f2016-01-03 16:31:31 +0100138 encoded_imsi_len = ranap_imsi_encode(encoded_imsi,
Harald Welte1d2c39d2015-09-11 17:49:37 +0200139 sizeof(encoded_imsi), ue->imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200140
Harald Welte2204f9d2015-09-07 21:10:50 +0200141 memset(&accept, 0, sizeof(accept));
Harald Welte10dfc5a2015-09-11 01:34:45 +0200142 accept.uE_Identity.present = UE_Identity_PR_iMSI;
Harald Welte1d2c39d2015-09-11 17:49:37 +0200143 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.iMSI,
144 (const char *)encoded_imsi, encoded_imsi_len);
Daniel Willmann92247312015-12-09 19:04:33 +0100145 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
Harald Welteba43de42015-08-29 20:33:16 +0200146
Harald Welte2204f9d2015-09-07 21:10:50 +0200147 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200148 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
149 if (rc < 0) {
150 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200151 }
Harald Welte27f9c4a2015-08-30 22:47:18 +0200152
153 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
154 Criticality_reject,
155 &asn_DEF_UERegisterAccept,
156 &accept_out);
Daniel Willmann541e4292015-12-22 16:25:29 +0100157
158 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING, &accept.uE_Identity.choice.iMSI);
159 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
160
Harald Welte27f9c4a2015-08-30 22:47:18 +0200161 return hnbgw_hnbap_tx(ue->hnb, msg);
Harald Welteba43de42015-08-29 20:33:16 +0200162}
163
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200164static int hnbgw_tx_ue_register_rej_tmsi(struct hnb_context *hnb, UE_Identity_t *ue_id)
165{
166 UERegisterReject_t reject_out;
167 UERegisterRejectIEs_t reject;
168 struct msgb *msg;
169 int rc;
170
171 memset(&reject, 0, sizeof(reject));
172 reject.uE_Identity.present = ue_id->present;
173
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200174 /* Copy the identity over to the reject message */
175 switch (ue_id->present) {
176 case UE_Identity_PR_tMSILAI:
177 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id tMSI %d %s\n",
178 ue_id->choice.tMSILAI.tMSI.size,
179 osmo_hexdump(ue_id->choice.tMSILAI.tMSI.buf,
180 ue_id->choice.tMSILAI.tMSI.size));
181
182 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id pLMNID %d %s\n",
183 ue_id->choice.tMSILAI.lAI.pLMNID.size,
184 osmo_hexdump(ue_id->choice.tMSILAI.lAI.pLMNID.buf,
185 ue_id->choice.tMSILAI.lAI.pLMNID.size));
186
187 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id lAC %d %s\n",
188 ue_id->choice.tMSILAI.lAI.lAC.size,
189 osmo_hexdump(ue_id->choice.tMSILAI.lAI.lAC.buf,
190 ue_id->choice.tMSILAI.lAI.lAC.size));
191
192 BIT_STRING_fromBuf(&reject.uE_Identity.choice.tMSILAI.tMSI,
193 ue_id->choice.tMSILAI.tMSI.buf,
194 ue_id->choice.tMSILAI.tMSI.size * 8
195 - ue_id->choice.tMSILAI.tMSI.bits_unused);
196 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.tMSILAI.lAI.pLMNID,
197 ue_id->choice.tMSILAI.lAI.pLMNID.buf,
198 ue_id->choice.tMSILAI.lAI.pLMNID.size);
199 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.tMSILAI.lAI.lAC,
200 ue_id->choice.tMSILAI.lAI.lAC.buf,
201 ue_id->choice.tMSILAI.lAI.lAC.size);
202 break;
203
204 case UE_Identity_PR_pTMSIRAI:
205 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id pTMSI %d %s\n",
206 ue_id->choice.pTMSIRAI.pTMSI.size,
207 osmo_hexdump(ue_id->choice.pTMSIRAI.pTMSI.buf,
208 ue_id->choice.pTMSIRAI.pTMSI.size));
209
210 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id pLMNID %d %s\n",
211 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size,
212 osmo_hexdump(ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.buf,
213 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size));
214
215 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id lAC %d %s\n",
216 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size,
217 osmo_hexdump(ue_id->choice.pTMSIRAI.rAI.lAI.lAC.buf,
218 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size));
219
220 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id rAC %d %s\n",
221 ue_id->choice.pTMSIRAI.rAI.rAC.size,
222 osmo_hexdump(ue_id->choice.pTMSIRAI.rAI.rAC.buf,
223 ue_id->choice.pTMSIRAI.rAI.rAC.size));
224
225 BIT_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.pTMSI,
226 ue_id->choice.pTMSIRAI.pTMSI.buf,
227 ue_id->choice.pTMSIRAI.pTMSI.size * 8
228 - ue_id->choice.pTMSIRAI.pTMSI.bits_unused);
229 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID,
230 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.buf,
231 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size);
232 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC,
233 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.buf,
234 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size);
235 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.rAI.rAC,
236 ue_id->choice.pTMSIRAI.rAI.rAC.buf,
237 ue_id->choice.pTMSIRAI.rAI.rAC.size);
238 break;
239
240 default:
241 LOGP(DHNBAP, LOGL_ERROR, "Cannot compose UE Register Reject:"
242 " unsupported UE ID (present=%d)\n", ue_id->present);
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200243 return -1;
244 }
245
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200246 LOGP(DHNBAP, LOGL_ERROR, "Rejecting UE Register Request:"
247 " TMSI identity registration is switched off\n");
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200248
249 reject.cause.present = Cause_PR_radioNetwork;
250 reject.cause.choice.radioNetwork = CauseRadioNetwork_invalid_UE_identity;
251
252 memset(&reject_out, 0, sizeof(reject_out));
253 rc = hnbap_encode_ueregisterrejecties(&reject_out, &reject);
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200254 if (rc < 0)
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200255 return rc;
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200256
257 msg = hnbap_generate_unsuccessful_outcome(ProcedureCode_id_UERegister,
258 Criticality_reject,
259 &asn_DEF_UERegisterReject,
260 &reject_out);
261
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200262 /* Free copied identity IEs */
263 switch (ue_id->present) {
264 case UE_Identity_PR_tMSILAI:
265 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
266 &reject.uE_Identity.choice.tMSILAI.tMSI);
267 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
268 &reject.uE_Identity.choice.tMSILAI.lAI.pLMNID);
269 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
270 &reject.uE_Identity.choice.tMSILAI.lAI.lAC);
271 break;
272
273 case UE_Identity_PR_pTMSIRAI:
274 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
275 &reject.uE_Identity.choice.pTMSIRAI.pTMSI);
276 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
277 &reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID);
278 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
279 &reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC);
280 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
281 &reject.uE_Identity.choice.pTMSIRAI.rAI.rAC);
282 break;
283
284 default:
285 /* should never happen after above switch() */
286 break;
287 }
288
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200289 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterReject, &reject_out);
290
291 return hnbgw_hnbap_tx(hnb, msg);
292}
293
Neels Hofmeyr12181a92016-04-25 15:05:32 +0200294static int hnbgw_tx_ue_register_acc_tmsi(struct hnb_context *hnb, UE_Identity_t *ue_id)
295{
296 UERegisterAccept_t accept_out;
297 UERegisterAcceptIEs_t accept;
298 struct msgb *msg;
299 uint32_t ctx_id;
300 uint32_t tmsi = 0;
301 struct ue_context *ue;
302 int rc;
303
304 memset(&accept, 0, sizeof(accept));
305 accept.uE_Identity.present = ue_id->present;
306
307 switch (ue_id->present) {
308 case UE_Identity_PR_tMSILAI:
309 BIT_STRING_fromBuf(&accept.uE_Identity.choice.tMSILAI.tMSI,
310 ue_id->choice.tMSILAI.tMSI.buf,
311 ue_id->choice.tMSILAI.tMSI.size * 8
312 - ue_id->choice.tMSILAI.tMSI.bits_unused);
313 tmsi = *(uint32_t*)accept.uE_Identity.choice.tMSILAI.tMSI.buf;
314 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.tMSILAI.lAI.pLMNID,
315 ue_id->choice.tMSILAI.lAI.pLMNID.buf,
316 ue_id->choice.tMSILAI.lAI.pLMNID.size);
317 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.tMSILAI.lAI.lAC,
318 ue_id->choice.tMSILAI.lAI.lAC.buf,
319 ue_id->choice.tMSILAI.lAI.lAC.size);
320 break;
321
322 case UE_Identity_PR_pTMSIRAI:
323 BIT_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.pTMSI,
324 ue_id->choice.pTMSIRAI.pTMSI.buf,
325 ue_id->choice.pTMSIRAI.pTMSI.size * 8
326 - ue_id->choice.pTMSIRAI.pTMSI.bits_unused);
327 tmsi = *(uint32_t*)accept.uE_Identity.choice.pTMSIRAI.pTMSI.buf;
328 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID,
329 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.buf,
330 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size);
331 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC,
332 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.buf,
333 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size);
334 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.rAI.rAC,
335 ue_id->choice.pTMSIRAI.rAI.rAC.buf,
336 ue_id->choice.pTMSIRAI.rAI.rAC.size);
337 break;
338
339 default:
340 LOGP(DHNBAP, LOGL_ERROR, "Unsupportedccept UE ID (present=%d)\n",
341 ue_id->present);
342 return -1;
343 }
344
345 tmsi = ntohl(tmsi);
346 LOGP(DHNBAP, LOGL_DEBUG, "HNBAP register with TMSI %x\n",
347 tmsi);
348
349 ue = ue_context_by_tmsi(hnb->gw, tmsi);
350 if (!ue)
351 ue = ue_context_alloc(hnb, NULL, tmsi);
352
353 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
354
355 memset(&accept_out, 0, sizeof(accept_out));
356 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
357 if (rc < 0)
358 return rc;
359
360 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
361 Criticality_reject,
362 &asn_DEF_UERegisterAccept,
363 &accept_out);
364
365 switch (ue_id->present) {
366 case UE_Identity_PR_tMSILAI:
367 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
368 &accept.uE_Identity.choice.tMSILAI.tMSI);
369 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
370 &accept.uE_Identity.choice.tMSILAI.lAI.pLMNID);
371 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
372 &accept.uE_Identity.choice.tMSILAI.lAI.lAC);
373 break;
374
375 case UE_Identity_PR_pTMSIRAI:
376 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
377 &accept.uE_Identity.choice.pTMSIRAI.pTMSI);
378 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
379 &accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID);
380 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
381 &accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC);
382 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
383 &accept.uE_Identity.choice.pTMSIRAI.rAI.rAC);
384 break;
385
386 default:
387 /* should never happen after above switch() */
388 break;
389 }
390
391 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
392
393 return hnbgw_hnbap_tx(hnb, msg);
394}
395
Daniel Willmannb433b972016-01-06 18:08:21 +0100396static int hnbgw_rx_hnb_deregister(struct hnb_context *ctx, ANY_t *in)
397{
398 HNBDe_RegisterIEs_t ies;
399 int rc;
400
401 rc = hnbap_decode_hnbde_registeries(&ies, in);
402 if (rc < 0)
403 return rc;
404
Harald Welteda86fe52017-11-21 08:14:37 +0100405 DEBUGP(DHNBAP, "HNB-DE-REGISTER cause=%s\n",
406 hnbap_cause_str(&ies.cause));
Daniel Willmannb433b972016-01-06 18:08:21 +0100407
Daniel Willmannd10002c2016-01-07 12:27:41 +0100408 hnbap_free_hnbde_registeries(&ies);
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200409 hnb_context_release(ctx);
Daniel Willmannb433b972016-01-06 18:08:21 +0100410
411 return 0;
412}
413
Harald Welte27f9c4a2015-08-30 22:47:18 +0200414static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200415{
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100416 struct hnb_context *hnb;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200417 HNBRegisterRequestIEs_t ies;
418 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200419
Harald Welte27f9c4a2015-08-30 22:47:18 +0200420 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
Neels Hofmeyr2293df02018-01-18 19:01:09 +0100421 if (rc < 0) {
422 LOGP(DHNBAP, LOGL_ERROR, "Failure to decode HNB-REGISTER-REQ from %s: rc=%d\n",
423 ctx->identity_info, rc);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200424 return rc;
Neels Hofmeyr2293df02018-01-18 19:01:09 +0100425 }
Harald Welteba43de42015-08-29 20:33:16 +0200426
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200427 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200428 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
429 sizeof(ctx->identity_info));
430 ctx->id.lac = asn1str_to_u16(&ies.lac);
431 ctx->id.sac = asn1str_to_u16(&ies.sac);
432 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100433 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Neels Hofmeyr66d6d762017-12-20 05:25:02 +0100434 gsm48_mcc_mnc_from_bcd(ies.plmNidentity.buf, &ctx->id.mcc, &ctx->id.mnc);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200435
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100436 llist_for_each_entry(hnb, &ctx->gw->hnb_list, list) {
437 if (hnb->hnb_registered && ctx != hnb && memcmp(&ctx->id, &hnb->id, sizeof(ctx->id)) == 0) {
438 struct osmo_fd *ofd = osmo_stream_srv_get_ofd(ctx->conn);
439 char *name = osmo_sock_get_name(ctx, ofd->fd);
440 LOGP(DHNBAP, LOGL_ERROR, "rejecting HNB-REGISTER-REQ with duplicate cell identity "
441 "MCC=%u,MNC=%u,LAC=%u,RAC=%u,SAC=%u,CID=%u from %s\n",
442 ctx->id.mcc, ctx->id.mnc, ctx->id.lac, ctx->id.rac, ctx->id.sac, ctx->id.cid, name);
443 talloc_free(name);
444 return hnbgw_tx_hnb_register_rej(ctx);
445 }
446 }
447
448 ctx->hnb_registered = true;
449
Daniel Willmannbded9842015-12-17 11:51:17 +0100450 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200451
Harald Welte27f9c4a2015-08-30 22:47:18 +0200452 /* Send HNBRegisterAccept */
Daniel Willmannd10002c2016-01-07 12:27:41 +0100453 rc = hnbgw_tx_hnb_register_acc(ctx);
454 hnbap_free_hnbregisterrequesties(&ies);
455 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200456}
457
Harald Welte27f9c4a2015-08-30 22:47:18 +0200458static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200459{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200460 UERegisterRequestIEs_t ies;
461 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200462 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200463 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200464
Harald Welte27f9c4a2015-08-30 22:47:18 +0200465 rc = hnbap_decode_ueregisterrequesties(&ies, in);
466 if (rc < 0)
467 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200468
Harald Welte10dfc5a2015-09-11 01:34:45 +0200469 switch (ies.uE_Identity.present) {
470 case UE_Identity_PR_iMSI:
Harald Welte056984f2016-01-03 16:31:31 +0100471 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200472 ies.uE_Identity.choice.iMSI.size);
473 break;
474 case UE_Identity_PR_iMSIDS41:
Harald Welte056984f2016-01-03 16:31:31 +0100475 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200476 ies.uE_Identity.choice.iMSIDS41.size);
477 break;
478 case UE_Identity_PR_iMSIESN:
Harald Welte056984f2016-01-03 16:31:31 +0100479 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200480 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
481 break;
Neels Hofmeyr12181a92016-04-25 15:05:32 +0200482 case UE_Identity_PR_tMSILAI:
483 case UE_Identity_PR_pTMSIRAI:
484 if (ctx->gw->config.hnbap_allow_tmsi)
485 rc = hnbgw_tx_ue_register_acc_tmsi(ctx, &ies.uE_Identity);
486 else
487 rc = hnbgw_tx_ue_register_rej_tmsi(ctx, &ies.uE_Identity);
488 /* all has been handled by TMSI, skip the IMSI code below */
489 hnbap_free_ueregisterrequesties(&ies);
490 return rc;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200491 default:
Neels Hofmeyr12181a92016-04-25 15:05:32 +0200492 LOGP(DHNBAP, LOGL_NOTICE,
493 "UE-REGISTER-REQ with unsupported UE Id type %d\n",
494 ies.uE_Identity.present);
Neels Hofmeyrb248c8c2016-04-25 14:51:27 +0200495 hnbap_free_ueregisterrequesties(&ies);
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200496 return rc;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200497 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200498
Harald Welte706213a2015-12-25 10:33:09 +0100499 DEBUGP(DHNBAP, "UE-REGISTER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200500 ies.uE_Identity.present, imsi, ies.registration_Cause);
501
Harald Weltec4338de2015-12-24 00:40:52 +0100502 ue = ue_context_by_imsi(ctx->gw, imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200503 if (!ue)
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200504 ue = ue_context_alloc(ctx, imsi, 0);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200505
Daniel Willmannd10002c2016-01-07 12:27:41 +0100506 hnbap_free_ueregisterrequesties(&ies);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200507 /* Send UERegisterAccept */
508 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200509}
510
Daniel Willmannefceb182015-12-15 20:30:12 +0100511static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
512{
513 UEDe_RegisterIEs_t ies;
514 struct ue_context *ue;
515 int rc;
516 uint32_t ctxid;
517
518 rc = hnbap_decode_uede_registeries(&ies, in);
519 if (rc < 0)
520 return rc;
521
522 ctxid = asn1bitstr_to_u24(&ies.context_ID);
523
Harald Welteda86fe52017-11-21 08:14:37 +0100524 DEBUGP(DHNBAP, "UE-DE-REGISTER context=%u cause=%s\n",
Harald Welte2963ee22015-12-25 10:32:37 +0100525 ctxid, hnbap_cause_str(&ies.cause));
Daniel Willmannefceb182015-12-15 20:30:12 +0100526
Harald Weltec4338de2015-12-24 00:40:52 +0100527 ue = ue_context_by_id(ctx->gw, ctxid);
Daniel Willmannefceb182015-12-15 20:30:12 +0100528 if (ue)
529 ue_context_free(ue);
530
Daniel Willmannd10002c2016-01-07 12:27:41 +0100531 hnbap_free_uede_registeries(&ies);
Daniel Willmannefceb182015-12-15 20:30:12 +0100532 return 0;
533}
534
Harald Welte3af1db82015-09-11 17:03:16 +0200535static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
536{
537 ErrorIndicationIEs_t ies;
538 int rc;
539
Harald Welte1d2c39d2015-09-11 17:49:37 +0200540 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200541 if (rc < 0)
542 return rc;
543
Daniel Willmannbded9842015-12-17 11:51:17 +0100544 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200545 hnbap_cause_str(&ies.cause));
546
Daniel Willmannd10002c2016-01-07 12:27:41 +0100547 hnbap_free_errorindicationies(&ies);
Harald Welte3af1db82015-09-11 17:03:16 +0200548 return 0;
549}
550
Harald Welte27f9c4a2015-08-30 22:47:18 +0200551static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200552{
Harald Welteda86fe52017-11-21 08:14:37 +0100553 int rc = 0;
Harald Welteba43de42015-08-29 20:33:16 +0200554
Harald Welteb3dae302015-08-30 12:20:09 +0200555 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200556 case ProcedureCode_id_HNBRegister: /* 8.2 */
557 rc = hnbgw_rx_hnb_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200558 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200559 case ProcedureCode_id_HNBDe_Register: /* 8.3 */
Daniel Willmannb433b972016-01-06 18:08:21 +0100560 rc = hnbgw_rx_hnb_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200561 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200562 case ProcedureCode_id_UERegister: /* 8.4 */
563 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200564 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200565 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100566 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200567 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200568 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200569 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
570 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200571 case ProcedureCode_id_TNLUpdate: /* 8.9 */
572 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
573 case ProcedureCode_id_RelocationComplete: /* 8.11 */
574 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
575 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100576 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200577 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200578 break;
579 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100580 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200581 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200582 break;
583 }
Harald Welteda86fe52017-11-21 08:14:37 +0100584
585 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200586}
587
Harald Welte27f9c4a2015-08-30 22:47:18 +0200588static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200589{
Neels Hofmeyr02296af2018-01-15 23:30:06 +0100590 /* We don't care much about HNBAP */
591 return 0;
Harald Welteba43de42015-08-29 20:33:16 +0200592}
593
Harald Welte27f9c4a2015-08-30 22:47:18 +0200594static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200595{
Neels Hofmeyr02296af2018-01-15 23:30:06 +0100596 /* We don't care much about HNBAP */
Neels Hofmeyr8a2b6e22018-01-15 23:34:37 +0100597 LOGP(DHNBAP, LOGL_ERROR, "Received Unsuccessful Outcome, procedureCode %ld, criticality %ld,"
598 " from '%s', cell mcc %u mnc %u lac %u rac %u sac %u cid %u\n",
599 msg->procedureCode, msg->criticality, hnb->identity_info,
600 hnb->id.mcc, hnb->id.mnc, hnb->id.lac, hnb->id.rac, hnb->id.sac, hnb->id.cid);
Neels Hofmeyr02296af2018-01-15 23:30:06 +0100601 return 0;
Harald Welteba43de42015-08-29 20:33:16 +0200602}
603
604
Harald Welte27f9c4a2015-08-30 22:47:18 +0200605static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200606{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100607 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200608
Harald Welteba43de42015-08-29 20:33:16 +0200609 /* it's a bit odd that we can't dispatch on procedure code, but
610 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200611 switch (pdu->present) {
612 case HNBAP_PDU_PR_initiatingMessage:
613 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200614 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200615 case HNBAP_PDU_PR_successfulOutcome:
616 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200617 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200618 case HNBAP_PDU_PR_unsuccessfulOutcome:
619 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200620 break;
621 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100622 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200623 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100624 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200625 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100626
627 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200628}
629
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200630int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200631{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200632 HNBAP_PDU_t _pdu, *pdu = &_pdu;
633 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200634 int rc;
635
636 /* decode and handle to _hnbgw_hnbap_rx() */
637
Harald Welte2204f9d2015-09-07 21:10:50 +0200638 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200639 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
640 msg->data, msgb_length(msg), 0, 0);
641 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100642 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Neels Hofmeyra6a68e62016-11-25 13:21:02 +0100643 return -1;
Harald Welteee77cff2015-08-30 16:57:53 +0200644 }
645
646 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200647
Daniel Willmann59d17d82015-12-17 17:56:56 +0100648 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
649
Harald Welteee77cff2015-08-30 16:57:53 +0200650 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200651}
652
653
654int hnbgw_hnbap_init(void)
655{
Harald Welteda86fe52017-11-21 08:14:37 +0100656 return 0;
Harald Welteba43de42015-08-29 20:33:16 +0200657}