blob: ea5a81aca95cc6e17938613abdb732bd6f1c9b90 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Welte2483f1b2016-06-19 18:06:02 +02004/* (C) 2008-2016 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
Maxddee01f2016-05-24 14:23:27 +020028#include <stdbool.h>
Harald Welte52b1f982008-12-23 20:25:15 +000029#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Maxddee01f2016-05-24 14:23:27 +020032#include <regex.h>
33#include <sys/types.h>
Harald Welte52b1f982008-12-23 20:25:15 +000034
Harald Welte9c3dc902012-04-08 16:59:24 +020035#include "bscconfig.h"
36
Neels Hofmeyr90843962017-09-04 15:04:35 +020037#include <osmocom/msc/db.h>
38#include <osmocom/msc/debug.h>
39#include <osmocom/msc/gsm_data.h>
40#include <osmocom/msc/gsm_subscriber.h>
41#include <osmocom/msc/gsm_04_11.h>
42#include <osmocom/msc/gsm_04_08.h>
43#include <osmocom/msc/gsm_04_80.h>
44#include <osmocom/msc/gsm_04_14.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020045#include <osmocom/msc/signal.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020046#include <osmocom/msc/transaction.h>
47#include <osmocom/msc/ussd.h>
48#include <osmocom/msc/silent_call.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020049#include <osmocom/msc/osmo_msc.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020050#include <osmocom/msc/mncc_int.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020051#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010052#include <osmocom/core/bitvec.h>
Neels Hofmeyr90843962017-09-04 15:04:35 +020053#include <osmocom/msc/vlr.h>
54#include <osmocom/msc/msc_ifaces.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080055
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/gsm/gsm48.h>
57#include <osmocom/gsm/gsm0480.h>
58#include <osmocom/gsm/gsm_utils.h>
Max8db12e42016-04-18 23:11:18 +020059#include <osmocom/gsm/protocol/gsm_04_08.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010060#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
Neels Hofmeyr93bafb62017-01-13 03:12:08 +010062#include <osmocom/core/utils.h>
Philipp Maier621ba032017-11-07 17:19:25 +010063#include <osmocom/core/byteswap.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010064#include <osmocom/gsm/tlv.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020065#include <osmocom/crypt/auth.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020066#ifdef BUILD_IU
Neels Hofmeyr00e82d62017-07-05 15:19:52 +020067#include <osmocom/ranap/iu_client.h>
Neels Hofmeyr84da6b12016-05-20 21:59:55 +020068#endif
Harald Welte52b1f982008-12-23 20:25:15 +000069
Neels Hofmeyr90843962017-09-04 15:04:35 +020070#include <osmocom/msc/msc_ifaces.h>
71#include <osmocom/msc/a_iface.h>
Philipp Maier621ba032017-11-07 17:19:25 +010072#include <osmocom/msc/msc_mgcp.h>
Philipp Maierfbf66102017-04-09 12:32:51 +020073
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020074#include <assert.h>
75
Neels Hofmeyre2f24d52017-05-08 15:12:20 +020076
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020077void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010078void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020079
Harald Welte2483f1b2016-06-19 18:06:02 +020080static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
81 uint32_t send_tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080082static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020083 uint8_t pdisc, uint8_t msg_type);
Harald Welte65e74cc2008-12-29 01:55:35 +000084
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020085static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080086
Neels Hofmeyr9d744252018-03-22 16:09:50 +010087static bool classmark_is_r99(struct gsm_classmark *cm)
88{
89 int rev_lev = 0;
90 if (cm->classmark1_set)
91 rev_lev = cm->classmark1.rev_lev;
92 else if (cm->classmark2_len > 0)
93 rev_lev = (cm->classmark2[0] >> 5) & 0x3;
94 return rev_lev >= 2;
95}
96
Harald Welte71330722017-12-23 19:59:02 +010097/* Determine if the given CLASSMARK (1/2/3) value permits a given A5/n cipher */
98static bool classmark_supports_a5(const struct gsm_classmark *cm, uint8_t a5)
99{
100 switch (a5) {
101 case 0:
102 /* all phones must implement A5/0, see 3GPP TS 43.020 4.9 */
103 return true;
104 case 1:
105 /* 3GPP TS 43.020 4.9 requires A5/1 to be suppored by all phones and actually states:
106 * "The network shall not provide service to an MS which indicates that it does not
107 * support the ciphering algorithm A5/1.". However, let's be more tolerant based
108 * on policy here */
109 /* See 3GPP TS 24.008 10.5.1.7 */
110 if (!cm->classmark1_set) {
111 DEBUGP(DMSC, "CLASSMARK 1 unknown, assuming MS supports A5/1\n");
112 return true;
113 } else {
114 if (cm->classmark1.a5_1)
115 return false; /* Inverted logic for this bit! */
116 else
117 return true;
118 }
119 break;
120 case 2:
121 case 3:
122 /* See 3GPP TS 24.008 10.5.1.6 */
123 if (cm->classmark2_len < 3) {
124 DEBUGP(DMSC, "CLASSMARK 2 unknown, assuming MS doesn't support A5/%u\n", a5);
125 return false;
126 } else {
127 if (cm->classmark2[2] & (1 << (a5-2)))
128 return true;
129 else
130 return false;
131 }
132 break;
133 case 4:
134 case 5:
135 case 6:
136 case 7:
137 /* See 3GPP TS 24.008 10.5.1.7 */
138 if (cm->classmark3_len < 1) {
139 DEBUGP(DMSC, "CLASSMARK 3 unknown, assuming MS doesn't support A5/%u\n", a5);
140 return false;
141 } else {
142 if (cm->classmark3[0] & (1 << (a5-4)))
143 return true;
144 else
145 return false;
146 }
147 break;
148 default:
149 return false;
150 }
151}
152
Harald Welte31c00f72011-03-03 23:29:05 +0100153void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
154{
155 net->mncc_recv(net, msg);
156}
157
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800158static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
159 struct gsm_trans *trans)
160{
161 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
162
163 /* if we get passed a transaction reference, do some common
164 * work that the caller no longer has to do */
165 if (trans) {
166 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
Harald Welte0e2fa5d2018-04-09 16:35:01 +0200167 OMSC_LINKID_CB(msg) = trans->dlci;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800168 }
169
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200170 return msc_tx_dtap(conn, msg);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800171}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100172
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800173int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
174{
175 struct gsm48_hdr *gh;
176 struct msgb *ss_notify;
177
178 ss_notify = gsm0480_create_notifySS(message);
179 if (!ss_notify)
180 return -1;
181
182 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
183 uint8_t *data = msgb_push(ss_notify, 1);
184 data[0] = ss_notify->len - 1;
185 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
186 gh->msg_type = GSM48_MT_CC_FACILITY;
187 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
188}
189
Harald Welte2483f1b2016-06-19 18:06:02 +0200190/* clear all transactions globally; used in case of MNCC socket disconnect */
Harald Welte371efe52010-12-22 23:17:50 +0100191void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
192{
193 struct gsm_trans *trans, *temp;
194
195 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
196
197 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100198 if (trans->protocol == protocol) {
199 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100200 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100201 }
Harald Welte371efe52010-12-22 23:17:50 +0100202 }
203}
204
Holger Freyther429e7762008-12-30 13:28:30 +0000205/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200206static int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000207{
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800208 struct msgb *msg;
209
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800210 msg = gsm48_create_loc_upd_rej(cause);
211 if (!msg) {
212 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
213 return -1;
214 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200215
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200216 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT\n",
217 vlr_subscr_name(conn->vsub));
Harald Welte24ff6ee2009-12-22 00:41:05 +0100218
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800219 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000220}
221
222/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte2483f1b2016-06-19 18:06:02 +0200223static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn,
224 uint32_t send_tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000225{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100226 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD ACC");
Harald Welte52b1f982008-12-23 20:25:15 +0000227 struct gsm48_hdr *gh;
228 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200229 uint8_t *mid;
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100230 struct osmo_location_area_id laid = {
231 .plmn = conn->network->plmn,
232 .lac = conn->lac,
233 };
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200234
Harald Welte52b1f982008-12-23 20:25:15 +0000235 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
236 gh->proto_discr = GSM48_PDISC_MM;
237 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
238
239 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100240 gsm48_generate_lai2(lai, &laid);
Harald Welte52b1f982008-12-23 20:25:15 +0000241
Harald Welte2483f1b2016-06-19 18:06:02 +0200242 if (send_tmsi == GSM_RESERVED_TMSI) {
243 /* we did not allocate a TMSI to the MS, so we need to
244 * include the IMSI in order for the MS to delete any
245 * old TMSI that might still be allocated */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200246 uint8_t mi[10];
247 int len;
Harald Welte2483f1b2016-06-19 18:06:02 +0200248 len = gsm48_generate_mid_from_imsi(mi, conn->vsub->imsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200249 mid = msgb_put(msg, len);
250 memcpy(mid, mi, len);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200251 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT\n",
252 vlr_subscr_name(conn->vsub));
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200253 } else {
Harald Welte2483f1b2016-06-19 18:06:02 +0200254 /* Include the TMSI, which means that the MS will send a
255 * TMSI REALLOCATION COMPLETE, and we should wait for
256 * that until T3250 expiration */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200257 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Harald Welte2483f1b2016-06-19 18:06:02 +0200258 gsm48_generate_mid_from_tmsi(mid, send_tmsi);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200259 DEBUGP(DMM, "-> %s LOCATION UPDATE ACCEPT (TMSI = 0x%08x)\n",
260 vlr_subscr_name(conn->vsub),
261 send_tmsi);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200262 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200263 /* TODO: Follow-on proceed */
264 /* TODO: CTS permission */
265 /* TODO: Equivalent PLMNs */
266 /* TODO: Emergency Number List */
267 /* TODO: Per-MS T3312 */
Harald Welte52b1f982008-12-23 20:25:15 +0000268
Harald Welte52b1f982008-12-23 20:25:15 +0000269
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800270 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000271}
272
Harald Weltebf5e8df2009-02-03 12:59:45 +0000273/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200274static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000275{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100276 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 ID REQ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000277 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000278
Harald Welte231ad4f2008-12-27 11:15:38 +0000279 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
280 gh->proto_discr = GSM48_PDISC_MM;
281 gh->msg_type = GSM48_MT_MM_ID_REQ;
282 gh->data[0] = id_type;
283
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800284 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000285}
286
Harald Weltebf5e8df2009-02-03 12:59:45 +0000287/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800288static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000289{
290 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200291 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200292 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000293
Harald Welte2483f1b2016-06-19 18:06:02 +0200294 if (!conn->vsub) {
295 LOGP(DMM, LOGL_ERROR,
296 "Rx MM Identity Response: invalid: no subscriber\n");
297 return -EINVAL;
298 }
299
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200300 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200301 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
302 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000303
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200304 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100305
Harald Welte2483f1b2016-06-19 18:06:02 +0200306 return vlr_subscr_rx_id_resp(conn->vsub, gh->data+1, gh->data[0]);
Harald Welte231ad4f2008-12-27 11:15:38 +0000307}
308
Harald Welte2483f1b2016-06-19 18:06:02 +0200309/* FIXME: to libosmogsm */
Harald Welteb9845f92015-08-16 18:07:48 +0200310static const struct value_string lupd_names[] = {
311 { GSM48_LUPD_NORMAL, "NORMAL" },
312 { GSM48_LUPD_PERIODIC, "PERIODIC" },
313 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
314 { 0, NULL }
315};
Harald Welte2a139372009-02-22 21:14:55 +0000316
Harald Welte2483f1b2016-06-19 18:06:02 +0200317/* Chapter 9.2.15: Receive Location Updating Request.
318 * Keep this function non-static for direct invocation by unit tests. */
319int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000320{
Harald Welte2483f1b2016-06-19 18:06:02 +0200321 struct gsm_network *net = conn->network;
Harald Welte8470bf22008-12-25 23:28:35 +0000322 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000323 struct gsm48_loc_upd_req *lu;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200324 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200325 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200326 enum vlr_lu_type vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte2483f1b2016-06-19 18:06:02 +0200327 uint32_t tmsi;
328 char *imsi;
329 struct osmo_location_area_id old_lai, new_lai;
330 struct osmo_fsm_inst *lu_fsm;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200331 bool is_utran;
Harald Welte52b1f982008-12-23 20:25:15 +0000332
Harald Welte8470bf22008-12-25 23:28:35 +0000333 lu = (struct gsm48_loc_upd_req *) gh->data;
334
335 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000336
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200337 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000338
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200339 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
340 LOGP(DMM, LOGL_ERROR,
341 "Cannot accept another LU, conn already busy establishing authenticity;"
342 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
343 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
344 return -EINVAL;
345 }
346
347 if (msc_subscr_conn_is_accepted(conn)) {
348 LOGP(DMM, LOGL_ERROR,
349 "Cannot accept another LU, conn already established;"
350 " extraneous LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
351 gsm48_mi_type_name(mi_type), mi_string, get_value_string(lupd_names, lu->type));
352 return -EINVAL;
353 }
Harald Welte2483f1b2016-06-19 18:06:02 +0200354
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200355 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_LU, mi_string);
356
Harald Welte2483f1b2016-06-19 18:06:02 +0200357 conn->classmark.classmark1 = lu->classmark1;
358 conn->classmark.classmark1_set = true;
359
360 DEBUGP(DMM, "LOCATION UPDATING REQUEST: MI(%s)=%s type=%s\n",
361 gsm48_mi_type_name(mi_type), mi_string,
362 get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000363
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200364 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100365
Harald Welte24ff6ee2009-12-22 00:41:05 +0100366 switch (lu->type) {
367 case GSM48_LUPD_NORMAL:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200368 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_NORMAL]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200369 vlr_lu_type = VLR_LU_TYPE_REGULAR;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100370 break;
371 case GSM48_LUPD_IMSI_ATT:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200372 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_ATTACH]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200373 vlr_lu_type = VLR_LU_TYPE_IMSI_ATTACH;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100374 break;
375 case GSM48_LUPD_PERIODIC:
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200376 rate_ctr_inc(&conn->network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_PERIODIC]);
Harald Welte2483f1b2016-06-19 18:06:02 +0200377 vlr_lu_type = VLR_LU_TYPE_PERIODIC;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100378 break;
379 }
380
Harald Welte2483f1b2016-06-19 18:06:02 +0200381 /* TODO: 10.5.1.6 MS Classmark for UMTS / Classmark 2 */
382 /* TODO: 10.5.3.14 Aditional update parameters (CS fallback calls) */
383 /* TODO: 10.5.7.8 Device properties */
384 /* TODO: 10.5.1.15 MS network feature support */
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200385
Harald Welte52b1f982008-12-23 20:25:15 +0000386 switch (mi_type) {
387 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200388 tmsi = GSM_RESERVED_TMSI;
389 imsi = mi_string;
Harald Welte4b634542008-12-27 01:55:51 +0000390 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000391 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200392 tmsi = tmsi_from_string(mi_string);
393 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000394 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200395 default:
Harald Weltea0368542009-06-27 02:58:43 +0200396 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200397 tmsi = GSM_RESERVED_TMSI;
398 imsi = NULL;
Harald Welte52b1f982008-12-23 20:25:15 +0000399 break;
400 }
401
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100402 gsm48_decode_lai2(&lu->lai, &old_lai);
403 new_lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200404 new_lai.lac = conn->lac;
Harald Welte2483f1b2016-06-19 18:06:02 +0200405 DEBUGP(DMM, "LU/new-LAC: %u/%u\n", old_lai.lac, new_lai.lac);
Harald Welte24516ea2009-07-04 10:18:00 +0200406
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200407 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200408 lu_fsm = vlr_loc_update(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200409 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200410 net->vlr, conn, vlr_lu_type, tmsi, imsi,
411 &old_lai, &new_lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200412 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100413 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200414 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200415 is_utran,
Harald Welte2483f1b2016-06-19 18:06:02 +0200416 net->vlr->cfg.assign_tmsi);
417 if (!lu_fsm) {
418 DEBUGP(DRR, "%s: Can't start LU FSM\n", mi_string);
419 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800420 }
421
Harald Welte2483f1b2016-06-19 18:06:02 +0200422 /* From vlr_loc_update() we expect an implicit dispatch of
423 * VLR_ULA_E_UPDATE_LA, and thus we expect msc_vlr_subscr_assoc() to
424 * already have been called and completed. Has an error occured? */
Harald Welte255539c2008-12-28 02:26:27 +0000425
Harald Welte2483f1b2016-06-19 18:06:02 +0200426 if (!conn->vsub || conn->vsub->lu_fsm != lu_fsm) {
427 LOGP(DRR, LOGL_ERROR,
428 "%s: internal error during Location Updating attempt\n",
429 mi_string);
430 return -EIO;
431 }
432
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200433 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200434 return 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000435}
436
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100437/* Turn int into semi-octet representation: 98 => 0x89 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200438/* FIXME: libosmocore/libosmogsm */
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100439static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800440{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100441 uint8_t ret;
442
443 ret = value / 10;
444 ret |= (value % 10) << 4;
445
446 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800447}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100448
Harald Welte4bfdfe72009-06-10 23:11:52 +0800449
Harald Weltedb253af2008-12-30 17:56:55 +0000450/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800451int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000452{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100453 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 MM INF");
Harald Weltedb253af2008-12-30 17:56:55 +0000454 struct gsm48_hdr *gh;
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200455 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200456 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200457 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100458
Harald Welte4bfdfe72009-06-10 23:11:52 +0800459 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100460 struct tm* gmt_time;
461 struct tm* local_time;
462 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100463 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000464
Harald Weltedb253af2008-12-30 17:56:55 +0000465 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
466 gh->proto_discr = GSM48_PDISC_MM;
467 gh->msg_type = GSM48_MT_MM_INFO;
468
469 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200470#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000471 name_len = strlen(net->name_long);
472 /* 10.5.3.5a */
473 ptr8 = msgb_put(msg, 3);
474 ptr8[0] = GSM48_IE_NAME_LONG;
475 ptr8[1] = name_len*2 +1;
476 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
477
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200478 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000479 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000480 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000481
482 /* FIXME: Use Cell Broadcast, not UCS-2, since
483 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200484#endif
485 name_len = (strlen(net->name_long)*7)/8;
486 name_pad = (8 - strlen(net->name_long)*7)%8;
487 if (name_pad > 0)
488 name_len++;
489 /* 10.5.3.5a */
490 ptr8 = msgb_put(msg, 3);
491 ptr8[0] = GSM48_IE_NAME_LONG;
492 ptr8[1] = name_len +1;
493 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
494
495 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100496 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200497
Harald Weltedb253af2008-12-30 17:56:55 +0000498 }
499
500 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200501#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000502 name_len = strlen(net->name_short);
503 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200504 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200505 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000506 ptr8[1] = name_len*2 + 1;
507 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
508
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200509 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000510 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000511 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200512#endif
513 name_len = (strlen(net->name_short)*7)/8;
514 name_pad = (8 - strlen(net->name_short)*7)%8;
515 if (name_pad > 0)
516 name_len++;
517 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200518 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200519 ptr8[0] = GSM48_IE_NAME_SHORT;
520 ptr8[1] = name_len +1;
521 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
522
523 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100524 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200525
Harald Weltedb253af2008-12-30 17:56:55 +0000526 }
527
Harald Weltedb253af2008-12-30 17:56:55 +0000528 /* Section 10.5.3.9 */
529 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100530 gmt_time = gmtime(&cur_t);
531
Harald Weltedb253af2008-12-30 17:56:55 +0000532 ptr8 = msgb_put(msg, 8);
533 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100534 ptr8[1] = bcdify(gmt_time->tm_year % 100);
535 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
536 ptr8[3] = bcdify(gmt_time->tm_mday);
537 ptr8[4] = bcdify(gmt_time->tm_hour);
538 ptr8[5] = bcdify(gmt_time->tm_min);
539 ptr8[6] = bcdify(gmt_time->tm_sec);
540
Neels Hofmeyr73983952016-05-10 13:29:33 +0200541 if (net->tz.override) {
Harald Welte45f91712012-07-08 16:48:11 +0200542 /* Convert tz.hr and tz.mn to units */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200543 if (net->tz.hr < 0) {
544 tzunits = ((net->tz.hr/-1)*4);
545 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100546 ptr8[7] = bcdify(tzunits);
547 /* Set negative time */
548 ptr8[7] |= 0x08;
549 }
550 else {
Neels Hofmeyr73983952016-05-10 13:29:33 +0200551 tzunits = net->tz.hr*4;
552 tzunits = tzunits + (net->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100553 ptr8[7] = bcdify(tzunits);
554 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100555 /* Convert DST value */
Neels Hofmeyr73983952016-05-10 13:29:33 +0200556 if (net->tz.dst >= 0 && net->tz.dst <= 2)
557 dst = net->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100558 }
559 else {
560 /* Need to get GSM offset and convert into 15 min units */
561 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
Harald Welte9c3dc902012-04-08 16:59:24 +0200562#ifdef HAVE_TM_GMTOFF_IN_TM
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200563 local_time = localtime(&cur_t);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100564 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200565#else
Benoit Bolsee38bb3442017-07-05 12:57:16 +0200566 /* find timezone offset */
567 time_t utc;
568 double offsetFromUTC;
569 utc = mktime(gmt_time);
570 local_time = localtime(&cur_t);
571 offsetFromUTC = difftime(cur_t, utc);
572 if (local_time->tm_isdst)
573 offsetFromUTC += 3600.0;
574 tzunits = ((int)offsetFromUTC) / 60 / 15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200575#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100576 if (tzunits < 0) {
577 tzunits = tzunits/-1;
578 ptr8[7] = bcdify(tzunits);
579 /* Flip it to negative */
580 ptr8[7] |= 0x08;
581 }
582 else
583 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100584
585 /* Does not support DST +2 */
586 if (local_time->tm_isdst)
587 dst = 1;
588 }
589
590 if (dst) {
591 ptr8 = msgb_put(msg, 3);
592 ptr8[0] = GSM48_IE_NET_DST;
593 ptr8[1] = 1;
594 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100595 }
Harald Weltedb253af2008-12-30 17:56:55 +0000596
Daniel Willmanneea93372009-08-13 03:42:07 +0200597 DEBUGP(DMM, "-> MM INFO\n");
598
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800599 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000600}
601
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100602/*! Send an Authentication Request to MS on the given subscriber connection
603 * according to 3GPP/ETSI TS 24.008, Section 9.2.2.
604 * \param[in] conn Subscriber connection to send on.
605 * \param[in] rand Random challenge token to send, must be 16 bytes long.
606 * \param[in] autn r99: In case of UMTS mutual authentication, AUTN token to
607 * send; must be 16 bytes long, or pass NULL for plain GSM auth.
608 * \param[in] key_seq auth tuple's sequence number.
609 */
610int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand,
611 uint8_t *autn, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200612{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +0100613 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 AUTH REQ");
Harald Welte7984d5c2009-08-12 22:56:50 +0200614 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200615 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200616
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100617 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump_nospc(rand, 16));
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100618 if (autn)
Neels Hofmeyr3355fd62017-02-14 17:42:25 +0100619 DEBUGP(DMM, " AUTH REQ (autn = %s)\n", osmo_hexdump_nospc(autn, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200620
Harald Welte7984d5c2009-08-12 22:56:50 +0200621 gh->proto_discr = GSM48_PDISC_MM;
622 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
623
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100624 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200625
Harald Welte7984d5c2009-08-12 22:56:50 +0200626 /* 16 bytes RAND parameters */
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100627 osmo_static_assert(sizeof(ar->rand) == 16, sizeof_auth_req_r99_rand);
Harald Welte7984d5c2009-08-12 22:56:50 +0200628 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200629 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200630
Neels Hofmeyrd6755152017-01-27 00:25:47 +0100631
632 /* 16 bytes AUTN */
633 if (autn)
634 msgb_tlv_put(msg, GSM48_IE_AUTN, 16, autn);
635
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800636 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200637}
638
639/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800640int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200641{
642 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800643 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200644}
645
Harald Welte2483f1b2016-06-19 18:06:02 +0200646static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200647static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value result);
Harald Welte2483f1b2016-06-19 18:06:02 +0200648
649static int cm_serv_reuse_conn(struct gsm_subscriber_connection *conn, const uint8_t *mi_lv)
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100650{
Harald Welte2483f1b2016-06-19 18:06:02 +0200651 uint8_t mi_type;
652 char mi_string[GSM48_MI_SIZE];
653 uint32_t tmsi;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100654
Harald Welte2483f1b2016-06-19 18:06:02 +0200655 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi_lv+1, mi_lv[0]);
656 mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100657
Harald Welte2483f1b2016-06-19 18:06:02 +0200658 switch (mi_type) {
659 case GSM_MI_TYPE_IMSI:
660 if (vlr_subscr_matches_imsi(conn->vsub, mi_string))
661 goto accept_reuse;
662 break;
663 case GSM_MI_TYPE_TMSI:
664 tmsi = osmo_load32be(mi_lv+2);
665 if (vlr_subscr_matches_tmsi(conn->vsub, tmsi))
666 goto accept_reuse;
667 break;
668 case GSM_MI_TYPE_IMEI:
669 if (vlr_subscr_matches_imei(conn->vsub, mi_string))
670 goto accept_reuse;
671 break;
672 default:
673 break;
674 }
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100675
Harald Welte2483f1b2016-06-19 18:06:02 +0200676 LOGP(DMM, LOGL_ERROR, "%s: CM Service Request with mismatching mobile identity: %s %s\n",
677 vlr_subscr_name(conn->vsub), gsm48_mi_type_name(mi_type), mi_string);
Neels Hofmeyr15809592018-04-06 02:57:51 +0200678 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_ILLEGAL_MS);
Harald Welte2483f1b2016-06-19 18:06:02 +0200679 return -EINVAL;
Sylvain Munautba87f452009-12-24 00:28:46 +0100680
Harald Welte2483f1b2016-06-19 18:06:02 +0200681accept_reuse:
682 DEBUGP(DMM, "%s: re-using already accepted connection\n",
683 vlr_subscr_name(conn->vsub));
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200684
685 if (!conn->received_cm_service_request) {
686 conn->received_cm_service_request = true;
687 msc_subscr_conn_get(conn, MSC_CONN_USE_CM_SERVICE);
688 }
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200689 msc_subscr_conn_update_id(conn, conn->complete_layer3_type, mi_string);
Harald Welte2483f1b2016-06-19 18:06:02 +0200690 return conn->network->vlr->ops.tx_cm_serv_acc(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100691}
692
Harald Welte4ed0e922009-01-10 03:17:30 +0000693/*
694 * Handle CM Service Requests
695 * a) Verify that the packet is long enough to contain the information
696 * we require otherwsie reject with INCORRECT_MESSAGE
697 * b) Try to parse the TMSI. If we do not have one reject
698 * c) Check that we know the subscriber with the TMSI otherwise reject
699 * with a HLR cause
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200700 * d) Set the subscriber on the conn and accept
Harald Welte2483f1b2016-06-19 18:06:02 +0200701 *
702 * Keep this function non-static for direct invocation by unit tests.
Harald Welte4ed0e922009-01-10 03:17:30 +0000703 */
Harald Welte2483f1b2016-06-19 18:06:02 +0200704int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000705{
Harald Welte2483f1b2016-06-19 18:06:02 +0200706 struct gsm_network *net = conn->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200707 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200708 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000709
Harald Welteba4cf162009-01-10 01:49:35 +0000710 struct gsm48_hdr *gh = msgb_l3(msg);
711 struct gsm48_service_request *req =
712 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800713 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200714 uint8_t classmark2_len = gh->data[1];
715 uint8_t *classmark2 = gh->data+2;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200716 uint8_t *mi_p = classmark2 + classmark2_len;
717 uint8_t mi_len = *mi_p;
718 uint8_t *mi = mi_p + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +0200719 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200720 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +0200721
Neels Hofmeyr379d5792018-02-22 04:04:54 +0100722 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200723 lai.lac = conn->lac;
Harald Welteba4cf162009-01-10 01:49:35 +0000724
725 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200726 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST wrong sized message\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200727 return msc_gsm48_tx_mm_serv_rej(conn,
728 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000729 }
730
731 if (msg->data_len < req->mi_len + 6) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200732 LOGP(DMM, LOGL_ERROR, "<- CM SERVICE REQUEST does not fit in packet\n");
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200733 return msc_gsm48_tx_mm_serv_rej(conn,
734 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000735 }
736
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200737 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000738 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200739 DEBUGP(DMM, "<- CM SERVICE REQUEST serv_type=0x%02x MI(%s)=%s\n",
740 req->cm_service_type, gsm48_mi_type_name(mi_type), mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200741
Harald Welte3995d2e2018-01-24 11:23:54 +0100742 switch (mi_type) {
743 case GSM_MI_TYPE_IMSI:
744 case GSM_MI_TYPE_TMSI:
745 /* continue below */
746 break;
747 case GSM_MI_TYPE_IMEI:
748 if (req->cm_service_type == GSM48_CMSERV_EMERGENCY) {
749 /* We don't do emergency calls by IMEI */
750 LOGP(DMM, LOGL_NOTICE, "<- CM SERVICE REQUEST(IMEI=%s) rejected\n", mi_string);
751 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_IMEI_NOT_ACCEPTED);
752 }
753 /* fall-through for non-emergency setup */
754 default:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200755 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +0200756 return msc_gsm48_tx_mm_serv_rej(conn,
757 GSM48_REJECT_INCORRECT_MESSAGE);
Harald Welteba4cf162009-01-10 01:49:35 +0000758 }
759
Harald Welte37382ec2018-01-24 12:49:34 +0100760 switch (req->cm_service_type) {
761 case GSM48_CMSERV_MO_CALL_PACKET:
762 case GSM48_CMSERV_EMERGENCY:
763 case GSM48_CMSERV_SMS:
764 case GSM48_CMSERV_SUP_SERV:
765 /* continue below */
766 break;
767 default:
768 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
769 }
770
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200771 if (msc_subscr_conn_is_accepted(conn))
772 return cm_serv_reuse_conn(conn, mi_p);
Harald Welte2483f1b2016-06-19 18:06:02 +0200773
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200774 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
775 LOGP(DMM, LOGL_ERROR,
776 "Cannot accept CM Service Request, conn already busy establishing authenticity\n");
Neels Hofmeyr15809592018-04-06 02:57:51 +0200777 msc_vlr_tx_cm_serv_rej(conn, GSM48_REJECT_CONGESTION);
Harald Welte2483f1b2016-06-19 18:06:02 +0200778 return -EINVAL;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200779 /* or should we accept and note down the service request anyway? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200780 }
781
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200782 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_CM_SERVICE_REQ, mi_string);
Harald Welte7659de12009-12-13 12:39:18 +0100783
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200784 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, mi_p);
785 memcpy(conn->classmark.classmark2, classmark2, classmark2_len);
786 conn->classmark.classmark2_len = classmark2_len;
787
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200788 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +0200789 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +0200790 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +0200791 net->vlr, conn,
792 VLR_PR_ARQ_T_CM_SERV_REQ, mi-1, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200793 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +0100794 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +0200795 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +0200796 is_utran);
Holger Freythereb443982009-06-04 13:58:42 +0000797
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +0200798 msc_subscr_conn_complete_layer_3(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +0200799 return 0;
Harald Welte4b634542008-12-27 01:55:51 +0000800}
801
Harald Welte49f8fcb2018-01-24 21:40:19 +0100802/* Receive a CM Re-establish Request */
803static int gsm48_rx_cm_reest_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
804{
805 uint8_t mi_type;
806 char mi_string[GSM48_MI_SIZE];
807 struct gsm48_hdr *gh = msgb_l3(msg);
808
809 uint8_t classmark2_len = gh->data[1];
810 uint8_t *classmark2 = gh->data+2;
811 uint8_t mi_len = *(classmark2 + classmark2_len);
812 uint8_t *mi = (classmark2 + classmark2_len + 1);
813
814 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
815 mi_type = mi[0] & GSM_MI_TYPE_MASK;
816 DEBUGP(DMM, "<- CM RE-ESTABLISH REQUEST MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
817
818 /* we don't support CM call re-establishment */
819 return msc_gsm48_tx_mm_serv_rej(conn, GSM48_REJECT_SRV_OPT_NOT_SUPPORTED);
820}
821
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200822static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000823{
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200824 struct gsm_network *network = conn->network;
Harald Welte2a139372009-02-22 21:14:55 +0000825 struct gsm48_hdr *gh = msgb_l3(msg);
826 struct gsm48_imsi_detach_ind *idi =
827 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200828 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200829 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +0200830 struct vlr_subscr *vsub = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000831
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200832 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2483f1b2016-06-19 18:06:02 +0200833 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s\n",
834 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000835
Neels Hofmeyra9f2bb52016-05-09 21:09:47 +0200836 rate_ctr_inc(&network->msc_ctrs->ctr[MSC_CTR_LOC_UPDATE_TYPE_DETACH]);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100837
Harald Welte2a139372009-02-22 21:14:55 +0000838 switch (mi_type) {
839 case GSM_MI_TYPE_TMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200840 vsub = vlr_subscr_find_by_tmsi(network->vlr,
841 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000842 break;
843 case GSM_MI_TYPE_IMSI:
Harald Welte2483f1b2016-06-19 18:06:02 +0200844 vsub = vlr_subscr_find_by_imsi(network->vlr, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000845 break;
846 case GSM_MI_TYPE_IMEI:
847 case GSM_MI_TYPE_IMEISV:
848 /* no sim card... FIXME: what to do ? */
Harald Welte2483f1b2016-06-19 18:06:02 +0200849 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unimplemented mobile identity type\n",
850 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000851 break;
Alexander Couzensfbd96f52016-08-29 18:40:02 +0200852 default:
Harald Welte2483f1b2016-06-19 18:06:02 +0200853 LOGP(DMM, LOGL_ERROR, "MI(%s)=%s: unknown mobile identity type\n",
854 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000855 break;
856 }
857
Harald Welte2483f1b2016-06-19 18:06:02 +0200858 /* TODO? We used to remember the subscriber's classmark1 here and
859 * stored it in the old sqlite db, but now we store it in a conn that
860 * will be discarded anyway: */
861 conn->classmark.classmark1 = idi->classmark1;
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200862
Harald Welte2483f1b2016-06-19 18:06:02 +0200863 if (!vsub) {
864 LOGP(DMM, LOGL_ERROR, "IMSI DETACH for unknown subscriber MI(%s)=%s\n",
865 gsm48_mi_type_name(mi_type), mi_string);
866 } else {
867 LOGP(DMM, LOGL_INFO, "IMSI DETACH for %s\n", vlr_subscr_name(vsub));
Neels Hofmeyrfacd57a2017-12-15 03:48:48 +0100868
869 if (vsub->cs.is_paging)
870 subscr_paging_cancel(vsub, GSM_PAGING_EXPIRED);
871
Harald Welte2483f1b2016-06-19 18:06:02 +0200872 vlr_subscr_rx_imsi_detach(vsub);
873 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_DETACHED, vsub);
874 vlr_subscr_put(vsub);
875 }
Harald Welte2a139372009-02-22 21:14:55 +0000876
Harald Welte2483f1b2016-06-19 18:06:02 +0200877 msc_subscr_conn_close(conn, 0);
Harald Welte2a139372009-02-22 21:14:55 +0000878 return 0;
879}
880
Harald Welted2a7f5a2009-06-05 20:08:20 +0000881static int gsm48_rx_mm_status(struct msgb *msg)
882{
883 struct gsm48_hdr *gh = msgb_l3(msg);
884
885 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
886
887 return 0;
888}
889
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100890static int parse_gsm_auth_resp(uint8_t *res, uint8_t *res_len,
891 struct gsm_subscriber_connection *conn,
892 struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100893{
894 struct gsm48_hdr *gh = msgb_l3(msg);
895 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100896
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100897 if (msgb_l3len(msg) < sizeof(*gh) + sizeof(*ar)) {
898 LOGP(DMM, LOGL_ERROR,
899 "%s: MM AUTHENTICATION RESPONSE:"
900 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200901 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100902 return -EINVAL;
903 }
904
905 *res_len = sizeof(ar->sres);
906 memcpy(res, ar->sres, sizeof(ar->sres));
907 return 0;
908}
909
910static int parse_umts_auth_resp(uint8_t *res, uint8_t *res_len,
911 struct gsm_subscriber_connection *conn,
912 struct msgb *msg)
913{
914 struct gsm48_hdr *gh;
915 uint8_t *data;
916 uint8_t iei;
917 uint8_t ie_len;
918 unsigned int data_len;
919
920 /* First parse the GSM part */
921 if (parse_gsm_auth_resp(res, res_len, conn, msg))
922 return -EINVAL;
923 OSMO_ASSERT(*res_len == 4);
924
925 /* Then add the extended res part */
926 gh = msgb_l3(msg);
927 data = gh->data + sizeof(struct gsm48_auth_resp);
928 data_len = msgb_l3len(msg) - (data - (uint8_t*)msgb_l3(msg));
929
930 if (data_len < 3) {
931 LOGP(DMM, LOGL_ERROR,
932 "%s: MM AUTHENTICATION RESPONSE:"
933 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200934 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100935 return -EINVAL;
936 }
937
938 iei = data[0];
939 ie_len = data[1];
940 if (iei != GSM48_IE_AUTH_RES_EXT) {
941 LOGP(DMM, LOGL_ERROR,
942 "%s: MM R99 AUTHENTICATION RESPONSE:"
943 " expected IEI 0x%02x, got 0x%02x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200944 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100945 GSM48_IE_AUTH_RES_EXT, iei);
946 return -EINVAL;
947 }
948
949 if (ie_len > 12) {
950 LOGP(DMM, LOGL_ERROR,
951 "%s: MM R99 AUTHENTICATION RESPONSE:"
952 " extended Auth Resp IE 0x%02x is too large: %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200953 vlr_subscr_name(conn->vsub), GSM48_IE_AUTH_RES_EXT, ie_len);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100954 return -EINVAL;
955 }
956
957 *res_len += ie_len;
958 memcpy(res + 4, &data[2], ie_len);
959 return 0;
960}
961
962/* Chapter 9.2.3: Authentication Response */
963static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
964{
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100965 uint8_t res[16];
966 uint8_t res_len;
967 int rc;
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100968 bool is_umts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100969
Harald Welte2483f1b2016-06-19 18:06:02 +0200970 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100971 LOGP(DMM, LOGL_ERROR,
972 "MM AUTHENTICATION RESPONSE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +0200973 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100974 return -EINVAL;
975 }
976
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100977 is_umts = (msgb_l3len(msg) > sizeof(struct gsm48_hdr) + sizeof(struct gsm48_auth_resp));
978
979 if (is_umts)
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100980 rc = parse_umts_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100981 else
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100982 rc = parse_gsm_auth_resp(res, &res_len, conn, msg);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100983
984 if (rc) {
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100985 LOGP(DMM, LOGL_ERROR,
986 "%s: MM AUTHENTICATION RESPONSE: invalid: parsing %s AKA Auth Response"
987 " failed with rc=%d; dispatching zero length SRES/RES to trigger failure\n",
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100988 vlr_subscr_name(conn->vsub), is_umts ? "UMTS" : "GSM", rc);
Neels Hofmeyr8e0af0b2018-03-10 03:32:18 +0100989 memset(res, 0, sizeof(res));
990 res_len = 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100991 }
992
993 DEBUGP(DMM, "%s: MM %s AUTHENTICATION RESPONSE (%s = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +0200994 vlr_subscr_name(conn->vsub),
Neels Hofmeyrd97821f2018-03-10 04:51:39 +0100995 is_umts ? "UMTS" : "GSM", is_umts ? "res" : "sres",
Neels Hofmeyr22a735b2017-02-03 01:22:40 +0100996 osmo_hexdump_nospc(res, res_len));
997
Neels Hofmeyr25f69d52018-03-10 03:44:06 +0100998 return vlr_subscr_rx_auth_resp(conn->vsub, classmark_is_r99(&conn->classmark),
Harald Welte2483f1b2016-06-19 18:06:02 +0200999 conn->via_ran == RAN_UTRAN_IU,
1000 res, res_len);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001001}
1002
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001003static int gsm48_rx_mm_auth_fail(struct gsm_subscriber_connection *conn, struct msgb *msg)
1004{
1005 struct gsm48_hdr *gh = msgb_l3(msg);
1006 uint8_t cause;
1007 uint8_t auts_tag;
1008 uint8_t auts_len;
1009 uint8_t *auts;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001010
Harald Welte2483f1b2016-06-19 18:06:02 +02001011 if (!conn->vsub) {
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001012 LOGP(DMM, LOGL_ERROR,
1013 "MM R99 AUTHENTICATION FAILURE: invalid: no subscriber\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001014 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001015 return -EINVAL;
1016 }
1017
1018 if (msgb_l3len(msg) < sizeof(*gh) + 1) {
1019 LOGP(DMM, LOGL_ERROR,
1020 "%s: MM R99 AUTHENTICATION FAILURE:"
1021 " l3 length invalid: %u\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001022 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1023 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001024 return -EINVAL;
1025 }
1026
1027 cause = gh->data[0];
1028
1029 if (cause != GSM48_REJECT_SYNCH_FAILURE) {
1030 LOGP(DMM, LOGL_INFO,
1031 "%s: MM R99 AUTHENTICATION FAILURE: cause 0x%0x\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001032 vlr_subscr_name(conn->vsub), cause);
1033 vlr_subscr_rx_auth_fail(conn->vsub, NULL);
1034 return 0;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001035 }
1036
1037 /* This is a Synch Failure procedure, which should pass an AUTS to
1038 * resynchronize the sequence nr with the HLR. Expecting exactly one
1039 * TLV with 14 bytes of AUTS. */
1040
1041 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2) {
1042 LOGP(DMM, LOGL_INFO,
1043 "%s: MM R99 AUTHENTICATION FAILURE:"
1044 " invalid Synch Failure: missing AUTS IE\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001045 vlr_subscr_name(conn->vsub));
1046 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001047 return -EINVAL;
1048 }
1049
1050 auts_tag = gh->data[1];
1051 auts_len = gh->data[2];
1052 auts = &gh->data[3];
1053
1054 if (auts_tag != GSM48_IE_AUTS
1055 || auts_len != 14) {
1056 LOGP(DMM, LOGL_INFO,
1057 "%s: MM R99 AUTHENTICATION FAILURE:"
1058 " invalid Synch Failure:"
1059 " expected AUTS IE 0x%02x of 14 bytes,"
1060 " got IE 0x%02x of %u bytes\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001061 vlr_subscr_name(conn->vsub),
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001062 GSM48_IE_AUTS, auts_tag, auts_len);
Harald Welte2483f1b2016-06-19 18:06:02 +02001063 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001064 return -EINVAL;
1065 }
1066
1067 if (msgb_l3len(msg) < sizeof(*gh) + 1 + 2 + auts_len) {
1068 LOGP(DMM, LOGL_INFO,
1069 "%s: MM R99 AUTHENTICATION FAILURE:"
1070 " invalid Synch Failure msg: message truncated (%u)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001071 vlr_subscr_name(conn->vsub), msgb_l3len(msg));
1072 msc_subscr_conn_close(conn, GSM_CAUSE_AUTH_FAILED);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001073 return -EINVAL;
1074 }
1075
1076 /* We have an AUTS IE with exactly 14 bytes of AUTS and the msgb is
1077 * large enough. */
1078
1079 DEBUGP(DMM, "%s: MM R99 AUTHENTICATION SYNCH (AUTS = %s)\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001080 vlr_subscr_name(conn->vsub), osmo_hexdump_nospc(auts, 14));
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001081
Harald Welte2483f1b2016-06-19 18:06:02 +02001082 return vlr_subscr_rx_auth_fail(conn->vsub, auts);
1083}
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001084
Harald Welte2483f1b2016-06-19 18:06:02 +02001085static int gsm48_rx_mm_tmsi_reall_compl(struct gsm_subscriber_connection *conn)
1086{
1087 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
1088 vlr_subscr_name(conn->vsub));
1089 if (!conn->vsub) {
1090 LOGP(DMM, LOGL_ERROR,
1091 "Rx MM TMSI Reallocation Complete: invalid: no subscriber\n");
1092 return -EINVAL;
1093 }
1094 return vlr_subscr_rx_tmsi_reall_compl(conn->vsub);
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001095}
1096
Harald Weltebf5e8df2009-02-03 12:59:45 +00001097/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001098static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001099{
1100 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001101 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001102
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001103 switch (gsm48_hdr_msg_type(gh)) {
Harald Welte52b1f982008-12-23 20:25:15 +00001104 case GSM48_MT_MM_LOC_UPD_REQUEST:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001105 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001106 break;
1107 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001108 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001109 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001110 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001111 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001112 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001113 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001114 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001115 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001116 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte2483f1b2016-06-19 18:06:02 +02001117 rc = gsm48_rx_mm_tmsi_reall_compl(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001118 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001119 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001120 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001121 break;
1122 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01001123 rc = gsm48_rx_cm_reest_req(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001124 break;
1125 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001126 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001127 break;
Neels Hofmeyr22a735b2017-02-03 01:22:40 +01001128 case GSM48_MT_MM_AUTH_FAIL:
1129 rc = gsm48_rx_mm_auth_fail(conn, msg);
1130 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001131 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001132 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001133 gh->msg_type);
1134 break;
1135 }
1136
1137 return rc;
1138}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001139
Harald Welte2483f1b2016-06-19 18:06:02 +02001140static uint8_t *gsm48_cm2_get_mi(uint8_t *classmark2_lv, unsigned int tot_len)
1141{
1142 /* Check the size for the classmark */
1143 if (tot_len < 1 + *classmark2_lv)
1144 return NULL;
1145
1146 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
1147 if (tot_len < 2 + *classmark2_lv + mi_lv[0])
1148 return NULL;
1149
1150 return mi_lv;
1151}
1152
Harald Welte2d35ae62009-02-06 12:02:13 +00001153/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001154static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001155{
Harald Welte2483f1b2016-06-19 18:06:02 +02001156 struct gsm_network *net = conn->network;
Harald Welte2d35ae62009-02-06 12:02:13 +00001157 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001158 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001159 uint8_t *classmark2_lv = gh->data + 1;
Harald Welte2483f1b2016-06-19 18:06:02 +02001160 uint8_t *mi_lv;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001161 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001162 char mi_string[GSM48_MI_SIZE];
Harald Welte2483f1b2016-06-19 18:06:02 +02001163 struct osmo_location_area_id lai;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001164 bool is_utran;
Harald Welte2483f1b2016-06-19 18:06:02 +02001165
Neels Hofmeyr379d5792018-02-22 04:04:54 +01001166 lai.plmn = conn->network->plmn;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001167 lai.lac = conn->lac;
Harald Welte2d35ae62009-02-06 12:02:13 +00001168
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001169 resp = (struct gsm48_pag_resp *) &gh->data[0];
1170 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1171 mi_string, &mi_type);
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001172
1173 if (msc_subscr_conn_is_establishing_auth_ciph(conn)) {
1174 LOGP(DMM, LOGL_ERROR,
1175 "Ignoring Paging Response, conn already busy establishing authenticity\n");
1176 return 0;
1177 }
1178
1179 if (msc_subscr_conn_is_accepted(conn)) {
1180 LOGP(DMM, LOGL_ERROR, "Ignoring Paging Response, conn already established\n");
1181 return 0;
1182 }
1183
1184 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n", gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001185
Harald Welte2483f1b2016-06-19 18:06:02 +02001186 mi_lv = gsm48_cm2_get_mi(classmark2_lv, msgb_l3len(msg) - sizeof(*gh));
1187 if (!mi_lv) {
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001188 LOGP(DRR, LOGL_ERROR, "PAGING RESPONSE: invalid Mobile Identity\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02001189 return -1;
Harald Weltefe18d8f2009-02-22 21:14:24 +00001190 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001191
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001192 msc_subscr_conn_update_id(conn, COMPLETE_LAYER3_PAGING_RESP, mi_string);
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001193
Harald Welte2483f1b2016-06-19 18:06:02 +02001194 memcpy(conn->classmark.classmark2, classmark2_lv+1, *classmark2_lv);
1195 conn->classmark.classmark2_len = *classmark2_lv;
Neels Hofmeyr6d804b12017-02-18 22:20:46 +01001196
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001197 is_utran = (conn->via_ran == RAN_UTRAN_IU);
Neels Hofmeyr4d3a66b2018-03-31 18:45:59 +02001198 vlr_proc_acc_req(conn->fi,
Neels Hofmeyr16c42b52018-04-02 12:26:16 +02001199 SUBSCR_CONN_E_ACCEPTED, SUBSCR_CONN_E_CN_CLOSE, NULL,
Harald Welte2483f1b2016-06-19 18:06:02 +02001200 net->vlr, conn,
1201 VLR_PR_ARQ_T_PAGING_RESP, mi_lv, &lai,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001202 is_utran || conn->network->authentication_required,
Harald Welte7b222aa2017-12-23 19:30:32 +01001203 is_utran || conn->network->a5_encryption_mask > 0x01,
Harald Welte2483f1b2016-06-19 18:06:02 +02001204 classmark_is_r99(&conn->classmark),
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001205 is_utran);
Holger Freyther053e09d2009-02-14 22:51:06 +00001206
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02001207 msc_subscr_conn_complete_layer_3(conn);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001208 return 0;
Harald Welte2d35ae62009-02-06 12:02:13 +00001209}
1210
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001211static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001212{
1213 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001214 uint8_t apdu_id_flags;
1215 uint8_t apdu_len;
1216 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001217
1218 apdu_id_flags = gh->data[0];
1219 apdu_len = gh->data[1];
1220 apdu_data = gh->data+2;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001221
Max8dc8f232017-02-09 19:13:02 +01001222 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001223 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001224
Harald Welte2483f1b2016-06-19 18:06:02 +02001225 /* we're not using the app info blob anywhere, so ignore. */
1226#if 0
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001227 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte2483f1b2016-06-19 18:06:02 +02001228#else
1229 return 0;
1230#endif
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001231}
1232
Harald Weltebf5e8df2009-02-03 12:59:45 +00001233/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001234static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001235{
1236 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001237 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001238
1239 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001240 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001241 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001242 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001243 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001244 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001245 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001246 default:
Philippd37981e2016-10-10 15:11:41 +02001247 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented %s GSM 04.08 RR "
1248 "message\n", gsm48_rr_msg_name(gh->msg_type));
Harald Welte52b1f982008-12-23 20:25:15 +00001249 break;
1250 }
1251
Harald Welte2d35ae62009-02-06 12:02:13 +00001252 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001253}
1254
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001255int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1256 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001257{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001258 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 APP INF");
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001259 struct gsm48_hdr *gh;
1260
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001261 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1262 apdu_id, apdu_len);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001263
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001264 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1265 gh->proto_discr = GSM48_PDISC_RR;
1266 gh->msg_type = GSM48_MT_RR_APP_INFO;
1267 gh->data[0] = apdu_id;
1268 gh->data[1] = apdu_len;
1269 memcpy(gh->data+2, apdu, apdu_len);
1270
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001271 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001272}
1273
Alexander Couzens92f552f2016-08-23 07:32:27 +02001274/* FIXME: this count_statistics is a state machine behaviour. we should convert
1275 * the complete call control into a state machine. Afterwards we can move this
1276 * code into state transitions.
1277 */
1278static void count_statistics(struct gsm_trans *trans, int new_state)
1279{
1280 int old_state = trans->cc.state;
1281 struct rate_ctr_group *msc = trans->net->msc_ctrs;
1282
1283 if (old_state == new_state)
1284 return;
1285
1286 /* state incoming */
1287 switch (new_state) {
1288 case GSM_CSTATE_ACTIVE:
1289 osmo_counter_inc(trans->net->active_calls);
1290 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_ACTIVE]);
1291 break;
1292 }
1293
1294 /* state outgoing */
1295 switch (old_state) {
1296 case GSM_CSTATE_ACTIVE:
1297 osmo_counter_dec(trans->net->active_calls);
1298 if (new_state == GSM_CSTATE_DISCONNECT_REQ ||
1299 new_state == GSM_CSTATE_DISCONNECT_IND)
1300 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_COMPLETE]);
1301 else
1302 rate_ctr_inc(&msc->ctr[MSC_CTR_CALL_INCOMPLETE]);
1303 break;
1304 }
1305}
1306
Harald Welte4bc90a12008-12-27 16:32:52 +00001307/* Call Control */
1308
Harald Welte7584aea2009-02-11 11:44:12 +00001309/* The entire call control code is written in accordance with Figure 7.10c
1310 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1311 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1312 * it for voice */
1313
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314static void new_cc_state(struct gsm_trans *trans, int state)
1315{
1316 if (state > 31 || state < 0)
1317 return;
1318
Neels Hofmeyrfef670b2017-11-22 01:57:00 +01001319 DEBUGP(DCC, "(ti %02x sub %s) new state %s -> %s\n",
1320 trans->transaction_id,
1321 vlr_subscr_name(trans->vsub),
1322 gsm48_cc_state_name(trans->cc.state),
1323 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001324
Alexander Couzens92f552f2016-08-23 07:32:27 +02001325 count_statistics(trans, state);
Harald Weltedcaf5652009-07-23 18:56:43 +02001326 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001327}
1328
1329static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001330{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001331 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STATUS");
Harald Welte4bc90a12008-12-27 16:32:52 +00001332 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001333 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001334
Harald Welte4bc90a12008-12-27 16:32:52 +00001335 gh->msg_type = GSM48_MT_CC_STATUS;
1336
1337 cause = msgb_put(msg, 3);
1338 cause[0] = 2;
1339 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1340 cause[2] = 0x80 | 30; /* response to status inquiry */
1341
1342 call_state = msgb_put(msg, 1);
1343 call_state[0] = 0xc0 | 0x00;
1344
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001345 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001346}
1347
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001348static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001349 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001350{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001351 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 TX SIMPLE");
Harald Welte4bc90a12008-12-27 16:32:52 +00001352 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1353
Harald Welte6f4b7532008-12-29 00:39:37 +00001354 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001355 gh->msg_type = msg_type;
1356
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001357 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001358}
1359
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1361{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001362 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001363 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001364 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001365 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 }
1367}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001368
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1370 int msg_type, struct gsm_mncc *mncc)
1371{
1372 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001373 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374
Philipp Maierfbf66102017-04-09 12:32:51 +02001375 DEBUGP(DMNCC, "transmit message %s\n", get_mncc_name(msg_type));
1376
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001377#if BEFORE_MSCSPLIT
1378 /* Re-enable this log output once we can obtain this information via
1379 * A-interface, see OS#2391. */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001381 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001382 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001384 trans->conn->lchan->ts->trx->bts->nr,
1385 trans->conn->lchan->ts->trx->nr,
1386 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte2483f1b2016-06-19 18:06:02 +02001387 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388 get_mncc_name(msg_type));
1389 else
1390 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1391 "Sending '%s' to MNCC.\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001392 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 get_mncc_name(msg_type));
1394 else
1395 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1396 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001397#else
1398 DEBUGP(DCC, "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1399#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08001400
1401 mncc->msg_type = msg_type;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001402
Harald Welte966636f2009-06-26 19:39:35 +02001403 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 if (!msg)
1405 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001406
1407 data = msgb_put(msg, sizeof(struct gsm_mncc));
1408 memcpy(data, mncc, sizeof(struct gsm_mncc));
1409
Harald Welte54209c22010-12-22 23:43:29 +01001410 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001411
1412 return 0;
1413}
1414
1415int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001416 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001417{
1418 struct gsm_mncc rel;
1419
Harald Welte92f70c52009-06-12 01:54:08 +08001420 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001421 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001422 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001423 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1424 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001425 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1426}
1427
Harald Weltedcaf5652009-07-23 18:56:43 +02001428/* Call Control Specific transaction release.
1429 * gets called by trans_free, DO NOT CALL YOURSELF! */
1430void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001431{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001432 gsm48_stop_cc_timer(trans);
1433
Philipp Maier621ba032017-11-07 17:19:25 +01001434 /* Initiate the teardown of the related connections on the MGW */
1435 msc_mgcp_call_release(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001436
Harald Welte4bfdfe72009-06-10 23:11:52 +08001437 /* send release to L4, if callref still exists */
1438 if (trans->callref) {
1439 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001440 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001441 GSM48_CAUSE_LOC_PRN_S_LU,
1442 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Neels Hofmeyrdc2514b2018-04-09 23:29:14 +02001443 /* This is a final freeing of the transaction. The MNCC release may have triggered the
1444 * T308 release timer, but we don't have the luxury of graceful CC Release here. */
1445 gsm48_stop_cc_timer(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001446 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001447 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448 new_cc_state(trans, GSM_CSTATE_NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001449}
1450
1451static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001452
Harald Welte09e38af2009-02-16 22:52:23 +00001453/* call-back from paging the B-end of the connection */
1454static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001455 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001456{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001457 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001458 struct gsm_trans *transt = _transt;
Pau Espin Pedrol25adebe2018-04-24 11:46:58 +02001459 enum gsm_paging_event paging_event = event;
Harald Weltec05677b2009-06-26 20:17:06 +02001460
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001461 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001462
Pau Espin Pedrol25adebe2018-04-24 11:46:58 +02001463 switch (paging_event) {
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001464 case GSM_PAGING_SUCCEEDED:
Harald Welte2483f1b2016-06-19 18:06:02 +02001465 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1466 vlr_subscr_msisdn_or_name(transt->vsub));
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001467 OSMO_ASSERT(conn);
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001468 /* Assign conn */
Neels Hofmeyr82be67d2017-11-22 15:43:03 +01001469 transt->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001470 /* send SETUP request to called party */
1471 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1472 break;
1473 case GSM_PAGING_EXPIRED:
1474 case GSM_PAGING_BUSY:
1475 DEBUGP(DCC, "Paging subscr %s expired!\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02001476 vlr_subscr_msisdn_or_name(transt->vsub));
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001477 /* Temporarily out of order */
1478 mncc_release_ind(transt->net, transt,
1479 transt->callref,
1480 GSM48_CAUSE_LOC_PRN_S_LU,
1481 GSM48_CC_CAUSE_DEST_OOO);
1482 transt->callref = 0;
1483 transt->paging_request = NULL;
1484 trans_free(transt);
1485 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001486 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001487
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001488 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001489 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001490}
Harald Welte7584aea2009-02-11 11:44:12 +00001491
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492/* bridge channels of two transactions */
Harald Welte53d51f52015-12-03 14:59:04 +01001493static int tch_bridge(struct gsm_network *net, struct gsm_mncc_bridge *bridge)
Harald Welte7ccf7782009-02-17 01:43:01 +00001494{
Harald Welte53d51f52015-12-03 14:59:04 +01001495 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[0]);
1496 struct gsm_trans *trans2 = trans_find_by_callref(net, bridge->callref[1]);
Philipp Maier621ba032017-11-07 17:19:25 +01001497 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001498
Harald Welte4bfdfe72009-06-10 23:11:52 +08001499 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001500 return -EIO;
1501
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001502 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001503 return -EIO;
1504
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001505 /* Which subscriber do we want to track trans1 or trans2? */
Harald Welte2483f1b2016-06-19 18:06:02 +02001506 log_set_context(LOG_CTX_VLR_SUBSCR, trans1->vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001507
Philipp Maier621ba032017-11-07 17:19:25 +01001508 /* Bridge RTP streams */
1509 rc = msc_mgcp_call_complete(trans1, trans2->conn->rtp.local_port_cn,
1510 trans2->conn->rtp.local_addr_cn);
1511 if (rc)
1512 return -EINVAL;
1513
1514 rc = msc_mgcp_call_complete(trans2, trans1->conn->rtp.local_port_cn,
1515 trans1->conn->rtp.local_addr_cn);
1516 if (rc)
1517 return -EINVAL;
1518
1519 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001520}
1521
Harald Welte4bfdfe72009-06-10 23:11:52 +08001522static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1523{
1524 DEBUGP(DCC, "-> STATUS ENQ\n");
1525 return gsm48_cc_tx_status(trans, msg);
1526}
1527
1528static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1529static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1530
1531static void gsm48_cc_timeout(void *arg)
1532{
1533 struct gsm_trans *trans = arg;
1534 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001535 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1536 int mo_location = GSM48_CAUSE_LOC_USER;
1537 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1538 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001539 struct gsm_mncc mo_rel, l4_rel;
1540
1541 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1542 mo_rel.callref = trans->callref;
1543 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1544 l4_rel.callref = trans->callref;
1545
Harald Weltedcaf5652009-07-23 18:56:43 +02001546 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001547 case 0x303:
1548 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001549 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001550 break;
1551 case 0x310:
1552 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001553 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001554 break;
1555 case 0x313:
1556 disconnect = 1;
1557 /* unknown, did not find it in the specs */
1558 break;
1559 case 0x301:
1560 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001561 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001562 break;
1563 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001564 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001565 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001566 gsm48_cc_tx_release(trans, &trans->cc.msg);
1567 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001568 break; /* stay in release state */
1569 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001570 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001571 return;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572 case 0x306:
1573 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001574 mo_cause = trans->cc.msg.cause.value;
1575 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001576 break;
1577 case 0x323:
1578 disconnect = 1;
1579 break;
1580 default:
1581 release = 1;
1582 }
1583
1584 if (release && trans->callref) {
1585 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001586 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001587 l4_location, l4_cause);
1588 trans->callref = 0;
1589 }
1590
1591 if (disconnect && trans->callref) {
1592 /* process disconnect towards layer 4 */
1593 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001594 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001595 }
1596
1597 /* process disconnect towards mobile station */
1598 if (disconnect || release) {
1599 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001600 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1601 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1602 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001603 mo_rel.cause.diag_len = 3;
1604
1605 if (disconnect)
1606 gsm48_cc_tx_disconnect(trans, &mo_rel);
1607 if (release)
1608 gsm48_cc_tx_release(trans, &mo_rel);
1609 }
1610
1611}
1612
Max3ffce192016-04-25 15:22:00 +02001613/* disconnect both calls from the bridge */
1614static inline void disconnect_bridge(struct gsm_network *net,
1615 struct gsm_mncc_bridge *bridge, int err)
1616{
1617 struct gsm_trans *trans0 = trans_find_by_callref(net, bridge->callref[0]);
1618 struct gsm_trans *trans1 = trans_find_by_callref(net, bridge->callref[1]);
1619 struct gsm_mncc mx_rel;
1620 if (!trans0 || !trans1)
1621 return;
1622
1623 DEBUGP(DCC, "Failed to bridge TCH for calls %x <-> %x :: %s \n",
1624 trans0->callref, trans1->callref, strerror(err));
1625
1626 memset(&mx_rel, 0, sizeof(struct gsm_mncc));
1627 mncc_set_cause(&mx_rel, GSM48_CAUSE_LOC_INN_NET,
1628 GSM48_CC_CAUSE_CHAN_UNACCEPT);
1629
1630 mx_rel.callref = trans0->callref;
1631 gsm48_cc_tx_disconnect(trans0, &mx_rel);
1632
1633 mx_rel.callref = trans1->callref;
1634 gsm48_cc_tx_disconnect(trans1, &mx_rel);
1635}
1636
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1638 int sec, int micro)
1639{
1640 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Pablo Neira Ayuso51215762017-05-08 20:57:52 +02001641 osmo_timer_setup(&trans->cc.timer, gsm48_cc_timeout, trans);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001642 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001643 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644}
1645
1646static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1647{
1648 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01001649 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1651 struct tlv_parsed tp;
1652 struct gsm_mncc setup;
1653
1654 memset(&setup, 0, sizeof(struct gsm_mncc));
1655 setup.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001656
Harald Welte474d19f2010-03-02 23:18:30 +01001657 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 /* emergency setup is identified by msg_type */
Harald Welte0dd01372018-01-24 11:06:19 +01001659 if (msg_type == GSM48_MT_CC_EMERG_SETUP) {
1660 setup.fields |= MNCC_F_EMERGENCY;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 setup.emergency = 1;
Harald Welte69c54a82018-02-09 20:41:14 +01001662 /* use destination number as configured by user (if any) */
1663 if (trans->net->emergency.route_to_msisdn) {
1664 setup.fields |= MNCC_F_CALLED;
1665 setup.called.type = 0; /* unknown */
1666 setup.called.plan = 0; /* unknown */
1667 OSMO_STRLCPY_ARRAY(setup.called.number,
1668 trans->net->emergency.route_to_msisdn);
1669 }
Harald Welte0dd01372018-01-24 11:06:19 +01001670 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671
1672 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001673 setup.fields |= MNCC_F_CALLING;
Max98f74672018-02-05 12:57:06 +01001674 OSMO_STRLCPY_ARRAY(setup.calling.number, trans->vsub->msisdn);
1675 OSMO_STRLCPY_ARRAY(setup.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001676
Harald Welte4bfdfe72009-06-10 23:11:52 +08001677 /* bearer capability */
1678 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1679 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001680 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001681 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001682
1683 /* Create a copy of the bearer capability
1684 * in the transaction struct, so we can use
1685 * this information later */
1686 memcpy(&trans->bearer_cap,&setup.bearer_cap,
1687 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688 }
1689 /* facility */
1690 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1691 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001692 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001693 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1694 }
1695 /* called party bcd number */
1696 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1697 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001698 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1700 }
1701 /* user-user */
1702 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1703 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001704 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001705 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1706 }
1707 /* ss-version */
1708 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1709 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001710 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1712 }
1713 /* CLIR suppression */
1714 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1715 setup.clir.sup = 1;
1716 /* CLIR invocation */
1717 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1718 setup.clir.inv = 1;
1719 /* cc cap */
1720 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1721 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001722 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1724 }
1725
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 new_cc_state(trans, GSM_CSTATE_INITIATED);
1727
Harald Welte79826e12018-01-24 10:50:24 +01001728 LOGP(DCC, setup.emergency ? LOGL_NOTICE : LOGL_INFO, "Subscriber %s (%s) sends %sSETUP to %s\n",
1729 vlr_subscr_name(trans->vsub), trans->vsub->msisdn, setup.emergency ? "EMERGENCY_" : "",
Harald Welte (local)47df3992009-12-26 19:45:03 +01001730 setup.called.number);
1731
Alexander Couzensb847a212016-08-02 11:34:11 +02001732 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001733
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001735 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736
Harald Welte13cac662009-07-29 12:10:35 +02001737 /* MNCC code will modify the channel asynchronously, we should
1738 * ipaccess-bind only after the modification has been made to the
1739 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 return 0;
1741}
1742
1743static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001744{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001745 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC STUP");
Harald Welte65e74cc2008-12-29 01:55:35 +00001746 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001748 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001749
Harald Welte7ccf7782009-02-17 01:43:01 +00001750 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001751
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 /* transaction id must not be assigned */
1753 if (trans->transaction_id != 0xff) { /* unasssigned */
1754 DEBUGP(DCC, "TX Setup with assigned transaction. "
1755 "This is not allowed!\n");
1756 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001757 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001758 GSM48_CAUSE_LOC_PRN_S_LU,
1759 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001761 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 return rc;
1763 }
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001764
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 /* Get free transaction_id */
Harald Welte2483f1b2016-06-19 18:06:02 +02001766 trans_id = trans_assign_trans_id(trans->net, trans->vsub,
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001767 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001768 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001769 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001770 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001771 GSM48_CAUSE_LOC_PRN_S_LU,
1772 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001774 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 return rc;
1776 }
Harald Welte78283ef2009-07-23 21:36:44 +02001777 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001778
Harald Welte65e74cc2008-12-29 01:55:35 +00001779 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001780
Harald Welte4bfdfe72009-06-10 23:11:52 +08001781 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001782
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 /* bearer capability */
Harald Welte32958872018-02-12 11:56:51 +01001784 if (setup->fields & MNCC_F_BEARER_CAP) {
1785 /* Create a copy of the bearer capability in the transaction struct, so we
1786 * can use this information later */
1787 memcpy(&trans->bearer_cap, &setup->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte55c8f352010-03-07 23:40:35 +01001788 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte32958872018-02-12 11:56:51 +01001789 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 /* facility */
1791 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001792 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793 /* progress */
1794 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001795 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 /* calling party BCD number */
1797 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001798 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 /* called party BCD number */
1800 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001801 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 /* user-user */
1803 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001804 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 /* redirecting party BCD number */
1806 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001807 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* signal */
1809 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001810 gsm48_encode_signal(msg, setup->signal);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001811
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001813
Alexander Couzensb847a212016-08-02 11:34:11 +02001814 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_SETUP]);
Harald Weltea29e43a2010-12-24 16:06:33 +01001815
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001816 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001817}
1818
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1820{
1821 struct gsm48_hdr *gh = msgb_l3(msg);
1822 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1823 struct tlv_parsed tp;
1824 struct gsm_mncc call_conf;
Philipp Maierfbf66102017-04-09 12:32:51 +02001825 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826
1827 gsm48_stop_cc_timer(trans);
1828 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1829
1830 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1831 call_conf.callref = trans->callref;
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02001832
Harald Welte474d19f2010-03-02 23:18:30 +01001833 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834#if 0
1835 /* repeat */
1836 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1837 call_conf.repeat = 1;
1838 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1839 call_conf.repeat = 2;
1840#endif
1841 /* bearer capability */
1842 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1843 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001844 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001845 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02001846
1847 /* Create a copy of the bearer capability
1848 * in the transaction struct, so we can use
1849 * this information later */
1850 memcpy(&trans->bearer_cap,&call_conf.bearer_cap,
1851 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852 }
1853 /* cause */
1854 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1855 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001856 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1858 }
1859 /* cc cap */
1860 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1861 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001862 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1864 }
1865
Andreas Eversberg035b8742013-09-19 09:27:06 +02001866 /* IMSI of called subscriber */
Max98f74672018-02-05 12:57:06 +01001867 OSMO_STRLCPY_ARRAY(call_conf.imsi, trans->vsub->imsi);
Andreas Eversberg035b8742013-09-19 09:27:06 +02001868
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1870
Philipp Maierfbf66102017-04-09 12:32:51 +02001871 /* Assign call (if not done yet) */
1872 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001873 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001874 trans->assignment_done = true;
1875 }
1876 else
1877 rc = 0;
1878
1879 /* don't continue, if there were problems with
1880 * the call assignment. */
1881 if (rc)
1882 return rc;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001883
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001884 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001885 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886}
1887
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001888static int gsm48_cc_tx_call_proc_and_assign(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889{
1890 struct gsm_mncc *proceeding = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001891 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001892 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001893 int rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1896
1897 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1898
1899 /* bearer capability */
Harald Welte4804c552018-02-12 12:30:54 +01001900 if (proceeding->fields & MNCC_F_BEARER_CAP) {
Harald Welte55c8f352010-03-07 23:40:35 +01001901 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01001902 memcpy(&trans->bearer_cap, &proceeding->bearer_cap, sizeof(trans->bearer_cap));
1903 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904 /* facility */
1905 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001906 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 /* progress */
1908 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001909 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02001911 rc = gsm48_conn_sendmsg(msg, trans->conn, trans);
1912 if (rc)
1913 return rc;
1914
Philipp Maierfbf66102017-04-09 12:32:51 +02001915 /* Assign call (if not done yet) */
1916 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01001917 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02001918 trans->assignment_done = true;
1919 }
1920 else
1921 rc = 0;
1922
1923 return rc;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924}
1925
1926static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1927{
1928 struct gsm48_hdr *gh = msgb_l3(msg);
1929 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1930 struct tlv_parsed tp;
1931 struct gsm_mncc alerting;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001932
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 gsm48_stop_cc_timer(trans);
1934 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1935
1936 memset(&alerting, 0, sizeof(struct gsm_mncc));
1937 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001938 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 /* facility */
1940 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1941 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1944 }
1945
1946 /* progress */
1947 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1948 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001949 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1951 }
1952 /* ss-version */
1953 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1954 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001955 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1957 }
1958
1959 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1960
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001961 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02001962 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963}
1964
1965static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1966{
1967 struct gsm_mncc *alerting = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001968 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC ALERT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1970
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 gh->msg_type = GSM48_MT_CC_ALERTING;
1972
1973 /* facility */
1974 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 /* progress */
1977 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 /* user-user */
1980 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001981 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982
1983 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02001984
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001985 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986}
1987
1988static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1989{
1990 struct gsm_mncc *progress = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01001991 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC PROGRESS");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1993
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994 gh->msg_type = GSM48_MT_CC_PROGRESS;
1995
1996 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001997 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 /* user-user */
1999 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002002 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003}
2004
2005static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2006{
2007 struct gsm_mncc *connect = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002008 struct msgb *msg = gsm48_msgb_alloc_name("GSN 04.08 CC CON");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2010
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 gh->msg_type = GSM48_MT_CC_CONNECT;
2012
2013 gsm48_stop_cc_timer(trans);
2014 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2015
2016 /* facility */
2017 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002018 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 /* progress */
2020 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002021 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 /* connected number */
2023 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 /* user-user */
2026 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002027 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028
2029 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2030
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002031 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032}
2033
2034static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2035{
2036 struct gsm48_hdr *gh = msgb_l3(msg);
2037 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2038 struct tlv_parsed tp;
2039 struct gsm_mncc connect;
2040
2041 gsm48_stop_cc_timer(trans);
2042
2043 memset(&connect, 0, sizeof(struct gsm_mncc));
2044 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002045 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002047 connect.fields |= MNCC_F_CONNECTED;
Max98f74672018-02-05 12:57:06 +01002048 OSMO_STRLCPY_ARRAY(connect.connected.number, trans->vsub->msisdn);
2049 OSMO_STRLCPY_ARRAY(connect.imsi, trans->vsub->imsi);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002050
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 /* facility */
2052 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2053 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2056 }
2057 /* user-user */
2058 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2059 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002060 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002061 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2062 }
2063 /* ss-version */
2064 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2065 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002066 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2068 }
2069
2070 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Alexander Couzensb847a212016-08-02 11:34:11 +02002071 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MT_CONNECT]);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002073 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074}
2075
2076
2077static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2078{
2079 struct gsm_mncc connect_ack;
2080
2081 gsm48_stop_cc_timer(trans);
2082
2083 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Alexander Couzensb847a212016-08-02 11:34:11 +02002084 rate_ctr_inc(&trans->net->msc_ctrs->ctr[MSC_CTR_CALL_MO_CONNECT_ACK]);
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002085
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2087 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002088
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002089 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 &connect_ack);
2091}
2092
2093static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2094{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002095 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC CON ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2097
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2099
2100 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2101
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002102 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103}
2104
2105static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2106{
2107 struct gsm48_hdr *gh = msgb_l3(msg);
2108 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2109 struct tlv_parsed tp;
2110 struct gsm_mncc disc;
2111
2112 gsm48_stop_cc_timer(trans);
2113
2114 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2115
2116 memset(&disc, 0, sizeof(struct gsm_mncc));
2117 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002118 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 /* cause */
2120 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2121 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2124 }
2125 /* facility */
2126 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2127 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002128 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2130 }
2131 /* user-user */
2132 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2133 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002134 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2136 }
2137 /* ss-version */
2138 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2139 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2142 }
2143
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002144 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145
2146}
2147
Harald Weltec66b71c2009-06-11 14:23:20 +08002148static struct gsm_mncc_cause default_cause = {
2149 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2150 .coding = 0,
2151 .rec = 0,
2152 .rec_val = 0,
2153 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2154 .diag_len = 0,
2155 .diag = { 0 },
2156};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157
2158static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2159{
2160 struct gsm_mncc *disc = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002161 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC DISC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2163
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2165
2166 gsm48_stop_cc_timer(trans);
2167 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2168
2169 /* cause */
2170 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002171 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 else
Harald Welte55c8f352010-03-07 23:40:35 +01002173 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174
2175 /* facility */
2176 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002177 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 /* progress */
2179 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002180 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 /* user-user */
2182 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002183 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002184
2185 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002186 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187
2188 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2189
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002190 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191}
2192
2193static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2194{
2195 struct gsm48_hdr *gh = msgb_l3(msg);
2196 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2197 struct tlv_parsed tp;
2198 struct gsm_mncc rel;
2199 int rc;
2200
2201 gsm48_stop_cc_timer(trans);
2202
2203 memset(&rel, 0, sizeof(struct gsm_mncc));
2204 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002205 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 /* cause */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2208 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2211 }
2212 /* facility */
2213 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2214 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002215 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2217 }
2218 /* user-user */
2219 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2220 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002221 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2223 }
2224 /* ss-version */
2225 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2226 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002227 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2229 }
2230
Harald Weltedcaf5652009-07-23 18:56:43 +02002231 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002233 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002235 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002236 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002237 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002238 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 }
2240
2241 new_cc_state(trans, GSM_CSTATE_NULL);
2242
2243 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002244 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245
2246 return rc;
2247}
2248
2249static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2250{
2251 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002252 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2254
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 gh->msg_type = GSM48_MT_CC_RELEASE;
2256
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 gsm48_stop_cc_timer(trans);
2258 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2259
2260 /* cause */
2261 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 /* facility */
2264 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 /* user-user */
2267 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002268 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269
Harald Weltedcaf5652009-07-23 18:56:43 +02002270 trans->cc.T308_second = 0;
2271 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272
Harald Weltedcaf5652009-07-23 18:56:43 +02002273 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002274 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2275
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002276 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277}
2278
2279static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2280{
2281 struct gsm48_hdr *gh = msgb_l3(msg);
2282 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2283 struct tlv_parsed tp;
2284 struct gsm_mncc rel;
2285 int rc = 0;
2286
2287 gsm48_stop_cc_timer(trans);
2288
2289 memset(&rel, 0, sizeof(struct gsm_mncc));
2290 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002291 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292 /* cause */
2293 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2294 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002295 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2297 }
2298 /* facility */
2299 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2300 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002301 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2303 }
2304 /* user-user */
2305 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2306 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002307 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2309 }
2310 /* ss-version */
2311 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2312 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002313 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2315 }
2316
2317 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002318 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002320 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 MNCC_REJ_IND, &rel);
2322 break;
2323 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002324 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 MNCC_REL_CNF, &rel);
2326 break;
2327 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002328 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329 MNCC_REL_IND, &rel);
2330 }
2331 }
2332
2333 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002334 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002335
2336 return rc;
2337}
2338
2339static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2340{
2341 struct gsm_mncc *rel = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002342 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC REL COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002344 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002345
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2347
2348 trans->callref = 0;
Alexander Couzensfbd96f52016-08-29 18:40:02 +02002349
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 gsm48_stop_cc_timer(trans);
2351
2352 /* cause */
2353 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 /* facility */
2356 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002357 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358 /* user-user */
2359 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002360 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361
Harald Weltec7782de2010-12-21 19:31:41 +01002362 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2363
Harald Weltedcaf5652009-07-23 18:56:43 +02002364 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365
Harald Weltec7782de2010-12-21 19:31:41 +01002366 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367}
2368
2369static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2370{
2371 struct gsm48_hdr *gh = msgb_l3(msg);
2372 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2373 struct tlv_parsed tp;
2374 struct gsm_mncc fac;
2375
2376 memset(&fac, 0, sizeof(struct gsm_mncc));
2377 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002378 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002379 /* facility */
2380 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2381 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002382 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2384 }
2385 /* ss-version */
2386 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2387 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002388 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2390 }
2391
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002392 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393}
2394
2395static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2396{
2397 struct gsm_mncc *fac = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002398 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC FAC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2400
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 gh->msg_type = GSM48_MT_CC_FACILITY;
2402
2403 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002404 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002406 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407}
2408
2409static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2410{
2411 struct gsm_mncc hold;
2412
2413 memset(&hold, 0, sizeof(struct gsm_mncc));
2414 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002415 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416}
2417
2418static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2419{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002420 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2422
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2424
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002425 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426}
2427
2428static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2429{
2430 struct gsm_mncc *hold_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002431 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC HLD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2433
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2435
2436 /* cause */
2437 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 else
Harald Welte55c8f352010-03-07 23:40:35 +01002440 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002442 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443}
2444
2445static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2446{
2447 struct gsm_mncc retrieve;
2448
2449 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2450 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002451 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002452 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453}
2454
2455static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2456{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002457 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2459
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2461
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002462 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2466{
2467 struct gsm_mncc *retrieve_rej = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002468 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC RETR REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2470
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2472
2473 /* cause */
2474 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002475 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 else
Harald Welte55c8f352010-03-07 23:40:35 +01002477 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002479 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480}
2481
2482static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2483{
2484 struct gsm48_hdr *gh = msgb_l3(msg);
2485 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2486 struct tlv_parsed tp;
2487 struct gsm_mncc dtmf;
2488
2489 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2490 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002491 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492 /* keypad facility */
2493 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2494 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2497 }
2498
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002499 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500}
2501
2502static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2503{
2504 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002505 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2507
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2509
2510 /* keypad */
2511 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002512 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513
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_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2518{
2519 struct gsm_mncc *dtmf = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002520 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2522
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2524
2525 /* cause */
2526 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002527 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 else
Harald Welte55c8f352010-03-07 23:40:35 +01002529 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002531 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532}
2533
2534static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2535{
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002536 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 DTMF STP ACK");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2538
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2540
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002541 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002542}
2543
2544static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2545{
2546 struct gsm_mncc dtmf;
2547
2548 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2549 dtmf.callref = trans->callref;
2550
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002551 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552}
2553
2554static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2555{
2556 struct gsm48_hdr *gh = msgb_l3(msg);
2557 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2558 struct tlv_parsed tp;
2559 struct gsm_mncc modify;
2560
2561 memset(&modify, 0, sizeof(struct gsm_mncc));
2562 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002563 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564 /* bearer capability */
2565 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2566 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002567 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002568 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002569
2570 /* Create a copy of the bearer capability
2571 * in the transaction struct, so we can use
2572 * this information later */
2573 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2574 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 }
2576
2577 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2578
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002579 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002580}
2581
2582static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2583{
2584 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002585 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2587
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588 gh->msg_type = GSM48_MT_CC_MODIFY;
2589
2590 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2591
2592 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002593 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002594 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595
2596 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2597
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002598 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599}
2600
2601static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2602{
2603 struct gsm48_hdr *gh = msgb_l3(msg);
2604 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2605 struct tlv_parsed tp;
2606 struct gsm_mncc modify;
2607
2608 gsm48_stop_cc_timer(trans);
2609
2610 memset(&modify, 0, sizeof(struct gsm_mncc));
2611 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002612 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613 /* bearer capability */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2615 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002616 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002618
2619 /* Create a copy of the bearer capability
2620 * in the transaction struct, so we can use
2621 * this information later */
2622 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2623 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624 }
2625
2626 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2627
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002628 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629}
2630
2631static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2632{
2633 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002634 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD COMPL");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2636
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2638
2639 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002640 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002641 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642
2643 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2644
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002645 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646}
2647
2648static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2649{
2650 struct gsm48_hdr *gh = msgb_l3(msg);
2651 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2652 struct tlv_parsed tp;
2653 struct gsm_mncc modify;
2654
2655 gsm48_stop_cc_timer(trans);
2656
2657 memset(&modify, 0, sizeof(struct gsm_mncc));
2658 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002659 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660 /* bearer capability */
2661 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2662 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002663 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Philipp Maierfbf66102017-04-09 12:32:51 +02002665
2666 /* Create a copy of the bearer capability
2667 * in the transaction struct, so we can use
2668 * this information later */
2669 memcpy(&trans->bearer_cap,&modify.bearer_cap,
2670 sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 }
2672 /* cause */
2673 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2674 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002675 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002676 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2677 }
2678
2679 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2680
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002681 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682}
2683
2684static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2685{
2686 struct gsm_mncc *modify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002687 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC MOD REJ");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2689
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2691
2692 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002693 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4804c552018-02-12 12:30:54 +01002694 memcpy(&trans->bearer_cap, &modify->bearer_cap, sizeof(trans->bearer_cap));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002696 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697
2698 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2699
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002700 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701}
2702
2703static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2704{
2705 struct gsm_mncc *notify = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002706 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 CC NOT");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2708
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 gh->msg_type = GSM48_MT_CC_NOTIFY;
2710
2711 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002712 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002714 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715}
2716
2717static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2718{
2719 struct gsm48_hdr *gh = msgb_l3(msg);
2720 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2721// struct tlv_parsed tp;
2722 struct gsm_mncc notify;
2723
2724 memset(&notify, 0, sizeof(struct gsm_mncc));
2725 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002726// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002728 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002730 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731}
2732
2733static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2734{
2735 struct gsm_mncc *user = arg;
Holger Hans Peter Freyther8239e062016-01-25 22:03:25 +01002736 struct msgb *msg = gsm48_msgb_alloc_name("GSM 04.08 USR INFO");
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2738
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 gh->msg_type = GSM48_MT_CC_USER_INFO;
2740
2741 /* user-user */
2742 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002743 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 /* more data */
2745 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002746 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002748 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002749}
2750
2751static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2752{
2753 struct gsm48_hdr *gh = msgb_l3(msg);
2754 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2755 struct tlv_parsed tp;
2756 struct gsm_mncc user;
2757
2758 memset(&user, 0, sizeof(struct gsm_mncc));
2759 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002760 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761 /* user-user */
2762 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2763 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002764 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002765 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2766 }
2767 /* more data */
2768 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2769 user.more = 1;
2770
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002771 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772}
2773
Philipp Maierfbf66102017-04-09 12:32:51 +02002774static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2775 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2776 uint32_t payload_msg_type)
2777{
2778 uint8_t data[sizeof(struct gsm_mncc)];
2779 struct gsm_mncc_rtp *rtp;
2780
2781 memset(&data, 0, sizeof(data));
2782 rtp = (struct gsm_mncc_rtp *) &data[0];
2783
2784 rtp->callref = callref;
2785 rtp->msg_type = cmd;
2786 rtp->ip = addr;
2787 rtp->port = port;
2788 rtp->payload_type = payload_type;
2789 rtp->payload_msg_type = payload_msg_type;
2790 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2791}
2792
2793static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2794{
2795 int msg_type;
2796
2797 /* FIXME This has to be set to some meaningful value.
2798 * Possible options are:
2799 * GSM_TCHF_FRAME, GSM_TCHF_FRAME_EFR,
2800 * GSM_TCHH_FRAME, GSM_TCH_FRAME_AMR
2801 * (0 if unknown) */
2802 msg_type = GSM_TCHF_FRAME;
2803
Philipp Maier621ba032017-11-07 17:19:25 +01002804 uint32_t addr = inet_addr(trans->conn->rtp.local_addr_cn);
2805 uint16_t port = trans->conn->rtp.local_port_cn;
2806
2807 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2808
2809 if (addr == INADDR_NONE) {
2810 LOGP(DMNCC, LOGL_ERROR,
2811 "(subscriber:%s) external MNCC is signalling invalid IP-Address\n",
2812 vlr_subscr_name(trans->vsub));
2813 return;
2814 }
2815 if (port == 0) {
2816 LOGP(DMNCC, LOGL_ERROR,
2817 "(subscriber:%s) external MNCC is signalling invalid Port\n",
2818 vlr_subscr_name(trans->vsub));
2819 return;
2820 }
Philipp Maierfbf66102017-04-09 12:32:51 +02002821
2822 /* FIXME: This has to be set to some meaningful value,
2823 * before the MSC-Split, this value was pulled from
2824 * lchan->abis_ip.rtp_payload */
2825 uint32_t payload_type = 0;
2826
2827 return mncc_recv_rtp(net, trans->callref, cmd,
2828 addr,
2829 port,
2830 payload_type,
2831 msg_type);
2832}
2833
2834static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2835{
2836 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2837}
2838
2839static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2840{
2841 struct gsm_trans *trans;
2842 int rc;
2843
2844 /* Find callref */
2845 trans = trans_find_by_callref(net, callref);
2846 if (!trans) {
2847 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2848 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2849 return -EIO;
2850 }
2851 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2852 if (!trans->conn) {
2853 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2854 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2855 return 0;
2856 }
2857
2858 trans->conn->mncc_rtp_bridge = 1;
2859
Philipp Maier621ba032017-11-07 17:19:25 +01002860 /* When we call msc_mgcp_call_assignment() we will trigger, depending
Philipp Maierfbf66102017-04-09 12:32:51 +02002861 * on the RAN type the call assignment on the A or Iu interface.
Philipp Maier621ba032017-11-07 17:19:25 +01002862 * msc_mgcp_call_assignment() also takes care about sending the CRCX
Philipp Maierfbf66102017-04-09 12:32:51 +02002863 * command to the MGCP-GW. The CRCX will return the port number,
2864 * where the PBX (e.g. Asterisk) will send its RTP stream to. We
2865 * have to return this port number back to the MNCC by sending
2866 * it back with the TCH_RTP_CREATE message. To make sure that
2867 * this message is sent AFTER the response to CRCX from the
Philipp Maier621ba032017-11-07 17:19:25 +01002868 * MGCP-GW has arrived, we need will instruct msc_mgcp_call_assignment()
Philipp Maierfbf66102017-04-09 12:32:51 +02002869 * to take care of this by setting trans->tch_rtp_create to true.
2870 * This will make sure that gsm48_tch_rtp_create() (below) is
2871 * called as soon as the local port number has become known. */
2872 trans->tch_rtp_create = true;
2873
2874 /* Assign call (if not done yet) */
2875 if (trans->assignment_done == false) {
Philipp Maier621ba032017-11-07 17:19:25 +01002876 rc = msc_mgcp_call_assignment(trans);
Philipp Maierfbf66102017-04-09 12:32:51 +02002877 trans->assignment_done = true;
2878 }
2879 else
2880 rc = 0;
2881
2882 return rc;
2883}
2884
2885/* Trigger TCH_RTP_CREATE acknowledgement */
2886int gsm48_tch_rtp_create(struct gsm_trans *trans)
2887{
2888 /* This function is called as soon as the port, on which the
2889 * mgcp-gw expects the incoming RTP stream from the remote
2890 * end (e.g. Asterisk) is known. */
2891
2892 struct gsm_subscriber_connection *conn = trans->conn;
2893 struct gsm_network *network = conn->network;
2894
2895 mncc_recv_rtp_sock(network, trans, MNCC_RTP_CREATE);
2896 return 0;
2897}
2898
2899static int tch_rtp_connect(struct gsm_network *net, void *arg)
2900{
2901 struct gsm_trans *trans;
2902 struct gsm_mncc_rtp *rtp = arg;
Philipp Maier621ba032017-11-07 17:19:25 +01002903 struct in_addr addr;
Philipp Maierfbf66102017-04-09 12:32:51 +02002904
2905 /* Find callref */
2906 trans = trans_find_by_callref(net, rtp->callref);
2907 if (!trans) {
2908 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
2909 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2910 return -EIO;
2911 }
2912 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
2913 if (!trans->conn) {
2914 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
2915 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
2916 return 0;
2917 }
2918
Philipp Maier621ba032017-11-07 17:19:25 +01002919 addr.s_addr = osmo_htonl(rtp->ip);
2920 return msc_mgcp_call_complete(trans, rtp->port, inet_ntoa(addr));
Philipp Maierfbf66102017-04-09 12:32:51 +02002921}
2922
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002924 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002925 int type;
2926 int (*rout) (struct gsm_trans *trans, void *arg);
2927} downstatelist[] = {
2928 /* mobile originating call establishment */
2929 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02002930 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc_and_assign},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002931 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2932 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2933 {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 */
2934 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2935 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2936 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2937 /* mobile terminating call establishment */
2938 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2939 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2940 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2941 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2942 /* signalling during call */
2943 {SBIT(GSM_CSTATE_ACTIVE),
2944 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2945 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2946 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2947 {ALL_STATES,
2948 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2949 {ALL_STATES,
2950 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2951 {ALL_STATES,
2952 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2953 {SBIT(GSM_CSTATE_ACTIVE),
2954 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2955 {SBIT(GSM_CSTATE_ACTIVE),
2956 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2957 {SBIT(GSM_CSTATE_ACTIVE),
2958 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2959 {SBIT(GSM_CSTATE_ACTIVE),
2960 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2961 {SBIT(GSM_CSTATE_ACTIVE),
2962 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2963 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2964 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2965 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2966 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2967 {SBIT(GSM_CSTATE_ACTIVE),
2968 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2969 /* clearing */
2970 {SBIT(GSM_CSTATE_INITIATED),
2971 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2972 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2973 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2974 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2975 MNCC_REL_REQ, gsm48_cc_tx_release},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976};
2977
2978#define DOWNSLLEN \
2979 (sizeof(downstatelist) / sizeof(struct downstate))
2980
2981
Harald Welte76556372010-12-22 23:57:45 +01002982int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002983{
Harald Welte1a6f7982009-08-09 18:52:33 +02002984 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002985 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002986 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 struct gsm_mncc *data = arg, rel;
2988
Harald Welte04dc88f2010-12-22 21:45:05 +01002989 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2990
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991 /* handle special messages */
2992 switch(msg_type) {
2993 case MNCC_BRIDGE:
Max3ffce192016-04-25 15:22:00 +02002994 rc = tch_bridge(net, arg);
2995 if (rc < 0)
2996 disconnect_bridge(net, arg, -rc);
2997 return rc;
Philipp Maierfbf66102017-04-09 12:32:51 +02002998 case MNCC_RTP_CREATE:
2999 return tch_rtp_create(net, data->callref);
3000 case MNCC_RTP_CONNECT:
3001 return tch_rtp_connect(net, arg);
3002 case MNCC_RTP_FREE:
3003 /* unused right now */
3004 return -EIO;
3005
Harald Welte4bfdfe72009-06-10 23:11:52 +08003006 case MNCC_FRAME_DROP:
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003008 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003009 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003010 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003011 case GSM_TCH_FRAME_AMR:
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003012 LOGP(DMNCC, LOGL_ERROR, "RTP streams must be handled externally; %s not supported.\n",
3013 get_mncc_name(msg_type));
3014 return -ENOTSUP;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015 }
3016
3017 memset(&rel, 0, sizeof(struct gsm_mncc));
3018 rel.callref = data->callref;
3019
3020 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003021 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022
3023 /* Callref unknown */
3024 if (!trans) {
Harald Welte2483f1b2016-06-19 18:06:02 +02003025 struct vlr_subscr *vsub;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003026
Harald Welte4a3464c2009-07-04 10:11:24 +02003027 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003028 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3029 "Received '%s' from MNCC with "
3030 "unknown callref %d\n", data->called.number,
3031 get_mncc_name(msg_type), data->callref);
3032 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003033 return mncc_release_ind(net, NULL, data->callref,
3034 GSM48_CAUSE_LOC_PRN_S_LU,
3035 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003037 if (!data->called.number[0] && !data->imsi[0]) {
3038 DEBUGP(DCC, "(bts - trx - ts - ti) "
3039 "Received '%s' from MNCC with "
3040 "no number or IMSI\n", get_mncc_name(msg_type));
3041 /* Invalid number */
3042 return mncc_release_ind(net, NULL, data->callref,
3043 GSM48_CAUSE_LOC_PRN_S_LU,
3044 GSM48_CC_CAUSE_INV_NR_FORMAT);
3045 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003046 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003047 if (data->called.number[0])
Harald Welte2483f1b2016-06-19 18:06:02 +02003048 vsub = vlr_subscr_find_by_msisdn(net->vlr,
3049 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003050 else
Harald Welte2483f1b2016-06-19 18:06:02 +02003051 vsub = vlr_subscr_find_by_imsi(net->vlr, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003052
3053 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003054 log_set_context(LOG_CTX_VLR_SUBSCR, vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003055
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 /* If subscriber is not found */
Harald Welte2483f1b2016-06-19 18:06:02 +02003057 if (!vsub) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3059 "Received '%s' from MNCC with "
3060 "unknown subscriber %s\n", data->called.number,
3061 get_mncc_name(msg_type), data->called.number);
3062 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003063 return mncc_release_ind(net, NULL, data->callref,
3064 GSM48_CAUSE_LOC_PRN_S_LU,
3065 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 }
3067 /* If subscriber is not "attached" */
Harald Welte2483f1b2016-06-19 18:06:02 +02003068 if (!vsub->lac) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3070 "Received '%s' from MNCC with "
3071 "detached subscriber %s\n", data->called.number,
Neels Hofmeyr7bbac162017-11-22 02:54:45 +01003072 get_mncc_name(msg_type), vlr_subscr_name(vsub));
Harald Welte2483f1b2016-06-19 18:06:02 +02003073 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003074 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003075 return mncc_release_ind(net, NULL, data->callref,
3076 GSM48_CAUSE_LOC_PRN_S_LU,
3077 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 }
3079 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003080 trans = trans_alloc(net, vsub, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003081 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003082 DEBUGP(DCC, "No memory for trans.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003083 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003085 mncc_release_ind(net, NULL, data->callref,
3086 GSM48_CAUSE_LOC_PRN_S_LU,
3087 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 return -ENOMEM;
3089 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003090
3091 /* Find conn */
Harald Welte2483f1b2016-06-19 18:06:02 +02003092 conn = connection_for_subscr(vsub);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003093
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003094 /* If subscriber has no conn */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003095 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 /* find transaction with this subscriber already paging */
3097 llist_for_each_entry(transt, &net->trans_list, entry) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003098 /* Transaction of our conn? */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003099 if (transt == trans ||
Harald Welte2483f1b2016-06-19 18:06:02 +02003100 transt->vsub != vsub)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003102 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003103 "Received '%s' from MNCC with "
3104 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003105 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 data->called.number,
Harald Welte2483f1b2016-06-19 18:06:02 +02003107 get_mncc_name(msg_type), vsub->lac);
3108 vlr_subscr_put(vsub);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003109 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003110 return 0;
3111 }
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003112 /* store setup information until paging succeeds */
Harald Weltedcaf5652009-07-23 18:56:43 +02003113 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003114
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003115 /* Request a channel */
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003116 trans->paging_request = subscr_request_conn(
Harald Welte2483f1b2016-06-19 18:06:02 +02003117 vsub,
Harald Welte2483f1b2016-06-19 18:06:02 +02003118 setup_trig_pag_evt,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003119 trans,
3120 "MNCC: establish call");
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003121 if (!trans->paging_request) {
3122 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
Harald Welte2483f1b2016-06-19 18:06:02 +02003123 vlr_subscr_put(vsub);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003124 trans_free(trans);
3125 return 0;
3126 }
Harald Welte2483f1b2016-06-19 18:06:02 +02003127 vlr_subscr_put(vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003128 return 0;
3129 }
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003130
3131 /* Assign conn */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003132 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003133 trans->dlci = 0x00; /* SAPI=0, not SACCH */
Harald Welte2483f1b2016-06-19 18:06:02 +02003134 vlr_subscr_put(vsub);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003135 } else {
3136 /* update the subscriber we deal with */
Harald Welte2483f1b2016-06-19 18:06:02 +02003137 log_set_context(LOG_CTX_VLR_SUBSCR, trans->vsub);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003139
3140 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003141 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142
3143 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003144 if (!conn) {
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003145 DEBUGP(DCC, "(sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003146 "Received '%s' from MNCC in paging state\n",
Harald Welte2483f1b2016-06-19 18:06:02 +02003147 vlr_subscr_msisdn_or_name(trans->vsub),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003149 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3150 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 if (msg_type == MNCC_REL_REQ)
3152 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3153 else
3154 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3155 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003156 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157 return rc;
Philipp Maiere4db08a2017-11-07 15:48:29 +01003158 } else {
3159 DEBUGP(DCC, "(ti %02x sub %s) "
3160 "Received '%s' from MNCC in state %d (%s)\n",
3161 trans->transaction_id,
3162 vlr_subscr_msisdn_or_name(trans->conn->vsub),
3163 get_mncc_name(msg_type), trans->cc.state,
3164 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003165 }
3166
Harald Welte4bfdfe72009-06-10 23:11:52 +08003167 /* Find function for current state and message */
3168 for (i = 0; i < DOWNSLLEN; i++)
3169 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003170 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003171 break;
3172 if (i == DOWNSLLEN) {
3173 DEBUGP(DCC, "Message unhandled at this state.\n");
3174 return 0;
3175 }
3176
3177 rc = downstatelist[i].rout(trans, arg);
3178
3179 return rc;
3180}
3181
3182
3183static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003184 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003185 int type;
3186 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3187} datastatelist[] = {
3188 /* mobile originating call establishment */
3189 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3190 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3191 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3192 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3193 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3194 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3195 /* mobile terminating call establishment */
3196 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3197 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3198 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3199 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003200 {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 +08003201 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3202 /* signalling during call */
3203 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3204 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3205 {SBIT(GSM_CSTATE_ACTIVE),
3206 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3207 {ALL_STATES,
3208 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3209 {ALL_STATES,
3210 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3211 {ALL_STATES,
3212 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3213 {SBIT(GSM_CSTATE_ACTIVE),
3214 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3215 {SBIT(GSM_CSTATE_ACTIVE),
3216 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3217 {SBIT(GSM_CSTATE_ACTIVE),
3218 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3219 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3220 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3221 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3222 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3223 {SBIT(GSM_CSTATE_ACTIVE),
3224 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3225 /* clearing */
3226 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3227 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3228 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3229 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3230 {ALL_STATES, /* 5.4.3.4 */
3231 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3232};
3233
3234#define DATASLLEN \
3235 (sizeof(datastatelist) / sizeof(struct datastate))
3236
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003237static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003238{
3239 struct gsm48_hdr *gh = msgb_l3(msg);
Neels Hofmeyr531734a2016-03-14 16:13:24 +01003240 uint8_t msg_type = gsm48_hdr_msg_type(gh);
Neels Hofmeyr961bd0b2016-03-14 16:13:25 +01003241 uint8_t transaction_id = gsm48_hdr_trans_id_flip_ti(gh);
Harald Weltedcaf5652009-07-23 18:56:43 +02003242 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003243 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003244
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 if (msg_type & 0x80) {
3246 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3247 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003248 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003249
Harald Welte2483f1b2016-06-19 18:06:02 +02003250 if (!conn->vsub) {
3251 LOGP(DCC, LOGL_ERROR, "Invalid conn: no subscriber\n");
Neels Hofmeyr35706dd2016-12-22 01:58:03 +01003252 return -EINVAL;
3253 }
3254
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003256 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003257
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003258#if BEFORE_MSCSPLIT
3259 /* Re-enable this log output once we can obtain this information via
3260 * A-interface, see OS#2391. */
Harald Welte6f5aee02009-07-23 21:21:14 +02003261 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003263 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte2483f1b2016-06-19 18:06:02 +02003264 transaction_id, vlr_subscr_msisdn_or_name(conn->vsub),
Harald Weltee95daf192010-03-25 12:13:02 +08003265 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3266 gsm48_cc_state_name(trans?(trans->cc.state):0));
Neels Hofmeyre2f24d52017-05-08 15:12:20 +02003267#endif
Harald Welte4bfdfe72009-06-10 23:11:52 +08003268
3269 /* Create transaction */
3270 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003271 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003272 "creating new trans.\n", transaction_id);
3273 /* Create transaction */
Harald Welte2483f1b2016-06-19 18:06:02 +02003274 trans = trans_alloc(conn->network, conn->vsub,
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003275 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003276 transaction_id, new_callref++);
3277 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003278 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003279 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003280 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003281 GSM48_MT_CC_RELEASE_COMPL);
3282 return -ENOMEM;
3283 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003284 /* Assign transaction */
Neels Hofmeyr6166f292017-11-22 14:33:12 +01003285 trans->conn = msc_subscr_conn_get(conn, MSC_CONN_USE_TRANS_CC);
Harald Welte0e2fa5d2018-04-09 16:35:01 +02003286 trans->dlci = OMSC_LINKID_CB(msg); /* DLCI as received from BSC */
Harald Welte2483f1b2016-06-19 18:06:02 +02003287 cm_service_request_concludes(conn, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003288 }
3289
3290 /* find function for current state and message */
3291 for (i = 0; i < DATASLLEN; i++)
3292 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003293 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003294 break;
3295 if (i == DATASLLEN) {
3296 DEBUGP(DCC, "Message unhandled at this state.\n");
3297 return 0;
3298 }
3299
Harald Welte2483f1b2016-06-19 18:06:02 +02003300 assert(trans->vsub);
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003301
Harald Welte4bfdfe72009-06-10 23:11:52 +08003302 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003303
Harald Welte2483f1b2016-06-19 18:06:02 +02003304 msc_subscr_conn_communicating(conn);
Harald Welte4bc90a12008-12-27 16:32:52 +00003305 return rc;
3306}
3307
Harald Welte2483f1b2016-06-19 18:06:02 +02003308static bool msg_is_initially_permitted(const struct gsm48_hdr *hdr)
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003309{
Harald Welte2483f1b2016-06-19 18:06:02 +02003310 uint8_t pdisc = gsm48_hdr_pdisc(hdr);
3311 uint8_t msg_type = gsm48_hdr_msg_type(hdr);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003312
Harald Welte2483f1b2016-06-19 18:06:02 +02003313 switch (pdisc) {
3314 case GSM48_PDISC_MM:
3315 switch (msg_type) {
3316 case GSM48_MT_MM_LOC_UPD_REQUEST:
3317 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte49f8fcb2018-01-24 21:40:19 +01003318 case GSM48_MT_MM_CM_REEST_REQ:
Harald Welte2483f1b2016-06-19 18:06:02 +02003319 case GSM48_MT_MM_AUTH_RESP:
3320 case GSM48_MT_MM_AUTH_FAIL:
3321 case GSM48_MT_MM_ID_RESP:
3322 case GSM48_MT_MM_TMSI_REALL_COMPL:
3323 case GSM48_MT_MM_IMSI_DETACH_IND:
3324 return true;
3325 default:
3326 break;
3327 }
3328 break;
3329 case GSM48_PDISC_RR:
3330 switch (msg_type) {
3331 case GSM48_MT_RR_CIPH_M_COMPL:
3332 case GSM48_MT_RR_PAG_RESP:
3333 return true;
3334 default:
3335 break;
3336 }
3337 break;
3338 default:
3339 break;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003340 }
3341
Harald Welte2483f1b2016-06-19 18:06:02 +02003342 return false;
Neels Hofmeyr42eb0142016-05-20 17:15:44 +02003343}
3344
Harald Welte2483f1b2016-06-19 18:06:02 +02003345void cm_service_request_concludes(struct gsm_subscriber_connection *conn,
3346 struct msgb *msg)
3347{
3348
3349 /* If a CM Service Request was received before, this is the request the
3350 * conn was opened for. No need to wait for further messages. */
3351
3352 if (!conn->received_cm_service_request)
3353 return;
3354
3355 if (log_check_level(DMM, LOGL_DEBUG)) {
3356 struct gsm48_hdr *gh = msgb_l3(msg);
3357 uint8_t pdisc = gsm48_hdr_pdisc(gh);
3358 uint8_t msg_type = gsm48_hdr_msg_type(gh);
3359
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003360 DEBUGP(DMM, "%s: rx msg %s:"
Harald Welte2483f1b2016-06-19 18:06:02 +02003361 " received_cm_service_request changes to false\n",
3362 vlr_subscr_name(conn->vsub),
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003363 gsm48_pdisc_msgtype_name(pdisc, msg_type));
Harald Welte2483f1b2016-06-19 18:06:02 +02003364 }
3365 conn->received_cm_service_request = false;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003366 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
Harald Welte2483f1b2016-06-19 18:06:02 +02003367}
3368
Harald Weltec2007852018-02-03 21:08:26 +01003369/* TS 24.007 11.2.3.2.3 Message Type Octet / Duplicate Detection */
3370int gsm0407_pdisc_ctr_bin(uint8_t pdisc)
3371{
3372 switch (pdisc) {
3373 case GSM48_PDISC_MM:
3374 case GSM48_PDISC_CC:
3375 case GSM48_PDISC_NC_SS:
3376 return 0;
3377 case GSM48_PDISC_GROUP_CC:
3378 return 1;
3379 case GSM48_PDISC_BCAST_CC:
3380 return 2;
3381 case GSM48_PDISC_LOC:
3382 return 3;
3383 default:
3384 return -1;
3385 }
3386}
3387
3388/* extract the N(SD) and return the modulo value for a R98 message */
3389static uint8_t gsm0407_determine_nsd_ret_modulo_r99(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3390{
3391 switch (pdisc) {
3392 case GSM48_PDISC_MM:
3393 case GSM48_PDISC_CC:
3394 case GSM48_PDISC_NC_SS:
3395 *n_sd = (msg_type >> 6) & 0x3;
3396 return 4;
3397 case GSM48_PDISC_GROUP_CC:
3398 case GSM48_PDISC_BCAST_CC:
3399 case GSM48_PDISC_LOC:
3400 *n_sd = (msg_type >> 6) & 0x1;
3401 return 2;
3402 default:
3403 /* no sequence number, we cannot detect dups */
3404 return 0;
3405 }
3406}
3407
3408/* extract the N(SD) and return the modulo value for a R99 message */
3409static uint8_t gsm0407_determine_nsd_ret_modulo_r98(uint8_t pdisc, uint8_t msg_type, uint8_t *n_sd)
3410{
3411 switch (pdisc) {
3412 case GSM48_PDISC_MM:
3413 case GSM48_PDISC_CC:
3414 case GSM48_PDISC_NC_SS:
3415 case GSM48_PDISC_GROUP_CC:
3416 case GSM48_PDISC_BCAST_CC:
3417 case GSM48_PDISC_LOC:
3418 *n_sd = (msg_type >> 6) & 0x1;
3419 return 2;
3420 default:
3421 /* no sequence number, we cannot detect dups */
3422 return 0;
3423 }
3424}
3425
3426/* TS 24.007 11.2.3.2 Message Type Octet / Duplicate Detection */
3427static bool gsm0407_is_duplicate(struct gsm_subscriber_connection *conn, struct msgb *msg)
3428{
3429 struct gsm48_hdr *gh;
3430 uint8_t pdisc;
3431 uint8_t n_sd, modulo, bin;
3432
3433 gh = msgb_l3(msg);
3434 pdisc = gsm48_hdr_pdisc(gh);
3435
3436 if (conn->classmark.classmark1_set && conn->classmark.classmark1.rev_lev < 2) {
3437 modulo = gsm0407_determine_nsd_ret_modulo_r98(pdisc, gh->msg_type, &n_sd);
3438 } else { /* R99 */
3439 modulo = gsm0407_determine_nsd_ret_modulo_r99(pdisc, gh->msg_type, &n_sd);
3440 }
3441 if (modulo == 0)
3442 return false;
3443 bin = gsm0407_pdisc_ctr_bin(pdisc);
3444 if (bin < 0)
3445 return false;
3446
3447 OSMO_ASSERT(bin < ARRAY_SIZE(conn->n_sd_next));
3448 if (n_sd != conn->n_sd_next[bin]) {
3449 /* not what we expected: duplicate */
3450 return true;
3451 } else {
3452 /* as expected: no dup; update expected counter for next message */
3453 conn->n_sd_next[bin] = (n_sd + 1) % modulo;
3454 return false;
3455 }
3456}
Harald Welte2483f1b2016-06-19 18:06:02 +02003457
Neels Hofmeyr378a4922016-05-09 21:07:43 +02003458/* 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 +08003459int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003460{
Philipp Maier91f10c72017-04-20 18:40:37 +02003461 struct gsm48_hdr *gh;
3462 uint8_t pdisc;
Harald Welte8470bf22008-12-25 23:28:35 +00003463 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003464
Neels Hofmeyr0906a392017-09-19 14:36:06 +02003465 OSMO_ASSERT(msg->l3h);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003466 OSMO_ASSERT(conn);
3467 OSMO_ASSERT(msg);
3468
Philipp Maier91f10c72017-04-20 18:40:37 +02003469 gh = msgb_l3(msg);
3470 pdisc = gsm48_hdr_pdisc(gh);
3471
Harald Weltec2007852018-02-03 21:08:26 +01003472 if (gsm0407_is_duplicate(conn, msg)) {
3473 LOGP(DRLL, LOGL_NOTICE, "%s: Discarding duplicate L3 message\n",
3474 (conn && conn->vsub) ? vlr_subscr_name(conn->vsub) : "UNKNOWN");
3475 return 0;
3476 }
3477
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003478 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message %s (0x%x:0x%x)\n",
3479 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)),
3480 pdisc, gsm48_hdr_msg_type(gh));
Harald Welte2483f1b2016-06-19 18:06:02 +02003481
3482 if (!msc_subscr_conn_is_accepted(conn)
3483 && !msg_is_initially_permitted(gh)) {
3484 LOGP(DRLL, LOGL_ERROR,
Neels Hofmeyr78ada642017-03-10 02:15:20 +01003485 "subscr %s: Message not permitted for initial conn: %s\n",
3486 vlr_subscr_name(conn->vsub),
3487 gsm48_pdisc_msgtype_name(pdisc, gsm48_hdr_msg_type(gh)));
Harald Welte2483f1b2016-06-19 18:06:02 +02003488 return -EACCES;
3489 }
3490
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003491 if (conn->vsub && conn->vsub->cs.attached_via_ran != conn->via_ran) {
3492 LOGP(DMM, LOGL_ERROR,
3493 "%s: Illegal situation: RAN type mismatch:"
3494 " attached via %s, received message via %s\n",
3495 vlr_subscr_name(conn->vsub),
3496 ran_type_name(conn->vsub->cs.attached_via_ran),
3497 ran_type_name(conn->via_ran));
3498 return -EACCES;
3499 }
3500
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003501#if 0
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003502 if (silent_call_reroute(conn, msg))
3503 return silent_call_rx(conn, msg);
Philipp Maiere0d5caa2017-02-27 16:56:59 +01003504#endif
Alexander Couzensfbd96f52016-08-29 18:40:02 +02003505
Harald Welte52b1f982008-12-23 20:25:15 +00003506 switch (pdisc) {
3507 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003508 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003509 break;
3510 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003511 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003512 break;
3513 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003514 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003515 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003516 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003517 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003518 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003519 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003520 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003521 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3522 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003523 rc = -ENOTSUP;
Harald Welte52b1f982008-12-23 20:25:15 +00003524 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003525 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003526 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003527 break;
Harald Welteeac38c32017-05-29 18:02:53 +02003528 case GSM48_PDISC_TEST:
3529 rc = gsm0414_rcv_test(conn, msg);
3530 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003531 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003532 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3533 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Neels Hofmeyrffaed9e2016-05-09 21:38:51 +02003534 rc = -EINVAL;
Harald Welte52b1f982008-12-23 20:25:15 +00003535 break;
3536 }
3537
3538 return rc;
3539}
Harald Welte8470bf22008-12-25 23:28:35 +00003540
Harald Welte2483f1b2016-06-19 18:06:02 +02003541/***********************************************************************
3542 * VLR integration
3543 ***********************************************************************/
3544
3545/* VLR asks us to send an authentication request */
3546static int msc_vlr_tx_auth_req(void *msc_conn_ref, struct gsm_auth_tuple *at,
3547 bool send_autn)
3548{
3549 struct gsm_subscriber_connection *conn = msc_conn_ref;
3550 return gsm48_tx_mm_auth_req(conn, at->vec.rand,
3551 send_autn? at->vec.autn : NULL,
3552 at->key_seq);
3553}
3554
3555/* VLR asks us to send an authentication reject */
3556static int msc_vlr_tx_auth_rej(void *msc_conn_ref)
3557{
3558 struct gsm_subscriber_connection *conn = msc_conn_ref;
3559 return gsm48_tx_mm_auth_rej(conn);
3560}
3561
3562/* VLR asks us to transmit an Identity Request of given type */
3563static int msc_vlr_tx_id_req(void *msc_conn_ref, uint8_t mi_type)
3564{
3565 struct gsm_subscriber_connection *conn = msc_conn_ref;
3566 return mm_tx_identity_req(conn, mi_type);
3567}
3568
3569/* VLR asks us to transmit a Location Update Accept */
3570static int msc_vlr_tx_lu_acc(void *msc_conn_ref, uint32_t send_tmsi)
3571{
3572 struct gsm_subscriber_connection *conn = msc_conn_ref;
3573 return gsm0408_loc_upd_acc(conn, send_tmsi);
3574}
3575
3576/* VLR asks us to transmit a Location Update Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02003577static int msc_vlr_tx_lu_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02003578{
3579 struct gsm_subscriber_connection *conn = msc_conn_ref;
3580 return gsm0408_loc_upd_rej(conn, cause);
3581}
3582
3583/* VLR asks us to transmit a CM Service Accept */
3584static int msc_vlr_tx_cm_serv_acc(void *msc_conn_ref)
3585{
3586 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003587 return msc_gsm48_tx_mm_serv_ack(conn);
3588}
3589
3590static int msc_vlr_tx_common_id(void *msc_conn_ref)
3591{
3592 struct gsm_subscriber_connection *conn = msc_conn_ref;
3593 return msc_tx_common_id(conn);
Harald Welte2483f1b2016-06-19 18:06:02 +02003594}
3595
Stefan Sperling3a741282018-03-13 21:11:49 +01003596/* VLR asks us to transmit MM info. */
3597static int msc_vlr_tx_mm_info(void *msc_conn_ref)
3598{
3599 struct gsm_subscriber_connection *conn = msc_conn_ref;
3600 if (!conn->network->send_mm_info)
3601 return 0;
3602 return gsm48_tx_mm_info(conn);
3603}
3604
Harald Welte2483f1b2016-06-19 18:06:02 +02003605/* VLR asks us to transmit a CM Service Reject */
Neels Hofmeyr15809592018-04-06 02:57:51 +02003606static int msc_vlr_tx_cm_serv_rej(void *msc_conn_ref, enum gsm48_reject_value cause)
Harald Welte2483f1b2016-06-19 18:06:02 +02003607{
Harald Welte2483f1b2016-06-19 18:06:02 +02003608 struct gsm_subscriber_connection *conn = msc_conn_ref;
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003609 int rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02003610
Neels Hofmeyre3d3dc62018-03-31 00:02:14 +02003611 rc = msc_gsm48_tx_mm_serv_rej(conn, cause);
3612
3613 if (conn->received_cm_service_request) {
3614 conn->received_cm_service_request = false;
3615 msc_subscr_conn_put(conn, MSC_CONN_USE_CM_SERVICE);
3616 }
3617
3618 return rc;
Harald Welte2483f1b2016-06-19 18:06:02 +02003619}
3620
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003621/* For msc_vlr_set_ciph_mode() */
3622osmo_static_assert(sizeof(((struct gsm0808_encrypt_info*)0)->key) >= sizeof(((struct osmo_auth_vector*)0)->kc),
3623 gsm0808_encrypt_info_key_fits_osmo_auth_vec_kc);
3624
Neels Hofmeyrda21a522018-03-02 01:50:09 +01003625/* VLR asks us to start using ciphering.
3626 * (Keep non-static to allow regression testing on this function.) */
3627int msc_vlr_set_ciph_mode(void *msc_conn_ref,
3628 bool umts_aka,
3629 bool retrieve_imeisv)
Harald Welte2483f1b2016-06-19 18:06:02 +02003630{
3631 struct gsm_subscriber_connection *conn = msc_conn_ref;
3632 struct vlr_subscr *vsub;
3633 struct gsm_auth_tuple *tuple;
3634
3635 if (!conn || !conn->vsub) {
3636 LOGP(DMM, LOGL_ERROR, "Cannot send Ciphering Mode Command to"
3637 " NULL conn/subscriber");
3638 return -EINVAL;
3639 }
3640
3641 vsub = conn->vsub;
3642 tuple = vsub->last_tuple;
3643
3644 if (!tuple) {
3645 LOGP(DMM, LOGL_ERROR, "subscr %s: Cannot send Ciphering Mode"
3646 " Command: no auth tuple available\n",
3647 vlr_subscr_name(vsub));
3648 return -EINVAL;
3649 }
3650
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003651 switch (conn->via_ran) {
3652 case RAN_GERAN_A:
3653 DEBUGP(DMM, "-> CIPHER MODE COMMAND %s\n",
3654 vlr_subscr_name(conn->vsub));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003655 {
Harald Welte7b222aa2017-12-23 19:30:32 +01003656 struct gsm_network *net = conn->network;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003657 struct gsm0808_encrypt_info ei;
Harald Welte7b222aa2017-12-23 19:30:32 +01003658 int i, j = 0;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003659
Harald Welte7b222aa2017-12-23 19:30:32 +01003660 for (i = 0; i < 8; i++) {
Harald Welte71330722017-12-23 19:59:02 +01003661 if (net->a5_encryption_mask & (1 << i) &&
3662 classmark_supports_a5(&conn->classmark, i))
Harald Welte7b222aa2017-12-23 19:30:32 +01003663 ei.perm_algo[j++] = vlr_ciph_to_gsm0808_alg_id(i);
3664 }
3665 ei.perm_algo_len = j;
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003666
Harald Welte71330722017-12-23 19:59:02 +01003667 if (ei.perm_algo_len == 0) {
3668 LOGP(DMM, LOGL_ERROR, "%s: cannot start ciphering, no intersection "
3669 "between MSC-configured and MS-supported A5 algorithms\n",
3670 vlr_subscr_name(conn->vsub));
3671 return -ENOTSUP;
3672 }
3673
Neels Hofmeyr52821712017-12-18 01:23:42 +01003674 /* In case of UMTS AKA, the Kc for ciphering must be derived from the 3G auth
3675 * tokens. tuple->vec.kc was calculated from the GSM algorithm and is not
3676 * necessarily a match for the UMTS AKA tokens. */
3677 if (umts_aka)
3678 osmo_auth_c3(ei.key, tuple->vec.ck, tuple->vec.ik);
3679 else
3680 memcpy(ei.key, tuple->vec.kc, sizeof(tuple->vec.kc));
Neels Hofmeyr703638e2017-12-14 05:30:16 +01003681 ei.key_len = sizeof(tuple->vec.kc);
3682
3683 return a_iface_tx_cipher_mode(conn, &ei, retrieve_imeisv);
3684 }
3685
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003686 case RAN_UTRAN_IU:
3687#ifdef BUILD_IU
3688 DEBUGP(DMM, "-> SECURITY MODE CONTROL %s\n",
3689 vlr_subscr_name(conn->vsub));
Neels Hofmeyr00e82d62017-07-05 15:19:52 +02003690 return ranap_iu_tx_sec_mode_cmd(conn->iu.ue_ctx, &tuple->vec, 0, 1);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003691#else
3692 LOGP(DMM, LOGL_ERROR, "Cannot send Security Mode Control over RAN_UTRAN_IU,"
3693 " built without Iu support\n");
3694 return -ENOTSUP;
3695#endif
3696
3697 default:
3698 break;
3699 }
3700 LOGP(DMM, LOGL_ERROR,
3701 "%s: cannot start ciphering, unknown RAN type %d\n",
3702 vlr_subscr_name(conn->vsub), conn->via_ran);
3703 return -ENOTSUP;
3704}
3705
3706void msc_rx_sec_mode_compl(struct gsm_subscriber_connection *conn)
3707{
3708 struct vlr_ciph_result vlr_res = {};
3709
3710 if (!conn || !conn->vsub) {
3711 LOGP(DMM, LOGL_ERROR,
3712 "Rx Security Mode Complete for invalid conn\n");
3713 return;
3714 }
3715
3716 DEBUGP(DMM, "<- SECURITY MODE COMPLETE %s\n",
3717 vlr_subscr_name(conn->vsub));
3718
3719 vlr_res.cause = VLR_CIPH_COMPL;
3720 vlr_subscr_rx_ciph_res(conn->vsub, &vlr_res);
Harald Welte2483f1b2016-06-19 18:06:02 +02003721}
3722
3723/* VLR informs us that the subscriber data has somehow been modified */
3724static void msc_vlr_subscr_update(struct vlr_subscr *subscr)
3725{
Maxa263bb22017-12-27 13:23:44 +01003726 LOGVSUBP(LOGL_NOTICE, subscr, "VLR: update for IMSI=%s (MSISDN=%s, used=%d)\n",
3727 subscr->imsi, subscr->msisdn, subscr->use_count);
Harald Welte2483f1b2016-06-19 18:06:02 +02003728}
3729
3730/* VLR informs us that the subscriber has been associated with a conn */
3731static void msc_vlr_subscr_assoc(void *msc_conn_ref,
3732 struct vlr_subscr *vsub)
3733{
3734 struct gsm_subscriber_connection *conn = msc_conn_ref;
Max48d4ec02018-02-12 16:51:03 +01003735 OSMO_ASSERT(vsub);
Harald Welte2483f1b2016-06-19 18:06:02 +02003736 OSMO_ASSERT(!conn->vsub);
3737 conn->vsub = vlr_subscr_get(vsub);
Max48d4ec02018-02-12 16:51:03 +01003738 OSMO_ASSERT(conn->vsub);
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003739 conn->vsub->cs.attached_via_ran = conn->via_ran;
Harald Welte2483f1b2016-06-19 18:06:02 +02003740}
3741
3742/* operations that we need to implement for libvlr */
3743static const struct vlr_ops msc_vlr_ops = {
3744 .tx_auth_req = msc_vlr_tx_auth_req,
3745 .tx_auth_rej = msc_vlr_tx_auth_rej,
3746 .tx_id_req = msc_vlr_tx_id_req,
3747 .tx_lu_acc = msc_vlr_tx_lu_acc,
3748 .tx_lu_rej = msc_vlr_tx_lu_rej,
3749 .tx_cm_serv_acc = msc_vlr_tx_cm_serv_acc,
3750 .tx_cm_serv_rej = msc_vlr_tx_cm_serv_rej,
3751 .set_ciph_mode = msc_vlr_set_ciph_mode,
Neels Hofmeyr84da6b12016-05-20 21:59:55 +02003752 .tx_common_id = msc_vlr_tx_common_id,
Stefan Sperling3a741282018-03-13 21:11:49 +01003753 .tx_mm_info = msc_vlr_tx_mm_info,
Harald Welte2483f1b2016-06-19 18:06:02 +02003754 .subscr_update = msc_vlr_subscr_update,
3755 .subscr_assoc = msc_vlr_subscr_assoc,
3756};
3757
3758/* Allocate net->vlr so that the VTY may configure the VLR's data structures */
3759int msc_vlr_alloc(struct gsm_network *net)
3760{
3761 net->vlr = vlr_alloc(net, &msc_vlr_ops);
3762 if (!net->vlr)
3763 return -ENOMEM;
3764 net->vlr->user_ctx = net;
3765 return 0;
3766}
3767
3768/* Launch the VLR, i.e. its GSUP connection */
3769int msc_vlr_start(struct gsm_network *net)
3770{
3771 OSMO_ASSERT(net->vlr);
3772 return vlr_start("MSC", net->vlr, net->gsup_server_addr_str,
3773 net->gsup_server_port);
3774}
Neels Hofmeyr79e580b2018-03-22 16:48:22 +01003775
3776struct msgb *gsm48_create_mm_serv_rej(enum gsm48_reject_value value)
3777{
3778 struct msgb *msg;
3779 struct gsm48_hdr *gh;
3780
3781 msg = gsm48_msgb_alloc_name("GSM 04.08 SERV REJ");
3782 if (!msg)
3783 return NULL;
3784
3785 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3786 gh->proto_discr = GSM48_PDISC_MM;
3787 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
3788 gh->data[0] = value;
3789
3790 return msg;
3791}
3792
3793struct msgb *gsm48_create_loc_upd_rej(uint8_t cause)
3794{
3795 struct gsm48_hdr *gh;
3796 struct msgb *msg;
3797
3798 msg = gsm48_msgb_alloc_name("GSM 04.08 LOC UPD REJ");
3799 if (!msg)
3800 return NULL;
3801
3802 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
3803 gh->proto_discr = GSM48_PDISC_MM;
3804 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
3805 gh->data[0] = cause;
3806 return msg;
3807}
3808
3809int gsm48_extract_mi(uint8_t *classmark2_lv, int length, char *mi_string, uint8_t *mi_type)
3810{
3811 /* Check the size for the classmark */
3812 if (length < 1 + *classmark2_lv)
3813 return -1;
3814
3815 uint8_t *mi_lv = classmark2_lv + *classmark2_lv + 1;
3816 if (length < 2 + *classmark2_lv + mi_lv[0])
3817 return -2;
3818
3819 *mi_type = mi_lv[1] & GSM_MI_TYPE_MASK;
3820 return gsm48_mi_to_string(mi_string, GSM48_MI_SIZE, mi_lv+1, *mi_lv);
3821}
3822
3823int gsm48_paging_extract_mi(struct gsm48_pag_resp *resp, int length,
3824 char *mi_string, uint8_t *mi_type)
3825{
3826 static const uint32_t classmark_offset =
3827 offsetof(struct gsm48_pag_resp, classmark2);
3828 uint8_t *classmark2_lv = (uint8_t *) &resp->classmark2;
3829 return gsm48_extract_mi(classmark2_lv, length - classmark_offset,
3830 mi_string, mi_type);
3831}