blob: 776a11d0dede9a0c6560ce0eab8b283af13a3154 [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 Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 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>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte9c3dc902012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut30a15382009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080057
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010058#include <osmocom/gsm/gsm48.h>
59#include <osmocom/gsm/gsm0480.h>
60#include <osmocom/gsm/gsm_utils.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000064
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020065#include <assert.h>
66
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020067void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010068void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020069
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +020070static int tch_rtp_signal(struct gsm_lchan *lchan, int signal);
71
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +020072static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080073static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020074 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010075static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080076static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000077
Harald Welte52b1f982008-12-23 20:25:15 +000078struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020079 uint16_t mcc;
80 uint16_t mnc;
81 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000082};
83
Andreas Eversberga83d5112013-12-07 18:32:28 +010084static int apply_codec_restrictions(struct gsm_bts *bts,
85 struct gsm_mncc_bearer_cap *bcap)
86{
87 int i, j;
88
89 /* remove unsupported speech versions from list */
90 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
91 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
92 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
93 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
94 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
95 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
96 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
99 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
100 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
101 }
102 bcap->speech_ver[j] = -1;
103
104 return 0;
105}
106
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200107static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800108
Harald Welte31c00f72011-03-03 23:29:05 +0100109void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
110{
111 net->mncc_recv(net, msg);
112}
113
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800114static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
115 struct gsm_trans *trans)
116{
117 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
118
119 /* if we get passed a transaction reference, do some common
120 * work that the caller no longer has to do */
121 if (trans) {
122 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
123 msg->lchan = trans->conn->lchan;
124 }
125
126
127 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200128 struct e1inp_sign_link *sign_link =
129 msg->lchan->ts->trx->rsl_link;
130
131 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800132 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
133 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200134 "Sending '%s' to MS.\n",
135 sign_link->trx->bts->nr,
136 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800137 gh->proto_discr & 0xf0,
138 gsm48_cc_msg_name(gh->msg_type));
139 else
140 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200141 "Sending 0x%02x to MS.\n",
142 sign_link->trx->bts->nr,
143 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800144 gh->proto_discr, gh->msg_type);
145 }
146
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100147 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800148}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100149
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800150int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
151{
152 struct gsm48_hdr *gh;
153 struct msgb *ss_notify;
154
155 ss_notify = gsm0480_create_notifySS(message);
156 if (!ss_notify)
157 return -1;
158
159 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
160 uint8_t *data = msgb_push(ss_notify, 1);
161 data[0] = ss_notify->len - 1;
162 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
163 gh->msg_type = GSM48_MT_CC_FACILITY;
164 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
165}
166
Harald Weltecf149ee2012-01-23 16:40:24 +0100167void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100168{
169 if (!conn->sec_operation)
170 return;
171
172 talloc_free(conn->sec_operation);
173 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800174 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100175}
176
Harald Weltecf149ee2012-01-23 16:40:24 +0100177void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100178{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100179 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
180 struct gsm_security_operation);
181}
182
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100183int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
184 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100185{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800186 struct gsm_network *net = conn->bts->network;
187 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100188 struct gsm_security_operation *op;
189 struct gsm_auth_tuple atuple;
190 int status = -1, rc;
191
192 /* Check if we _can_ enable encryption. Cases where we can't:
193 * - Encryption disabled in config
194 * - Channel already secured (nothing to do)
195 * - Subscriber equipment doesn't support configured encryption
196 */
197 if (!net->a5_encryption) {
198 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800199 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200201 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100202 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
203 net->a5_encryption)) {
204 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
205 status = GSM_SECURITY_NOAVAIL;
206 }
207
208 /* If not done yet, try to get info for this user */
209 if (status < 0) {
210 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
211 if (rc <= 0)
212 status = GSM_SECURITY_NOAVAIL;
213 }
214
215 /* Are we done yet ? */
216 if (status >= 0)
217 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800218 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100219 0;
220
221 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800222 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100223 return -EBUSY;
224
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800225 allocate_security_operation(conn);
226 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100227 op->cb = cb;
228 op->cb_data = cb_data;
229 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
230
231 /* FIXME: Should start a timer for completion ... */
232
233 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100234 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100235 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800236 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100237 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100238 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100239 return gsm0808_cipher_mode(conn, net->a5_encryption,
240 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100241 }
242
243 return -EINVAL; /* not reached */
244}
245
Holger Freyther73487a22008-12-31 18:53:57 +0000246static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
247 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000248{
249 if (!subscriber)
250 return 0;
251
Holger Freyther73487a22008-12-31 18:53:57 +0000252 /*
253 * Do not send accept yet as more information should arrive. Some
254 * phones will not send us the information and we will have to check
255 * what we want to do with that.
256 */
257 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
258 return 0;
259
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100260 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200261 case GSM_AUTH_POLICY_CLOSED:
262 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200263 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200264 if (subscriber->authorized)
265 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200266 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000268 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200269 default:
270 return 0;
271 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000272}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000273
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100274static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000275{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100276 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000277 return;
278
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800279 /* No need to keep the connection up */
280 release_anchor(conn);
281
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200282 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100283 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100284 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100285 if (release)
286 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000287}
288
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100289static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000290{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800291 if (conn->loc_operation)
292 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100293 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000294
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100295 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200296 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000297}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000298
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200299static int finish_lu(struct gsm_subscriber_connection *conn)
300{
301 int rc = 0;
302 int avoid_tmsi = conn->bts->network->avoid_tmsi;
303
304 /* We're all good */
305 if (avoid_tmsi) {
306 conn->subscr->tmsi = GSM_RESERVED_TMSI;
307 db_sync_subscriber(conn->subscr);
308 } else {
309 db_subscriber_alloc_tmsi(conn->subscr);
310 }
311
312 rc = gsm0408_loc_upd_acc(conn);
313 if (conn->bts->network->send_mm_info) {
314 /* send MM INFO with network name */
315 rc = gsm48_tx_mm_info(conn);
316 }
317
318 /* call subscr_update after putting the loc_upd_acc
319 * in the transmit queue, since S_SUBSCR_ATTACHED might
320 * trigger further action like SMS delivery */
321 subscr_update(conn->subscr, conn->bts,
322 GSM_SUBSCRIBER_UPDATE_ATTACHED);
323
324 /*
325 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
326 * MS needs to respond with a TMSI REALLOCATION COMPLETE
327 * (even if the TMSI is the same).
328 */
329 if (avoid_tmsi)
330 release_loc_updating_req(conn, 1);
331
332 return rc;
333}
334
Sylvain Munaut267fba02009-12-24 00:28:01 +0100335static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
336 struct msgb *msg, void *data, void *param)
337{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800338 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100339 int rc = 0;
340
341 switch (event) {
342 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100343 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100344 break;
345
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200346 case GSM_SECURITY_ALREADY:
347 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
348 "UPDATING after CM SERVICE REQUEST\n");
349 /* fall through */
350
Sylvain Munaut267fba02009-12-24 00:28:01 +0100351 case GSM_SECURITY_NOAVAIL:
352 case GSM_SECURITY_SUCCEEDED:
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200353 rc = finish_lu(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100354 break;
355
356 default:
357 rc = -EINVAL;
358 };
359
360 return rc;
361}
362
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100363static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000364{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100365 if (!conn->loc_operation)
366 return 0;
367
Sylvain Munaut267fba02009-12-24 00:28:01 +0100368 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800369 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100370 conn->loc_operation->key_seq,
371 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000372 return 0;
373}
374
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800375void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000376{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800377 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800378
379 /* avoid someone issuing a clear */
380 conn->in_release = 1;
381
Holger Freyther7c19f742009-06-06 13:54:35 +0000382 /*
383 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800384 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000385 */
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100386 release_loc_updating_req(conn, 1);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100387
388 /* We might need to cancel the paging response or such. */
389 if (conn->sec_operation && conn->sec_operation->cb) {
390 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
391 NULL, conn, conn->sec_operation->cb_data);
392 }
393
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800394 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800395 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000396
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100397 /*
398 * Free all transactions that are associated with the released
399 * connection. The transaction code will inform the CC or SMS
400 * facilities that will send the release indications. As part of
401 * the CC REL_IND the remote leg might be released and this will
402 * trigger the call to trans_free. This is something the llist
403 * macro can not handle and we will need to re-iterate the list.
404 *
405 * TODO: Move the trans_list into the subscriber connection and
406 * create a pending list for MT transactions. These exist before
407 * we have a subscriber connection.
408 */
409restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800410 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100411 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200412 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100413 goto restart;
414 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800415 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000416}
417
Harald Welte371efe52010-12-22 23:17:50 +0100418void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
419{
420 struct gsm_trans *trans, *temp;
421
422 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
423
424 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100425 if (trans->protocol == protocol) {
426 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100427 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100428 }
Harald Welte371efe52010-12-22 23:17:50 +0100429 }
430}
431
Holger Freyther429e7762008-12-30 13:28:30 +0000432/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200433int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000434{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800435 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800436 struct msgb *msg;
437
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200438 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800439
440 msg = gsm48_create_loc_upd_rej(cause);
441 if (!msg) {
442 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
443 return -1;
444 }
Harald Welte52b1f982008-12-23 20:25:15 +0000445
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800446 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000447
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100448 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100449 "LAC=%u BTS=%u\n", conn->subscr ?
450 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800451 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100452
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800453 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000454}
455
456/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200457static int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn)
Harald Welte52b1f982008-12-23 20:25:15 +0000458{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800459 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000460 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000461 struct gsm48_hdr *gh;
462 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200463 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000464
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800465 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000466
467 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
468 gh->proto_discr = GSM48_PDISC_MM;
469 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
470
471 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100472 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000473 bts->network->network_code, bts->location_area_code);
474
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200475 if (conn->subscr->tmsi == GSM_RESERVED_TMSI) {
476 uint8_t mi[10];
477 int len;
478 len = gsm48_generate_mid_from_imsi(mi, conn->subscr->imsi);
479 mid = msgb_put(msg, len);
480 memcpy(mid, mi, len);
481 } else {
482 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
483 gsm48_generate_mid_from_tmsi(mid, conn->subscr->tmsi);
484 }
Harald Welte52b1f982008-12-23 20:25:15 +0000485
486 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
487
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200488 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100489
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800490 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000491}
492
Harald Weltebf5e8df2009-02-03 12:59:45 +0000493/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200494static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000495{
496 struct msgb *msg = gsm48_msgb_alloc();
497 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000498
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800499 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000500
501 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
502 gh->proto_discr = GSM48_PDISC_MM;
503 gh->msg_type = GSM48_MT_MM_ID_REQ;
504 gh->data[0] = id_type;
505
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800506 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000507}
508
Harald Welte231ad4f2008-12-27 11:15:38 +0000509
Harald Weltebf5e8df2009-02-03 12:59:45 +0000510/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800511static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000512{
513 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000514 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200515 struct gsm_bts *bts = lchan->ts->trx->bts;
516 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200518 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000519
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200520 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welteb9845f92015-08-16 18:07:48 +0200521 DEBUGP(DMM, "IDENTITY RESPONSE: MI(%s)=%s\n",
522 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte231ad4f2008-12-27 11:15:38 +0000523
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200524 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100525
Harald Welte75a983f2008-12-27 21:34:06 +0000526 switch (mi_type) {
527 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200528 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100529 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100530 conn->subscr = subscr_get_by_imsi(net->subscr_group,
531 mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100532 if (!conn->subscr && net->create_subscriber)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100533 conn->subscr = subscr_create_subscriber(
534 net->subscr_group, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200535 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100536 if (!conn->subscr && conn->loc_operation) {
537 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
538 release_loc_updating_req(conn, 1);
539 return 0;
540 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100541 if (conn->loc_operation)
542 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000543 break;
544 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000545 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000546 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100547 if (conn->subscr) {
548 db_subscriber_assoc_imei(conn->subscr, mi_string);
549 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200550 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100551 if (conn->loc_operation)
552 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000553 break;
554 }
Holger Freyther73487a22008-12-31 18:53:57 +0000555
556 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100557 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000558}
559
Harald Welte255539c2008-12-28 02:26:27 +0000560
561static void loc_upd_rej_cb(void *data)
562{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100563 struct gsm_subscriber_connection *conn = data;
564 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100565 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000566
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100567 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800568 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100569 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000570}
571
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100572static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000573{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100574 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
575 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200576 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000577}
578
Harald Welteb9845f92015-08-16 18:07:48 +0200579static const struct value_string lupd_names[] = {
580 { GSM48_LUPD_NORMAL, "NORMAL" },
581 { GSM48_LUPD_PERIODIC, "PERIODIC" },
582 { GSM48_LUPD_IMSI_ATT, "IMSI ATTACH" },
583 { 0, NULL }
584};
Harald Welte2a139372009-02-22 21:14:55 +0000585
Harald Weltebf5e8df2009-02-03 12:59:45 +0000586/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800587static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000588{
Harald Welte8470bf22008-12-25 23:28:35 +0000589 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000590 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800591 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800592 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200593 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200594 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000595
Harald Welte8470bf22008-12-25 23:28:35 +0000596 lu = (struct gsm48_loc_upd_req *) gh->data;
597
598 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000599
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200600 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000601
Harald Welteb9845f92015-08-16 18:07:48 +0200602 DEBUGPC(DMM, "MI(%s)=%s type=%s ", gsm48_mi_type_name(mi_type),
603 mi_string, get_value_string(lupd_names, lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000604
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200605 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100606
Harald Welte24ff6ee2009-12-22 00:41:05 +0100607 switch (lu->type) {
608 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200609 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100610 break;
611 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200612 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100613 break;
614 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200615 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100616 break;
617 }
618
Holger Freythereaf04692009-06-06 13:54:44 +0000619 /*
620 * Pseudo Spoof detection: Just drop a second/concurrent
621 * location updating request.
622 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100623 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200624 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100625 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800626 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000627 return 0;
628 }
629
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800630 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000631
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200632 conn->loc_operation->key_seq = lu->key_seq;
633
Harald Welte52b1f982008-12-23 20:25:15 +0000634 switch (mi_type) {
635 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200636 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000637 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200638 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100639 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000640
Jan Luebbe370b41d2009-08-12 10:19:34 +0200641 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100642 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100643 if (!subscr && bts->network->create_subscriber) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100644 subscr = subscr_create_subscriber(
645 bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200646 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100647 if (!subscr) {
648 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
649 release_loc_updating_req(conn, 0);
650 return 0;
651 }
Harald Welte4b634542008-12-27 01:55:51 +0000652 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000653 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200654 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000655 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100656 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200657 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000658 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000659 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200660 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100661 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000662 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200663 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200664 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200665 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000666 break;
667 case GSM_MI_TYPE_IMEI:
668 case GSM_MI_TYPE_IMEISV:
669 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200670 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000671 break;
672 default:
Harald Weltea0368542009-06-27 02:58:43 +0200673 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000674 break;
675 }
676
Harald Welte24516ea2009-07-04 10:18:00 +0200677 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100678 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200679
Harald Welte4bfdfe72009-06-10 23:11:52 +0800680 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200681 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682 /* FIXME: request id? close channel? */
683 return -EINVAL;
684 }
685
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100686 conn->subscr = subscr;
687 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000688
Harald Welte24516ea2009-07-04 10:18:00 +0200689 /* check if we can let the subscriber into our network immediately
690 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100691 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000692}
693
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100694/* Turn int into semi-octet representation: 98 => 0x89 */
695static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800696{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100697 uint8_t ret;
698
699 ret = value / 10;
700 ret |= (value % 10) << 4;
701
702 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800703}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100704
Harald Welte4bfdfe72009-06-10 23:11:52 +0800705
Harald Weltedb253af2008-12-30 17:56:55 +0000706/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800707int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000708{
709 struct msgb *msg = gsm48_msgb_alloc();
710 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800711 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100712 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200713 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200714 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100715
Harald Welte4bfdfe72009-06-10 23:11:52 +0800716 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100717 struct tm* gmt_time;
718 struct tm* local_time;
719 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100720 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000721
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800722 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000723
724 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
725 gh->proto_discr = GSM48_PDISC_MM;
726 gh->msg_type = GSM48_MT_MM_INFO;
727
728 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200729#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000730 name_len = strlen(net->name_long);
731 /* 10.5.3.5a */
732 ptr8 = msgb_put(msg, 3);
733 ptr8[0] = GSM48_IE_NAME_LONG;
734 ptr8[1] = name_len*2 +1;
735 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
736
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200737 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000738 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000739 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000740
741 /* FIXME: Use Cell Broadcast, not UCS-2, since
742 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200743#endif
744 name_len = (strlen(net->name_long)*7)/8;
745 name_pad = (8 - strlen(net->name_long)*7)%8;
746 if (name_pad > 0)
747 name_len++;
748 /* 10.5.3.5a */
749 ptr8 = msgb_put(msg, 3);
750 ptr8[0] = GSM48_IE_NAME_LONG;
751 ptr8[1] = name_len +1;
752 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
753
754 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100755 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200756
Harald Weltedb253af2008-12-30 17:56:55 +0000757 }
758
759 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200760#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000761 name_len = strlen(net->name_short);
762 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200763 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200764 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000765 ptr8[1] = name_len*2 + 1;
766 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
767
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200768 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000769 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000770 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200771#endif
772 name_len = (strlen(net->name_short)*7)/8;
773 name_pad = (8 - strlen(net->name_short)*7)%8;
774 if (name_pad > 0)
775 name_len++;
776 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200777 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200778 ptr8[0] = GSM48_IE_NAME_SHORT;
779 ptr8[1] = name_len +1;
780 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
781
782 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100783 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200784
Harald Weltedb253af2008-12-30 17:56:55 +0000785 }
786
Harald Weltedb253af2008-12-30 17:56:55 +0000787 /* Section 10.5.3.9 */
788 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100789 gmt_time = gmtime(&cur_t);
790
Harald Weltedb253af2008-12-30 17:56:55 +0000791 ptr8 = msgb_put(msg, 8);
792 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100793 ptr8[1] = bcdify(gmt_time->tm_year % 100);
794 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
795 ptr8[3] = bcdify(gmt_time->tm_mday);
796 ptr8[4] = bcdify(gmt_time->tm_hour);
797 ptr8[5] = bcdify(gmt_time->tm_min);
798 ptr8[6] = bcdify(gmt_time->tm_sec);
799
Harald Welte45f91712012-07-08 16:48:11 +0200800 if (bts->tz.override) {
801 /* Convert tz.hr and tz.mn to units */
802 if (bts->tz.hr < 0) {
803 tzunits = ((bts->tz.hr/-1)*4);
804 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100805 ptr8[7] = bcdify(tzunits);
806 /* Set negative time */
807 ptr8[7] |= 0x08;
808 }
809 else {
Harald Welte45f91712012-07-08 16:48:11 +0200810 tzunits = bts->tz.hr*4;
811 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100812 ptr8[7] = bcdify(tzunits);
813 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100814 /* Convert DST value */
815 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
816 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100817 }
818 else {
819 /* Need to get GSM offset and convert into 15 min units */
820 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
821 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200822#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100823 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200824#else
825#warning find a portable way to obtain the timezone offset
826 tzunits = 0;
827#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100828 if (tzunits < 0) {
829 tzunits = tzunits/-1;
830 ptr8[7] = bcdify(tzunits);
831 /* Flip it to negative */
832 ptr8[7] |= 0x08;
833 }
834 else
835 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100836
837 /* Does not support DST +2 */
838 if (local_time->tm_isdst)
839 dst = 1;
840 }
841
842 if (dst) {
843 ptr8 = msgb_put(msg, 3);
844 ptr8[0] = GSM48_IE_NET_DST;
845 ptr8[1] = 1;
846 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100847 }
Harald Weltedb253af2008-12-30 17:56:55 +0000848
Daniel Willmanneea93372009-08-13 03:42:07 +0200849 DEBUGP(DMM, "-> MM INFO\n");
850
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800851 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000852}
853
Harald Welte7984d5c2009-08-12 22:56:50 +0200854/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200855int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200856{
857 struct msgb *msg = gsm48_msgb_alloc();
858 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200859 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200860
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200861 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200862
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800863 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200864 gh->proto_discr = GSM48_PDISC_MM;
865 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
866
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100867 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200868
Harald Welte7984d5c2009-08-12 22:56:50 +0200869 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200870 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200871 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200872
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800873 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200874}
875
876/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800877int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200878{
879 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800880 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200881}
882
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100883/*
884 * At the 30C3 phones miss their periodic update
885 * interval a lot and then remain unreachable. In case
886 * we still know the TMSI we can just attach it again.
887 */
888static void implit_attach(struct gsm_subscriber_connection *conn)
889{
890 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
891 return;
892
893 subscr_update(conn->subscr, conn->bts,
894 GSM_SUBSCRIBER_UPDATE_ATTACHED);
895}
896
897
Sylvain Munautba87f452009-12-24 00:28:46 +0100898static int _gsm48_rx_mm_serv_req_sec_cb(
899 unsigned int hooknum, unsigned int event,
900 struct msgb *msg, void *data, void *param)
901{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800902 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100903 int rc = 0;
904
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200905 /* auth failed or succeeded, the timer was stopped */
906 conn->expire_timer_stopped = 1;
907
Sylvain Munautba87f452009-12-24 00:28:46 +0100908 switch (event) {
909 case GSM_SECURITY_AUTH_FAILED:
910 /* Nothing to do */
911 break;
912
913 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200914 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800915 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100916 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100917 break;
918
919 case GSM_SECURITY_SUCCEEDED:
920 /* nothing to do. CIPHER MODE COMMAND is
921 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100922 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100923 break;
924
925 default:
926 rc = -EINVAL;
927 };
928
929 return rc;
930}
931
Harald Welte4ed0e922009-01-10 03:17:30 +0000932/*
933 * Handle CM Service Requests
934 * a) Verify that the packet is long enough to contain the information
935 * we require otherwsie reject with INCORRECT_MESSAGE
936 * b) Try to parse the TMSI. If we do not have one reject
937 * c) Check that we know the subscriber with the TMSI otherwise reject
938 * with a HLR cause
939 * d) Set the subscriber on the gsm_lchan and accept
940 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800941static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000942{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200943 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200944 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000945
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800946 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000947 struct gsm_subscriber *subscr;
948 struct gsm48_hdr *gh = msgb_l3(msg);
949 struct gsm48_service_request *req =
950 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800951 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200952 uint8_t classmark2_len = gh->data[1];
953 uint8_t *classmark2 = gh->data+2;
954 uint8_t mi_len = *(classmark2 + classmark2_len);
955 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000956
Harald Weltec9e02182009-05-01 19:07:53 +0000957 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000958 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000959 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800960 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000961 GSM48_REJECT_INCORRECT_MESSAGE);
962 }
963
964 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000965 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800966 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000967 GSM48_REJECT_INCORRECT_MESSAGE);
968 }
969
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200970 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000971 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200972
973 if (mi_type == GSM_MI_TYPE_IMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200974 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
975 req->cm_service_type, gsm48_mi_type_name(mi_type),
976 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200977 subscr = subscr_get_by_imsi(bts->network->subscr_group,
978 mi_string);
979 } else if (mi_type == GSM_MI_TYPE_TMSI) {
Harald Welteb9845f92015-08-16 18:07:48 +0200980 DEBUGPC(DMM, "serv_type=0x%02x MI(%s)=%s\n",
981 req->cm_service_type, gsm48_mi_type_name(mi_type),
982 mi_string);
Holger Hans Peter Freyther666e36a2015-07-13 20:33:08 +0200983 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
984 tmsi_from_string(mi_string));
985 } else {
986 DEBUGPC(DMM, "mi_type is not expected: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800987 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000988 GSM48_REJECT_INCORRECT_MESSAGE);
989 }
990
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200991 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100992
Harald Welte3ac7f102009-08-10 10:12:45 +0200993 if (is_siemens_bts(bts))
994 send_siemens_mrpci(msg->lchan, classmark2-1);
995
Holger Freythereb443982009-06-04 13:58:42 +0000996
Harald Welte2a139372009-02-22 21:14:55 +0000997 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000998 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800999 return gsm48_tx_mm_serv_rej(conn,
Holger Hans Peter Freyther5f8a0c52015-07-13 14:28:29 +02001000 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +00001001
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001002 if (!conn->subscr)
1003 conn->subscr = subscr;
1004 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +01001005 subscr_put(subscr); /* lchan already has a ref, don't need another one */
1006 else {
Harald Welte9bb7c702009-01-10 03:21:41 +00001007 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
1008 subscr_put(subscr);
1009 }
1010
Harald Weltec2e302d2009-07-05 14:08:13 +02001011 subscr->equipment.classmark2_len = classmark2_len;
1012 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
1013 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001014
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001015 /* we will send a MM message soon */
1016 conn->expire_timer_stopped = 1;
1017
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001018 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +01001019 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +00001020}
1021
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001022static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +00001023{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001024 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +00001025 struct gsm48_hdr *gh = msgb_l3(msg);
1026 struct gsm48_imsi_detach_ind *idi =
1027 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001028 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001029 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001030 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001031
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001032 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welteb9845f92015-08-16 18:07:48 +02001033 DEBUGP(DMM, "IMSI DETACH INDICATION: MI(%s)=%s",
1034 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001035
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001036 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001037
Harald Welte2a139372009-02-22 21:14:55 +00001038 switch (mi_type) {
1039 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001040 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001041 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001042 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001043 break;
1044 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001045 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001046 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1047 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001048 break;
1049 case GSM_MI_TYPE_IMEI:
1050 case GSM_MI_TYPE_IMEISV:
1051 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001052 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001053 break;
1054 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001055 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001056 break;
1057 }
1058
Holger Freyther4a49e772009-04-12 05:37:29 +00001059 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001060 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001061 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001062 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001063
1064 subscr->equipment.classmark1 = idi->classmark1;
1065 db_sync_equipment(&subscr->equipment);
1066
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001067 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001068 } else
Harald Welte2a139372009-02-22 21:14:55 +00001069 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1070
Harald Welte31981a02009-12-20 09:58:40 +01001071 /* FIXME: iterate over all transactions and release them,
1072 * imagine an IMSI DETACH happening during an active call! */
1073
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001074 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001075 return 0;
1076}
1077
Harald Welted2a7f5a2009-06-05 20:08:20 +00001078static int gsm48_rx_mm_status(struct msgb *msg)
1079{
1080 struct gsm48_hdr *gh = msgb_l3(msg);
1081
1082 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1083
1084 return 0;
1085}
1086
Sylvain Munaut30a15382009-12-24 00:27:26 +01001087/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001088static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001089{
1090 struct gsm48_hdr *gh = msgb_l3(msg);
1091 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001092 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001093
Sylvain Munautc593cf12010-06-10 23:51:41 +02001094 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001095 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001096
1097 /* Safety check */
1098 if (!conn->sec_operation) {
1099 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1100 return -EIO;
1101 }
1102
1103 /* Validate SRES */
1104 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001105 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001106 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001107
1108 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001109 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001110
Sylvain Munaut30a15382009-12-24 00:27:26 +01001111 if (cb)
1112 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001113 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001114
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001115 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001116 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001117 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001118 }
1119
Sylvain Munautc593cf12010-06-10 23:51:41 +02001120 DEBUGPC(DMM, "OK\n");
1121
Sylvain Munaut30a15382009-12-24 00:27:26 +01001122 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001123 return gsm0808_cipher_mode(conn, net->a5_encryption,
1124 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001125}
1126
Harald Weltebf5e8df2009-02-03 12:59:45 +00001127/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001128static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001129{
1130 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001131 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001132
1133 switch (gh->msg_type & 0xbf) {
1134 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001135 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001136 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001137 break;
1138 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001139 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001140 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001141 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001142 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001143 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001144 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001145 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001146 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001147 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001148 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001149 conn->subscr ?
1150 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001151 "unknown subscriber");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001152 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001153 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001154 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001155 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001156 break;
1157 case GSM48_MT_MM_CM_REEST_REQ:
1158 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1159 break;
1160 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001161 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001162 break;
1163 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001164 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001165 gh->msg_type);
1166 break;
1167 }
1168
1169 return rc;
1170}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001171
Harald Welte2d35ae62009-02-06 12:02:13 +00001172/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001173static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001174{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001175 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001176 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001177 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001178 uint8_t *classmark2_lv = gh->data + 1;
1179 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001180 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001181 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001182 int rc = 0;
1183
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001184 resp = (struct gsm48_pag_resp *) &gh->data[0];
1185 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1186 mi_string, &mi_type);
Harald Welteb9845f92015-08-16 18:07:48 +02001187 DEBUGP(DRR, "PAGING RESPONSE: MI(%s)=%s\n",
1188 gsm48_mi_type_name(mi_type), mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001189
Harald Weltefe18d8f2009-02-22 21:14:24 +00001190 switch (mi_type) {
1191 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001192 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001193 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001194 break;
1195 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001196 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1197 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001198 break;
1199 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001200
1201 if (!subscr) {
1202 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001203 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001204 return -EINVAL;
1205 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001206 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001207 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001208 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001209
Harald Weltec2e302d2009-07-05 14:08:13 +02001210 subscr->equipment.classmark2_len = *classmark2_lv;
1211 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1212 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001213
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001214 /* We received a paging */
1215 conn->expire_timer_stopped = 1;
1216
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001217 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001218 return rc;
1219}
1220
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001221static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001222{
1223 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001224 uint8_t apdu_id_flags;
1225 uint8_t apdu_len;
1226 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227
1228 apdu_id_flags = gh->data[0];
1229 apdu_len = gh->data[1];
1230 apdu_data = gh->data+2;
1231
Harald Welte70f92052012-07-16 13:22:19 +02001232 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001233 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001234
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001235 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001236}
1237
Harald Weltebf5e8df2009-02-03 12:59:45 +00001238/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001239static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001240{
1241 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001242 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001243
1244 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001245 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001246 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001247 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001248 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001249 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001250 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001251 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001252 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001253 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001254 break;
1255 }
1256
Harald Welte2d35ae62009-02-06 12:02:13 +00001257 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001258}
1259
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001260int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1261 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001262{
1263 struct msgb *msg = gsm48_msgb_alloc();
1264 struct gsm48_hdr *gh;
1265
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001266 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001267
1268 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1269 apdu_id, apdu_len);
1270
1271 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1272 gh->proto_discr = GSM48_PDISC_RR;
1273 gh->msg_type = GSM48_MT_RR_APP_INFO;
1274 gh->data[0] = apdu_id;
1275 gh->data[1] = apdu_len;
1276 memcpy(gh->data+2, apdu, apdu_len);
1277
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001278 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001279}
1280
Harald Welte4bc90a12008-12-27 16:32:52 +00001281/* Call Control */
1282
Harald Welte7584aea2009-02-11 11:44:12 +00001283/* The entire call control code is written in accordance with Figure 7.10c
1284 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1285 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1286 * it for voice */
1287
Harald Welte4bfdfe72009-06-10 23:11:52 +08001288static void new_cc_state(struct gsm_trans *trans, int state)
1289{
1290 if (state > 31 || state < 0)
1291 return;
1292
1293 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001294 gsm48_cc_state_name(trans->cc.state),
1295 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296
Harald Weltedcaf5652009-07-23 18:56:43 +02001297 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298}
1299
1300static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001301{
1302 struct msgb *msg = gsm48_msgb_alloc();
1303 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001304 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001305
Harald Welte4bc90a12008-12-27 16:32:52 +00001306 gh->msg_type = GSM48_MT_CC_STATUS;
1307
1308 cause = msgb_put(msg, 3);
1309 cause[0] = 2;
1310 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1311 cause[2] = 0x80 | 30; /* response to status inquiry */
1312
1313 call_state = msgb_put(msg, 1);
1314 call_state[0] = 0xc0 | 0x00;
1315
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001316 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001317}
1318
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001319static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001320 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001321{
1322 struct msgb *msg = gsm48_msgb_alloc();
1323 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1324
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001325 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001326
Harald Welte6f4b7532008-12-29 00:39:37 +00001327 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001328 gh->msg_type = msg_type;
1329
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001330 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001331}
1332
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1334{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001335 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001336 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001337 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001338 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 }
1340}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001341
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1343 int msg_type, struct gsm_mncc *mncc)
1344{
1345 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001346 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347
1348 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001349 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001350 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001352 trans->conn->lchan->ts->trx->bts->nr,
1353 trans->conn->lchan->ts->trx->nr,
1354 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 (trans->subscr)?(trans->subscr->extension):"-",
1356 get_mncc_name(msg_type));
1357 else
1358 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1359 "Sending '%s' to MNCC.\n",
1360 (trans->subscr)?(trans->subscr->extension):"-",
1361 get_mncc_name(msg_type));
1362 else
1363 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1364 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1365
1366 mncc->msg_type = msg_type;
1367
Harald Welte966636f2009-06-26 19:39:35 +02001368 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 if (!msg)
1370 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001371
1372 data = msgb_put(msg, sizeof(struct gsm_mncc));
1373 memcpy(data, mncc, sizeof(struct gsm_mncc));
1374
Harald Welte54209c22010-12-22 23:43:29 +01001375 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001376
1377 return 0;
1378}
1379
1380int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001381 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382{
1383 struct gsm_mncc rel;
1384
Harald Welte92f70c52009-06-12 01:54:08 +08001385 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001387 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001388 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1389 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1391}
1392
Harald Weltedcaf5652009-07-23 18:56:43 +02001393/* Call Control Specific transaction release.
1394 * gets called by trans_free, DO NOT CALL YOURSELF! */
1395void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001396{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001397 gsm48_stop_cc_timer(trans);
1398
1399 /* send release to L4, if callref still exists */
1400 if (trans->callref) {
1401 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001402 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001403 GSM48_CAUSE_LOC_PRN_S_LU,
1404 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001406 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001407 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001408 if (trans->conn)
1409 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410}
1411
1412static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001413
Harald Welte09e38af2009-02-16 22:52:23 +00001414/* call-back from paging the B-end of the connection */
1415static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001416 struct msgb *msg, void *_conn, void *_transt)
Harald Welte09e38af2009-02-16 22:52:23 +00001417{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001418 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001419 struct gsm_trans *transt = _transt;
Harald Weltec05677b2009-06-26 20:17:06 +02001420
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001421 OSMO_ASSERT(!transt->conn);
Harald Welte7584aea2009-02-11 11:44:12 +00001422
Harald Welte4bfdfe72009-06-10 23:11:52 +08001423 /* check all tranactions (without lchan) for subscriber */
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001424 switch (event) {
1425 case GSM_PAGING_SUCCEEDED:
1426 DEBUGP(DCC, "Paging subscr %s succeeded!\n", transt->subscr->extension);
Ivan Kluchnikov5fd0c642015-05-26 13:36:58 +03001427 OSMO_ASSERT(conn);
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001428 /* Assign lchan */
1429 transt->conn = conn;
1430 /* send SETUP request to called party */
1431 gsm48_cc_tx_setup(transt, &transt->cc.msg);
1432 break;
1433 case GSM_PAGING_EXPIRED:
1434 case GSM_PAGING_BUSY:
1435 DEBUGP(DCC, "Paging subscr %s expired!\n",
1436 transt->subscr->extension);
1437 /* Temporarily out of order */
1438 mncc_release_ind(transt->net, transt,
1439 transt->callref,
1440 GSM48_CAUSE_LOC_PRN_S_LU,
1441 GSM48_CC_CAUSE_DEST_OOO);
1442 transt->callref = 0;
1443 transt->paging_request = NULL;
1444 trans_free(transt);
1445 break;
1446 default:
1447 LOGP(DCC, LOGL_ERROR, "Unknown paging event %d\n", event);
1448 break;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001449 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001450
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02001451 transt->paging_request = NULL;
Harald Welte09e38af2009-02-16 22:52:23 +00001452 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001453}
Harald Welte7584aea2009-02-11 11:44:12 +00001454
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001455static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001456
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001457/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001458static int switch_for_handover(struct gsm_lchan *old_lchan,
1459 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001460{
1461 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001462
1463 if (ipacc_rtp_direct) {
1464 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1465 return 0;
1466 }
1467
1468 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001469 new_rs = new_lchan->abis_ip.rtp_socket;
1470 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001471
1472 if (!new_rs) {
1473 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1474 return -EIO;
1475 }
1476
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001477 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001478
1479 if (!old_rs) {
1480 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1481 return -EIO;
1482 }
1483
1484 /* copy rx_action and reference to other sock */
1485 new_rs->rx_action = old_rs->rx_action;
1486 new_rs->tx_action = old_rs->tx_action;
1487 new_rs->transmit = old_rs->transmit;
1488
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001489 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001490 case RTP_PROXY:
1491 other_rs = old_rs->proxy.other_sock;
1492 rtp_socket_proxy(new_rs, other_rs);
1493 /* delete reference to other end socket to prevent
1494 * rtp_socket_free() from removing the inverse reference */
1495 old_rs->proxy.other_sock = NULL;
1496 break;
1497 case RTP_RECV_UPSTREAM:
1498 new_rs->receive = old_rs->receive;
1499 break;
1500 case RTP_NONE:
1501 break;
1502 }
1503
1504 return 0;
1505}
1506
Harald Welte805f6442009-07-28 18:25:29 +02001507/* some other part of the code sends us a signal */
1508static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1509 void *handler_data, void *signal_data)
1510{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001511 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001512 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001513 struct gsm_network *net;
1514 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001515
1516 if (subsys != SS_ABISIP)
1517 return 0;
1518
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02001519 /* RTP bridge handling */
1520 if (lchan->conn && lchan->conn->mncc_rtp_bridge)
1521 return tch_rtp_signal(lchan, signal);
1522
Harald Welte805f6442009-07-28 18:25:29 +02001523 /* in case we use direct BTS-to-BTS RTP */
1524 if (ipacc_rtp_direct)
1525 return 0;
1526
Harald Welte805f6442009-07-28 18:25:29 +02001527 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001528 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001529 /* in case we don't use direct BTS-to-BTS RTP */
1530 /* the BTS has successfully bound a TCH to a local ip/port,
1531 * which means we can connect our UDP socket to it */
1532 if (lchan->abis_ip.rtp_socket) {
1533 rtp_socket_free(lchan->abis_ip.rtp_socket);
1534 lchan->abis_ip.rtp_socket = NULL;
1535 }
1536
1537 lchan->abis_ip.rtp_socket = rtp_socket_create();
1538 if (!lchan->abis_ip.rtp_socket)
1539 return -EIO;
1540
1541 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1542 lchan->abis_ip.bound_ip,
1543 lchan->abis_ip.bound_port);
1544 if (rc < 0)
1545 return -EIO;
1546
Harald Welteda7ab742009-12-19 22:23:05 +01001547 /* check if any transactions on this lchan still have
1548 * a tch_recv_mncc request pending */
1549 net = lchan->ts->trx->bts->network;
1550 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001551 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001552 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1553 tch_recv_mncc(net, trans->callref, 1);
1554 }
1555 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001556
1557 /*
1558 * TODO: this appears to be too early? Why not until after
1559 * the handover detect or the handover complete?
1560 *
1561 * Do we have a handover pending for this new lchan? In that
1562 * case re-route the audio from the old channel to the new one.
1563 */
1564 old_lchan = bsc_handover_pending(lchan);
1565 if (old_lchan)
1566 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001567 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001568 case S_ABISIP_DLCX_IND:
1569 /* the BTS tells us a RTP stream has been disconnected */
1570 if (lchan->abis_ip.rtp_socket) {
1571 rtp_socket_free(lchan->abis_ip.rtp_socket);
1572 lchan->abis_ip.rtp_socket = NULL;
1573 }
1574
1575 break;
Harald Welte805f6442009-07-28 18:25:29 +02001576 }
1577
1578 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001579}
1580
Harald Welte49f48b82009-02-17 15:29:33 +00001581/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001582static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001583{
Harald Welte11fa29c2009-02-19 17:24:39 +00001584 struct gsm_bts *bts = lchan->ts->trx->bts;
1585 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001586 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001587
Harald Welte11fa29c2009-02-19 17:24:39 +00001588 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1589 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1590 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1591
1592 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001593 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001594 return -EINVAL;
1595 }
Harald Welteda7ab742009-12-19 22:23:05 +01001596
1597 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001598 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001599 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001600 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001601 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001602 if (!lchan->abis_ip.rtp_socket) {
1603 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1604 "lchan\n");
1605 return -EIO;
1606 }
1607 if (!remote_lchan->abis_ip.rtp_socket) {
1608 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1609 "remote_lchan\n");
1610 return -EIO;
1611 }
1612
Harald Welte805f6442009-07-28 18:25:29 +02001613 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001614 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001615 if (rc < 0)
1616 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001617 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001618 if (rc < 0)
1619 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001620 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301621 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1622 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001623 } else {
1624 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301625 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1626 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301627 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001628 if (rc < 0)
1629 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301630 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1631 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301632 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001633 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001634 break;
1635 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001636 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001637 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001638 trau_mux_map_lchan(lchan, remote_lchan);
1639 break;
1640 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001641 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001642 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001643 }
Harald Welte49f48b82009-02-17 15:29:33 +00001644
1645 return 0;
1646}
1647
Harald Welte4bfdfe72009-06-10 23:11:52 +08001648/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001649static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001650{
Harald Weltedcaf5652009-07-23 18:56:43 +02001651 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1652 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001653
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001655 return -EIO;
1656
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001657 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001658 return -EIO;
1659
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001660 /* Which subscriber do we want to track trans1 or trans2? */
1661 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1662
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001664 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001665}
1666
Harald Welteda7ab742009-12-19 22:23:05 +01001667/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001668static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669{
1670 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001671 struct gsm_lchan *lchan;
1672 struct gsm_bts *bts;
1673 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001674
Harald Welte4bfdfe72009-06-10 23:11:52 +08001675 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001676 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001677 if (!trans)
1678 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001679 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001681
1682 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001683 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001684 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001686 /* store receive state */
1687 trans->tch_recv = enable;
1688
Harald Welteda7ab742009-12-19 22:23:05 +01001689 switch (bts->type) {
1690 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001691 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001692 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001693 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001694 return -EINVAL;
1695 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001696 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1697 * will not be connected to our RTP proxy and the socket will
1698 * not be assigned to the application interface. This method
1699 * will be called again, once the audio socket is created and
1700 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001701 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001702 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1703 return 0;
1704 }
1705 if (enable) {
1706 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001707 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001708 if (rc < 0)
1709 return rc;
1710 /* assign socket to application interface */
1711 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1712 net, callref);
1713 } else
1714 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1715 net, 0);
1716 break;
1717 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001718 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001719 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001720 /* In case we don't have a TCH with correct mode, the TRAU muxer
1721 * will not be asigned to the application interface. This is
1722 * performed by switch_trau_mux() after successful handover or
1723 * assignment. */
1724 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1725 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1726 return 0;
1727 }
Harald Welteda7ab742009-12-19 22:23:05 +01001728 if (enable)
1729 return trau_recv_lchan(lchan, callref);
1730 return trau_mux_unmap(NULL, callref);
1731 break;
1732 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001733 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001734 return -EINVAL;
1735 }
1736
1737 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738}
1739
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1741{
1742 DEBUGP(DCC, "-> STATUS ENQ\n");
1743 return gsm48_cc_tx_status(trans, msg);
1744}
1745
1746static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1747static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1748
1749static void gsm48_cc_timeout(void *arg)
1750{
1751 struct gsm_trans *trans = arg;
1752 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001753 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1754 int mo_location = GSM48_CAUSE_LOC_USER;
1755 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1756 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 struct gsm_mncc mo_rel, l4_rel;
1758
1759 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1760 mo_rel.callref = trans->callref;
1761 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1762 l4_rel.callref = trans->callref;
1763
Harald Weltedcaf5652009-07-23 18:56:43 +02001764 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 case 0x303:
1766 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001767 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 break;
1769 case 0x310:
1770 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001771 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 break;
1773 case 0x313:
1774 disconnect = 1;
1775 /* unknown, did not find it in the specs */
1776 break;
1777 case 0x301:
1778 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001779 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 break;
1781 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001782 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001784 gsm48_cc_tx_release(trans, &trans->cc.msg);
1785 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 break; /* stay in release state */
1787 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001788 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 return;
1790// release = 1;
1791// l4_cause = 14;
1792// break;
1793 case 0x306:
1794 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001795 mo_cause = trans->cc.msg.cause.value;
1796 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 break;
1798 case 0x323:
1799 disconnect = 1;
1800 break;
1801 default:
1802 release = 1;
1803 }
1804
1805 if (release && trans->callref) {
1806 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001807 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 l4_location, l4_cause);
1809 trans->callref = 0;
1810 }
1811
1812 if (disconnect && trans->callref) {
1813 /* process disconnect towards layer 4 */
1814 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001815 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 }
1817
1818 /* process disconnect towards mobile station */
1819 if (disconnect || release) {
1820 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001821 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1822 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1823 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 mo_rel.cause.diag_len = 3;
1825
1826 if (disconnect)
1827 gsm48_cc_tx_disconnect(trans, &mo_rel);
1828 if (release)
1829 gsm48_cc_tx_release(trans, &mo_rel);
1830 }
1831
1832}
1833
1834static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1835 int sec, int micro)
1836{
1837 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001838 trans->cc.timer.cb = gsm48_cc_timeout;
1839 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001840 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001841 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001842}
1843
1844static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1845{
1846 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001847 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1849 struct tlv_parsed tp;
1850 struct gsm_mncc setup;
1851
1852 memset(&setup, 0, sizeof(struct gsm_mncc));
1853 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001854 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001855 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 /* emergency setup is identified by msg_type */
1857 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1858 setup.emergency = 1;
1859
1860 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001861 setup.fields |= MNCC_F_CALLING;
1862 strncpy(setup.calling.number, trans->subscr->extension,
1863 sizeof(setup.calling.number)-1);
1864 strncpy(setup.imsi, trans->subscr->imsi,
1865 sizeof(setup.imsi)-1);
1866
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867 /* bearer capability */
1868 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1869 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001870 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001872 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 }
1874 /* facility */
1875 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1876 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1879 }
1880 /* called party bcd number */
1881 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1882 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1885 }
1886 /* user-user */
1887 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1888 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1891 }
1892 /* ss-version */
1893 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1894 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001895 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1897 }
1898 /* CLIR suppression */
1899 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1900 setup.clir.sup = 1;
1901 /* CLIR invocation */
1902 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1903 setup.clir.inv = 1;
1904 /* cc cap */
1905 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1906 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001907 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1909 }
1910
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 new_cc_state(trans, GSM_CSTATE_INITIATED);
1912
Harald Welte (local)47df3992009-12-26 19:45:03 +01001913 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1914 subscr_name(trans->subscr), trans->subscr->extension,
1915 setup.called.number);
1916
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001917 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001918
Harald Welte4bfdfe72009-06-10 23:11:52 +08001919 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001920 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921
Harald Welte13cac662009-07-29 12:10:35 +02001922 /* MNCC code will modify the channel asynchronously, we should
1923 * ipaccess-bind only after the modification has been made to the
1924 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 return 0;
1926}
1927
1928static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001929{
1930 struct msgb *msg = gsm48_msgb_alloc();
1931 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001932 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001933 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001934
Harald Welte7ccf7782009-02-17 01:43:01 +00001935 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001936
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 /* transaction id must not be assigned */
1938 if (trans->transaction_id != 0xff) { /* unasssigned */
1939 DEBUGP(DCC, "TX Setup with assigned transaction. "
1940 "This is not allowed!\n");
1941 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001942 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001943 GSM48_CAUSE_LOC_PRN_S_LU,
1944 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001946 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947 return rc;
1948 }
1949
1950 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001951 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
1952 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001953 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001955 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001956 GSM48_CAUSE_LOC_PRN_S_LU,
1957 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001959 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 return rc;
1961 }
Harald Welte78283ef2009-07-23 21:36:44 +02001962 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001963
Harald Welte65e74cc2008-12-29 01:55:35 +00001964 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001965
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001967
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 /* bearer capability */
1969 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001970 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 /* facility */
1972 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 /* progress */
1975 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001976 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 /* calling party BCD number */
1978 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001979 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980 /* called party BCD number */
1981 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 /* user-user */
1984 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001985 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001986 /* redirecting party BCD number */
1987 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001988 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989 /* signal */
1990 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001991 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992
1993 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001994
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001995 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001996
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001997 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001998}
1999
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
2001{
2002 struct gsm48_hdr *gh = msgb_l3(msg);
2003 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2004 struct tlv_parsed tp;
2005 struct gsm_mncc call_conf;
2006
2007 gsm48_stop_cc_timer(trans);
2008 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2009
2010 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2011 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002012 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002013 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014#if 0
2015 /* repeat */
2016 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2017 call_conf.repeat = 1;
2018 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2019 call_conf.repeat = 2;
2020#endif
2021 /* bearer capability */
2022 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2023 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002026 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 }
2028 /* cause */
2029 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2030 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002031 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2033 }
2034 /* cc cap */
2035 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2036 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002037 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2039 }
2040
Andreas Eversberg035b8742013-09-19 09:27:06 +02002041 /* IMSI of called subscriber */
2042 strncpy(call_conf.imsi, trans->subscr->imsi,
2043 sizeof(call_conf.imsi)-1);
2044
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2046
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002047 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002048 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049}
2050
2051static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2052{
2053 struct gsm_mncc *proceeding = arg;
2054 struct msgb *msg = gsm48_msgb_alloc();
2055 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2056
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2058
2059 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2060
2061 /* bearer capability */
2062 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002063 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 /* facility */
2065 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002066 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 /* progress */
2068 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002069 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002071 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072}
2073
2074static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2075{
2076 struct gsm48_hdr *gh = msgb_l3(msg);
2077 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2078 struct tlv_parsed tp;
2079 struct gsm_mncc alerting;
2080
2081 gsm48_stop_cc_timer(trans);
2082 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2083
2084 memset(&alerting, 0, sizeof(struct gsm_mncc));
2085 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002086 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002087 /* facility */
2088 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2089 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2092 }
2093
2094 /* progress */
2095 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2096 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002097 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2099 }
2100 /* ss-version */
2101 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2102 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002103 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2105 }
2106
2107 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2108
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002109 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002110 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111}
2112
2113static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2114{
2115 struct gsm_mncc *alerting = arg;
2116 struct msgb *msg = gsm48_msgb_alloc();
2117 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2118
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119 gh->msg_type = GSM48_MT_CC_ALERTING;
2120
2121 /* facility */
2122 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002123 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 /* progress */
2125 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002126 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 /* user-user */
2128 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130
2131 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2132
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002133 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134}
2135
2136static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2137{
2138 struct gsm_mncc *progress = arg;
2139 struct msgb *msg = gsm48_msgb_alloc();
2140 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2141
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142 gh->msg_type = GSM48_MT_CC_PROGRESS;
2143
2144 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002145 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146 /* user-user */
2147 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002148 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002150 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151}
2152
2153static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2154{
2155 struct gsm_mncc *connect = arg;
2156 struct msgb *msg = gsm48_msgb_alloc();
2157 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2158
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 gh->msg_type = GSM48_MT_CC_CONNECT;
2160
2161 gsm48_stop_cc_timer(trans);
2162 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2163
2164 /* facility */
2165 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002166 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002167 /* progress */
2168 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002169 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 /* connected number */
2171 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002172 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173 /* user-user */
2174 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002175 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176
2177 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2178
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002179 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180}
2181
2182static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2183{
2184 struct gsm48_hdr *gh = msgb_l3(msg);
2185 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2186 struct tlv_parsed tp;
2187 struct gsm_mncc connect;
2188
2189 gsm48_stop_cc_timer(trans);
2190
2191 memset(&connect, 0, sizeof(struct gsm_mncc));
2192 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002193 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002195 connect.fields |= MNCC_F_CONNECTED;
2196 strncpy(connect.connected.number, trans->subscr->extension,
2197 sizeof(connect.connected.number)-1);
2198 strncpy(connect.imsi, trans->subscr->imsi,
2199 sizeof(connect.imsi)-1);
2200
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201 /* facility */
2202 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2203 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002204 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2206 }
2207 /* user-user */
2208 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2209 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002210 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2212 }
2213 /* ss-version */
2214 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2215 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002216 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2218 }
2219
2220 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002221 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002223 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002224}
2225
2226
2227static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2228{
2229 struct gsm_mncc connect_ack;
2230
2231 gsm48_stop_cc_timer(trans);
2232
2233 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002234 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235
2236 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2237 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002238
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002239 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 &connect_ack);
2241}
2242
2243static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2244{
2245 struct msgb *msg = gsm48_msgb_alloc();
2246 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2247
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2249
2250 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2251
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002252 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253}
2254
2255static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2256{
2257 struct gsm48_hdr *gh = msgb_l3(msg);
2258 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2259 struct tlv_parsed tp;
2260 struct gsm_mncc disc;
2261
2262 gsm48_stop_cc_timer(trans);
2263
2264 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2265
2266 memset(&disc, 0, sizeof(struct gsm_mncc));
2267 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002268 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 /* cause */
2270 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2271 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002272 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2274 }
2275 /* facility */
2276 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2277 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002278 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2280 }
2281 /* user-user */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2283 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2286 }
2287 /* ss-version */
2288 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2289 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002290 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2292 }
2293
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002294 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295
2296}
2297
Harald Weltec66b71c2009-06-11 14:23:20 +08002298static struct gsm_mncc_cause default_cause = {
2299 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2300 .coding = 0,
2301 .rec = 0,
2302 .rec_val = 0,
2303 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2304 .diag_len = 0,
2305 .diag = { 0 },
2306};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307
2308static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2309{
2310 struct gsm_mncc *disc = arg;
2311 struct msgb *msg = gsm48_msgb_alloc();
2312 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2313
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2315
2316 gsm48_stop_cc_timer(trans);
2317 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2318
2319 /* cause */
2320 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002321 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 else
Harald Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324
2325 /* facility */
2326 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002327 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 /* progress */
2329 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002330 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 /* user-user */
2332 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002333 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334
2335 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002336 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337
2338 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2339
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002340 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002341}
2342
2343static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2344{
2345 struct gsm48_hdr *gh = msgb_l3(msg);
2346 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2347 struct tlv_parsed tp;
2348 struct gsm_mncc rel;
2349 int rc;
2350
2351 gsm48_stop_cc_timer(trans);
2352
2353 memset(&rel, 0, sizeof(struct gsm_mncc));
2354 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002355 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356 /* cause */
2357 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2358 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002359 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2361 }
2362 /* facility */
2363 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2364 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002365 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2367 }
2368 /* user-user */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2370 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2373 }
2374 /* ss-version */
2375 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2376 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002377 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2379 }
2380
Harald Weltedcaf5652009-07-23 18:56:43 +02002381 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002383 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002385 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002386 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002387 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002388 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002389 }
2390
2391 new_cc_state(trans, GSM_CSTATE_NULL);
2392
2393 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002394 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395
2396 return rc;
2397}
2398
2399static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2400{
2401 struct gsm_mncc *rel = arg;
2402 struct msgb *msg = gsm48_msgb_alloc();
2403 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2404
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 gh->msg_type = GSM48_MT_CC_RELEASE;
2406
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407 gsm48_stop_cc_timer(trans);
2408 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2409
2410 /* cause */
2411 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002412 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413 /* facility */
2414 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002415 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416 /* user-user */
2417 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002418 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419
Harald Weltedcaf5652009-07-23 18:56:43 +02002420 trans->cc.T308_second = 0;
2421 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422
Harald Weltedcaf5652009-07-23 18:56:43 +02002423 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2425
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002426 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427}
2428
2429static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2430{
2431 struct gsm48_hdr *gh = msgb_l3(msg);
2432 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2433 struct tlv_parsed tp;
2434 struct gsm_mncc rel;
2435 int rc = 0;
2436
2437 gsm48_stop_cc_timer(trans);
2438
2439 memset(&rel, 0, sizeof(struct gsm_mncc));
2440 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002441 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 /* cause */
2443 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2444 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002445 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2447 }
2448 /* facility */
2449 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2450 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002451 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2453 }
2454 /* user-user */
2455 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2456 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002457 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2459 }
2460 /* ss-version */
2461 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2462 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002463 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2465 }
2466
2467 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002468 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002470 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 MNCC_REJ_IND, &rel);
2472 break;
2473 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002474 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 MNCC_REL_CNF, &rel);
2476 break;
2477 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002478 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 MNCC_REL_IND, &rel);
2480 }
2481 }
2482
2483 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002484 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485
2486 return rc;
2487}
2488
2489static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2490{
2491 struct gsm_mncc *rel = arg;
2492 struct msgb *msg = gsm48_msgb_alloc();
2493 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002494 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2497
2498 trans->callref = 0;
2499
2500 gsm48_stop_cc_timer(trans);
2501
2502 /* cause */
2503 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002504 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505 /* facility */
2506 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002507 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 /* user-user */
2509 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002510 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511
Harald Weltec7782de2010-12-21 19:31:41 +01002512 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2513
Harald Weltedcaf5652009-07-23 18:56:43 +02002514 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515
Harald Weltec7782de2010-12-21 19:31:41 +01002516 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2520{
2521 struct gsm48_hdr *gh = msgb_l3(msg);
2522 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2523 struct tlv_parsed tp;
2524 struct gsm_mncc fac;
2525
2526 memset(&fac, 0, sizeof(struct gsm_mncc));
2527 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002528 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 /* facility */
2530 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2531 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002532 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2534 }
2535 /* ss-version */
2536 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2537 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002538 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2540 }
2541
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002542 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2546{
2547 struct gsm_mncc *fac = arg;
2548 struct msgb *msg = gsm48_msgb_alloc();
2549 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2550
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 gh->msg_type = GSM48_MT_CC_FACILITY;
2552
2553 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002554 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002556 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557}
2558
2559static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2560{
2561 struct gsm_mncc hold;
2562
2563 memset(&hold, 0, sizeof(struct gsm_mncc));
2564 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002565 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566}
2567
2568static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2569{
2570 struct msgb *msg = gsm48_msgb_alloc();
2571 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2572
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2574
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002575 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002576}
2577
2578static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2579{
2580 struct gsm_mncc *hold_rej = arg;
2581 struct msgb *msg = gsm48_msgb_alloc();
2582 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2583
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2585
2586 /* cause */
2587 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002588 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589 else
Harald Welte55c8f352010-03-07 23:40:35 +01002590 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002592 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593}
2594
2595static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2596{
2597 struct gsm_mncc retrieve;
2598
2599 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2600 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002601 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002602 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603}
2604
2605static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2606{
2607 struct msgb *msg = gsm48_msgb_alloc();
2608 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2609
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2611
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002612 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2616{
2617 struct gsm_mncc *retrieve_rej = arg;
2618 struct msgb *msg = gsm48_msgb_alloc();
2619 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2620
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2622
2623 /* cause */
2624 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002625 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 else
Harald Welte55c8f352010-03-07 23:40:35 +01002627 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002628
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002629 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2633{
2634 struct gsm48_hdr *gh = msgb_l3(msg);
2635 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2636 struct tlv_parsed tp;
2637 struct gsm_mncc dtmf;
2638
2639 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2640 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002641 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642 /* keypad facility */
2643 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2644 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2647 }
2648
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002649 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650}
2651
2652static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2653{
2654 struct gsm_mncc *dtmf = arg;
2655 struct msgb *msg = gsm48_msgb_alloc();
2656 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2657
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2659
2660 /* keypad */
2661 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002662 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002664 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665}
2666
2667static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2668{
2669 struct gsm_mncc *dtmf = arg;
2670 struct msgb *msg = gsm48_msgb_alloc();
2671 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2672
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2674
2675 /* cause */
2676 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002677 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 else
Harald Welte55c8f352010-03-07 23:40:35 +01002679 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002681 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682}
2683
2684static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2685{
2686 struct msgb *msg = gsm48_msgb_alloc();
2687 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2688
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2690
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002691 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692}
2693
2694static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2695{
2696 struct gsm_mncc dtmf;
2697
2698 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2699 dtmf.callref = trans->callref;
2700
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002701 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702}
2703
2704static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2705{
2706 struct gsm48_hdr *gh = msgb_l3(msg);
2707 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2708 struct tlv_parsed tp;
2709 struct gsm_mncc modify;
2710
2711 memset(&modify, 0, sizeof(struct gsm_mncc));
2712 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002713 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002714 /* bearer capability */
2715 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2716 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002717 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002719 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 }
2721
2722 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2723
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002724 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725}
2726
2727static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2728{
2729 struct gsm_mncc *modify = arg;
2730 struct msgb *msg = gsm48_msgb_alloc();
2731 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2732
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733 gh->msg_type = GSM48_MT_CC_MODIFY;
2734
2735 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2736
2737 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002738 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739
2740 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2741
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002742 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743}
2744
2745static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2746{
2747 struct gsm48_hdr *gh = msgb_l3(msg);
2748 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2749 struct tlv_parsed tp;
2750 struct gsm_mncc modify;
2751
2752 gsm48_stop_cc_timer(trans);
2753
2754 memset(&modify, 0, sizeof(struct gsm_mncc));
2755 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002756 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757 /* bearer capability */
2758 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2759 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002760 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002762 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002763 }
2764
2765 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2766
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002767 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768}
2769
2770static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2771{
2772 struct gsm_mncc *modify = arg;
2773 struct msgb *msg = gsm48_msgb_alloc();
2774 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2775
Harald Welte4bfdfe72009-06-10 23:11:52 +08002776 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2777
2778 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002779 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780
2781 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2782
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002783 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784}
2785
2786static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2787{
2788 struct gsm48_hdr *gh = msgb_l3(msg);
2789 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2790 struct tlv_parsed tp;
2791 struct gsm_mncc modify;
2792
2793 gsm48_stop_cc_timer(trans);
2794
2795 memset(&modify, 0, sizeof(struct gsm_mncc));
2796 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002797 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798 /* bearer capability */
2799 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2800 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002801 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002803 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 }
2805 /* cause */
2806 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2807 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002808 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2810 }
2811
2812 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2813
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002814 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815}
2816
2817static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2818{
2819 struct gsm_mncc *modify = arg;
2820 struct msgb *msg = gsm48_msgb_alloc();
2821 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2822
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2824
2825 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002826 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002828 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002829
2830 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2831
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002832 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833}
2834
2835static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2836{
2837 struct gsm_mncc *notify = arg;
2838 struct msgb *msg = gsm48_msgb_alloc();
2839 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2840
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 gh->msg_type = GSM48_MT_CC_NOTIFY;
2842
2843 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002844 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002846 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847}
2848
2849static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2850{
2851 struct gsm48_hdr *gh = msgb_l3(msg);
2852 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2853// struct tlv_parsed tp;
2854 struct gsm_mncc notify;
2855
2856 memset(&notify, 0, sizeof(struct gsm_mncc));
2857 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002858// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002860 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002862 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863}
2864
2865static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2866{
2867 struct gsm_mncc *user = arg;
2868 struct msgb *msg = gsm48_msgb_alloc();
2869 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2870
Harald Welte4bfdfe72009-06-10 23:11:52 +08002871 gh->msg_type = GSM48_MT_CC_USER_INFO;
2872
2873 /* user-user */
2874 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002875 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 /* more data */
2877 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002878 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002880 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881}
2882
2883static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2884{
2885 struct gsm48_hdr *gh = msgb_l3(msg);
2886 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2887 struct tlv_parsed tp;
2888 struct gsm_mncc user;
2889
2890 memset(&user, 0, sizeof(struct gsm_mncc));
2891 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002892 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002893 /* user-user */
2894 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2895 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002896 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2898 }
2899 /* more data */
2900 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2901 user.more = 1;
2902
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002903 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002904}
2905
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002906static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002907{
2908 struct gsm_mncc *mode = arg;
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002909 struct gsm_lchan *lchan = trans->conn->lchan;
2910
2911 /*
2912 * We were forced to make an assignment a lot earlier and
2913 * we should avoid sending another assignment that might
2914 * even lead to a different kind of lchan (TCH/F vs. TCH/H).
2915 * In case of rtp-bridge it is too late to change things
2916 * here.
2917 */
2918 if (trans->conn->mncc_rtp_bridge && lchan->tch_mode != GSM48_CMODE_SIGN)
2919 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002921 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2922 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923}
2924
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02002925static void mncc_recv_rtp(struct gsm_network *net, uint32_t callref,
2926 int cmd, uint32_t addr, uint16_t port, uint32_t payload_type,
2927 uint32_t payload_msg_type)
2928{
2929 uint8_t data[sizeof(struct gsm_mncc)];
2930 struct gsm_mncc_rtp *rtp;
2931
2932 memset(&data, 0, sizeof(data));
2933 rtp = (struct gsm_mncc_rtp *) &data[0];
2934
2935 rtp->callref = callref;
2936 rtp->msg_type = cmd;
2937 rtp->ip = addr;
2938 rtp->port = port;
2939 rtp->payload_type = payload_type;
2940 rtp->payload_msg_type = payload_msg_type;
2941 mncc_recvmsg(net, NULL, cmd, (struct gsm_mncc *)data);
2942}
2943
2944static void mncc_recv_rtp_sock(struct gsm_network *net, struct gsm_trans *trans, int cmd)
2945{
2946 struct gsm_lchan *lchan;
2947 int msg_type;
2948
2949 lchan = trans->conn->lchan;
2950 switch (lchan->abis_ip.rtp_payload) {
2951 case RTP_PT_GSM_FULL:
2952 msg_type = GSM_TCHF_FRAME;
2953 break;
2954 case RTP_PT_GSM_EFR:
2955 msg_type = GSM_TCHF_FRAME_EFR;
2956 break;
2957 case RTP_PT_GSM_HALF:
2958 msg_type = GSM_TCHH_FRAME;
2959 break;
2960 case RTP_PT_AMR:
2961 msg_type = GSM_TCH_FRAME_AMR;
2962 break;
2963 default:
2964 LOGP(DMNCC, LOGL_ERROR, "%s unknown payload type %d\n",
2965 gsm_lchan_name(lchan), lchan->abis_ip.rtp_payload);
2966 msg_type = 0;
2967 break;
2968 }
2969
2970 return mncc_recv_rtp(net, trans->callref, cmd,
2971 lchan->abis_ip.bound_ip,
2972 lchan->abis_ip.bound_port,
2973 lchan->abis_ip.rtp_payload,
2974 msg_type);
2975}
2976
2977static void mncc_recv_rtp_err(struct gsm_network *net, uint32_t callref, int cmd)
2978{
2979 return mncc_recv_rtp(net, callref, cmd, 0, 0, 0, 0);
2980}
2981
2982static int tch_rtp_create(struct gsm_network *net, uint32_t callref)
2983{
2984 struct gsm_bts *bts;
2985 struct gsm_lchan *lchan;
2986 struct gsm_trans *trans;
2987
2988 /* Find callref */
2989 trans = trans_find_by_callref(net, callref);
2990 if (!trans) {
2991 LOGP(DMNCC, LOGL_ERROR, "RTP create for non-existing trans\n");
2992 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2993 return -EIO;
2994 }
2995 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
2996 if (!trans->conn) {
2997 LOGP(DMNCC, LOGL_NOTICE, "RTP create for trans without conn\n");
2998 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
2999 return 0;
3000 }
3001
3002 lchan = trans->conn->lchan;
3003 bts = lchan->ts->trx->bts;
3004 if (!is_ipaccess_bts(bts)) {
3005 /*
3006 * I want this to be straight forward and have no audio flow
3007 * through the nitb/osmo-mss system. This currently means that
3008 * this will not work with BS11/Nokia type BTS. We would need
3009 * to have a trau<->rtp bridge for these but still preferable
3010 * in another process.
3011 */
3012 LOGP(DMNCC, LOGL_ERROR, "RTP create only works with IP systems\n");
3013 mncc_recv_rtp_err(net, callref, MNCC_RTP_CREATE);
3014 return -EINVAL;
3015 }
3016
3017 trans->conn->mncc_rtp_bridge = 1;
3018 /*
3019 * *sigh* we need to pick a codec now. Pick the most generic one
3020 * right now and hope we could fix that later on. This is very
3021 * similiar to the above routine.
3022 * TODO: Use the default codec version...
3023 */
3024 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
3025 trans->conn->mncc_rtp_create_pending = 1;
3026 /* TODO... transport or fix the default type... */
3027 return gsm0808_assign_req(trans->conn, GSM48_CMODE_SPEECH_V1,
3028 lchan->type != GSM_LCHAN_TCH_H);
3029 }
3030
3031 mncc_recv_rtp_sock(trans->net, trans, MNCC_RTP_CREATE);
3032 return 0;
3033}
3034
3035static int tch_rtp_connect(struct gsm_network *net, void *arg)
3036{
3037 struct gsm_lchan *lchan;
3038 struct gsm_trans *trans;
3039 struct gsm_mncc_rtp *rtp = arg;
3040
3041 /* Find callref */
3042 trans = trans_find_by_callref(net, rtp->callref);
3043 if (!trans) {
3044 LOGP(DMNCC, LOGL_ERROR, "RTP connect for non-existing trans\n");
3045 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3046 return -EIO;
3047 }
3048 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
3049 if (!trans->conn) {
3050 LOGP(DMNCC, LOGL_ERROR, "RTP connect for trans without conn\n");
3051 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3052 return 0;
3053 }
3054
3055 lchan = trans->conn->lchan;
3056
3057 /* TODO: Check if payload_msg_type is compatible with what we have */
3058 if (rtp->payload_type != lchan->abis_ip.rtp_payload) {
3059 LOGP(DMNCC, LOGL_ERROR, "RTP connect with different RTP payload\n");
3060 mncc_recv_rtp_err(net, rtp->callref, MNCC_RTP_CONNECT);
3061 }
3062
3063 /*
3064 * FIXME: payload2 can't be sent with MDCX as the osmo-bts code
3065 * complains about both rtp and rtp payload2 being present in the
3066 * same package!
3067 */
3068 return rsl_ipacc_mdcx(lchan, rtp->ip, rtp->port, 0);
3069}
3070
3071static int tch_rtp_signal(struct gsm_lchan *lchan, int signal)
3072{
3073 struct gsm_network *net;
3074 struct gsm_trans *tmp, *trans = NULL;
3075
3076 net = lchan->ts->trx->bts->network;
3077 llist_for_each_entry(tmp, &net->trans_list, entry) {
3078 if (!tmp->conn)
3079 continue;
3080 if (tmp->conn->lchan != lchan)
3081 continue;
3082 trans = tmp;
3083 break;
3084 }
3085
3086 if (!trans) {
3087 LOGP(DMNCC, LOGL_ERROR, "%s IPA abis signal but no transaction.\n",
3088 gsm_lchan_name(lchan));
3089 return 0;
3090 }
3091
3092 switch (signal) {
3093 case S_ABISIP_CRCX_ACK:
3094 if (lchan->conn->mncc_rtp_create_pending) {
3095 lchan->conn->mncc_rtp_create_pending = 0;
3096 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP create ind.\n",
3097 gsm_lchan_name(lchan));
3098 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CREATE);
3099 }
3100 break;
3101 case S_ABISIP_MDCX_ACK:
3102 if (lchan->conn->mncc_rtp_bridge) {
3103 LOGP(DMNCC, LOGL_NOTICE, "%s sending pending RTP connect ind.\n",
3104 gsm_lchan_name(lchan));
3105 mncc_recv_rtp_sock(net, trans, MNCC_RTP_CONNECT);
3106 }
3107 break;
3108 }
3109
3110 return 0;
3111}
3112
3113
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003115 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003116 int type;
3117 int (*rout) (struct gsm_trans *trans, void *arg);
3118} downstatelist[] = {
3119 /* mobile originating call establishment */
3120 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
3121 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
3122 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
3123 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
3124 {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 */
3125 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
3126 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
3127 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
3128 /* mobile terminating call establishment */
3129 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
3130 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
3131 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
3132 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
3133 /* signalling during call */
3134 {SBIT(GSM_CSTATE_ACTIVE),
3135 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
3136 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
3137 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
3138 {ALL_STATES,
3139 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
3140 {ALL_STATES,
3141 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
3142 {ALL_STATES,
3143 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
3144 {SBIT(GSM_CSTATE_ACTIVE),
3145 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
3146 {SBIT(GSM_CSTATE_ACTIVE),
3147 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
3148 {SBIT(GSM_CSTATE_ACTIVE),
3149 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
3150 {SBIT(GSM_CSTATE_ACTIVE),
3151 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
3152 {SBIT(GSM_CSTATE_ACTIVE),
3153 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
3154 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3155 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
3156 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
3157 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
3158 {SBIT(GSM_CSTATE_ACTIVE),
3159 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
3160 /* clearing */
3161 {SBIT(GSM_CSTATE_INITIATED),
3162 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
3163 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
3164 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
3165 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3166 MNCC_REL_REQ, gsm48_cc_tx_release},
3167 /* special */
3168 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02003169 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08003170};
3171
3172#define DOWNSLLEN \
3173 (sizeof(downstatelist) / sizeof(struct downstate))
3174
3175
Harald Welte76556372010-12-22 23:57:45 +01003176int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177{
Harald Welte1a6f7982009-08-09 18:52:33 +02003178 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003179 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003180 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01003181 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003182 struct gsm_mncc *data = arg, rel;
3183
Harald Welte04dc88f2010-12-22 21:45:05 +01003184 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
3185
Harald Welte4bfdfe72009-06-10 23:11:52 +08003186 /* handle special messages */
3187 switch(msg_type) {
3188 case MNCC_BRIDGE:
3189 return tch_bridge(net, arg);
3190 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01003191 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003192 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01003193 return tch_recv_mncc(net, data->callref, 1);
Holger Hans Peter Freytherc8a6c132015-08-04 13:32:09 +02003194 case MNCC_RTP_CREATE:
3195 return tch_rtp_create(net, data->callref);
3196 case MNCC_RTP_CONNECT:
3197 return tch_rtp_connect(net, arg);
3198 case MNCC_RTP_FREE:
3199 /* unused right now */
3200 return -EIO;
Harald Welteda7ab742009-12-19 22:23:05 +01003201 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01003202 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003203 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01003204 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01003205 /* Find callref */
3206 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01003207 if (!trans) {
3208 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003209 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01003210 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003211 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01003212 if (!trans->conn) {
3213 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003214 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003215 }
Andreas Eversberg93e795c2013-03-11 08:20:48 +01003216 if (!trans->conn->lchan) {
3217 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without lchan\n");
3218 return 0;
3219 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003220 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3221 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003222 /* This should be LOGL_ERROR or NOTICE, but
3223 * unfortuantely it happens for a couple of frames at
3224 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003225 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003226 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003227 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003228 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003229 switch (bts->type) {
3230 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003231 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003232 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003233 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003234 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003235 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003236 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003237 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003238 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003239 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003240 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003241 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003242 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003243 }
3244 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003245 }
3246
3247 memset(&rel, 0, sizeof(struct gsm_mncc));
3248 rel.callref = data->callref;
3249
3250 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003251 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003252
3253 /* Callref unknown */
3254 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003255 struct gsm_subscriber *subscr;
3256
Harald Welte4a3464c2009-07-04 10:11:24 +02003257 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003258 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3259 "Received '%s' from MNCC with "
3260 "unknown callref %d\n", data->called.number,
3261 get_mncc_name(msg_type), data->callref);
3262 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003263 return mncc_release_ind(net, NULL, data->callref,
3264 GSM48_CAUSE_LOC_PRN_S_LU,
3265 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003266 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003267 if (!data->called.number[0] && !data->imsi[0]) {
3268 DEBUGP(DCC, "(bts - trx - ts - ti) "
3269 "Received '%s' from MNCC with "
3270 "no number or IMSI\n", get_mncc_name(msg_type));
3271 /* Invalid number */
3272 return mncc_release_ind(net, NULL, data->callref,
3273 GSM48_CAUSE_LOC_PRN_S_LU,
3274 GSM48_CC_CAUSE_INV_NR_FORMAT);
3275 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003276 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003277 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003278 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003279 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003280 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003281 subscr = subscr_get_by_imsi(net->subscr_group,
3282 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003283
3284 /* update the subscriber we deal with */
3285 log_set_context(BSC_CTX_SUBSCR, subscr);
3286
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 /* If subscriber is not found */
3288 if (!subscr) {
3289 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3290 "Received '%s' from MNCC with "
3291 "unknown subscriber %s\n", data->called.number,
3292 get_mncc_name(msg_type), data->called.number);
3293 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003294 return mncc_release_ind(net, NULL, data->callref,
3295 GSM48_CAUSE_LOC_PRN_S_LU,
3296 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003297 }
3298 /* If subscriber is not "attached" */
3299 if (!subscr->lac) {
3300 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3301 "Received '%s' from MNCC with "
3302 "detached subscriber %s\n", data->called.number,
3303 get_mncc_name(msg_type), data->called.number);
3304 subscr_put(subscr);
3305 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003306 return mncc_release_ind(net, NULL, data->callref,
3307 GSM48_CAUSE_LOC_PRN_S_LU,
3308 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003309 }
3310 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003311 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003312 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003313 DEBUGP(DCC, "No memory for trans.\n");
3314 subscr_put(subscr);
3315 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003316 mncc_release_ind(net, NULL, data->callref,
3317 GSM48_CAUSE_LOC_PRN_S_LU,
3318 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003319 return -ENOMEM;
3320 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003321 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003322 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003323
Harald Welte4bfdfe72009-06-10 23:11:52 +08003324 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003325 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003326 /* find transaction with this subscriber already paging */
3327 llist_for_each_entry(transt, &net->trans_list, entry) {
3328 /* Transaction of our lchan? */
3329 if (transt == trans ||
3330 transt->subscr != subscr)
3331 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003332 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003333 "Received '%s' from MNCC with "
3334 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003335 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003336 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003337 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003338 subscr_put(subscr);
3339 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003340 return 0;
3341 }
3342 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003343 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003344
Holger Hans Peter Freytherd6d7aff2015-04-06 12:03:45 +02003345 /* Request a channel */
3346 trans->paging_request = subscr_request_channel(subscr,
3347 RSL_CHANNEED_TCH_F, setup_trig_pag_evt,
3348 trans);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003349 if (!trans->paging_request) {
3350 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3351 subscr_put(subscr);
3352 trans_free(trans);
3353 return 0;
3354 }
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003355 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003356 return 0;
3357 }
3358 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003359 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003360 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003361 } else {
3362 /* update the subscriber we deal with */
3363 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003364 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003365
3366 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003367 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003368
3369 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003370 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003371 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003372 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003373 (trans->subscr)?(trans->subscr->extension):"-",
3374 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003375 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3376 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003377 if (msg_type == MNCC_REL_REQ)
3378 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3379 else
3380 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3381 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003382 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003383 return rc;
3384 }
3385
3386 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3387 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003388 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003389 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003390 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003391 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003392 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003393
3394 /* Find function for current state and message */
3395 for (i = 0; i < DOWNSLLEN; i++)
3396 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003397 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003398 break;
3399 if (i == DOWNSLLEN) {
3400 DEBUGP(DCC, "Message unhandled at this state.\n");
3401 return 0;
3402 }
3403
3404 rc = downstatelist[i].rout(trans, arg);
3405
3406 return rc;
3407}
3408
3409
3410static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003411 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003412 int type;
3413 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3414} datastatelist[] = {
3415 /* mobile originating call establishment */
3416 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3417 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3418 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3419 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3420 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3421 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3422 /* mobile terminating call establishment */
3423 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3424 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3425 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3426 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003427 {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 +08003428 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3429 /* signalling during call */
3430 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3431 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3432 {SBIT(GSM_CSTATE_ACTIVE),
3433 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3434 {ALL_STATES,
3435 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3436 {ALL_STATES,
3437 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3438 {ALL_STATES,
3439 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3440 {SBIT(GSM_CSTATE_ACTIVE),
3441 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3442 {SBIT(GSM_CSTATE_ACTIVE),
3443 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3444 {SBIT(GSM_CSTATE_ACTIVE),
3445 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3446 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3447 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3448 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3449 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3450 {SBIT(GSM_CSTATE_ACTIVE),
3451 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3452 /* clearing */
3453 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3454 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3455 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3456 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3457 {ALL_STATES, /* 5.4.3.4 */
3458 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3459};
3460
3461#define DATASLLEN \
3462 (sizeof(datastatelist) / sizeof(struct datastate))
3463
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003464static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003465{
3466 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003467 uint8_t msg_type = gh->msg_type & 0xbf;
3468 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003469 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003470 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003471
Harald Welte4bfdfe72009-06-10 23:11:52 +08003472 if (msg_type & 0x80) {
3473 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3474 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003475 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003476
Harald Welte4bfdfe72009-06-10 23:11:52 +08003477 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003478 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003479
Harald Welte6f5aee02009-07-23 21:21:14 +02003480 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003481 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003482 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003483 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003484 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3485 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003486
3487 /* Create transaction */
3488 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003489 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003490 "creating new trans.\n", transaction_id);
3491 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003492 trans = trans_alloc(conn->bts->network, conn->subscr,
3493 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003494 transaction_id, new_callref++);
3495 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003496 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003497 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003498 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003499 GSM48_MT_CC_RELEASE_COMPL);
3500 return -ENOMEM;
3501 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003502 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003503 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003504 }
3505
3506 /* find function for current state and message */
3507 for (i = 0; i < DATASLLEN; i++)
3508 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003509 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003510 break;
3511 if (i == DATASLLEN) {
3512 DEBUGP(DCC, "Message unhandled at this state.\n");
3513 return 0;
3514 }
3515
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003516 assert(trans->subscr);
3517
Harald Welte4bfdfe72009-06-10 23:11:52 +08003518 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003519
3520 return rc;
3521}
3522
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003523/* Create a dummy to wait five seconds */
3524static void release_anchor(struct gsm_subscriber_connection *conn)
3525{
3526 if (!conn->anch_operation)
3527 return;
3528
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003529 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003530 talloc_free(conn->anch_operation);
3531 conn->anch_operation = NULL;
3532}
3533
3534static void anchor_timeout(void *_data)
3535{
3536 struct gsm_subscriber_connection *con = _data;
3537
3538 release_anchor(con);
3539 msc_release_connection(con);
3540}
3541
3542int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3543{
3544 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3545 if (!conn->anch_operation)
3546 return -1;
3547
3548 conn->anch_operation->timeout.data = conn;
3549 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003550 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003551 return 0;
3552}
3553
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003554/* here we get data from the BSC level... */
3555int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003556{
3557 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003558 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003559 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003560
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003561 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003562 if (silent_call_reroute(conn, msg))
3563 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003564
3565 switch (pdisc) {
3566 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003567 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003568 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003569 break;
3570 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003571 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003572 break;
3573 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003574 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003575 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003576 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003577 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003578 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003579 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003580 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003581 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003582 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3583 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003584 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003585 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003586 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003587 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003588 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003589 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003590 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3591 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003592 break;
3593 }
3594
3595 return rc;
3596}
Harald Welte8470bf22008-12-25 23:28:35 +00003597
Harald Welte805f6442009-07-28 18:25:29 +02003598/*
3599 * This will be ran by the linker when loading the DSO. We use it to
3600 * do system initialization, e.g. registration of signal handlers.
3601 */
3602static __attribute__((constructor)) void on_dso_load_0408(void)
3603{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003604 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003605}