blob: 0623561cf985ed7e376def386c5414149fc84e8e [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>
Neels Hofmeyre3644ac2018-01-15 23:25:42 +010022#include <errno.h>
Neels Hofmeyr37017f52016-04-15 22:47:21 +020023
Harald Weltec4338de2015-12-24 00:40:52 +010024#include <osmocom/core/msgb.h>
25#include <osmocom/core/utils.h>
26#include <osmocom/core/timer.h>
27
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020028#include <osmocom/sigtran/protocol/m3ua.h>
Harald Weltec4338de2015-12-24 00:40:52 +010029#include <osmocom/sigtran/sccp_sap.h>
Neels Hofmeyr3da86082016-03-30 12:36:15 +020030#include <osmocom/sigtran/sccp_helpers.h>
Harald Weltec4338de2015-12-24 00:40:52 +010031
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020032#include <osmocom/iuh/hnbgw.h>
33#include <osmocom/iuh/hnbgw_rua.h>
Neels Hofmeyr96979af2016-01-05 15:19:44 +010034#include <osmocom/ranap/ranap_ies_defs.h>
35#include <osmocom/ranap/ranap_msg_factory.h>
Neels Hofmeyrdf63de22016-08-18 13:13:55 +020036#include <osmocom/iuh/context_map.h>
Neels Hofmeyr39d7b5c2019-03-04 22:37:59 +010037#include <osmocom/hnbap/CN-DomainIndicator.h>
Harald Weltec4338de2015-12-24 00:40:52 +010038
Harald Weltec4338de2015-12-24 00:40:52 +010039/***********************************************************************
40 * Outbound RANAP RESET to CN
41 ***********************************************************************/
42
Harald Welteda86fe52017-11-21 08:14:37 +010043void hnbgw_cnlink_change_state(struct hnbgw_cnlink *cnlink, enum hnbgw_cnlink_state state);
Harald Weltec4338de2015-12-24 00:40:52 +010044
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020045static int transmit_rst(struct hnb_gw *gw, RANAP_CN_DomainIndicator_t domain,
46 struct osmo_sccp_addr *remote_addr)
Harald Weltec4338de2015-12-24 00:40:52 +010047{
48 struct msgb *msg;
Harald Weltec4338de2015-12-24 00:40:52 +010049 RANAP_Cause_t cause = {
50 .present = RANAP_Cause_PR_transmissionNetwork,
51 .choice. transmissionNetwork = RANAP_CauseTransmissionNetwork_signalling_transport_resource_failure,
52 };
53
Neels Hofmeyr39d7b5c2019-03-04 22:37:59 +010054 LOGP(DRANAP, LOGL_NOTICE, "Tx RESET to %s %s\n",
55 domain == CN_DomainIndicator_cs_domain ? "IuCS" : "IuPS",
56 osmo_sccp_inst_addr_name(gw->sccp.cnlink->sccp, remote_addr));
57
Harald Weltec4338de2015-12-24 00:40:52 +010058 msg = ranap_new_msg_reset(domain, &cause);
59
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020060 return osmo_sccp_tx_unitdata_msg(gw->sccp.cnlink->sccp_user,
61 &gw->sccp.local_addr,
62 remote_addr,
63 msg);
Harald Weltec4338de2015-12-24 00:40:52 +010064}
65
Neels Hofmeyre22027a2019-03-04 22:32:42 +010066static int transmit_reset_ack(struct hnb_gw *gw, RANAP_CN_DomainIndicator_t domain,
67 const struct osmo_sccp_addr *remote_addr)
68{
69 struct msgb *msg;
70
71 LOGP(DRANAP, LOGL_NOTICE, "Tx RESET ACK to %s %s\n",
72 domain == CN_DomainIndicator_cs_domain ? "IuCS" : "IuPS",
73 osmo_sccp_inst_addr_name(gw->sccp.cnlink->sccp, remote_addr));
74
75 msg = ranap_new_msg_reset_ack(domain, NULL);
76
77 return osmo_sccp_tx_unitdata_msg(gw->sccp.cnlink->sccp_user,
78 &gw->sccp.local_addr,
79 remote_addr,
80 msg);
81}
82
Harald Weltec4338de2015-12-24 00:40:52 +010083/* Timer callback once T_RafC expires */
84static void cnlink_trafc_cb(void *data)
85{
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020086 struct hnb_gw *gw = data;
Harald Weltec4338de2015-12-24 00:40:52 +010087
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +020088 transmit_rst(gw, RANAP_CN_DomainIndicator_cs_domain, &gw->sccp.iucs_remote_addr);
89 transmit_rst(gw, RANAP_CN_DomainIndicator_ps_domain, &gw->sccp.iups_remote_addr);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +020090 hnbgw_cnlink_change_state(gw->sccp.cnlink, CNLINK_S_EST_RST_TX_WAIT_ACK);
Harald Weltec4338de2015-12-24 00:40:52 +010091 /* The spec states that we should abandon after a configurable
92 * number of times. We decide to simply continue trying */
93}
94
95/* change the state of a CN Link */
Harald Welteda86fe52017-11-21 08:14:37 +010096void hnbgw_cnlink_change_state(struct hnbgw_cnlink *cnlink, enum hnbgw_cnlink_state state)
Harald Weltec4338de2015-12-24 00:40:52 +010097{
98 switch (state) {
99 case CNLINK_S_NULL:
100 case CNLINK_S_EST_PEND:
101 break;
102 case CNLINK_S_EST_CONF:
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200103 cnlink_trafc_cb(cnlink->gw);
Harald Weltec4338de2015-12-24 00:40:52 +0100104 break;
105 case CNLINK_S_EST_RST_TX_WAIT_ACK:
106 osmo_timer_schedule(&cnlink->T_RafC, 5, 0);
107 break;
108 case CNLINK_S_EST_ACTIVE:
109 osmo_timer_del(&cnlink->T_RafC);
110 break;
111 }
112}
113
114/***********************************************************************
115 * Incoming primitives from SCCP User SAP
116 ***********************************************************************/
117
118static int cn_ranap_rx_reset_cmd(struct hnbgw_cnlink *cnlink,
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100119 const struct osmo_scu_unitdata_param *unitdata,
Harald Weltec4338de2015-12-24 00:40:52 +0100120 RANAP_InitiatingMessage_t *imsg)
121{
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100122 CN_DomainIndicator_t domain;
Harald Weltec4338de2015-12-24 00:40:52 +0100123 RANAP_ResetIEs_t ies;
124 int rc;
125
126 rc = ranap_decode_reseties(&ies, &imsg->value);
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100127 domain = ies.cN_DomainIndicator;
Daniel Willmann11e912a2016-01-07 13:19:30 +0100128 ranap_free_reseties(&ies);
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100129
130 LOGP(DRANAP, LOGL_NOTICE, "Rx RESET from %s %s, returning ACK\n",
131 domain == CN_DomainIndicator_cs_domain ? "IuCS" : "IuPS",
132 osmo_sccp_inst_addr_name(cnlink->sccp, &unitdata->calling_addr));
133
134 /* FIXME: actually reset connections, if any */
135
136 if (transmit_reset_ack(cnlink->gw, domain, &unitdata->calling_addr))
137 LOGP(DRANAP, LOGL_ERROR, "Error: cannot send RESET ACK to %s %s\n",
138 domain == CN_DomainIndicator_cs_domain ? "IuCS" : "IuPS",
139 osmo_sccp_inst_addr_name(cnlink->sccp, &unitdata->calling_addr));
140
Harald Weltec4338de2015-12-24 00:40:52 +0100141 return rc;
142}
143
144static int cn_ranap_rx_reset_ack(struct hnbgw_cnlink *cnlink,
145 RANAP_SuccessfulOutcome_t *omsg)
146{
147 RANAP_ResetAcknowledgeIEs_t ies;
148 int rc;
149
150 rc = ranap_decode_resetacknowledgeies(&ies, &omsg->value);
151
152 hnbgw_cnlink_change_state(cnlink, CNLINK_S_EST_ACTIVE);
153
Daniel Willmann11e912a2016-01-07 13:19:30 +0100154 ranap_free_resetacknowledgeies(&ies);
Harald Weltec4338de2015-12-24 00:40:52 +0100155 return rc;
156}
157
158static int cn_ranap_rx_paging_cmd(struct hnbgw_cnlink *cnlink,
Harald Weltebc4560c2015-12-24 08:46:58 +0100159 RANAP_InitiatingMessage_t *imsg,
160 const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100161{
Neels Hofmeyr2b01f3a2016-04-19 17:57:03 +0200162 struct hnb_gw *gw = cnlink->gw;
Harald Weltebc4560c2015-12-24 08:46:58 +0100163 struct hnb_context *hnb;
Harald Weltec4338de2015-12-24 00:40:52 +0100164 RANAP_PagingIEs_t ies;
Harald Welteda86fe52017-11-21 08:14:37 +0100165 int rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100166
167 rc = ranap_decode_pagingies(&ies, &imsg->value);
Harald Welteda86fe52017-11-21 08:14:37 +0100168 if (rc < 0)
169 return rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100170
Harald Weltebc4560c2015-12-24 08:46:58 +0100171 /* FIXME: determine which HNBs to send this Paging command,
172 * rather than broadcasting to all HNBs */
173 llist_for_each_entry(hnb, &gw->hnb_list, list) {
174 rc = rua_tx_udt(hnb, data, len);
175 }
Daniel Willmann11e912a2016-01-07 13:19:30 +0100176
177 ranap_free_pagingies(&ies);
Harald Weltebc4560c2015-12-24 08:46:58 +0100178 return 0;
Harald Weltec4338de2015-12-24 00:40:52 +0100179}
180
181static int cn_ranap_rx_initiating_msg(struct hnbgw_cnlink *cnlink,
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100182 const struct osmo_scu_unitdata_param *unitdata,
Harald Weltebc4560c2015-12-24 08:46:58 +0100183 RANAP_InitiatingMessage_t *imsg,
184 const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100185{
Harald Weltec4338de2015-12-24 00:40:52 +0100186 switch (imsg->procedureCode) {
187 case RANAP_ProcedureCode_id_Reset:
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100188 return cn_ranap_rx_reset_cmd(cnlink, unitdata, imsg);
Harald Weltec4338de2015-12-24 00:40:52 +0100189 case RANAP_ProcedureCode_id_Paging:
Harald Weltebc4560c2015-12-24 08:46:58 +0100190 return cn_ranap_rx_paging_cmd(cnlink, imsg, data, len);
Harald Weltec4338de2015-12-24 00:40:52 +0100191 case RANAP_ProcedureCode_id_OverloadControl: /* Overload ind */
192 break;
193 case RANAP_ProcedureCode_id_ErrorIndication: /* Error ind */
194 break;
195 case RANAP_ProcedureCode_id_ResetResource: /* request */
196 case RANAP_ProcedureCode_id_InformationTransfer:
197 case RANAP_ProcedureCode_id_DirectInformationTransfer:
198 case RANAP_ProcedureCode_id_UplinkInformationExchange:
199 LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100200 "Procedure %ld from CN, ignoring\n", imsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100201 break;
202 default:
203 LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100204 "Procedure %ld from CN, ignoring\n", imsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100205 break;
206 }
207 return 0;
208}
209
210static int cn_ranap_rx_successful_msg(struct hnbgw_cnlink *cnlink,
211 RANAP_SuccessfulOutcome_t *omsg)
212{
Harald Weltec4338de2015-12-24 00:40:52 +0100213 switch (omsg->procedureCode) {
214 case RANAP_ProcedureCode_id_Reset: /* Reset acknowledge */
215 return cn_ranap_rx_reset_ack(cnlink, omsg);
216 case RANAP_ProcedureCode_id_ResetResource: /* response */
217 case RANAP_ProcedureCode_id_InformationTransfer:
218 case RANAP_ProcedureCode_id_DirectInformationTransfer:
219 case RANAP_ProcedureCode_id_UplinkInformationExchange:
220 LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100221 "Procedure %ld from CN, ignoring\n", omsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100222 break;
223 default:
224 LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100225 "Procedure %ld from CN, ignoring\n", omsg->procedureCode);
Harald Weltec4338de2015-12-24 00:40:52 +0100226 break;
227 }
228 return 0;
229}
230
231
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100232static int _cn_ranap_rx(struct hnbgw_cnlink *cnlink,
233 const struct osmo_scu_unitdata_param *unitdata,
234 RANAP_RANAP_PDU_t *pdu, const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100235{
236 int rc;
237
238 switch (pdu->present) {
239 case RANAP_RANAP_PDU_PR_initiatingMessage:
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100240 rc = cn_ranap_rx_initiating_msg(cnlink, unitdata, &pdu->choice.initiatingMessage,
Harald Weltebc4560c2015-12-24 08:46:58 +0100241 data, len);
Harald Weltec4338de2015-12-24 00:40:52 +0100242 break;
243 case RANAP_RANAP_PDU_PR_successfulOutcome:
244 rc = cn_ranap_rx_successful_msg(cnlink, &pdu->choice.successfulOutcome);
245 break;
246 case RANAP_RANAP_PDU_PR_unsuccessfulOutcome:
247 LOGP(DRANAP, LOGL_NOTICE, "Received unsupported RANAP "
Harald Welteda86fe52017-11-21 08:14:37 +0100248 "unsuccessful outcome procedure %ld from CN, ignoring\n",
Harald Weltec4338de2015-12-24 00:40:52 +0100249 pdu->choice.unsuccessfulOutcome.procedureCode);
Neels Hofmeyre3644ac2018-01-15 23:25:42 +0100250 rc = -ENOTSUP;
Harald Weltec4338de2015-12-24 00:40:52 +0100251 break;
252 default:
253 LOGP(DRANAP, LOGL_NOTICE, "Received suspicious RANAP "
254 "presence %u from CN, ignoring\n", pdu->present);
Neels Hofmeyre3644ac2018-01-15 23:25:42 +0100255 rc = -EINVAL;
Harald Weltec4338de2015-12-24 00:40:52 +0100256 break;
257 }
Harald Welteda86fe52017-11-21 08:14:37 +0100258
259 return rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100260}
261
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100262static int handle_cn_ranap(struct hnbgw_cnlink *cnlink, const struct osmo_scu_unitdata_param *unitdata,
263 const uint8_t *data, unsigned int len)
Harald Weltec4338de2015-12-24 00:40:52 +0100264{
265 RANAP_RANAP_PDU_t _pdu, *pdu = &_pdu;
266 asn_dec_rval_t dec_ret;
267 int rc;
268
269 memset(pdu, 0, sizeof(*pdu));
270 dec_ret = aper_decode(NULL,&asn_DEF_RANAP_RANAP_PDU, (void **) &pdu,
271 data, len, 0, 0);
272 if (dec_ret.code != RC_OK) {
273 LOGP(DRANAP, LOGL_ERROR, "Error in RANAP ASN.1 decode\n");
Neels Hofmeyra6a68e62016-11-25 13:21:02 +0100274 return -1;
Harald Weltec4338de2015-12-24 00:40:52 +0100275 }
276
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100277 rc = _cn_ranap_rx(cnlink, unitdata, pdu, data, len);
Harald Weltec4338de2015-12-24 00:40:52 +0100278
279 return rc;
280}
281
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200282static bool pc_and_ssn_match(const struct osmo_sccp_addr *a, const struct osmo_sccp_addr *b)
283{
284 return (a == b)
285 || ((a->pc == b->pc)
286 && (a->ssn == b->ssn));
287}
288
289static int classify_cn_remote_addr(const struct hnb_gw *gw,
290 const struct osmo_sccp_addr *cn_remote_addr,
291 bool *is_ps)
292{
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200293 if (pc_and_ssn_match(cn_remote_addr, &gw->sccp.iucs_remote_addr)) {
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200294 if (is_ps)
295 *is_ps = false;
296 return 0;
297 }
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200298 if (pc_and_ssn_match(cn_remote_addr, &gw->sccp.iups_remote_addr)) {
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200299 if (is_ps)
300 *is_ps = true;
301 return 0;
302 }
303 LOGP(DMAIN, LOGL_ERROR, "Unexpected remote address, matches neither CS nor PS address: %s\n",
304 osmo_sccp_addr_dump(cn_remote_addr));
305 return -1;
306}
Harald Weltec4338de2015-12-24 00:40:52 +0100307
308static int handle_cn_unitdata(struct hnbgw_cnlink *cnlink,
309 const struct osmo_scu_unitdata_param *param,
310 struct osmo_prim_hdr *oph)
311{
Harald Welte8c572fe2015-12-26 08:42:07 +0100312 if (param->called_addr.ssn != OSMO_SCCP_SSN_RANAP) {
Harald Weltec4338de2015-12-24 00:40:52 +0100313 LOGP(DMAIN, LOGL_NOTICE, "N-UNITDATA.ind for unknown SSN %u\n",
314 param->called_addr.ssn);
315 return -1;
316 }
317
Neels Hofmeyra3bcd6d2017-07-03 14:29:04 +0200318 if (classify_cn_remote_addr(cnlink->gw, &param->calling_addr, NULL) < 0)
319 return -1;
320
Neels Hofmeyre22027a2019-03-04 22:32:42 +0100321 return handle_cn_ranap(cnlink, param, msgb_l2(oph->msg), msgb_l2len(oph->msg));
Harald Weltec4338de2015-12-24 00:40:52 +0100322}
323
Neels Hofmeyr65037672016-04-19 18:00:54 +0200324static int handle_cn_conn_conf(struct hnbgw_cnlink *cnlink,
Neels Hofmeyr630483b2016-04-19 18:01:25 +0200325 const struct osmo_scu_connect_param *param,
326 struct osmo_prim_hdr *oph)
Harald Weltec4338de2015-12-24 00:40:52 +0100327{
328 /* we don't actually need to do anything, as RUA towards the HNB
329 * doesn't seem to know any confirmations to its CONNECT
330 * operation */
331
Neels Hofmeyr0ff24432016-04-04 18:33:33 +0200332 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() conn_id=%d\n",
333 param->conn_id);
334 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() called_addr=%s\n",
335 inet_ntoa(param->called_addr.ip.v4));
336 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() calling_addr=%s\n",
337 inet_ntoa(param->calling_addr.ip.v4));
338 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_conn_conf() responding_addr=%s\n",
339 inet_ntoa(param->responding_addr.ip.v4));
340
Harald Weltec4338de2015-12-24 00:40:52 +0100341 return 0;
342}
343
Neels Hofmeyr65037672016-04-19 18:00:54 +0200344static int handle_cn_data_ind(struct hnbgw_cnlink *cnlink,
Harald Weltec4338de2015-12-24 00:40:52 +0100345 const struct osmo_scu_data_param *param,
346 struct osmo_prim_hdr *oph)
347{
348 struct hnbgw_context_map *map;
349
350 /* connection-oriented data is always passed transparently
351 * towards the specific HNB, via a RUA connection identified by
352 * conn_id */
353
354 map = context_map_by_cn(cnlink, param->conn_id);
355 if (!map) {
356 /* FIXME: Return an error / released primitive */
357 return 0;
358 }
359
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200360 return rua_tx_dt(map->hnb_ctx, map->is_ps, map->rua_ctx_id,
Harald Weltec4338de2015-12-24 00:40:52 +0100361 msgb_l2(oph->msg), msgb_l2len(oph->msg));
362}
363
Neels Hofmeyr65037672016-04-19 18:00:54 +0200364static int handle_cn_disc_ind(struct hnbgw_cnlink *cnlink,
Harald Weltec4338de2015-12-24 00:40:52 +0100365 const struct osmo_scu_disconn_param *param,
366 struct osmo_prim_hdr *oph)
367{
368 struct hnbgw_context_map *map;
369
Neels Hofmeyr02be4e32016-04-04 19:29:35 +0200370 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_disc_ind() conn_id=%d originator=%d\n",
371 param->conn_id, param->originator);
372 LOGP(DMAIN, LOGL_DEBUG, "handle_cn_disc_ind() responding_addr=%s\n",
373 inet_ntoa(param->responding_addr.ip.v4));
374
Harald Weltec4338de2015-12-24 00:40:52 +0100375 RUA_Cause_t rua_cause = {
376 .present = RUA_Cause_PR_NOTHING,
377 /* FIXME: Convert incoming SCCP cause to RUA cause */
378 };
379
380 /* we need to notify the HNB associated with this connection via
381 * a RUA DISCONNECT */
382
383 map = context_map_by_cn(cnlink, param->conn_id);
384 if (!map) {
385 /* FIXME: Return an error / released primitive */
386 return 0;
387 }
388
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200389 return rua_tx_disc(map->hnb_ctx, map->is_ps, map->rua_ctx_id,
Harald Weltec4338de2015-12-24 00:40:52 +0100390 &rua_cause, msgb_l2(oph->msg), msgb_l2len(oph->msg));
391}
392
393/* Entry point for primitives coming up from SCCP User SAP */
Neels Hofmeyra1bf4f32016-07-07 15:36:07 +0200394static int sccp_sap_up(struct osmo_prim_hdr *oph, void *ctx)
Harald Weltec4338de2015-12-24 00:40:52 +0100395{
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200396 struct osmo_sccp_user *scu = ctx;
Neels Hofmeyrcb246312017-06-20 22:49:34 +0200397 struct hnbgw_cnlink *cnlink;
Harald Weltec4338de2015-12-24 00:40:52 +0100398 struct osmo_scu_prim *prim = (struct osmo_scu_prim *) oph;
Harald Welteda86fe52017-11-21 08:14:37 +0100399 int rc = 0;
Harald Weltec4338de2015-12-24 00:40:52 +0100400
401 LOGP(DMAIN, LOGL_DEBUG, "sccp_sap_up(%s)\n", osmo_scu_prim_name(oph));
402
Neels Hofmeyrcb246312017-06-20 22:49:34 +0200403 if (!scu) {
404 LOGP(DMAIN, LOGL_ERROR,
405 "sccp_sap_up(): NULL osmo_sccp_user, cannot send prim (sap %u prim %u op %d)\n",
406 oph->sap, oph->primitive, oph->operation);
407 return -1;
408 }
409
410 cnlink = osmo_sccp_user_get_priv(scu);
411 if (!cnlink) {
412 LOGP(DMAIN, LOGL_ERROR,
413 "sccp_sap_up(): NULL hnbgw_cnlink, cannot send prim (sap %u prim %u op %d)\n",
414 oph->sap, oph->primitive, oph->operation);
415 return -1;
416 }
417
Harald Weltec4338de2015-12-24 00:40:52 +0100418 switch (OSMO_PRIM_HDR(oph)) {
419 case OSMO_PRIM(OSMO_SCU_PRIM_N_UNITDATA, PRIM_OP_INDICATION):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200420 rc = handle_cn_unitdata(cnlink, &prim->u.unitdata, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100421 break;
422 case OSMO_PRIM(OSMO_SCU_PRIM_N_CONNECT, PRIM_OP_CONFIRM):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200423 rc = handle_cn_conn_conf(cnlink, &prim->u.connect, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100424 break;
425 case OSMO_PRIM(OSMO_SCU_PRIM_N_DATA, PRIM_OP_INDICATION):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200426 rc = handle_cn_data_ind(cnlink, &prim->u.data, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100427 break;
428 case OSMO_PRIM(OSMO_SCU_PRIM_N_DISCONNECT, PRIM_OP_INDICATION):
Neels Hofmeyr65037672016-04-19 18:00:54 +0200429 rc = handle_cn_disc_ind(cnlink, &prim->u.disconnect, oph);
Harald Weltec4338de2015-12-24 00:40:52 +0100430 break;
Harald Welteffc20932017-11-21 08:09:40 +0100431 default:
Harald Weltec4338de2015-12-24 00:40:52 +0100432 LOGP(DMAIN, LOGL_ERROR,
433 "Received unknown prim %u from SCCP USER SAP\n",
434 OSMO_PRIM_HDR(oph));
435 break;
436 }
437
438 msgb_free(oph->msg);
439
Harald Welteda86fe52017-11-21 08:14:37 +0100440 return rc;
Harald Weltec4338de2015-12-24 00:40:52 +0100441}
442
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200443static bool addr_has_pc_and_ssn(const struct osmo_sccp_addr *addr)
Harald Weltec4338de2015-12-24 00:40:52 +0100444{
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200445 if (!(addr->presence & OSMO_SCCP_ADDR_T_SSN))
446 return false;
447 if (!(addr->presence & OSMO_SCCP_ADDR_T_PC))
448 return false;
449 return true;
450}
Harald Weltec4338de2015-12-24 00:40:52 +0100451
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200452static int resolve_addr_name(struct osmo_sccp_addr *dest, struct osmo_ss7_instance **ss7,
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100453 const char *addr_name, const char *label,
454 uint32_t default_pc)
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200455{
456 struct osmo_ss7_instance *ss7_tmp;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200457
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200458 if (!addr_name) {
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100459 osmo_sccp_make_addr_pc_ssn(dest, default_pc, OSMO_SCCP_SSN_RANAP);
460 LOGP(DMAIN, LOGL_INFO, "%s remote addr not configured, using default: %s\n", label,
461 osmo_sccp_addr_name(*ss7, dest));
462 return 0;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200463 }
464
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200465 ss7_tmp = osmo_sccp_addr_by_name(dest, addr_name);
466 if (!ss7_tmp) {
467 LOGP(DMAIN, LOGL_ERROR, "%s remote addr: no such SCCP address book entry: '%s'\n",
468 label, addr_name);
469 return -1;
470 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200471
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200472 if (*ss7 && (*ss7 != ss7_tmp)) {
473 LOGP(DMAIN, LOGL_ERROR, "IuCS and IuPS cannot be served from separate CS7 instances,"
474 " cs7 instance %d != %d\n", (*ss7)->cfg.id, ss7_tmp->cfg.id);
475 return -1;
476 }
477
478 *ss7 = ss7_tmp;
479
480 osmo_sccp_addr_set_ssn(dest, OSMO_SCCP_SSN_RANAP);
481
482 if (!addr_has_pc_and_ssn(dest)) {
483 LOGP(DMAIN, LOGL_ERROR, "Invalid/incomplete %s remote-addr: %s\n",
Alexander Couzense03d8a02017-08-15 12:19:45 +0000484 label, osmo_sccp_addr_name(*ss7, dest));
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200485 return -1;
486 }
487
488 LOGP(DRANAP, LOGL_NOTICE, "Remote %s SCCP addr: %s\n",
489 label, osmo_sccp_addr_name(*ss7, dest));
490 return 0;
491}
492
493int hnbgw_cnlink_init(struct hnb_gw *gw, const char *stp_host, uint16_t stp_port, const char *local_ip)
494{
495 struct hnbgw_cnlink *cnlink;
496 struct osmo_ss7_instance *ss7;
497 uint32_t local_pc;
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200498
499 OSMO_ASSERT(!gw->sccp.client);
500 OSMO_ASSERT(!gw->sccp.cnlink);
501
502 ss7 = NULL;
503 if (resolve_addr_name(&gw->sccp.iucs_remote_addr, &ss7,
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100504 gw->config.iucs_remote_addr_name, "IuCS", (23 << 3) + 1))
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200505 return -1;
506 if (resolve_addr_name(&gw->sccp.iups_remote_addr, &ss7,
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100507 gw->config.iups_remote_addr_name, "IuPS", (23 << 3) + 4))
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200508 return -1;
509
Neels Hofmeyr2af648f2017-11-23 00:39:53 +0100510 if (!ss7) {
511 LOGP(DRANAP, LOGL_NOTICE, "No cs7 instance configured for IuCS nor IuPS,"
512 " creating default instance\n");
513 ss7 = osmo_ss7_instance_find_or_create(gw, 0);
514 ss7->cfg.primary_pc = (23 << 3) + 5;
515 }
516
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200517 if (!osmo_ss7_pc_is_valid(ss7->cfg.primary_pc)) {
518 LOGP(DMAIN, LOGL_ERROR, "IuCS/IuPS uplink cannot be setup: CS7 instance %d has no point-code set\n",
519 ss7->cfg.id);
520 return -1;
521 }
522 local_pc = ss7->cfg.primary_pc;
523
524 osmo_sccp_make_addr_pc_ssn(&gw->sccp.local_addr, local_pc, OSMO_SCCP_SSN_RANAP);
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200525 LOGP(DRANAP, LOGL_NOTICE, "Local SCCP addr: %s\n", osmo_sccp_addr_name(ss7, &gw->sccp.local_addr));
526
527 gw->sccp.client = osmo_sccp_simple_client_on_ss7_id(gw, ss7->cfg.id, "OsmoHNBGW",
528 local_pc, OSMO_SS7_ASP_PROT_M3UA,
529 0, local_ip, stp_port, stp_host);
530 if (!gw->sccp.client) {
531 LOGP(DMAIN, LOGL_ERROR, "Failed to init SCCP Client\n");
532 return -1;
533 }
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200534
535 cnlink = talloc_zero(gw, struct hnbgw_cnlink);
Neels Hofmeyr2b01f3a2016-04-19 17:57:03 +0200536 cnlink->gw = gw;
Harald Weltec4338de2015-12-24 00:40:52 +0100537 INIT_LLIST_HEAD(&cnlink->map_list);
538 cnlink->T_RafC.cb = cnlink_trafc_cb;
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200539 cnlink->T_RafC.data = gw;
Harald Welte552fdf12015-12-26 23:39:30 +0100540 cnlink->next_conn_id = 1000;
Harald Weltec4338de2015-12-24 00:40:52 +0100541
Neels Hofmeyrecbdc5c2017-07-31 13:13:24 +0200542 cnlink->sccp_user = osmo_sccp_user_bind_pc(gw->sccp.client, "OsmoHNBGW", sccp_sap_up,
543 OSMO_SCCP_SSN_RANAP, gw->sccp.local_addr.pc);
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200544 if (!cnlink->sccp_user) {
545 LOGP(DMAIN, LOGL_ERROR, "Failed to init SCCP User\n");
546 return -1;
Harald Weltec4338de2015-12-24 00:40:52 +0100547 }
548
Neels Hofmeyrb5939692017-11-23 00:40:04 +0100549 LOGP(DRANAP, LOGL_NOTICE, "Remote SCCP addr: IuCS: %s\n",
550 osmo_sccp_addr_name(ss7, &gw->sccp.iucs_remote_addr));
551 LOGP(DRANAP, LOGL_NOTICE, "Remote SCCP addr: IuPS: %s\n",
552 osmo_sccp_addr_name(ss7, &gw->sccp.iups_remote_addr));
553
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200554 /* In sccp_sap_up() we expect the cnlink in the user's priv. */
555 osmo_sccp_user_set_priv(cnlink->sccp_user, cnlink);
Harald Weltec4338de2015-12-24 00:40:52 +0100556
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200557 gw->sccp.cnlink = cnlink;
Harald Weltec4338de2015-12-24 00:40:52 +0100558
Neels Hofmeyr0f88c112017-07-03 16:49:43 +0200559 return 0;
Harald Weltec4338de2015-12-24 00:40:52 +0100560}