blob: c48f4b7a0d567597e0b849fdbd53ea8cbe11b829 [file] [log] [blame]
Harald Weltec4338de2015-12-24 00:40:52 +01001/* IuCS/IuPS Core Network interface of HNB-GW */
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
Neels Hofmeyr37017f52016-04-15 22:47:21 +020021#include <arpa/inet.h>
22
Harald Weltec4338de2015-12-24 00:40:52 +010023#include <osmocom/core/msgb.h>
24#include <osmocom/core/utils.h>
25#include <osmocom/core/timer.h>
26
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020027#include <osmocom/sigtran/protocol/m3ua.h>
Harald Weltec4338de2015-12-24 00:40:52 +010028#include <osmocom/sigtran/sccp_sap.h>
Neels Hofmeyr3da86082016-03-30 12:36:15 +020029#include <osmocom/sigtran/sccp_helpers.h>
Harald Weltec4338de2015-12-24 00:40:52 +010030
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020031#include <osmocom/iuh/hnbgw.h>
32#include <osmocom/iuh/hnbgw_rua.h>
Neels Hofmeyr96979af2016-01-05 15:19:44 +010033#include <osmocom/ranap/ranap_ies_defs.h>
34#include <osmocom/ranap/ranap_msg_factory.h>
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020035#include <osmocom/iuh/context_map.h>
Harald Weltec4338de2015-12-24 00:40:52 +010036
Harald Weltec4338de2015-12-24 00:40:52 +010037/***********************************************************************
38 * Outbound RANAP RESET to CN
39 ***********************************************************************/
40
Harald Welteda86fe52017-11-21 08:14:37 +010041void hnbgw_cnlink_change_state(struct hnbgw_cnlink *cnlink, enum hnbgw_cnlink_state state);
Harald Weltec4338de2015-12-24 00:40:52 +010042
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020043static int transmit_rst(struct hnb_gw *gw, RANAP_CN_DomainIndicator_t domain,
44 struct osmo_sccp_addr *remote_addr)
Harald Weltec4338de2015-12-24 00:40:52 +010045{
46 struct msgb *msg;
Harald Weltec4338de2015-12-24 00:40:52 +010047 RANAP_Cause_t cause = {
48 .present = RANAP_Cause_PR_transmissionNetwork,
49 .choice. transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
50 };
51
Harald Weltec4338de2015-12-24 00:40:52 +010052 msg = ranap_new_msg_reset(domain, &cause);
53
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020054 return osmo_sccp_tx_unitdata_msg(gw->sccp.cnlink->sccp_user,
55 &gw->sccp.local_addr,
56 remote_addr,
57 msg);
Harald Weltec4338de2015-12-24 00:40:52 +010058}
59
60/* Timer callback once T_RafC expires */
61static void cnlink_trafc_cb(void *data)
62{
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020063 struct hnb_gw *gw = data;
Harald Weltec4338de2015-12-24 00:40:52 +010064
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +020065 transmit_rst(gw, RANAP_CN_DomainIndicator_cs_domain, &gw->sccp.iucs_remote_addr);
66 transmit_rst(gw, RANAP_CN_DomainIndicator_ps_domain, &gw->sccp.iups_remote_addr);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020067 hnbgw_cnlink_change_state(gw->sccp.cnlink, CNLINK_S_EST_RST_TX_WAIT_ACK);
Harald Weltec4338de2015-12-24 00:40:52 +010068 /* The spec states that we should abandon after a configurable
69 * number of times. We decide to simply continue trying */
70}
71
72/* change the state of a CN Link */
Harald Welteda86fe52017-11-21 08:14:37 +010073void hnbgw_cnlink_change_state(struct hnbgw_cnlink *cnlink, enum hnbgw_cnlink_state state)
Harald Weltec4338de2015-12-24 00:40:52 +010074{
75 switch (state) {
76 case CNLINK_S_NULL:
77 case CNLINK_S_EST_PEND:
78 break;
79 case CNLINK_S_EST_CONF:
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020080 cnlink_trafc_cb(cnlink->gw);
Harald Weltec4338de2015-12-24 00:40:52 +010081 break;
82 case CNLINK_S_EST_RST_TX_WAIT_ACK:
83 osmo_timer_schedule(&cnlink->T_RafC, 5, 0);
84 break;
85 case CNLINK_S_EST_ACTIVE:
86 osmo_timer_del(&cnlink->T_RafC);
87 break;
88 }
89}
90
91/***********************************************************************
92 * Incoming primitives from SCCP User SAP
93 ***********************************************************************/
94
95static int cn_ranap_rx_reset_cmd(struct hnbgw_cnlink *cnlink,
96 RANAP_InitiatingMessage_t *imsg)
97{
98 RANAP_ResetIEs_t ies;
99 int rc;
100
101 rc = ranap_decode_reseties(&ies, &imsg->value);
102 /* FIXME: reset resources and return reset ack */
Daniel Willmann11e912a2016-01-07 13:19:30 +0100103
104 ranap_free_reseties(&ies);
Harald Weltec4338de2015-12-24 00:40:52 +0100105 return rc;
106}
107
108static int cn_ranap_rx_reset_ack(struct hnbgw_cnlink *cnlink,
109 RANAP_SuccessfulOutcome_t *omsg)
110{
111 RANAP_ResetAcknowledgeIEs_t ies;
112 int rc;
113
114 rc = ranap_decode_resetacknowledgeies(&ies, &omsg->value);
115
116 hnbgw_cnlink_change_state(cnlink, CNLINK_S_EST_ACTIVE);
117
Daniel Willmann11e912a2016-01-07 13:19:30 +0100118 ranap_free_resetacknowledgeies(&ies);
Harald Weltec4338de2015-12-24 00:40:52 +0100119 return rc;
120}
121
122static int cn_ranap_rx_paging_cmd(struct hnbgw_cnlink *cnlink,
Harald Weltebc4560c2015-12-24 08:46:58 +0100123 RANAP_InitiatingMessage_t *imsg,
124 const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100125{
Neels Hofmeyr2b01f3a2016-04-19 17:57:03 +0200126 struct hnb_gw *gw = cnlink->gw;
Harald Weltebc4560c2015-12-24 08:46:58 +0100127 struct hnb_context *hnb;
Harald Weltec4338de2015-12-24 00:40:52 +0100128 RANAP_PagingIEs_t ies;
Harald Welteda86fe52017-11-21 08:14:37 +0100129 int rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100130
131 rc = ranap_decode_pagingies(&ies, &imsg->value);
Harald Welteda86fe52017-11-21 08:14:37 +0100132 if (rc < 0)
133 return rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100134
Harald Weltebc4560c2015-12-24 08:46:58 +0100135 /* FIXME: determine which HNBs to send this Paging command,
136 * rather than broadcasting to all HNBs */
137 llist_for_each_entry(hnb, &gw->hnb_list, list) {
138 rc = rua_tx_udt(hnb, data, len);
139 }
Daniel Willmann11e912a2016-01-07 13:19:30 +0100140
141 ranap_free_pagingies(&ies);
Harald Weltebc4560c2015-12-24 08:46:58 +0100142 return 0;
Harald Weltec4338de2015-12-24 00:40:52 +0100143}
144
145static int cn_ranap_rx_initiating_msg(struct hnbgw_cnlink *cnlink,
Harald Weltebc4560c2015-12-24 08:46:58 +0100146 RANAP_InitiatingMessage_t *imsg,
147 const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100148{
Harald Weltec4338de2015-12-24 00:40:52 +0100149 switch (imsg->procedureCode) {
150 case RANAP_ProcedureCode_id_Reset:
151 return cn_ranap_rx_reset_cmd(cnlink, imsg);
152 case RANAP_ProcedureCode_id_Paging:
Harald Weltebc4560c2015-12-24 08:46:58 +0100153 return cn_ranap_rx_paging_cmd(cnlink, imsg, data, len);
Harald Weltec4338de2015-12-24 00:40:52 +0100154 case RANAP_ProcedureCode_id_OverloadControl: /* Overload ind */
155 break;
156 case RANAP_ProcedureCode_id_ErrorIndication: /* Error ind */
157 break;
158 case RANAP_ProcedureCode_id_ResetResource: /* request */
159 case RANAP_ProcedureCode_id_InformationTransfer:
160 case RANAP_ProcedureCode_id_DirectInformationTransfer:
161 case RANAP_ProcedureCode_id_UplinkInformationExchange:
162 LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100163 "Procedure %ld from CN, ignoring\n", imsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100164 break;
165 default:
166 LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100167 "Procedure %ld from CN, ignoring\n", imsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100168 break;
169 }
170 return 0;
171}
172
173static int cn_ranap_rx_successful_msg(struct hnbgw_cnlink *cnlink,
174 RANAP_SuccessfulOutcome_t *omsg)
175{
Harald Weltec4338de2015-12-24 00:40:52 +0100176 switch (omsg->procedureCode) {
177 case RANAP_ProcedureCode_id_Reset: /* Reset acknowledge */
178 return cn_ranap_rx_reset_ack(cnlink, omsg);
179 case RANAP_ProcedureCode_id_ResetResource: /* response */
180 case RANAP_ProcedureCode_id_InformationTransfer:
181 case RANAP_ProcedureCode_id_DirectInformationTransfer:
182 case RANAP_ProcedureCode_id_UplinkInformationExchange:
183 LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100184 "Procedure %ld from CN, ignoring\n", omsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100185 break;
186 default:
187 LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100188 "Procedure %ld from CN, ignoring\n", omsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100189 break;
190 }
191 return 0;
192}
193
194
Harald Weltebc4560c2015-12-24 08:46:58 +0100195static int _cn_ranap_rx(struct hnbgw_cnlink *cnlink, RANAP_RANAP_PDU_t *pdu,
196 const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100197{
198 int rc;
199
200 switch (pdu->present) {
201 case RANAP_RANAP_PDU_PR_initiatingMessage:
Harald Weltebc4560c2015-12-24 08:46:58 +0100202 rc = cn_ranap_rx_initiating_msg(cnlink, &pdu->choice.initiatingMessage,
203 data, len);
Harald Weltec4338de2015-12-24 00:40:52 +0100204 break;
205 case RANAP_RANAP_PDU_PR_successfulOutcome:
206 rc = cn_ranap_rx_successful_msg(cnlink, &pdu->choice.successfulOutcome);
207 break;
208 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
209 LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100210 "unsuccessful outcome procedure %ld from CN, ignoring\n",
Harald Weltec4338de2015-12-24 00:40:52 +0100211 pdu->choice.unsuccessfulOutcome.procedureCode);
212 break;
213 default:
214 LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP "
215 "presence %u from CN, ignoring\n", pdu->present);
216 break;
217 }
Harald Welteda86fe52017-11-21 08:14:37 +0100218
219 return rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100220}
221
222static int handle_cn_ranap(struct hnbgw_cnlink *cnlink, const uint8_t *data,
223 unsigned int len)
224{
225 RANAP_RANAP_PDU_t _pdu, *pdu = &_pdu;
226 asn_dec_rval_t dec_ret;
227 int rc;
228
229 memset(pdu, 0, sizeof(*pdu));
230 dec_ret = aper_decode(NULL,&asn_DEF_RANAP_RANAP_PDU, (void **) &pdu,
231 data, len, 0, 0);
232 if (dec_ret.code != RC_OK) {
233 LOGP(DRANAP, LOGL_ERROR, "Error in RANAP ASN.1 decode\n");
Neels Hofmeyra6a68e62016-11-25 13:21:02 +0100234 return -1;
Harald Weltec4338de2015-12-24 00:40:52 +0100235 }
236
Harald Weltebc4560c2015-12-24 08:46:58 +0100237 rc = _cn_ranap_rx(cnlink, pdu, data, len);
Harald Weltec4338de2015-12-24 00:40:52 +0100238
239 return rc;
240}
241
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200242static bool pc_and_ssn_match(const struct osmo_sccp_addr *a, const struct osmo_sccp_addr *b)
243{
244 return (a == b)
245 || ((a->pc == b->pc)
246 && (a->ssn == b->ssn));
247}
248
249static int classify_cn_remote_addr(const struct hnb_gw *gw,
250 const struct osmo_sccp_addr *cn_remote_addr,
251 bool *is_ps)
252{
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200253 if (pc_and_ssn_match(cn_remote_addr, &gw->sccp.iucs_remote_addr)) {
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200254 if (is_ps)
255 *is_ps = false;
256 return 0;
257 }
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200258 if (pc_and_ssn_match(cn_remote_addr, &gw->sccp.iups_remote_addr)) {
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200259 if (is_ps)
260 *is_ps = true;
261 return 0;
262 }
263 LOGP(DMAIN, LOGL_ERROR, "Unexpected remote address, matches neither CS nor PS address: %s\n",
264 osmo_sccp_addr_dump(cn_remote_addr));
265 return -1;
266}
Harald Weltec4338de2015-12-24 00:40:52 +0100267
268static int handle_cn_unitdata(struct hnbgw_cnlink *cnlink,
269 const struct osmo_scu_unitdata_param *param,
270 struct osmo_prim_hdr *oph)
271{
Harald Welte8c572fe2015-12-26 08:42:07 +0100272 if (param->called_addr.ssn != OSMO_SCCP_SSN_RANAP) {
Harald Weltec4338de2015-12-24 00:40:52 +0100273 LOGP(DMAIN, LOGL_NOTICE, "N-UNITDATA.ind for unknown SSN %u\n",
274 param->called_addr.ssn);
275 return -1;
276 }
277
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200278 if (classify_cn_remote_addr(cnlink->gw, &param->calling_addr, NULL) < 0)
279 return -1;
280
Harald Weltec4338de2015-12-24 00:40:52 +0100281 return handle_cn_ranap(cnlink, msgb_l2(oph->msg), msgb_l2len(oph->msg));
282}
283
Neels Hofmeyr65037672016-04-19 18:00:54 +0200284static int handle_cn_conn_conf(struct hnbgw_cnlink *cnlink,
Neels Hofmeyr630483b2016-04-19 18:01:25 +0200285 const struct osmo_scu_connect_param *param,
286 struct osmo_prim_hdr *oph)
Harald Weltec4338de2015-12-24 00:40:52 +0100287{
288 /* we don't actually need to do anything, as RUA towards the HNB
289 * doesn't seem to know any confirmations to its CONNECT
290 * operation */
291
Neels Hofmeyr0ff24432016-04-04 18:33:33 +0200292 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() conn_id=%d\n",
293 param->conn_id);
294 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() called_addr=%s\n",
295 inet_ntoa(param->called_addr.ip.v4));
296 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() calling_addr=%s\n",
297 inet_ntoa(param->calling_addr.ip.v4));
298 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() responding_addr=%s\n",
299 inet_ntoa(param->responding_addr.ip.v4));
300
Harald Weltec4338de2015-12-24 00:40:52 +0100301 return 0;
302}
303
Neels Hofmeyr65037672016-04-19 18:00:54 +0200304static int handle_cn_data_ind(struct hnbgw_cnlink *cnlink,
Harald Weltec4338de2015-12-24 00:40:52 +0100305 const struct osmo_scu_data_param *param,
306 struct osmo_prim_hdr *oph)
307{
308 struct hnbgw_context_map *map;
309
310 /* connection-oriented data is always passed transparently
311 * towards the specific HNB, via a RUA connection identified by
312 * conn_id */
313
314 map = context_map_by_cn(cnlink, param->conn_id);
315 if (!map) {
316 /* FIXME: Return an error / released primitive */
317 return 0;
318 }
319
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200320 return rua_tx_dt(map->hnb_ctx, map->is_ps, map->rua_ctx_id,
Harald Weltec4338de2015-12-24 00:40:52 +0100321 msgb_l2(oph->msg), msgb_l2len(oph->msg));
322}
323
Neels Hofmeyr65037672016-04-19 18:00:54 +0200324static int handle_cn_disc_ind(struct hnbgw_cnlink *cnlink,
Harald Weltec4338de2015-12-24 00:40:52 +0100325 const struct osmo_scu_disconn_param *param,
326 struct osmo_prim_hdr *oph)
327{
328 struct hnbgw_context_map *map;
329
Neels Hofmeyr02be4e32016-04-04 19:29:35 +0200330 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_disc_ind() conn_id=%d originator=%d\n",
331 param->conn_id, param->originator);
332 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_disc_ind() responding_addr=%s\n",
333 inet_ntoa(param->responding_addr.ip.v4));
334
Harald Weltec4338de2015-12-24 00:40:52 +0100335 RUA_Cause_t rua_cause = {
336 .present = RUA_Cause_PR_NOTHING,
337 /* FIXME: Convert incoming SCCP cause to RUA cause */
338 };
339
340 /* we need to notify the HNB associated with this connection via
341 * a RUA DISCONNECT */
342
343 map = context_map_by_cn(cnlink, param->conn_id);
344 if (!map) {
345 /* FIXME: Return an error / released primitive */
346 return 0;
347 }
348
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200349 return rua_tx_disc(map->hnb_ctx, map->is_ps, map->rua_ctx_id,
Harald Weltec4338de2015-12-24 00:40:52 +0100350 &rua_cause, msgb_l2(oph->msg), msgb_l2len(oph->msg));
351}
352
353/* Entry point for primitives coming up from SCCP User SAP */
Neels Hofmeyra1bf4f32016-07-07 15:36:07 +0200354static int sccp_sap_up(struct osmo_prim_hdr *oph, void *ctx)
Harald Weltec4338de2015-12-24 00:40:52 +0100355{
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200356 struct osmo_sccp_user *scu = ctx;
Neels Hofmeyrcb246312017-06-20 22:49:34 +0200357 struct hnbgw_cnlink *cnlink;
Harald Weltec4338de2015-12-24 00:40:52 +0100358 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
Harald Welteda86fe52017-11-21 08:14:37 +0100359 int rc = 0;
Harald Weltec4338de2015-12-24 00:40:52 +0100360
361 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
362
Neels Hofmeyrcb246312017-06-20 22:49:34 +0200363 if (!scu) {
364 LOGP(DMAIN, LOGL_ERROR,
365 "sccp_sap_up(): NULL osmo_sccp_user, cannot send prim (sap %u prim %u op %d)\n",
366 oph->sap, oph->primitive, oph->operation);
367 return -1;
368 }
369
370 cnlink = osmo_sccp_user_get_priv(scu);
371 if (!cnlink) {
372 LOGP(DMAIN, LOGL_ERROR,
373 "sccp_sap_up(): NULL hnbgw_cnlink, cannot send prim (sap %u prim %u op %d)\n",
374 oph->sap, oph->primitive, oph->operation);
375 return -1;
376 }
377
Harald Weltec4338de2015-12-24 00:40:52 +0100378 switch (OSMO_PRIM_HDR(oph)) {
379 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200380 rc = handle_cn_unitdata(cnlink, &prim->u.unitdata, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100381 break;
382 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200383 rc = handle_cn_conn_conf(cnlink, &prim->u.connect, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100384 break;
385 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200386 rc = handle_cn_data_ind(cnlink, &prim->u.data, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100387 break;
388 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200389 rc = handle_cn_disc_ind(cnlink, &prim->u.disconnect, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100390 break;
Harald Welteffc20932017-11-21 08:09:40 +0100391 default:
Harald Weltec4338de2015-12-24 00:40:52 +0100392 LOGP(DMAIN, LOGL_ERROR,
393 "Received unknown prim %u from SCCP USER SAP\n",
394 OSMO_PRIM_HDR(oph));
395 break;
396 }
397
398 msgb_free(oph->msg);
399
Harald Welteda86fe52017-11-21 08:14:37 +0100400 return rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100401}
402
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200403static bool addr_has_pc_and_ssn(const struct osmo_sccp_addr *addr)
Harald Weltec4338de2015-12-24 00:40:52 +0100404{
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200405 if (!(addr->presence & OSMO_SCCP_ADDR_T_SSN))
406 return false;
407 if (!(addr->presence & OSMO_SCCP_ADDR_T_PC))
408 return false;
409 return true;
410}
Harald Weltec4338de2015-12-24 00:40:52 +0100411
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200412static int resolve_addr_name(struct osmo_sccp_addr *dest, struct osmo_ss7_instance **ss7,
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100413 const char *addr_name, const char *label,
414 uint32_t default_pc)
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200415{
416 struct osmo_ss7_instance *ss7_tmp;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200417
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200418 if (!addr_name) {
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100419 osmo_sccp_make_addr_pc_ssn(dest, default_pc, OSMO_SCCP_SSN_RANAP);
420 LOGP(DMAIN, LOGL_INFO, "%s remote addr not configured, using default: %s\n", label,
421 osmo_sccp_addr_name(*ss7, dest));
422 return 0;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200423 }
424
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200425 ss7_tmp = osmo_sccp_addr_by_name(dest, addr_name);
426 if (!ss7_tmp) {
427 LOGP(DMAIN, LOGL_ERROR, "%s remote addr: no such SCCP address book entry: '%s'\n",
428 label, addr_name);
429 return -1;
430 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200431
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200432 if (*ss7 && (*ss7 != ss7_tmp)) {
433 LOGP(DMAIN, LOGL_ERROR, "IuCS and IuPS cannot be served from separate CS7 instances,"
434 " cs7 instance %d != %d\n", (*ss7)->cfg.id, ss7_tmp->cfg.id);
435 return -1;
436 }
437
438 *ss7 = ss7_tmp;
439
440 osmo_sccp_addr_set_ssn(dest, OSMO_SCCP_SSN_RANAP);
441
442 if (!addr_has_pc_and_ssn(dest)) {
443 LOGP(DMAIN, LOGL_ERROR, "Invalid/incomplete %s remote-addr: %s\n",
Alexander Couzense03d8a02017-08-15 12:19:45 +0000444 label, osmo_sccp_addr_name(*ss7, dest));
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200445 return -1;
446 }
447
448 LOGP(DRANAP, LOGL_NOTICE, "Remote %s SCCP addr: %s\n",
449 label, osmo_sccp_addr_name(*ss7, dest));
450 return 0;
451}
452
453int hnbgw_cnlink_init(struct hnb_gw *gw, const char *stp_host, uint16_t stp_port, const char *local_ip)
454{
455 struct hnbgw_cnlink *cnlink;
456 struct osmo_ss7_instance *ss7;
457 uint32_t local_pc;
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200458
459 OSMO_ASSERT(!gw->sccp.client);
460 OSMO_ASSERT(!gw->sccp.cnlink);
461
462 ss7 = NULL;
463 if (resolve_addr_name(&gw->sccp.iucs_remote_addr, &ss7,
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100464 gw->config.iucs_remote_addr_name, "IuCS", (23 << 3) + 1))
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200465 return -1;
466 if (resolve_addr_name(&gw->sccp.iups_remote_addr, &ss7,
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100467 gw->config.iups_remote_addr_name, "IuPS", (23 << 3) + 4))
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200468 return -1;
469
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100470 if (!ss7) {
471 LOGP(DRANAP, LOGL_NOTICE, "No cs7 instance configured for IuCS nor IuPS,"
472 " creating default instance\n");
473 ss7 = osmo_ss7_instance_find_or_create(gw, 0);
474 ss7->cfg.primary_pc = (23 << 3) + 5;
475 }
476
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200477 if (!osmo_ss7_pc_is_valid(ss7->cfg.primary_pc)) {
478 LOGP(DMAIN, LOGL_ERROR, "IuCS/IuPS uplink cannot be setup: CS7 instance %d has no point-code set\n",
479 ss7->cfg.id);
480 return -1;
481 }
482 local_pc = ss7->cfg.primary_pc;
483
484 osmo_sccp_make_addr_pc_ssn(&gw->sccp.local_addr, local_pc, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200485 LOGP(DRANAP, LOGL_NOTICE, "Local SCCP addr: %s\n", osmo_sccp_addr_name(ss7, &gw->sccp.local_addr));
486
487 gw->sccp.client = osmo_sccp_simple_client_on_ss7_id(gw, ss7->cfg.id, "OsmoHNBGW",
488 local_pc, OSMO_SS7_ASP_PROT_M3UA,
489 0, local_ip, stp_port, stp_host);
490 if (!gw->sccp.client) {
491 LOGP(DMAIN, LOGL_ERROR, "Failed to init SCCP Client\n");
492 return -1;
493 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200494
495 cnlink = talloc_zero(gw, struct hnbgw_cnlink);
Neels Hofmeyr2b01f3a2016-04-19 17:57:03 +0200496 cnlink->gw = gw;
Harald Weltec4338de2015-12-24 00:40:52 +0100497 INIT_LLIST_HEAD(&cnlink->map_list);
498 cnlink->T_RafC.cb = cnlink_trafc_cb;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200499 cnlink->T_RafC.data = gw;
Harald Welte552fdf12015-12-26 23:39:30 +0100500 cnlink->next_conn_id = 1000;
Harald Weltec4338de2015-12-24 00:40:52 +0100501
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200502 cnlink->sccp_user = osmo_sccp_user_bind_pc(gw->sccp.client, "OsmoHNBGW", sccp_sap_up,
503 OSMO_SCCP_SSN_RANAP, gw->sccp.local_addr.pc);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200504 if (!cnlink->sccp_user) {
505 LOGP(DMAIN, LOGL_ERROR, "Failed to init SCCP User\n");
506 return -1;
Harald Weltec4338de2015-12-24 00:40:52 +0100507 }
508
Neels Hofmeyrb5939692017-11-23 00:40:04 +0100509 LOGP(DRANAP, LOGL_NOTICE, "Remote SCCP addr: IuCS: %s\n",
510 osmo_sccp_addr_name(ss7, &gw->sccp.iucs_remote_addr));
511 LOGP(DRANAP, LOGL_NOTICE, "Remote SCCP addr: IuPS: %s\n",
512 osmo_sccp_addr_name(ss7, &gw->sccp.iups_remote_addr));
513
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200514 /* In sccp_sap_up() we expect the cnlink in the user's priv. */
515 osmo_sccp_user_set_priv(cnlink->sccp_user, cnlink);
Harald Weltec4338de2015-12-24 00:40:52 +0100516
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200517 gw->sccp.cnlink = cnlink;
Harald Weltec4338de2015-12-24 00:40:52 +0100518
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200519 return 0;
Harald Weltec4338de2015-12-24 00:40:52 +0100520}