blob: eea073614739da146b1c15da80ab9c7fbd982d71 [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
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080083
Harald Welte31c00f72011-03-03 23:29:05 +010084void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
85{
86 net->mncc_recv(net, msg);
87}
88
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080089static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
90 struct gsm_trans *trans)
91{
92 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
93
94 /* if we get passed a transaction reference, do some common
95 * work that the caller no longer has to do */
96 if (trans) {
97 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
98 msg->lchan = trans->conn->lchan;
99 }
100
101
102 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200103 struct e1inp_sign_link *sign_link =
104 msg->lchan->ts->trx->rsl_link;
105
106 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800107 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
108 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200109 "Sending '%s' to MS.\n",
110 sign_link->trx->bts->nr,
111 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800112 gh->proto_discr & 0xf0,
113 gsm48_cc_msg_name(gh->msg_type));
114 else
115 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200116 "Sending 0x%02x to MS.\n",
117 sign_link->trx->bts->nr,
118 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800119 gh->proto_discr, gh->msg_type);
120 }
121
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100122 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800123}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100124
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800125int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
126{
127 struct gsm48_hdr *gh;
128 struct msgb *ss_notify;
129
130 ss_notify = gsm0480_create_notifySS(message);
131 if (!ss_notify)
132 return -1;
133
134 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
135 uint8_t *data = msgb_push(ss_notify, 1);
136 data[0] = ss_notify->len - 1;
137 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
138 gh->msg_type = GSM48_MT_CC_FACILITY;
139 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
140}
141
Harald Weltecf149ee2012-01-23 16:40:24 +0100142void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100143{
144 if (!conn->sec_operation)
145 return;
146
147 talloc_free(conn->sec_operation);
148 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800149 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100150}
151
Harald Weltecf149ee2012-01-23 16:40:24 +0100152void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100153{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100154 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
155 struct gsm_security_operation);
156}
157
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100158int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
159 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100160{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800161 struct gsm_network *net = conn->bts->network;
162 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100163 struct gsm_security_operation *op;
164 struct gsm_auth_tuple atuple;
165 int status = -1, rc;
166
167 /* Check if we _can_ enable encryption. Cases where we can't:
168 * - Encryption disabled in config
169 * - Channel already secured (nothing to do)
170 * - Subscriber equipment doesn't support configured encryption
171 */
172 if (!net->a5_encryption) {
173 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800174 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100175 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200176 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100177 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
178 net->a5_encryption)) {
179 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
180 status = GSM_SECURITY_NOAVAIL;
181 }
182
183 /* If not done yet, try to get info for this user */
184 if (status < 0) {
185 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
186 if (rc <= 0)
187 status = GSM_SECURITY_NOAVAIL;
188 }
189
190 /* Are we done yet ? */
191 if (status >= 0)
192 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800193 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100194 0;
195
196 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800197 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100198 return -EBUSY;
199
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800200 allocate_security_operation(conn);
201 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100202 op->cb = cb;
203 op->cb_data = cb_data;
204 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
205
206 /* FIXME: Should start a timer for completion ... */
207
208 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100209 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100210 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800211 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100212 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100213 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100214 return gsm0808_cipher_mode(conn, net->a5_encryption,
215 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100216 }
217
218 return -EINVAL; /* not reached */
219}
220
Holger Freyther73487a22008-12-31 18:53:57 +0000221static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
222 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000223{
224 if (!subscriber)
225 return 0;
226
Holger Freyther73487a22008-12-31 18:53:57 +0000227 /*
228 * Do not send accept yet as more information should arrive. Some
229 * phones will not send us the information and we will have to check
230 * what we want to do with that.
231 */
232 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
233 return 0;
234
Jan Luebbe06513f22009-08-12 12:48:00 +0200235 switch (subscriber->net->auth_policy) {
236 case GSM_AUTH_POLICY_CLOSED:
237 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200238 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200239 if (subscriber->authorized)
240 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200241 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200242 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000243 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200244 default:
245 return 0;
246 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000247}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000248
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100249static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000250{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100251 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000252 return;
253
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800254 /* No need to keep the connection up */
255 release_anchor(conn);
256
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200257 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100258 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100259 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800260 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000261}
262
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100263static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000264{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800265 if (conn->loc_operation)
266 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100267 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000268
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100269 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200270 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000271}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000272
Sylvain Munaut267fba02009-12-24 00:28:01 +0100273static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
274 struct msgb *msg, void *data, void *param)
275{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800276 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100277 int rc = 0;
278
279 switch (event) {
280 case GSM_SECURITY_AUTH_FAILED:
281 release_loc_updating_req(conn);
282 break;
283
284 case GSM_SECURITY_NOAVAIL:
285 case GSM_SECURITY_SUCCEEDED:
286 /* We're all good */
287 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800288 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800289 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100290 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800291 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100292 }
293
294 /* call subscr_update after putting the loc_upd_acc
295 * in the transmit queue, since S_SUBSCR_ATTACHED might
296 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800297 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100298 GSM_SUBSCRIBER_UPDATE_ATTACHED);
299
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100300 /*
301 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
302 * MS needs to respond with a TMSI REALLOCATION COMPLETE
303 * (even if the TMSI is the same).
304 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100305 break;
306
307 default:
308 rc = -EINVAL;
309 };
310
311 return rc;
312}
313
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100314static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000315{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100316 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800317 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100318 conn->loc_operation->key_seq,
319 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000320 return 0;
321}
322
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800323void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000324{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800325 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800326
327 /* avoid someone issuing a clear */
328 conn->in_release = 1;
329
Holger Freyther7c19f742009-06-06 13:54:35 +0000330 /*
331 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800332 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000333 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800334 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100335
336 /* We might need to cancel the paging response or such. */
337 if (conn->sec_operation && conn->sec_operation->cb) {
338 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
339 NULL, conn, conn->sec_operation->cb_data);
340 }
341
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800342 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800343 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000344
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100345 /*
346 * Free all transactions that are associated with the released
347 * connection. The transaction code will inform the CC or SMS
348 * facilities that will send the release indications. As part of
349 * the CC REL_IND the remote leg might be released and this will
350 * trigger the call to trans_free. This is something the llist
351 * macro can not handle and we will need to re-iterate the list.
352 *
353 * TODO: Move the trans_list into the subscriber connection and
354 * create a pending list for MT transactions. These exist before
355 * we have a subscriber connection.
356 */
357restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800358 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100359 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200360 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100361 goto restart;
362 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800363 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000364}
365
Harald Welte371efe52010-12-22 23:17:50 +0100366void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
367{
368 struct gsm_trans *trans, *temp;
369
370 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
371
372 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100373 if (trans->protocol == protocol) {
374 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100375 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100376 }
Harald Welte371efe52010-12-22 23:17:50 +0100377 }
378}
379
Holger Freyther429e7762008-12-30 13:28:30 +0000380/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200381int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000382{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800383 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800384 struct msgb *msg;
385
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200386 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800387
388 msg = gsm48_create_loc_upd_rej(cause);
389 if (!msg) {
390 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
391 return -1;
392 }
Harald Welte52b1f982008-12-23 20:25:15 +0000393
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800394 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000395
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100396 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100397 "LAC=%u BTS=%u\n", conn->subscr ?
398 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800399 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100400
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800401 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000402}
403
404/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200405int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000406{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800407 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000408 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000409 struct gsm48_hdr *gh;
410 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200411 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000412
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800413 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000414
415 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
416 gh->proto_discr = GSM48_PDISC_MM;
417 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
418
419 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100420 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000421 bts->network->network_code, bts->location_area_code);
422
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200423 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200424 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000425
426 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
427
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200428 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100429
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800430 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000431}
432
Harald Weltebf5e8df2009-02-03 12:59:45 +0000433/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200434static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000435{
436 struct msgb *msg = gsm48_msgb_alloc();
437 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000438
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800439 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000440
441 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
442 gh->proto_discr = GSM48_PDISC_MM;
443 gh->msg_type = GSM48_MT_MM_ID_REQ;
444 gh->data[0] = id_type;
445
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800446 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000447}
448
Harald Welte231ad4f2008-12-27 11:15:38 +0000449
Harald Weltebf5e8df2009-02-03 12:59:45 +0000450/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800451static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000452{
453 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000454 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200455 struct gsm_bts *bts = lchan->ts->trx->bts;
456 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200457 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200458 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000459
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200460 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000461 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000462 mi_type, mi_string);
463
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200464 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100465
Harald Welte75a983f2008-12-27 21:34:06 +0000466 switch (mi_type) {
467 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200468 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100469 if (!conn->subscr) {
470 conn->subscr = subscr_get_by_imsi(net, mi_string);
471 if (!conn->subscr)
472 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200473 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100474 if (conn->loc_operation)
475 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000476 break;
477 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000478 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000479 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100480 if (conn->subscr) {
481 db_subscriber_assoc_imei(conn->subscr, mi_string);
482 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200483 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100484 if (conn->loc_operation)
485 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000486 break;
487 }
Holger Freyther73487a22008-12-31 18:53:57 +0000488
489 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100490 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000491}
492
Harald Welte255539c2008-12-28 02:26:27 +0000493
494static void loc_upd_rej_cb(void *data)
495{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100496 struct gsm_subscriber_connection *conn = data;
497 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100498 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000499
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100500 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800501 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800502 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000503}
504
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100505static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000506{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100507 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
508 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200509 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000510}
511
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200512static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000513{
514 switch (type) {
515 case GSM48_LUPD_NORMAL:
516 return "NORMAL";
517 case GSM48_LUPD_PERIODIC:
518 return "PEROIDOC";
519 case GSM48_LUPD_IMSI_ATT:
520 return "IMSI ATTACH";
521 default:
522 return "UNKNOWN";
523 }
524}
525
Harald Weltebf5e8df2009-02-03 12:59:45 +0000526/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800527static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000528{
Harald Welte8470bf22008-12-25 23:28:35 +0000529 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000530 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800531 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800532 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200533 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200534 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000535
Harald Welte8470bf22008-12-25 23:28:35 +0000536 lu = (struct gsm48_loc_upd_req *) gh->data;
537
538 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000539
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200540 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000541
Harald Weltea0368542009-06-27 02:58:43 +0200542 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000543 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000544
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200545 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100546
Harald Welte24ff6ee2009-12-22 00:41:05 +0100547 switch (lu->type) {
548 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200549 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100550 break;
551 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200552 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100553 break;
554 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200555 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100556 break;
557 }
558
Holger Freythereaf04692009-06-06 13:54:44 +0000559 /*
560 * Pseudo Spoof detection: Just drop a second/concurrent
561 * location updating request.
562 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100563 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200564 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100565 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800566 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000567 return 0;
568 }
569
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800570 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000571
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200572 conn->loc_operation->key_seq = lu->key_seq;
573
Harald Welte52b1f982008-12-23 20:25:15 +0000574 switch (mi_type) {
575 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200576 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000577 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200578 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100579 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000580
Jan Luebbe370b41d2009-08-12 10:19:34 +0200581 /* look up subscriber based on IMSI, create if not found */
582 subscr = subscr_get_by_imsi(bts->network, mi_string);
583 if (!subscr) {
584 subscr = db_create_subscriber(bts->network, mi_string);
585 }
Harald Welte4b634542008-12-27 01:55:51 +0000586 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000587 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200588 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000589 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200590 subscr = subscr_get_by_tmsi(bts->network,
591 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000592 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000593 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200594 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100595 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000596 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200597 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200598 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200599 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000600 break;
601 case GSM_MI_TYPE_IMEI:
602 case GSM_MI_TYPE_IMEISV:
603 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200604 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000605 break;
606 default:
Harald Weltea0368542009-06-27 02:58:43 +0200607 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000608 break;
609 }
610
Harald Welte24516ea2009-07-04 10:18:00 +0200611 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100612 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200613
Harald Welte4bfdfe72009-06-10 23:11:52 +0800614 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200615 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800616 /* FIXME: request id? close channel? */
617 return -EINVAL;
618 }
619
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100620 conn->subscr = subscr;
621 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000622
Harald Welte24516ea2009-07-04 10:18:00 +0200623 /* check if we can let the subscriber into our network immediately
624 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100625 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000626}
627
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100628/* Turn int into semi-octet representation: 98 => 0x89 */
629static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800630{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100631 uint8_t ret;
632
633 ret = value / 10;
634 ret |= (value % 10) << 4;
635
636 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800637}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100638
Harald Welte4bfdfe72009-06-10 23:11:52 +0800639
Harald Weltedb253af2008-12-30 17:56:55 +0000640/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800641int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000642{
643 struct msgb *msg = gsm48_msgb_alloc();
644 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800645 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100646 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200647 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200648 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100649
Harald Welte4bfdfe72009-06-10 23:11:52 +0800650 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100651 struct tm* gmt_time;
652 struct tm* local_time;
653 int tzunits;
Harald Weltedb253af2008-12-30 17:56:55 +0000654
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800655 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000656
657 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
658 gh->proto_discr = GSM48_PDISC_MM;
659 gh->msg_type = GSM48_MT_MM_INFO;
660
661 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200662#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000663 name_len = strlen(net->name_long);
664 /* 10.5.3.5a */
665 ptr8 = msgb_put(msg, 3);
666 ptr8[0] = GSM48_IE_NAME_LONG;
667 ptr8[1] = name_len*2 +1;
668 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
669
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200670 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000671 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000672 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000673
674 /* FIXME: Use Cell Broadcast, not UCS-2, since
675 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200676#endif
677 name_len = (strlen(net->name_long)*7)/8;
678 name_pad = (8 - strlen(net->name_long)*7)%8;
679 if (name_pad > 0)
680 name_len++;
681 /* 10.5.3.5a */
682 ptr8 = msgb_put(msg, 3);
683 ptr8[0] = GSM48_IE_NAME_LONG;
684 ptr8[1] = name_len +1;
685 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
686
687 ptr8 = msgb_put(msg, name_len);
688 gsm_7bit_encode(ptr8, net->name_long);
689
Harald Weltedb253af2008-12-30 17:56:55 +0000690 }
691
692 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200693#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000694 name_len = strlen(net->name_short);
695 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200696 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200697 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000698 ptr8[1] = name_len*2 + 1;
699 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
700
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200701 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000702 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000703 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200704#endif
705 name_len = (strlen(net->name_short)*7)/8;
706 name_pad = (8 - strlen(net->name_short)*7)%8;
707 if (name_pad > 0)
708 name_len++;
709 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200710 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200711 ptr8[0] = GSM48_IE_NAME_SHORT;
712 ptr8[1] = name_len +1;
713 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
714
715 ptr8 = msgb_put(msg, name_len);
716 gsm_7bit_encode(ptr8, net->name_short);
717
Harald Weltedb253af2008-12-30 17:56:55 +0000718 }
719
Harald Weltedb253af2008-12-30 17:56:55 +0000720 /* Section 10.5.3.9 */
721 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100722 gmt_time = gmtime(&cur_t);
723
Harald Weltedb253af2008-12-30 17:56:55 +0000724 ptr8 = msgb_put(msg, 8);
725 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100726 ptr8[1] = bcdify(gmt_time->tm_year % 100);
727 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
728 ptr8[3] = bcdify(gmt_time->tm_mday);
729 ptr8[4] = bcdify(gmt_time->tm_hour);
730 ptr8[5] = bcdify(gmt_time->tm_min);
731 ptr8[6] = bcdify(gmt_time->tm_sec);
732
Harald Welte45f91712012-07-08 16:48:11 +0200733 if (bts->tz.override) {
734 /* Convert tz.hr and tz.mn to units */
735 if (bts->tz.hr < 0) {
736 tzunits = ((bts->tz.hr/-1)*4);
737 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100738 ptr8[7] = bcdify(tzunits);
739 /* Set negative time */
740 ptr8[7] |= 0x08;
741 }
742 else {
Harald Welte45f91712012-07-08 16:48:11 +0200743 tzunits = bts->tz.hr*4;
744 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100745 ptr8[7] = bcdify(tzunits);
746 }
747 }
748 else {
749 /* Need to get GSM offset and convert into 15 min units */
750 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
751 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200752#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100753 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200754#else
755#warning find a portable way to obtain the timezone offset
756 tzunits = 0;
757#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100758 if (tzunits < 0) {
759 tzunits = tzunits/-1;
760 ptr8[7] = bcdify(tzunits);
761 /* Flip it to negative */
762 ptr8[7] |= 0x08;
763 }
764 else
765 ptr8[7] = bcdify(tzunits);
766 }
Harald Weltedb253af2008-12-30 17:56:55 +0000767
Daniel Willmanneea93372009-08-13 03:42:07 +0200768 DEBUGP(DMM, "-> MM INFO\n");
769
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800770 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000771}
772
Harald Welte7984d5c2009-08-12 22:56:50 +0200773/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200774int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200775{
776 struct msgb *msg = gsm48_msgb_alloc();
777 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200778 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200779
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200780 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200781
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800782 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200783 gh->proto_discr = GSM48_PDISC_MM;
784 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
785
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100786 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200787
Harald Welte7984d5c2009-08-12 22:56:50 +0200788 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200789 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200790 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200791
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800792 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200793}
794
795/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800796int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200797{
798 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800799 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200800}
801
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800802static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000803{
Harald Welte4b634542008-12-27 01:55:51 +0000804 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800805 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000806}
Harald Welteba4cf162009-01-10 01:49:35 +0000807
808/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100809static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000810 enum gsm48_reject_value value)
811{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800812 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000813
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800814 msg = gsm48_create_mm_serv_rej(value);
815 if (!msg) {
816 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
817 return -1;
818 }
Harald Welteba4cf162009-01-10 01:49:35 +0000819
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800820 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100821 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800822 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000823}
824
Sylvain Munautba87f452009-12-24 00:28:46 +0100825static int _gsm48_rx_mm_serv_req_sec_cb(
826 unsigned int hooknum, unsigned int event,
827 struct msgb *msg, void *data, void *param)
828{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800829 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100830 int rc = 0;
831
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200832 /* auth failed or succeeded, the timer was stopped */
833 conn->expire_timer_stopped = 1;
834
Sylvain Munautba87f452009-12-24 00:28:46 +0100835 switch (event) {
836 case GSM_SECURITY_AUTH_FAILED:
837 /* Nothing to do */
838 break;
839
840 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200841 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800842 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100843 break;
844
845 case GSM_SECURITY_SUCCEEDED:
846 /* nothing to do. CIPHER MODE COMMAND is
847 * implicit CM SERV ACK */
848 break;
849
850 default:
851 rc = -EINVAL;
852 };
853
854 return rc;
855}
856
Harald Welte4ed0e922009-01-10 03:17:30 +0000857/*
858 * Handle CM Service Requests
859 * a) Verify that the packet is long enough to contain the information
860 * we require otherwsie reject with INCORRECT_MESSAGE
861 * b) Try to parse the TMSI. If we do not have one reject
862 * c) Check that we know the subscriber with the TMSI otherwise reject
863 * with a HLR cause
864 * d) Set the subscriber on the gsm_lchan and accept
865 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800866static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000867{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200868 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200869 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000870
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800871 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000872 struct gsm_subscriber *subscr;
873 struct gsm48_hdr *gh = msgb_l3(msg);
874 struct gsm48_service_request *req =
875 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800876 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200877 uint8_t classmark2_len = gh->data[1];
878 uint8_t *classmark2 = gh->data+2;
879 uint8_t mi_len = *(classmark2 + classmark2_len);
880 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000881
Harald Weltec9e02182009-05-01 19:07:53 +0000882 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000883 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000884 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800885 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000886 GSM48_REJECT_INCORRECT_MESSAGE);
887 }
888
889 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000890 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800891 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000892 GSM48_REJECT_INCORRECT_MESSAGE);
893 }
894
Harald Weltec9e02182009-05-01 19:07:53 +0000895 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000896 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000897 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800898 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000899 GSM48_REJECT_INCORRECT_MESSAGE);
900 }
901
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200902 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000903 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000904 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000905
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200906 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100907
Harald Welte3ac7f102009-08-10 10:12:45 +0200908 if (is_siemens_bts(bts))
909 send_siemens_mrpci(msg->lchan, classmark2-1);
910
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200911 subscr = subscr_get_by_tmsi(bts->network,
912 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000913
Harald Welte2a139372009-02-22 21:14:55 +0000914 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000915 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800916 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000917 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Holger Hans Peter Freyther4d2a68c2013-01-01 17:18:27 +0100918 if (subscr->lac == GSM_LAC_RESERVED_DETACHED)
Sylvain Munaut11c1b6e2012-12-28 00:33:04 +0100919 /* If the subscriber is not attached, reject service */
920 return gsm48_tx_mm_serv_rej(conn,
921 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +0000922
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800923 if (!conn->subscr)
924 conn->subscr = subscr;
925 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100926 subscr_put(subscr); /* lchan already has a ref, don't need another one */
927 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000928 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
929 subscr_put(subscr);
930 }
931
Harald Weltec2e302d2009-07-05 14:08:13 +0200932 subscr->equipment.classmark2_len = classmark2_len;
933 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
934 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000935
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200936 /* we will send a MM message soon */
937 conn->expire_timer_stopped = 1;
938
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800939 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100940 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000941}
942
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200943static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000944{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200945 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000946 struct gsm48_hdr *gh = msgb_l3(msg);
947 struct gsm48_imsi_detach_ind *idi =
948 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200949 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200950 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800951 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000952
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200953 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100954 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000955 mi_type, mi_string);
956
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200957 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100958
Harald Welte2a139372009-02-22 21:14:55 +0000959 switch (mi_type) {
960 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100961 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200962 subscr = subscr_get_by_tmsi(bts->network,
963 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000964 break;
965 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100966 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +0200967 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000968 break;
969 case GSM_MI_TYPE_IMEI:
970 case GSM_MI_TYPE_IMEISV:
971 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +0100972 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000973 break;
974 default:
Daniel Willmann977cd132012-12-28 19:29:44 +0100975 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000976 break;
977 }
978
Holger Freyther4a49e772009-04-12 05:37:29 +0000979 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200980 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000981 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100982 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200983
984 subscr->equipment.classmark1 = idi->classmark1;
985 db_sync_equipment(&subscr->equipment);
986
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000987 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000988 } else
Harald Welte2a139372009-02-22 21:14:55 +0000989 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
990
Harald Welte31981a02009-12-20 09:58:40 +0100991 /* FIXME: iterate over all transactions and release them,
992 * imagine an IMSI DETACH happening during an active call! */
993
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200994 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +0000995 return 0;
996}
997
Harald Welted2a7f5a2009-06-05 20:08:20 +0000998static int gsm48_rx_mm_status(struct msgb *msg)
999{
1000 struct gsm48_hdr *gh = msgb_l3(msg);
1001
1002 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1003
1004 return 0;
1005}
1006
Sylvain Munaut30a15382009-12-24 00:27:26 +01001007/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001008static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001009{
1010 struct gsm48_hdr *gh = msgb_l3(msg);
1011 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001012 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001013
Sylvain Munautc593cf12010-06-10 23:51:41 +02001014 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001015 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001016
1017 /* Safety check */
1018 if (!conn->sec_operation) {
1019 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1020 return -EIO;
1021 }
1022
1023 /* Validate SRES */
1024 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001025 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001026 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001027
1028 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001029 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001030
Sylvain Munaut30a15382009-12-24 00:27:26 +01001031 if (cb)
1032 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001033 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001034
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001035 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001036 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001037 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001038 }
1039
Sylvain Munautc593cf12010-06-10 23:51:41 +02001040 DEBUGPC(DMM, "OK\n");
1041
Sylvain Munaut30a15382009-12-24 00:27:26 +01001042 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001043 return gsm0808_cipher_mode(conn, net->a5_encryption,
1044 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001045}
1046
Harald Weltebf5e8df2009-02-03 12:59:45 +00001047/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001048static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001049{
1050 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001051 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001052
1053 switch (gh->msg_type & 0xbf) {
1054 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001055 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001056 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001057 break;
1058 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001059 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001060 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001061 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001062 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001063 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001064 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001065 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001066 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001067 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001068 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001069 conn->subscr ?
1070 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001071 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001072 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001073 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001074 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001075 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001076 break;
1077 case GSM48_MT_MM_CM_REEST_REQ:
1078 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1079 break;
1080 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001081 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001082 break;
1083 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001084 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001085 gh->msg_type);
1086 break;
1087 }
1088
1089 return rc;
1090}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001091
Harald Welte2d35ae62009-02-06 12:02:13 +00001092/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001093static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001094{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001095 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001096 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001097 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001098 uint8_t *classmark2_lv = gh->data + 1;
1099 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001100 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001101 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001102 int rc = 0;
1103
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001104 resp = (struct gsm48_pag_resp *) &gh->data[0];
1105 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1106 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001107 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1108 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001109
Harald Weltefe18d8f2009-02-22 21:14:24 +00001110 switch (mi_type) {
1111 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001112 subscr = subscr_get_by_tmsi(bts->network,
1113 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001114 break;
1115 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001116 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001117 break;
1118 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001119
1120 if (!subscr) {
1121 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001122 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001123 return -EINVAL;
1124 }
1125 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001126 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001127
Harald Weltec2e302d2009-07-05 14:08:13 +02001128 subscr->equipment.classmark2_len = *classmark2_lv;
1129 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1130 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001131
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001132 /* We received a paging */
1133 conn->expire_timer_stopped = 1;
1134
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001135 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001136 return rc;
1137}
1138
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001139static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001140{
1141 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001142 uint8_t apdu_id_flags;
1143 uint8_t apdu_len;
1144 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145
1146 apdu_id_flags = gh->data[0];
1147 apdu_len = gh->data[1];
1148 apdu_data = gh->data+2;
1149
Harald Welte70f92052012-07-16 13:22:19 +02001150 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001151 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001152
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001153 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001154}
1155
Harald Weltebf5e8df2009-02-03 12:59:45 +00001156/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001157static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001158{
1159 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001160 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001161
1162 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001163 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001164 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001165 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001166 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001167 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001168 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001169 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001170 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001171 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001172 break;
1173 }
1174
Harald Welte2d35ae62009-02-06 12:02:13 +00001175 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001176}
1177
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001178int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1179 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001180{
1181 struct msgb *msg = gsm48_msgb_alloc();
1182 struct gsm48_hdr *gh;
1183
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001184 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001185
1186 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1187 apdu_id, apdu_len);
1188
1189 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1190 gh->proto_discr = GSM48_PDISC_RR;
1191 gh->msg_type = GSM48_MT_RR_APP_INFO;
1192 gh->data[0] = apdu_id;
1193 gh->data[1] = apdu_len;
1194 memcpy(gh->data+2, apdu, apdu_len);
1195
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001196 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001197}
1198
Harald Welte4bc90a12008-12-27 16:32:52 +00001199/* Call Control */
1200
Harald Welte7584aea2009-02-11 11:44:12 +00001201/* The entire call control code is written in accordance with Figure 7.10c
1202 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1203 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1204 * it for voice */
1205
Harald Welte4bfdfe72009-06-10 23:11:52 +08001206static void new_cc_state(struct gsm_trans *trans, int state)
1207{
1208 if (state > 31 || state < 0)
1209 return;
1210
1211 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001212 gsm48_cc_state_name(trans->cc.state),
1213 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001214
Harald Weltedcaf5652009-07-23 18:56:43 +02001215 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001216}
1217
1218static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001219{
1220 struct msgb *msg = gsm48_msgb_alloc();
1221 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001222 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001223
Harald Welte4bc90a12008-12-27 16:32:52 +00001224 gh->msg_type = GSM48_MT_CC_STATUS;
1225
1226 cause = msgb_put(msg, 3);
1227 cause[0] = 2;
1228 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1229 cause[2] = 0x80 | 30; /* response to status inquiry */
1230
1231 call_state = msgb_put(msg, 1);
1232 call_state[0] = 0xc0 | 0x00;
1233
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001234 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001235}
1236
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001237static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001238 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001239{
1240 struct msgb *msg = gsm48_msgb_alloc();
1241 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1242
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001243 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001244
Harald Welte6f4b7532008-12-29 00:39:37 +00001245 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001246 gh->msg_type = msg_type;
1247
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001248 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001249}
1250
Harald Welte4bfdfe72009-06-10 23:11:52 +08001251static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1252{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001253 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001254 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001255 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001256 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001257 }
1258}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001259
Harald Welte4bfdfe72009-06-10 23:11:52 +08001260static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1261 int msg_type, struct gsm_mncc *mncc)
1262{
1263 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001264 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001265
1266 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001267 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001268 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001269 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001270 trans->conn->lchan->ts->trx->bts->nr,
1271 trans->conn->lchan->ts->trx->nr,
1272 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001273 (trans->subscr)?(trans->subscr->extension):"-",
1274 get_mncc_name(msg_type));
1275 else
1276 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1277 "Sending '%s' to MNCC.\n",
1278 (trans->subscr)?(trans->subscr->extension):"-",
1279 get_mncc_name(msg_type));
1280 else
1281 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1282 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1283
1284 mncc->msg_type = msg_type;
1285
Harald Welte966636f2009-06-26 19:39:35 +02001286 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001287 if (!msg)
1288 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001289
1290 data = msgb_put(msg, sizeof(struct gsm_mncc));
1291 memcpy(data, mncc, sizeof(struct gsm_mncc));
1292
Harald Welte54209c22010-12-22 23:43:29 +01001293 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294
1295 return 0;
1296}
1297
1298int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001299 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300{
1301 struct gsm_mncc rel;
1302
Harald Welte92f70c52009-06-12 01:54:08 +08001303 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001304 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001305 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001306 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1307 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001308 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1309}
1310
Harald Weltedcaf5652009-07-23 18:56:43 +02001311/* Call Control Specific transaction release.
1312 * gets called by trans_free, DO NOT CALL YOURSELF! */
1313void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001315 gsm48_stop_cc_timer(trans);
1316
1317 /* send release to L4, if callref still exists */
1318 if (trans->callref) {
1319 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001320 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001321 GSM48_CAUSE_LOC_PRN_S_LU,
1322 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001324 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001325 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001326 if (trans->conn)
1327 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328}
1329
1330static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001331
Harald Welte09e38af2009-02-16 22:52:23 +00001332/* call-back from paging the B-end of the connection */
1333static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001334 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001335{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001336 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001337 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001338 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001340
Harald Welte09e38af2009-02-16 22:52:23 +00001341 if (hooknum != GSM_HOOK_RR_PAGING)
1342 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001343
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001344 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 if (!net) {
1346 DEBUGP(DCC, "Error Network not set!\n");
1347 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001348 }
Harald Welte7584aea2009-02-11 11:44:12 +00001349
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350 /* check all tranactions (without lchan) for subscriber */
1351 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001352 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353 continue;
1354 switch (event) {
1355 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001356 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357 break;
1358 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001359 transt->subscr->extension);
1360 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001362 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001363 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001364 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001365 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 }
1367 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001368 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 break;
1370 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001371 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001373 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001375 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001376 mncc_release_ind(transt->subscr->net, transt,
1377 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001378 GSM48_CAUSE_LOC_PRN_S_LU,
1379 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001381 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001382 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383 break;
1384 }
1385 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001386
1387 talloc_free(paging_request);
1388
1389 /*
1390 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1391 * failure and then the subscr will be poke again. This needs a lot of fixing
1392 * in the subscriber queue code.
1393 */
1394 if (!found && conn)
1395 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001396 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001397}
Harald Welte7584aea2009-02-11 11:44:12 +00001398
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001399static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001400
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001401/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001402static int switch_for_handover(struct gsm_lchan *old_lchan,
1403 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001404{
1405 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001406
1407 if (ipacc_rtp_direct) {
1408 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1409 return 0;
1410 }
1411
1412 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001413 new_rs = new_lchan->abis_ip.rtp_socket;
1414 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001415
1416 if (!new_rs) {
1417 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1418 return -EIO;
1419 }
1420
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001421 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001422
1423 if (!old_rs) {
1424 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1425 return -EIO;
1426 }
1427
1428 /* copy rx_action and reference to other sock */
1429 new_rs->rx_action = old_rs->rx_action;
1430 new_rs->tx_action = old_rs->tx_action;
1431 new_rs->transmit = old_rs->transmit;
1432
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001433 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001434 case RTP_PROXY:
1435 other_rs = old_rs->proxy.other_sock;
1436 rtp_socket_proxy(new_rs, other_rs);
1437 /* delete reference to other end socket to prevent
1438 * rtp_socket_free() from removing the inverse reference */
1439 old_rs->proxy.other_sock = NULL;
1440 break;
1441 case RTP_RECV_UPSTREAM:
1442 new_rs->receive = old_rs->receive;
1443 break;
1444 case RTP_NONE:
1445 break;
1446 }
1447
1448 return 0;
1449}
1450
Harald Welte805f6442009-07-28 18:25:29 +02001451/* some other part of the code sends us a signal */
1452static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1453 void *handler_data, void *signal_data)
1454{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001455 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001456 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001457 struct gsm_network *net;
1458 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001459
1460 if (subsys != SS_ABISIP)
1461 return 0;
1462
1463 /* in case we use direct BTS-to-BTS RTP */
1464 if (ipacc_rtp_direct)
1465 return 0;
1466
Harald Welte805f6442009-07-28 18:25:29 +02001467 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001468 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001469 /* in case we don't use direct BTS-to-BTS RTP */
1470 /* the BTS has successfully bound a TCH to a local ip/port,
1471 * which means we can connect our UDP socket to it */
1472 if (lchan->abis_ip.rtp_socket) {
1473 rtp_socket_free(lchan->abis_ip.rtp_socket);
1474 lchan->abis_ip.rtp_socket = NULL;
1475 }
1476
1477 lchan->abis_ip.rtp_socket = rtp_socket_create();
1478 if (!lchan->abis_ip.rtp_socket)
1479 return -EIO;
1480
1481 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1482 lchan->abis_ip.bound_ip,
1483 lchan->abis_ip.bound_port);
1484 if (rc < 0)
1485 return -EIO;
1486
Harald Welteda7ab742009-12-19 22:23:05 +01001487 /* check if any transactions on this lchan still have
1488 * a tch_recv_mncc request pending */
1489 net = lchan->ts->trx->bts->network;
1490 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001491 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001492 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1493 tch_recv_mncc(net, trans->callref, 1);
1494 }
1495 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001496
1497 /*
1498 * TODO: this appears to be too early? Why not until after
1499 * the handover detect or the handover complete?
1500 *
1501 * Do we have a handover pending for this new lchan? In that
1502 * case re-route the audio from the old channel to the new one.
1503 */
1504 old_lchan = bsc_handover_pending(lchan);
1505 if (old_lchan)
1506 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001507 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001508 case S_ABISIP_DLCX_IND:
1509 /* the BTS tells us a RTP stream has been disconnected */
1510 if (lchan->abis_ip.rtp_socket) {
1511 rtp_socket_free(lchan->abis_ip.rtp_socket);
1512 lchan->abis_ip.rtp_socket = NULL;
1513 }
1514
1515 break;
Harald Welte805f6442009-07-28 18:25:29 +02001516 }
1517
1518 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001519}
1520
Harald Welte49f48b82009-02-17 15:29:33 +00001521/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001522static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001523{
Harald Welte11fa29c2009-02-19 17:24:39 +00001524 struct gsm_bts *bts = lchan->ts->trx->bts;
1525 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001526 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001527
Harald Welte11fa29c2009-02-19 17:24:39 +00001528 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1529 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1530 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1531
1532 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001533 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001534 return -EINVAL;
1535 }
Harald Welteda7ab742009-12-19 22:23:05 +01001536
1537 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001538 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001539 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001540 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001541 if (!ipacc_rtp_direct) {
1542 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001543 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001544 if (rc < 0)
1545 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001546 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001547 if (rc < 0)
1548 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001549 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301550 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1551 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001552 } else {
1553 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301554 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1555 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301556 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001557 if (rc < 0)
1558 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301559 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1560 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301561 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001562 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001563 break;
1564 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001565 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001566 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001567 trau_mux_map_lchan(lchan, remote_lchan);
1568 break;
1569 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001570 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001571 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001572 }
Harald Welte49f48b82009-02-17 15:29:33 +00001573
1574 return 0;
1575}
1576
Harald Welte4bfdfe72009-06-10 23:11:52 +08001577/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001578static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001579{
Harald Weltedcaf5652009-07-23 18:56:43 +02001580 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1581 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001582
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001584 return -EIO;
1585
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001586 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001587 return -EIO;
1588
1589 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001590 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001591}
1592
Harald Welteda7ab742009-12-19 22:23:05 +01001593/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001594static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001595{
1596 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001597 struct gsm_lchan *lchan;
1598 struct gsm_bts *bts;
1599 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001600
Harald Welte4bfdfe72009-06-10 23:11:52 +08001601 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001602 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001603 if (!trans)
1604 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001605 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001606 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001607 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001608 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001609
Harald Welteda7ab742009-12-19 22:23:05 +01001610 switch (bts->type) {
1611 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001612 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001613 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001614 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001615 return -EINVAL;
1616 }
1617 /* in case, we don't have a RTP socket yet, we note this
1618 * in the transaction and try later */
1619 if (!lchan->abis_ip.rtp_socket) {
1620 trans->tch_recv = enable;
1621 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1622 return 0;
1623 }
1624 if (enable) {
1625 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001626 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001627 if (rc < 0)
1628 return rc;
1629 /* assign socket to application interface */
1630 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1631 net, callref);
1632 } else
1633 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1634 net, 0);
1635 break;
1636 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001637 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001638 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001639 if (enable)
1640 return trau_recv_lchan(lchan, callref);
1641 return trau_mux_unmap(NULL, callref);
1642 break;
1643 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001644 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001645 return -EINVAL;
1646 }
1647
1648 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649}
1650
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1652{
1653 DEBUGP(DCC, "-> STATUS ENQ\n");
1654 return gsm48_cc_tx_status(trans, msg);
1655}
1656
1657static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1658static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1659
1660static void gsm48_cc_timeout(void *arg)
1661{
1662 struct gsm_trans *trans = arg;
1663 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001664 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1665 int mo_location = GSM48_CAUSE_LOC_USER;
1666 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1667 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 struct gsm_mncc mo_rel, l4_rel;
1669
1670 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1671 mo_rel.callref = trans->callref;
1672 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1673 l4_rel.callref = trans->callref;
1674
Harald Weltedcaf5652009-07-23 18:56:43 +02001675 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001676 case 0x303:
1677 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001678 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 break;
1680 case 0x310:
1681 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001682 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683 break;
1684 case 0x313:
1685 disconnect = 1;
1686 /* unknown, did not find it in the specs */
1687 break;
1688 case 0x301:
1689 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001690 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001691 break;
1692 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001695 gsm48_cc_tx_release(trans, &trans->cc.msg);
1696 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 break; /* stay in release state */
1698 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001699 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 return;
1701// release = 1;
1702// l4_cause = 14;
1703// break;
1704 case 0x306:
1705 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001706 mo_cause = trans->cc.msg.cause.value;
1707 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 break;
1709 case 0x323:
1710 disconnect = 1;
1711 break;
1712 default:
1713 release = 1;
1714 }
1715
1716 if (release && trans->callref) {
1717 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001718 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001719 l4_location, l4_cause);
1720 trans->callref = 0;
1721 }
1722
1723 if (disconnect && trans->callref) {
1724 /* process disconnect towards layer 4 */
1725 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001726 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727 }
1728
1729 /* process disconnect towards mobile station */
1730 if (disconnect || release) {
1731 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001732 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1733 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1734 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 mo_rel.cause.diag_len = 3;
1736
1737 if (disconnect)
1738 gsm48_cc_tx_disconnect(trans, &mo_rel);
1739 if (release)
1740 gsm48_cc_tx_release(trans, &mo_rel);
1741 }
1742
1743}
1744
1745static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1746 int sec, int micro)
1747{
1748 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001749 trans->cc.timer.cb = gsm48_cc_timeout;
1750 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001751 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001752 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753}
1754
1755static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1756{
1757 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001758 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1760 struct tlv_parsed tp;
1761 struct gsm_mncc setup;
1762
1763 memset(&setup, 0, sizeof(struct gsm_mncc));
1764 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001765 if (trans->conn && trans->conn->lchan)
1766 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001767 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 /* emergency setup is identified by msg_type */
1769 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1770 setup.emergency = 1;
1771
1772 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001773 setup.fields |= MNCC_F_CALLING;
1774 strncpy(setup.calling.number, trans->subscr->extension,
1775 sizeof(setup.calling.number)-1);
1776 strncpy(setup.imsi, trans->subscr->imsi,
1777 sizeof(setup.imsi)-1);
1778
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* bearer capability */
1780 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1781 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001782 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1784 }
1785 /* facility */
1786 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1787 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001788 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1790 }
1791 /* called party bcd number */
1792 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1793 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001794 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1796 }
1797 /* user-user */
1798 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1799 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001800 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1802 }
1803 /* ss-version */
1804 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1805 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1808 }
1809 /* CLIR suppression */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1811 setup.clir.sup = 1;
1812 /* CLIR invocation */
1813 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1814 setup.clir.inv = 1;
1815 /* cc cap */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1817 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001818 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1820 }
1821
Harald Welte4bfdfe72009-06-10 23:11:52 +08001822 new_cc_state(trans, GSM_CSTATE_INITIATED);
1823
Harald Welte (local)47df3992009-12-26 19:45:03 +01001824 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1825 subscr_name(trans->subscr), trans->subscr->extension,
1826 setup.called.number);
1827
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001828 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001829
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001831 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001832
Harald Welte13cac662009-07-29 12:10:35 +02001833 /* MNCC code will modify the channel asynchronously, we should
1834 * ipaccess-bind only after the modification has been made to the
1835 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836 return 0;
1837}
1838
1839static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001840{
1841 struct msgb *msg = gsm48_msgb_alloc();
1842 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001844 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001845
Harald Welte7ccf7782009-02-17 01:43:01 +00001846 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001847
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 /* transaction id must not be assigned */
1849 if (trans->transaction_id != 0xff) { /* unasssigned */
1850 DEBUGP(DCC, "TX Setup with assigned transaction. "
1851 "This is not allowed!\n");
1852 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001853 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001854 GSM48_CAUSE_LOC_PRN_S_LU,
1855 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001857 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001858 return rc;
1859 }
1860
1861 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001862 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1863 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001864 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001865 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001866 GSM48_CAUSE_LOC_PRN_S_LU,
1867 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001869 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001870 return rc;
1871 }
Harald Welte78283ef2009-07-23 21:36:44 +02001872 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001873
Harald Welte65e74cc2008-12-29 01:55:35 +00001874 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001875
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001877
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 /* bearer capability */
1879 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* facility */
1882 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 /* progress */
1885 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 /* calling party BCD number */
1888 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 /* called party BCD number */
1891 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 /* user-user */
1894 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001895 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 /* redirecting party BCD number */
1897 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 /* signal */
1900 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001901 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902
1903 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001904
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001905 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001906
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001907 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001908}
1909
Harald Welte4bfdfe72009-06-10 23:11:52 +08001910static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1911{
1912 struct gsm48_hdr *gh = msgb_l3(msg);
1913 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1914 struct tlv_parsed tp;
1915 struct gsm_mncc call_conf;
1916
1917 gsm48_stop_cc_timer(trans);
1918 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1919
1920 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1921 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001922 if (trans->conn && trans->conn->lchan)
1923 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001924 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925#if 0
1926 /* repeat */
1927 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1928 call_conf.repeat = 1;
1929 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1930 call_conf.repeat = 2;
1931#endif
1932 /* bearer capability */
1933 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1934 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001935 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1937 }
1938 /* cause */
1939 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1940 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001941 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001942 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1943 }
1944 /* cc cap */
1945 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1946 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001947 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1949 }
1950
1951 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1952
Harald Welte596fed42009-07-23 19:06:52 +02001953 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1954 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955}
1956
1957static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1958{
1959 struct gsm_mncc *proceeding = arg;
1960 struct msgb *msg = gsm48_msgb_alloc();
1961 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1962
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1964
1965 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1966
1967 /* bearer capability */
1968 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 /* facility */
1971 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 /* progress */
1974 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001977 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978}
1979
1980static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1981{
1982 struct gsm48_hdr *gh = msgb_l3(msg);
1983 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1984 struct tlv_parsed tp;
1985 struct gsm_mncc alerting;
1986
1987 gsm48_stop_cc_timer(trans);
1988 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1989
1990 memset(&alerting, 0, sizeof(struct gsm_mncc));
1991 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001992 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993 /* facility */
1994 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1995 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001996 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1998 }
1999
2000 /* progress */
2001 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2002 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002003 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2005 }
2006 /* ss-version */
2007 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2008 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002009 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002010 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2011 }
2012
2013 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2014
Harald Welte596fed42009-07-23 19:06:52 +02002015 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2016 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002017}
2018
2019static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2020{
2021 struct gsm_mncc *alerting = arg;
2022 struct msgb *msg = gsm48_msgb_alloc();
2023 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2024
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 gh->msg_type = GSM48_MT_CC_ALERTING;
2026
2027 /* facility */
2028 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002029 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030 /* progress */
2031 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002032 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002033 /* user-user */
2034 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002035 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002036
2037 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2038
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002039 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002040}
2041
2042static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2043{
2044 struct gsm_mncc *progress = arg;
2045 struct msgb *msg = gsm48_msgb_alloc();
2046 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2047
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 gh->msg_type = GSM48_MT_CC_PROGRESS;
2049
2050 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* user-user */
2053 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002056 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057}
2058
2059static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2060{
2061 struct gsm_mncc *connect = arg;
2062 struct msgb *msg = gsm48_msgb_alloc();
2063 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2064
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065 gh->msg_type = GSM48_MT_CC_CONNECT;
2066
2067 gsm48_stop_cc_timer(trans);
2068 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2069
2070 /* facility */
2071 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002072 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 /* progress */
2074 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 /* connected number */
2077 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002078 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 /* user-user */
2080 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002081 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082
2083 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2084
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002085 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002086}
2087
2088static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2089{
2090 struct gsm48_hdr *gh = msgb_l3(msg);
2091 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2092 struct tlv_parsed tp;
2093 struct gsm_mncc connect;
2094
2095 gsm48_stop_cc_timer(trans);
2096
2097 memset(&connect, 0, sizeof(struct gsm_mncc));
2098 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002099 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002101 connect.fields |= MNCC_F_CONNECTED;
2102 strncpy(connect.connected.number, trans->subscr->extension,
2103 sizeof(connect.connected.number)-1);
2104 strncpy(connect.imsi, trans->subscr->imsi,
2105 sizeof(connect.imsi)-1);
2106
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 /* facility */
2108 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2109 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2112 }
2113 /* user-user */
2114 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2115 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002116 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002117 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2118 }
2119 /* ss-version */
2120 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2121 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2124 }
2125
2126 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002127 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128
Harald Welte596fed42009-07-23 19:06:52 +02002129 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130}
2131
2132
2133static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2134{
2135 struct gsm_mncc connect_ack;
2136
2137 gsm48_stop_cc_timer(trans);
2138
2139 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002140 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141
2142 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2143 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002144
Harald Welte596fed42009-07-23 19:06:52 +02002145 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146 &connect_ack);
2147}
2148
2149static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2150{
2151 struct msgb *msg = gsm48_msgb_alloc();
2152 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2153
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2155
2156 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2157
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002158 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159}
2160
2161static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2162{
2163 struct gsm48_hdr *gh = msgb_l3(msg);
2164 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2165 struct tlv_parsed tp;
2166 struct gsm_mncc disc;
2167
2168 gsm48_stop_cc_timer(trans);
2169
2170 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2171
2172 memset(&disc, 0, sizeof(struct gsm_mncc));
2173 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002174 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175 /* cause */
2176 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2177 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002178 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2180 }
2181 /* facility */
2182 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2183 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002184 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002185 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2186 }
2187 /* user-user */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2189 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002190 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2192 }
2193 /* ss-version */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2195 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002196 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2198 }
2199
Harald Welte596fed42009-07-23 19:06:52 +02002200 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201
2202}
2203
Harald Weltec66b71c2009-06-11 14:23:20 +08002204static struct gsm_mncc_cause default_cause = {
2205 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2206 .coding = 0,
2207 .rec = 0,
2208 .rec_val = 0,
2209 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2210 .diag_len = 0,
2211 .diag = { 0 },
2212};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213
2214static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2215{
2216 struct gsm_mncc *disc = arg;
2217 struct msgb *msg = gsm48_msgb_alloc();
2218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2219
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2221
2222 gsm48_stop_cc_timer(trans);
2223 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2224
2225 /* cause */
2226 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002227 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228 else
Harald Welte55c8f352010-03-07 23:40:35 +01002229 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230
2231 /* facility */
2232 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002233 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 /* progress */
2235 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 /* user-user */
2238 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002239 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240
2241 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002242 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243
2244 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2245
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002246 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247}
2248
2249static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2250{
2251 struct gsm48_hdr *gh = msgb_l3(msg);
2252 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2253 struct tlv_parsed tp;
2254 struct gsm_mncc rel;
2255 int rc;
2256
2257 gsm48_stop_cc_timer(trans);
2258
2259 memset(&rel, 0, sizeof(struct gsm_mncc));
2260 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002261 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 /* cause */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2264 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_decode_cause(&rel.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 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002271 gsm48_decode_facility(&rel.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 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_decode_useruser(&rel.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 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002283 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2285 }
2286
Harald Weltedcaf5652009-07-23 18:56:43 +02002287 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002289 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002291 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002292 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002293 GSM48_MT_CC_RELEASE_COMPL);
2294 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 }
2296
2297 new_cc_state(trans, GSM_CSTATE_NULL);
2298
2299 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002300 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301
2302 return rc;
2303}
2304
2305static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2306{
2307 struct gsm_mncc *rel = arg;
2308 struct msgb *msg = gsm48_msgb_alloc();
2309 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2310
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311 gh->msg_type = GSM48_MT_CC_RELEASE;
2312
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 gsm48_stop_cc_timer(trans);
2314 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2315
2316 /* cause */
2317 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002318 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319 /* facility */
2320 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002321 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 /* user-user */
2323 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002324 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325
Harald Weltedcaf5652009-07-23 18:56:43 +02002326 trans->cc.T308_second = 0;
2327 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328
Harald Weltedcaf5652009-07-23 18:56:43 +02002329 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2331
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002332 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333}
2334
2335static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2336{
2337 struct gsm48_hdr *gh = msgb_l3(msg);
2338 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2339 struct tlv_parsed tp;
2340 struct gsm_mncc rel;
2341 int rc = 0;
2342
2343 gsm48_stop_cc_timer(trans);
2344
2345 memset(&rel, 0, sizeof(struct gsm_mncc));
2346 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002347 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 /* cause */
2349 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2350 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002351 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2353 }
2354 /* facility */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2356 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002357 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2359 }
2360 /* user-user */
2361 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2362 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002363 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2365 }
2366 /* ss-version */
2367 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2368 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002369 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2371 }
2372
2373 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002374 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002376 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377 MNCC_REJ_IND, &rel);
2378 break;
2379 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002380 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 MNCC_REL_CNF, &rel);
2382 break;
2383 default:
Harald Welte596fed42009-07-23 19:06:52 +02002384 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385 MNCC_REL_IND, &rel);
2386 }
2387 }
2388
2389 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002390 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391
2392 return rc;
2393}
2394
2395static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2396{
2397 struct gsm_mncc *rel = arg;
2398 struct msgb *msg = gsm48_msgb_alloc();
2399 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002400 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2403
2404 trans->callref = 0;
2405
2406 gsm48_stop_cc_timer(trans);
2407
2408 /* cause */
2409 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002410 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 /* facility */
2412 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002413 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 /* user-user */
2415 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002416 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417
Harald Weltec7782de2010-12-21 19:31:41 +01002418 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2419
Harald Weltedcaf5652009-07-23 18:56:43 +02002420 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421
Harald Weltec7782de2010-12-21 19:31:41 +01002422 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002423}
2424
2425static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2426{
2427 struct gsm48_hdr *gh = msgb_l3(msg);
2428 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2429 struct tlv_parsed tp;
2430 struct gsm_mncc fac;
2431
2432 memset(&fac, 0, sizeof(struct gsm_mncc));
2433 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002434 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435 /* facility */
2436 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2437 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2440 }
2441 /* ss-version */
2442 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2443 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002444 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2446 }
2447
Harald Welte596fed42009-07-23 19:06:52 +02002448 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002449}
2450
2451static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2452{
2453 struct gsm_mncc *fac = arg;
2454 struct msgb *msg = gsm48_msgb_alloc();
2455 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2456
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 gh->msg_type = GSM48_MT_CC_FACILITY;
2458
2459 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002460 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002462 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2466{
2467 struct gsm_mncc hold;
2468
2469 memset(&hold, 0, sizeof(struct gsm_mncc));
2470 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002471 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002472}
2473
2474static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2475{
2476 struct msgb *msg = gsm48_msgb_alloc();
2477 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2478
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2480
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002481 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482}
2483
2484static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2485{
2486 struct gsm_mncc *hold_rej = arg;
2487 struct msgb *msg = gsm48_msgb_alloc();
2488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2489
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2491
2492 /* cause */
2493 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002494 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 else
Harald Welte55c8f352010-03-07 23:40:35 +01002496 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002498 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2502{
2503 struct gsm_mncc retrieve;
2504
2505 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2506 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002507 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2508 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509}
2510
2511static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2512{
2513 struct msgb *msg = gsm48_msgb_alloc();
2514 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2515
Harald Welte4bfdfe72009-06-10 23:11:52 +08002516 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2517
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002518 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2522{
2523 struct gsm_mncc *retrieve_rej = arg;
2524 struct msgb *msg = gsm48_msgb_alloc();
2525 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2526
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2528
2529 /* cause */
2530 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002531 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532 else
Harald Welte55c8f352010-03-07 23:40:35 +01002533 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002535 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2539{
2540 struct gsm48_hdr *gh = msgb_l3(msg);
2541 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2542 struct tlv_parsed tp;
2543 struct gsm_mncc dtmf;
2544
2545 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2546 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002547 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002548 /* keypad facility */
2549 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2550 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002551 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2553 }
2554
Harald Welte596fed42009-07-23 19:06:52 +02002555 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002556}
2557
2558static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2559{
2560 struct gsm_mncc *dtmf = arg;
2561 struct msgb *msg = gsm48_msgb_alloc();
2562 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2563
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2565
2566 /* keypad */
2567 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002568 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002569
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002570 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571}
2572
2573static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2574{
2575 struct gsm_mncc *dtmf = arg;
2576 struct msgb *msg = gsm48_msgb_alloc();
2577 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2578
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2580
2581 /* cause */
2582 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002583 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584 else
Harald Welte55c8f352010-03-07 23:40:35 +01002585 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002587 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002588}
2589
2590static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2591{
2592 struct msgb *msg = gsm48_msgb_alloc();
2593 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2594
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2596
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002597 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598}
2599
2600static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2601{
2602 struct gsm_mncc dtmf;
2603
2604 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2605 dtmf.callref = trans->callref;
2606
Harald Welte596fed42009-07-23 19:06:52 +02002607 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002608}
2609
2610static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2611{
2612 struct gsm48_hdr *gh = msgb_l3(msg);
2613 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2614 struct tlv_parsed tp;
2615 struct gsm_mncc modify;
2616
2617 memset(&modify, 0, sizeof(struct gsm_mncc));
2618 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002619 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620 /* bearer capability */
2621 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2622 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002623 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2625 }
2626
2627 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2628
Harald Welte596fed42009-07-23 19:06:52 +02002629 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630}
2631
2632static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2633{
2634 struct gsm_mncc *modify = arg;
2635 struct msgb *msg = gsm48_msgb_alloc();
2636 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2637
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 gh->msg_type = GSM48_MT_CC_MODIFY;
2639
2640 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2641
2642 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002643 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002644
2645 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2646
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002647 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2651{
2652 struct gsm48_hdr *gh = msgb_l3(msg);
2653 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2654 struct tlv_parsed tp;
2655 struct gsm_mncc modify;
2656
2657 gsm48_stop_cc_timer(trans);
2658
2659 memset(&modify, 0, sizeof(struct gsm_mncc));
2660 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002661 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662 /* bearer capability */
2663 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2664 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002665 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2667 }
2668
2669 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2670
Harald Welte596fed42009-07-23 19:06:52 +02002671 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002672}
2673
2674static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2675{
2676 struct gsm_mncc *modify = arg;
2677 struct msgb *msg = gsm48_msgb_alloc();
2678 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2679
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2681
2682 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002683 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684
2685 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2686
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002687 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688}
2689
2690static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2691{
2692 struct gsm48_hdr *gh = msgb_l3(msg);
2693 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2694 struct tlv_parsed tp;
2695 struct gsm_mncc modify;
2696
2697 gsm48_stop_cc_timer(trans);
2698
2699 memset(&modify, 0, sizeof(struct gsm_mncc));
2700 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002701 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 /* bearer capability */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2704 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002705 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2707 }
2708 /* cause */
2709 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2710 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002711 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2713 }
2714
2715 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2716
Harald Welte596fed42009-07-23 19:06:52 +02002717 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718}
2719
2720static int gsm48_cc_tx_modify_reject(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_REJECT;
2727
2728 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002729 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002731 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002732
2733 new_cc_state(trans, GSM_CSTATE_ACTIVE);
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_tx_notify(struct gsm_trans *trans, void *arg)
2739{
2740 struct gsm_mncc *notify = arg;
2741 struct msgb *msg = gsm48_msgb_alloc();
2742 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2743
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 gh->msg_type = GSM48_MT_CC_NOTIFY;
2745
2746 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002747 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002749 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750}
2751
2752static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2753{
2754 struct gsm48_hdr *gh = msgb_l3(msg);
2755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2756// struct tlv_parsed tp;
2757 struct gsm_mncc notify;
2758
2759 memset(&notify, 0, sizeof(struct gsm_mncc));
2760 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002761// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002763 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764
Harald Welte596fed42009-07-23 19:06:52 +02002765 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766}
2767
2768static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2769{
2770 struct gsm_mncc *user = arg;
2771 struct msgb *msg = gsm48_msgb_alloc();
2772 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2773
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 gh->msg_type = GSM48_MT_CC_USER_INFO;
2775
2776 /* user-user */
2777 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002778 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779 /* more data */
2780 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002781 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002783 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784}
2785
2786static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2787{
2788 struct gsm48_hdr *gh = msgb_l3(msg);
2789 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2790 struct tlv_parsed tp;
2791 struct gsm_mncc user;
2792
2793 memset(&user, 0, sizeof(struct gsm_mncc));
2794 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002795 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002796 /* user-user */
2797 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2798 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002799 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2801 }
2802 /* more data */
2803 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2804 user.more = 1;
2805
Harald Welte596fed42009-07-23 19:06:52 +02002806 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807}
2808
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002809static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810{
2811 struct gsm_mncc *mode = arg;
2812
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002813 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814}
2815
2816static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002817 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 int type;
2819 int (*rout) (struct gsm_trans *trans, void *arg);
2820} downstatelist[] = {
2821 /* mobile originating call establishment */
2822 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2823 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2824 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2825 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2826 {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 */
2827 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2828 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2829 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2830 /* mobile terminating call establishment */
2831 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2832 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2833 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2834 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2835 /* signalling during call */
2836 {SBIT(GSM_CSTATE_ACTIVE),
2837 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2838 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2839 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2840 {ALL_STATES,
2841 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2842 {ALL_STATES,
2843 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2844 {ALL_STATES,
2845 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2846 {SBIT(GSM_CSTATE_ACTIVE),
2847 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2848 {SBIT(GSM_CSTATE_ACTIVE),
2849 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2850 {SBIT(GSM_CSTATE_ACTIVE),
2851 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2852 {SBIT(GSM_CSTATE_ACTIVE),
2853 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2854 {SBIT(GSM_CSTATE_ACTIVE),
2855 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2856 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2857 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2858 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2859 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2860 {SBIT(GSM_CSTATE_ACTIVE),
2861 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2862 /* clearing */
2863 {SBIT(GSM_CSTATE_INITIATED),
2864 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2865 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2866 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2867 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2868 MNCC_REL_REQ, gsm48_cc_tx_release},
2869 /* special */
2870 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002871 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872};
2873
2874#define DOWNSLLEN \
2875 (sizeof(downstatelist) / sizeof(struct downstate))
2876
2877
Harald Welte76556372010-12-22 23:57:45 +01002878int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879{
Harald Welte1a6f7982009-08-09 18:52:33 +02002880 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002882 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002883 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 struct gsm_mncc *data = arg, rel;
2885
Harald Welte04dc88f2010-12-22 21:45:05 +01002886 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2887
Harald Welte4bfdfe72009-06-10 23:11:52 +08002888 /* handle special messages */
2889 switch(msg_type) {
2890 case MNCC_BRIDGE:
2891 return tch_bridge(net, arg);
2892 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002893 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002895 return tch_recv_mncc(net, data->callref, 1);
2896 case GSM_TCHF_FRAME:
2897 /* Find callref */
2898 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002899 if (!trans) {
2900 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002901 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002902 }
2903 if (!trans->conn) {
2904 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002905 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002906 }
2907 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2908 /* This should be LOGL_ERROR or NOTICE, but
2909 * unfortuantely it happens for a couple of frames at
2910 * the beginning of every RTP connection */
2911 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002912 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002913 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002914 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002915 switch (bts->type) {
2916 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002917 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002918 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002919 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002920 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002921 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002922 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002923 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002924 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002925 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002926 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002927 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002928 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002929 }
2930 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002931 }
2932
2933 memset(&rel, 0, sizeof(struct gsm_mncc));
2934 rel.callref = data->callref;
2935
2936 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002937 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002938
2939 /* Callref unknown */
2940 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002941 struct gsm_subscriber *subscr;
2942
Harald Welte4a3464c2009-07-04 10:11:24 +02002943 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2945 "Received '%s' from MNCC with "
2946 "unknown callref %d\n", data->called.number,
2947 get_mncc_name(msg_type), data->callref);
2948 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002949 return mncc_release_ind(net, NULL, data->callref,
2950 GSM48_CAUSE_LOC_PRN_S_LU,
2951 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002953 if (!data->called.number[0] && !data->imsi[0]) {
2954 DEBUGP(DCC, "(bts - trx - ts - ti) "
2955 "Received '%s' from MNCC with "
2956 "no number or IMSI\n", get_mncc_name(msg_type));
2957 /* Invalid number */
2958 return mncc_release_ind(net, NULL, data->callref,
2959 GSM48_CAUSE_LOC_PRN_S_LU,
2960 GSM48_CC_CAUSE_INV_NR_FORMAT);
2961 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002962 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002963 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002964 subscr = subscr_get_by_extension(net,
2965 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002966 else
Harald Welte9176bd42009-07-23 18:46:00 +02002967 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002968 /* If subscriber is not found */
2969 if (!subscr) {
2970 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2971 "Received '%s' from MNCC with "
2972 "unknown subscriber %s\n", data->called.number,
2973 get_mncc_name(msg_type), data->called.number);
2974 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002975 return mncc_release_ind(net, NULL, data->callref,
2976 GSM48_CAUSE_LOC_PRN_S_LU,
2977 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978 }
2979 /* If subscriber is not "attached" */
2980 if (!subscr->lac) {
2981 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2982 "Received '%s' from MNCC with "
2983 "detached subscriber %s\n", data->called.number,
2984 get_mncc_name(msg_type), data->called.number);
2985 subscr_put(subscr);
2986 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002987 return mncc_release_ind(net, NULL, data->callref,
2988 GSM48_CAUSE_LOC_PRN_S_LU,
2989 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990 }
2991 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002992 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2993 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994 DEBUGP(DCC, "No memory for trans.\n");
2995 subscr_put(subscr);
2996 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002997 mncc_release_ind(net, NULL, data->callref,
2998 GSM48_CAUSE_LOC_PRN_S_LU,
2999 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003000 return -ENOMEM;
3001 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003003 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003004
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003006 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003007 /* find transaction with this subscriber already paging */
3008 llist_for_each_entry(transt, &net->trans_list, entry) {
3009 /* Transaction of our lchan? */
3010 if (transt == trans ||
3011 transt->subscr != subscr)
3012 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003013 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003014 "Received '%s' from MNCC with "
3015 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003016 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003018 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003019 subscr_put(subscr);
3020 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003021 return 0;
3022 }
3023 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003024 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003025
3026 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003027 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3028 if (!trans->paging_request) {
3029 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3030 subscr_put(subscr);
3031 trans_free(trans);
3032 return 0;
3033 }
3034
3035 *trans->paging_request = subscr->net;
3036 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003037
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003038 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039 return 0;
3040 }
3041 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003042 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003043 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003045
3046 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003047 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048
3049 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003050 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003051 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003052 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053 (trans->subscr)?(trans->subscr->extension):"-",
3054 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003055 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3056 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003057 if (msg_type == MNCC_REL_REQ)
3058 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3059 else
3060 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3061 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003062 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 return rc;
3064 }
3065
3066 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3067 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003068 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003070 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003071 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003072 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003073
3074 /* Find function for current state and message */
3075 for (i = 0; i < DOWNSLLEN; i++)
3076 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003077 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 break;
3079 if (i == DOWNSLLEN) {
3080 DEBUGP(DCC, "Message unhandled at this state.\n");
3081 return 0;
3082 }
3083
3084 rc = downstatelist[i].rout(trans, arg);
3085
3086 return rc;
3087}
3088
3089
3090static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003091 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092 int type;
3093 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3094} datastatelist[] = {
3095 /* mobile originating call establishment */
3096 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3097 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3098 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3099 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3100 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3101 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3102 /* mobile terminating call establishment */
3103 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3104 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3105 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3106 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003107 {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 +08003108 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3109 /* signalling during call */
3110 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3111 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3112 {SBIT(GSM_CSTATE_ACTIVE),
3113 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3114 {ALL_STATES,
3115 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3116 {ALL_STATES,
3117 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3118 {ALL_STATES,
3119 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3120 {SBIT(GSM_CSTATE_ACTIVE),
3121 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3122 {SBIT(GSM_CSTATE_ACTIVE),
3123 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3124 {SBIT(GSM_CSTATE_ACTIVE),
3125 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3126 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3127 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3128 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3129 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3130 {SBIT(GSM_CSTATE_ACTIVE),
3131 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3132 /* clearing */
3133 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3134 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3135 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3136 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3137 {ALL_STATES, /* 5.4.3.4 */
3138 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3139};
3140
3141#define DATASLLEN \
3142 (sizeof(datastatelist) / sizeof(struct datastate))
3143
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003144static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003145{
3146 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003147 uint8_t msg_type = gh->msg_type & 0xbf;
3148 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003149 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003151
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 if (msg_type & 0x80) {
3153 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3154 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003155 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003156
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003158 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003159
Harald Welte6f5aee02009-07-23 21:21:14 +02003160 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003161 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003162 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003163 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003164 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3165 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166
3167 /* Create transaction */
3168 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003169 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003170 "creating new trans.\n", transaction_id);
3171 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003172 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003173 transaction_id, new_callref++);
3174 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003175 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003176 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003177 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003178 GSM48_MT_CC_RELEASE_COMPL);
3179 return -ENOMEM;
3180 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003181 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003182 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003183 }
3184
3185 /* find function for current state and message */
3186 for (i = 0; i < DATASLLEN; i++)
3187 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003188 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003189 break;
3190 if (i == DATASLLEN) {
3191 DEBUGP(DCC, "Message unhandled at this state.\n");
3192 return 0;
3193 }
3194
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003195 assert(trans->subscr);
3196
Harald Welte4bfdfe72009-06-10 23:11:52 +08003197 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003198
3199 return rc;
3200}
3201
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003202/* Create a dummy to wait five seconds */
3203static void release_anchor(struct gsm_subscriber_connection *conn)
3204{
3205 if (!conn->anch_operation)
3206 return;
3207
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003208 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003209 talloc_free(conn->anch_operation);
3210 conn->anch_operation = NULL;
3211}
3212
3213static void anchor_timeout(void *_data)
3214{
3215 struct gsm_subscriber_connection *con = _data;
3216
3217 release_anchor(con);
3218 msc_release_connection(con);
3219}
3220
3221int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3222{
3223 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3224 if (!conn->anch_operation)
3225 return -1;
3226
3227 conn->anch_operation->timeout.data = conn;
3228 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003229 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003230 return 0;
3231}
3232
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003233/* here we get data from the BSC level... */
3234int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003235{
3236 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003237 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003238 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003239
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003240 if (silent_call_reroute(conn, msg))
3241 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003242
3243 switch (pdisc) {
3244 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003245 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003246 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003247 break;
3248 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003249 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003250 break;
3251 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003252 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003253 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003254 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003255 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003256 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003257 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003258 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003259 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003260 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3261 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003262 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003263 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003264 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003265 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003266 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003267 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003268 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3269 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003270 break;
3271 }
3272
3273 return rc;
3274}
Harald Welte8470bf22008-12-25 23:28:35 +00003275
Harald Welte805f6442009-07-28 18:25:29 +02003276/*
3277 * This will be ran by the linker when loading the DSO. We use it to
3278 * do system initialization, e.g. registration of signal handlers.
3279 */
3280static __attribute__((constructor)) void on_dso_load_0408(void)
3281{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003282 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003283}