blob: 6cea2420c037db493583f8a5f36bbf9c60d3aff0 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Sylvain Munaut30a15382009-12-24 00:27:26 +010037#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000038#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000039#include <openbsc/debug.h>
40#include <openbsc/gsm_data.h>
41#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000045#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000046#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000047#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000048#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020049#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000050#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020052#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020053#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010054#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080055#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080056#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010057#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020058#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020059#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/bitvec.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020061#include <openbsc/vlr.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080062
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010063#include <osmocom/gsm/gsm48.h>
64#include <osmocom/gsm/gsm0480.h>
65#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020066#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010067#include <osmocom/core/msgb.h>
68#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010069#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010070#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000071
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020072#include <assert.h>
73
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020074void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010075void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020076
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020077static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
78
Harald Welte2483f1b2016-06-19 18:06:02 +020079static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
80 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080081static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000083
Harald Welte52b1f982008-12-23 20:25:15 +000084struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085 uint16_t mcc;
86 uint16_t mnc;
87 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000088};
89
Andreas Eversberga83d5112013-12-07 18:32:28 +010090static int apply_codec_restrictions(struct gsm_bts *bts,
91 struct gsm_mncc_bearer_cap *bcap)
92{
93 int i, j;
94
95 /* remove unsupported speech versions from list */
96 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
101 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
102 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
103 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
104 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
105 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
106 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
107 }
108 bcap->speech_ver[j] = -1;
109
110 return 0;
111}
112
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200113static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800114
Harald Welte31c00f72011-03-03 23:29:05 +0100115void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
116{
117 net->mncc_recv(net, msg);
118}
119
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800120static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
121 struct gsm_trans *trans)
122{
123 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
124
125 /* if we get passed a transaction reference, do some common
126 * work that the caller no longer has to do */
127 if (trans) {
128 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
129 msg->lchan = trans->conn->lchan;
130 }
131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800132 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200133 struct e1inp_sign_link *sign_link =
134 msg->lchan->ts->trx->rsl_link;
135
136 msg->dst = sign_link;
Neels Hofmeyr531734a2016-03-14 16:13:24 +0100137 if (gsm48_hdr_pdisc(gh) == GSM48_PDISC_CC)
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800138 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending '%s' to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr & 0xf0,
143 gsm48_cc_msg_name(gh->msg_type));
144 else
145 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200146 "Sending 0x%02x to MS.\n",
147 sign_link->trx->bts->nr,
148 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800149 gh->proto_discr, gh->msg_type);
150 }
151
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100152 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800153}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100154
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800155int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
156{
157 struct gsm48_hdr *gh;
158 struct msgb *ss_notify;
159
160 ss_notify = gsm0480_create_notifySS(message);
161 if (!ss_notify)
162 return -1;
163
164 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
165 uint8_t *data = msgb_push(ss_notify, 1);
166 data[0] = ss_notify->len - 1;
167 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
168 gh->msg_type = GSM48_MT_CC_FACILITY;
169 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
170}
171
Harald Welte2483f1b2016-06-19 18:06:02 +0200172/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100173void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
174{
175 struct gsm_trans *trans, *temp;
176
177 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
178
179 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100180 if (trans->protocol == protocol) {
181 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100182 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100183 }
Harald Welte371efe52010-12-22 23:17:50 +0100184 }
185}
186
Holger Freyther429e7762008-12-30 13:28:30 +0000187/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200188int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000189{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800190 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800191 struct msgb *msg;
192
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800193 msg = gsm48_create_loc_upd_rej(cause);
194 if (!msg) {
195 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
196 return -1;
197 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200198
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800199 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000200
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100201 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Harald Welte2483f1b2016-06-19 18:06:02 +0200202 "LAC=%u BTS=%u\n", vlr_subscr_name(conn->vsub),
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800203 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100204
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800205 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000206}
207
208/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200209static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
210 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000211{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100212 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000213 struct gsm48_hdr *gh;
214 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200215 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200216
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800217 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000218
219 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
220 gh->proto_discr = GSM48_PDISC_MM;
221 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
222
223 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200224 gsm48_generate_lai(lai, conn->network->country_code,
225 conn->network->network_code,
226 conn->bts->location_area_code);
Harald Welte52b1f982008-12-23 20:25:15 +0000227
Harald Welte2483f1b2016-06-19 18:06:02 +0200228 if (send_tmsi == GSM_RESERVED_TMSI) {
229 /* we did not allocate a TMSI to the MS, so we need to
230 * include the IMSI in order for the MS to delete any
231 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200232 uint8_t mi[10];
233 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200234 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200235 mid = msgb_put(msg, len);
236 memcpy(mid, mi, len);
237 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200238 /* Include the TMSI, which means that the MS will send a
239 * TMSI REALLOCATION COMPLETE, and we should wait for
240 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200241 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200242 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200243 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200244 /* TODO: Follow-on proceed */
245 /* TODO: CTS permission */
246 /* TODO: Equivalent PLMNs */
247 /* TODO: Emergency Number List */
248 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000249
250 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
251
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800252 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000253}
254
Harald Weltebf5e8df2009-02-03 12:59:45 +0000255/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200256static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000257{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100258 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000259 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000260
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800261 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000262
263 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
264 gh->proto_discr = GSM48_PDISC_MM;
265 gh->msg_type = GSM48_MT_MM_ID_REQ;
266 gh->data[0] = id_type;
267
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800268 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000269}
270
Harald Weltebf5e8df2009-02-03 12:59:45 +0000271/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800272static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000273{
274 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200275 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200276 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000277
Harald Welte2483f1b2016-06-19 18:06:02 +0200278 if (!conn->vsub) {
279 LOGP(DMM, LOGL_ERROR,
280 "Rx MM Identity Response: invalid: no subscriber\n");
281 return -EINVAL;
282 }
283
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200284 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200285 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
286 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000287
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200288 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100289
Harald Welte2483f1b2016-06-19 18:06:02 +0200290 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000291}
292
Harald Welte2483f1b2016-06-19 18:06:02 +0200293/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200294static const struct value_string lupd_names[] = {
295 { GSM48_LUPD_NORMAL, "NORMAL" },
296 { GSM48_LUPD_PERIODIC, "PERIODIC" },
297 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
298 { 0, NULL }
299};
Harald Welte2a139372009-02-22 21:14:55 +0000300
Harald Welte2483f1b2016-06-19 18:06:02 +0200301/* Chapter 9.2.15: Receive Location Updating Request.
302 * Keep this function non-static for direct invocation by unit tests. */
303int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000304{
Harald Welte2483f1b2016-06-19 18:06:02 +0200305 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
306 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000307 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000308 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200309 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200310 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200311 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
312
313 uint32_t tmsi;
314 char *imsi;
315 struct osmo_location_area_id old_lai, new_lai;
316 struct osmo_fsm_inst *lu_fsm;
317 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000318
Harald Welte8470bf22008-12-25 23:28:35 +0000319 lu = (struct gsm48_loc_upd_req *) gh->data;
320
321 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000322
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200323 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000324
Harald Welte2483f1b2016-06-19 18:06:02 +0200325 rc = msc_create_conn_fsm(conn, mi_string);
326 if (rc)
327 /* logging already happened in msc_create_conn_fsm() */
328 return rc;
329
330 conn->classmark.classmark1 = lu->classmark1;
331 conn->classmark.classmark1_set = true;
332
333 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
334 gsm48_mi_type_name(mi_type), mi_string,
335 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000336
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200337 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100338
Harald Welte24ff6ee2009-12-22 00:41:05 +0100339 switch (lu->type) {
340 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200341 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200342 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100343 break;
344 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200345 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200346 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100347 break;
348 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200349 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200350 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100351 break;
352 }
353
Harald Welte2483f1b2016-06-19 18:06:02 +0200354 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
355 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
356 /* TODO: 10.5.7.8 Device properties */
357 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200358
Harald Welte52b1f982008-12-23 20:25:15 +0000359 switch (mi_type) {
360 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200361 tmsi = GSM_RESERVED_TMSI;
362 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000363 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000364 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200365 tmsi = tmsi_from_string(mi_string);
366 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000367 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200368 default:
Harald Weltea0368542009-06-27 02:58:43 +0200369 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200370 tmsi = GSM_RESERVED_TMSI;
371 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000372 break;
373 }
374
Harald Welte2483f1b2016-06-19 18:06:02 +0200375 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
376 &old_lai.plmn.mnc, &old_lai.lac);
377 new_lai.plmn.mcc = conn->network->country_code;
378 new_lai.plmn.mnc = conn->network->network_code;
379 new_lai.lac = conn->bts->location_area_code;
380 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200381
Harald Welte2483f1b2016-06-19 18:06:02 +0200382 lu_fsm = vlr_loc_update(conn->conn_fsm,
383 SUBSCR_CONN_E_ACCEPTED,
384 SUBSCR_CONN_E_CN_CLOSE,
385 (void*)&conn_from_lu,
386 net->vlr, conn, vlr_lu_type, tmsi, imsi,
387 &old_lai, &new_lai,
388 conn->network->authentication_required,
389 conn->network->a5_encryption,
390 classmark_is_r99(&conn->classmark),
391 conn->via_ran == RAN_UTRAN_IU,
392 net->vlr->cfg.assign_tmsi);
393 if (!lu_fsm) {
394 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
395 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800396 }
397
Harald Welte2483f1b2016-06-19 18:06:02 +0200398 /* From vlr_loc_update() we expect an implicit dispatch of
399 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
400 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000401
Harald Welte2483f1b2016-06-19 18:06:02 +0200402 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
403 LOGP(DRR, LOGL_ERROR,
404 "%s: internal error during Location Updating attempt\n",
405 mi_string);
406 return -EIO;
407 }
408
409 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000410}
411
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100412/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200413/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100414static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800415{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100416 uint8_t ret;
417
418 ret = value / 10;
419 ret |= (value % 10) << 4;
420
421 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800422}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100423
Harald Welte4bfdfe72009-06-10 23:11:52 +0800424
Harald Weltedb253af2008-12-30 17:56:55 +0000425/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800426int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000427{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100428 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000429 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200430 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200431 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200432 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100433
Harald Welte4bfdfe72009-06-10 23:11:52 +0800434 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100435 struct tm* gmt_time;
436 struct tm* local_time;
437 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100438 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000439
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800440 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000441
442 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
443 gh->proto_discr = GSM48_PDISC_MM;
444 gh->msg_type = GSM48_MT_MM_INFO;
445
446 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200447#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000448 name_len = strlen(net->name_long);
449 /* 10.5.3.5a */
450 ptr8 = msgb_put(msg, 3);
451 ptr8[0] = GSM48_IE_NAME_LONG;
452 ptr8[1] = name_len*2 +1;
453 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
454
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200455 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000456 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000457 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000458
459 /* FIXME: Use Cell Broadcast, not UCS-2, since
460 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200461#endif
462 name_len = (strlen(net->name_long)*7)/8;
463 name_pad = (8 - strlen(net->name_long)*7)%8;
464 if (name_pad > 0)
465 name_len++;
466 /* 10.5.3.5a */
467 ptr8 = msgb_put(msg, 3);
468 ptr8[0] = GSM48_IE_NAME_LONG;
469 ptr8[1] = name_len +1;
470 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
471
472 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100473 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200474
Harald Weltedb253af2008-12-30 17:56:55 +0000475 }
476
477 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200478#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000479 name_len = strlen(net->name_short);
480 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200481 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200482 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000483 ptr8[1] = name_len*2 + 1;
484 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
485
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200486 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000487 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000488 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200489#endif
490 name_len = (strlen(net->name_short)*7)/8;
491 name_pad = (8 - strlen(net->name_short)*7)%8;
492 if (name_pad > 0)
493 name_len++;
494 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200495 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200496 ptr8[0] = GSM48_IE_NAME_SHORT;
497 ptr8[1] = name_len +1;
498 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
499
500 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100501 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200502
Harald Weltedb253af2008-12-30 17:56:55 +0000503 }
504
Harald Weltedb253af2008-12-30 17:56:55 +0000505 /* Section 10.5.3.9 */
506 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100507 gmt_time = gmtime(&cur_t);
508
Harald Weltedb253af2008-12-30 17:56:55 +0000509 ptr8 = msgb_put(msg, 8);
510 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100511 ptr8[1] = bcdify(gmt_time->tm_year % 100);
512 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
513 ptr8[3] = bcdify(gmt_time->tm_mday);
514 ptr8[4] = bcdify(gmt_time->tm_hour);
515 ptr8[5] = bcdify(gmt_time->tm_min);
516 ptr8[6] = bcdify(gmt_time->tm_sec);
517
Neels Hofmeyr73983952016-05-10 13:29:33 +0200518 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200519 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200520 if (net->tz.hr < 0) {
521 tzunits = ((net->tz.hr/-1)*4);
522 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100523 ptr8[7] = bcdify(tzunits);
524 /* Set negative time */
525 ptr8[7] |= 0x08;
526 }
527 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200528 tzunits = net->tz.hr*4;
529 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100530 ptr8[7] = bcdify(tzunits);
531 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100532 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200533 if (net->tz.dst >= 0 && net->tz.dst <= 2)
534 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100535 }
536 else {
537 /* Need to get GSM offset and convert into 15 min units */
538 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
539 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200540#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100541 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200542#else
543#warning find a portable way to obtain the timezone offset
544 tzunits = 0;
545#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100546 if (tzunits < 0) {
547 tzunits = tzunits/-1;
548 ptr8[7] = bcdify(tzunits);
549 /* Flip it to negative */
550 ptr8[7] |= 0x08;
551 }
552 else
553 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100554
555 /* Does not support DST +2 */
556 if (local_time->tm_isdst)
557 dst = 1;
558 }
559
560 if (dst) {
561 ptr8 = msgb_put(msg, 3);
562 ptr8[0] = GSM48_IE_NET_DST;
563 ptr8[1] = 1;
564 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100565 }
Harald Weltedb253af2008-12-30 17:56:55 +0000566
Daniel Willmanneea93372009-08-13 03:42:07 +0200567 DEBUGP(DMM, "-> MM INFO\n");
568
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800569 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000570}
571
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100572/*! Send an Authentication Request to MS on the given subscriber connection
573 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
574 * \param[in] conn Subscriber connection to send on.
575 * \param[in] rand Random challenge token to send, must be 16 bytes long.
576 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
577 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
578 * \param[in] key_seq auth tuple's sequence number.
579 */
580int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
581 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200582{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100583 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200584 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200585 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200586
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100587 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100588 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100589 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200590
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800591 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200592 gh->proto_discr = GSM48_PDISC_MM;
593 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
594
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100595 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200596
Harald Welte7984d5c2009-08-12 22:56:50 +0200597 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100598 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200599 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200600 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200601
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100602
603 /* 16 bytes AUTN */
604 if (autn)
605 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
606
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800607 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200608}
609
610/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800611int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200612{
613 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800614 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200615}
616
Harald Welte2483f1b2016-06-19 18:06:02 +0200617static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
618static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
619
620static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100621{
Harald Welte2483f1b2016-06-19 18:06:02 +0200622 uint8_t mi_type;
623 char mi_string[GSM48_MI_SIZE];
624 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100625
Harald Welte2483f1b2016-06-19 18:06:02 +0200626 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
627 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100628
Harald Welte2483f1b2016-06-19 18:06:02 +0200629 switch (mi_type) {
630 case GSM_MI_TYPE_IMSI:
631 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
632 goto accept_reuse;
633 break;
634 case GSM_MI_TYPE_TMSI:
635 tmsi = osmo_load32be(mi_lv+2);
636 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
637 goto accept_reuse;
638 break;
639 case GSM_MI_TYPE_IMEI:
640 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
641 goto accept_reuse;
642 break;
643 default:
644 break;
645 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100646
Harald Welte2483f1b2016-06-19 18:06:02 +0200647 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
648 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
649 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
650 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100651
Harald Welte2483f1b2016-06-19 18:06:02 +0200652accept_reuse:
653 DEBUGP(DMM, "%s: re-using already accepted connection\n",
654 vlr_subscr_name(conn->vsub));
655 conn->received_cm_service_request = true;
656 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100657}
658
Harald Welte4ed0e922009-01-10 03:17:30 +0000659/*
660 * Handle CM Service Requests
661 * a) Verify that the packet is long enough to contain the information
662 * we require otherwsie reject with INCORRECT_MESSAGE
663 * b) Try to parse the TMSI. If we do not have one reject
664 * c) Check that we know the subscriber with the TMSI otherwise reject
665 * with a HLR cause
666 * d) Set the subscriber on the gsm_lchan and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200667 *
668 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000669 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200670int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000671{
Harald Welte2483f1b2016-06-19 18:06:02 +0200672 static const enum subscr_conn_from conn_from_cm_service_req =
673 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
674 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200675 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200676 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000677
Harald Welteba4cf162009-01-10 01:49:35 +0000678 struct gsm48_hdr *gh = msgb_l3(msg);
679 struct gsm48_service_request *req =
680 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800681 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200682 uint8_t classmark2_len = gh->data[1];
683 uint8_t *classmark2 = gh->data+2;
684 uint8_t mi_len = *(classmark2 + classmark2_len);
685 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200686 struct osmo_location_area_id lai;
687 int rc;
688
689 lai.plmn.mcc = conn->network->country_code;
690 lai.plmn.mnc = conn->network->network_code;
691 lai.lac = conn->bts->location_area_code;
Harald Welteba4cf162009-01-10 01:49:35 +0000692
Harald Weltec9e02182009-05-01 19:07:53 +0000693 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000694 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000695 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800696 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000697 GSM48_REJECT_INCORRECT_MESSAGE);
698 }
699
700 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000701 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800702 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000703 GSM48_REJECT_INCORRECT_MESSAGE);
704 }
705
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200706 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000707 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200708
709 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200710 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
711 req->cm_service_type, gsm48_mi_type_name(mi_type),
712 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200713 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200714 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
715 req->cm_service_type, gsm48_mi_type_name(mi_type),
716 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200717 } else {
718 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800719 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000720 GSM48_REJECT_INCORRECT_MESSAGE);
721 }
722
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200723 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200724 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
725 conn->classmark.classmark2_len = classmark2_len;
726
727 if (conn->conn_fsm) {
728 if (msc_subscr_conn_is_accepted(conn))
729 return cm_serv_reuse_conn(conn, mi-1);
730 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
731 vlr_subscr_name(conn->vsub));
732 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
733 return -EINVAL;
734 }
735
736 rc = msc_create_conn_fsm(conn, mi_string);
737 if (rc) {
738 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
739 /* logging already happened in msc_create_conn_fsm() */
740 return rc;
741 }
Harald Welte7659de12009-12-13 12:39:18 +0100742
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200743 if (is_siemens_bts(conn->bts))
Harald Welte3ac7f102009-08-10 10:12:45 +0200744 send_siemens_mrpci(msg->lchan, classmark2-1);
745
Harald Welte2483f1b2016-06-19 18:06:02 +0200746 vlr_proc_acc_req(conn->conn_fsm,
747 SUBSCR_CONN_E_ACCEPTED,
748 SUBSCR_CONN_E_CN_CLOSE,
749 (void*)&conn_from_cm_service_req,
750 net->vlr, conn,
751 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
752 conn->network->authentication_required,
753 conn->network->a5_encryption,
754 classmark_is_r99(&conn->classmark),
755 conn->via_ran == RAN_UTRAN_IU);
Holger Freythereb443982009-06-04 13:58:42 +0000756
Harald Welte2483f1b2016-06-19 18:06:02 +0200757 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000758}
759
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200760static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000761{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200762 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000763 struct gsm48_hdr *gh = msgb_l3(msg);
764 struct gsm48_imsi_detach_ind *idi =
765 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200766 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200767 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200768 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000769
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200770 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200771 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
772 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000773
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200774 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100775
Harald Welte2a139372009-02-22 21:14:55 +0000776 switch (mi_type) {
777 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200778 vsub = vlr_subscr_find_by_tmsi(network->vlr,
779 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000780 break;
781 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200782 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000783 break;
784 case GSM_MI_TYPE_IMEI:
785 case GSM_MI_TYPE_IMEISV:
786 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200787 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
788 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000789 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200790 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200791 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
792 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000793 break;
794 }
795
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200796
Harald Welte2483f1b2016-06-19 18:06:02 +0200797 /* TODO? We used to remember the subscriber's classmark1 here and
798 * stored it in the old sqlite db, but now we store it in a conn that
799 * will be discarded anyway: */
800 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200801
Harald Welte2483f1b2016-06-19 18:06:02 +0200802 if (!vsub) {
803 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
804 gsm48_mi_type_name(mi_type), mi_string);
805 } else {
806 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
807 vlr_subscr_rx_imsi_detach(vsub);
808 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
809 vlr_subscr_put(vsub);
810 }
Harald Welte2a139372009-02-22 21:14:55 +0000811
Harald Welte2483f1b2016-06-19 18:06:02 +0200812 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000813 return 0;
814}
815
Harald Welted2a7f5a2009-06-05 20:08:20 +0000816static int gsm48_rx_mm_status(struct msgb *msg)
817{
818 struct gsm48_hdr *gh = msgb_l3(msg);
819
820 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
821
822 return 0;
823}
824
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100825static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
826 struct gsm_subscriber_connection *conn,
827 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100828{
829 struct gsm48_hdr *gh = msgb_l3(msg);
830 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100831
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100832 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
833 LOGP(DMM, LOGL_ERROR,
834 "%s: MM AUTHENTICATION RESPONSE:"
835 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200836 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100837 return -EINVAL;
838 }
839
840 *res_len = sizeof(ar->sres);
841 memcpy(res, ar->sres, sizeof(ar->sres));
842 return 0;
843}
844
845static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
846 struct gsm_subscriber_connection *conn,
847 struct msgb *msg)
848{
849 struct gsm48_hdr *gh;
850 uint8_t *data;
851 uint8_t iei;
852 uint8_t ie_len;
853 unsigned int data_len;
854
855 /* First parse the GSM part */
856 if (parse_gsm_auth_resp(res, res_len, conn, msg))
857 return -EINVAL;
858 OSMO_ASSERT(*res_len == 4);
859
860 /* Then add the extended res part */
861 gh = msgb_l3(msg);
862 data = gh->data + sizeof(struct gsm48_auth_resp);
863 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
864
865 if (data_len < 3) {
866 LOGP(DMM, LOGL_ERROR,
867 "%s: MM AUTHENTICATION RESPONSE:"
868 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200869 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100870 return -EINVAL;
871 }
872
873 iei = data[0];
874 ie_len = data[1];
875 if (iei != GSM48_IE_AUTH_RES_EXT) {
876 LOGP(DMM, LOGL_ERROR,
877 "%s: MM R99 AUTHENTICATION RESPONSE:"
878 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200879 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100880 GSM48_IE_AUTH_RES_EXT, iei);
881 return -EINVAL;
882 }
883
884 if (ie_len > 12) {
885 LOGP(DMM, LOGL_ERROR,
886 "%s: MM R99 AUTHENTICATION RESPONSE:"
887 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200888 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100889 return -EINVAL;
890 }
891
892 *res_len += ie_len;
893 memcpy(res + 4, &data[2], ie_len);
894 return 0;
895}
896
897/* Chapter 9.2.3: Authentication Response */
898static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
899{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100900 uint8_t res[16];
901 uint8_t res_len;
902 int rc;
903 bool is_r99;
904
Harald Welte2483f1b2016-06-19 18:06:02 +0200905 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100906 LOGP(DMM, LOGL_ERROR,
907 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200908 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100909 return -EINVAL;
910 }
911
912 if (msgb_l3len(msg) >
913 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
914 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
915 is_r99 = true;
916 } else {
917 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
918 is_r99 = false;
919 }
920
921 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200922 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100923 return -EINVAL;
924 }
925
926 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200927 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100928 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
929 osmo_hexdump_nospc(res, res_len));
930
Harald Welte2483f1b2016-06-19 18:06:02 +0200931 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
932 conn->via_ran == RAN_UTRAN_IU,
933 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100934}
935
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100936static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
937{
938 struct gsm48_hdr *gh = msgb_l3(msg);
939 uint8_t cause;
940 uint8_t auts_tag;
941 uint8_t auts_len;
942 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100943
Harald Welte2483f1b2016-06-19 18:06:02 +0200944 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100945 LOGP(DMM, LOGL_ERROR,
946 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200947 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100948 return -EINVAL;
949 }
950
951 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
952 LOGP(DMM, LOGL_ERROR,
953 "%s: MM R99 AUTHENTICATION FAILURE:"
954 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200955 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
956 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100957 return -EINVAL;
958 }
959
960 cause = gh->data[0];
961
962 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
963 LOGP(DMM, LOGL_INFO,
964 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200965 vlr_subscr_name(conn->vsub), cause);
966 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
967 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100968 }
969
970 /* This is a Synch Failure procedure, which should pass an AUTS to
971 * resynchronize the sequence nr with the HLR. Expecting exactly one
972 * TLV with 14 bytes of AUTS. */
973
974 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
975 LOGP(DMM, LOGL_INFO,
976 "%s: MM R99 AUTHENTICATION FAILURE:"
977 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200978 vlr_subscr_name(conn->vsub));
979 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980 return -EINVAL;
981 }
982
983 auts_tag = gh->data[1];
984 auts_len = gh->data[2];
985 auts = &gh->data[3];
986
987 if (auts_tag != GSM48_IE_AUTS
988 || auts_len != 14) {
989 LOGP(DMM, LOGL_INFO,
990 "%s: MM R99 AUTHENTICATION FAILURE:"
991 " invalid Synch Failure:"
992 " expected AUTS IE 0x%02x of 14 bytes,"
993 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200994 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100995 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200996 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100997 return -EINVAL;
998 }
999
1000 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1001 LOGP(DMM, LOGL_INFO,
1002 "%s: MM R99 AUTHENTICATION FAILURE:"
1003 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001004 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1005 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001006 return -EINVAL;
1007 }
1008
1009 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1010 * large enough. */
1011
1012 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001013 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001014
Harald Welte2483f1b2016-06-19 18:06:02 +02001015 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1016}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001017
Harald Welte2483f1b2016-06-19 18:06:02 +02001018static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1019{
1020 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1021 vlr_subscr_name(conn->vsub));
1022 if (!conn->vsub) {
1023 LOGP(DMM, LOGL_ERROR,
1024 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1025 return -EINVAL;
1026 }
1027 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001028}
1029
Harald Weltebf5e8df2009-02-03 12:59:45 +00001030/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001031static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001032{
1033 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001034 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001035
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001036 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001037 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001038 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001039 break;
1040 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001041 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001042 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001043 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001044 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001045 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001046 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001047 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001048 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001049 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001050 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001051 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001052 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001053 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001054 break;
1055 case GSM48_MT_MM_CM_REEST_REQ:
1056 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1057 break;
1058 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001059 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001060 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001061 case GSM48_MT_MM_AUTH_FAIL:
1062 rc = gsm48_rx_mm_auth_fail(conn, msg);
1063 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001064 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001065 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001066 gh->msg_type);
1067 break;
1068 }
1069
1070 return rc;
1071}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001072
Harald Welte2483f1b2016-06-19 18:06:02 +02001073static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1074{
1075 /* Check the size for the classmark */
1076 if (tot_len < 1 + *classmark2_lv)
1077 return NULL;
1078
1079 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1080 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1081 return NULL;
1082
1083 return mi_lv;
1084}
1085
Harald Welte2d35ae62009-02-06 12:02:13 +00001086/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001087static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001088{
Harald Welte2483f1b2016-06-19 18:06:02 +02001089 static const enum subscr_conn_from conn_from_paging_resp =
1090 SUBSCR_CONN_FROM_PAGING_RESP;
1091 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001092 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001093 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001094 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001095 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001096 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001097 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001098 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001099 struct osmo_location_area_id lai;
1100
1101 lai.plmn.mcc = conn->network->country_code;
1102 lai.plmn.mnc = conn->network->network_code;
1103 lai.lac = conn->bts->location_area_code; /* (will be replaced by conn->lac soon) */
Harald Welte2d35ae62009-02-06 12:02:13 +00001104
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001105 resp = (struct gsm48_pag_resp *) &gh->data[0];
1106 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1107 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001108 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1109 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001110
Harald Welte2483f1b2016-06-19 18:06:02 +02001111 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1112 if (!mi_lv) {
1113 /* FIXME */
1114 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001115 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001116
Harald Welte2483f1b2016-06-19 18:06:02 +02001117 rc = msc_create_conn_fsm(conn, mi_string);
1118 if (rc)
1119 /* logging already happened in msc_create_conn_fsm() */
1120 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001121
Harald Welte2483f1b2016-06-19 18:06:02 +02001122 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1123 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001124
Harald Welte2483f1b2016-06-19 18:06:02 +02001125 vlr_proc_acc_req(conn->conn_fsm,
1126 SUBSCR_CONN_E_ACCEPTED,
1127 SUBSCR_CONN_E_CN_CLOSE,
1128 (void*)&conn_from_paging_resp,
1129 net->vlr, conn,
1130 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
1131 conn->network->authentication_required,
1132 conn->network->a5_encryption,
1133 classmark_is_r99(&conn->classmark),
1134 conn->via_ran == RAN_UTRAN_IU);
Holger Freyther053e09d2009-02-14 22:51:06 +00001135
Harald Welte2d35ae62009-02-06 12:02:13 +00001136 return rc;
1137}
1138
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001139static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001140{
1141 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001142 uint8_t apdu_id_flags;
1143 uint8_t apdu_len;
1144 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145
1146 apdu_id_flags = gh->data[0];
1147 apdu_len = gh->data[1];
1148 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001149
Max8dc8f232017-02-09 19:13:02 +01001150 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001151 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001152
Harald Welte2483f1b2016-06-19 18:06:02 +02001153 /* we're not using the app info blob anywhere, so ignore. */
1154#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001155 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001156#else
1157 return 0;
1158#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001159}
1160
Harald Weltebf5e8df2009-02-03 12:59:45 +00001161/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001162static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001163{
1164 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001165 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001166
1167 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001168 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001169 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001170 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001171 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001172 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001173 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001174 default:
Philippd37981e2016-10-10 15:11:41 +02001175 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1176 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001177 break;
1178 }
1179
Harald Welte2d35ae62009-02-06 12:02:13 +00001180 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001181}
1182
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001183int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1184 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001185{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001186 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001187 struct gsm48_hdr *gh;
1188
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001189 msg->lchan = conn->lchan;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001190
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001191 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1192 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001193
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001194 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1195 gh->proto_discr = GSM48_PDISC_RR;
1196 gh->msg_type = GSM48_MT_RR_APP_INFO;
1197 gh->data[0] = apdu_id;
1198 gh->data[1] = apdu_len;
1199 memcpy(gh->data+2, apdu, apdu_len);
1200
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001201 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001202}
1203
Alexander Couzens92f552f2016-08-23 07:32:27 +02001204/* FIXME: this count_statistics is a state machine behaviour. we should convert
1205 * the complete call control into a state machine. Afterwards we can move this
1206 * code into state transitions.
1207 */
1208static void count_statistics(struct gsm_trans *trans, int new_state)
1209{
1210 int old_state = trans->cc.state;
1211 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1212
1213 if (old_state == new_state)
1214 return;
1215
1216 /* state incoming */
1217 switch (new_state) {
1218 case GSM_CSTATE_ACTIVE:
1219 osmo_counter_inc(trans->net->active_calls);
1220 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1221 break;
1222 }
1223
1224 /* state outgoing */
1225 switch (old_state) {
1226 case GSM_CSTATE_ACTIVE:
1227 osmo_counter_dec(trans->net->active_calls);
1228 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1229 new_state == GSM_CSTATE_DISCONNECT_IND)
1230 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1231 else
1232 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1233 break;
1234 }
1235}
1236
Harald Welte4bc90a12008-12-27 16:32:52 +00001237/* Call Control */
1238
Harald Welte7584aea2009-02-11 11:44:12 +00001239/* The entire call control code is written in accordance with Figure 7.10c
1240 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1241 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1242 * it for voice */
1243
Harald Welte4bfdfe72009-06-10 23:11:52 +08001244static void new_cc_state(struct gsm_trans *trans, int state)
1245{
1246 if (state > 31 || state < 0)
1247 return;
1248
1249 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001250 gsm48_cc_state_name(trans->cc.state),
1251 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001252
Alexander Couzens92f552f2016-08-23 07:32:27 +02001253 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001254 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001255}
1256
1257static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001258{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001259 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001260 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001261 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001262
Harald Welte4bc90a12008-12-27 16:32:52 +00001263 gh->msg_type = GSM48_MT_CC_STATUS;
1264
1265 cause = msgb_put(msg, 3);
1266 cause[0] = 2;
1267 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1268 cause[2] = 0x80 | 30; /* response to status inquiry */
1269
1270 call_state = msgb_put(msg, 1);
1271 call_state[0] = 0xc0 | 0x00;
1272
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001273 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001274}
1275
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001276static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001277 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001278{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001279 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001280 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1281
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001282 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001283
Harald Welte6f4b7532008-12-29 00:39:37 +00001284 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001285 gh->msg_type = msg_type;
1286
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001287 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001288}
1289
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1291{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001292 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001293 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001294 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001295 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296 }
1297}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001298
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1300 int msg_type, struct gsm_mncc *mncc)
1301{
1302 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001303 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001304
1305 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001306 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001307 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001309 trans->conn->lchan->ts->trx->bts->nr,
1310 trans->conn->lchan->ts->trx->nr,
1311 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001312 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001313 get_mncc_name(msg_type));
1314 else
1315 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1316 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001317 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 get_mncc_name(msg_type));
1319 else
1320 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1321 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1322
1323 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001324
Harald Welte966636f2009-06-26 19:39:35 +02001325 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326 if (!msg)
1327 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001328
1329 data = msgb_put(msg, sizeof(struct gsm_mncc));
1330 memcpy(data, mncc, sizeof(struct gsm_mncc));
1331
Harald Welte54209c22010-12-22 23:43:29 +01001332 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333
1334 return 0;
1335}
1336
1337int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001338 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339{
1340 struct gsm_mncc rel;
1341
Harald Welte92f70c52009-06-12 01:54:08 +08001342 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001344 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001345 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1346 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1348}
1349
Harald Weltedcaf5652009-07-23 18:56:43 +02001350/* Call Control Specific transaction release.
1351 * gets called by trans_free, DO NOT CALL YOURSELF! */
1352void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354 gsm48_stop_cc_timer(trans);
1355
1356 /* send release to L4, if callref still exists */
1357 if (trans->callref) {
1358 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001359 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001360 GSM48_CAUSE_LOC_PRN_S_LU,
1361 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001363 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001365 if (trans->conn)
1366 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367}
1368
1369static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001370
Harald Welte09e38af2009-02-16 22:52:23 +00001371/* call-back from paging the B-end of the connection */
1372static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001373 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001374{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001375 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001376 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001377
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001378 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001379
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001381 switch (event) {
1382 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001383 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1384 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001385 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001386 /* Assign lchan */
1387 transt->conn = conn;
1388 /* send SETUP request to called party */
1389 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1390 break;
1391 case GSM_PAGING_EXPIRED:
1392 case GSM_PAGING_BUSY:
1393 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001394 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001395 /* Temporarily out of order */
1396 mncc_release_ind(transt->net, transt,
1397 transt->callref,
1398 GSM48_CAUSE_LOC_PRN_S_LU,
1399 GSM48_CC_CAUSE_DEST_OOO);
1400 transt->callref = 0;
1401 transt->paging_request = NULL;
1402 trans_free(transt);
1403 break;
1404 default:
1405 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1406 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001407 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001408
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001409 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001410 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001411}
Harald Welte7584aea2009-02-11 11:44:12 +00001412
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001413static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001414
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001415/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001416static int switch_for_handover(struct gsm_lchan *old_lchan,
1417 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001418{
1419 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001420
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001421 /* Ask the new socket to send to the already known port. */
1422 if (new_lchan->conn->mncc_rtp_bridge) {
1423 LOGP(DHO, LOGL_DEBUG, "Forwarding RTP\n");
1424 rsl_ipacc_mdcx(new_lchan,
1425 old_lchan->abis_ip.connect_ip,
1426 old_lchan->abis_ip.connect_port, 0);
1427 return 0;
1428 }
1429
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001430 if (ipacc_rtp_direct) {
1431 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1432 return 0;
1433 }
1434
1435 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001436 new_rs = new_lchan->abis_ip.rtp_socket;
1437 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001438
1439 if (!new_rs) {
1440 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1441 return -EIO;
1442 }
1443
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001444 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001445
1446 if (!old_rs) {
1447 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1448 return -EIO;
1449 }
1450
1451 /* copy rx_action and reference to other sock */
1452 new_rs->rx_action = old_rs->rx_action;
1453 new_rs->tx_action = old_rs->tx_action;
1454 new_rs->transmit = old_rs->transmit;
1455
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001456 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001457 case RTP_PROXY:
1458 other_rs = old_rs->proxy.other_sock;
1459 rtp_socket_proxy(new_rs, other_rs);
1460 /* delete reference to other end socket to prevent
1461 * rtp_socket_free() from removing the inverse reference */
1462 old_rs->proxy.other_sock = NULL;
1463 break;
1464 case RTP_RECV_UPSTREAM:
1465 new_rs->receive = old_rs->receive;
1466 break;
1467 case RTP_NONE:
1468 break;
1469 }
1470
1471 return 0;
1472}
1473
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001474static void maybe_switch_for_handover(struct gsm_lchan *lchan)
1475{
1476 struct gsm_lchan *old_lchan;
1477 old_lchan = bsc_handover_pending(lchan);
1478 if (old_lchan)
1479 switch_for_handover(old_lchan, lchan);
1480}
1481
Harald Welte805f6442009-07-28 18:25:29 +02001482/* some other part of the code sends us a signal */
1483static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1484 void *handler_data, void *signal_data)
1485{
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001486 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001487 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001488 struct gsm_network *net;
1489 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001490
1491 if (subsys != SS_ABISIP)
1492 return 0;
1493
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001494 /* RTP bridge handling */
1495 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1496 return tch_rtp_signal(lchan, signal);
1497
Harald Welte805f6442009-07-28 18:25:29 +02001498 /* in case we use direct BTS-to-BTS RTP */
1499 if (ipacc_rtp_direct)
1500 return 0;
1501
Harald Welte805f6442009-07-28 18:25:29 +02001502 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001503 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001504 /* in case we don't use direct BTS-to-BTS RTP */
1505 /* the BTS has successfully bound a TCH to a local ip/port,
1506 * which means we can connect our UDP socket to it */
1507 if (lchan->abis_ip.rtp_socket) {
1508 rtp_socket_free(lchan->abis_ip.rtp_socket);
1509 lchan->abis_ip.rtp_socket = NULL;
1510 }
1511
1512 lchan->abis_ip.rtp_socket = rtp_socket_create();
1513 if (!lchan->abis_ip.rtp_socket)
1514 return -EIO;
1515
1516 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1517 lchan->abis_ip.bound_ip,
1518 lchan->abis_ip.bound_port);
1519 if (rc < 0)
1520 return -EIO;
1521
Harald Welteda7ab742009-12-19 22:23:05 +01001522 /* check if any transactions on this lchan still have
1523 * a tch_recv_mncc request pending */
1524 net = lchan->ts->trx->bts->network;
1525 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001526 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001527 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1528 tch_recv_mncc(net, trans->callref, 1);
1529 }
1530 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001531
1532 /*
1533 * TODO: this appears to be too early? Why not until after
1534 * the handover detect or the handover complete?
1535 *
1536 * Do we have a handover pending for this new lchan? In that
1537 * case re-route the audio from the old channel to the new one.
1538 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02001539 maybe_switch_for_handover(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001540 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001541 case S_ABISIP_DLCX_IND:
1542 /* the BTS tells us a RTP stream has been disconnected */
1543 if (lchan->abis_ip.rtp_socket) {
1544 rtp_socket_free(lchan->abis_ip.rtp_socket);
1545 lchan->abis_ip.rtp_socket = NULL;
1546 }
1547
1548 break;
Harald Welte805f6442009-07-28 18:25:29 +02001549 }
1550
1551 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001552}
1553
Harald Welte49f48b82009-02-17 15:29:33 +00001554/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001555static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001556{
Harald Welte11fa29c2009-02-19 17:24:39 +00001557 struct gsm_bts *bts = lchan->ts->trx->bts;
1558 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Max8db12e42016-04-18 23:11:18 +02001559 enum gsm_chan_t lt = lchan->type, rt = remote_lchan->type;
Max3ffce192016-04-25 15:22:00 +02001560 enum gsm48_chan_mode lm = lchan->tch_mode, rm = remote_lchan->tch_mode;
Harald Welte805f6442009-07-28 18:25:29 +02001561 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001562
Max8db12e42016-04-18 23:11:18 +02001563 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u,%s) and "
1564 "(bts=%u,trx=%u,ts=%u,%s)\n",
1565 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1566 get_value_string(gsm_chan_t_names, lt),
1567 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr,
1568 get_value_string(gsm_chan_t_names, rt));
Harald Welte11fa29c2009-02-19 17:24:39 +00001569
1570 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001571 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001572 return -EINVAL;
1573 }
Harald Welteda7ab742009-12-19 22:23:05 +01001574
Max3ffce192016-04-25 15:22:00 +02001575 if (lt != rt) {
1576 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1577 " channel types: local = %s, remote = %s\n",
1578 get_value_string(gsm_chan_t_names, lt),
1579 get_value_string(gsm_chan_t_names, rt));
1580 return -EBADSLT;
1581 }
1582
1583 if (lm != rm) {
1584 LOGP(DCC, LOGL_ERROR, "Cannot patch through call with different"
1585 " channel modes: local = %s, remote = %s\n",
1586 get_value_string(gsm48_chan_mode_names, lm),
1587 get_value_string(gsm48_chan_mode_names, rm));
1588 return -EMEDIUMTYPE;
1589 }
1590
Harald Welteda7ab742009-12-19 22:23:05 +01001591 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001592 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001593 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01001594 case GSM_BTS_TYPE_OSMOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001595 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001596 if (!lchan->abis_ip.rtp_socket) {
1597 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1598 "lchan\n");
1599 return -EIO;
1600 }
1601 if (!remote_lchan->abis_ip.rtp_socket) {
1602 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1603 "remote_lchan\n");
1604 return -EIO;
1605 }
1606
Harald Welte805f6442009-07-28 18:25:29 +02001607 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001608 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001609 if (rc < 0)
1610 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001611 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001612 if (rc < 0)
1613 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001614 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301615 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1616 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001617 } else {
1618 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301619 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1620 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301621 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001622 if (rc < 0)
1623 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301624 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1625 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301626 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001627 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001628 break;
1629 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001630 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001631 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001632 trau_mux_map_lchan(lchan, remote_lchan);
1633 break;
1634 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001635 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001636 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001637 }
Harald Welte49f48b82009-02-17 15:29:33 +00001638
1639 return 0;
1640}
1641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001643static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001644{
Harald Welte53d51f52015-12-03 14:59:04 +01001645 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1646 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001647
Harald Welte4bfdfe72009-06-10 23:11:52 +08001648 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001649 return -EIO;
1650
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001651 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 return -EIO;
1653
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001654 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001655 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001656
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001658 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001659}
1660
Harald Welteda7ab742009-12-19 22:23:05 +01001661/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001662static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663{
1664 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001665 struct gsm_lchan *lchan;
1666 struct gsm_bts *bts;
1667 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001668
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001670 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 if (!trans)
1672 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001673 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001675
Harald Welte2483f1b2016-06-19 18:06:02 +02001676 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001677 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001678 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001680 /* store receive state */
1681 trans->tch_recv = enable;
1682
Harald Welteda7ab742009-12-19 22:23:05 +01001683 switch (bts->type) {
1684 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01001685 case GSM_BTS_TYPE_OSMOBTS:
Harald Welteda7ab742009-12-19 22:23:05 +01001686 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001687 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001688 return -EINVAL;
1689 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001690 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1691 * will not be connected to our RTP proxy and the socket will
1692 * not be assigned to the application interface. This method
1693 * will be called again, once the audio socket is created and
1694 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001695 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001696 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1697 return 0;
1698 }
1699 if (enable) {
1700 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001701 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001702 if (rc < 0)
1703 return rc;
1704 /* assign socket to application interface */
1705 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1706 net, callref);
1707 } else
1708 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1709 net, 0);
1710 break;
1711 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001712 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001713 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001714 /* In case we don't have a TCH with correct mode, the TRAU muxer
1715 * will not be asigned to the application interface. This is
1716 * performed by switch_trau_mux() after successful handover or
1717 * assignment. */
1718 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1719 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1720 return 0;
1721 }
Harald Welteda7ab742009-12-19 22:23:05 +01001722 if (enable)
1723 return trau_recv_lchan(lchan, callref);
1724 return trau_mux_unmap(NULL, callref);
1725 break;
1726 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001727 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001728 return -EINVAL;
1729 }
1730
1731 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001732}
1733
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1735{
1736 DEBUGP(DCC, "-> STATUS ENQ\n");
1737 return gsm48_cc_tx_status(trans, msg);
1738}
1739
1740static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1741static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1742
1743static void gsm48_cc_timeout(void *arg)
1744{
1745 struct gsm_trans *trans = arg;
1746 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001747 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1748 int mo_location = GSM48_CAUSE_LOC_USER;
1749 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1750 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 struct gsm_mncc mo_rel, l4_rel;
1752
1753 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1754 mo_rel.callref = trans->callref;
1755 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1756 l4_rel.callref = trans->callref;
1757
Harald Weltedcaf5652009-07-23 18:56:43 +02001758 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 case 0x303:
1760 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001761 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 break;
1763 case 0x310:
1764 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001765 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 break;
1767 case 0x313:
1768 disconnect = 1;
1769 /* unknown, did not find it in the specs */
1770 break;
1771 case 0x301:
1772 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001773 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 break;
1775 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001776 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001778 gsm48_cc_tx_release(trans, &trans->cc.msg);
1779 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 break; /* stay in release state */
1781 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001782 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 return;
1784// release = 1;
1785// l4_cause = 14;
1786// break;
1787 case 0x306:
1788 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001789 mo_cause = trans->cc.msg.cause.value;
1790 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 break;
1792 case 0x323:
1793 disconnect = 1;
1794 break;
1795 default:
1796 release = 1;
1797 }
1798
1799 if (release && trans->callref) {
1800 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001801 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 l4_location, l4_cause);
1803 trans->callref = 0;
1804 }
1805
1806 if (disconnect && trans->callref) {
1807 /* process disconnect towards layer 4 */
1808 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001809 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 }
1811
1812 /* process disconnect towards mobile station */
1813 if (disconnect || release) {
1814 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001815 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1816 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1817 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 mo_rel.cause.diag_len = 3;
1819
1820 if (disconnect)
1821 gsm48_cc_tx_disconnect(trans, &mo_rel);
1822 if (release)
1823 gsm48_cc_tx_release(trans, &mo_rel);
1824 }
1825
1826}
1827
Max3ffce192016-04-25 15:22:00 +02001828/* disconnect both calls from the bridge */
1829static inline void disconnect_bridge(struct gsm_network *net,
1830 struct gsm_mncc_bridge *bridge, int err)
1831{
1832 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1833 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1834 struct gsm_mncc mx_rel;
1835 if (!trans0 || !trans1)
1836 return;
1837
1838 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1839 trans0->callref, trans1->callref, strerror(err));
1840
1841 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1842 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1843 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1844
1845 mx_rel.callref = trans0->callref;
1846 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1847
1848 mx_rel.callref = trans1->callref;
1849 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1850}
1851
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1853 int sec, int micro)
1854{
1855 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001856 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001857 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001858 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859}
1860
1861static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1862{
1863 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001864 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1866 struct tlv_parsed tp;
1867 struct gsm_mncc setup;
1868
1869 memset(&setup, 0, sizeof(struct gsm_mncc));
1870 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001871 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001872 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 /* emergency setup is identified by msg_type */
1874 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1875 setup.emergency = 1;
1876
1877 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001878 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001879 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1880 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001881
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 /* bearer capability */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1884 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001887 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 }
1889 /* facility */
1890 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1891 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1894 }
1895 /* called party bcd number */
1896 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1897 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1900 }
1901 /* user-user */
1902 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1903 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1906 }
1907 /* ss-version */
1908 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1909 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001910 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1912 }
1913 /* CLIR suppression */
1914 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1915 setup.clir.sup = 1;
1916 /* CLIR invocation */
1917 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1918 setup.clir.inv = 1;
1919 /* cc cap */
1920 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1921 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001922 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1924 }
1925
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926 new_cc_state(trans, GSM_CSTATE_INITIATED);
1927
Harald Welte (local)47df3992009-12-26 19:45:03 +01001928 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001929 vlr_subscr_name(trans->vsub), trans->vsub->msisdn,
Harald Welte (local)47df3992009-12-26 19:45:03 +01001930 setup.called.number);
1931
Alexander Couzensb847a212016-08-02 11:34:11 +02001932 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001933
Harald Welte4bfdfe72009-06-10 23:11:52 +08001934 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001935 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936
Harald Welte13cac662009-07-29 12:10:35 +02001937 /* MNCC code will modify the channel asynchronously, we should
1938 * ipaccess-bind only after the modification has been made to the
1939 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 return 0;
1941}
1942
1943static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001944{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001945 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001946 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001948 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001949
Harald Welte7ccf7782009-02-17 01:43:01 +00001950 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001951
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 /* transaction id must not be assigned */
1953 if (trans->transaction_id != 0xff) { /* unasssigned */
1954 DEBUGP(DCC, "TX Setup with assigned transaction. "
1955 "This is not allowed!\n");
1956 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001957 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001958 GSM48_CAUSE_LOC_PRN_S_LU,
1959 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001961 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001962 return rc;
1963 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001964
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001966 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001967 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001968 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001970 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001971 GSM48_CAUSE_LOC_PRN_S_LU,
1972 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001974 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 return rc;
1976 }
Harald Welte78283ef2009-07-23 21:36:44 +02001977 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001978
Harald Welte65e74cc2008-12-29 01:55:35 +00001979 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001980
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001982
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 /* bearer capability */
1984 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001985 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 /* facility */
1987 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001988 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 /* progress */
1990 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001991 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 /* calling party BCD number */
1993 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001994 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 /* called party BCD number */
1996 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001997 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 /* user-user */
1999 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 /* redirecting party BCD number */
2002 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01002003 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 /* signal */
2005 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002007
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00002009
Alexander Couzensb847a212016-08-02 11:34:11 +02002010 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01002011
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002012 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00002013}
2014
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2016{
2017 struct gsm48_hdr *gh = msgb_l3(msg);
2018 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2019 struct tlv_parsed tp;
2020 struct gsm_mncc call_conf;
2021
2022 gsm48_stop_cc_timer(trans);
2023 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2024
2025 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2026 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002027 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002028 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002029#if 0
2030 /* repeat */
2031 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2032 call_conf.repeat = 1;
2033 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2034 call_conf.repeat = 2;
2035#endif
2036 /* bearer capability */
2037 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2038 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002039 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002041 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 }
2043 /* cause */
2044 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2045 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2048 }
2049 /* cc cap */
2050 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2051 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002052 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2054 }
2055
Andreas Eversberg035b8742013-09-19 09:27:06 +02002056 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02002057 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02002058
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2060
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002061 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002062 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063}
2064
2065static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2066{
2067 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002068 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2070
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2072
2073 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2074
2075 /* bearer capability */
2076 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002077 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 /* facility */
2079 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002080 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 /* progress */
2082 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002083 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002085 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086}
2087
2088static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2089{
2090 struct gsm48_hdr *gh = msgb_l3(msg);
2091 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2092 struct tlv_parsed tp;
2093 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002094
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 gsm48_stop_cc_timer(trans);
2096 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2097
2098 memset(&alerting, 0, sizeof(struct gsm_mncc));
2099 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002100 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002101 /* facility */
2102 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2103 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002104 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2106 }
2107
2108 /* progress */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2110 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2113 }
2114 /* ss-version */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2116 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2119 }
2120
2121 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2122
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002123 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002124 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002125}
2126
2127static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2128{
2129 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002130 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2132
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133 gh->msg_type = GSM48_MT_CC_ALERTING;
2134
2135 /* facility */
2136 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 /* progress */
2139 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 /* user-user */
2142 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002143 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144
2145 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002146
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002147 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148}
2149
2150static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2151{
2152 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002153 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2155
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156 gh->msg_type = GSM48_MT_CC_PROGRESS;
2157
2158 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002159 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 /* user-user */
2161 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002162 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002164 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165}
2166
2167static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2168{
2169 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002170 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2172
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 gh->msg_type = GSM48_MT_CC_CONNECT;
2174
2175 gsm48_stop_cc_timer(trans);
2176 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2177
2178 /* facility */
2179 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002180 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 /* progress */
2182 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002183 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002184 /* connected number */
2185 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 /* user-user */
2188 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002189 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190
2191 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2192
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002193 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194}
2195
2196static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2197{
2198 struct gsm48_hdr *gh = msgb_l3(msg);
2199 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2200 struct tlv_parsed tp;
2201 struct gsm_mncc connect;
2202
2203 gsm48_stop_cc_timer(trans);
2204
2205 memset(&connect, 0, sizeof(struct gsm_mncc));
2206 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002207 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002209 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02002210 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
2211 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002212
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213 /* facility */
2214 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2215 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002216 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2218 }
2219 /* user-user */
2220 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2221 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002222 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2224 }
2225 /* ss-version */
2226 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2227 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2230 }
2231
2232 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002233 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002235 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236}
2237
2238
2239static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2240{
2241 struct gsm_mncc connect_ack;
2242
2243 gsm48_stop_cc_timer(trans);
2244
2245 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002246 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002247
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2249 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002250
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002251 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 &connect_ack);
2253}
2254
2255static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2256{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002257 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2259
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2261
2262 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2263
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002264 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265}
2266
2267static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2268{
2269 struct gsm48_hdr *gh = msgb_l3(msg);
2270 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2271 struct tlv_parsed tp;
2272 struct gsm_mncc disc;
2273
2274 gsm48_stop_cc_timer(trans);
2275
2276 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2277
2278 memset(&disc, 0, sizeof(struct gsm_mncc));
2279 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002280 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 /* cause */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2283 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2286 }
2287 /* facility */
2288 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2289 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002290 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2292 }
2293 /* user-user */
2294 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2295 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002296 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2298 }
2299 /* ss-version */
2300 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2301 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002302 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2304 }
2305
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002306 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307
2308}
2309
Harald Weltec66b71c2009-06-11 14:23:20 +08002310static struct gsm_mncc_cause default_cause = {
2311 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2312 .coding = 0,
2313 .rec = 0,
2314 .rec_val = 0,
2315 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2316 .diag_len = 0,
2317 .diag = { 0 },
2318};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319
2320static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2321{
2322 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002323 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2325
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2327
2328 gsm48_stop_cc_timer(trans);
2329 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2330
2331 /* cause */
2332 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002333 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 else
Harald Welte55c8f352010-03-07 23:40:35 +01002335 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336
2337 /* facility */
2338 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002339 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 /* progress */
2341 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 /* user-user */
2344 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346
2347 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002348 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349
2350 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2351
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002352 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353}
2354
2355static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2356{
2357 struct gsm48_hdr *gh = msgb_l3(msg);
2358 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2359 struct tlv_parsed tp;
2360 struct gsm_mncc rel;
2361 int rc;
2362
2363 gsm48_stop_cc_timer(trans);
2364
2365 memset(&rel, 0, sizeof(struct gsm_mncc));
2366 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002367 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368 /* cause */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2370 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2373 }
2374 /* facility */
2375 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2376 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002377 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2379 }
2380 /* user-user */
2381 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2382 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002383 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2385 }
2386 /* ss-version */
2387 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2388 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002389 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2391 }
2392
Harald Weltedcaf5652009-07-23 18:56:43 +02002393 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002395 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002397 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002398 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002399 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002400 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 }
2402
2403 new_cc_state(trans, GSM_CSTATE_NULL);
2404
2405 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002406 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407
2408 return rc;
2409}
2410
2411static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2412{
2413 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002414 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2416
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417 gh->msg_type = GSM48_MT_CC_RELEASE;
2418
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419 gsm48_stop_cc_timer(trans);
2420 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2421
2422 /* cause */
2423 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002424 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425 /* facility */
2426 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002427 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002428 /* user-user */
2429 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002430 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002431
Harald Weltedcaf5652009-07-23 18:56:43 +02002432 trans->cc.T308_second = 0;
2433 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434
Harald Weltedcaf5652009-07-23 18:56:43 +02002435 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2437
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002438 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439}
2440
2441static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2442{
2443 struct gsm48_hdr *gh = msgb_l3(msg);
2444 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2445 struct tlv_parsed tp;
2446 struct gsm_mncc rel;
2447 int rc = 0;
2448
2449 gsm48_stop_cc_timer(trans);
2450
2451 memset(&rel, 0, sizeof(struct gsm_mncc));
2452 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002453 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 /* cause */
2455 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2456 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2459 }
2460 /* facility */
2461 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2462 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002463 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2465 }
2466 /* user-user */
2467 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2468 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002469 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2471 }
2472 /* ss-version */
2473 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2474 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002475 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2477 }
2478
2479 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002480 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002482 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483 MNCC_REJ_IND, &rel);
2484 break;
2485 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002486 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 MNCC_REL_CNF, &rel);
2488 break;
2489 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002490 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 MNCC_REL_IND, &rel);
2492 }
2493 }
2494
2495 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002496 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497
2498 return rc;
2499}
2500
2501static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2502{
2503 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002504 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002506 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2509
2510 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002511
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512 gsm48_stop_cc_timer(trans);
2513
2514 /* cause */
2515 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002516 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517 /* facility */
2518 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002519 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002520 /* user-user */
2521 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002522 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523
Harald Weltec7782de2010-12-21 19:31:41 +01002524 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2525
Harald Weltedcaf5652009-07-23 18:56:43 +02002526 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527
Harald Weltec7782de2010-12-21 19:31:41 +01002528 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529}
2530
2531static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2532{
2533 struct gsm48_hdr *gh = msgb_l3(msg);
2534 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2535 struct tlv_parsed tp;
2536 struct gsm_mncc fac;
2537
2538 memset(&fac, 0, sizeof(struct gsm_mncc));
2539 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002540 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 /* facility */
2542 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2543 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002544 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2546 }
2547 /* ss-version */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2549 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002550 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2552 }
2553
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002554 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555}
2556
2557static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2558{
2559 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002560 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2562
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 gh->msg_type = GSM48_MT_CC_FACILITY;
2564
2565 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002566 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002568 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569}
2570
2571static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2572{
2573 struct gsm_mncc hold;
2574
2575 memset(&hold, 0, sizeof(struct gsm_mncc));
2576 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002577 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578}
2579
2580static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2581{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002582 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2584
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2586
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002587 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588}
2589
2590static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2591{
2592 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002593 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2595
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2597
2598 /* cause */
2599 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002600 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 else
Harald Welte55c8f352010-03-07 23:40:35 +01002602 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002604 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605}
2606
2607static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2608{
2609 struct gsm_mncc retrieve;
2610
2611 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2612 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002613 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002614 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615}
2616
2617static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2618{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002619 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2621
Harald Welte4bfdfe72009-06-10 23:11:52 +08002622 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2623
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002624 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625}
2626
2627static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2628{
2629 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002630 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2632
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2634
2635 /* cause */
2636 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002637 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 else
Harald Welte55c8f352010-03-07 23:40:35 +01002639 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002641 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642}
2643
2644static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2645{
2646 struct gsm48_hdr *gh = msgb_l3(msg);
2647 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2648 struct tlv_parsed tp;
2649 struct gsm_mncc dtmf;
2650
2651 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2652 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002653 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002654 /* keypad facility */
2655 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2656 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002657 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2659 }
2660
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002661 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662}
2663
2664static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2665{
2666 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002667 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2669
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2671
2672 /* keypad */
2673 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002676 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677}
2678
2679static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2680{
2681 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002682 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2684
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2686
2687 /* cause */
2688 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002689 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690 else
Harald Welte55c8f352010-03-07 23:40:35 +01002691 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002693 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694}
2695
2696static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2697{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002698 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002699 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2700
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2702
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002703 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002704}
2705
2706static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2707{
2708 struct gsm_mncc dtmf;
2709
2710 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2711 dtmf.callref = trans->callref;
2712
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002713 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714}
2715
2716static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2717{
2718 struct gsm48_hdr *gh = msgb_l3(msg);
2719 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2720 struct tlv_parsed tp;
2721 struct gsm_mncc modify;
2722
2723 memset(&modify, 0, sizeof(struct gsm_mncc));
2724 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002725 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726 /* bearer capability */
2727 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2728 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002729 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002731 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732 }
2733
2734 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2735
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002736 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737}
2738
2739static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2740{
2741 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002742 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2744
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745 gh->msg_type = GSM48_MT_CC_MODIFY;
2746
2747 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2748
2749 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002750 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751
2752 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2753
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002754 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755}
2756
2757static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2758{
2759 struct gsm48_hdr *gh = msgb_l3(msg);
2760 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2761 struct tlv_parsed tp;
2762 struct gsm_mncc modify;
2763
2764 gsm48_stop_cc_timer(trans);
2765
2766 memset(&modify, 0, sizeof(struct gsm_mncc));
2767 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002768 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002769 /* bearer capability */
2770 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2771 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002772 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002774 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775 }
2776
2777 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2778
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002779 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780}
2781
2782static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2783{
2784 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002785 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2787
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2789
2790 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002791 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792
2793 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2794
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002795 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796}
2797
2798static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2799{
2800 struct gsm48_hdr *gh = msgb_l3(msg);
2801 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2802 struct tlv_parsed tp;
2803 struct gsm_mncc modify;
2804
2805 gsm48_stop_cc_timer(trans);
2806
2807 memset(&modify, 0, sizeof(struct gsm_mncc));
2808 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002809 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 /* bearer capability */
2811 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2812 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002813 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002815 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816 }
2817 /* cause */
2818 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2819 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002820 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2822 }
2823
2824 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2825
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002826 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827}
2828
2829static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2830{
2831 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002832 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2834
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2836
2837 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002838 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002840 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841
2842 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2843
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002844 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845}
2846
2847static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2848{
2849 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002850 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2852
Harald Welte4bfdfe72009-06-10 23:11:52 +08002853 gh->msg_type = GSM48_MT_CC_NOTIFY;
2854
2855 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002856 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002858 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859}
2860
2861static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2862{
2863 struct gsm48_hdr *gh = msgb_l3(msg);
2864 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2865// struct tlv_parsed tp;
2866 struct gsm_mncc notify;
2867
2868 memset(&notify, 0, sizeof(struct gsm_mncc));
2869 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002870// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002872 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002874 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875}
2876
2877static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2878{
2879 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002880 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2882
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883 gh->msg_type = GSM48_MT_CC_USER_INFO;
2884
2885 /* user-user */
2886 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002887 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002888 /* more data */
2889 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002890 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002892 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893}
2894
2895static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2896{
2897 struct gsm48_hdr *gh = msgb_l3(msg);
2898 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2899 struct tlv_parsed tp;
2900 struct gsm_mncc user;
2901
2902 memset(&user, 0, sizeof(struct gsm_mncc));
2903 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002904 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905 /* user-user */
2906 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2907 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002908 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2910 }
2911 /* more data */
2912 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2913 user.more = 1;
2914
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002915 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002916}
2917
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002918static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002919{
2920 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002921 struct gsm_lchan *lchan = trans->conn->lchan;
2922
2923 /*
2924 * We were forced to make an assignment a lot earlier and
2925 * we should avoid sending another assignment that might
2926 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
2927 * In case of rtp-bridge it is too late to change things
2928 * here.
2929 */
2930 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
2931 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002933 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2934 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935}
2936
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002937static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2938 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2939 uint32_t payload_msg_type)
2940{
2941 uint8_t data[sizeof(struct gsm_mncc)];
2942 struct gsm_mncc_rtp *rtp;
2943
2944 memset(&data, 0, sizeof(data));
2945 rtp = (struct gsm_mncc_rtp *) &data[0];
2946
2947 rtp->callref = callref;
2948 rtp->msg_type = cmd;
2949 rtp->ip = addr;
2950 rtp->port = port;
2951 rtp->payload_type = payload_type;
2952 rtp->payload_msg_type = payload_msg_type;
2953 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2954}
2955
2956static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2957{
2958 struct gsm_lchan *lchan;
2959 int msg_type;
2960
2961 lchan = trans->conn->lchan;
2962 switch (lchan->abis_ip.rtp_payload) {
2963 case RTP_PT_GSM_FULL:
2964 msg_type = GSM_TCHF_FRAME;
2965 break;
2966 case RTP_PT_GSM_EFR:
2967 msg_type = GSM_TCHF_FRAME_EFR;
2968 break;
2969 case RTP_PT_GSM_HALF:
2970 msg_type = GSM_TCHH_FRAME;
2971 break;
2972 case RTP_PT_AMR:
2973 msg_type = GSM_TCH_FRAME_AMR;
2974 break;
2975 default:
2976 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
2977 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
2978 msg_type = 0;
2979 break;
2980 }
2981
2982 return mncc_recv_rtp(net, trans->callref, cmd,
2983 lchan->abis_ip.bound_ip,
2984 lchan->abis_ip.bound_port,
2985 lchan->abis_ip.rtp_payload,
2986 msg_type);
2987}
2988
2989static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2990{
2991 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2992}
2993
2994static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2995{
2996 struct gsm_bts *bts;
2997 struct gsm_lchan *lchan;
2998 struct gsm_trans *trans;
Max8db12e42016-04-18 23:11:18 +02002999 enum gsm48_chan_mode m;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003000
3001 /* Find callref */
3002 trans = trans_find_by_callref(net, callref);
3003 if (!trans) {
3004 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
3005 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3006 return -EIO;
3007 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003008 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003009 if (!trans->conn) {
3010 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
3011 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3012 return 0;
3013 }
3014
3015 lchan = trans->conn->lchan;
3016 bts = lchan->ts->trx->bts;
3017 if (!is_ipaccess_bts(bts)) {
3018 /*
3019 * I want this to be straight forward and have no audio flow
3020 * through the nitb/osmo-mss system. This currently means that
3021 * this will not work with BS11/Nokia type BTS. We would need
3022 * to have a trau<->rtp bridge for these but still preferable
3023 * in another process.
3024 */
3025 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3026 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3027 return -EINVAL;
3028 }
3029
3030 trans->conn->mncc_rtp_bridge = 1;
3031 /*
3032 * *sigh* we need to pick a codec now. Pick the most generic one
3033 * right now and hope we could fix that later on. This is very
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +02003034 * similiar to the routine above.
3035 * Fallback to the internal MNCC mode to select a route.
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003036 */
3037 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3038 trans->conn->mncc_rtp_create_pending = 1;
Max8db12e42016-04-18 23:11:18 +02003039 m = mncc_codec_for_mode(lchan->type);
3040 LOGP(DMNCC, LOGL_DEBUG, "RTP create: codec=%s, chan_type=%s\n",
3041 get_value_string(gsm48_chan_mode_names, m),
3042 get_value_string(gsm_chan_t_names, lchan->type));
3043 return gsm0808_assign_req(trans->conn, m,
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003044 lchan->type != GSM_LCHAN_TCH_H);
3045 }
3046
3047 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3048 return 0;
3049}
3050
3051static int tch_rtp_connect(struct gsm_network *net, void *arg)
3052{
3053 struct gsm_lchan *lchan;
3054 struct gsm_trans *trans;
3055 struct gsm_mncc_rtp *rtp = arg;
3056
3057 /* Find callref */
3058 trans = trans_find_by_callref(net, rtp->callref);
3059 if (!trans) {
3060 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3061 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3062 return -EIO;
3063 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003064 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003065 if (!trans->conn) {
3066 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3067 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3068 return 0;
3069 }
3070
3071 lchan = trans->conn->lchan;
Max8db12e42016-04-18 23:11:18 +02003072 LOGP(DMNCC, LOGL_DEBUG, "RTP connect: codec=%s, chan_type=%s\n",
3073 get_value_string(gsm48_chan_mode_names,
3074 mncc_codec_for_mode(lchan->type)),
3075 get_value_string(gsm_chan_t_names, lchan->type));
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003076
3077 /* TODO: Check if payload_msg_type is compatible with what we have */
3078 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3079 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3080 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3081 }
3082
3083 /*
3084 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3085 * complains about both rtp and rtp payload2 being present in the
3086 * same package!
3087 */
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003088 trans->conn->mncc_rtp_connect_pending = 1;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003089 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3090}
3091
3092static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3093{
3094 struct gsm_network *net;
3095 struct gsm_trans *tmp, *trans = NULL;
3096
3097 net = lchan->ts->trx->bts->network;
3098 llist_for_each_entry(tmp, &net->trans_list, entry) {
3099 if (!tmp->conn)
3100 continue;
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003101 if (tmp->conn->lchan != lchan && tmp->conn->ho_lchan != lchan)
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003102 continue;
3103 trans = tmp;
3104 break;
3105 }
3106
3107 if (!trans) {
3108 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3109 gsm_lchan_name(lchan));
3110 return 0;
3111 }
3112
3113 switch (signal) {
3114 case S_ABISIP_CRCX_ACK:
3115 if (lchan->conn->mncc_rtp_create_pending) {
3116 lchan->conn->mncc_rtp_create_pending = 0;
3117 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3118 gsm_lchan_name(lchan));
3119 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3120 }
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003121 /*
3122 * TODO: this appears to be too early? Why not until after
3123 * the handover detect or the handover complete?
3124 */
3125 maybe_switch_for_handover(lchan);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003126 break;
3127 case S_ABISIP_MDCX_ACK:
Holger Hans Peter Freytherc21dcb22015-08-04 14:41:21 +02003128 if (lchan->conn->mncc_rtp_connect_pending) {
3129 lchan->conn->mncc_rtp_connect_pending = 0;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003130 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3131 gsm_lchan_name(lchan));
3132 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3133 }
3134 break;
3135 }
3136
3137 return 0;
3138}
3139
3140
Harald Welte4bfdfe72009-06-10 23:11:52 +08003141static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003142 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143 int type;
3144 int (*rout) (struct gsm_trans *trans, void *arg);
3145} downstatelist[] = {
3146 /* mobile originating call establishment */
3147 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3148 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3149 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3150 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3151 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC) | SBIT(GSM_CSTATE_CALL_DELIVERED), /* 5.2.1.2 | 5.2.1.6 | 5.2.1.6 */
3152 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3153 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3154 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3155 /* mobile terminating call establishment */
3156 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3157 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3158 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3159 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3160 /* signalling during call */
3161 {SBIT(GSM_CSTATE_ACTIVE),
3162 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3163 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3164 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3165 {ALL_STATES,
3166 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3167 {ALL_STATES,
3168 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3169 {ALL_STATES,
3170 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3171 {SBIT(GSM_CSTATE_ACTIVE),
3172 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3173 {SBIT(GSM_CSTATE_ACTIVE),
3174 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3175 {SBIT(GSM_CSTATE_ACTIVE),
3176 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3177 {SBIT(GSM_CSTATE_ACTIVE),
3178 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3179 {SBIT(GSM_CSTATE_ACTIVE),
3180 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3181 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3182 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3183 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3184 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3185 {SBIT(GSM_CSTATE_ACTIVE),
3186 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3187 /* clearing */
3188 {SBIT(GSM_CSTATE_INITIATED),
3189 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3190 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3191 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3192 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3193 MNCC_REL_REQ, gsm48_cc_tx_release},
3194 /* special */
3195 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003196 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003197};
3198
3199#define DOWNSLLEN \
3200 (sizeof(downstatelist) / sizeof(struct downstate))
3201
3202
Harald Welte76556372010-12-22 23:57:45 +01003203int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003204{
Harald Welte1a6f7982009-08-09 18:52:33 +02003205 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003206 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003207 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003208 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003209 struct gsm_mncc *data = arg, rel;
3210
Harald Welte04dc88f2010-12-22 21:45:05 +01003211 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3212
Harald Welte4bfdfe72009-06-10 23:11:52 +08003213 /* handle special messages */
3214 switch(msg_type) {
3215 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02003216 rc = tch_bridge(net, arg);
3217 if (rc < 0)
3218 disconnect_bridge(net, arg, -rc);
3219 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003220 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003221 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003222 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003223 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003224 case MNCC_RTP_CREATE:
3225 return tch_rtp_create(net, data->callref);
3226 case MNCC_RTP_CONNECT:
3227 return tch_rtp_connect(net, arg);
3228 case MNCC_RTP_FREE:
3229 /* unused right now */
3230 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003231 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003232 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003233 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003234 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003235 /* Find callref */
3236 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003237 if (!trans) {
3238 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003239 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003240 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003241 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte04dc88f2010-12-22 21:45:05 +01003242 if (!trans->conn) {
3243 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003244 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003245 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003246 if (!trans->conn->lchan) {
3247 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3248 return 0;
3249 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003250 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3251 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003252 /* This should be LOGL_ERROR or NOTICE, but
3253 * unfortuantely it happens for a couple of frames at
3254 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003255 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003256 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003257 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003258 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003259 switch (bts->type) {
3260 case GSM_BTS_TYPE_NANOBTS:
Maxf9685c12017-03-23 12:01:07 +01003261 case GSM_BTS_TYPE_OSMOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01003262 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003263 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003264 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003265 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003266 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003267 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003268 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003269 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003270 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003271 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003272 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003273 }
3274 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003275 }
3276
3277 memset(&rel, 0, sizeof(struct gsm_mncc));
3278 rel.callref = data->callref;
3279
3280 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003281 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003282
3283 /* Callref unknown */
3284 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003285 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003286
Harald Welte4a3464c2009-07-04 10:11:24 +02003287 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003288 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3289 "Received '%s' from MNCC with "
3290 "unknown callref %d\n", data->called.number,
3291 get_mncc_name(msg_type), data->callref);
3292 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003293 return mncc_release_ind(net, NULL, data->callref,
3294 GSM48_CAUSE_LOC_PRN_S_LU,
3295 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003296 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003297 if (!data->called.number[0] && !data->imsi[0]) {
3298 DEBUGP(DCC, "(bts - trx - ts - ti) "
3299 "Received '%s' from MNCC with "
3300 "no number or IMSI\n", get_mncc_name(msg_type));
3301 /* Invalid number */
3302 return mncc_release_ind(net, NULL, data->callref,
3303 GSM48_CAUSE_LOC_PRN_S_LU,
3304 GSM48_CC_CAUSE_INV_NR_FORMAT);
3305 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003306 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003307 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003308 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3309 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003310 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003311 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003312
3313 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003314 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003315
Harald Welte4bfdfe72009-06-10 23:11:52 +08003316 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003317 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003318 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3319 "Received '%s' from MNCC with "
3320 "unknown subscriber %s\n", data->called.number,
3321 get_mncc_name(msg_type), data->called.number);
3322 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003323 return mncc_release_ind(net, NULL, data->callref,
3324 GSM48_CAUSE_LOC_PRN_S_LU,
3325 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003326 }
3327 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003328 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003329 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3330 "Received '%s' from MNCC with "
3331 "detached subscriber %s\n", data->called.number,
3332 get_mncc_name(msg_type), data->called.number);
Harald Welte2483f1b2016-06-19 18:06:02 +02003333 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003334 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003335 return mncc_release_ind(net, NULL, data->callref,
3336 GSM48_CAUSE_LOC_PRN_S_LU,
3337 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003338 }
3339 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003340 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003341 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003342 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003343 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003344 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003345 mncc_release_ind(net, NULL, data->callref,
3346 GSM48_CAUSE_LOC_PRN_S_LU,
3347 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003348 return -ENOMEM;
3349 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003350 /* Find lchan */
Harald Welte2483f1b2016-06-19 18:06:02 +02003351 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003352
Harald Welte4bfdfe72009-06-10 23:11:52 +08003353 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003354 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003355 /* find transaction with this subscriber already paging */
3356 llist_for_each_entry(transt, &net->trans_list, entry) {
3357 /* Transaction of our lchan? */
3358 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003359 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003360 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003361 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003362 "Received '%s' from MNCC with "
3363 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003364 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003365 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003366 get_mncc_name(msg_type), vsub->lac);
3367 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003368 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003369 return 0;
3370 }
3371 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003372 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003373
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003374 /* Request a channel */
Harald Welte2483f1b2016-06-19 18:06:02 +02003375 trans->paging_request = subscr_request_channel(
3376 vsub,
3377 RSL_CHANNEED_TCH_F,
3378 setup_trig_pag_evt,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003379 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003380 if (!trans->paging_request) {
3381 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003382 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003383 trans_free(trans);
3384 return 0;
3385 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003386 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003387 return 0;
3388 }
3389 /* Assign lchan */
Harald Welte2483f1b2016-06-19 18:06:02 +02003390 trans->conn = msc_subscr_conn_get(conn);
3391 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003392 } else {
3393 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003394 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003395 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003396
3397 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003398 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003399
3400 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003401 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003402 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003403 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003404 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003405 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003406 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3407 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003408 if (msg_type == MNCC_REL_REQ)
3409 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3410 else
3411 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3412 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003413 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003414 return rc;
3415 }
3416
3417 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3418 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003419 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003420 trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003421 vlr_subscr_msisdn_or_name(trans->conn->vsub),
Harald Weltedcaf5652009-07-23 18:56:43 +02003422 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003423 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003424
3425 /* Find function for current state and message */
3426 for (i = 0; i < DOWNSLLEN; i++)
3427 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003428 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003429 break;
3430 if (i == DOWNSLLEN) {
3431 DEBUGP(DCC, "Message unhandled at this state.\n");
3432 return 0;
3433 }
3434
3435 rc = downstatelist[i].rout(trans, arg);
3436
3437 return rc;
3438}
3439
3440
3441static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003442 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003443 int type;
3444 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3445} datastatelist[] = {
3446 /* mobile originating call establishment */
3447 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3448 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3449 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3450 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3451 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3452 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3453 /* mobile terminating call establishment */
3454 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3455 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3456 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3457 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003458 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003459 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3460 /* signalling during call */
3461 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3462 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3463 {SBIT(GSM_CSTATE_ACTIVE),
3464 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3465 {ALL_STATES,
3466 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3467 {ALL_STATES,
3468 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3469 {ALL_STATES,
3470 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3471 {SBIT(GSM_CSTATE_ACTIVE),
3472 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3473 {SBIT(GSM_CSTATE_ACTIVE),
3474 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3475 {SBIT(GSM_CSTATE_ACTIVE),
3476 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3477 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3478 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3479 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3480 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3481 {SBIT(GSM_CSTATE_ACTIVE),
3482 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3483 /* clearing */
3484 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3485 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3486 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3487 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3488 {ALL_STATES, /* 5.4.3.4 */
3489 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3490};
3491
3492#define DATASLLEN \
3493 (sizeof(datastatelist) / sizeof(struct datastate))
3494
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003495static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003496{
3497 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003498 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003499 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003500 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003501 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003502
Harald Welte4bfdfe72009-06-10 23:11:52 +08003503 if (msg_type & 0x80) {
3504 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3505 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003506 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003507
Harald Welte2483f1b2016-06-19 18:06:02 +02003508 if (!conn->vsub) {
3509 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003510 return -EINVAL;
3511 }
3512
Harald Welte4bfdfe72009-06-10 23:11:52 +08003513 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003514 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003515
Harald Welte6f5aee02009-07-23 21:21:14 +02003516 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003517 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003518 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003519 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003520 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3521 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003522
3523 /* Create transaction */
3524 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003525 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003526 "creating new trans.\n", transaction_id);
3527 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003528 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003529 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003530 transaction_id, new_callref++);
3531 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003532 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003533 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003534 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003535 GSM48_MT_CC_RELEASE_COMPL);
3536 return -ENOMEM;
3537 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003538 /* Assign transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003539 trans->conn = msc_subscr_conn_get(conn);
3540 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003541 }
3542
3543 /* find function for current state and message */
3544 for (i = 0; i < DATASLLEN; i++)
3545 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003546 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003547 break;
3548 if (i == DATASLLEN) {
3549 DEBUGP(DCC, "Message unhandled at this state.\n");
3550 return 0;
3551 }
3552
Harald Welte2483f1b2016-06-19 18:06:02 +02003553 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003554
Harald Welte4bfdfe72009-06-10 23:11:52 +08003555 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003556
Harald Welte2483f1b2016-06-19 18:06:02 +02003557 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003558 return rc;
3559}
3560
Harald Welte2483f1b2016-06-19 18:06:02 +02003561static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003562{
Harald Welte2483f1b2016-06-19 18:06:02 +02003563 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3564 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003565
Harald Welte2483f1b2016-06-19 18:06:02 +02003566 switch (pdisc) {
3567 case GSM48_PDISC_MM:
3568 switch (msg_type) {
3569 case GSM48_MT_MM_LOC_UPD_REQUEST:
3570 case GSM48_MT_MM_CM_SERV_REQ:
3571 case GSM48_MT_MM_AUTH_RESP:
3572 case GSM48_MT_MM_AUTH_FAIL:
3573 case GSM48_MT_MM_ID_RESP:
3574 case GSM48_MT_MM_TMSI_REALL_COMPL:
3575 case GSM48_MT_MM_IMSI_DETACH_IND:
3576 return true;
3577 default:
3578 break;
3579 }
3580 break;
3581 case GSM48_PDISC_RR:
3582 switch (msg_type) {
3583 case GSM48_MT_RR_CIPH_M_COMPL:
3584 case GSM48_MT_RR_PAG_RESP:
3585 return true;
3586 default:
3587 break;
3588 }
3589 break;
3590 default:
3591 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003592 }
3593
Harald Welte2483f1b2016-06-19 18:06:02 +02003594 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003595}
3596
Harald Welte2483f1b2016-06-19 18:06:02 +02003597void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3598 struct msgb *msg)
3599{
3600
3601 /* If a CM Service Request was received before, this is the request the
3602 * conn was opened for. No need to wait for further messages. */
3603
3604 if (!conn->received_cm_service_request)
3605 return;
3606
3607 if (log_check_level(DMM, LOGL_DEBUG)) {
3608 struct gsm48_hdr *gh = msgb_l3(msg);
3609 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3610 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3611
3612 DEBUGP(DMM, "%s pdisc=%d msg_type=0x%02x:"
3613 " received_cm_service_request changes to false\n",
3614 vlr_subscr_name(conn->vsub),
3615 pdisc, msg_type);
3616 }
3617 conn->received_cm_service_request = false;
3618}
3619
3620
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003621/* Main entry point for GSM 04.08/44.008 Layer 3 data (e.g. from the BSC). */
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003622int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003623{
3624 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003625 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003626 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003627
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003628 OSMO_ASSERT(conn);
3629 OSMO_ASSERT(msg);
3630
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003631 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Harald Welte2483f1b2016-06-19 18:06:02 +02003632
3633 if (!msc_subscr_conn_is_accepted(conn)
3634 && !msg_is_initially_permitted(gh)) {
3635 LOGP(DRLL, LOGL_ERROR,
3636 "subscr %s: Message not permitted for initial conn:"
3637 " pdisc=0x%02x msg_type=0x%02x\n",
3638 vlr_subscr_name(conn->vsub), gh->proto_discr, gh->msg_type);
3639 return -EACCES;
3640 }
3641
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003642#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003643 if (silent_call_reroute(conn, msg))
3644 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003645#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003646
Harald Welte52b1f982008-12-23 20:25:15 +00003647 switch (pdisc) {
3648 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003649 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003650 break;
3651 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003652 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003653 break;
3654 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003655 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003656 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003657 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003658 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003659 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003660 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003661 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003662 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3663 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003664 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003665 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003666 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003667 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003668 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003669 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003670 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3671 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003672 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003673 break;
3674 }
3675
3676 return rc;
3677}
Harald Welte8470bf22008-12-25 23:28:35 +00003678
Harald Welte2483f1b2016-06-19 18:06:02 +02003679/***********************************************************************
3680 * VLR integration
3681 ***********************************************************************/
3682
3683/* VLR asks us to send an authentication request */
3684static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3685 bool send_autn)
3686{
3687 struct gsm_subscriber_connection *conn = msc_conn_ref;
3688 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3689 send_autn? at->vec.autn : NULL,
3690 at->key_seq);
3691}
3692
3693/* VLR asks us to send an authentication reject */
3694static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3695{
3696 struct gsm_subscriber_connection *conn = msc_conn_ref;
3697 return gsm48_tx_mm_auth_rej(conn);
3698}
3699
3700/* VLR asks us to transmit an Identity Request of given type */
3701static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3702{
3703 struct gsm_subscriber_connection *conn = msc_conn_ref;
3704 return mm_tx_identity_req(conn, mi_type);
3705}
3706
3707/* VLR asks us to transmit a Location Update Accept */
3708static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3709{
3710 struct gsm_subscriber_connection *conn = msc_conn_ref;
3711 return gsm0408_loc_upd_acc(conn, send_tmsi);
3712}
3713
3714/* VLR asks us to transmit a Location Update Reject */
3715static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3716{
3717 struct gsm_subscriber_connection *conn = msc_conn_ref;
3718 return gsm0408_loc_upd_rej(conn, cause);
3719}
3720
3721/* VLR asks us to transmit a CM Service Accept */
3722static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3723{
3724 struct gsm_subscriber_connection *conn = msc_conn_ref;
3725 return gsm48_tx_mm_serv_ack(conn);
3726}
3727
3728/* VLR asks us to transmit a CM Service Reject */
3729static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3730{
3731 uint8_t cause;
3732 struct gsm_subscriber_connection *conn = msc_conn_ref;
3733 conn->received_cm_service_request = false;
3734
3735 switch (result) {
3736 default:
3737 case VLR_PR_ARQ_RES_NONE:
3738 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3739 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3740 cause = GSM48_REJECT_NETWORK_FAILURE;
3741 break;
3742 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3743 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3744 break;
3745 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3746 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3747 break;
3748 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3749 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3750 break;
3751 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3752 cause = GSM48_REJECT_ILLEGAL_MS;
3753 break;
3754 case VLR_PR_ARQ_RES_TIMEOUT:
3755 cause = GSM48_REJECT_CONGESTION;
3756 break;
3757 };
3758
3759 return gsm48_tx_mm_serv_rej(conn, cause);
3760}
3761
3762/* VLR asks us to start using ciphering */
3763static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3764 enum vlr_ciph ciph,
3765 bool retrieve_imeisv)
3766{
3767 struct gsm_subscriber_connection *conn = msc_conn_ref;
3768 struct vlr_subscr *vsub;
3769 struct gsm_auth_tuple *tuple;
3770
3771 if (!conn || !conn->vsub) {
3772 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3773 " NULL conn/subscriber");
3774 return -EINVAL;
3775 }
3776
3777 vsub = conn->vsub;
3778 tuple = vsub->last_tuple;
3779
3780 if (!tuple) {
3781 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3782 " Command: no auth tuple available\n",
3783 vlr_subscr_name(vsub));
3784 return -EINVAL;
3785 }
3786
3787 /* TODO: MSCSPLIT: don't directly push BSC buttons */
3788 return gsm0808_cipher_mode(conn, ciph, tuple->vec.kc, 8,
3789 retrieve_imeisv);
3790}
3791
3792/* VLR informs us that the subscriber data has somehow been modified */
3793static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3794{
3795 /* FIXME */
3796}
3797
3798/* VLR informs us that the subscriber has been associated with a conn */
3799static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3800 struct vlr_subscr *vsub)
3801{
3802 struct gsm_subscriber_connection *conn = msc_conn_ref;
3803 OSMO_ASSERT(!conn->vsub);
3804 conn->vsub = vlr_subscr_get(vsub);
3805}
3806
3807/* operations that we need to implement for libvlr */
3808static const struct vlr_ops msc_vlr_ops = {
3809 .tx_auth_req = msc_vlr_tx_auth_req,
3810 .tx_auth_rej = msc_vlr_tx_auth_rej,
3811 .tx_id_req = msc_vlr_tx_id_req,
3812 .tx_lu_acc = msc_vlr_tx_lu_acc,
3813 .tx_lu_rej = msc_vlr_tx_lu_rej,
3814 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3815 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3816 .set_ciph_mode = msc_vlr_set_ciph_mode,
3817 .subscr_update = msc_vlr_subscr_update,
3818 .subscr_assoc = msc_vlr_subscr_assoc,
3819};
3820
3821/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3822int msc_vlr_alloc(struct gsm_network *net)
3823{
3824 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3825 if (!net->vlr)
3826 return -ENOMEM;
3827 net->vlr->user_ctx = net;
3828 return 0;
3829}
3830
3831/* Launch the VLR, i.e. its GSUP connection */
3832int msc_vlr_start(struct gsm_network *net)
3833{
3834 OSMO_ASSERT(net->vlr);
3835 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3836 net->gsup_server_port);
3837}
3838
Harald Welte805f6442009-07-28 18:25:29 +02003839/*
Neels Hofmeyrcc7db182016-12-18 23:52:38 +01003840 * This will be run by the linker when loading the DSO. We use it to
Harald Welte805f6442009-07-28 18:25:29 +02003841 * do system initialization, e.g. registration of signal handlers.
3842 */
3843static __attribute__((constructor)) void on_dso_load_0408(void)
3844{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003845 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003846}