blob: 941090c8de04d5828a6e11b705d973002b4640e9 [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 Freytherc42ad8b2011-04-18 17:04:00 +020070int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080071static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020072 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010073static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080074static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000075
Harald Welte52b1f982008-12-23 20:25:15 +000076struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077 uint16_t mcc;
78 uint16_t mnc;
79 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000080};
81
Andreas Eversberga83d5112013-12-07 18:32:28 +010082static int apply_codec_restrictions(struct gsm_bts *bts,
83 struct gsm_mncc_bearer_cap *bcap)
84{
85 int i, j;
86
87 /* remove unsupported speech versions from list */
88 for (i = 0, j = 0; bcap->speech_ver[i] >= 0; i++) {
89 if (bcap->speech_ver[i] == GSM48_BCAP_SV_FR)
90 bcap->speech_ver[j++] = GSM48_BCAP_SV_FR;
91 if (bcap->speech_ver[i] == GSM48_BCAP_SV_EFR && bts->codec.efr)
92 bcap->speech_ver[j++] = GSM48_BCAP_SV_EFR;
93 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_F && bts->codec.amr)
94 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_F;
95 if (bcap->speech_ver[i] == GSM48_BCAP_SV_HR && bts->codec.hr)
96 bcap->speech_ver[j++] = GSM48_BCAP_SV_HR;
97 if (bcap->speech_ver[i] == GSM48_BCAP_SV_AMR_H && bts->codec.amr)
98 bcap->speech_ver[j++] = GSM48_BCAP_SV_AMR_H;
99 }
100 bcap->speech_ver[j] = -1;
101
102 return 0;
103}
104
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200105static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800106
Harald Welte31c00f72011-03-03 23:29:05 +0100107void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
108{
109 net->mncc_recv(net, msg);
110}
111
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800112static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
113 struct gsm_trans *trans)
114{
115 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
116
117 /* if we get passed a transaction reference, do some common
118 * work that the caller no longer has to do */
119 if (trans) {
120 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
121 msg->lchan = trans->conn->lchan;
122 }
123
124
125 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200126 struct e1inp_sign_link *sign_link =
127 msg->lchan->ts->trx->rsl_link;
128
129 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800130 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
131 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200132 "Sending '%s' to MS.\n",
133 sign_link->trx->bts->nr,
134 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800135 gh->proto_discr & 0xf0,
136 gsm48_cc_msg_name(gh->msg_type));
137 else
138 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200139 "Sending 0x%02x to MS.\n",
140 sign_link->trx->bts->nr,
141 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800142 gh->proto_discr, gh->msg_type);
143 }
144
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100145 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800146}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100147
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800148int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
149{
150 struct gsm48_hdr *gh;
151 struct msgb *ss_notify;
152
153 ss_notify = gsm0480_create_notifySS(message);
154 if (!ss_notify)
155 return -1;
156
157 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
158 uint8_t *data = msgb_push(ss_notify, 1);
159 data[0] = ss_notify->len - 1;
160 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
161 gh->msg_type = GSM48_MT_CC_FACILITY;
162 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
163}
164
Harald Weltecf149ee2012-01-23 16:40:24 +0100165void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100166{
167 if (!conn->sec_operation)
168 return;
169
170 talloc_free(conn->sec_operation);
171 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800172 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100173}
174
Harald Weltecf149ee2012-01-23 16:40:24 +0100175void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100176{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100177 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
178 struct gsm_security_operation);
179}
180
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100181int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
182 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800184 struct gsm_network *net = conn->bts->network;
185 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100186 struct gsm_security_operation *op;
187 struct gsm_auth_tuple atuple;
188 int status = -1, rc;
189
190 /* Check if we _can_ enable encryption. Cases where we can't:
191 * - Encryption disabled in config
192 * - Channel already secured (nothing to do)
193 * - Subscriber equipment doesn't support configured encryption
194 */
195 if (!net->a5_encryption) {
196 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800197 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100198 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200199 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100200 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
201 net->a5_encryption)) {
202 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
203 status = GSM_SECURITY_NOAVAIL;
204 }
205
206 /* If not done yet, try to get info for this user */
207 if (status < 0) {
208 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
209 if (rc <= 0)
210 status = GSM_SECURITY_NOAVAIL;
211 }
212
213 /* Are we done yet ? */
214 if (status >= 0)
215 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800216 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100217 0;
218
219 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800220 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100221 return -EBUSY;
222
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800223 allocate_security_operation(conn);
224 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100225 op->cb = cb;
226 op->cb_data = cb_data;
227 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
228
229 /* FIXME: Should start a timer for completion ... */
230
231 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100232 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100233 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800234 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100235 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100236 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100237 return gsm0808_cipher_mode(conn, net->a5_encryption,
238 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100239 }
240
241 return -EINVAL; /* not reached */
242}
243
Holger Freyther73487a22008-12-31 18:53:57 +0000244static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
245 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000246{
247 if (!subscriber)
248 return 0;
249
Holger Freyther73487a22008-12-31 18:53:57 +0000250 /*
251 * Do not send accept yet as more information should arrive. Some
252 * phones will not send us the information and we will have to check
253 * what we want to do with that.
254 */
255 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
256 return 0;
257
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100258 switch (subscriber->group->net->auth_policy) {
Jan Luebbe06513f22009-08-12 12:48:00 +0200259 case GSM_AUTH_POLICY_CLOSED:
260 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200261 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200262 if (subscriber->authorized)
263 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200264 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200265 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000266 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200267 default:
268 return 0;
269 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000270}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000271
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100272static void release_loc_updating_req(struct gsm_subscriber_connection *conn, int release)
Holger Freyther73487a22008-12-31 18:53:57 +0000273{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100274 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000275 return;
276
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800277 /* No need to keep the connection up */
278 release_anchor(conn);
279
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200280 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100281 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100282 conn->loc_operation = NULL;
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100283 if (release)
284 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000285}
286
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100287static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000288{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800289 if (conn->loc_operation)
290 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100291 release_loc_updating_req(conn, 0);
Holger Freyther73487a22008-12-31 18:53:57 +0000292
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100293 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200294 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000295}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000296
Sylvain Munaut267fba02009-12-24 00:28:01 +0100297static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
298 struct msgb *msg, void *data, void *param)
299{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800300 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100301 int rc = 0;
302
303 switch (event) {
304 case GSM_SECURITY_AUTH_FAILED:
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100305 release_loc_updating_req(conn, 1);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100306 break;
307
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200308 case GSM_SECURITY_ALREADY:
309 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
310 "UPDATING after CM SERVICE REQUEST\n");
311 /* fall through */
312
Sylvain Munaut267fba02009-12-24 00:28:01 +0100313 case GSM_SECURITY_NOAVAIL:
314 case GSM_SECURITY_SUCCEEDED:
315 /* We're all good */
316 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800317 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800318 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100319 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800320 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100321 }
322
323 /* call subscr_update after putting the loc_upd_acc
324 * in the transmit queue, since S_SUBSCR_ATTACHED might
325 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800326 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100327 GSM_SUBSCRIBER_UPDATE_ATTACHED);
328
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100329 /*
330 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
331 * MS needs to respond with a TMSI REALLOCATION COMPLETE
332 * (even if the TMSI is the same).
333 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100334 break;
335
336 default:
337 rc = -EINVAL;
338 };
339
340 return rc;
341}
342
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100343static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000344{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100345 if (!conn->loc_operation)
346 return 0;
347
Sylvain Munaut267fba02009-12-24 00:28:01 +0100348 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800349 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100350 conn->loc_operation->key_seq,
351 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000352 return 0;
353}
354
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800355void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000356{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800357 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800358
359 /* avoid someone issuing a clear */
360 conn->in_release = 1;
361
Holger Freyther7c19f742009-06-06 13:54:35 +0000362 /*
363 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800364 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000365 */
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100366 release_loc_updating_req(conn, 1);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100367
368 /* We might need to cancel the paging response or such. */
369 if (conn->sec_operation && conn->sec_operation->cb) {
370 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
371 NULL, conn, conn->sec_operation->cb_data);
372 }
373
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800374 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800375 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000376
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100377 /*
378 * Free all transactions that are associated with the released
379 * connection. The transaction code will inform the CC or SMS
380 * facilities that will send the release indications. As part of
381 * the CC REL_IND the remote leg might be released and this will
382 * trigger the call to trans_free. This is something the llist
383 * macro can not handle and we will need to re-iterate the list.
384 *
385 * TODO: Move the trans_list into the subscriber connection and
386 * create a pending list for MT transactions. These exist before
387 * we have a subscriber connection.
388 */
389restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800390 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100391 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200392 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100393 goto restart;
394 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800395 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000396}
397
Harald Welte371efe52010-12-22 23:17:50 +0100398void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
399{
400 struct gsm_trans *trans, *temp;
401
402 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
403
404 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100405 if (trans->protocol == protocol) {
406 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100407 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100408 }
Harald Welte371efe52010-12-22 23:17:50 +0100409 }
410}
411
Holger Freyther429e7762008-12-30 13:28:30 +0000412/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200413int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000414{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800415 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800416 struct msgb *msg;
417
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200418 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800419
420 msg = gsm48_create_loc_upd_rej(cause);
421 if (!msg) {
422 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
423 return -1;
424 }
Harald Welte52b1f982008-12-23 20:25:15 +0000425
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800426 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000427
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100428 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100429 "LAC=%u BTS=%u\n", conn->subscr ?
430 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800431 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100432
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800433 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000434}
435
436/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200437int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000438{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800439 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000440 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000441 struct gsm48_hdr *gh;
442 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200443 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000444
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800445 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000446
447 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
448 gh->proto_discr = GSM48_PDISC_MM;
449 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
450
451 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100452 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000453 bts->network->network_code, bts->location_area_code);
454
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200455 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200456 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000457
458 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
459
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200460 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100461
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800462 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000463}
464
Harald Weltebf5e8df2009-02-03 12:59:45 +0000465/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200466static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000467{
468 struct msgb *msg = gsm48_msgb_alloc();
469 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000470
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800471 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000472
473 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
474 gh->proto_discr = GSM48_PDISC_MM;
475 gh->msg_type = GSM48_MT_MM_ID_REQ;
476 gh->data[0] = id_type;
477
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800478 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000479}
480
Harald Welte231ad4f2008-12-27 11:15:38 +0000481
Harald Weltebf5e8df2009-02-03 12:59:45 +0000482/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800483static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000484{
485 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000486 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200487 struct gsm_bts *bts = lchan->ts->trx->bts;
488 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200489 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200490 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000491
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200492 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000493 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000494 mi_type, mi_string);
495
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200496 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100497
Harald Welte75a983f2008-12-27 21:34:06 +0000498 switch (mi_type) {
499 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200500 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100501 if (!conn->subscr) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100502 conn->subscr = subscr_get_by_imsi(net->subscr_group,
503 mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100504 if (!conn->subscr && net->create_subscriber)
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100505 conn->subscr = subscr_create_subscriber(
506 net->subscr_group, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200507 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100508 if (!conn->subscr && conn->loc_operation) {
509 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
510 release_loc_updating_req(conn, 1);
511 return 0;
512 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100513 if (conn->loc_operation)
514 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000515 break;
516 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000517 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000518 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100519 if (conn->subscr) {
520 db_subscriber_assoc_imei(conn->subscr, mi_string);
521 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200522 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100523 if (conn->loc_operation)
524 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000525 break;
526 }
Holger Freyther73487a22008-12-31 18:53:57 +0000527
528 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100529 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000530}
531
Harald Welte255539c2008-12-28 02:26:27 +0000532
533static void loc_upd_rej_cb(void *data)
534{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100535 struct gsm_subscriber_connection *conn = data;
536 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100537 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000538
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100539 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800540 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100541 release_loc_updating_req(conn, 1);
Harald Welte255539c2008-12-28 02:26:27 +0000542}
543
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100544static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000545{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100546 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
547 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200548 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000549}
550
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200551static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000552{
553 switch (type) {
554 case GSM48_LUPD_NORMAL:
555 return "NORMAL";
556 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200557 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000558 case GSM48_LUPD_IMSI_ATT:
559 return "IMSI ATTACH";
560 default:
561 return "UNKNOWN";
562 }
563}
564
Harald Weltebf5e8df2009-02-03 12:59:45 +0000565/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800566static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000567{
Harald Welte8470bf22008-12-25 23:28:35 +0000568 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000569 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800570 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800571 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200572 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200573 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000574
Harald Welte8470bf22008-12-25 23:28:35 +0000575 lu = (struct gsm48_loc_upd_req *) gh->data;
576
577 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000578
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200579 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000580
Harald Weltea0368542009-06-27 02:58:43 +0200581 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000582 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000583
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200584 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100585
Harald Welte24ff6ee2009-12-22 00:41:05 +0100586 switch (lu->type) {
587 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200588 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100589 break;
590 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200591 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100592 break;
593 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200594 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100595 break;
596 }
597
Holger Freythereaf04692009-06-06 13:54:44 +0000598 /*
599 * Pseudo Spoof detection: Just drop a second/concurrent
600 * location updating request.
601 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100602 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200603 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100604 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800605 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000606 return 0;
607 }
608
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800609 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000610
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200611 conn->loc_operation->key_seq = lu->key_seq;
612
Harald Welte52b1f982008-12-23 20:25:15 +0000613 switch (mi_type) {
614 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200615 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000616 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200617 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100618 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000619
Jan Luebbe370b41d2009-08-12 10:19:34 +0200620 /* look up subscriber based on IMSI, create if not found */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100621 subscr = subscr_get_by_imsi(bts->network->subscr_group, mi_string);
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100622 if (!subscr && bts->network->create_subscriber) {
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100623 subscr = subscr_create_subscriber(
624 bts->network->subscr_group, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200625 }
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +0100626 if (!subscr) {
627 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
628 release_loc_updating_req(conn, 0);
629 return 0;
630 }
Harald Welte4b634542008-12-27 01:55:51 +0000631 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000632 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200633 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000634 /* look up the subscriber based on TMSI, request IMSI if it fails */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100635 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200636 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000637 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000638 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200639 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100640 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000641 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200642 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200643 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200644 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000645 break;
646 case GSM_MI_TYPE_IMEI:
647 case GSM_MI_TYPE_IMEISV:
648 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200649 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000650 break;
651 default:
Harald Weltea0368542009-06-27 02:58:43 +0200652 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000653 break;
654 }
655
Harald Welte24516ea2009-07-04 10:18:00 +0200656 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100657 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200658
Harald Welte4bfdfe72009-06-10 23:11:52 +0800659 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200660 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800661 /* FIXME: request id? close channel? */
662 return -EINVAL;
663 }
664
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100665 conn->subscr = subscr;
666 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000667
Harald Welte24516ea2009-07-04 10:18:00 +0200668 /* check if we can let the subscriber into our network immediately
669 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100670 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000671}
672
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100673/* Turn int into semi-octet representation: 98 => 0x89 */
674static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800675{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100676 uint8_t ret;
677
678 ret = value / 10;
679 ret |= (value % 10) << 4;
680
681 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800682}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100683
Harald Welte4bfdfe72009-06-10 23:11:52 +0800684
Harald Weltedb253af2008-12-30 17:56:55 +0000685/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800686int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000687{
688 struct msgb *msg = gsm48_msgb_alloc();
689 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800690 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100691 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200692 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200693 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100694
Harald Welte4bfdfe72009-06-10 23:11:52 +0800695 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100696 struct tm* gmt_time;
697 struct tm* local_time;
698 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100699 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000700
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800701 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000702
703 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
704 gh->proto_discr = GSM48_PDISC_MM;
705 gh->msg_type = GSM48_MT_MM_INFO;
706
707 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200708#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000709 name_len = strlen(net->name_long);
710 /* 10.5.3.5a */
711 ptr8 = msgb_put(msg, 3);
712 ptr8[0] = GSM48_IE_NAME_LONG;
713 ptr8[1] = name_len*2 +1;
714 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
715
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200716 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000717 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000718 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000719
720 /* FIXME: Use Cell Broadcast, not UCS-2, since
721 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200722#endif
723 name_len = (strlen(net->name_long)*7)/8;
724 name_pad = (8 - strlen(net->name_long)*7)%8;
725 if (name_pad > 0)
726 name_len++;
727 /* 10.5.3.5a */
728 ptr8 = msgb_put(msg, 3);
729 ptr8[0] = GSM48_IE_NAME_LONG;
730 ptr8[1] = name_len +1;
731 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
732
733 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100734 gsm_7bit_encode_n(ptr8, name_len, net->name_long, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200735
Harald Weltedb253af2008-12-30 17:56:55 +0000736 }
737
738 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200739#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000740 name_len = strlen(net->name_short);
741 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200742 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200743 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000744 ptr8[1] = name_len*2 + 1;
745 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
746
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200747 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000748 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000749 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200750#endif
751 name_len = (strlen(net->name_short)*7)/8;
752 name_pad = (8 - strlen(net->name_short)*7)%8;
753 if (name_pad > 0)
754 name_len++;
755 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200756 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200757 ptr8[0] = GSM48_IE_NAME_SHORT;
758 ptr8[1] = name_len +1;
759 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
760
761 ptr8 = msgb_put(msg, name_len);
Harald Welte5468f762015-01-01 12:41:17 +0100762 gsm_7bit_encode_n(ptr8, name_len, net->name_short, NULL);
Daniel Willmanneea93372009-08-13 03:42:07 +0200763
Harald Weltedb253af2008-12-30 17:56:55 +0000764 }
765
Harald Weltedb253af2008-12-30 17:56:55 +0000766 /* Section 10.5.3.9 */
767 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100768 gmt_time = gmtime(&cur_t);
769
Harald Weltedb253af2008-12-30 17:56:55 +0000770 ptr8 = msgb_put(msg, 8);
771 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100772 ptr8[1] = bcdify(gmt_time->tm_year % 100);
773 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
774 ptr8[3] = bcdify(gmt_time->tm_mday);
775 ptr8[4] = bcdify(gmt_time->tm_hour);
776 ptr8[5] = bcdify(gmt_time->tm_min);
777 ptr8[6] = bcdify(gmt_time->tm_sec);
778
Harald Welte45f91712012-07-08 16:48:11 +0200779 if (bts->tz.override) {
780 /* Convert tz.hr and tz.mn to units */
781 if (bts->tz.hr < 0) {
782 tzunits = ((bts->tz.hr/-1)*4);
783 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100784 ptr8[7] = bcdify(tzunits);
785 /* Set negative time */
786 ptr8[7] |= 0x08;
787 }
788 else {
Harald Welte45f91712012-07-08 16:48:11 +0200789 tzunits = bts->tz.hr*4;
790 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100791 ptr8[7] = bcdify(tzunits);
792 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100793 /* Convert DST value */
794 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
795 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100796 }
797 else {
798 /* Need to get GSM offset and convert into 15 min units */
799 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
800 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200801#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100802 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200803#else
804#warning find a portable way to obtain the timezone offset
805 tzunits = 0;
806#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100807 if (tzunits < 0) {
808 tzunits = tzunits/-1;
809 ptr8[7] = bcdify(tzunits);
810 /* Flip it to negative */
811 ptr8[7] |= 0x08;
812 }
813 else
814 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100815
816 /* Does not support DST +2 */
817 if (local_time->tm_isdst)
818 dst = 1;
819 }
820
821 if (dst) {
822 ptr8 = msgb_put(msg, 3);
823 ptr8[0] = GSM48_IE_NET_DST;
824 ptr8[1] = 1;
825 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100826 }
Harald Weltedb253af2008-12-30 17:56:55 +0000827
Daniel Willmanneea93372009-08-13 03:42:07 +0200828 DEBUGP(DMM, "-> MM INFO\n");
829
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800830 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000831}
832
Harald Welte7984d5c2009-08-12 22:56:50 +0200833/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200834int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200835{
836 struct msgb *msg = gsm48_msgb_alloc();
837 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200838 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200839
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200840 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200841
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800842 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200843 gh->proto_discr = GSM48_PDISC_MM;
844 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
845
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100846 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200847
Harald Welte7984d5c2009-08-12 22:56:50 +0200848 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200849 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200850 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200851
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800852 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200853}
854
855/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800856int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200857{
858 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800859 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200860}
861
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100862/*
863 * At the 30C3 phones miss their periodic update
864 * interval a lot and then remain unreachable. In case
865 * we still know the TMSI we can just attach it again.
866 */
867static void implit_attach(struct gsm_subscriber_connection *conn)
868{
869 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
870 return;
871
872 subscr_update(conn->subscr, conn->bts,
873 GSM_SUBSCRIBER_UPDATE_ATTACHED);
874}
875
876
Sylvain Munautba87f452009-12-24 00:28:46 +0100877static int _gsm48_rx_mm_serv_req_sec_cb(
878 unsigned int hooknum, unsigned int event,
879 struct msgb *msg, void *data, void *param)
880{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800881 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100882 int rc = 0;
883
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200884 /* auth failed or succeeded, the timer was stopped */
885 conn->expire_timer_stopped = 1;
886
Sylvain Munautba87f452009-12-24 00:28:46 +0100887 switch (event) {
888 case GSM_SECURITY_AUTH_FAILED:
889 /* Nothing to do */
890 break;
891
892 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200893 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800894 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100895 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100896 break;
897
898 case GSM_SECURITY_SUCCEEDED:
899 /* nothing to do. CIPHER MODE COMMAND is
900 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100901 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100902 break;
903
904 default:
905 rc = -EINVAL;
906 };
907
908 return rc;
909}
910
Harald Welte4ed0e922009-01-10 03:17:30 +0000911/*
912 * Handle CM Service Requests
913 * a) Verify that the packet is long enough to contain the information
914 * we require otherwsie reject with INCORRECT_MESSAGE
915 * b) Try to parse the TMSI. If we do not have one reject
916 * c) Check that we know the subscriber with the TMSI otherwise reject
917 * with a HLR cause
918 * d) Set the subscriber on the gsm_lchan and accept
919 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800920static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000921{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200922 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200923 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000924
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800925 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000926 struct gsm_subscriber *subscr;
927 struct gsm48_hdr *gh = msgb_l3(msg);
928 struct gsm48_service_request *req =
929 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800930 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200931 uint8_t classmark2_len = gh->data[1];
932 uint8_t *classmark2 = gh->data+2;
933 uint8_t mi_len = *(classmark2 + classmark2_len);
934 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000935
Harald Weltec9e02182009-05-01 19:07:53 +0000936 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000937 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000938 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800939 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000940 GSM48_REJECT_INCORRECT_MESSAGE);
941 }
942
943 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000944 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800945 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000946 GSM48_REJECT_INCORRECT_MESSAGE);
947 }
948
Harald Weltec9e02182009-05-01 19:07:53 +0000949 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000950 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000951 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800952 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000953 GSM48_REJECT_INCORRECT_MESSAGE);
954 }
955
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200956 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000957 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000958 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000959
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200960 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100961
Harald Welte3ac7f102009-08-10 10:12:45 +0200962 if (is_siemens_bts(bts))
963 send_siemens_mrpci(msg->lchan, classmark2-1);
964
Jacob Erlbeck1e30a282014-12-03 09:28:24 +0100965 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200966 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000967
Harald Welte2a139372009-02-22 21:14:55 +0000968 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000969 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800970 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000971 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
972
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800973 if (!conn->subscr)
974 conn->subscr = subscr;
975 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100976 subscr_put(subscr); /* lchan already has a ref, don't need another one */
977 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000978 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
979 subscr_put(subscr);
980 }
981
Harald Weltec2e302d2009-07-05 14:08:13 +0200982 subscr->equipment.classmark2_len = classmark2_len;
983 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
984 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000985
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200986 /* we will send a MM message soon */
987 conn->expire_timer_stopped = 1;
988
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800989 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100990 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000991}
992
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200993static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000994{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200995 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000996 struct gsm48_hdr *gh = msgb_l3(msg);
997 struct gsm48_imsi_detach_ind *idi =
998 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200999 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001000 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001001 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +00001002
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001003 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +01001004 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +00001005 mi_type, mi_string);
1006
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001007 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +01001008
Harald Welte2a139372009-02-22 21:14:55 +00001009 switch (mi_type) {
1010 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001011 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001012 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001013 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +00001014 break;
1015 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +01001016 DEBUGPC(DMM, "\n");
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001017 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1018 mi_string);
Harald Welte2a139372009-02-22 21:14:55 +00001019 break;
1020 case GSM_MI_TYPE_IMEI:
1021 case GSM_MI_TYPE_IMEISV:
1022 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +01001023 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001024 break;
1025 default:
Daniel Willmann977cd132012-12-28 19:29:44 +01001026 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +00001027 break;
1028 }
1029
Holger Freyther4a49e772009-04-12 05:37:29 +00001030 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001031 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +00001032 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +01001033 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +02001034
1035 subscr->equipment.classmark1 = idi->classmark1;
1036 db_sync_equipment(&subscr->equipment);
1037
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001038 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001039 } else
Harald Welte2a139372009-02-22 21:14:55 +00001040 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1041
Harald Welte31981a02009-12-20 09:58:40 +01001042 /* FIXME: iterate over all transactions and release them,
1043 * imagine an IMSI DETACH happening during an active call! */
1044
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001045 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001046 return 0;
1047}
1048
Harald Welted2a7f5a2009-06-05 20:08:20 +00001049static int gsm48_rx_mm_status(struct msgb *msg)
1050{
1051 struct gsm48_hdr *gh = msgb_l3(msg);
1052
1053 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1054
1055 return 0;
1056}
1057
Sylvain Munaut30a15382009-12-24 00:27:26 +01001058/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001059static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001060{
1061 struct gsm48_hdr *gh = msgb_l3(msg);
1062 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001063 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001064
Sylvain Munautc593cf12010-06-10 23:51:41 +02001065 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001066 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001067
1068 /* Safety check */
1069 if (!conn->sec_operation) {
1070 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1071 return -EIO;
1072 }
1073
1074 /* Validate SRES */
1075 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001076 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001077 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001078
1079 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001080 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001081
Sylvain Munaut30a15382009-12-24 00:27:26 +01001082 if (cb)
1083 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001084 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001085
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001086 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001087 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001088 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001089 }
1090
Sylvain Munautc593cf12010-06-10 23:51:41 +02001091 DEBUGPC(DMM, "OK\n");
1092
Sylvain Munaut30a15382009-12-24 00:27:26 +01001093 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001094 return gsm0808_cipher_mode(conn, net->a5_encryption,
1095 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001096}
1097
Harald Weltebf5e8df2009-02-03 12:59:45 +00001098/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001099static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001100{
1101 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001102 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001103
1104 switch (gh->msg_type & 0xbf) {
1105 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001106 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001107 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001108 break;
1109 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001110 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001111 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001112 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001113 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001114 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001115 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001116 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001117 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001118 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001119 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001120 conn->subscr ?
1121 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001122 "unknown subscriber");
Holger Hans Peter Freyther1ba07302015-01-27 10:27:53 +01001123 release_loc_updating_req(conn, 1);
Harald Welte69b2af22009-01-06 19:47:00 +00001124 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001125 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001126 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001127 break;
1128 case GSM48_MT_MM_CM_REEST_REQ:
1129 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1130 break;
1131 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001132 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001133 break;
1134 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001135 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001136 gh->msg_type);
1137 break;
1138 }
1139
1140 return rc;
1141}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001142
Harald Welte2d35ae62009-02-06 12:02:13 +00001143/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001144static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001145{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001146 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001147 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001148 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001149 uint8_t *classmark2_lv = gh->data + 1;
1150 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001151 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001152 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001153 int rc = 0;
1154
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001155 resp = (struct gsm48_pag_resp *) &gh->data[0];
1156 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1157 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001158 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1159 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001160
Harald Weltefe18d8f2009-02-22 21:14:24 +00001161 switch (mi_type) {
1162 case GSM_MI_TYPE_TMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001163 subscr = subscr_get_by_tmsi(bts->network->subscr_group,
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001164 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001165 break;
1166 case GSM_MI_TYPE_IMSI:
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01001167 subscr = subscr_get_by_imsi(bts->network->subscr_group,
1168 mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001169 break;
1170 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001171
1172 if (!subscr) {
1173 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001174 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001175 return -EINVAL;
1176 }
Holger Hans Peter Freytherb7ccac42014-12-28 15:00:36 +01001177 log_set_context(BSC_CTX_SUBSCR, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001178 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001179 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001180
Harald Weltec2e302d2009-07-05 14:08:13 +02001181 subscr->equipment.classmark2_len = *classmark2_lv;
1182 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1183 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001184
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001185 /* We received a paging */
1186 conn->expire_timer_stopped = 1;
1187
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001188 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001189 return rc;
1190}
1191
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001192static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001193{
1194 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001195 uint8_t apdu_id_flags;
1196 uint8_t apdu_len;
1197 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001198
1199 apdu_id_flags = gh->data[0];
1200 apdu_len = gh->data[1];
1201 apdu_data = gh->data+2;
1202
Harald Welte70f92052012-07-16 13:22:19 +02001203 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001204 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001205
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001206 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001207}
1208
Harald Weltebf5e8df2009-02-03 12:59:45 +00001209/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001210static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001211{
1212 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001213 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001214
1215 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001216 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001217 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001218 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001219 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001220 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001221 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001222 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001223 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001224 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001225 break;
1226 }
1227
Harald Welte2d35ae62009-02-06 12:02:13 +00001228 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001229}
1230
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001231int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1232 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001233{
1234 struct msgb *msg = gsm48_msgb_alloc();
1235 struct gsm48_hdr *gh;
1236
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001237 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001238
1239 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1240 apdu_id, apdu_len);
1241
1242 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1243 gh->proto_discr = GSM48_PDISC_RR;
1244 gh->msg_type = GSM48_MT_RR_APP_INFO;
1245 gh->data[0] = apdu_id;
1246 gh->data[1] = apdu_len;
1247 memcpy(gh->data+2, apdu, apdu_len);
1248
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001249 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001250}
1251
Harald Welte4bc90a12008-12-27 16:32:52 +00001252/* Call Control */
1253
Harald Welte7584aea2009-02-11 11:44:12 +00001254/* The entire call control code is written in accordance with Figure 7.10c
1255 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1256 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1257 * it for voice */
1258
Harald Welte4bfdfe72009-06-10 23:11:52 +08001259static void new_cc_state(struct gsm_trans *trans, int state)
1260{
1261 if (state > 31 || state < 0)
1262 return;
1263
1264 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001265 gsm48_cc_state_name(trans->cc.state),
1266 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001267
Harald Weltedcaf5652009-07-23 18:56:43 +02001268 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001269}
1270
1271static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001272{
1273 struct msgb *msg = gsm48_msgb_alloc();
1274 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001275 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001276
Harald Welte4bc90a12008-12-27 16:32:52 +00001277 gh->msg_type = GSM48_MT_CC_STATUS;
1278
1279 cause = msgb_put(msg, 3);
1280 cause[0] = 2;
1281 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1282 cause[2] = 0x80 | 30; /* response to status inquiry */
1283
1284 call_state = msgb_put(msg, 1);
1285 call_state[0] = 0xc0 | 0x00;
1286
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001287 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001288}
1289
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001290static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001291 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001292{
1293 struct msgb *msg = gsm48_msgb_alloc();
1294 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1295
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001296 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001297
Harald Welte6f4b7532008-12-29 00:39:37 +00001298 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001299 gh->msg_type = msg_type;
1300
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001301 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001302}
1303
Harald Welte4bfdfe72009-06-10 23:11:52 +08001304static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1305{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001306 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001307 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001308 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001309 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001310 }
1311}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001312
Harald Welte4bfdfe72009-06-10 23:11:52 +08001313static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1314 int msg_type, struct gsm_mncc *mncc)
1315{
1316 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001317 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318
1319 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001320 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001321 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001322 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001323 trans->conn->lchan->ts->trx->bts->nr,
1324 trans->conn->lchan->ts->trx->nr,
1325 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326 (trans->subscr)?(trans->subscr->extension):"-",
1327 get_mncc_name(msg_type));
1328 else
1329 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1330 "Sending '%s' to MNCC.\n",
1331 (trans->subscr)?(trans->subscr->extension):"-",
1332 get_mncc_name(msg_type));
1333 else
1334 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1335 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1336
1337 mncc->msg_type = msg_type;
1338
Harald Welte966636f2009-06-26 19:39:35 +02001339 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 if (!msg)
1341 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001342
1343 data = msgb_put(msg, sizeof(struct gsm_mncc));
1344 memcpy(data, mncc, sizeof(struct gsm_mncc));
1345
Harald Welte54209c22010-12-22 23:43:29 +01001346 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347
1348 return 0;
1349}
1350
1351int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001352 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353{
1354 struct gsm_mncc rel;
1355
Harald Welte92f70c52009-06-12 01:54:08 +08001356 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001358 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001359 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1360 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1362}
1363
Harald Weltedcaf5652009-07-23 18:56:43 +02001364/* Call Control Specific transaction release.
1365 * gets called by trans_free, DO NOT CALL YOURSELF! */
1366void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368 gsm48_stop_cc_timer(trans);
1369
1370 /* send release to L4, if callref still exists */
1371 if (trans->callref) {
1372 /* Ressource unavailable */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001373 mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001374 GSM48_CAUSE_LOC_PRN_S_LU,
1375 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001376 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001377 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001379 if (trans->conn)
1380 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381}
1382
1383static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001384
Harald Welte09e38af2009-02-16 22:52:23 +00001385/* call-back from paging the B-end of the connection */
1386static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001387 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001388{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001389 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001390 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001391 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001393
Harald Welte09e38af2009-02-16 22:52:23 +00001394 if (hooknum != GSM_HOOK_RR_PAGING)
1395 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001396
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001397 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001398 if (!net) {
1399 DEBUGP(DCC, "Error Network not set!\n");
1400 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001401 }
Harald Welte7584aea2009-02-11 11:44:12 +00001402
Harald Welte4bfdfe72009-06-10 23:11:52 +08001403 /* check all tranactions (without lchan) for subscriber */
1404 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001405 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 continue;
1407 switch (event) {
1408 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001409 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410 break;
1411 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001412 transt->subscr->extension);
1413 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001415 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001416 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001417 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001418 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001419 }
1420 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001421 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001422 break;
1423 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001424 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001425 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001426 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001427 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001428 found = 1;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001429 mncc_release_ind(transt->net, transt,
Harald Welte596fed42009-07-23 19:06:52 +02001430 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001431 GSM48_CAUSE_LOC_PRN_S_LU,
1432 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001433 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001434 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001435 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001436 break;
1437 }
1438 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001439
1440 talloc_free(paging_request);
1441
1442 /*
1443 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1444 * failure and then the subscr will be poke again. This needs a lot of fixing
1445 * in the subscriber queue code.
1446 */
1447 if (!found && conn)
1448 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001449 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001450}
Harald Welte7584aea2009-02-11 11:44:12 +00001451
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001452static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001453
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001454/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001455static int switch_for_handover(struct gsm_lchan *old_lchan,
1456 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001457{
1458 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001459
1460 if (ipacc_rtp_direct) {
1461 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1462 return 0;
1463 }
1464
1465 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001466 new_rs = new_lchan->abis_ip.rtp_socket;
1467 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001468
1469 if (!new_rs) {
1470 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1471 return -EIO;
1472 }
1473
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001474 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001475
1476 if (!old_rs) {
1477 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1478 return -EIO;
1479 }
1480
1481 /* copy rx_action and reference to other sock */
1482 new_rs->rx_action = old_rs->rx_action;
1483 new_rs->tx_action = old_rs->tx_action;
1484 new_rs->transmit = old_rs->transmit;
1485
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001486 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001487 case RTP_PROXY:
1488 other_rs = old_rs->proxy.other_sock;
1489 rtp_socket_proxy(new_rs, other_rs);
1490 /* delete reference to other end socket to prevent
1491 * rtp_socket_free() from removing the inverse reference */
1492 old_rs->proxy.other_sock = NULL;
1493 break;
1494 case RTP_RECV_UPSTREAM:
1495 new_rs->receive = old_rs->receive;
1496 break;
1497 case RTP_NONE:
1498 break;
1499 }
1500
1501 return 0;
1502}
1503
Harald Welte805f6442009-07-28 18:25:29 +02001504/* some other part of the code sends us a signal */
1505static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1506 void *handler_data, void *signal_data)
1507{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001508 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001509 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001510 struct gsm_network *net;
1511 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001512
1513 if (subsys != SS_ABISIP)
1514 return 0;
1515
1516 /* in case we use direct BTS-to-BTS RTP */
1517 if (ipacc_rtp_direct)
1518 return 0;
1519
Harald Welte805f6442009-07-28 18:25:29 +02001520 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001521 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001522 /* in case we don't use direct BTS-to-BTS RTP */
1523 /* the BTS has successfully bound a TCH to a local ip/port,
1524 * which means we can connect our UDP socket to it */
1525 if (lchan->abis_ip.rtp_socket) {
1526 rtp_socket_free(lchan->abis_ip.rtp_socket);
1527 lchan->abis_ip.rtp_socket = NULL;
1528 }
1529
1530 lchan->abis_ip.rtp_socket = rtp_socket_create();
1531 if (!lchan->abis_ip.rtp_socket)
1532 return -EIO;
1533
1534 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1535 lchan->abis_ip.bound_ip,
1536 lchan->abis_ip.bound_port);
1537 if (rc < 0)
1538 return -EIO;
1539
Harald Welteda7ab742009-12-19 22:23:05 +01001540 /* check if any transactions on this lchan still have
1541 * a tch_recv_mncc request pending */
1542 net = lchan->ts->trx->bts->network;
1543 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001544 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001545 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1546 tch_recv_mncc(net, trans->callref, 1);
1547 }
1548 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001549
1550 /*
1551 * TODO: this appears to be too early? Why not until after
1552 * the handover detect or the handover complete?
1553 *
1554 * Do we have a handover pending for this new lchan? In that
1555 * case re-route the audio from the old channel to the new one.
1556 */
1557 old_lchan = bsc_handover_pending(lchan);
1558 if (old_lchan)
1559 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001560 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001561 case S_ABISIP_DLCX_IND:
1562 /* the BTS tells us a RTP stream has been disconnected */
1563 if (lchan->abis_ip.rtp_socket) {
1564 rtp_socket_free(lchan->abis_ip.rtp_socket);
1565 lchan->abis_ip.rtp_socket = NULL;
1566 }
1567
1568 break;
Harald Welte805f6442009-07-28 18:25:29 +02001569 }
1570
1571 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001572}
1573
Harald Welte49f48b82009-02-17 15:29:33 +00001574/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001575static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001576{
Harald Welte11fa29c2009-02-19 17:24:39 +00001577 struct gsm_bts *bts = lchan->ts->trx->bts;
1578 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001579 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001580
Harald Welte11fa29c2009-02-19 17:24:39 +00001581 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1582 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1583 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1584
1585 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001586 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001587 return -EINVAL;
1588 }
Harald Welteda7ab742009-12-19 22:23:05 +01001589
1590 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001591 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001592 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001593 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001594 if (!ipacc_rtp_direct) {
Andreas Eversberga4d0e3c2014-01-22 08:57:18 +01001595 if (!lchan->abis_ip.rtp_socket) {
1596 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1597 "lchan\n");
1598 return -EIO;
1599 }
1600 if (!remote_lchan->abis_ip.rtp_socket) {
1601 LOGP(DHO, LOGL_ERROR, "no RTP socket for "
1602 "remote_lchan\n");
1603 return -EIO;
1604 }
1605
Harald Welte805f6442009-07-28 18:25:29 +02001606 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001607 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001608 if (rc < 0)
1609 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001610 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001611 if (rc < 0)
1612 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001613 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301614 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1615 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001616 } else {
1617 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301618 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1619 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301620 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001621 if (rc < 0)
1622 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301623 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1624 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301625 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001626 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001627 break;
1628 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001629 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001630 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001631 trau_mux_map_lchan(lchan, remote_lchan);
1632 break;
1633 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001634 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001635 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001636 }
Harald Welte49f48b82009-02-17 15:29:33 +00001637
1638 return 0;
1639}
1640
Harald Welte4bfdfe72009-06-10 23:11:52 +08001641/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001642static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001643{
Harald Weltedcaf5652009-07-23 18:56:43 +02001644 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1645 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001646
Harald Welte4bfdfe72009-06-10 23:11:52 +08001647 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001648 return -EIO;
1649
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001650 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 return -EIO;
1652
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001653 /* Which subscriber do we want to track trans1 or trans2? */
1654 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1655
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001657 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001658}
1659
Harald Welteda7ab742009-12-19 22:23:05 +01001660/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001661static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662{
1663 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001664 struct gsm_lchan *lchan;
1665 struct gsm_bts *bts;
1666 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001667
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001669 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 if (!trans)
1671 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001672 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001673 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001674
1675 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001676 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001677 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001679 /* store receive state */
1680 trans->tch_recv = enable;
1681
Harald Welteda7ab742009-12-19 22:23:05 +01001682 switch (bts->type) {
1683 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001684 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001685 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001686 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001687 return -EINVAL;
1688 }
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001689 /* In case, we don't have a RTP socket to the BTS yet, the BTS
1690 * will not be connected to our RTP proxy and the socket will
1691 * not be assigned to the application interface. This method
1692 * will be called again, once the audio socket is created and
1693 * connected. */
Harald Welteda7ab742009-12-19 22:23:05 +01001694 if (!lchan->abis_ip.rtp_socket) {
Harald Welteda7ab742009-12-19 22:23:05 +01001695 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1696 return 0;
1697 }
1698 if (enable) {
1699 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001700 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001701 if (rc < 0)
1702 return rc;
1703 /* assign socket to application interface */
1704 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1705 net, callref);
1706 } else
1707 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1708 net, 0);
1709 break;
1710 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001711 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001712 case GSM_BTS_TYPE_NOKIA_SITE:
Andreas Eversbergdcf38e12013-12-05 14:37:11 +01001713 /* In case we don't have a TCH with correct mode, the TRAU muxer
1714 * will not be asigned to the application interface. This is
1715 * performed by switch_trau_mux() after successful handover or
1716 * assignment. */
1717 if (lchan->tch_mode == GSM48_CMODE_SIGN) {
1718 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1719 return 0;
1720 }
Harald Welteda7ab742009-12-19 22:23:05 +01001721 if (enable)
1722 return trau_recv_lchan(lchan, callref);
1723 return trau_mux_unmap(NULL, callref);
1724 break;
1725 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001726 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001727 return -EINVAL;
1728 }
1729
1730 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731}
1732
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1734{
1735 DEBUGP(DCC, "-> STATUS ENQ\n");
1736 return gsm48_cc_tx_status(trans, msg);
1737}
1738
1739static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1740static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1741
1742static void gsm48_cc_timeout(void *arg)
1743{
1744 struct gsm_trans *trans = arg;
1745 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001746 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1747 int mo_location = GSM48_CAUSE_LOC_USER;
1748 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1749 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750 struct gsm_mncc mo_rel, l4_rel;
1751
1752 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1753 mo_rel.callref = trans->callref;
1754 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1755 l4_rel.callref = trans->callref;
1756
Harald Weltedcaf5652009-07-23 18:56:43 +02001757 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758 case 0x303:
1759 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001760 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 break;
1762 case 0x310:
1763 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001764 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001765 break;
1766 case 0x313:
1767 disconnect = 1;
1768 /* unknown, did not find it in the specs */
1769 break;
1770 case 0x301:
1771 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001772 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 break;
1774 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001775 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001777 gsm48_cc_tx_release(trans, &trans->cc.msg);
1778 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 break; /* stay in release state */
1780 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001781 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 return;
1783// release = 1;
1784// l4_cause = 14;
1785// break;
1786 case 0x306:
1787 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001788 mo_cause = trans->cc.msg.cause.value;
1789 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 break;
1791 case 0x323:
1792 disconnect = 1;
1793 break;
1794 default:
1795 release = 1;
1796 }
1797
1798 if (release && trans->callref) {
1799 /* process release towards layer 4 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001800 mncc_release_ind(trans->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 l4_location, l4_cause);
1802 trans->callref = 0;
1803 }
1804
1805 if (disconnect && trans->callref) {
1806 /* process disconnect towards layer 4 */
1807 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001808 mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 }
1810
1811 /* process disconnect towards mobile station */
1812 if (disconnect || release) {
1813 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001814 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1815 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1816 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 mo_rel.cause.diag_len = 3;
1818
1819 if (disconnect)
1820 gsm48_cc_tx_disconnect(trans, &mo_rel);
1821 if (release)
1822 gsm48_cc_tx_release(trans, &mo_rel);
1823 }
1824
1825}
1826
1827static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1828 int sec, int micro)
1829{
1830 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001831 trans->cc.timer.cb = gsm48_cc_timeout;
1832 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001833 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001834 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835}
1836
1837static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1838{
1839 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001840 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1842 struct tlv_parsed tp;
1843 struct gsm_mncc setup;
1844
1845 memset(&setup, 0, sizeof(struct gsm_mncc));
1846 setup.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01001847 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001848 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 /* emergency setup is identified by msg_type */
1850 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1851 setup.emergency = 1;
1852
1853 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001854 setup.fields |= MNCC_F_CALLING;
1855 strncpy(setup.calling.number, trans->subscr->extension,
1856 sizeof(setup.calling.number)-1);
1857 strncpy(setup.imsi, trans->subscr->imsi,
1858 sizeof(setup.imsi)-1);
1859
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860 /* bearer capability */
1861 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1862 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001863 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01001865 apply_codec_restrictions(trans->conn->bts, &setup.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 }
1867 /* facility */
1868 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1869 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001870 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1872 }
1873 /* called party bcd number */
1874 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1875 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001876 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001877 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1878 }
1879 /* user-user */
1880 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1881 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001882 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1884 }
1885 /* ss-version */
1886 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1887 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001888 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1890 }
1891 /* CLIR suppression */
1892 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1893 setup.clir.sup = 1;
1894 /* CLIR invocation */
1895 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1896 setup.clir.inv = 1;
1897 /* cc cap */
1898 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1899 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001900 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1902 }
1903
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904 new_cc_state(trans, GSM_CSTATE_INITIATED);
1905
Harald Welte (local)47df3992009-12-26 19:45:03 +01001906 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1907 subscr_name(trans->subscr), trans->subscr->extension,
1908 setup.called.number);
1909
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001910 osmo_counter_inc(trans->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001911
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912 /* indicate setup to MNCC */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001913 mncc_recvmsg(trans->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914
Harald Welte13cac662009-07-29 12:10:35 +02001915 /* MNCC code will modify the channel asynchronously, we should
1916 * ipaccess-bind only after the modification has been made to the
1917 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918 return 0;
1919}
1920
1921static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001922{
1923 struct msgb *msg = gsm48_msgb_alloc();
1924 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001926 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001927
Harald Welte7ccf7782009-02-17 01:43:01 +00001928 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001929
Harald Welte4bfdfe72009-06-10 23:11:52 +08001930 /* transaction id must not be assigned */
1931 if (trans->transaction_id != 0xff) { /* unasssigned */
1932 DEBUGP(DCC, "TX Setup with assigned transaction. "
1933 "This is not allowed!\n");
1934 /* Temporarily out of order */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001935 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001936 GSM48_CAUSE_LOC_PRN_S_LU,
1937 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001938 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001939 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 return rc;
1941 }
1942
1943 /* Get free transaction_id */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01001944 trans_id = trans_assign_trans_id(trans->net, trans->subscr,
1945 GSM48_PDISC_CC, 0);
Harald Welte78283ef2009-07-23 21:36:44 +02001946 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947 /* no free transaction ID */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001948 rc = mncc_release_ind(trans->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001949 GSM48_CAUSE_LOC_PRN_S_LU,
1950 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001952 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 return rc;
1954 }
Harald Welte78283ef2009-07-23 21:36:44 +02001955 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001956
Harald Welte65e74cc2008-12-29 01:55:35 +00001957 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001958
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001960
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 /* bearer capability */
1962 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001963 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964 /* facility */
1965 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001966 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967 /* progress */
1968 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 /* calling party BCD number */
1971 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 /* called party BCD number */
1974 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 /* user-user */
1977 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 /* redirecting party BCD number */
1980 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001981 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982 /* signal */
1983 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001984 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001985
1986 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001987
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01001988 osmo_counter_inc(trans->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001989
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001990 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001991}
1992
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1994{
1995 struct gsm48_hdr *gh = msgb_l3(msg);
1996 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1997 struct tlv_parsed tp;
1998 struct gsm_mncc call_conf;
1999
2000 gsm48_stop_cc_timer(trans);
2001 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
2002
2003 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2004 call_conf.callref = trans->callref;
Andreas Eversbergcaae10b2014-01-16 16:04:12 +01002005 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002006 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007#if 0
2008 /* repeat */
2009 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2010 call_conf.repeat = 1;
2011 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2012 call_conf.repeat = 2;
2013#endif
2014 /* bearer capability */
2015 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2016 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002017 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002019 apply_codec_restrictions(trans->conn->bts, &call_conf.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 }
2021 /* cause */
2022 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2023 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2026 }
2027 /* cc cap */
2028 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2029 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2032 }
2033
Andreas Eversberg035b8742013-09-19 09:27:06 +02002034 /* IMSI of called subscriber */
2035 strncpy(call_conf.imsi, trans->subscr->imsi,
2036 sizeof(call_conf.imsi)-1);
2037
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2039
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002040 return mncc_recvmsg(trans->net, trans, MNCC_CALL_CONF_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002041 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042}
2043
2044static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2045{
2046 struct gsm_mncc *proceeding = arg;
2047 struct msgb *msg = gsm48_msgb_alloc();
2048 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2049
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2051
2052 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2053
2054 /* bearer capability */
2055 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002056 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057 /* facility */
2058 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002059 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 /* progress */
2061 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002062 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002064 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065}
2066
2067static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2068{
2069 struct gsm48_hdr *gh = msgb_l3(msg);
2070 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2071 struct tlv_parsed tp;
2072 struct gsm_mncc alerting;
2073
2074 gsm48_stop_cc_timer(trans);
2075 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2076
2077 memset(&alerting, 0, sizeof(struct gsm_mncc));
2078 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002079 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 /* facility */
2081 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2082 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002083 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2085 }
2086
2087 /* progress */
2088 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2089 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2092 }
2093 /* ss-version */
2094 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2095 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2098 }
2099
2100 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2101
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002102 return mncc_recvmsg(trans->net, trans, MNCC_ALERT_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002103 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104}
2105
2106static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2107{
2108 struct gsm_mncc *alerting = arg;
2109 struct msgb *msg = gsm48_msgb_alloc();
2110 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2111
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 gh->msg_type = GSM48_MT_CC_ALERTING;
2113
2114 /* facility */
2115 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002116 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 /* progress */
2118 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002119 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 /* user-user */
2121 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123
2124 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2125
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002126 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127}
2128
2129static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2130{
2131 struct gsm_mncc *progress = arg;
2132 struct msgb *msg = gsm48_msgb_alloc();
2133 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2134
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 gh->msg_type = GSM48_MT_CC_PROGRESS;
2136
2137 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002138 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139 /* user-user */
2140 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002141 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002142
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002143 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144}
2145
2146static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2147{
2148 struct gsm_mncc *connect = arg;
2149 struct msgb *msg = gsm48_msgb_alloc();
2150 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2151
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 gh->msg_type = GSM48_MT_CC_CONNECT;
2153
2154 gsm48_stop_cc_timer(trans);
2155 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2156
2157 /* facility */
2158 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002159 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002160 /* progress */
2161 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002162 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002163 /* connected number */
2164 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002165 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 /* user-user */
2167 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002168 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002169
2170 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2171
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002172 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002173}
2174
2175static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2176{
2177 struct gsm48_hdr *gh = msgb_l3(msg);
2178 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2179 struct tlv_parsed tp;
2180 struct gsm_mncc connect;
2181
2182 gsm48_stop_cc_timer(trans);
2183
2184 memset(&connect, 0, sizeof(struct gsm_mncc));
2185 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002186 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002188 connect.fields |= MNCC_F_CONNECTED;
2189 strncpy(connect.connected.number, trans->subscr->extension,
2190 sizeof(connect.connected.number)-1);
2191 strncpy(connect.imsi, trans->subscr->imsi,
2192 sizeof(connect.imsi)-1);
2193
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 /* facility */
2195 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2196 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002197 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002198 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2199 }
2200 /* user-user */
2201 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2202 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002203 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2205 }
2206 /* ss-version */
2207 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2208 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002209 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2211 }
2212
2213 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002214 osmo_counter_inc(trans->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002216 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217}
2218
2219
2220static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2221{
2222 struct gsm_mncc connect_ack;
2223
2224 gsm48_stop_cc_timer(trans);
2225
2226 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002227 osmo_counter_inc(trans->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228
2229 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2230 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002231
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002232 return mncc_recvmsg(trans->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 &connect_ack);
2234}
2235
2236static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2237{
2238 struct msgb *msg = gsm48_msgb_alloc();
2239 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2240
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2242
2243 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2244
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002245 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246}
2247
2248static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2249{
2250 struct gsm48_hdr *gh = msgb_l3(msg);
2251 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2252 struct tlv_parsed tp;
2253 struct gsm_mncc disc;
2254
2255 gsm48_stop_cc_timer(trans);
2256
2257 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2258
2259 memset(&disc, 0, sizeof(struct gsm_mncc));
2260 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002261 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 /* cause */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2264 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2267 }
2268 /* facility */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2270 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002271 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2273 }
2274 /* user-user */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2276 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2279 }
2280 /* ss-version */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2282 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002283 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2285 }
2286
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002287 return mncc_recvmsg(trans->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288
2289}
2290
Harald Weltec66b71c2009-06-11 14:23:20 +08002291static struct gsm_mncc_cause default_cause = {
2292 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2293 .coding = 0,
2294 .rec = 0,
2295 .rec_val = 0,
2296 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2297 .diag_len = 0,
2298 .diag = { 0 },
2299};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300
2301static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2302{
2303 struct gsm_mncc *disc = arg;
2304 struct msgb *msg = gsm48_msgb_alloc();
2305 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2306
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2308
2309 gsm48_stop_cc_timer(trans);
2310 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2311
2312 /* cause */
2313 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002314 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 else
Harald Welte55c8f352010-03-07 23:40:35 +01002316 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317
2318 /* facility */
2319 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002320 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 /* progress */
2322 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* user-user */
2325 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002326 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327
2328 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002329 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330
2331 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2332
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002333 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334}
2335
2336static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2337{
2338 struct gsm48_hdr *gh = msgb_l3(msg);
2339 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2340 struct tlv_parsed tp;
2341 struct gsm_mncc rel;
2342 int rc;
2343
2344 gsm48_stop_cc_timer(trans);
2345
2346 memset(&rel, 0, sizeof(struct gsm_mncc));
2347 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002348 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349 /* cause */
2350 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2351 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002352 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2354 }
2355 /* facility */
2356 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2357 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002358 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2360 }
2361 /* user-user */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2363 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002364 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2366 }
2367 /* ss-version */
2368 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2369 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002370 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2372 }
2373
Harald Weltedcaf5652009-07-23 18:56:43 +02002374 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 /* release collision 5.4.5 */
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002376 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002378 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002379 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002380 GSM48_MT_CC_RELEASE_COMPL);
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002381 rc = mncc_recvmsg(trans->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 }
2383
2384 new_cc_state(trans, GSM_CSTATE_NULL);
2385
2386 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002387 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388
2389 return rc;
2390}
2391
2392static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2393{
2394 struct gsm_mncc *rel = arg;
2395 struct msgb *msg = gsm48_msgb_alloc();
2396 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2397
Harald Welte4bfdfe72009-06-10 23:11:52 +08002398 gh->msg_type = GSM48_MT_CC_RELEASE;
2399
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 gsm48_stop_cc_timer(trans);
2401 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2402
2403 /* cause */
2404 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 /* facility */
2407 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002408 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409 /* user-user */
2410 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412
Harald Weltedcaf5652009-07-23 18:56:43 +02002413 trans->cc.T308_second = 0;
2414 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415
Harald Weltedcaf5652009-07-23 18:56:43 +02002416 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2418
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002419 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420}
2421
2422static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2423{
2424 struct gsm48_hdr *gh = msgb_l3(msg);
2425 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2426 struct tlv_parsed tp;
2427 struct gsm_mncc rel;
2428 int rc = 0;
2429
2430 gsm48_stop_cc_timer(trans);
2431
2432 memset(&rel, 0, sizeof(struct gsm_mncc));
2433 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002434 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 /* cause */
2436 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2437 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2440 }
2441 /* facility */
2442 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2443 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002444 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2446 }
2447 /* user-user */
2448 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2449 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002450 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2452 }
2453 /* ss-version */
2454 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2455 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2458 }
2459
2460 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002461 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002462 case GSM_CSTATE_CALL_PRESENT:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002463 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464 MNCC_REJ_IND, &rel);
2465 break;
2466 case GSM_CSTATE_RELEASE_REQ:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002467 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002468 MNCC_REL_CNF, &rel);
2469 break;
2470 default:
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002471 rc = mncc_recvmsg(trans->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472 MNCC_REL_IND, &rel);
2473 }
2474 }
2475
2476 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002477 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002478
2479 return rc;
2480}
2481
2482static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2483{
2484 struct gsm_mncc *rel = arg;
2485 struct msgb *msg = gsm48_msgb_alloc();
2486 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002487 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2490
2491 trans->callref = 0;
2492
2493 gsm48_stop_cc_timer(trans);
2494
2495 /* cause */
2496 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002497 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498 /* facility */
2499 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002500 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501 /* user-user */
2502 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002503 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504
Harald Weltec7782de2010-12-21 19:31:41 +01002505 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2506
Harald Weltedcaf5652009-07-23 18:56:43 +02002507 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508
Harald Weltec7782de2010-12-21 19:31:41 +01002509 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510}
2511
2512static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2513{
2514 struct gsm48_hdr *gh = msgb_l3(msg);
2515 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2516 struct tlv_parsed tp;
2517 struct gsm_mncc fac;
2518
2519 memset(&fac, 0, sizeof(struct gsm_mncc));
2520 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002521 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 /* facility */
2523 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2524 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002525 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002526 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2527 }
2528 /* ss-version */
2529 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2530 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002531 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2533 }
2534
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002535 return mncc_recvmsg(trans->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2539{
2540 struct gsm_mncc *fac = arg;
2541 struct msgb *msg = gsm48_msgb_alloc();
2542 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2543
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544 gh->msg_type = GSM48_MT_CC_FACILITY;
2545
2546 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002547 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002549 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550}
2551
2552static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2553{
2554 struct gsm_mncc hold;
2555
2556 memset(&hold, 0, sizeof(struct gsm_mncc));
2557 hold.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002558 return mncc_recvmsg(trans->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2562{
2563 struct msgb *msg = gsm48_msgb_alloc();
2564 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2565
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2567
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002568 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569}
2570
2571static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2572{
2573 struct gsm_mncc *hold_rej = arg;
2574 struct msgb *msg = gsm48_msgb_alloc();
2575 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2576
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2578
2579 /* cause */
2580 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002581 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582 else
Harald Welte55c8f352010-03-07 23:40:35 +01002583 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002585 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586}
2587
2588static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2589{
2590 struct gsm_mncc retrieve;
2591
2592 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2593 retrieve.callref = trans->callref;
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002594 return mncc_recvmsg(trans->net, trans, MNCC_RETRIEVE_IND,
Harald Welte596fed42009-07-23 19:06:52 +02002595 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596}
2597
2598static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2599{
2600 struct msgb *msg = gsm48_msgb_alloc();
2601 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2602
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2604
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002605 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606}
2607
2608static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2609{
2610 struct gsm_mncc *retrieve_rej = arg;
2611 struct msgb *msg = gsm48_msgb_alloc();
2612 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2613
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2615
2616 /* cause */
2617 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 else
Harald Welte55c8f352010-03-07 23:40:35 +01002620 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002622 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623}
2624
2625static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2626{
2627 struct gsm48_hdr *gh = msgb_l3(msg);
2628 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2629 struct tlv_parsed tp;
2630 struct gsm_mncc dtmf;
2631
2632 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2633 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002634 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 /* keypad facility */
2636 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2637 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002638 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2640 }
2641
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002642 return mncc_recvmsg(trans->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643}
2644
2645static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2646{
2647 struct gsm_mncc *dtmf = arg;
2648 struct msgb *msg = gsm48_msgb_alloc();
2649 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2650
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2652
2653 /* keypad */
2654 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002655 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002657 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002658}
2659
2660static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2661{
2662 struct gsm_mncc *dtmf = arg;
2663 struct msgb *msg = gsm48_msgb_alloc();
2664 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2665
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2667
2668 /* cause */
2669 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002670 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 else
Harald Welte55c8f352010-03-07 23:40:35 +01002672 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002674 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675}
2676
2677static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2678{
2679 struct msgb *msg = gsm48_msgb_alloc();
2680 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2681
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2683
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002684 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685}
2686
2687static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2688{
2689 struct gsm_mncc dtmf;
2690
2691 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2692 dtmf.callref = trans->callref;
2693
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002694 return mncc_recvmsg(trans->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695}
2696
2697static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2698{
2699 struct gsm48_hdr *gh = msgb_l3(msg);
2700 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2701 struct tlv_parsed tp;
2702 struct gsm_mncc modify;
2703
2704 memset(&modify, 0, sizeof(struct gsm_mncc));
2705 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002706 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 /* bearer capability */
2708 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2709 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002710 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002712 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713 }
2714
2715 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2716
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002717 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718}
2719
2720static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2721{
2722 struct gsm_mncc *modify = arg;
2723 struct msgb *msg = gsm48_msgb_alloc();
2724 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2725
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726 gh->msg_type = GSM48_MT_CC_MODIFY;
2727
2728 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2729
2730 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002731 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732
2733 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2734
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002735 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002736}
2737
2738static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2739{
2740 struct gsm48_hdr *gh = msgb_l3(msg);
2741 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2742 struct tlv_parsed tp;
2743 struct gsm_mncc modify;
2744
2745 gsm48_stop_cc_timer(trans);
2746
2747 memset(&modify, 0, sizeof(struct gsm_mncc));
2748 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002749 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750 /* bearer capability */
2751 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2752 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002753 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002755 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002756 }
2757
2758 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2759
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002760 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761}
2762
2763static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2764{
2765 struct gsm_mncc *modify = arg;
2766 struct msgb *msg = gsm48_msgb_alloc();
2767 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2768
Harald Welte4bfdfe72009-06-10 23:11:52 +08002769 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2770
2771 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002772 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773
2774 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2775
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002776 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777}
2778
2779static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2780{
2781 struct gsm48_hdr *gh = msgb_l3(msg);
2782 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2783 struct tlv_parsed tp;
2784 struct gsm_mncc modify;
2785
2786 gsm48_stop_cc_timer(trans);
2787
2788 memset(&modify, 0, sizeof(struct gsm_mncc));
2789 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002790 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791 /* bearer capability */
2792 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2793 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002794 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
Andreas Eversberga83d5112013-12-07 18:32:28 +01002796 apply_codec_restrictions(trans->conn->bts, &modify.bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797 }
2798 /* cause */
2799 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2800 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002801 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2803 }
2804
2805 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2806
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002807 return mncc_recvmsg(trans->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808}
2809
2810static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2811{
2812 struct gsm_mncc *modify = arg;
2813 struct msgb *msg = gsm48_msgb_alloc();
2814 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2815
Harald Welte4bfdfe72009-06-10 23:11:52 +08002816 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2817
2818 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002819 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002820 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002821 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002822
2823 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2824
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002825 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826}
2827
2828static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2829{
2830 struct gsm_mncc *notify = arg;
2831 struct msgb *msg = gsm48_msgb_alloc();
2832 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2833
Harald Welte4bfdfe72009-06-10 23:11:52 +08002834 gh->msg_type = GSM48_MT_CC_NOTIFY;
2835
2836 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002837 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002839 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002840}
2841
2842static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2843{
2844 struct gsm48_hdr *gh = msgb_l3(msg);
2845 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2846// struct tlv_parsed tp;
2847 struct gsm_mncc notify;
2848
2849 memset(&notify, 0, sizeof(struct gsm_mncc));
2850 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002851// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002852 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002853 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002854
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002855 return mncc_recvmsg(trans->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002856}
2857
2858static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2859{
2860 struct gsm_mncc *user = arg;
2861 struct msgb *msg = gsm48_msgb_alloc();
2862 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2863
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 gh->msg_type = GSM48_MT_CC_USER_INFO;
2865
2866 /* user-user */
2867 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002868 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869 /* more data */
2870 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002871 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002873 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874}
2875
2876static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2877{
2878 struct gsm48_hdr *gh = msgb_l3(msg);
2879 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2880 struct tlv_parsed tp;
2881 struct gsm_mncc user;
2882
2883 memset(&user, 0, sizeof(struct gsm_mncc));
2884 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002885 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002886 /* user-user */
2887 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2888 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002889 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002890 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2891 }
2892 /* more data */
2893 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2894 user.more = 1;
2895
Jacob Erlbeckf07c6052014-12-02 11:58:00 +01002896 return mncc_recvmsg(trans->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897}
2898
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002899static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900{
2901 struct gsm_mncc *mode = arg;
2902
Andreas Eversberg72c0dbd2013-03-31 11:50:35 +02002903 return gsm0808_assign_req(trans->conn, mode->lchan_mode,
2904 trans->conn->lchan->type != GSM_LCHAN_TCH_H);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905}
2906
2907static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002908 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 int type;
2910 int (*rout) (struct gsm_trans *trans, void *arg);
2911} downstatelist[] = {
2912 /* mobile originating call establishment */
2913 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2914 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2915 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2916 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2917 {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 */
2918 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2919 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2920 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2921 /* mobile terminating call establishment */
2922 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2923 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2924 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2925 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2926 /* signalling during call */
2927 {SBIT(GSM_CSTATE_ACTIVE),
2928 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2929 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2930 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2931 {ALL_STATES,
2932 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2933 {ALL_STATES,
2934 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2935 {ALL_STATES,
2936 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2937 {SBIT(GSM_CSTATE_ACTIVE),
2938 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2939 {SBIT(GSM_CSTATE_ACTIVE),
2940 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2941 {SBIT(GSM_CSTATE_ACTIVE),
2942 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2943 {SBIT(GSM_CSTATE_ACTIVE),
2944 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2945 {SBIT(GSM_CSTATE_ACTIVE),
2946 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2947 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2948 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2949 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2950 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2951 {SBIT(GSM_CSTATE_ACTIVE),
2952 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2953 /* clearing */
2954 {SBIT(GSM_CSTATE_INITIATED),
2955 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2956 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2957 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2958 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2959 MNCC_REL_REQ, gsm48_cc_tx_release},
2960 /* special */
2961 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002962 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963};
2964
2965#define DOWNSLLEN \
2966 (sizeof(downstatelist) / sizeof(struct downstate))
2967
2968
Harald Welte76556372010-12-22 23:57:45 +01002969int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970{
Harald Welte1a6f7982009-08-09 18:52:33 +02002971 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002972 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002973 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002974 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975 struct gsm_mncc *data = arg, rel;
2976
Harald Welte04dc88f2010-12-22 21:45:05 +01002977 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2978
Harald Welte4bfdfe72009-06-10 23:11:52 +08002979 /* handle special messages */
2980 switch(msg_type) {
2981 case MNCC_BRIDGE:
2982 return tch_bridge(net, arg);
2983 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002984 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002985 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002986 return tch_recv_mncc(net, data->callref, 1);
2987 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002988 case GSM_TCHF_FRAME_EFR:
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01002989 case GSM_TCHH_FRAME:
Andreas Eversbergd8967f72012-03-08 14:39:19 +01002990 case GSM_TCH_FRAME_AMR:
Harald Welteda7ab742009-12-19 22:23:05 +01002991 /* Find callref */
2992 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002993 if (!trans) {
2994 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002995 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002996 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002997 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01002998 if (!trans->conn) {
2999 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003000 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003001 }
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003002 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F
3003 && trans->conn->lchan->type != GSM_LCHAN_TCH_H) {
Harald Welte04dc88f2010-12-22 21:45:05 +01003004 /* This should be LOGL_ERROR or NOTICE, but
3005 * unfortuantely it happens for a couple of frames at
3006 * the beginning of every RTP connection */
Andreas Eversberg63bfdd82014-01-17 19:06:38 +01003007 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F/TCH_H\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003008 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003009 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003010 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01003011 switch (bts->type) {
3012 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02003013 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01003014 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003015 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003016 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003017 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003018 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003019 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003020 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003021 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003022 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003023 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003024 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003025 }
3026 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 }
3028
3029 memset(&rel, 0, sizeof(struct gsm_mncc));
3030 rel.callref = data->callref;
3031
3032 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003033 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003034
3035 /* Callref unknown */
3036 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003037 struct gsm_subscriber *subscr;
3038
Harald Welte4a3464c2009-07-04 10:11:24 +02003039 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3041 "Received '%s' from MNCC with "
3042 "unknown callref %d\n", data->called.number,
3043 get_mncc_name(msg_type), data->callref);
3044 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003045 return mncc_release_ind(net, NULL, data->callref,
3046 GSM48_CAUSE_LOC_PRN_S_LU,
3047 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003049 if (!data->called.number[0] && !data->imsi[0]) {
3050 DEBUGP(DCC, "(bts - trx - ts - ti) "
3051 "Received '%s' from MNCC with "
3052 "no number or IMSI\n", get_mncc_name(msg_type));
3053 /* Invalid number */
3054 return mncc_release_ind(net, NULL, data->callref,
3055 GSM48_CAUSE_LOC_PRN_S_LU,
3056 GSM48_CC_CAUSE_INV_NR_FORMAT);
3057 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003059 if (data->called.number[0])
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003060 subscr = subscr_get_by_extension(net->subscr_group,
Harald Welte9176bd42009-07-23 18:46:00 +02003061 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003062 else
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003063 subscr = subscr_get_by_imsi(net->subscr_group,
3064 data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003065
3066 /* update the subscriber we deal with */
3067 log_set_context(BSC_CTX_SUBSCR, subscr);
3068
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 /* If subscriber is not found */
3070 if (!subscr) {
3071 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3072 "Received '%s' from MNCC with "
3073 "unknown subscriber %s\n", data->called.number,
3074 get_mncc_name(msg_type), data->called.number);
3075 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003076 return mncc_release_ind(net, NULL, data->callref,
3077 GSM48_CAUSE_LOC_PRN_S_LU,
3078 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003079 }
3080 /* If subscriber is not "attached" */
3081 if (!subscr->lac) {
3082 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3083 "Received '%s' from MNCC with "
3084 "detached subscriber %s\n", data->called.number,
3085 get_mncc_name(msg_type), data->called.number);
3086 subscr_put(subscr);
3087 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003088 return mncc_release_ind(net, NULL, data->callref,
3089 GSM48_CAUSE_LOC_PRN_S_LU,
3090 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003091 }
3092 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003093 trans = trans_alloc(net, subscr, GSM48_PDISC_CC, 0xff, data->callref);
Harald Weltedcaf5652009-07-23 18:56:43 +02003094 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003095 DEBUGP(DCC, "No memory for trans.\n");
3096 subscr_put(subscr);
3097 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003098 mncc_release_ind(net, NULL, data->callref,
3099 GSM48_CAUSE_LOC_PRN_S_LU,
3100 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003101 return -ENOMEM;
3102 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003103 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003104 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003105
Harald Welte4bfdfe72009-06-10 23:11:52 +08003106 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003107 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003108 /* find transaction with this subscriber already paging */
3109 llist_for_each_entry(transt, &net->trans_list, entry) {
3110 /* Transaction of our lchan? */
3111 if (transt == trans ||
3112 transt->subscr != subscr)
3113 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003114 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003115 "Received '%s' from MNCC with "
3116 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003117 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003118 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003119 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003120 subscr_put(subscr);
3121 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003122 return 0;
3123 }
3124 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003125 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003126
3127 /* Get a channel */
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003128 trans->paging_request = talloc_zero(subscr->group->net,
3129 struct gsm_network*);
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003130 if (!trans->paging_request) {
3131 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3132 subscr_put(subscr);
3133 trans_free(trans);
3134 return 0;
3135 }
3136
Jacob Erlbeck1e30a282014-12-03 09:28:24 +01003137 *trans->paging_request = subscr->group->net;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003138 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003139
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003140 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003141 return 0;
3142 }
3143 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003144 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003145 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003146 } else {
3147 /* update the subscriber we deal with */
3148 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003149 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003150
3151 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003152 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153
3154 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003155 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003156 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003157 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003158 (trans->subscr)?(trans->subscr->extension):"-",
3159 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003160 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3161 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003162 if (msg_type == MNCC_REL_REQ)
3163 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3164 else
3165 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3166 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003167 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003168 return rc;
3169 }
3170
3171 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3172 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003173 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003174 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003175 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003176 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003177 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003178
3179 /* Find function for current state and message */
3180 for (i = 0; i < DOWNSLLEN; i++)
3181 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003182 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003183 break;
3184 if (i == DOWNSLLEN) {
3185 DEBUGP(DCC, "Message unhandled at this state.\n");
3186 return 0;
3187 }
3188
3189 rc = downstatelist[i].rout(trans, arg);
3190
3191 return rc;
3192}
3193
3194
3195static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003196 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003197 int type;
3198 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3199} datastatelist[] = {
3200 /* mobile originating call establishment */
3201 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3202 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3203 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3204 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3205 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3206 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3207 /* mobile terminating call establishment */
3208 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3209 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3210 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3211 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003212 {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 +08003213 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3214 /* signalling during call */
3215 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3216 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3217 {SBIT(GSM_CSTATE_ACTIVE),
3218 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3219 {ALL_STATES,
3220 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3221 {ALL_STATES,
3222 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3223 {ALL_STATES,
3224 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3225 {SBIT(GSM_CSTATE_ACTIVE),
3226 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3227 {SBIT(GSM_CSTATE_ACTIVE),
3228 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3229 {SBIT(GSM_CSTATE_ACTIVE),
3230 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3231 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3232 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3233 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3234 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3235 {SBIT(GSM_CSTATE_ACTIVE),
3236 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3237 /* clearing */
3238 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3239 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3240 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3241 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3242 {ALL_STATES, /* 5.4.3.4 */
3243 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3244};
3245
3246#define DATASLLEN \
3247 (sizeof(datastatelist) / sizeof(struct datastate))
3248
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003249static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003250{
3251 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003252 uint8_t msg_type = gh->msg_type & 0xbf;
3253 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003254 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003255 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003256
Harald Welte4bfdfe72009-06-10 23:11:52 +08003257 if (msg_type & 0x80) {
3258 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3259 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003260 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003261
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 /* Find transaction */
Jacob Erlbeckdae1f642014-12-02 14:22:53 +01003263 trans = trans_find_by_id(conn, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003264
Harald Welte6f5aee02009-07-23 21:21:14 +02003265 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003266 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003267 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003268 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003269 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3270 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003271
3272 /* Create transaction */
3273 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003274 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003275 "creating new trans.\n", transaction_id);
3276 /* Create transaction */
Jacob Erlbeckaf792d62014-12-02 14:22:53 +01003277 trans = trans_alloc(conn->bts->network, conn->subscr,
3278 GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003279 transaction_id, new_callref++);
3280 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003281 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003282 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003283 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003284 GSM48_MT_CC_RELEASE_COMPL);
3285 return -ENOMEM;
3286 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003287 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003288 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003289 }
3290
3291 /* find function for current state and message */
3292 for (i = 0; i < DATASLLEN; i++)
3293 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003294 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003295 break;
3296 if (i == DATASLLEN) {
3297 DEBUGP(DCC, "Message unhandled at this state.\n");
3298 return 0;
3299 }
3300
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003301 assert(trans->subscr);
3302
Harald Welte4bfdfe72009-06-10 23:11:52 +08003303 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003304
3305 return rc;
3306}
3307
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003308/* Create a dummy to wait five seconds */
3309static void release_anchor(struct gsm_subscriber_connection *conn)
3310{
3311 if (!conn->anch_operation)
3312 return;
3313
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003314 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003315 talloc_free(conn->anch_operation);
3316 conn->anch_operation = NULL;
3317}
3318
3319static void anchor_timeout(void *_data)
3320{
3321 struct gsm_subscriber_connection *con = _data;
3322
3323 release_anchor(con);
3324 msc_release_connection(con);
3325}
3326
3327int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3328{
3329 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3330 if (!conn->anch_operation)
3331 return -1;
3332
3333 conn->anch_operation->timeout.data = conn;
3334 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003335 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003336 return 0;
3337}
3338
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003339/* here we get data from the BSC level... */
3340int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003341{
3342 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003343 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003344 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003345
Jacob Erlbeck8e68b562014-01-30 21:01:12 +01003346 LOGP(DRLL, LOGL_DEBUG, "Dispatching 04.08 message, pdisc=%d\n", pdisc);
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003347 if (silent_call_reroute(conn, msg))
3348 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003349
3350 switch (pdisc) {
3351 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003352 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003353 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003354 break;
3355 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003356 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003357 break;
3358 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003359 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003360 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003361 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003362 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003363 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003364 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003365 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003366 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003367 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3368 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003369 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003370 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003371 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003372 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003373 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003374 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003375 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3376 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003377 break;
3378 }
3379
3380 return rc;
3381}
Harald Welte8470bf22008-12-25 23:28:35 +00003382
Harald Welte805f6442009-07-28 18:25:29 +02003383/*
3384 * This will be ran by the linker when loading the DSO. We use it to
3385 * do system initialization, e.g. registration of signal handlers.
3386 */
3387static __attribute__((constructor)) void on_dso_load_0408(void)
3388{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003389 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003390}