blob: b6746a5647dbc27a533b62659cf9ff07042a086b [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>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020034#include <openssl/rand.h>
Harald Welte52b1f982008-12-23 20:25:15 +000035
Harald Welte9c3dc902012-04-08 16:59:24 +020036#include "bscconfig.h"
37
Sylvain Munaut30a15382009-12-24 00:27:26 +010038#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000039#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/debug.h>
41#include <openbsc/gsm_data.h>
42#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000043#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000044#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080045#include <openbsc/gsm_04_80.h>
Harald Welteeac38c32017-05-29 18:02:53 +020046#include <openbsc/gsm_04_14.h>
Harald Welte8470bf22008-12-25 23:28:35 +000047#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000048#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000049#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000050#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000052#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020053#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020054#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020055#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010056#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080057#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080058#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010059#include <openbsc/handover.h>
Holger Hans Peter Freyther53122b02015-08-20 19:10:58 +020060#include <openbsc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020061#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010062#include <osmocom/core/bitvec.h>
Harald Welte2483f1b2016-06-19 18:06:02 +020063#include <openbsc/vlr.h>
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020064#include <openbsc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080065
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010066#include <osmocom/gsm/gsm48.h>
67#include <osmocom/gsm/gsm0480.h>
68#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020069#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010070#include <osmocom/core/msgb.h>
71#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010072#include <osmocom/core/utils.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010073#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020074#include <osmocom/crypt/auth.h>
75
76#include <openbsc/msc_ifaces.h>
77
78#ifdef BUILD_IU
79#include <openbsc/iu.h>
80#endif
Harald Welte52b1f982008-12-23 20:25:15 +000081
Philipp Maierfbf66102017-04-09 12:32:51 +020082#include <openbsc/a_iface.h>
83
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020084#include <assert.h>
85
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020086
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020087void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010088void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020089
Harald Welte2483f1b2016-06-19 18:06:02 +020090static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
91 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080092static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020093 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000094
Harald Welte52b1f982008-12-23 20:25:15 +000095struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020096 uint16_t mcc;
97 uint16_t mnc;
98 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000099};
100
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200101static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800102
Harald Welte31c00f72011-03-03 23:29:05 +0100103void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
104{
105 net->mncc_recv(net, msg);
106}
107
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800108static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
109 struct gsm_trans *trans)
110{
111 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
112
113 /* if we get passed a transaction reference, do some common
114 * work that the caller no longer has to do */
115 if (trans) {
116 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800117 }
118
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200119 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800120}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100121
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800122int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
123{
124 struct gsm48_hdr *gh;
125 struct msgb *ss_notify;
126
127 ss_notify = gsm0480_create_notifySS(message);
128 if (!ss_notify)
129 return -1;
130
131 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
132 uint8_t *data = msgb_push(ss_notify, 1);
133 data[0] = ss_notify->len - 1;
134 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
135 gh->msg_type = GSM48_MT_CC_FACILITY;
136 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
137}
138
Harald Welte2483f1b2016-06-19 18:06:02 +0200139/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100140void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
141{
142 struct gsm_trans *trans, *temp;
143
144 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
145
146 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100147 if (trans->protocol == protocol) {
148 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100149 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100150 }
Harald Welte371efe52010-12-22 23:17:50 +0100151 }
152}
153
Holger Freyther429e7762008-12-30 13:28:30 +0000154/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200155static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000156{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800157 struct msgb *msg;
158
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800159 msg = gsm48_create_loc_upd_rej(cause);
160 if (!msg) {
161 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
162 return -1;
163 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200164
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200165 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
166 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100167
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800168 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000169}
170
171/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200172static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
173 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000174{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100175 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000176 struct gsm48_hdr *gh;
177 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200178 uint8_t *mid;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200179
Harald Welte52b1f982008-12-23 20:25:15 +0000180 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
181 gh->proto_discr = GSM48_PDISC_MM;
182 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
183
184 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200185 gsm48_generate_lai(lai, conn->network->country_code,
186 conn->network->network_code,
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200187 conn->lac);
Harald Welte52b1f982008-12-23 20:25:15 +0000188
Harald Welte2483f1b2016-06-19 18:06:02 +0200189 if (send_tmsi == GSM_RESERVED_TMSI) {
190 /* we did not allocate a TMSI to the MS, so we need to
191 * include the IMSI in order for the MS to delete any
192 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200193 uint8_t mi[10];
194 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200195 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200196 mid = msgb_put(msg, len);
197 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200198 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
199 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200200 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200201 /* Include the TMSI, which means that the MS will send a
202 * TMSI REALLOCATION COMPLETE, and we should wait for
203 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200204 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200205 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200206 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
207 vlr_subscr_name(conn->vsub),
208 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200209 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200210 /* TODO: Follow-on proceed */
211 /* TODO: CTS permission */
212 /* TODO: Equivalent PLMNs */
213 /* TODO: Emergency Number List */
214 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000215
Harald Welte52b1f982008-12-23 20:25:15 +0000216
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800217 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000218}
219
Harald Weltebf5e8df2009-02-03 12:59:45 +0000220/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200221static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000222{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100223 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000224 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000225
Harald Welte231ad4f2008-12-27 11:15:38 +0000226 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
227 gh->proto_discr = GSM48_PDISC_MM;
228 gh->msg_type = GSM48_MT_MM_ID_REQ;
229 gh->data[0] = id_type;
230
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800231 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000232}
233
Harald Weltebf5e8df2009-02-03 12:59:45 +0000234/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800235static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000236{
237 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200238 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200239 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000240
Harald Welte2483f1b2016-06-19 18:06:02 +0200241 if (!conn->vsub) {
242 LOGP(DMM, LOGL_ERROR,
243 "Rx MM Identity Response: invalid: no subscriber\n");
244 return -EINVAL;
245 }
246
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200247 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200248 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
249 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000250
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200251 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100252
Harald Welte2483f1b2016-06-19 18:06:02 +0200253 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000254}
255
Harald Welte2483f1b2016-06-19 18:06:02 +0200256/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200257static const struct value_string lupd_names[] = {
258 { GSM48_LUPD_NORMAL, "NORMAL" },
259 { GSM48_LUPD_PERIODIC, "PERIODIC" },
260 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
261 { 0, NULL }
262};
Harald Welte2a139372009-02-22 21:14:55 +0000263
Harald Welte2483f1b2016-06-19 18:06:02 +0200264/* Chapter 9.2.15: Receive Location Updating Request.
265 * Keep this function non-static for direct invocation by unit tests. */
266int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000267{
Harald Welte2483f1b2016-06-19 18:06:02 +0200268 static const enum subscr_conn_from conn_from_lu = SUBSCR_CONN_FROM_LU;
269 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000270 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000271 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200272 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200273 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200274 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200275 uint32_t tmsi;
276 char *imsi;
277 struct osmo_location_area_id old_lai, new_lai;
278 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200279 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200280 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000281
Harald Welte8470bf22008-12-25 23:28:35 +0000282 lu = (struct gsm48_loc_upd_req *) gh->data;
283
284 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000285
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200286 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000287
Harald Welte2483f1b2016-06-19 18:06:02 +0200288 rc = msc_create_conn_fsm(conn, mi_string);
289 if (rc)
290 /* logging already happened in msc_create_conn_fsm() */
291 return rc;
292
293 conn->classmark.classmark1 = lu->classmark1;
294 conn->classmark.classmark1_set = true;
295
296 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
297 gsm48_mi_type_name(mi_type), mi_string,
298 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000299
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200300 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100301
Harald Welte24ff6ee2009-12-22 00:41:05 +0100302 switch (lu->type) {
303 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200304 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200305 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100306 break;
307 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200308 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200309 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100310 break;
311 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200312 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200313 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100314 break;
315 }
316
Harald Welte2483f1b2016-06-19 18:06:02 +0200317 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
318 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
319 /* TODO: 10.5.7.8 Device properties */
320 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200321
Harald Welte52b1f982008-12-23 20:25:15 +0000322 switch (mi_type) {
323 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200324 tmsi = GSM_RESERVED_TMSI;
325 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000326 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000327 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200328 tmsi = tmsi_from_string(mi_string);
329 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000330 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200331 default:
Harald Weltea0368542009-06-27 02:58:43 +0200332 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200333 tmsi = GSM_RESERVED_TMSI;
334 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000335 break;
336 }
337
Harald Welte2483f1b2016-06-19 18:06:02 +0200338 gsm48_decode_lai(&lu->lai, &old_lai.plmn.mcc,
339 &old_lai.plmn.mnc, &old_lai.lac);
340 new_lai.plmn.mcc = conn->network->country_code;
341 new_lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200342 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200343 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200344
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200345 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200346 lu_fsm = vlr_loc_update(conn->conn_fsm,
347 SUBSCR_CONN_E_ACCEPTED,
348 SUBSCR_CONN_E_CN_CLOSE,
349 (void*)&conn_from_lu,
350 net->vlr, conn, vlr_lu_type, tmsi, imsi,
351 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200352 is_utran || conn->network->authentication_required,
353 is_utran? VLR_CIPH_A5_3
354 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200355 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200356 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200357 net->vlr->cfg.assign_tmsi);
358 if (!lu_fsm) {
359 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
360 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800361 }
362
Harald Welte2483f1b2016-06-19 18:06:02 +0200363 /* From vlr_loc_update() we expect an implicit dispatch of
364 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
365 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000366
Harald Welte2483f1b2016-06-19 18:06:02 +0200367 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
368 LOGP(DRR, LOGL_ERROR,
369 "%s: internal error during Location Updating attempt\n",
370 mi_string);
371 return -EIO;
372 }
373
374 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000375}
376
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100377/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200378/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100379static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800380{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100381 uint8_t ret;
382
383 ret = value / 10;
384 ret |= (value % 10) << 4;
385
386 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800387}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100388
Harald Welte4bfdfe72009-06-10 23:11:52 +0800389
Harald Weltedb253af2008-12-30 17:56:55 +0000390/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800391int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000392{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100393 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000394 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200395 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200396 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200397 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100398
Harald Welte4bfdfe72009-06-10 23:11:52 +0800399 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100400 struct tm* gmt_time;
401 struct tm* local_time;
402 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100403 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000404
Harald Weltedb253af2008-12-30 17:56:55 +0000405 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
406 gh->proto_discr = GSM48_PDISC_MM;
407 gh->msg_type = GSM48_MT_MM_INFO;
408
409 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200410#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000411 name_len = strlen(net->name_long);
412 /* 10.5.3.5a */
413 ptr8 = msgb_put(msg, 3);
414 ptr8[0] = GSM48_IE_NAME_LONG;
415 ptr8[1] = name_len*2 +1;
416 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
417
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200418 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000419 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000420 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000421
422 /* FIXME: Use Cell Broadcast, not UCS-2, since
423 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200424#endif
425 name_len = (strlen(net->name_long)*7)/8;
426 name_pad = (8 - strlen(net->name_long)*7)%8;
427 if (name_pad > 0)
428 name_len++;
429 /* 10.5.3.5a */
430 ptr8 = msgb_put(msg, 3);
431 ptr8[0] = GSM48_IE_NAME_LONG;
432 ptr8[1] = name_len +1;
433 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
434
435 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100436 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200437
Harald Weltedb253af2008-12-30 17:56:55 +0000438 }
439
440 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200441#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000442 name_len = strlen(net->name_short);
443 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200444 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200445 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000446 ptr8[1] = name_len*2 + 1;
447 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
448
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200449 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000450 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000451 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200452#endif
453 name_len = (strlen(net->name_short)*7)/8;
454 name_pad = (8 - strlen(net->name_short)*7)%8;
455 if (name_pad > 0)
456 name_len++;
457 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200458 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200459 ptr8[0] = GSM48_IE_NAME_SHORT;
460 ptr8[1] = name_len +1;
461 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
462
463 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100464 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200465
Harald Weltedb253af2008-12-30 17:56:55 +0000466 }
467
Harald Weltedb253af2008-12-30 17:56:55 +0000468 /* Section 10.5.3.9 */
469 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100470 gmt_time = gmtime(&cur_t);
471
Harald Weltedb253af2008-12-30 17:56:55 +0000472 ptr8 = msgb_put(msg, 8);
473 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100474 ptr8[1] = bcdify(gmt_time->tm_year % 100);
475 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
476 ptr8[3] = bcdify(gmt_time->tm_mday);
477 ptr8[4] = bcdify(gmt_time->tm_hour);
478 ptr8[5] = bcdify(gmt_time->tm_min);
479 ptr8[6] = bcdify(gmt_time->tm_sec);
480
Neels Hofmeyr73983952016-05-10 13:29:33 +0200481 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200482 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200483 if (net->tz.hr < 0) {
484 tzunits = ((net->tz.hr/-1)*4);
485 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100486 ptr8[7] = bcdify(tzunits);
487 /* Set negative time */
488 ptr8[7] |= 0x08;
489 }
490 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200491 tzunits = net->tz.hr*4;
492 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100493 ptr8[7] = bcdify(tzunits);
494 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100495 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200496 if (net->tz.dst >= 0 && net->tz.dst <= 2)
497 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100498 }
499 else {
500 /* Need to get GSM offset and convert into 15 min units */
501 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200502#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200503 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100504 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200505#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200506 /* find timezone offset */
507 time_t utc;
508 double offsetFromUTC;
509 utc = mktime(gmt_time);
510 local_time = localtime(&cur_t);
511 offsetFromUTC = difftime(cur_t, utc);
512 if (local_time->tm_isdst)
513 offsetFromUTC += 3600.0;
514 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200515#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100516 if (tzunits < 0) {
517 tzunits = tzunits/-1;
518 ptr8[7] = bcdify(tzunits);
519 /* Flip it to negative */
520 ptr8[7] |= 0x08;
521 }
522 else
523 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100524
525 /* Does not support DST +2 */
526 if (local_time->tm_isdst)
527 dst = 1;
528 }
529
530 if (dst) {
531 ptr8 = msgb_put(msg, 3);
532 ptr8[0] = GSM48_IE_NET_DST;
533 ptr8[1] = 1;
534 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100535 }
Harald Weltedb253af2008-12-30 17:56:55 +0000536
Daniel Willmanneea93372009-08-13 03:42:07 +0200537 DEBUGP(DMM, "-> MM INFO\n");
538
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800539 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000540}
541
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100542/*! Send an Authentication Request to MS on the given subscriber connection
543 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
544 * \param[in] conn Subscriber connection to send on.
545 * \param[in] rand Random challenge token to send, must be 16 bytes long.
546 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
547 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
548 * \param[in] key_seq auth tuple's sequence number.
549 */
550int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
551 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200552{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100553 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200554 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200555 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200556
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100557 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100558 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100559 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200560
Harald Welte7984d5c2009-08-12 22:56:50 +0200561 gh->proto_discr = GSM48_PDISC_MM;
562 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
563
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100564 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200565
Harald Welte7984d5c2009-08-12 22:56:50 +0200566 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100567 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200568 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200569 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200570
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100571
572 /* 16 bytes AUTN */
573 if (autn)
574 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
575
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800576 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200577}
578
579/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800580int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200581{
582 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800583 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200584}
585
Harald Welte2483f1b2016-06-19 18:06:02 +0200586static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
587static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result);
588
589static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100590{
Harald Welte2483f1b2016-06-19 18:06:02 +0200591 uint8_t mi_type;
592 char mi_string[GSM48_MI_SIZE];
593 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100594
Harald Welte2483f1b2016-06-19 18:06:02 +0200595 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
596 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100597
Harald Welte2483f1b2016-06-19 18:06:02 +0200598 switch (mi_type) {
599 case GSM_MI_TYPE_IMSI:
600 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
601 goto accept_reuse;
602 break;
603 case GSM_MI_TYPE_TMSI:
604 tmsi = osmo_load32be(mi_lv+2);
605 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
606 goto accept_reuse;
607 break;
608 case GSM_MI_TYPE_IMEI:
609 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
610 goto accept_reuse;
611 break;
612 default:
613 break;
614 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100615
Harald Welte2483f1b2016-06-19 18:06:02 +0200616 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
617 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
618 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_ILLEGAL_SUBSCR);
619 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100620
Harald Welte2483f1b2016-06-19 18:06:02 +0200621accept_reuse:
622 DEBUGP(DMM, "%s: re-using already accepted connection\n",
623 vlr_subscr_name(conn->vsub));
624 conn->received_cm_service_request = true;
625 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100626}
627
Harald Welte4ed0e922009-01-10 03:17:30 +0000628/*
629 * Handle CM Service Requests
630 * a) Verify that the packet is long enough to contain the information
631 * we require otherwsie reject with INCORRECT_MESSAGE
632 * b) Try to parse the TMSI. If we do not have one reject
633 * c) Check that we know the subscriber with the TMSI otherwise reject
634 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200635 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200636 *
637 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000638 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200639int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000640{
Harald Welte2483f1b2016-06-19 18:06:02 +0200641 static const enum subscr_conn_from conn_from_cm_service_req =
642 SUBSCR_CONN_FROM_CM_SERVICE_REQ;
643 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200644 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200645 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000646
Harald Welteba4cf162009-01-10 01:49:35 +0000647 struct gsm48_hdr *gh = msgb_l3(msg);
648 struct gsm48_service_request *req =
649 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800650 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200651 uint8_t classmark2_len = gh->data[1];
652 uint8_t *classmark2 = gh->data+2;
653 uint8_t mi_len = *(classmark2 + classmark2_len);
654 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welte2483f1b2016-06-19 18:06:02 +0200655 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200656 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200657 int rc;
658
659 lai.plmn.mcc = conn->network->country_code;
660 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200661 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000662
Harald Weltec9e02182009-05-01 19:07:53 +0000663 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000664 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000665 DEBUGPC(DMM, "wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200666 return msc_gsm48_tx_mm_serv_rej(conn,
667 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000668 }
669
670 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000671 DEBUGPC(DMM, "does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200672 return msc_gsm48_tx_mm_serv_rej(conn,
673 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000674 }
675
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200676 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000677 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200678
679 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200680 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
681 req->cm_service_type, gsm48_mi_type_name(mi_type),
682 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200683 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200684 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
685 req->cm_service_type, gsm48_mi_type_name(mi_type),
686 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200687 } else {
688 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200689 return msc_gsm48_tx_mm_serv_rej(conn,
690 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000691 }
692
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200693 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte2483f1b2016-06-19 18:06:02 +0200694 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
695 conn->classmark.classmark2_len = classmark2_len;
696
697 if (conn->conn_fsm) {
698 if (msc_subscr_conn_is_accepted(conn))
699 return cm_serv_reuse_conn(conn, mi-1);
700 LOGP(DMM, LOGL_ERROR, "%s: connection already in use\n",
701 vlr_subscr_name(conn->vsub));
702 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
703 return -EINVAL;
704 }
705
706 rc = msc_create_conn_fsm(conn, mi_string);
707 if (rc) {
708 msc_vlr_tx_cm_serv_rej(conn, VLR_PR_ARQ_RES_UNKNOWN_ERROR);
709 /* logging already happened in msc_create_conn_fsm() */
710 return rc;
711 }
Harald Welte7659de12009-12-13 12:39:18 +0100712
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200713 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +0200714 vlr_proc_acc_req(conn->conn_fsm,
715 SUBSCR_CONN_E_ACCEPTED,
716 SUBSCR_CONN_E_CN_CLOSE,
717 (void*)&conn_from_cm_service_req,
718 net->vlr, conn,
719 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200720 is_utran || conn->network->authentication_required,
721 is_utran? VLR_CIPH_A5_3
722 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +0200723 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200724 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000725
Harald Welte2483f1b2016-06-19 18:06:02 +0200726 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000727}
728
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200729static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000730{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200731 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000732 struct gsm48_hdr *gh = msgb_l3(msg);
733 struct gsm48_imsi_detach_ind *idi =
734 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200735 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200736 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200737 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000738
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200739 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200740 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
741 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000742
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200743 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100744
Harald Welte2a139372009-02-22 21:14:55 +0000745 switch (mi_type) {
746 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200747 vsub = vlr_subscr_find_by_tmsi(network->vlr,
748 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000749 break;
750 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200751 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000752 break;
753 case GSM_MI_TYPE_IMEI:
754 case GSM_MI_TYPE_IMEISV:
755 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200756 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
757 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000758 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200759 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200760 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
761 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000762 break;
763 }
764
Harald Welte2483f1b2016-06-19 18:06:02 +0200765 /* TODO? We used to remember the subscriber's classmark1 here and
766 * stored it in the old sqlite db, but now we store it in a conn that
767 * will be discarded anyway: */
768 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200769
Harald Welte2483f1b2016-06-19 18:06:02 +0200770 if (!vsub) {
771 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
772 gsm48_mi_type_name(mi_type), mi_string);
773 } else {
774 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
775 vlr_subscr_rx_imsi_detach(vsub);
776 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
777 vlr_subscr_put(vsub);
778 }
Harald Welte2a139372009-02-22 21:14:55 +0000779
Harald Welte2483f1b2016-06-19 18:06:02 +0200780 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000781 return 0;
782}
783
Harald Welted2a7f5a2009-06-05 20:08:20 +0000784static int gsm48_rx_mm_status(struct msgb *msg)
785{
786 struct gsm48_hdr *gh = msgb_l3(msg);
787
788 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
789
790 return 0;
791}
792
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100793static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
794 struct gsm_subscriber_connection *conn,
795 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100796{
797 struct gsm48_hdr *gh = msgb_l3(msg);
798 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100799
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100800 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
801 LOGP(DMM, LOGL_ERROR,
802 "%s: MM AUTHENTICATION RESPONSE:"
803 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200804 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100805 return -EINVAL;
806 }
807
808 *res_len = sizeof(ar->sres);
809 memcpy(res, ar->sres, sizeof(ar->sres));
810 return 0;
811}
812
813static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
814 struct gsm_subscriber_connection *conn,
815 struct msgb *msg)
816{
817 struct gsm48_hdr *gh;
818 uint8_t *data;
819 uint8_t iei;
820 uint8_t ie_len;
821 unsigned int data_len;
822
823 /* First parse the GSM part */
824 if (parse_gsm_auth_resp(res, res_len, conn, msg))
825 return -EINVAL;
826 OSMO_ASSERT(*res_len == 4);
827
828 /* Then add the extended res part */
829 gh = msgb_l3(msg);
830 data = gh->data + sizeof(struct gsm48_auth_resp);
831 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
832
833 if (data_len < 3) {
834 LOGP(DMM, LOGL_ERROR,
835 "%s: MM AUTHENTICATION RESPONSE:"
836 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200837 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100838 return -EINVAL;
839 }
840
841 iei = data[0];
842 ie_len = data[1];
843 if (iei != GSM48_IE_AUTH_RES_EXT) {
844 LOGP(DMM, LOGL_ERROR,
845 "%s: MM R99 AUTHENTICATION RESPONSE:"
846 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200847 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100848 GSM48_IE_AUTH_RES_EXT, iei);
849 return -EINVAL;
850 }
851
852 if (ie_len > 12) {
853 LOGP(DMM, LOGL_ERROR,
854 "%s: MM R99 AUTHENTICATION RESPONSE:"
855 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200856 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100857 return -EINVAL;
858 }
859
860 *res_len += ie_len;
861 memcpy(res + 4, &data[2], ie_len);
862 return 0;
863}
864
865/* Chapter 9.2.3: Authentication Response */
866static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
867{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100868 uint8_t res[16];
869 uint8_t res_len;
870 int rc;
871 bool is_r99;
872
Harald Welte2483f1b2016-06-19 18:06:02 +0200873 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100874 LOGP(DMM, LOGL_ERROR,
875 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200876 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100877 return -EINVAL;
878 }
879
880 if (msgb_l3len(msg) >
881 sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp)) {
882 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
883 is_r99 = true;
884 } else {
885 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
886 is_r99 = false;
887 }
888
889 if (rc) {
Harald Welte2483f1b2016-06-19 18:06:02 +0200890 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100891 return -EINVAL;
892 }
893
894 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200895 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100896 is_r99 ? "R99" : "GSM", is_r99 ? "res" : "sres",
897 osmo_hexdump_nospc(res, res_len));
898
Harald Welte2483f1b2016-06-19 18:06:02 +0200899 return vlr_subscr_rx_auth_resp(conn->vsub, is_r99,
900 conn->via_ran == RAN_UTRAN_IU,
901 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100902}
903
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100904static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
905{
906 struct gsm48_hdr *gh = msgb_l3(msg);
907 uint8_t cause;
908 uint8_t auts_tag;
909 uint8_t auts_len;
910 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100911
Harald Welte2483f1b2016-06-19 18:06:02 +0200912 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100913 LOGP(DMM, LOGL_ERROR,
914 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200915 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100916 return -EINVAL;
917 }
918
919 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
920 LOGP(DMM, LOGL_ERROR,
921 "%s: MM R99 AUTHENTICATION FAILURE:"
922 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200923 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
924 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100925 return -EINVAL;
926 }
927
928 cause = gh->data[0];
929
930 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
931 LOGP(DMM, LOGL_INFO,
932 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200933 vlr_subscr_name(conn->vsub), cause);
934 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
935 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100936 }
937
938 /* This is a Synch Failure procedure, which should pass an AUTS to
939 * resynchronize the sequence nr with the HLR. Expecting exactly one
940 * TLV with 14 bytes of AUTS. */
941
942 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
943 LOGP(DMM, LOGL_INFO,
944 "%s: MM R99 AUTHENTICATION FAILURE:"
945 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200946 vlr_subscr_name(conn->vsub));
947 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100948 return -EINVAL;
949 }
950
951 auts_tag = gh->data[1];
952 auts_len = gh->data[2];
953 auts = &gh->data[3];
954
955 if (auts_tag != GSM48_IE_AUTS
956 || auts_len != 14) {
957 LOGP(DMM, LOGL_INFO,
958 "%s: MM R99 AUTHENTICATION FAILURE:"
959 " invalid Synch Failure:"
960 " expected AUTS IE 0x%02x of 14 bytes,"
961 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200962 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100963 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200964 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100965 return -EINVAL;
966 }
967
968 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
969 LOGP(DMM, LOGL_INFO,
970 "%s: MM R99 AUTHENTICATION FAILURE:"
971 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200972 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
973 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100974 return -EINVAL;
975 }
976
977 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
978 * large enough. */
979
980 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200981 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100982
Harald Welte2483f1b2016-06-19 18:06:02 +0200983 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
984}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100985
Harald Welte2483f1b2016-06-19 18:06:02 +0200986static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
987{
988 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
989 vlr_subscr_name(conn->vsub));
990 if (!conn->vsub) {
991 LOGP(DMM, LOGL_ERROR,
992 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
993 return -EINVAL;
994 }
995 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100996}
997
Harald Weltebf5e8df2009-02-03 12:59:45 +0000998/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800999static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001000{
1001 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001002 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001003
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001004 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001005 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001006 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001007 break;
1008 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001009 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001010 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001011 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001012 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001013 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001014 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001015 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001016 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001017 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001018 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001019 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001020 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001021 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001022 break;
1023 case GSM48_MT_MM_CM_REEST_REQ:
1024 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1025 break;
1026 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001027 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001028 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001029 case GSM48_MT_MM_AUTH_FAIL:
1030 rc = gsm48_rx_mm_auth_fail(conn, msg);
1031 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001032 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001033 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001034 gh->msg_type);
1035 break;
1036 }
1037
1038 return rc;
1039}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001040
Harald Welte2483f1b2016-06-19 18:06:02 +02001041static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1042{
1043 /* Check the size for the classmark */
1044 if (tot_len < 1 + *classmark2_lv)
1045 return NULL;
1046
1047 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1048 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1049 return NULL;
1050
1051 return mi_lv;
1052}
1053
Harald Welte2d35ae62009-02-06 12:02:13 +00001054/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001055static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001056{
Harald Welte2483f1b2016-06-19 18:06:02 +02001057 static const enum subscr_conn_from conn_from_paging_resp =
1058 SUBSCR_CONN_FROM_PAGING_RESP;
1059 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001060 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001061 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001062 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001063 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001064 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001065 char mi_string[GSM48_MI_SIZE];
Harald Welte2d35ae62009-02-06 12:02:13 +00001066 int rc = 0;
Harald Welte2483f1b2016-06-19 18:06:02 +02001067 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001068 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001069
1070 lai.plmn.mcc = conn->network->country_code;
1071 lai.plmn.mnc = conn->network->network_code;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001072 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001073
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001074 resp = (struct gsm48_pag_resp *) &gh->data[0];
1075 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1076 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001077 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1078 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001079
Harald Welte2483f1b2016-06-19 18:06:02 +02001080 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1081 if (!mi_lv) {
1082 /* FIXME */
1083 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001084 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001085
Harald Welte2483f1b2016-06-19 18:06:02 +02001086 rc = msc_create_conn_fsm(conn, mi_string);
1087 if (rc)
1088 /* logging already happened in msc_create_conn_fsm() */
1089 return rc;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001090
Harald Welte2483f1b2016-06-19 18:06:02 +02001091 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1092 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001093
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001094 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Harald Welte2483f1b2016-06-19 18:06:02 +02001095 vlr_proc_acc_req(conn->conn_fsm,
1096 SUBSCR_CONN_E_ACCEPTED,
1097 SUBSCR_CONN_E_CN_CLOSE,
1098 (void*)&conn_from_paging_resp,
1099 net->vlr, conn,
1100 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001101 is_utran || conn->network->authentication_required,
1102 is_utran? VLR_CIPH_A5_3
1103 : conn->network->a5_encryption,
Harald Welte2483f1b2016-06-19 18:06:02 +02001104 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001105 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001106
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001107 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001108}
1109
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001110static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001111{
1112 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001113 uint8_t apdu_id_flags;
1114 uint8_t apdu_len;
1115 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001116
1117 apdu_id_flags = gh->data[0];
1118 apdu_len = gh->data[1];
1119 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001120
Max8dc8f232017-02-09 19:13:02 +01001121 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001122 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001123
Harald Welte2483f1b2016-06-19 18:06:02 +02001124 /* we're not using the app info blob anywhere, so ignore. */
1125#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001126 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001127#else
1128 return 0;
1129#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001130}
1131
Harald Weltebf5e8df2009-02-03 12:59:45 +00001132/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001133static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001134{
1135 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001136 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001137
1138 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001139 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001140 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001141 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001142 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001143 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001144 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001145 default:
Philippd37981e2016-10-10 15:11:41 +02001146 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1147 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001148 break;
1149 }
1150
Harald Welte2d35ae62009-02-06 12:02:13 +00001151 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001152}
1153
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001154int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1155 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001156{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001157 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001158 struct gsm48_hdr *gh;
1159
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001160 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1161 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001162
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001163 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1164 gh->proto_discr = GSM48_PDISC_RR;
1165 gh->msg_type = GSM48_MT_RR_APP_INFO;
1166 gh->data[0] = apdu_id;
1167 gh->data[1] = apdu_len;
1168 memcpy(gh->data+2, apdu, apdu_len);
1169
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001170 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001171}
1172
Alexander Couzens92f552f2016-08-23 07:32:27 +02001173/* FIXME: this count_statistics is a state machine behaviour. we should convert
1174 * the complete call control into a state machine. Afterwards we can move this
1175 * code into state transitions.
1176 */
1177static void count_statistics(struct gsm_trans *trans, int new_state)
1178{
1179 int old_state = trans->cc.state;
1180 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1181
1182 if (old_state == new_state)
1183 return;
1184
1185 /* state incoming */
1186 switch (new_state) {
1187 case GSM_CSTATE_ACTIVE:
1188 osmo_counter_inc(trans->net->active_calls);
1189 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1190 break;
1191 }
1192
1193 /* state outgoing */
1194 switch (old_state) {
1195 case GSM_CSTATE_ACTIVE:
1196 osmo_counter_dec(trans->net->active_calls);
1197 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1198 new_state == GSM_CSTATE_DISCONNECT_IND)
1199 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1200 else
1201 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1202 break;
1203 }
1204}
1205
Harald Welte4bc90a12008-12-27 16:32:52 +00001206/* Call Control */
1207
Harald Welte7584aea2009-02-11 11:44:12 +00001208/* The entire call control code is written in accordance with Figure 7.10c
1209 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1210 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1211 * it for voice */
1212
Harald Welte4bfdfe72009-06-10 23:11:52 +08001213static void new_cc_state(struct gsm_trans *trans, int state)
1214{
1215 if (state > 31 || state < 0)
1216 return;
1217
1218 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001219 gsm48_cc_state_name(trans->cc.state),
1220 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001221
Alexander Couzens92f552f2016-08-23 07:32:27 +02001222 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001223 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001224}
1225
1226static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001227{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001228 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001229 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001230 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001231
Harald Welte4bc90a12008-12-27 16:32:52 +00001232 gh->msg_type = GSM48_MT_CC_STATUS;
1233
1234 cause = msgb_put(msg, 3);
1235 cause[0] = 2;
1236 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1237 cause[2] = 0x80 | 30; /* response to status inquiry */
1238
1239 call_state = msgb_put(msg, 1);
1240 call_state[0] = 0xc0 | 0x00;
1241
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001242 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001243}
1244
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001245static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001246 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001247{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001248 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001249 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1250
Harald Welte6f4b7532008-12-29 00:39:37 +00001251 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001252 gh->msg_type = msg_type;
1253
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001254 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001255}
1256
Harald Welte4bfdfe72009-06-10 23:11:52 +08001257static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1258{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001259 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001260 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001261 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001262 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001263 }
1264}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001265
Harald Welte4bfdfe72009-06-10 23:11:52 +08001266static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1267 int msg_type, struct gsm_mncc *mncc)
1268{
1269 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001270 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001271
Philipp Maierfbf66102017-04-09 12:32:51 +02001272 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1273
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001274#if BEFORE_MSCSPLIT
1275 /* Re-enable this log output once we can obtain this information via
1276 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001277 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001278 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001279 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001280 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001281 trans->conn->lchan->ts->trx->bts->nr,
1282 trans->conn->lchan->ts->trx->nr,
1283 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001284 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285 get_mncc_name(msg_type));
1286 else
1287 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1288 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001289 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290 get_mncc_name(msg_type));
1291 else
1292 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1293 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001294#else
1295 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1296#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297
1298 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001299
Harald Welte966636f2009-06-26 19:39:35 +02001300 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001301 if (!msg)
1302 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001303
1304 data = msgb_put(msg, sizeof(struct gsm_mncc));
1305 memcpy(data, mncc, sizeof(struct gsm_mncc));
1306
Harald Welte54209c22010-12-22 23:43:29 +01001307 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308
1309 return 0;
1310}
1311
1312int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001313 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314{
1315 struct gsm_mncc rel;
1316
Harald Welte92f70c52009-06-12 01:54:08 +08001317 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001319 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001320 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1321 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001322 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1323}
1324
Harald Weltedcaf5652009-07-23 18:56:43 +02001325/* Call Control Specific transaction release.
1326 * gets called by trans_free, DO NOT CALL YOURSELF! */
1327void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329 gsm48_stop_cc_timer(trans);
1330
Philipp Maierfbf66102017-04-09 12:32:51 +02001331 /* Make sure call also gets released on the mgcp side */
1332 msc_call_release(trans);
1333
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334 /* send release to L4, if callref still exists */
1335 if (trans->callref) {
1336 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001337 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001338 GSM48_CAUSE_LOC_PRN_S_LU,
1339 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001341 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343}
1344
1345static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001346
Harald Welte09e38af2009-02-16 22:52:23 +00001347/* call-back from paging the B-end of the connection */
1348static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001349 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001350{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001351 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001352 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001353
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001354 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001355
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001356 switch (event) {
1357 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001358 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1359 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001360 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001361 /* Assign conn */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001362 transt->conn = conn;
1363 /* send SETUP request to called party */
1364 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1365 break;
1366 case GSM_PAGING_EXPIRED:
1367 case GSM_PAGING_BUSY:
1368 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001369 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001370 /* Temporarily out of order */
1371 mncc_release_ind(transt->net, transt,
1372 transt->callref,
1373 GSM48_CAUSE_LOC_PRN_S_LU,
1374 GSM48_CC_CAUSE_DEST_OOO);
1375 transt->callref = 0;
1376 transt->paging_request = NULL;
1377 trans_free(transt);
1378 break;
1379 default:
1380 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1381 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001383
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001384 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001385 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001386}
Harald Welte7584aea2009-02-11 11:44:12 +00001387
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001389static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001390{
Harald Welte53d51f52015-12-03 14:59:04 +01001391 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1392 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001393
Harald Welte4bfdfe72009-06-10 23:11:52 +08001394 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001395 return -EIO;
1396
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001397 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398 return -EIO;
1399
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001400 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001401 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001402
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001403 return msc_call_bridge(trans1, trans2);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404}
1405
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1407{
1408 DEBUGP(DCC, "-> STATUS ENQ\n");
1409 return gsm48_cc_tx_status(trans, msg);
1410}
1411
1412static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1413static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1414
1415static void gsm48_cc_timeout(void *arg)
1416{
1417 struct gsm_trans *trans = arg;
1418 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001419 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1420 int mo_location = GSM48_CAUSE_LOC_USER;
1421 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1422 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001423 struct gsm_mncc mo_rel, l4_rel;
1424
1425 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1426 mo_rel.callref = trans->callref;
1427 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1428 l4_rel.callref = trans->callref;
1429
Harald Weltedcaf5652009-07-23 18:56:43 +02001430 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001431 case 0x303:
1432 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001433 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001434 break;
1435 case 0x310:
1436 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001437 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001438 break;
1439 case 0x313:
1440 disconnect = 1;
1441 /* unknown, did not find it in the specs */
1442 break;
1443 case 0x301:
1444 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001445 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001446 break;
1447 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001448 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001449 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001450 gsm48_cc_tx_release(trans, &trans->cc.msg);
1451 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001452 break; /* stay in release state */
1453 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001454 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001455 return;
1456// release = 1;
1457// l4_cause = 14;
1458// break;
1459 case 0x306:
1460 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001461 mo_cause = trans->cc.msg.cause.value;
1462 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001463 break;
1464 case 0x323:
1465 disconnect = 1;
1466 break;
1467 default:
1468 release = 1;
1469 }
1470
1471 if (release && trans->callref) {
1472 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001473 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001474 l4_location, l4_cause);
1475 trans->callref = 0;
1476 }
1477
1478 if (disconnect && trans->callref) {
1479 /* process disconnect towards layer 4 */
1480 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001481 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001482 }
1483
1484 /* process disconnect towards mobile station */
1485 if (disconnect || release) {
1486 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001487 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1488 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1489 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001490 mo_rel.cause.diag_len = 3;
1491
1492 if (disconnect)
1493 gsm48_cc_tx_disconnect(trans, &mo_rel);
1494 if (release)
1495 gsm48_cc_tx_release(trans, &mo_rel);
1496 }
1497
1498}
1499
Max3ffce192016-04-25 15:22:00 +02001500/* disconnect both calls from the bridge */
1501static inline void disconnect_bridge(struct gsm_network *net,
1502 struct gsm_mncc_bridge *bridge, int err)
1503{
1504 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1505 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1506 struct gsm_mncc mx_rel;
1507 if (!trans0 || !trans1)
1508 return;
1509
1510 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1511 trans0->callref, trans1->callref, strerror(err));
1512
1513 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1514 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1515 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1516
1517 mx_rel.callref = trans0->callref;
1518 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1519
1520 mx_rel.callref = trans1->callref;
1521 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1522}
1523
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1525 int sec, int micro)
1526{
1527 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001528 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001529 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001530 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001531}
1532
1533static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1534{
1535 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001536 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001537 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1538 struct tlv_parsed tp;
1539 struct gsm_mncc setup;
1540
1541 memset(&setup, 0, sizeof(struct gsm_mncc));
1542 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001543
Harald Welte474d19f2010-03-02 23:18:30 +01001544 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001545 /* emergency setup is identified by msg_type */
1546 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1547 setup.emergency = 1;
1548
1549 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001550 setup.fields |= MNCC_F_CALLING;
Harald Welte2483f1b2016-06-19 18:06:02 +02001551 osmo_strlcpy(setup.calling.number, trans->vsub->msisdn, sizeof(setup.calling.number));
1552 osmo_strlcpy(setup.imsi, trans->vsub->imsi, sizeof(setup.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001553
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 /* bearer capability */
1555 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1556 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001557 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001558 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001559
1560 /* Create a copy of the bearer capability
1561 * in the transaction struct, so we can use
1562 * this information later */
1563 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1564 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001565 }
1566 /* facility */
1567 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1568 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001569 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001570 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1571 }
1572 /* called party bcd number */
1573 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1574 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001575 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001576 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1577 }
1578 /* user-user */
1579 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1580 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001581 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001582 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1583 }
1584 /* ss-version */
1585 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1586 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001587 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001588 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1589 }
1590 /* CLIR suppression */
1591 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1592 setup.clir.sup = 1;
1593 /* CLIR invocation */
1594 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1595 setup.clir.inv = 1;
1596 /* cc cap */
1597 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1598 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001599 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001600 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1601 }
1602
Harald Welte4bfdfe72009-06-10 23:11:52 +08001603 new_cc_state(trans, GSM_CSTATE_INITIATED);
1604
Harald Welte (local)47df3992009-12-26 19:45:03 +01001605 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001606 vlr_subscr_name(trans->vsub), trans->vsub->msisdn,
Harald Welte (local)47df3992009-12-26 19:45:03 +01001607 setup.called.number);
1608
Alexander Couzensb847a212016-08-02 11:34:11 +02001609 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001610
Harald Welte4bfdfe72009-06-10 23:11:52 +08001611 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001612 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001613
Harald Welte13cac662009-07-29 12:10:35 +02001614 /* MNCC code will modify the channel asynchronously, we should
1615 * ipaccess-bind only after the modification has been made to the
1616 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617 return 0;
1618}
1619
1620static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001621{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001622 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001623 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001625 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001626
Harald Welte7ccf7782009-02-17 01:43:01 +00001627 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001628
Harald Welte4bfdfe72009-06-10 23:11:52 +08001629 /* transaction id must not be assigned */
1630 if (trans->transaction_id != 0xff) { /* unasssigned */
1631 DEBUGP(DCC, "TX Setup with assigned transaction. "
1632 "This is not allowed!\n");
1633 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001634 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001635 GSM48_CAUSE_LOC_PRN_S_LU,
1636 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001638 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001639 return rc;
1640 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001643 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001644 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001645 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001647 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001648 GSM48_CAUSE_LOC_PRN_S_LU,
1649 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001651 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652 return rc;
1653 }
Harald Welte78283ef2009-07-23 21:36:44 +02001654 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001655
Harald Welte65e74cc2008-12-29 01:55:35 +00001656 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001657
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001659
Harald Welte4bfdfe72009-06-10 23:11:52 +08001660 /* bearer capability */
1661 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001662 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 /* facility */
1664 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001665 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 /* progress */
1667 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001668 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 /* calling party BCD number */
1670 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001671 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001672 /* called party BCD number */
1673 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001674 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001675 /* user-user */
1676 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001677 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 /* redirecting party BCD number */
1679 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001680 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001681 /* signal */
1682 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001683 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001684
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001686
Alexander Couzensb847a212016-08-02 11:34:11 +02001687 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001688
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001689 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001690}
1691
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1693{
1694 struct gsm48_hdr *gh = msgb_l3(msg);
1695 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1696 struct tlv_parsed tp;
1697 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001698 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699
1700 gsm48_stop_cc_timer(trans);
1701 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1702
1703 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1704 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001705
Harald Welte474d19f2010-03-02 23:18:30 +01001706 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707#if 0
1708 /* repeat */
1709 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1710 call_conf.repeat = 1;
1711 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1712 call_conf.repeat = 2;
1713#endif
1714 /* bearer capability */
1715 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1716 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001717 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001719
1720 /* Create a copy of the bearer capability
1721 * in the transaction struct, so we can use
1722 * this information later */
1723 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1724 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725 }
1726 /* cause */
1727 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1728 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001729 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1731 }
1732 /* cc cap */
1733 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1734 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001735 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1737 }
1738
Andreas Eversberg035b8742013-09-19 09:27:06 +02001739 /* IMSI of called subscriber */
Harald Welte2483f1b2016-06-19 18:06:02 +02001740 osmo_strlcpy(call_conf.imsi, trans->vsub->imsi, sizeof(call_conf.imsi));
Andreas Eversberg035b8742013-09-19 09:27:06 +02001741
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1743
Philipp Maierfbf66102017-04-09 12:32:51 +02001744 /* Assign call (if not done yet) */
1745 if (trans->assignment_done == false) {
1746 rc = msc_call_assignment(trans);
1747 trans->assignment_done = true;
1748 }
1749 else
1750 rc = 0;
1751
1752 /* don't continue, if there were problems with
1753 * the call assignment. */
1754 if (rc)
1755 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001756
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001757 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001758 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759}
1760
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001761static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762{
1763 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001764 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001766 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1769
1770 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1771
1772 /* bearer capability */
1773 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001774 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 /* facility */
1776 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001777 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 /* progress */
1779 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001780 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001782 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1783 if (rc)
1784 return rc;
1785
Philipp Maierfbf66102017-04-09 12:32:51 +02001786 /* Assign call (if not done yet) */
1787 if (trans->assignment_done == false) {
1788 rc = msc_call_assignment(trans);
1789 trans->assignment_done = true;
1790 }
1791 else
1792 rc = 0;
1793
1794 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795}
1796
1797static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1798{
1799 struct gsm48_hdr *gh = msgb_l3(msg);
1800 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1801 struct tlv_parsed tp;
1802 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001803
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804 gsm48_stop_cc_timer(trans);
1805 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1806
1807 memset(&alerting, 0, sizeof(struct gsm_mncc));
1808 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001809 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 /* facility */
1811 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1812 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001813 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1815 }
1816
1817 /* progress */
1818 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1819 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001820 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1822 }
1823 /* ss-version */
1824 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1825 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001826 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1828 }
1829
1830 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1831
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001832 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001833 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834}
1835
1836static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1837{
1838 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001839 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001840 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1841
Harald Welte4bfdfe72009-06-10 23:11:52 +08001842 gh->msg_type = GSM48_MT_CC_ALERTING;
1843
1844 /* facility */
1845 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001846 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 /* progress */
1848 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001849 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 /* user-user */
1851 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001852 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853
1854 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001855
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001856 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857}
1858
1859static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1860{
1861 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001862 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1864
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 gh->msg_type = GSM48_MT_CC_PROGRESS;
1866
1867 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001868 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 /* user-user */
1870 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001873 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874}
1875
1876static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1877{
1878 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001879 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1881
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 gh->msg_type = GSM48_MT_CC_CONNECT;
1883
1884 gsm48_stop_cc_timer(trans);
1885 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1886
1887 /* facility */
1888 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 /* progress */
1891 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 /* connected number */
1894 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001895 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 /* user-user */
1897 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899
1900 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1901
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001902 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903}
1904
1905static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1906{
1907 struct gsm48_hdr *gh = msgb_l3(msg);
1908 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1909 struct tlv_parsed tp;
1910 struct gsm_mncc connect;
1911
1912 gsm48_stop_cc_timer(trans);
1913
1914 memset(&connect, 0, sizeof(struct gsm_mncc));
1915 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001916 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001918 connect.fields |= MNCC_F_CONNECTED;
Harald Welte2483f1b2016-06-19 18:06:02 +02001919 osmo_strlcpy(connect.connected.number, trans->vsub->msisdn, sizeof(connect.connected.number));
1920 osmo_strlcpy(connect.imsi, trans->vsub->imsi, sizeof(connect.imsi));
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001921
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 /* facility */
1923 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1924 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001925 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001926 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1927 }
1928 /* user-user */
1929 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1930 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001931 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1933 }
1934 /* ss-version */
1935 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1936 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001937 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001938 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1939 }
1940
1941 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02001942 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001944 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945}
1946
1947
1948static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
1949{
1950 struct gsm_mncc connect_ack;
1951
1952 gsm48_stop_cc_timer(trans);
1953
1954 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02001955 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001956
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
1958 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01001959
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001960 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 &connect_ack);
1962}
1963
1964static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
1965{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001966 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1968
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
1970
1971 new_cc_state(trans, GSM_CSTATE_ACTIVE);
1972
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001973 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974}
1975
1976static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
1977{
1978 struct gsm48_hdr *gh = msgb_l3(msg);
1979 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1980 struct tlv_parsed tp;
1981 struct gsm_mncc disc;
1982
1983 gsm48_stop_cc_timer(trans);
1984
1985 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
1986
1987 memset(&disc, 0, sizeof(struct gsm_mncc));
1988 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001989 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990 /* cause */
1991 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1992 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001993 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1995 }
1996 /* facility */
1997 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1998 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2001 }
2002 /* user-user */
2003 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2004 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002005 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2007 }
2008 /* ss-version */
2009 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2010 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002011 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2013 }
2014
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002015 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016
2017}
2018
Harald Weltec66b71c2009-06-11 14:23:20 +08002019static struct gsm_mncc_cause default_cause = {
2020 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2021 .coding = 0,
2022 .rec = 0,
2023 .rec_val = 0,
2024 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2025 .diag_len = 0,
2026 .diag = { 0 },
2027};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028
2029static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2030{
2031 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002032 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002033 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2034
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2036
2037 gsm48_stop_cc_timer(trans);
2038 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2039
2040 /* cause */
2041 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 else
Harald Welte55c8f352010-03-07 23:40:35 +01002044 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045
2046 /* facility */
2047 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 /* progress */
2050 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* user-user */
2053 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055
2056 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002057 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058
2059 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2060
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002061 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002062}
2063
2064static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2065{
2066 struct gsm48_hdr *gh = msgb_l3(msg);
2067 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2068 struct tlv_parsed tp;
2069 struct gsm_mncc rel;
2070 int rc;
2071
2072 gsm48_stop_cc_timer(trans);
2073
2074 memset(&rel, 0, sizeof(struct gsm_mncc));
2075 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002076 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077 /* cause */
2078 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2079 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002080 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2082 }
2083 /* facility */
2084 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2085 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002086 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2088 }
2089 /* user-user */
2090 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2091 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002092 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002093 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2094 }
2095 /* ss-version */
2096 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2097 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002098 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2100 }
2101
Harald Weltedcaf5652009-07-23 18:56:43 +02002102 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002104 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002105 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002106 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002107 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002108 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002109 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 }
2111
2112 new_cc_state(trans, GSM_CSTATE_NULL);
2113
2114 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002115 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116
2117 return rc;
2118}
2119
2120static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2121{
2122 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002123 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2125
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 gh->msg_type = GSM48_MT_CC_RELEASE;
2127
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 gsm48_stop_cc_timer(trans);
2129 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2130
2131 /* cause */
2132 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002133 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 /* facility */
2135 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002136 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 /* user-user */
2138 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002139 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140
Harald Weltedcaf5652009-07-23 18:56:43 +02002141 trans->cc.T308_second = 0;
2142 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143
Harald Weltedcaf5652009-07-23 18:56:43 +02002144 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2146
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_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2151{
2152 struct gsm48_hdr *gh = msgb_l3(msg);
2153 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2154 struct tlv_parsed tp;
2155 struct gsm_mncc rel;
2156 int rc = 0;
2157
2158 gsm48_stop_cc_timer(trans);
2159
2160 memset(&rel, 0, sizeof(struct gsm_mncc));
2161 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002162 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 /* cause */
2164 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2165 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002166 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2168 }
2169 /* facility */
2170 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2171 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002172 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2174 }
2175 /* user-user */
2176 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2177 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002178 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2180 }
2181 /* ss-version */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2183 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002184 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2186 }
2187
2188 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002189 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002191 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192 MNCC_REJ_IND, &rel);
2193 break;
2194 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002195 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 MNCC_REL_CNF, &rel);
2197 break;
2198 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002199 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 MNCC_REL_IND, &rel);
2201 }
2202 }
2203
2204 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002205 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206
2207 return rc;
2208}
2209
2210static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2211{
2212 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002213 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002215 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2218
2219 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002220
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221 gsm48_stop_cc_timer(trans);
2222
2223 /* cause */
2224 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002225 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226 /* facility */
2227 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 /* user-user */
2230 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002231 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232
Harald Weltec7782de2010-12-21 19:31:41 +01002233 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2234
Harald Weltedcaf5652009-07-23 18:56:43 +02002235 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236
Harald Weltec7782de2010-12-21 19:31:41 +01002237 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238}
2239
2240static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2241{
2242 struct gsm48_hdr *gh = msgb_l3(msg);
2243 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2244 struct tlv_parsed tp;
2245 struct gsm_mncc fac;
2246
2247 memset(&fac, 0, sizeof(struct gsm_mncc));
2248 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002249 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250 /* facility */
2251 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2252 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002253 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002254 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2255 }
2256 /* ss-version */
2257 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2258 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2261 }
2262
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002263 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264}
2265
2266static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2267{
2268 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002269 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2271
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 gh->msg_type = GSM48_MT_CC_FACILITY;
2273
2274 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002275 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002277 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278}
2279
2280static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2281{
2282 struct gsm_mncc hold;
2283
2284 memset(&hold, 0, sizeof(struct gsm_mncc));
2285 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002286 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287}
2288
2289static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2290{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002291 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2293
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2295
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002296 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297}
2298
2299static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2300{
2301 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002302 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2304
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2306
2307 /* cause */
2308 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 else
Harald Welte55c8f352010-03-07 23:40:35 +01002311 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002313 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314}
2315
2316static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2317{
2318 struct gsm_mncc retrieve;
2319
2320 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2321 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002322 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002323 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324}
2325
2326static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2327{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002328 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2330
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2332
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002333 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334}
2335
2336static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2337{
2338 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002339 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2341
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2343
2344 /* cause */
2345 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002346 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 else
Harald Welte55c8f352010-03-07 23:40:35 +01002348 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002350 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351}
2352
2353static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2354{
2355 struct gsm48_hdr *gh = msgb_l3(msg);
2356 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2357 struct tlv_parsed tp;
2358 struct gsm_mncc dtmf;
2359
2360 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2361 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002362 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002363 /* keypad facility */
2364 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2365 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002366 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2368 }
2369
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002370 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371}
2372
2373static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2374{
2375 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002376 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2378
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2380
2381 /* keypad */
2382 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002383 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002385 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386}
2387
2388static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2389{
2390 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002391 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2393
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2395
2396 /* cause */
2397 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002398 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399 else
Harald Welte55c8f352010-03-07 23:40:35 +01002400 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002402 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403}
2404
2405static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2406{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002407 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2409
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2411
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002412 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413}
2414
2415static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2416{
2417 struct gsm_mncc dtmf;
2418
2419 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2420 dtmf.callref = trans->callref;
2421
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002422 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423}
2424
2425static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2426{
2427 struct gsm48_hdr *gh = msgb_l3(msg);
2428 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2429 struct tlv_parsed tp;
2430 struct gsm_mncc modify;
2431
2432 memset(&modify, 0, sizeof(struct gsm_mncc));
2433 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002434 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 /* bearer capability */
2436 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2437 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002440
2441 /* Create a copy of the bearer capability
2442 * in the transaction struct, so we can use
2443 * this information later */
2444 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2445 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 }
2447
2448 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2449
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002450 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451}
2452
2453static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2454{
2455 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002456 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2458
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 gh->msg_type = GSM48_MT_CC_MODIFY;
2460
2461 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2462
2463 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002464 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002465
2466 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2467
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002468 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469}
2470
2471static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2472{
2473 struct gsm48_hdr *gh = msgb_l3(msg);
2474 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2475 struct tlv_parsed tp;
2476 struct gsm_mncc modify;
2477
2478 gsm48_stop_cc_timer(trans);
2479
2480 memset(&modify, 0, sizeof(struct gsm_mncc));
2481 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002482 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483 /* bearer capability */
2484 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2485 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002486 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002488
2489 /* Create a copy of the bearer capability
2490 * in the transaction struct, so we can use
2491 * this information later */
2492 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2493 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494 }
2495
2496 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2497
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002498 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2502{
2503 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002504 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2506
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2508
2509 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002510 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511
2512 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2513
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002514 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515}
2516
2517static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2518{
2519 struct gsm48_hdr *gh = msgb_l3(msg);
2520 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2521 struct tlv_parsed tp;
2522 struct gsm_mncc modify;
2523
2524 gsm48_stop_cc_timer(trans);
2525
2526 memset(&modify, 0, sizeof(struct gsm_mncc));
2527 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002528 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 /* bearer capability */
2530 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2531 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002532 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002534
2535 /* Create a copy of the bearer capability
2536 * in the transaction struct, so we can use
2537 * this information later */
2538 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2539 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 }
2541 /* cause */
2542 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2543 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002544 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2546 }
2547
2548 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2549
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002550 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551}
2552
2553static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2554{
2555 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002556 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2558
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2560
2561 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002562 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002563 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002564 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565
2566 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2567
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_tx_notify(struct gsm_trans *trans, void *arg)
2572{
2573 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002574 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2576
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 gh->msg_type = GSM48_MT_CC_NOTIFY;
2578
2579 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002580 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002582 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583}
2584
2585static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2586{
2587 struct gsm48_hdr *gh = msgb_l3(msg);
2588 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2589// struct tlv_parsed tp;
2590 struct gsm_mncc notify;
2591
2592 memset(&notify, 0, sizeof(struct gsm_mncc));
2593 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002594// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002596 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002598 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599}
2600
2601static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2602{
2603 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002604 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002605 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2606
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607 gh->msg_type = GSM48_MT_CC_USER_INFO;
2608
2609 /* user-user */
2610 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002611 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002612 /* more data */
2613 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002614 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002616 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617}
2618
2619static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2620{
2621 struct gsm48_hdr *gh = msgb_l3(msg);
2622 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2623 struct tlv_parsed tp;
2624 struct gsm_mncc user;
2625
2626 memset(&user, 0, sizeof(struct gsm_mncc));
2627 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002628 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 /* user-user */
2630 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2631 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002632 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2634 }
2635 /* more data */
2636 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2637 user.more = 1;
2638
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002639 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002640}
2641
Philipp Maierfbf66102017-04-09 12:32:51 +02002642static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2643 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2644 uint32_t payload_msg_type)
2645{
2646 uint8_t data[sizeof(struct gsm_mncc)];
2647 struct gsm_mncc_rtp *rtp;
2648
2649 memset(&data, 0, sizeof(data));
2650 rtp = (struct gsm_mncc_rtp *) &data[0];
2651
2652 rtp->callref = callref;
2653 rtp->msg_type = cmd;
2654 rtp->ip = addr;
2655 rtp->port = port;
2656 rtp->payload_type = payload_type;
2657 rtp->payload_msg_type = payload_msg_type;
2658 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2659}
2660
2661static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2662{
2663 int msg_type;
2664
2665 /* FIXME This has to be set to some meaningful value.
2666 * Possible options are:
2667 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2668 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2669 * (0 if unknown) */
2670 msg_type = GSM_TCHF_FRAME;
2671
2672 uint32_t addr = mgcpgw_client_remote_addr_n(net->mgcpgw.client);
2673 uint16_t port = trans->conn->rtp.port_cn;
2674
2675 /* FIXME: This has to be set to some meaningful value,
2676 * before the MSC-Split, this value was pulled from
2677 * lchan->abis_ip.rtp_payload */
2678 uint32_t payload_type = 0;
2679
2680 return mncc_recv_rtp(net, trans->callref, cmd,
2681 addr,
2682 port,
2683 payload_type,
2684 msg_type);
2685}
2686
2687static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2688{
2689 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2690}
2691
2692static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2693{
2694 struct gsm_trans *trans;
2695 int rc;
2696
2697 /* Find callref */
2698 trans = trans_find_by_callref(net, callref);
2699 if (!trans) {
2700 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2701 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2702 return -EIO;
2703 }
2704 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2705 if (!trans->conn) {
2706 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2707 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2708 return 0;
2709 }
2710
2711 trans->conn->mncc_rtp_bridge = 1;
2712
2713 /* When we call msc_call_assignment() we will trigger, depending
2714 * on the RAN type the call assignment on the A or Iu interface.
2715 * msc_call_assignment() also takes care about sending the CRCX
2716 * command to the MGCP-GW. The CRCX will return the port number,
2717 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2718 * have to return this port number back to the MNCC by sending
2719 * it back with the TCH_RTP_CREATE message. To make sure that
2720 * this message is sent AFTER the response to CRCX from the
2721 * MGCP-GW has arrived, we need will instruct msc_call_assignment()
2722 * to take care of this by setting trans->tch_rtp_create to true.
2723 * This will make sure that gsm48_tch_rtp_create() (below) is
2724 * called as soon as the local port number has become known. */
2725 trans->tch_rtp_create = true;
2726
2727 /* Assign call (if not done yet) */
2728 if (trans->assignment_done == false) {
2729 rc = msc_call_assignment(trans);
2730 trans->assignment_done = true;
2731 }
2732 else
2733 rc = 0;
2734
2735 return rc;
2736}
2737
2738/* Trigger TCH_RTP_CREATE acknowledgement */
2739int gsm48_tch_rtp_create(struct gsm_trans *trans)
2740{
2741 /* This function is called as soon as the port, on which the
2742 * mgcp-gw expects the incoming RTP stream from the remote
2743 * end (e.g. Asterisk) is known. */
2744
2745 struct gsm_subscriber_connection *conn = trans->conn;
2746 struct gsm_network *network = conn->network;
2747
2748 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2749 return 0;
2750}
2751
2752static int tch_rtp_connect(struct gsm_network *net, void *arg)
2753{
2754 struct gsm_trans *trans;
2755 struct gsm_mncc_rtp *rtp = arg;
2756
2757 /* Find callref */
2758 trans = trans_find_by_callref(net, rtp->callref);
2759 if (!trans) {
2760 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2761 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2762 return -EIO;
2763 }
2764 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2765 if (!trans->conn) {
2766 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2767 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2768 return 0;
2769 }
2770
2771 msc_call_connect(trans,rtp->port,rtp->ip);
2772 return 0;
2773}
2774
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002776 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777 int type;
2778 int (*rout) (struct gsm_trans *trans, void *arg);
2779} downstatelist[] = {
2780 /* mobile originating call establishment */
2781 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002782 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2784 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2785 {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 */
2786 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2787 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2788 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2789 /* mobile terminating call establishment */
2790 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2791 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2792 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2793 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2794 /* signalling during call */
2795 {SBIT(GSM_CSTATE_ACTIVE),
2796 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2797 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2798 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2799 {ALL_STATES,
2800 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2801 {ALL_STATES,
2802 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2803 {ALL_STATES,
2804 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2805 {SBIT(GSM_CSTATE_ACTIVE),
2806 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2807 {SBIT(GSM_CSTATE_ACTIVE),
2808 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2809 {SBIT(GSM_CSTATE_ACTIVE),
2810 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2811 {SBIT(GSM_CSTATE_ACTIVE),
2812 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2813 {SBIT(GSM_CSTATE_ACTIVE),
2814 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2815 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2816 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2817 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2818 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2819 {SBIT(GSM_CSTATE_ACTIVE),
2820 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2821 /* clearing */
2822 {SBIT(GSM_CSTATE_INITIATED),
2823 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2824 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2825 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2826 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2827 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828};
2829
2830#define DOWNSLLEN \
2831 (sizeof(downstatelist) / sizeof(struct downstate))
2832
2833
Harald Welte76556372010-12-22 23:57:45 +01002834int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002835{
Harald Welte1a6f7982009-08-09 18:52:33 +02002836 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002837 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002838 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839 struct gsm_mncc *data = arg, rel;
2840
Harald Welte04dc88f2010-12-22 21:45:05 +01002841 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2842
Harald Welte4bfdfe72009-06-10 23:11:52 +08002843 /* handle special messages */
2844 switch(msg_type) {
2845 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002846 rc = tch_bridge(net, arg);
2847 if (rc < 0)
2848 disconnect_bridge(net, arg, -rc);
2849 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002850 case MNCC_RTP_CREATE:
2851 return tch_rtp_create(net, data->callref);
2852 case MNCC_RTP_CONNECT:
2853 return tch_rtp_connect(net, arg);
2854 case MNCC_RTP_FREE:
2855 /* unused right now */
2856 return -EIO;
2857
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002860 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002861 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002862 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002863 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002864 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
2865 get_mncc_name(msg_type));
2866 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867 }
2868
2869 memset(&rel, 0, sizeof(struct gsm_mncc));
2870 rel.callref = data->callref;
2871
2872 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002873 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874
2875 /* Callref unknown */
2876 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02002877 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002878
Harald Welte4a3464c2009-07-04 10:11:24 +02002879 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002880 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2881 "Received '%s' from MNCC with "
2882 "unknown callref %d\n", data->called.number,
2883 get_mncc_name(msg_type), data->callref);
2884 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002885 return mncc_release_ind(net, NULL, data->callref,
2886 GSM48_CAUSE_LOC_PRN_S_LU,
2887 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002888 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002889 if (!data->called.number[0] && !data->imsi[0]) {
2890 DEBUGP(DCC, "(bts - trx - ts - ti) "
2891 "Received '%s' from MNCC with "
2892 "no number or IMSI\n", get_mncc_name(msg_type));
2893 /* Invalid number */
2894 return mncc_release_ind(net, NULL, data->callref,
2895 GSM48_CAUSE_LOC_PRN_S_LU,
2896 GSM48_CC_CAUSE_INV_NR_FORMAT);
2897 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002898 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002899 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02002900 vsub = vlr_subscr_find_by_msisdn(net->vlr,
2901 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002902 else
Harald Welte2483f1b2016-06-19 18:06:02 +02002903 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002904
2905 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002906 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002907
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02002909 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002910 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2911 "Received '%s' from MNCC with "
2912 "unknown subscriber %s\n", data->called.number,
2913 get_mncc_name(msg_type), data->called.number);
2914 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002915 return mncc_release_ind(net, NULL, data->callref,
2916 GSM48_CAUSE_LOC_PRN_S_LU,
2917 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918 }
2919 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02002920 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002921 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2922 "Received '%s' from MNCC with "
2923 "detached subscriber %s\n", data->called.number,
2924 get_mncc_name(msg_type), data->called.number);
Harald Welte2483f1b2016-06-19 18:06:02 +02002925 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002926 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002927 return mncc_release_ind(net, NULL, data->callref,
2928 GSM48_CAUSE_LOC_PRN_S_LU,
2929 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930 }
2931 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02002932 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02002933 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002935 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002937 mncc_release_ind(net, NULL, data->callref,
2938 GSM48_CAUSE_LOC_PRN_S_LU,
2939 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 return -ENOMEM;
2941 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002942
2943 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002944 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002945
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002946 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002947 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 /* find transaction with this subscriber already paging */
2949 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002950 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02002952 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002954 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 "Received '%s' from MNCC with "
2956 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002957 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02002959 get_mncc_name(msg_type), vsub->lac);
2960 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002961 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962 return 0;
2963 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002964 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02002965 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01002966
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02002967 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002968 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02002969 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02002970 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002971 trans,
2972 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002973 if (!trans->paging_request) {
2974 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02002975 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01002976 trans_free(trans);
2977 return 0;
2978 }
Harald Welte2483f1b2016-06-19 18:06:02 +02002979 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002980 return 0;
2981 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002982
2983 /* Assign conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02002984 trans->conn = msc_subscr_conn_get(conn);
2985 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002986 } else {
2987 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02002988 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002989 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002990
2991 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002992 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993
2994 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002995 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02002996 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002997 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02002998 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08002999 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003000 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3001 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 if (msg_type == MNCC_REL_REQ)
3003 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3004 else
3005 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3006 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003007 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 return rc;
3009 }
3010
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003011 DEBUGP(DCC, "(ti %02x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 "Received '%s' from MNCC in state %d (%s)\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003013 trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003014 vlr_subscr_msisdn_or_name(trans->conn->vsub),
Harald Weltedcaf5652009-07-23 18:56:43 +02003015 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003016 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017
3018 /* Find function for current state and message */
3019 for (i = 0; i < DOWNSLLEN; i++)
3020 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003021 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022 break;
3023 if (i == DOWNSLLEN) {
3024 DEBUGP(DCC, "Message unhandled at this state.\n");
3025 return 0;
3026 }
3027
3028 rc = downstatelist[i].rout(trans, arg);
3029
3030 return rc;
3031}
3032
3033
3034static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003035 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 int type;
3037 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3038} datastatelist[] = {
3039 /* mobile originating call establishment */
3040 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3041 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3042 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3043 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3044 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3045 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3046 /* mobile terminating call establishment */
3047 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3048 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3049 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3050 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003051 {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 +08003052 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3053 /* signalling during call */
3054 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3055 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3056 {SBIT(GSM_CSTATE_ACTIVE),
3057 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3058 {ALL_STATES,
3059 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3060 {ALL_STATES,
3061 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3062 {ALL_STATES,
3063 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3064 {SBIT(GSM_CSTATE_ACTIVE),
3065 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3066 {SBIT(GSM_CSTATE_ACTIVE),
3067 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3068 {SBIT(GSM_CSTATE_ACTIVE),
3069 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3070 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3071 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3072 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3073 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3074 {SBIT(GSM_CSTATE_ACTIVE),
3075 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3076 /* clearing */
3077 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3078 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3079 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3080 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3081 {ALL_STATES, /* 5.4.3.4 */
3082 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3083};
3084
3085#define DATASLLEN \
3086 (sizeof(datastatelist) / sizeof(struct datastate))
3087
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003088static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003089{
3090 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003091 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003092 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003093 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003094 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003095
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 if (msg_type & 0x80) {
3097 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3098 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003099 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003100
Harald Welte2483f1b2016-06-19 18:06:02 +02003101 if (!conn->vsub) {
3102 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003103 return -EINVAL;
3104 }
3105
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003107 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003108
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003109#if BEFORE_MSCSPLIT
3110 /* Re-enable this log output once we can obtain this information via
3111 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003112 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003113 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003114 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003115 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003116 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3117 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003118#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119
3120 /* Create transaction */
3121 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003122 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003123 "creating new trans.\n", transaction_id);
3124 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003125 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003126 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003127 transaction_id, new_callref++);
3128 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003130 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003131 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 GSM48_MT_CC_RELEASE_COMPL);
3133 return -ENOMEM;
3134 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003135 /* Assign transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003136 trans->conn = msc_subscr_conn_get(conn);
3137 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 }
3139
3140 /* find function for current state and message */
3141 for (i = 0; i < DATASLLEN; i++)
3142 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003143 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 break;
3145 if (i == DATASLLEN) {
3146 DEBUGP(DCC, "Message unhandled at this state.\n");
3147 return 0;
3148 }
3149
Harald Welte2483f1b2016-06-19 18:06:02 +02003150 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003151
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003153
Harald Welte2483f1b2016-06-19 18:06:02 +02003154 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003155 return rc;
3156}
3157
Harald Welte2483f1b2016-06-19 18:06:02 +02003158static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003159{
Harald Welte2483f1b2016-06-19 18:06:02 +02003160 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3161 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003162
Harald Welte2483f1b2016-06-19 18:06:02 +02003163 switch (pdisc) {
3164 case GSM48_PDISC_MM:
3165 switch (msg_type) {
3166 case GSM48_MT_MM_LOC_UPD_REQUEST:
3167 case GSM48_MT_MM_CM_SERV_REQ:
3168 case GSM48_MT_MM_AUTH_RESP:
3169 case GSM48_MT_MM_AUTH_FAIL:
3170 case GSM48_MT_MM_ID_RESP:
3171 case GSM48_MT_MM_TMSI_REALL_COMPL:
3172 case GSM48_MT_MM_IMSI_DETACH_IND:
3173 return true;
3174 default:
3175 break;
3176 }
3177 break;
3178 case GSM48_PDISC_RR:
3179 switch (msg_type) {
3180 case GSM48_MT_RR_CIPH_M_COMPL:
3181 case GSM48_MT_RR_PAG_RESP:
3182 return true;
3183 default:
3184 break;
3185 }
3186 break;
3187 default:
3188 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003189 }
3190
Harald Welte2483f1b2016-06-19 18:06:02 +02003191 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003192}
3193
Harald Welte2483f1b2016-06-19 18:06:02 +02003194void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3195 struct msgb *msg)
3196{
3197
3198 /* If a CM Service Request was received before, this is the request the
3199 * conn was opened for. No need to wait for further messages. */
3200
3201 if (!conn->received_cm_service_request)
3202 return;
3203
3204 if (log_check_level(DMM, LOGL_DEBUG)) {
3205 struct gsm48_hdr *gh = msgb_l3(msg);
3206 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3207 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3208
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003209 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003210 " received_cm_service_request changes to false\n",
3211 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003212 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003213 }
3214 conn->received_cm_service_request = false;
3215}
3216
3217
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003218/* 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 +08003219int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003220{
3221 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003222 uint8_t pdisc = gsm48_hdr_pdisc(gh);
Harald Welte8470bf22008-12-25 23:28:35 +00003223 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003224
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003225 OSMO_ASSERT(conn);
3226 OSMO_ASSERT(msg);
3227
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003228 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3229 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3230 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003231
3232 if (!msc_subscr_conn_is_accepted(conn)
3233 && !msg_is_initially_permitted(gh)) {
3234 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003235 "subscr %s: Message not permitted for initial conn: %s\n",
3236 vlr_subscr_name(conn->vsub),
3237 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003238 return -EACCES;
3239 }
3240
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003241 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3242 LOGP(DMM, LOGL_ERROR,
3243 "%s: Illegal situation: RAN type mismatch:"
3244 " attached via %s, received message via %s\n",
3245 vlr_subscr_name(conn->vsub),
3246 ran_type_name(conn->vsub->cs.attached_via_ran),
3247 ran_type_name(conn->via_ran));
3248 return -EACCES;
3249 }
3250
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003251#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003252 if (silent_call_reroute(conn, msg))
3253 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003254#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003255
Harald Welte52b1f982008-12-23 20:25:15 +00003256 switch (pdisc) {
3257 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003258 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003259 break;
3260 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003261 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003262 break;
3263 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003264 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003265 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003266 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003267 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003268 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003269 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003270 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003271 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3272 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003273 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003274 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003275 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003276 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003277 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003278 case GSM48_PDISC_TEST:
3279 rc = gsm0414_rcv_test(conn, msg);
3280 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003281 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003282 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3283 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003284 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003285 break;
3286 }
3287
3288 return rc;
3289}
Harald Welte8470bf22008-12-25 23:28:35 +00003290
Harald Welte2483f1b2016-06-19 18:06:02 +02003291/***********************************************************************
3292 * VLR integration
3293 ***********************************************************************/
3294
3295/* VLR asks us to send an authentication request */
3296static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3297 bool send_autn)
3298{
3299 struct gsm_subscriber_connection *conn = msc_conn_ref;
3300 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3301 send_autn? at->vec.autn : NULL,
3302 at->key_seq);
3303}
3304
3305/* VLR asks us to send an authentication reject */
3306static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3307{
3308 struct gsm_subscriber_connection *conn = msc_conn_ref;
3309 return gsm48_tx_mm_auth_rej(conn);
3310}
3311
3312/* VLR asks us to transmit an Identity Request of given type */
3313static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3314{
3315 struct gsm_subscriber_connection *conn = msc_conn_ref;
3316 return mm_tx_identity_req(conn, mi_type);
3317}
3318
3319/* VLR asks us to transmit a Location Update Accept */
3320static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3321{
3322 struct gsm_subscriber_connection *conn = msc_conn_ref;
3323 return gsm0408_loc_upd_acc(conn, send_tmsi);
3324}
3325
3326/* VLR asks us to transmit a Location Update Reject */
3327static int msc_vlr_tx_lu_rej(void *msc_conn_ref, uint8_t cause)
3328{
3329 struct gsm_subscriber_connection *conn = msc_conn_ref;
3330 return gsm0408_loc_upd_rej(conn, cause);
3331}
3332
3333/* VLR asks us to transmit a CM Service Accept */
3334static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3335{
3336 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003337 return msc_gsm48_tx_mm_serv_ack(conn);
3338}
3339
3340static int msc_vlr_tx_common_id(void *msc_conn_ref)
3341{
3342 struct gsm_subscriber_connection *conn = msc_conn_ref;
3343 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003344}
3345
3346/* VLR asks us to transmit a CM Service Reject */
3347static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum vlr_proc_arq_result result)
3348{
3349 uint8_t cause;
3350 struct gsm_subscriber_connection *conn = msc_conn_ref;
3351 conn->received_cm_service_request = false;
3352
3353 switch (result) {
3354 default:
3355 case VLR_PR_ARQ_RES_NONE:
3356 case VLR_PR_ARQ_RES_SYSTEM_FAILURE:
3357 case VLR_PR_ARQ_RES_UNKNOWN_ERROR:
3358 cause = GSM48_REJECT_NETWORK_FAILURE;
3359 break;
3360 case VLR_PR_ARQ_RES_ILLEGAL_SUBSCR:
3361 cause = GSM48_REJECT_LOC_NOT_ALLOWED;
3362 break;
3363 case VLR_PR_ARQ_RES_UNIDENT_SUBSCR:
3364 cause = GSM48_REJECT_INVALID_MANDANTORY_INF;
3365 break;
3366 case VLR_PR_ARQ_RES_ROAMING_NOTALLOWED:
3367 cause = GSM48_REJECT_ROAMING_NOT_ALLOWED;
3368 break;
3369 case VLR_PR_ARQ_RES_ILLEGAL_EQUIP:
3370 cause = GSM48_REJECT_ILLEGAL_MS;
3371 break;
3372 case VLR_PR_ARQ_RES_TIMEOUT:
3373 cause = GSM48_REJECT_CONGESTION;
3374 break;
3375 };
3376
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003377 return msc_gsm48_tx_mm_serv_rej(conn, cause);
Harald Welte2483f1b2016-06-19 18:06:02 +02003378}
3379
3380/* VLR asks us to start using ciphering */
3381static int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3382 enum vlr_ciph ciph,
3383 bool retrieve_imeisv)
3384{
3385 struct gsm_subscriber_connection *conn = msc_conn_ref;
3386 struct vlr_subscr *vsub;
3387 struct gsm_auth_tuple *tuple;
3388
3389 if (!conn || !conn->vsub) {
3390 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3391 " NULL conn/subscriber");
3392 return -EINVAL;
3393 }
3394
3395 vsub = conn->vsub;
3396 tuple = vsub->last_tuple;
3397
3398 if (!tuple) {
3399 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3400 " Command: no auth tuple available\n",
3401 vlr_subscr_name(vsub));
3402 return -EINVAL;
3403 }
3404
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003405 switch (conn->via_ran) {
3406 case RAN_GERAN_A:
3407 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3408 vlr_subscr_name(conn->vsub));
Philipp Maierfbf66102017-04-09 12:32:51 +02003409 return a_iface_tx_cipher_mode(conn, ciph, tuple->vec.kc, 8,
3410 retrieve_imeisv);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003411 case RAN_UTRAN_IU:
3412#ifdef BUILD_IU
3413 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3414 vlr_subscr_name(conn->vsub));
3415 return iu_tx_sec_mode_cmd(conn->iu.ue_ctx, tuple, 0, 1);
3416#else
3417 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3418 " built without Iu support\n");
3419 return -ENOTSUP;
3420#endif
3421
3422 default:
3423 break;
3424 }
3425 LOGP(DMM, LOGL_ERROR,
3426 "%s: cannot start ciphering, unknown RAN type %d\n",
3427 vlr_subscr_name(conn->vsub), conn->via_ran);
3428 return -ENOTSUP;
3429}
3430
3431void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3432{
3433 struct vlr_ciph_result vlr_res = {};
3434
3435 if (!conn || !conn->vsub) {
3436 LOGP(DMM, LOGL_ERROR,
3437 "Rx Security Mode Complete for invalid conn\n");
3438 return;
3439 }
3440
3441 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3442 vlr_subscr_name(conn->vsub));
3443
3444 vlr_res.cause = VLR_CIPH_COMPL;
3445 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003446}
3447
3448/* VLR informs us that the subscriber data has somehow been modified */
3449static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3450{
3451 /* FIXME */
3452}
3453
3454/* VLR informs us that the subscriber has been associated with a conn */
3455static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3456 struct vlr_subscr *vsub)
3457{
3458 struct gsm_subscriber_connection *conn = msc_conn_ref;
3459 OSMO_ASSERT(!conn->vsub);
3460 conn->vsub = vlr_subscr_get(vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003461 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003462}
3463
3464/* operations that we need to implement for libvlr */
3465static const struct vlr_ops msc_vlr_ops = {
3466 .tx_auth_req = msc_vlr_tx_auth_req,
3467 .tx_auth_rej = msc_vlr_tx_auth_rej,
3468 .tx_id_req = msc_vlr_tx_id_req,
3469 .tx_lu_acc = msc_vlr_tx_lu_acc,
3470 .tx_lu_rej = msc_vlr_tx_lu_rej,
3471 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3472 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3473 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003474 .tx_common_id = msc_vlr_tx_common_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02003475 .subscr_update = msc_vlr_subscr_update,
3476 .subscr_assoc = msc_vlr_subscr_assoc,
3477};
3478
3479/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3480int msc_vlr_alloc(struct gsm_network *net)
3481{
3482 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3483 if (!net->vlr)
3484 return -ENOMEM;
3485 net->vlr->user_ctx = net;
3486 return 0;
3487}
3488
3489/* Launch the VLR, i.e. its GSUP connection */
3490int msc_vlr_start(struct gsm_network *net)
3491{
3492 OSMO_ASSERT(net->vlr);
3493 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3494 net->gsup_server_port);
3495}