blob: 7f38be28eeb4404b3898cee2cd0920b2d3fa334b [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
832 switch (event) {
833 case GSM_SECURITY_AUTH_FAILED:
834 /* Nothing to do */
835 break;
836
837 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200838 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800839 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100840 break;
841
842 case GSM_SECURITY_SUCCEEDED:
843 /* nothing to do. CIPHER MODE COMMAND is
844 * implicit CM SERV ACK */
845 break;
846
847 default:
848 rc = -EINVAL;
849 };
850
851 return rc;
852}
853
Harald Welte4ed0e922009-01-10 03:17:30 +0000854/*
855 * Handle CM Service Requests
856 * a) Verify that the packet is long enough to contain the information
857 * we require otherwsie reject with INCORRECT_MESSAGE
858 * b) Try to parse the TMSI. If we do not have one reject
859 * c) Check that we know the subscriber with the TMSI otherwise reject
860 * with a HLR cause
861 * d) Set the subscriber on the gsm_lchan and accept
862 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800863static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000864{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200865 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200866 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000867
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800868 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000869 struct gsm_subscriber *subscr;
870 struct gsm48_hdr *gh = msgb_l3(msg);
871 struct gsm48_service_request *req =
872 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800873 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200874 uint8_t classmark2_len = gh->data[1];
875 uint8_t *classmark2 = gh->data+2;
876 uint8_t mi_len = *(classmark2 + classmark2_len);
877 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000878
Harald Weltec9e02182009-05-01 19:07:53 +0000879 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000880 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000881 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800882 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000883 GSM48_REJECT_INCORRECT_MESSAGE);
884 }
885
886 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000887 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800888 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000889 GSM48_REJECT_INCORRECT_MESSAGE);
890 }
891
Harald Weltec9e02182009-05-01 19:07:53 +0000892 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000893 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000894 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800895 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000896 GSM48_REJECT_INCORRECT_MESSAGE);
897 }
898
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200899 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000900 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000901 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000902
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200903 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100904
Harald Welte3ac7f102009-08-10 10:12:45 +0200905 if (is_siemens_bts(bts))
906 send_siemens_mrpci(msg->lchan, classmark2-1);
907
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200908 subscr = subscr_get_by_tmsi(bts->network,
909 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000910
Harald Welte2a139372009-02-22 21:14:55 +0000911 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000912 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800913 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000914 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
Holger Hans Peter Freyther4d2a68c2013-01-01 17:18:27 +0100915 if (subscr->lac == GSM_LAC_RESERVED_DETACHED)
Sylvain Munaut11c1b6e2012-12-28 00:33:04 +0100916 /* If the subscriber is not attached, reject service */
917 return gsm48_tx_mm_serv_rej(conn,
918 GSM48_REJECT_IMSI_UNKNOWN_IN_VLR);
Harald Welte4ed0e922009-01-10 03:17:30 +0000919
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800920 if (!conn->subscr)
921 conn->subscr = subscr;
922 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100923 subscr_put(subscr); /* lchan already has a ref, don't need another one */
924 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000925 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
926 subscr_put(subscr);
927 }
928
Harald Weltec2e302d2009-07-05 14:08:13 +0200929 subscr->equipment.classmark2_len = classmark2_len;
930 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
931 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000932
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800933 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100934 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000935}
936
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200937static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000938{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200939 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000940 struct gsm48_hdr *gh = msgb_l3(msg);
941 struct gsm48_imsi_detach_ind *idi =
942 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200943 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200944 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800945 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000946
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200947 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100948 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000949 mi_type, mi_string);
950
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200951 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100952
Harald Welte2a139372009-02-22 21:14:55 +0000953 switch (mi_type) {
954 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100955 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200956 subscr = subscr_get_by_tmsi(bts->network,
957 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000958 break;
959 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100960 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +0200961 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000962 break;
963 case GSM_MI_TYPE_IMEI:
964 case GSM_MI_TYPE_IMEISV:
965 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +0100966 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000967 break;
968 default:
Daniel Willmann977cd132012-12-28 19:29:44 +0100969 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000970 break;
971 }
972
Holger Freyther4a49e772009-04-12 05:37:29 +0000973 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200974 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000975 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100976 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200977
978 subscr->equipment.classmark1 = idi->classmark1;
979 db_sync_equipment(&subscr->equipment);
980
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000981 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000982 } else
Harald Welte2a139372009-02-22 21:14:55 +0000983 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
984
Harald Welte31981a02009-12-20 09:58:40 +0100985 /* FIXME: iterate over all transactions and release them,
986 * imagine an IMSI DETACH happening during an active call! */
987
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200988 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +0000989 return 0;
990}
991
Harald Welted2a7f5a2009-06-05 20:08:20 +0000992static int gsm48_rx_mm_status(struct msgb *msg)
993{
994 struct gsm48_hdr *gh = msgb_l3(msg);
995
996 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
997
998 return 0;
999}
1000
Sylvain Munaut30a15382009-12-24 00:27:26 +01001001/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001002static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001003{
1004 struct gsm48_hdr *gh = msgb_l3(msg);
1005 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001006 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001007
Sylvain Munautc593cf12010-06-10 23:51:41 +02001008 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001009 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001010
1011 /* Safety check */
1012 if (!conn->sec_operation) {
1013 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1014 return -EIO;
1015 }
1016
1017 /* Validate SRES */
1018 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001019 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001020 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001021
1022 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001023 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001024
Sylvain Munaut30a15382009-12-24 00:27:26 +01001025 if (cb)
1026 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001027 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001028
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001029 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001030 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001031 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001032 }
1033
Sylvain Munautc593cf12010-06-10 23:51:41 +02001034 DEBUGPC(DMM, "OK\n");
1035
Sylvain Munaut30a15382009-12-24 00:27:26 +01001036 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001037 return gsm0808_cipher_mode(conn, net->a5_encryption,
1038 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001039}
1040
Harald Weltebf5e8df2009-02-03 12:59:45 +00001041/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001042static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001043{
1044 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001045 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001046
1047 switch (gh->msg_type & 0xbf) {
1048 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001049 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001050 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001051 break;
1052 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001053 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001054 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001055 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001056 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001057 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001058 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001059 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001060 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001061 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001062 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001063 conn->subscr ?
1064 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001065 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001066 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001067 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001068 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001069 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001070 break;
1071 case GSM48_MT_MM_CM_REEST_REQ:
1072 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1073 break;
1074 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001075 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001076 break;
1077 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001078 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001079 gh->msg_type);
1080 break;
1081 }
1082
1083 return rc;
1084}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001085
Harald Welte2d35ae62009-02-06 12:02:13 +00001086/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001087static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001088{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001089 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001090 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001091 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001092 uint8_t *classmark2_lv = gh->data + 1;
1093 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001094 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001095 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001096 int rc = 0;
1097
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001098 resp = (struct gsm48_pag_resp *) &gh->data[0];
1099 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1100 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001101 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1102 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001103
Harald Weltefe18d8f2009-02-22 21:14:24 +00001104 switch (mi_type) {
1105 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001106 subscr = subscr_get_by_tmsi(bts->network,
1107 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001108 break;
1109 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001110 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001111 break;
1112 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001113
1114 if (!subscr) {
1115 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001116 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001117 return -EINVAL;
1118 }
1119 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001120 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001121
Harald Weltec2e302d2009-07-05 14:08:13 +02001122 subscr->equipment.classmark2_len = *classmark2_lv;
1123 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1124 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001125
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001126 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001127 return rc;
1128}
1129
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001130static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001131{
1132 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001133 uint8_t apdu_id_flags;
1134 uint8_t apdu_len;
1135 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001136
1137 apdu_id_flags = gh->data[0];
1138 apdu_len = gh->data[1];
1139 apdu_data = gh->data+2;
1140
Harald Welte70f92052012-07-16 13:22:19 +02001141 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001142 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001143
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001144 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145}
1146
Harald Weltebf5e8df2009-02-03 12:59:45 +00001147/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001148static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001149{
1150 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001151 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001152
1153 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001154 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001155 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001156 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001157 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001158 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001159 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001160 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001161 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001162 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001163 break;
1164 }
1165
Harald Welte2d35ae62009-02-06 12:02:13 +00001166 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001167}
1168
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001169int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1170 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001171{
1172 struct msgb *msg = gsm48_msgb_alloc();
1173 struct gsm48_hdr *gh;
1174
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001175 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001176
1177 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1178 apdu_id, apdu_len);
1179
1180 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1181 gh->proto_discr = GSM48_PDISC_RR;
1182 gh->msg_type = GSM48_MT_RR_APP_INFO;
1183 gh->data[0] = apdu_id;
1184 gh->data[1] = apdu_len;
1185 memcpy(gh->data+2, apdu, apdu_len);
1186
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001187 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001188}
1189
Harald Welte4bc90a12008-12-27 16:32:52 +00001190/* Call Control */
1191
Harald Welte7584aea2009-02-11 11:44:12 +00001192/* The entire call control code is written in accordance with Figure 7.10c
1193 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1194 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1195 * it for voice */
1196
Harald Welte4bfdfe72009-06-10 23:11:52 +08001197static void new_cc_state(struct gsm_trans *trans, int state)
1198{
1199 if (state > 31 || state < 0)
1200 return;
1201
1202 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001203 gsm48_cc_state_name(trans->cc.state),
1204 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001205
Harald Weltedcaf5652009-07-23 18:56:43 +02001206 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001207}
1208
1209static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001210{
1211 struct msgb *msg = gsm48_msgb_alloc();
1212 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001213 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001214
Harald Welte4bc90a12008-12-27 16:32:52 +00001215 gh->msg_type = GSM48_MT_CC_STATUS;
1216
1217 cause = msgb_put(msg, 3);
1218 cause[0] = 2;
1219 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1220 cause[2] = 0x80 | 30; /* response to status inquiry */
1221
1222 call_state = msgb_put(msg, 1);
1223 call_state[0] = 0xc0 | 0x00;
1224
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001225 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001226}
1227
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001228static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001229 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001230{
1231 struct msgb *msg = gsm48_msgb_alloc();
1232 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1233
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001234 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001235
Harald Welte6f4b7532008-12-29 00:39:37 +00001236 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001237 gh->msg_type = msg_type;
1238
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001239 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001240}
1241
Harald Welte4bfdfe72009-06-10 23:11:52 +08001242static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1243{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001244 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001245 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001246 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001247 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001248 }
1249}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001250
Harald Welte4bfdfe72009-06-10 23:11:52 +08001251static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1252 int msg_type, struct gsm_mncc *mncc)
1253{
1254 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001255 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001256
1257 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001258 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001259 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001260 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001261 trans->conn->lchan->ts->trx->bts->nr,
1262 trans->conn->lchan->ts->trx->nr,
1263 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264 (trans->subscr)?(trans->subscr->extension):"-",
1265 get_mncc_name(msg_type));
1266 else
1267 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1268 "Sending '%s' to MNCC.\n",
1269 (trans->subscr)?(trans->subscr->extension):"-",
1270 get_mncc_name(msg_type));
1271 else
1272 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1273 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1274
1275 mncc->msg_type = msg_type;
1276
Harald Welte966636f2009-06-26 19:39:35 +02001277 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001278 if (!msg)
1279 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001280
1281 data = msgb_put(msg, sizeof(struct gsm_mncc));
1282 memcpy(data, mncc, sizeof(struct gsm_mncc));
1283
Harald Welte54209c22010-12-22 23:43:29 +01001284 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285
1286 return 0;
1287}
1288
1289int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001290 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001291{
1292 struct gsm_mncc rel;
1293
Harald Welte92f70c52009-06-12 01:54:08 +08001294 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001296 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001297 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1298 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1300}
1301
Harald Weltedcaf5652009-07-23 18:56:43 +02001302/* Call Control Specific transaction release.
1303 * gets called by trans_free, DO NOT CALL YOURSELF! */
1304void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306 gsm48_stop_cc_timer(trans);
1307
1308 /* send release to L4, if callref still exists */
1309 if (trans->callref) {
1310 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001311 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001312 GSM48_CAUSE_LOC_PRN_S_LU,
1313 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001315 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001316 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001317 if (trans->conn)
1318 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001319}
1320
1321static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001322
Harald Welte09e38af2009-02-16 22:52:23 +00001323/* call-back from paging the B-end of the connection */
1324static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001325 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001326{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001327 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001328 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001329 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001331
Harald Welte09e38af2009-02-16 22:52:23 +00001332 if (hooknum != GSM_HOOK_RR_PAGING)
1333 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001334
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001335 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336 if (!net) {
1337 DEBUGP(DCC, "Error Network not set!\n");
1338 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001339 }
Harald Welte7584aea2009-02-11 11:44:12 +00001340
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341 /* check all tranactions (without lchan) for subscriber */
1342 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001343 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001344 continue;
1345 switch (event) {
1346 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001347 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 break;
1349 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001350 transt->subscr->extension);
1351 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001353 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001354 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001355 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001356 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001357 }
1358 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001359 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360 break;
1361 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001362 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001364 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001366 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001367 mncc_release_ind(transt->subscr->net, transt,
1368 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001369 GSM48_CAUSE_LOC_PRN_S_LU,
1370 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001371 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001372 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001373 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 break;
1375 }
1376 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001377
1378 talloc_free(paging_request);
1379
1380 /*
1381 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1382 * failure and then the subscr will be poke again. This needs a lot of fixing
1383 * in the subscriber queue code.
1384 */
1385 if (!found && conn)
1386 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001387 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001388}
Harald Welte7584aea2009-02-11 11:44:12 +00001389
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001390static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001391
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001392/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001393static int switch_for_handover(struct gsm_lchan *old_lchan,
1394 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001395{
1396 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001397
1398 if (ipacc_rtp_direct) {
1399 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1400 return 0;
1401 }
1402
1403 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001404 new_rs = new_lchan->abis_ip.rtp_socket;
1405 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001406
1407 if (!new_rs) {
1408 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1409 return -EIO;
1410 }
1411
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001412 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001413
1414 if (!old_rs) {
1415 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1416 return -EIO;
1417 }
1418
1419 /* copy rx_action and reference to other sock */
1420 new_rs->rx_action = old_rs->rx_action;
1421 new_rs->tx_action = old_rs->tx_action;
1422 new_rs->transmit = old_rs->transmit;
1423
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001424 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001425 case RTP_PROXY:
1426 other_rs = old_rs->proxy.other_sock;
1427 rtp_socket_proxy(new_rs, other_rs);
1428 /* delete reference to other end socket to prevent
1429 * rtp_socket_free() from removing the inverse reference */
1430 old_rs->proxy.other_sock = NULL;
1431 break;
1432 case RTP_RECV_UPSTREAM:
1433 new_rs->receive = old_rs->receive;
1434 break;
1435 case RTP_NONE:
1436 break;
1437 }
1438
1439 return 0;
1440}
1441
Harald Welte805f6442009-07-28 18:25:29 +02001442/* some other part of the code sends us a signal */
1443static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1444 void *handler_data, void *signal_data)
1445{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001446 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001447 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001448 struct gsm_network *net;
1449 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001450
1451 if (subsys != SS_ABISIP)
1452 return 0;
1453
1454 /* in case we use direct BTS-to-BTS RTP */
1455 if (ipacc_rtp_direct)
1456 return 0;
1457
Harald Welte805f6442009-07-28 18:25:29 +02001458 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001459 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001460 /* in case we don't use direct BTS-to-BTS RTP */
1461 /* the BTS has successfully bound a TCH to a local ip/port,
1462 * which means we can connect our UDP socket to it */
1463 if (lchan->abis_ip.rtp_socket) {
1464 rtp_socket_free(lchan->abis_ip.rtp_socket);
1465 lchan->abis_ip.rtp_socket = NULL;
1466 }
1467
1468 lchan->abis_ip.rtp_socket = rtp_socket_create();
1469 if (!lchan->abis_ip.rtp_socket)
1470 return -EIO;
1471
1472 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1473 lchan->abis_ip.bound_ip,
1474 lchan->abis_ip.bound_port);
1475 if (rc < 0)
1476 return -EIO;
1477
Harald Welteda7ab742009-12-19 22:23:05 +01001478 /* check if any transactions on this lchan still have
1479 * a tch_recv_mncc request pending */
1480 net = lchan->ts->trx->bts->network;
1481 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001482 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001483 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1484 tch_recv_mncc(net, trans->callref, 1);
1485 }
1486 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001487
1488 /*
1489 * TODO: this appears to be too early? Why not until after
1490 * the handover detect or the handover complete?
1491 *
1492 * Do we have a handover pending for this new lchan? In that
1493 * case re-route the audio from the old channel to the new one.
1494 */
1495 old_lchan = bsc_handover_pending(lchan);
1496 if (old_lchan)
1497 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001498 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001499 case S_ABISIP_DLCX_IND:
1500 /* the BTS tells us a RTP stream has been disconnected */
1501 if (lchan->abis_ip.rtp_socket) {
1502 rtp_socket_free(lchan->abis_ip.rtp_socket);
1503 lchan->abis_ip.rtp_socket = NULL;
1504 }
1505
1506 break;
Harald Welte805f6442009-07-28 18:25:29 +02001507 }
1508
1509 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001510}
1511
Harald Welte49f48b82009-02-17 15:29:33 +00001512/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001513static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001514{
Harald Welte11fa29c2009-02-19 17:24:39 +00001515 struct gsm_bts *bts = lchan->ts->trx->bts;
1516 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001517 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001518
Harald Welte11fa29c2009-02-19 17:24:39 +00001519 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1520 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1521 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1522
1523 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001524 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001525 return -EINVAL;
1526 }
Harald Welteda7ab742009-12-19 22:23:05 +01001527
1528 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001529 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001530 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001531 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001532 if (!ipacc_rtp_direct) {
1533 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001534 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001535 if (rc < 0)
1536 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001537 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001538 if (rc < 0)
1539 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001540 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301541 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1542 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001543 } else {
1544 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301545 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1546 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301547 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001548 if (rc < 0)
1549 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301550 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1551 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301552 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001553 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001554 break;
1555 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001556 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001557 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001558 trau_mux_map_lchan(lchan, remote_lchan);
1559 break;
1560 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001561 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001562 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001563 }
Harald Welte49f48b82009-02-17 15:29:33 +00001564
1565 return 0;
1566}
1567
Harald Welte4bfdfe72009-06-10 23:11:52 +08001568/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001569static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001570{
Harald Weltedcaf5652009-07-23 18:56:43 +02001571 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1572 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001573
Harald Welte4bfdfe72009-06-10 23:11:52 +08001574 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001575 return -EIO;
1576
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001577 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001578 return -EIO;
1579
1580 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001581 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001582}
1583
Harald Welteda7ab742009-12-19 22:23:05 +01001584/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001585static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586{
1587 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001588 struct gsm_lchan *lchan;
1589 struct gsm_bts *bts;
1590 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001591
Harald Welte4bfdfe72009-06-10 23:11:52 +08001592 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001593 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001594 if (!trans)
1595 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001596 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001598 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001599 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001600
Harald Welteda7ab742009-12-19 22:23:05 +01001601 switch (bts->type) {
1602 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001603 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001604 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001605 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001606 return -EINVAL;
1607 }
1608 /* in case, we don't have a RTP socket yet, we note this
1609 * in the transaction and try later */
1610 if (!lchan->abis_ip.rtp_socket) {
1611 trans->tch_recv = enable;
1612 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1613 return 0;
1614 }
1615 if (enable) {
1616 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001617 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001618 if (rc < 0)
1619 return rc;
1620 /* assign socket to application interface */
1621 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1622 net, callref);
1623 } else
1624 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1625 net, 0);
1626 break;
1627 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001628 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001629 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001630 if (enable)
1631 return trau_recv_lchan(lchan, callref);
1632 return trau_mux_unmap(NULL, callref);
1633 break;
1634 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001635 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001636 return -EINVAL;
1637 }
1638
1639 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640}
1641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1643{
1644 DEBUGP(DCC, "-> STATUS ENQ\n");
1645 return gsm48_cc_tx_status(trans, msg);
1646}
1647
1648static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1649static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1650
1651static void gsm48_cc_timeout(void *arg)
1652{
1653 struct gsm_trans *trans = arg;
1654 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001655 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1656 int mo_location = GSM48_CAUSE_LOC_USER;
1657 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1658 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001659 struct gsm_mncc mo_rel, l4_rel;
1660
1661 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1662 mo_rel.callref = trans->callref;
1663 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1664 l4_rel.callref = trans->callref;
1665
Harald Weltedcaf5652009-07-23 18:56:43 +02001666 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667 case 0x303:
1668 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001669 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001670 break;
1671 case 0x310:
1672 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001673 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674 break;
1675 case 0x313:
1676 disconnect = 1;
1677 /* unknown, did not find it in the specs */
1678 break;
1679 case 0x301:
1680 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001681 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 break;
1683 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001684 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001686 gsm48_cc_tx_release(trans, &trans->cc.msg);
1687 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688 break; /* stay in release state */
1689 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001690 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001691 return;
1692// release = 1;
1693// l4_cause = 14;
1694// break;
1695 case 0x306:
1696 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001697 mo_cause = trans->cc.msg.cause.value;
1698 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 break;
1700 case 0x323:
1701 disconnect = 1;
1702 break;
1703 default:
1704 release = 1;
1705 }
1706
1707 if (release && trans->callref) {
1708 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001709 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001710 l4_location, l4_cause);
1711 trans->callref = 0;
1712 }
1713
1714 if (disconnect && trans->callref) {
1715 /* process disconnect towards layer 4 */
1716 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001717 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 }
1719
1720 /* process disconnect towards mobile station */
1721 if (disconnect || release) {
1722 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001723 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1724 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1725 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 mo_rel.cause.diag_len = 3;
1727
1728 if (disconnect)
1729 gsm48_cc_tx_disconnect(trans, &mo_rel);
1730 if (release)
1731 gsm48_cc_tx_release(trans, &mo_rel);
1732 }
1733
1734}
1735
1736static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1737 int sec, int micro)
1738{
1739 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001740 trans->cc.timer.cb = gsm48_cc_timeout;
1741 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001742 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001743 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744}
1745
1746static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1747{
1748 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001749 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001750 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1751 struct tlv_parsed tp;
1752 struct gsm_mncc setup;
1753
1754 memset(&setup, 0, sizeof(struct gsm_mncc));
1755 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001756 if (trans->conn && trans->conn->lchan)
1757 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001758 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759 /* emergency setup is identified by msg_type */
1760 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1761 setup.emergency = 1;
1762
1763 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001764 setup.fields |= MNCC_F_CALLING;
1765 strncpy(setup.calling.number, trans->subscr->extension,
1766 sizeof(setup.calling.number)-1);
1767 strncpy(setup.imsi, trans->subscr->imsi,
1768 sizeof(setup.imsi)-1);
1769
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 /* bearer capability */
1771 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1772 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1775 }
1776 /* facility */
1777 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1778 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001779 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1781 }
1782 /* called party bcd number */
1783 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1784 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001785 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1787 }
1788 /* user-user */
1789 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1790 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001791 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1793 }
1794 /* ss-version */
1795 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1796 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001797 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1799 }
1800 /* CLIR suppression */
1801 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1802 setup.clir.sup = 1;
1803 /* CLIR invocation */
1804 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1805 setup.clir.inv = 1;
1806 /* cc cap */
1807 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1808 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001809 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1811 }
1812
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 new_cc_state(trans, GSM_CSTATE_INITIATED);
1814
Harald Welte (local)47df3992009-12-26 19:45:03 +01001815 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1816 subscr_name(trans->subscr), trans->subscr->extension,
1817 setup.called.number);
1818
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001819 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001820
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001822 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823
Harald Welte13cac662009-07-29 12:10:35 +02001824 /* MNCC code will modify the channel asynchronously, we should
1825 * ipaccess-bind only after the modification has been made to the
1826 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 return 0;
1828}
1829
1830static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001831{
1832 struct msgb *msg = gsm48_msgb_alloc();
1833 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001834 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001835 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001836
Harald Welte7ccf7782009-02-17 01:43:01 +00001837 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001838
Harald Welte4bfdfe72009-06-10 23:11:52 +08001839 /* transaction id must not be assigned */
1840 if (trans->transaction_id != 0xff) { /* unasssigned */
1841 DEBUGP(DCC, "TX Setup with assigned transaction. "
1842 "This is not allowed!\n");
1843 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001844 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001845 GSM48_CAUSE_LOC_PRN_S_LU,
1846 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001848 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 return rc;
1850 }
1851
1852 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001853 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1854 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001855 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001856 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001857 GSM48_CAUSE_LOC_PRN_S_LU,
1858 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001860 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861 return rc;
1862 }
Harald Welte78283ef2009-07-23 21:36:44 +02001863 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001864
Harald Welte65e74cc2008-12-29 01:55:35 +00001865 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001866
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001868
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 /* bearer capability */
1870 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 /* facility */
1873 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001874 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875 /* progress */
1876 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 /* calling party BCD number */
1879 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* called party BCD number */
1882 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 /* user-user */
1885 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 /* redirecting party BCD number */
1888 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 /* signal */
1891 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893
1894 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001895
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001896 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001897
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001898 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001899}
1900
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1902{
1903 struct gsm48_hdr *gh = msgb_l3(msg);
1904 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1905 struct tlv_parsed tp;
1906 struct gsm_mncc call_conf;
1907
1908 gsm48_stop_cc_timer(trans);
1909 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1910
1911 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1912 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001913 if (trans->conn && trans->conn->lchan)
1914 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001915 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916#if 0
1917 /* repeat */
1918 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1919 call_conf.repeat = 1;
1920 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1921 call_conf.repeat = 2;
1922#endif
1923 /* bearer capability */
1924 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1925 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1928 }
1929 /* cause */
1930 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1931 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001932 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1934 }
1935 /* cc cap */
1936 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1937 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001938 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1940 }
1941
1942 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1943
Harald Welte596fed42009-07-23 19:06:52 +02001944 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1945 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946}
1947
1948static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1949{
1950 struct gsm_mncc *proceeding = arg;
1951 struct msgb *msg = gsm48_msgb_alloc();
1952 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1953
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1955
1956 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1957
1958 /* bearer capability */
1959 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961 /* facility */
1962 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001963 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001964 /* progress */
1965 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001966 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001968 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969}
1970
1971static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1972{
1973 struct gsm48_hdr *gh = msgb_l3(msg);
1974 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1975 struct tlv_parsed tp;
1976 struct gsm_mncc alerting;
1977
1978 gsm48_stop_cc_timer(trans);
1979 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1980
1981 memset(&alerting, 0, sizeof(struct gsm_mncc));
1982 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001983 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 /* facility */
1985 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1986 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001987 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1989 }
1990
1991 /* progress */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1993 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001994 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1996 }
1997 /* ss-version */
1998 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1999 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2002 }
2003
2004 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2005
Harald Welte596fed42009-07-23 19:06:52 +02002006 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2007 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008}
2009
2010static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2011{
2012 struct gsm_mncc *alerting = arg;
2013 struct msgb *msg = gsm48_msgb_alloc();
2014 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2015
Harald Welte4bfdfe72009-06-10 23:11:52 +08002016 gh->msg_type = GSM48_MT_CC_ALERTING;
2017
2018 /* facility */
2019 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002020 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 /* progress */
2022 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002023 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002024 /* user-user */
2025 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002026 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027
2028 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2029
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002030 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031}
2032
2033static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2034{
2035 struct gsm_mncc *progress = arg;
2036 struct msgb *msg = gsm48_msgb_alloc();
2037 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2038
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 gh->msg_type = GSM48_MT_CC_PROGRESS;
2040
2041 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002042 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 /* user-user */
2044 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002045 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002047 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048}
2049
2050static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2051{
2052 struct gsm_mncc *connect = arg;
2053 struct msgb *msg = gsm48_msgb_alloc();
2054 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2055
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 gh->msg_type = GSM48_MT_CC_CONNECT;
2057
2058 gsm48_stop_cc_timer(trans);
2059 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2060
2061 /* facility */
2062 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002063 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002064 /* progress */
2065 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002066 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 /* connected number */
2068 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002069 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 /* user-user */
2071 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002072 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073
2074 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2075
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002076 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077}
2078
2079static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2080{
2081 struct gsm48_hdr *gh = msgb_l3(msg);
2082 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2083 struct tlv_parsed tp;
2084 struct gsm_mncc connect;
2085
2086 gsm48_stop_cc_timer(trans);
2087
2088 memset(&connect, 0, sizeof(struct gsm_mncc));
2089 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002090 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002092 connect.fields |= MNCC_F_CONNECTED;
2093 strncpy(connect.connected.number, trans->subscr->extension,
2094 sizeof(connect.connected.number)-1);
2095 strncpy(connect.imsi, trans->subscr->imsi,
2096 sizeof(connect.imsi)-1);
2097
Harald Welte4bfdfe72009-06-10 23:11:52 +08002098 /* facility */
2099 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2100 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002101 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2103 }
2104 /* user-user */
2105 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2106 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002107 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002108 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2109 }
2110 /* ss-version */
2111 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2112 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002113 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2115 }
2116
2117 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002118 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119
Harald Welte596fed42009-07-23 19:06:52 +02002120 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121}
2122
2123
2124static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2125{
2126 struct gsm_mncc connect_ack;
2127
2128 gsm48_stop_cc_timer(trans);
2129
2130 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002131 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132
2133 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2134 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002135
Harald Welte596fed42009-07-23 19:06:52 +02002136 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 &connect_ack);
2138}
2139
2140static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2141{
2142 struct msgb *msg = gsm48_msgb_alloc();
2143 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2144
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2146
2147 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2148
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002149 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002150}
2151
2152static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2153{
2154 struct gsm48_hdr *gh = msgb_l3(msg);
2155 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2156 struct tlv_parsed tp;
2157 struct gsm_mncc disc;
2158
2159 gsm48_stop_cc_timer(trans);
2160
2161 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2162
2163 memset(&disc, 0, sizeof(struct gsm_mncc));
2164 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002165 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 /* cause */
2167 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2168 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002169 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2171 }
2172 /* facility */
2173 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2174 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002175 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002176 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2177 }
2178 /* user-user */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2180 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2183 }
2184 /* ss-version */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2186 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2189 }
2190
Harald Welte596fed42009-07-23 19:06:52 +02002191 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192
2193}
2194
Harald Weltec66b71c2009-06-11 14:23:20 +08002195static struct gsm_mncc_cause default_cause = {
2196 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2197 .coding = 0,
2198 .rec = 0,
2199 .rec_val = 0,
2200 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2201 .diag_len = 0,
2202 .diag = { 0 },
2203};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204
2205static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2206{
2207 struct gsm_mncc *disc = arg;
2208 struct msgb *msg = gsm48_msgb_alloc();
2209 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2210
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2212
2213 gsm48_stop_cc_timer(trans);
2214 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2215
2216 /* cause */
2217 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002218 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219 else
Harald Welte55c8f352010-03-07 23:40:35 +01002220 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221
2222 /* facility */
2223 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 /* progress */
2226 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002227 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228 /* user-user */
2229 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231
2232 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002233 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234
2235 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2236
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002237 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238}
2239
2240static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2241{
2242 struct gsm48_hdr *gh = msgb_l3(msg);
2243 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2244 struct tlv_parsed tp;
2245 struct gsm_mncc rel;
2246 int rc;
2247
2248 gsm48_stop_cc_timer(trans);
2249
2250 memset(&rel, 0, sizeof(struct gsm_mncc));
2251 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002252 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 /* cause */
2254 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2255 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002256 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2258 }
2259 /* facility */
2260 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2261 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2264 }
2265 /* user-user */
2266 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2267 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002268 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2270 }
2271 /* ss-version */
2272 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2273 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2276 }
2277
Harald Weltedcaf5652009-07-23 18:56:43 +02002278 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002280 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002282 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002283 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002284 GSM48_MT_CC_RELEASE_COMPL);
2285 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286 }
2287
2288 new_cc_state(trans, GSM_CSTATE_NULL);
2289
2290 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002291 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292
2293 return rc;
2294}
2295
2296static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2297{
2298 struct gsm_mncc *rel = arg;
2299 struct msgb *msg = gsm48_msgb_alloc();
2300 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2301
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 gh->msg_type = GSM48_MT_CC_RELEASE;
2303
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 gsm48_stop_cc_timer(trans);
2305 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2306
2307 /* cause */
2308 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310 /* facility */
2311 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002312 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 /* user-user */
2314 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002315 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316
Harald Weltedcaf5652009-07-23 18:56:43 +02002317 trans->cc.T308_second = 0;
2318 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319
Harald Weltedcaf5652009-07-23 18:56:43 +02002320 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2322
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002323 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324}
2325
2326static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2327{
2328 struct gsm48_hdr *gh = msgb_l3(msg);
2329 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2330 struct tlv_parsed tp;
2331 struct gsm_mncc rel;
2332 int rc = 0;
2333
2334 gsm48_stop_cc_timer(trans);
2335
2336 memset(&rel, 0, sizeof(struct gsm_mncc));
2337 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002338 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339 /* cause */
2340 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2341 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2344 }
2345 /* facility */
2346 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2347 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002348 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002349 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2350 }
2351 /* user-user */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2353 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2356 }
2357 /* ss-version */
2358 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2359 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002360 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2362 }
2363
2364 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002365 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002367 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368 MNCC_REJ_IND, &rel);
2369 break;
2370 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002371 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 MNCC_REL_CNF, &rel);
2373 break;
2374 default:
Harald Welte596fed42009-07-23 19:06:52 +02002375 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 MNCC_REL_IND, &rel);
2377 }
2378 }
2379
2380 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002381 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382
2383 return rc;
2384}
2385
2386static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2387{
2388 struct gsm_mncc *rel = arg;
2389 struct msgb *msg = gsm48_msgb_alloc();
2390 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002391 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2394
2395 trans->callref = 0;
2396
2397 gsm48_stop_cc_timer(trans);
2398
2399 /* cause */
2400 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002401 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 /* facility */
2403 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002404 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 /* user-user */
2406 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002407 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408
Harald Weltec7782de2010-12-21 19:31:41 +01002409 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2410
Harald Weltedcaf5652009-07-23 18:56:43 +02002411 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412
Harald Weltec7782de2010-12-21 19:31:41 +01002413 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414}
2415
2416static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2417{
2418 struct gsm48_hdr *gh = msgb_l3(msg);
2419 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2420 struct tlv_parsed tp;
2421 struct gsm_mncc fac;
2422
2423 memset(&fac, 0, sizeof(struct gsm_mncc));
2424 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002425 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426 /* facility */
2427 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2428 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002429 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2431 }
2432 /* ss-version */
2433 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2434 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002435 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2437 }
2438
Harald Welte596fed42009-07-23 19:06:52 +02002439 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440}
2441
2442static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2443{
2444 struct gsm_mncc *fac = arg;
2445 struct msgb *msg = gsm48_msgb_alloc();
2446 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2447
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448 gh->msg_type = GSM48_MT_CC_FACILITY;
2449
2450 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002451 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002453 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454}
2455
2456static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2457{
2458 struct gsm_mncc hold;
2459
2460 memset(&hold, 0, sizeof(struct gsm_mncc));
2461 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002462 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2466{
2467 struct msgb *msg = gsm48_msgb_alloc();
2468 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2469
Harald Welte4bfdfe72009-06-10 23:11:52 +08002470 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2471
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002472 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473}
2474
2475static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2476{
2477 struct gsm_mncc *hold_rej = arg;
2478 struct msgb *msg = gsm48_msgb_alloc();
2479 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2480
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2482
2483 /* cause */
2484 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002485 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002486 else
Harald Welte55c8f352010-03-07 23:40:35 +01002487 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002488
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002489 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490}
2491
2492static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2493{
2494 struct gsm_mncc retrieve;
2495
2496 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2497 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002498 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2499 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500}
2501
2502static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2503{
2504 struct msgb *msg = gsm48_msgb_alloc();
2505 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2506
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2508
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002509 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510}
2511
2512static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2513{
2514 struct gsm_mncc *retrieve_rej = arg;
2515 struct msgb *msg = gsm48_msgb_alloc();
2516 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2517
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2519
2520 /* cause */
2521 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002522 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523 else
Harald Welte55c8f352010-03-07 23:40:35 +01002524 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002526 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527}
2528
2529static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2530{
2531 struct gsm48_hdr *gh = msgb_l3(msg);
2532 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2533 struct tlv_parsed tp;
2534 struct gsm_mncc dtmf;
2535
2536 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2537 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002538 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 /* keypad facility */
2540 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2541 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002542 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2544 }
2545
Harald Welte596fed42009-07-23 19:06:52 +02002546 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547}
2548
2549static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2550{
2551 struct gsm_mncc *dtmf = arg;
2552 struct msgb *msg = gsm48_msgb_alloc();
2553 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2554
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2556
2557 /* keypad */
2558 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002559 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002561 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562}
2563
2564static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2565{
2566 struct gsm_mncc *dtmf = arg;
2567 struct msgb *msg = gsm48_msgb_alloc();
2568 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2569
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2571
2572 /* cause */
2573 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002574 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 else
Harald Welte55c8f352010-03-07 23:40:35 +01002576 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002578 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579}
2580
2581static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2582{
2583 struct msgb *msg = gsm48_msgb_alloc();
2584 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2585
Harald Welte4bfdfe72009-06-10 23:11:52 +08002586 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2587
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002588 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589}
2590
2591static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2592{
2593 struct gsm_mncc dtmf;
2594
2595 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2596 dtmf.callref = trans->callref;
2597
Harald Welte596fed42009-07-23 19:06:52 +02002598 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599}
2600
2601static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2602{
2603 struct gsm48_hdr *gh = msgb_l3(msg);
2604 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2605 struct tlv_parsed tp;
2606 struct gsm_mncc modify;
2607
2608 memset(&modify, 0, sizeof(struct gsm_mncc));
2609 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002610 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611 /* bearer capability */
2612 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2613 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002614 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2616 }
2617
2618 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2619
Harald Welte596fed42009-07-23 19:06:52 +02002620 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621}
2622
2623static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2624{
2625 struct gsm_mncc *modify = arg;
2626 struct msgb *msg = gsm48_msgb_alloc();
2627 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2628
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 gh->msg_type = GSM48_MT_CC_MODIFY;
2630
2631 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2632
2633 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002634 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635
2636 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2637
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002638 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639}
2640
2641static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2642{
2643 struct gsm48_hdr *gh = msgb_l3(msg);
2644 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2645 struct tlv_parsed tp;
2646 struct gsm_mncc modify;
2647
2648 gsm48_stop_cc_timer(trans);
2649
2650 memset(&modify, 0, sizeof(struct gsm_mncc));
2651 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002652 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 /* bearer capability */
2654 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2655 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002656 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2658 }
2659
2660 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2661
Harald Welte596fed42009-07-23 19:06:52 +02002662 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663}
2664
2665static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2666{
2667 struct gsm_mncc *modify = arg;
2668 struct msgb *msg = gsm48_msgb_alloc();
2669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2670
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2672
2673 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675
2676 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2677
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002678 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679}
2680
2681static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2682{
2683 struct gsm48_hdr *gh = msgb_l3(msg);
2684 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2685 struct tlv_parsed tp;
2686 struct gsm_mncc modify;
2687
2688 gsm48_stop_cc_timer(trans);
2689
2690 memset(&modify, 0, sizeof(struct gsm_mncc));
2691 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002692 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693 /* bearer capability */
2694 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2695 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002696 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2698 }
2699 /* cause */
2700 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2701 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002702 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002703 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2704 }
2705
2706 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2707
Harald Welte596fed42009-07-23 19:06:52 +02002708 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709}
2710
2711static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2712{
2713 struct gsm_mncc *modify = arg;
2714 struct msgb *msg = gsm48_msgb_alloc();
2715 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2716
Harald Welte4bfdfe72009-06-10 23:11:52 +08002717 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2718
2719 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002720 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002722 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723
2724 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2725
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002726 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727}
2728
2729static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2730{
2731 struct gsm_mncc *notify = arg;
2732 struct msgb *msg = gsm48_msgb_alloc();
2733 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2734
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735 gh->msg_type = GSM48_MT_CC_NOTIFY;
2736
2737 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002738 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002740 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741}
2742
2743static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2744{
2745 struct gsm48_hdr *gh = msgb_l3(msg);
2746 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2747// struct tlv_parsed tp;
2748 struct gsm_mncc notify;
2749
2750 memset(&notify, 0, sizeof(struct gsm_mncc));
2751 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002752// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002754 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755
Harald Welte596fed42009-07-23 19:06:52 +02002756 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757}
2758
2759static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2760{
2761 struct gsm_mncc *user = arg;
2762 struct msgb *msg = gsm48_msgb_alloc();
2763 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2764
Harald Welte4bfdfe72009-06-10 23:11:52 +08002765 gh->msg_type = GSM48_MT_CC_USER_INFO;
2766
2767 /* user-user */
2768 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002769 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770 /* more data */
2771 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002772 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002773
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002774 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002775}
2776
2777static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2778{
2779 struct gsm48_hdr *gh = msgb_l3(msg);
2780 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2781 struct tlv_parsed tp;
2782 struct gsm_mncc user;
2783
2784 memset(&user, 0, sizeof(struct gsm_mncc));
2785 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002786 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002787 /* user-user */
2788 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2789 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002790 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2792 }
2793 /* more data */
2794 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2795 user.more = 1;
2796
Harald Welte596fed42009-07-23 19:06:52 +02002797 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798}
2799
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002800static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002801{
2802 struct gsm_mncc *mode = arg;
2803
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002804 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002805}
2806
2807static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002808 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809 int type;
2810 int (*rout) (struct gsm_trans *trans, void *arg);
2811} downstatelist[] = {
2812 /* mobile originating call establishment */
2813 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2814 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2815 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2816 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2817 {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 */
2818 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2819 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2820 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2821 /* mobile terminating call establishment */
2822 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2823 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2824 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2825 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2826 /* signalling during call */
2827 {SBIT(GSM_CSTATE_ACTIVE),
2828 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2829 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2830 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2831 {ALL_STATES,
2832 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2833 {ALL_STATES,
2834 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2835 {ALL_STATES,
2836 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2837 {SBIT(GSM_CSTATE_ACTIVE),
2838 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2839 {SBIT(GSM_CSTATE_ACTIVE),
2840 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2841 {SBIT(GSM_CSTATE_ACTIVE),
2842 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2843 {SBIT(GSM_CSTATE_ACTIVE),
2844 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2845 {SBIT(GSM_CSTATE_ACTIVE),
2846 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2847 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2848 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2849 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2850 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2851 {SBIT(GSM_CSTATE_ACTIVE),
2852 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2853 /* clearing */
2854 {SBIT(GSM_CSTATE_INITIATED),
2855 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2856 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2857 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2858 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2859 MNCC_REL_REQ, gsm48_cc_tx_release},
2860 /* special */
2861 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002862 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863};
2864
2865#define DOWNSLLEN \
2866 (sizeof(downstatelist) / sizeof(struct downstate))
2867
2868
Harald Welte76556372010-12-22 23:57:45 +01002869int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870{
Harald Welte1a6f7982009-08-09 18:52:33 +02002871 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002873 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002874 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875 struct gsm_mncc *data = arg, rel;
2876
Harald Welte04dc88f2010-12-22 21:45:05 +01002877 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2878
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 /* handle special messages */
2880 switch(msg_type) {
2881 case MNCC_BRIDGE:
2882 return tch_bridge(net, arg);
2883 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002884 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002886 return tch_recv_mncc(net, data->callref, 1);
2887 case GSM_TCHF_FRAME:
2888 /* Find callref */
2889 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002890 if (!trans) {
2891 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002892 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002893 }
2894 if (!trans->conn) {
2895 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002896 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002897 }
2898 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2899 /* This should be LOGL_ERROR or NOTICE, but
2900 * unfortuantely it happens for a couple of frames at
2901 * the beginning of every RTP connection */
2902 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002903 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002904 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002905 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002906 switch (bts->type) {
2907 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002908 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002909 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002910 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002911 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002912 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002913 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002914 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002915 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002916 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002917 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002918 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002919 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002920 }
2921 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002922 }
2923
2924 memset(&rel, 0, sizeof(struct gsm_mncc));
2925 rel.callref = data->callref;
2926
2927 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002928 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002929
2930 /* Callref unknown */
2931 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002932 struct gsm_subscriber *subscr;
2933
Harald Welte4a3464c2009-07-04 10:11:24 +02002934 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002935 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2936 "Received '%s' from MNCC with "
2937 "unknown callref %d\n", data->called.number,
2938 get_mncc_name(msg_type), data->callref);
2939 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002940 return mncc_release_ind(net, NULL, data->callref,
2941 GSM48_CAUSE_LOC_PRN_S_LU,
2942 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002944 if (!data->called.number[0] && !data->imsi[0]) {
2945 DEBUGP(DCC, "(bts - trx - ts - ti) "
2946 "Received '%s' from MNCC with "
2947 "no number or IMSI\n", get_mncc_name(msg_type));
2948 /* Invalid number */
2949 return mncc_release_ind(net, NULL, data->callref,
2950 GSM48_CAUSE_LOC_PRN_S_LU,
2951 GSM48_CC_CAUSE_INV_NR_FORMAT);
2952 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002953 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002954 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002955 subscr = subscr_get_by_extension(net,
2956 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002957 else
Harald Welte9176bd42009-07-23 18:46:00 +02002958 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959 /* If subscriber is not found */
2960 if (!subscr) {
2961 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2962 "Received '%s' from MNCC with "
2963 "unknown subscriber %s\n", data->called.number,
2964 get_mncc_name(msg_type), data->called.number);
2965 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002966 return mncc_release_ind(net, NULL, data->callref,
2967 GSM48_CAUSE_LOC_PRN_S_LU,
2968 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002969 }
2970 /* If subscriber is not "attached" */
2971 if (!subscr->lac) {
2972 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2973 "Received '%s' from MNCC with "
2974 "detached subscriber %s\n", data->called.number,
2975 get_mncc_name(msg_type), data->called.number);
2976 subscr_put(subscr);
2977 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002978 return mncc_release_ind(net, NULL, data->callref,
2979 GSM48_CAUSE_LOC_PRN_S_LU,
2980 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002981 }
2982 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002983 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2984 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002985 DEBUGP(DCC, "No memory for trans.\n");
2986 subscr_put(subscr);
2987 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002988 mncc_release_ind(net, NULL, data->callref,
2989 GSM48_CAUSE_LOC_PRN_S_LU,
2990 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002991 return -ENOMEM;
2992 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002994 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002995
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002997 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002998 /* find transaction with this subscriber already paging */
2999 llist_for_each_entry(transt, &net->trans_list, entry) {
3000 /* Transaction of our lchan? */
3001 if (transt == trans ||
3002 transt->subscr != subscr)
3003 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003004 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 "Received '%s' from MNCC with "
3006 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003007 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003008 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003009 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003010 subscr_put(subscr);
3011 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 return 0;
3013 }
3014 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003015 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003016
3017 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003018 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3019 if (!trans->paging_request) {
3020 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3021 subscr_put(subscr);
3022 trans_free(trans);
3023 return 0;
3024 }
3025
3026 *trans->paging_request = subscr->net;
3027 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003028
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003029 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003030 return 0;
3031 }
3032 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003033 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003034 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003036
3037 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003038 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039
3040 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003041 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003042 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003043 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044 (trans->subscr)?(trans->subscr->extension):"-",
3045 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003046 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3047 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 if (msg_type == MNCC_REL_REQ)
3049 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3050 else
3051 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3052 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003053 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003054 return rc;
3055 }
3056
3057 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3058 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003059 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003061 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003062 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003063 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064
3065 /* Find function for current state and message */
3066 for (i = 0; i < DOWNSLLEN; i++)
3067 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003068 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 break;
3070 if (i == DOWNSLLEN) {
3071 DEBUGP(DCC, "Message unhandled at this state.\n");
3072 return 0;
3073 }
3074
3075 rc = downstatelist[i].rout(trans, arg);
3076
3077 return rc;
3078}
3079
3080
3081static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003082 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 int type;
3084 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3085} datastatelist[] = {
3086 /* mobile originating call establishment */
3087 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3088 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3089 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3090 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3091 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3092 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3093 /* mobile terminating call establishment */
3094 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3095 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3096 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3097 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003098 {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 +08003099 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3100 /* signalling during call */
3101 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3102 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3103 {SBIT(GSM_CSTATE_ACTIVE),
3104 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3105 {ALL_STATES,
3106 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3107 {ALL_STATES,
3108 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3109 {ALL_STATES,
3110 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3111 {SBIT(GSM_CSTATE_ACTIVE),
3112 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3113 {SBIT(GSM_CSTATE_ACTIVE),
3114 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3115 {SBIT(GSM_CSTATE_ACTIVE),
3116 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3117 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3118 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3119 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3120 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3121 {SBIT(GSM_CSTATE_ACTIVE),
3122 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3123 /* clearing */
3124 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3125 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3126 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3127 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3128 {ALL_STATES, /* 5.4.3.4 */
3129 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3130};
3131
3132#define DATASLLEN \
3133 (sizeof(datastatelist) / sizeof(struct datastate))
3134
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003135static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003136{
3137 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003138 uint8_t msg_type = gh->msg_type & 0xbf;
3139 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003140 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003141 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003142
Harald Welte4bfdfe72009-06-10 23:11:52 +08003143 if (msg_type & 0x80) {
3144 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3145 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003146 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003147
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003149 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003150
Harald Welte6f5aee02009-07-23 21:21:14 +02003151 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003152 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003153 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003154 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003155 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3156 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157
3158 /* Create transaction */
3159 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003160 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003161 "creating new trans.\n", transaction_id);
3162 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003163 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003164 transaction_id, new_callref++);
3165 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003167 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003168 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169 GSM48_MT_CC_RELEASE_COMPL);
3170 return -ENOMEM;
3171 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003172 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003173 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003174 }
3175
3176 /* find function for current state and message */
3177 for (i = 0; i < DATASLLEN; i++)
3178 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003179 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003180 break;
3181 if (i == DATASLLEN) {
3182 DEBUGP(DCC, "Message unhandled at this state.\n");
3183 return 0;
3184 }
3185
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003186 assert(trans->subscr);
3187
Harald Welte4bfdfe72009-06-10 23:11:52 +08003188 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003189
3190 return rc;
3191}
3192
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003193/* Create a dummy to wait five seconds */
3194static void release_anchor(struct gsm_subscriber_connection *conn)
3195{
3196 if (!conn->anch_operation)
3197 return;
3198
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003199 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003200 talloc_free(conn->anch_operation);
3201 conn->anch_operation = NULL;
3202}
3203
3204static void anchor_timeout(void *_data)
3205{
3206 struct gsm_subscriber_connection *con = _data;
3207
3208 release_anchor(con);
3209 msc_release_connection(con);
3210}
3211
3212int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3213{
3214 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3215 if (!conn->anch_operation)
3216 return -1;
3217
3218 conn->anch_operation->timeout.data = conn;
3219 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003220 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003221 return 0;
3222}
3223
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003224/* here we get data from the BSC level... */
3225int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003226{
3227 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003228 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003229 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003230
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003231 if (silent_call_reroute(conn, msg))
3232 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003233
3234 switch (pdisc) {
3235 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003236 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003237 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003238 break;
3239 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003240 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003241 break;
3242 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003243 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003244 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003245 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003246 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003247 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003248 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003249 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003250 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003251 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3252 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003253 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003254 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003255 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003256 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003257 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003258 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003259 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3260 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003261 break;
3262 }
3263
3264 return rc;
3265}
Harald Welte8470bf22008-12-25 23:28:35 +00003266
Harald Welte805f6442009-07-28 18:25:29 +02003267/*
3268 * This will be ran by the linker when loading the DSO. We use it to
3269 * do system initialization, e.g. registration of signal handlers.
3270 */
3271static __attribute__((constructor)) void on_dso_load_0408(void)
3272{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003273 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003274}