blob: 2a19dda9fd2e147c81eb3a1f7e61f3309b42fa0a [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);
Stefan Sperlingc964a2c2018-02-19 17:02:12 +010088 }
89}
90
Harald Welte27f9c4a2015-08-30 22:47:18 +020091static int hnbgw_tx_hnb_register_acc(struct hnb_context *ctx)
92{
93 HNBRegisterAccept_t accept_out;
94 struct msgb *msg;
95 int rc;
96
97 /* Single required response IE: RNC-ID */
98 HNBRegisterAcceptIEs_t accept = {
99 .rnc_id = ctx->gw->config.rnc_id
100 };
101
102 /* encode the Information Elements */
Harald Welte2204f9d2015-09-07 21:10:50 +0200103 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200104 rc = hnbap_encode_hnbregisteraccepties(&accept_out, &accept);
105 if (rc < 0) {
Neels Hofmeyr2293df02018-01-18 19:01:09 +0100106 LOGP(DHNBAP, LOGL_ERROR, "Failure to encode HNB-REGISTER-ACCEPT to %s: rc=%d\n",
107 ctx->identity_info, rc);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200108 return rc;
Harald Welteee77cff2015-08-30 16:57:53 +0200109 }
110
Harald Welte27f9c4a2015-08-30 22:47:18 +0200111 /* generate a successfull outcome PDU */
112 msg = hnbap_generate_successful_outcome(ProcedureCode_id_HNBRegister,
113 Criticality_reject,
114 &asn_DEF_HNBRegisterAccept,
115 &accept_out);
Harald Welteee77cff2015-08-30 16:57:53 +0200116
Daniel Willmann59d17d82015-12-17 17:56:56 +0100117 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBRegisterAccept, &accept_out);
118
Harald Welte27f9c4a2015-08-30 22:47:18 +0200119 return hnbgw_hnbap_tx(ctx, msg);
Harald Welteee77cff2015-08-30 16:57:53 +0200120}
121
122
Harald Welte27f9c4a2015-08-30 22:47:18 +0200123static int hnbgw_tx_ue_register_acc(struct ue_context *ue)
Harald Welteba43de42015-08-29 20:33:16 +0200124{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200125 UERegisterAccept_t accept_out;
126 UERegisterAcceptIEs_t accept;
127 struct msgb *msg;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200128 uint8_t encoded_imsi[10];
Daniel Willmann92247312015-12-09 19:04:33 +0100129 uint32_t ctx_id;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200130 size_t encoded_imsi_len;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200131 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200132
Harald Welte056984f2016-01-03 16:31:31 +0100133 encoded_imsi_len = ranap_imsi_encode(encoded_imsi,
Harald Welte1d2c39d2015-09-11 17:49:37 +0200134 sizeof(encoded_imsi), ue->imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200135
Harald Welte2204f9d2015-09-07 21:10:50 +0200136 memset(&accept, 0, sizeof(accept));
Harald Welte10dfc5a2015-09-11 01:34:45 +0200137 accept.uE_Identity.present = UE_Identity_PR_iMSI;
Harald Welte1d2c39d2015-09-11 17:49:37 +0200138 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.iMSI,
139 (const char *)encoded_imsi, encoded_imsi_len);
Daniel Willmann92247312015-12-09 19:04:33 +0100140 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
Harald Welteba43de42015-08-29 20:33:16 +0200141
Harald Welte2204f9d2015-09-07 21:10:50 +0200142 memset(&accept_out, 0, sizeof(accept_out));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200143 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
144 if (rc < 0) {
145 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200146 }
Harald Welte27f9c4a2015-08-30 22:47:18 +0200147
148 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
149 Criticality_reject,
150 &asn_DEF_UERegisterAccept,
151 &accept_out);
Daniel Willmann541e4292015-12-22 16:25:29 +0100152
153 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING, &accept.uE_Identity.choice.iMSI);
154 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
155
Harald Welte27f9c4a2015-08-30 22:47:18 +0200156 return hnbgw_hnbap_tx(ue->hnb, msg);
Harald Welteba43de42015-08-29 20:33:16 +0200157}
158
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200159static int hnbgw_tx_ue_register_rej_tmsi(struct hnb_context *hnb, UE_Identity_t *ue_id)
160{
161 UERegisterReject_t reject_out;
162 UERegisterRejectIEs_t reject;
163 struct msgb *msg;
164 int rc;
165
166 memset(&reject, 0, sizeof(reject));
167 reject.uE_Identity.present = ue_id->present;
168
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200169 /* Copy the identity over to the reject message */
170 switch (ue_id->present) {
171 case UE_Identity_PR_tMSILAI:
172 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id tMSI %d %s\n",
173 ue_id->choice.tMSILAI.tMSI.size,
174 osmo_hexdump(ue_id->choice.tMSILAI.tMSI.buf,
175 ue_id->choice.tMSILAI.tMSI.size));
176
177 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id pLMNID %d %s\n",
178 ue_id->choice.tMSILAI.lAI.pLMNID.size,
179 osmo_hexdump(ue_id->choice.tMSILAI.lAI.pLMNID.buf,
180 ue_id->choice.tMSILAI.lAI.pLMNID.size));
181
182 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id lAC %d %s\n",
183 ue_id->choice.tMSILAI.lAI.lAC.size,
184 osmo_hexdump(ue_id->choice.tMSILAI.lAI.lAC.buf,
185 ue_id->choice.tMSILAI.lAI.lAC.size));
186
187 BIT_STRING_fromBuf(&reject.uE_Identity.choice.tMSILAI.tMSI,
188 ue_id->choice.tMSILAI.tMSI.buf,
189 ue_id->choice.tMSILAI.tMSI.size * 8
190 - ue_id->choice.tMSILAI.tMSI.bits_unused);
191 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.tMSILAI.lAI.pLMNID,
192 ue_id->choice.tMSILAI.lAI.pLMNID.buf,
193 ue_id->choice.tMSILAI.lAI.pLMNID.size);
194 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.tMSILAI.lAI.lAC,
195 ue_id->choice.tMSILAI.lAI.lAC.buf,
196 ue_id->choice.tMSILAI.lAI.lAC.size);
197 break;
198
199 case UE_Identity_PR_pTMSIRAI:
200 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id pTMSI %d %s\n",
201 ue_id->choice.pTMSIRAI.pTMSI.size,
202 osmo_hexdump(ue_id->choice.pTMSIRAI.pTMSI.buf,
203 ue_id->choice.pTMSIRAI.pTMSI.size));
204
205 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id pLMNID %d %s\n",
206 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size,
207 osmo_hexdump(ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.buf,
208 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size));
209
210 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id lAC %d %s\n",
211 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size,
212 osmo_hexdump(ue_id->choice.pTMSIRAI.rAI.lAI.lAC.buf,
213 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size));
214
215 LOGP(DHNBAP, LOGL_DEBUG, "REJ UE_Id rAC %d %s\n",
216 ue_id->choice.pTMSIRAI.rAI.rAC.size,
217 osmo_hexdump(ue_id->choice.pTMSIRAI.rAI.rAC.buf,
218 ue_id->choice.pTMSIRAI.rAI.rAC.size));
219
220 BIT_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.pTMSI,
221 ue_id->choice.pTMSIRAI.pTMSI.buf,
222 ue_id->choice.pTMSIRAI.pTMSI.size * 8
223 - ue_id->choice.pTMSIRAI.pTMSI.bits_unused);
224 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID,
225 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.buf,
226 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size);
227 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC,
228 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.buf,
229 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size);
230 OCTET_STRING_fromBuf(&reject.uE_Identity.choice.pTMSIRAI.rAI.rAC,
231 ue_id->choice.pTMSIRAI.rAI.rAC.buf,
232 ue_id->choice.pTMSIRAI.rAI.rAC.size);
233 break;
234
235 default:
236 LOGP(DHNBAP, LOGL_ERROR, "Cannot compose UE Register Reject:"
237 " unsupported UE ID (present=%d)\n", ue_id->present);
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200238 return -1;
239 }
240
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200241 LOGP(DHNBAP, LOGL_ERROR, "Rejecting UE Register Request:"
242 " TMSI identity registration is switched off\n");
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200243
244 reject.cause.present = Cause_PR_radioNetwork;
245 reject.cause.choice.radioNetwork = CauseRadioNetwork_invalid_UE_identity;
246
247 memset(&reject_out, 0, sizeof(reject_out));
248 rc = hnbap_encode_ueregisterrejecties(&reject_out, &reject);
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200249 if (rc < 0)
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200250 return rc;
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200251
252 msg = hnbap_generate_unsuccessful_outcome(ProcedureCode_id_UERegister,
253 Criticality_reject,
254 &asn_DEF_UERegisterReject,
255 &reject_out);
256
Neels Hofmeyrc94ed092016-09-26 01:27:55 +0200257 /* Free copied identity IEs */
258 switch (ue_id->present) {
259 case UE_Identity_PR_tMSILAI:
260 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
261 &reject.uE_Identity.choice.tMSILAI.tMSI);
262 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
263 &reject.uE_Identity.choice.tMSILAI.lAI.pLMNID);
264 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
265 &reject.uE_Identity.choice.tMSILAI.lAI.lAC);
266 break;
267
268 case UE_Identity_PR_pTMSIRAI:
269 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
270 &reject.uE_Identity.choice.pTMSIRAI.pTMSI);
271 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
272 &reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID);
273 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
274 &reject.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC);
275 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
276 &reject.uE_Identity.choice.pTMSIRAI.rAI.rAC);
277 break;
278
279 default:
280 /* should never happen after above switch() */
281 break;
282 }
283
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200284 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterReject, &reject_out);
285
286 return hnbgw_hnbap_tx(hnb, msg);
287}
288
Neels Hofmeyr12181a92016-04-25 15:05:32 +0200289static int hnbgw_tx_ue_register_acc_tmsi(struct hnb_context *hnb, UE_Identity_t *ue_id)
290{
291 UERegisterAccept_t accept_out;
292 UERegisterAcceptIEs_t accept;
293 struct msgb *msg;
294 uint32_t ctx_id;
295 uint32_t tmsi = 0;
296 struct ue_context *ue;
297 int rc;
298
299 memset(&accept, 0, sizeof(accept));
300 accept.uE_Identity.present = ue_id->present;
301
302 switch (ue_id->present) {
303 case UE_Identity_PR_tMSILAI:
304 BIT_STRING_fromBuf(&accept.uE_Identity.choice.tMSILAI.tMSI,
305 ue_id->choice.tMSILAI.tMSI.buf,
306 ue_id->choice.tMSILAI.tMSI.size * 8
307 - ue_id->choice.tMSILAI.tMSI.bits_unused);
308 tmsi = *(uint32_t*)accept.uE_Identity.choice.tMSILAI.tMSI.buf;
309 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.tMSILAI.lAI.pLMNID,
310 ue_id->choice.tMSILAI.lAI.pLMNID.buf,
311 ue_id->choice.tMSILAI.lAI.pLMNID.size);
312 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.tMSILAI.lAI.lAC,
313 ue_id->choice.tMSILAI.lAI.lAC.buf,
314 ue_id->choice.tMSILAI.lAI.lAC.size);
315 break;
316
317 case UE_Identity_PR_pTMSIRAI:
318 BIT_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.pTMSI,
319 ue_id->choice.pTMSIRAI.pTMSI.buf,
320 ue_id->choice.pTMSIRAI.pTMSI.size * 8
321 - ue_id->choice.pTMSIRAI.pTMSI.bits_unused);
322 tmsi = *(uint32_t*)accept.uE_Identity.choice.pTMSIRAI.pTMSI.buf;
323 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID,
324 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.buf,
325 ue_id->choice.pTMSIRAI.rAI.lAI.pLMNID.size);
326 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC,
327 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.buf,
328 ue_id->choice.pTMSIRAI.rAI.lAI.lAC.size);
329 OCTET_STRING_fromBuf(&accept.uE_Identity.choice.pTMSIRAI.rAI.rAC,
330 ue_id->choice.pTMSIRAI.rAI.rAC.buf,
331 ue_id->choice.pTMSIRAI.rAI.rAC.size);
332 break;
333
334 default:
335 LOGP(DHNBAP, LOGL_ERROR, "Unsupportedccept UE ID (present=%d)\n",
336 ue_id->present);
337 return -1;
338 }
339
340 tmsi = ntohl(tmsi);
341 LOGP(DHNBAP, LOGL_DEBUG, "HNBAP register with TMSI %x\n",
342 tmsi);
343
344 ue = ue_context_by_tmsi(hnb->gw, tmsi);
345 if (!ue)
346 ue = ue_context_alloc(hnb, NULL, tmsi);
347
348 asn1_u24_to_bitstring(&accept.context_ID, &ctx_id, ue->context_id);
349
350 memset(&accept_out, 0, sizeof(accept_out));
351 rc = hnbap_encode_ueregisteraccepties(&accept_out, &accept);
352 if (rc < 0)
353 return rc;
354
355 msg = hnbap_generate_successful_outcome(ProcedureCode_id_UERegister,
356 Criticality_reject,
357 &asn_DEF_UERegisterAccept,
358 &accept_out);
359
360 switch (ue_id->present) {
361 case UE_Identity_PR_tMSILAI:
362 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
363 &accept.uE_Identity.choice.tMSILAI.tMSI);
364 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
365 &accept.uE_Identity.choice.tMSILAI.lAI.pLMNID);
366 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
367 &accept.uE_Identity.choice.tMSILAI.lAI.lAC);
368 break;
369
370 case UE_Identity_PR_pTMSIRAI:
371 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_BIT_STRING,
372 &accept.uE_Identity.choice.pTMSIRAI.pTMSI);
373 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
374 &accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.pLMNID);
375 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
376 &accept.uE_Identity.choice.pTMSIRAI.rAI.lAI.lAC);
377 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_OCTET_STRING,
378 &accept.uE_Identity.choice.pTMSIRAI.rAI.rAC);
379 break;
380
381 default:
382 /* should never happen after above switch() */
383 break;
384 }
385
386 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_UERegisterAccept, &accept_out);
387
388 return hnbgw_hnbap_tx(hnb, msg);
389}
390
Daniel Willmannb433b972016-01-06 18:08:21 +0100391static int hnbgw_rx_hnb_deregister(struct hnb_context *ctx, ANY_t *in)
392{
393 HNBDe_RegisterIEs_t ies;
394 int rc;
395
396 rc = hnbap_decode_hnbde_registeries(&ies, in);
397 if (rc < 0)
398 return rc;
399
Harald Welteda86fe52017-11-21 08:14:37 +0100400 DEBUGP(DHNBAP, "HNB-DE-REGISTER cause=%s\n",
401 hnbap_cause_str(&ies.cause));
Daniel Willmannb433b972016-01-06 18:08:21 +0100402
Daniel Willmannd10002c2016-01-07 12:27:41 +0100403 hnbap_free_hnbde_registeries(&ies);
Alexander Couzensad4ea3b2018-07-24 19:04:47 +0200404 hnb_context_release(ctx);
Daniel Willmannb433b972016-01-06 18:08:21 +0100405
406 return 0;
407}
408
Harald Welte27f9c4a2015-08-30 22:47:18 +0200409static int hnbgw_rx_hnb_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200410{
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100411 struct hnb_context *hnb;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200412 HNBRegisterRequestIEs_t ies;
413 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200414
Harald Welte27f9c4a2015-08-30 22:47:18 +0200415 rc = hnbap_decode_hnbregisterrequesties(&ies, in);
Neels Hofmeyr2293df02018-01-18 19:01:09 +0100416 if (rc < 0) {
417 LOGP(DHNBAP, LOGL_ERROR, "Failure to decode HNB-REGISTER-REQ from %s: rc=%d\n",
418 ctx->identity_info, rc);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200419 return rc;
Neels Hofmeyr2293df02018-01-18 19:01:09 +0100420 }
Harald Welteba43de42015-08-29 20:33:16 +0200421
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200422 /* copy all identity parameters from the message to ctx */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200423 asn1_strncpy(ctx->identity_info, &ies.hnB_Identity.hNB_Identity_Info,
424 sizeof(ctx->identity_info));
425 ctx->id.lac = asn1str_to_u16(&ies.lac);
426 ctx->id.sac = asn1str_to_u16(&ies.sac);
427 ctx->id.rac = asn1str_to_u8(&ies.rac);
Daniel Willmannd6a45b42015-12-08 13:55:17 +0100428 ctx->id.cid = asn1bitstr_to_u28(&ies.cellIdentity);
Neels Hofmeyr66d6d762017-12-20 05:25:02 +0100429 gsm48_mcc_mnc_from_bcd(ies.plmNidentity.buf, &ctx->id.mcc, &ctx->id.mnc);
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200430
Stefan Sperlingc964a2c2018-02-19 17:02:12 +0100431 llist_for_each_entry(hnb, &ctx->gw->hnb_list, list) {
432 if (hnb->hnb_registered && ctx != hnb && memcmp(&ctx->id, &hnb->id, sizeof(ctx->id)) == 0) {
433 struct osmo_fd *ofd = osmo_stream_srv_get_ofd(ctx->conn);
434 char *name = osmo_sock_get_name(ctx, ofd->fd);
435 LOGP(DHNBAP, LOGL_ERROR, "rejecting HNB-REGISTER-REQ with duplicate cell identity "
436 "MCC=%u,MNC=%u,LAC=%u,RAC=%u,SAC=%u,CID=%u from %s\n",
437 ctx->id.mcc, ctx->id.mnc, ctx->id.lac, ctx->id.rac, ctx->id.sac, ctx->id.cid, name);
438 talloc_free(name);
439 return hnbgw_tx_hnb_register_rej(ctx);
440 }
441 }
442
443 ctx->hnb_registered = true;
444
Daniel Willmannbded9842015-12-17 11:51:17 +0100445 DEBUGP(DHNBAP, "HNB-REGISTER-REQ from %s\n", ctx->identity_info);
Harald Welteee77cff2015-08-30 16:57:53 +0200446
Harald Welte27f9c4a2015-08-30 22:47:18 +0200447 /* Send HNBRegisterAccept */
Daniel Willmannd10002c2016-01-07 12:27:41 +0100448 rc = hnbgw_tx_hnb_register_acc(ctx);
449 hnbap_free_hnbregisterrequesties(&ies);
450 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200451}
452
Harald Welte27f9c4a2015-08-30 22:47:18 +0200453static int hnbgw_rx_ue_register_req(struct hnb_context *ctx, ANY_t *in)
Harald Welteba43de42015-08-29 20:33:16 +0200454{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200455 UERegisterRequestIEs_t ies;
456 struct ue_context *ue;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200457 char imsi[16];
Harald Welte27f9c4a2015-08-30 22:47:18 +0200458 int rc;
Harald Welteba43de42015-08-29 20:33:16 +0200459
Harald Welte27f9c4a2015-08-30 22:47:18 +0200460 rc = hnbap_decode_ueregisterrequesties(&ies, in);
461 if (rc < 0)
462 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200463
Harald Welte10dfc5a2015-09-11 01:34:45 +0200464 switch (ies.uE_Identity.present) {
465 case UE_Identity_PR_iMSI:
Harald Welte056984f2016-01-03 16:31:31 +0100466 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSI.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200467 ies.uE_Identity.choice.iMSI.size);
468 break;
469 case UE_Identity_PR_iMSIDS41:
Harald Welte056984f2016-01-03 16:31:31 +0100470 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200471 ies.uE_Identity.choice.iMSIDS41.size);
472 break;
473 case UE_Identity_PR_iMSIESN:
Harald Welte056984f2016-01-03 16:31:31 +0100474 ranap_bcd_decode(imsi, sizeof(imsi), ies.uE_Identity.choice.iMSIESN.iMSIDS41.buf,
Harald Welte10dfc5a2015-09-11 01:34:45 +0200475 ies.uE_Identity.choice.iMSIESN.iMSIDS41.size);
476 break;
Neels Hofmeyr12181a92016-04-25 15:05:32 +0200477 case UE_Identity_PR_tMSILAI:
478 case UE_Identity_PR_pTMSIRAI:
479 if (ctx->gw->config.hnbap_allow_tmsi)
480 rc = hnbgw_tx_ue_register_acc_tmsi(ctx, &ies.uE_Identity);
481 else
482 rc = hnbgw_tx_ue_register_rej_tmsi(ctx, &ies.uE_Identity);
483 /* all has been handled by TMSI, skip the IMSI code below */
484 hnbap_free_ueregisterrequesties(&ies);
485 return rc;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200486 default:
Neels Hofmeyr12181a92016-04-25 15:05:32 +0200487 LOGP(DHNBAP, LOGL_NOTICE,
488 "UE-REGISTER-REQ with unsupported UE Id type %d\n",
489 ies.uE_Identity.present);
Neels Hofmeyrb248c8c2016-04-25 14:51:27 +0200490 hnbap_free_ueregisterrequesties(&ies);
Neels Hofmeyr1a0bb512016-04-25 14:55:35 +0200491 return rc;
Harald Welte10dfc5a2015-09-11 01:34:45 +0200492 }
Harald Welteb534e5c2015-09-11 00:15:16 +0200493
Harald Welte706213a2015-12-25 10:33:09 +0100494 DEBUGP(DHNBAP, "UE-REGISTER-REQ ID_type=%d imsi=%s cause=%ld\n",
Harald Welte10dfc5a2015-09-11 01:34:45 +0200495 ies.uE_Identity.present, imsi, ies.registration_Cause);
496
Harald Weltec4338de2015-12-24 00:40:52 +0100497 ue = ue_context_by_imsi(ctx->gw, imsi);
Harald Welte10dfc5a2015-09-11 01:34:45 +0200498 if (!ue)
Neels Hofmeyrf33e8352016-09-22 18:06:59 +0200499 ue = ue_context_alloc(ctx, imsi, 0);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200500
Daniel Willmannd10002c2016-01-07 12:27:41 +0100501 hnbap_free_ueregisterrequesties(&ies);
Harald Welte27f9c4a2015-08-30 22:47:18 +0200502 /* Send UERegisterAccept */
503 return hnbgw_tx_ue_register_acc(ue);
Harald Welteba43de42015-08-29 20:33:16 +0200504}
505
Daniel Willmannefceb182015-12-15 20:30:12 +0100506static int hnbgw_rx_ue_deregister(struct hnb_context *ctx, ANY_t *in)
507{
508 UEDe_RegisterIEs_t ies;
509 struct ue_context *ue;
510 int rc;
511 uint32_t ctxid;
512
513 rc = hnbap_decode_uede_registeries(&ies, in);
514 if (rc < 0)
515 return rc;
516
517 ctxid = asn1bitstr_to_u24(&ies.context_ID);
518
Harald Welteda86fe52017-11-21 08:14:37 +0100519 DEBUGP(DHNBAP, "UE-DE-REGISTER context=%u cause=%s\n",
Harald Welte2963ee22015-12-25 10:32:37 +0100520 ctxid, hnbap_cause_str(&ies.cause));
Daniel Willmannefceb182015-12-15 20:30:12 +0100521
Harald Weltec4338de2015-12-24 00:40:52 +0100522 ue = ue_context_by_id(ctx->gw, ctxid);
Daniel Willmannefceb182015-12-15 20:30:12 +0100523 if (ue)
524 ue_context_free(ue);
525
Daniel Willmannd10002c2016-01-07 12:27:41 +0100526 hnbap_free_uede_registeries(&ies);
Daniel Willmannefceb182015-12-15 20:30:12 +0100527 return 0;
528}
529
Harald Welte3af1db82015-09-11 17:03:16 +0200530static int hnbgw_rx_err_ind(struct hnb_context *hnb, ANY_t *in)
531{
532 ErrorIndicationIEs_t ies;
533 int rc;
534
Harald Welte1d2c39d2015-09-11 17:49:37 +0200535 rc = hnbap_decode_errorindicationies(&ies, in);
Harald Welte3af1db82015-09-11 17:03:16 +0200536 if (rc < 0)
537 return rc;
538
Daniel Willmannbded9842015-12-17 11:51:17 +0100539 LOGP(DHNBAP, LOGL_NOTICE, "HNBAP ERROR.ind, cause: %s\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200540 hnbap_cause_str(&ies.cause));
541
Daniel Willmannd10002c2016-01-07 12:27:41 +0100542 hnbap_free_errorindicationies(&ies);
Harald Welte3af1db82015-09-11 17:03:16 +0200543 return 0;
544}
545
Harald Welte27f9c4a2015-08-30 22:47:18 +0200546static int hnbgw_rx_initiating_msg(struct hnb_context *hnb, InitiatingMessage_t *imsg)
Harald Welteba43de42015-08-29 20:33:16 +0200547{
Harald Welteda86fe52017-11-21 08:14:37 +0100548 int rc = 0;
Harald Welteba43de42015-08-29 20:33:16 +0200549
Harald Welteb3dae302015-08-30 12:20:09 +0200550 switch (imsg->procedureCode) {
Harald Welte27f9c4a2015-08-30 22:47:18 +0200551 case ProcedureCode_id_HNBRegister: /* 8.2 */
552 rc = hnbgw_rx_hnb_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200553 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200554 case ProcedureCode_id_HNBDe_Register: /* 8.3 */
Daniel Willmannb433b972016-01-06 18:08:21 +0100555 rc = hnbgw_rx_hnb_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200556 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200557 case ProcedureCode_id_UERegister: /* 8.4 */
558 rc = hnbgw_rx_ue_register_req(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200559 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200560 case ProcedureCode_id_UEDe_Register: /* 8.5 */
Daniel Willmannefceb182015-12-15 20:30:12 +0100561 rc = hnbgw_rx_ue_deregister(hnb, &imsg->value);
Harald Welteba43de42015-08-29 20:33:16 +0200562 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200563 case ProcedureCode_id_ErrorIndication: /* 8.6 */
Harald Welte3af1db82015-09-11 17:03:16 +0200564 rc = hnbgw_rx_err_ind(hnb, &imsg->value);
565 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200566 case ProcedureCode_id_TNLUpdate: /* 8.9 */
567 case ProcedureCode_id_HNBConfigTransfer: /* 8.10 */
568 case ProcedureCode_id_RelocationComplete: /* 8.11 */
569 case ProcedureCode_id_U_RNTIQuery: /* 8.12 */
570 case ProcedureCode_id_privateMessage:
Daniel Willmannbded9842015-12-17 11:51:17 +0100571 LOGP(DHNBAP, LOGL_NOTICE, "Unimplemented HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200572 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200573 break;
574 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100575 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Procedure %ld\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200576 imsg->procedureCode);
Harald Welteba43de42015-08-29 20:33:16 +0200577 break;
578 }
Harald Welteda86fe52017-11-21 08:14:37 +0100579
580 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200581}
582
Harald Welte27f9c4a2015-08-30 22:47:18 +0200583static int hnbgw_rx_successful_outcome_msg(struct hnb_context *hnb, SuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200584{
Neels Hofmeyr02296af2018-01-15 23:30:06 +0100585 /* We don't care much about HNBAP */
586 return 0;
Harald Welteba43de42015-08-29 20:33:16 +0200587}
588
Harald Welte27f9c4a2015-08-30 22:47:18 +0200589static int hnbgw_rx_unsuccessful_outcome_msg(struct hnb_context *hnb, UnsuccessfulOutcome_t *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200590{
Neels Hofmeyr02296af2018-01-15 23:30:06 +0100591 /* We don't care much about HNBAP */
Neels Hofmeyr8a2b6e22018-01-15 23:34:37 +0100592 LOGP(DHNBAP, LOGL_ERROR, "Received Unsuccessful Outcome, procedureCode %ld, criticality %ld,"
593 " from '%s', cell mcc %u mnc %u lac %u rac %u sac %u cid %u\n",
594 msg->procedureCode, msg->criticality, hnb->identity_info,
595 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 +0100596 return 0;
Harald Welteba43de42015-08-29 20:33:16 +0200597}
598
599
Harald Welte27f9c4a2015-08-30 22:47:18 +0200600static int _hnbgw_hnbap_rx(struct hnb_context *hnb, HNBAP_PDU_t *pdu)
Harald Welteba43de42015-08-29 20:33:16 +0200601{
Daniel Willmann5f810f42015-12-17 17:57:51 +0100602 int rc = 0;
Harald Welteb3dae302015-08-30 12:20:09 +0200603
Harald Welteba43de42015-08-29 20:33:16 +0200604 /* it's a bit odd that we can't dispatch on procedure code, but
605 * that's not possible */
Harald Welte27f9c4a2015-08-30 22:47:18 +0200606 switch (pdu->present) {
607 case HNBAP_PDU_PR_initiatingMessage:
608 rc = hnbgw_rx_initiating_msg(hnb, &pdu->choice.initiatingMessage);
Harald Welteba43de42015-08-29 20:33:16 +0200609 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200610 case HNBAP_PDU_PR_successfulOutcome:
611 rc = hnbgw_rx_successful_outcome_msg(hnb, &pdu->choice.successfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200612 break;
Harald Welte27f9c4a2015-08-30 22:47:18 +0200613 case HNBAP_PDU_PR_unsuccessfulOutcome:
614 rc = hnbgw_rx_unsuccessful_outcome_msg(hnb, &pdu->choice.unsuccessfulOutcome);
Harald Welteba43de42015-08-29 20:33:16 +0200615 break;
616 default:
Daniel Willmannbded9842015-12-17 11:51:17 +0100617 LOGP(DHNBAP, LOGL_NOTICE, "Unknown HNBAP Presence %u\n",
Harald Welte3af1db82015-09-11 17:03:16 +0200618 pdu->present);
Daniel Willmann5f810f42015-12-17 17:57:51 +0100619 rc = -1;
Harald Welteba43de42015-08-29 20:33:16 +0200620 }
Daniel Willmann5f810f42015-12-17 17:57:51 +0100621
622 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200623}
624
Harald Weltea2e6a7a2015-08-29 21:47:39 +0200625int hnbgw_hnbap_rx(struct hnb_context *hnb, struct msgb *msg)
Harald Welteba43de42015-08-29 20:33:16 +0200626{
Harald Welte27f9c4a2015-08-30 22:47:18 +0200627 HNBAP_PDU_t _pdu, *pdu = &_pdu;
628 asn_dec_rval_t dec_ret;
Harald Welteee77cff2015-08-30 16:57:53 +0200629 int rc;
630
631 /* decode and handle to _hnbgw_hnbap_rx() */
632
Harald Welte2204f9d2015-09-07 21:10:50 +0200633 memset(pdu, 0, sizeof(*pdu));
Harald Welte27f9c4a2015-08-30 22:47:18 +0200634 dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
635 msg->data, msgb_length(msg), 0, 0);
636 if (dec_ret.code != RC_OK) {
Daniel Willmannbded9842015-12-17 11:51:17 +0100637 LOGP(DHNBAP, LOGL_ERROR, "Error in ASN.1 decode\n");
Neels Hofmeyra6a68e62016-11-25 13:21:02 +0100638 return -1;
Harald Welteee77cff2015-08-30 16:57:53 +0200639 }
640
641 rc = _hnbgw_hnbap_rx(hnb, pdu);
Harald Welteee77cff2015-08-30 16:57:53 +0200642
Daniel Willmann59d17d82015-12-17 17:56:56 +0100643 ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
644
Harald Welteee77cff2015-08-30 16:57:53 +0200645 return rc;
Harald Welteba43de42015-08-29 20:33:16 +0200646}
647
648
649int hnbgw_hnbap_init(void)
650{
Harald Welteda86fe52017-11-21 08:14:37 +0100651 return 0;
Harald Welteba43de42015-08-29 20:33:16 +0200652}