blob: 75e464d5f9e083da5455c71d515a4cc909b6e2d7 [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
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200284 case GSM_SECURITY_ALREADY:
285 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
286 "UPDATING after CM SERVICE REQUEST\n");
287 /* fall through */
288
Sylvain Munaut267fba02009-12-24 00:28:01 +0100289 case GSM_SECURITY_NOAVAIL:
290 case GSM_SECURITY_SUCCEEDED:
291 /* We're all good */
292 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800293 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800294 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100295 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800296 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100297 }
298
299 /* call subscr_update after putting the loc_upd_acc
300 * in the transmit queue, since S_SUBSCR_ATTACHED might
301 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800302 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100303 GSM_SUBSCRIBER_UPDATE_ATTACHED);
304
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100305 /*
306 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
307 * MS needs to respond with a TMSI REALLOCATION COMPLETE
308 * (even if the TMSI is the same).
309 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100310 break;
311
312 default:
313 rc = -EINVAL;
314 };
315
316 return rc;
317}
318
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100319static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000320{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100321 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800322 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100323 conn->loc_operation->key_seq,
324 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000325 return 0;
326}
327
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800328void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000329{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800330 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800331
332 /* avoid someone issuing a clear */
333 conn->in_release = 1;
334
Holger Freyther7c19f742009-06-06 13:54:35 +0000335 /*
336 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800337 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000338 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800339 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100340
341 /* We might need to cancel the paging response or such. */
342 if (conn->sec_operation && conn->sec_operation->cb) {
343 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
344 NULL, conn, conn->sec_operation->cb_data);
345 }
346
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800347 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800348 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000349
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100350 /*
351 * Free all transactions that are associated with the released
352 * connection. The transaction code will inform the CC or SMS
353 * facilities that will send the release indications. As part of
354 * the CC REL_IND the remote leg might be released and this will
355 * trigger the call to trans_free. This is something the llist
356 * macro can not handle and we will need to re-iterate the list.
357 *
358 * TODO: Move the trans_list into the subscriber connection and
359 * create a pending list for MT transactions. These exist before
360 * we have a subscriber connection.
361 */
362restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800363 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100364 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200365 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100366 goto restart;
367 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800368 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000369}
370
Harald Welte371efe52010-12-22 23:17:50 +0100371void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
372{
373 struct gsm_trans *trans, *temp;
374
375 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
376
377 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100378 if (trans->protocol == protocol) {
379 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100380 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100381 }
Harald Welte371efe52010-12-22 23:17:50 +0100382 }
383}
384
Holger Freyther429e7762008-12-30 13:28:30 +0000385/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200386int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000387{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800388 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800389 struct msgb *msg;
390
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200391 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800392
393 msg = gsm48_create_loc_upd_rej(cause);
394 if (!msg) {
395 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
396 return -1;
397 }
Harald Welte52b1f982008-12-23 20:25:15 +0000398
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800399 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000400
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100401 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100402 "LAC=%u BTS=%u\n", conn->subscr ?
403 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800404 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100405
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800406 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000407}
408
409/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200410int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000411{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800412 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000413 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000414 struct gsm48_hdr *gh;
415 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200416 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000417
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800418 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000419
420 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
421 gh->proto_discr = GSM48_PDISC_MM;
422 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
423
424 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100425 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000426 bts->network->network_code, bts->location_area_code);
427
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200428 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200429 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000430
431 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
432
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200433 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100434
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800435 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000436}
437
Harald Weltebf5e8df2009-02-03 12:59:45 +0000438/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200439static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000440{
441 struct msgb *msg = gsm48_msgb_alloc();
442 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000443
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800444 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000445
446 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
447 gh->proto_discr = GSM48_PDISC_MM;
448 gh->msg_type = GSM48_MT_MM_ID_REQ;
449 gh->data[0] = id_type;
450
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800451 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000452}
453
Harald Welte231ad4f2008-12-27 11:15:38 +0000454
Harald Weltebf5e8df2009-02-03 12:59:45 +0000455/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800456static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000457{
458 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000459 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200460 struct gsm_bts *bts = lchan->ts->trx->bts;
461 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200462 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200463 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000464
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200465 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000466 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000467 mi_type, mi_string);
468
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200469 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100470
Harald Welte75a983f2008-12-27 21:34:06 +0000471 switch (mi_type) {
472 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200473 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100474 if (!conn->subscr) {
475 conn->subscr = subscr_get_by_imsi(net, mi_string);
476 if (!conn->subscr)
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200477 conn->subscr = subscr_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200478 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100479 if (conn->loc_operation)
480 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000481 break;
482 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000483 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000484 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100485 if (conn->subscr) {
486 db_subscriber_assoc_imei(conn->subscr, mi_string);
487 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200488 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100489 if (conn->loc_operation)
490 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000491 break;
492 }
Holger Freyther73487a22008-12-31 18:53:57 +0000493
494 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100495 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000496}
497
Harald Welte255539c2008-12-28 02:26:27 +0000498
499static void loc_upd_rej_cb(void *data)
500{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100501 struct gsm_subscriber_connection *conn = data;
502 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100503 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000504
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100505 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800506 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800507 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000508}
509
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100510static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000511{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100512 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
513 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200514 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000515}
516
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000518{
519 switch (type) {
520 case GSM48_LUPD_NORMAL:
521 return "NORMAL";
522 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200523 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000524 case GSM48_LUPD_IMSI_ATT:
525 return "IMSI ATTACH";
526 default:
527 return "UNKNOWN";
528 }
529}
530
Harald Weltebf5e8df2009-02-03 12:59:45 +0000531/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800532static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000533{
Harald Welte8470bf22008-12-25 23:28:35 +0000534 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000535 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800536 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800537 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200538 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200539 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000540
Harald Welte8470bf22008-12-25 23:28:35 +0000541 lu = (struct gsm48_loc_upd_req *) gh->data;
542
543 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000544
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200545 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000546
Harald Weltea0368542009-06-27 02:58:43 +0200547 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000548 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000549
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200550 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100551
Harald Welte24ff6ee2009-12-22 00:41:05 +0100552 switch (lu->type) {
553 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200554 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100555 break;
556 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200557 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100558 break;
559 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200560 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100561 break;
562 }
563
Holger Freythereaf04692009-06-06 13:54:44 +0000564 /*
565 * Pseudo Spoof detection: Just drop a second/concurrent
566 * location updating request.
567 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100568 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200569 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100570 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800571 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000572 return 0;
573 }
574
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800575 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000576
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200577 conn->loc_operation->key_seq = lu->key_seq;
578
Harald Welte52b1f982008-12-23 20:25:15 +0000579 switch (mi_type) {
580 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200581 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000582 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200583 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100584 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000585
Jan Luebbe370b41d2009-08-12 10:19:34 +0200586 /* look up subscriber based on IMSI, create if not found */
587 subscr = subscr_get_by_imsi(bts->network, mi_string);
588 if (!subscr) {
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200589 subscr = subscr_create_subscriber(bts->network, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200590 }
Harald Welte4b634542008-12-27 01:55:51 +0000591 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000592 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200593 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000594 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200595 subscr = subscr_get_by_tmsi(bts->network,
596 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000597 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000598 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200599 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100600 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000601 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200602 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200603 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200604 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000605 break;
606 case GSM_MI_TYPE_IMEI:
607 case GSM_MI_TYPE_IMEISV:
608 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200609 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000610 break;
611 default:
Harald Weltea0368542009-06-27 02:58:43 +0200612 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000613 break;
614 }
615
Harald Welte24516ea2009-07-04 10:18:00 +0200616 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100617 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200618
Harald Welte4bfdfe72009-06-10 23:11:52 +0800619 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200620 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800621 /* FIXME: request id? close channel? */
622 return -EINVAL;
623 }
624
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100625 conn->subscr = subscr;
626 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000627
Harald Welte24516ea2009-07-04 10:18:00 +0200628 /* check if we can let the subscriber into our network immediately
629 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100630 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000631}
632
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100633/* Turn int into semi-octet representation: 98 => 0x89 */
634static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100636 uint8_t ret;
637
638 ret = value / 10;
639 ret |= (value % 10) << 4;
640
641 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800642}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100643
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644
Harald Weltedb253af2008-12-30 17:56:55 +0000645/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800646int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000647{
648 struct msgb *msg = gsm48_msgb_alloc();
649 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800650 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100651 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200652 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200653 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100654
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100656 struct tm* gmt_time;
657 struct tm* local_time;
658 int tzunits;
Harald Weltedb253af2008-12-30 17:56:55 +0000659
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800660 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000661
662 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
663 gh->proto_discr = GSM48_PDISC_MM;
664 gh->msg_type = GSM48_MT_MM_INFO;
665
666 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200667#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000668 name_len = strlen(net->name_long);
669 /* 10.5.3.5a */
670 ptr8 = msgb_put(msg, 3);
671 ptr8[0] = GSM48_IE_NAME_LONG;
672 ptr8[1] = name_len*2 +1;
673 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
674
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200675 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000676 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000677 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000678
679 /* FIXME: Use Cell Broadcast, not UCS-2, since
680 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200681#endif
682 name_len = (strlen(net->name_long)*7)/8;
683 name_pad = (8 - strlen(net->name_long)*7)%8;
684 if (name_pad > 0)
685 name_len++;
686 /* 10.5.3.5a */
687 ptr8 = msgb_put(msg, 3);
688 ptr8[0] = GSM48_IE_NAME_LONG;
689 ptr8[1] = name_len +1;
690 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
691
692 ptr8 = msgb_put(msg, name_len);
693 gsm_7bit_encode(ptr8, net->name_long);
694
Harald Weltedb253af2008-12-30 17:56:55 +0000695 }
696
697 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200698#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000699 name_len = strlen(net->name_short);
700 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200701 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200702 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000703 ptr8[1] = name_len*2 + 1;
704 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
705
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200706 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000707 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000708 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200709#endif
710 name_len = (strlen(net->name_short)*7)/8;
711 name_pad = (8 - strlen(net->name_short)*7)%8;
712 if (name_pad > 0)
713 name_len++;
714 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200715 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200716 ptr8[0] = GSM48_IE_NAME_SHORT;
717 ptr8[1] = name_len +1;
718 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
719
720 ptr8 = msgb_put(msg, name_len);
721 gsm_7bit_encode(ptr8, net->name_short);
722
Harald Weltedb253af2008-12-30 17:56:55 +0000723 }
724
Harald Weltedb253af2008-12-30 17:56:55 +0000725 /* Section 10.5.3.9 */
726 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100727 gmt_time = gmtime(&cur_t);
728
Harald Weltedb253af2008-12-30 17:56:55 +0000729 ptr8 = msgb_put(msg, 8);
730 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100731 ptr8[1] = bcdify(gmt_time->tm_year % 100);
732 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
733 ptr8[3] = bcdify(gmt_time->tm_mday);
734 ptr8[4] = bcdify(gmt_time->tm_hour);
735 ptr8[5] = bcdify(gmt_time->tm_min);
736 ptr8[6] = bcdify(gmt_time->tm_sec);
737
Harald Welte45f91712012-07-08 16:48:11 +0200738 if (bts->tz.override) {
739 /* Convert tz.hr and tz.mn to units */
740 if (bts->tz.hr < 0) {
741 tzunits = ((bts->tz.hr/-1)*4);
742 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100743 ptr8[7] = bcdify(tzunits);
744 /* Set negative time */
745 ptr8[7] |= 0x08;
746 }
747 else {
Harald Welte45f91712012-07-08 16:48:11 +0200748 tzunits = bts->tz.hr*4;
749 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100750 ptr8[7] = bcdify(tzunits);
751 }
752 }
753 else {
754 /* Need to get GSM offset and convert into 15 min units */
755 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
756 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200757#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100758 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200759#else
760#warning find a portable way to obtain the timezone offset
761 tzunits = 0;
762#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100763 if (tzunits < 0) {
764 tzunits = tzunits/-1;
765 ptr8[7] = bcdify(tzunits);
766 /* Flip it to negative */
767 ptr8[7] |= 0x08;
768 }
769 else
770 ptr8[7] = bcdify(tzunits);
771 }
Harald Weltedb253af2008-12-30 17:56:55 +0000772
Daniel Willmanneea93372009-08-13 03:42:07 +0200773 DEBUGP(DMM, "-> MM INFO\n");
774
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800775 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000776}
777
Harald Welte7984d5c2009-08-12 22:56:50 +0200778/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200779int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200780{
781 struct msgb *msg = gsm48_msgb_alloc();
782 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200783 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200784
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200785 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200786
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800787 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200788 gh->proto_discr = GSM48_PDISC_MM;
789 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
790
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100791 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200792
Harald Welte7984d5c2009-08-12 22:56:50 +0200793 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200794 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200795 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200796
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800797 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200798}
799
800/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800801int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200802{
803 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800804 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200805}
806
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100807/*
808 * At the 30C3 phones miss their periodic update
809 * interval a lot and then remain unreachable. In case
810 * we still know the TMSI we can just attach it again.
811 */
812static void implit_attach(struct gsm_subscriber_connection *conn)
813{
814 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
815 return;
816
817 subscr_update(conn->subscr, conn->bts,
818 GSM_SUBSCRIBER_UPDATE_ATTACHED);
819}
820
821
Sylvain Munautba87f452009-12-24 00:28:46 +0100822static int _gsm48_rx_mm_serv_req_sec_cb(
823 unsigned int hooknum, unsigned int event,
824 struct msgb *msg, void *data, void *param)
825{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800826 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100827 int rc = 0;
828
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200829 /* auth failed or succeeded, the timer was stopped */
830 conn->expire_timer_stopped = 1;
831
Sylvain Munautba87f452009-12-24 00:28:46 +0100832 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);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100840 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100841 break;
842
843 case GSM_SECURITY_SUCCEEDED:
844 /* nothing to do. CIPHER MODE COMMAND is
845 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100846 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100847 break;
848
849 default:
850 rc = -EINVAL;
851 };
852
853 return rc;
854}
855
Harald Welte4ed0e922009-01-10 03:17:30 +0000856/*
857 * Handle CM Service Requests
858 * a) Verify that the packet is long enough to contain the information
859 * we require otherwsie reject with INCORRECT_MESSAGE
860 * b) Try to parse the TMSI. If we do not have one reject
861 * c) Check that we know the subscriber with the TMSI otherwise reject
862 * with a HLR cause
863 * d) Set the subscriber on the gsm_lchan and accept
864 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800865static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000866{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200867 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200868 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000869
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800870 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000871 struct gsm_subscriber *subscr;
872 struct gsm48_hdr *gh = msgb_l3(msg);
873 struct gsm48_service_request *req =
874 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800875 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200876 uint8_t classmark2_len = gh->data[1];
877 uint8_t *classmark2 = gh->data+2;
878 uint8_t mi_len = *(classmark2 + classmark2_len);
879 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000880
Harald Weltec9e02182009-05-01 19:07:53 +0000881 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000882 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000883 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800884 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000885 GSM48_REJECT_INCORRECT_MESSAGE);
886 }
887
888 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000889 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800890 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000891 GSM48_REJECT_INCORRECT_MESSAGE);
892 }
893
Harald Weltec9e02182009-05-01 19:07:53 +0000894 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000895 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000896 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800897 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000898 GSM48_REJECT_INCORRECT_MESSAGE);
899 }
900
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200901 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000902 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000903 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000904
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200905 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100906
Harald Welte3ac7f102009-08-10 10:12:45 +0200907 if (is_siemens_bts(bts))
908 send_siemens_mrpci(msg->lchan, classmark2-1);
909
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200910 subscr = subscr_get_by_tmsi(bts->network,
911 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000912
Harald Welte2a139372009-02-22 21:14:55 +0000913 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000914 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800915 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000916 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
917
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800918 if (!conn->subscr)
919 conn->subscr = subscr;
920 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100921 subscr_put(subscr); /* lchan already has a ref, don't need another one */
922 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000923 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
924 subscr_put(subscr);
925 }
926
Harald Weltec2e302d2009-07-05 14:08:13 +0200927 subscr->equipment.classmark2_len = classmark2_len;
928 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
929 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000930
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200931 /* we will send a MM message soon */
932 conn->expire_timer_stopped = 1;
933
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800934 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100935 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000936}
937
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200938static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000939{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200940 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000941 struct gsm48_hdr *gh = msgb_l3(msg);
942 struct gsm48_imsi_detach_ind *idi =
943 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200944 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200945 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800946 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000947
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200948 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100949 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000950 mi_type, mi_string);
951
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200952 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100953
Harald Welte2a139372009-02-22 21:14:55 +0000954 switch (mi_type) {
955 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100956 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200957 subscr = subscr_get_by_tmsi(bts->network,
958 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000959 break;
960 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100961 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +0200962 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000963 break;
964 case GSM_MI_TYPE_IMEI:
965 case GSM_MI_TYPE_IMEISV:
966 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +0100967 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000968 break;
969 default:
Daniel Willmann977cd132012-12-28 19:29:44 +0100970 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000971 break;
972 }
973
Holger Freyther4a49e772009-04-12 05:37:29 +0000974 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200975 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000976 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100977 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200978
979 subscr->equipment.classmark1 = idi->classmark1;
980 db_sync_equipment(&subscr->equipment);
981
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000982 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000983 } else
Harald Welte2a139372009-02-22 21:14:55 +0000984 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
985
Harald Welte31981a02009-12-20 09:58:40 +0100986 /* FIXME: iterate over all transactions and release them,
987 * imagine an IMSI DETACH happening during an active call! */
988
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200989 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +0000990 return 0;
991}
992
Harald Welted2a7f5a2009-06-05 20:08:20 +0000993static int gsm48_rx_mm_status(struct msgb *msg)
994{
995 struct gsm48_hdr *gh = msgb_l3(msg);
996
997 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
998
999 return 0;
1000}
1001
Sylvain Munaut30a15382009-12-24 00:27:26 +01001002/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001003static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001004{
1005 struct gsm48_hdr *gh = msgb_l3(msg);
1006 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001007 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001008
Sylvain Munautc593cf12010-06-10 23:51:41 +02001009 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001010 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001011
1012 /* Safety check */
1013 if (!conn->sec_operation) {
1014 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1015 return -EIO;
1016 }
1017
1018 /* Validate SRES */
1019 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001020 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001021 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001022
1023 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001024 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001025
Sylvain Munaut30a15382009-12-24 00:27:26 +01001026 if (cb)
1027 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001028 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001029
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001030 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001031 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001032 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001033 }
1034
Sylvain Munautc593cf12010-06-10 23:51:41 +02001035 DEBUGPC(DMM, "OK\n");
1036
Sylvain Munaut30a15382009-12-24 00:27:26 +01001037 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001038 return gsm0808_cipher_mode(conn, net->a5_encryption,
1039 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001040}
1041
Harald Weltebf5e8df2009-02-03 12:59:45 +00001042/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001043static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001044{
1045 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001046 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001047
1048 switch (gh->msg_type & 0xbf) {
1049 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001050 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001051 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001052 break;
1053 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001054 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001055 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001056 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001057 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001058 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001059 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001060 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001061 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001062 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001063 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001064 conn->subscr ?
1065 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001066 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001067 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001068 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001069 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001070 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001071 break;
1072 case GSM48_MT_MM_CM_REEST_REQ:
1073 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1074 break;
1075 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001076 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001077 break;
1078 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001079 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001080 gh->msg_type);
1081 break;
1082 }
1083
1084 return rc;
1085}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001086
Harald Welte2d35ae62009-02-06 12:02:13 +00001087/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001088static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001089{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001090 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001091 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001092 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001093 uint8_t *classmark2_lv = gh->data + 1;
1094 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001095 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001096 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001097 int rc = 0;
1098
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001099 resp = (struct gsm48_pag_resp *) &gh->data[0];
1100 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1101 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001102 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1103 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001104
Harald Weltefe18d8f2009-02-22 21:14:24 +00001105 switch (mi_type) {
1106 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001107 subscr = subscr_get_by_tmsi(bts->network,
1108 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001109 break;
1110 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001111 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001112 break;
1113 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001114
1115 if (!subscr) {
1116 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001117 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001118 return -EINVAL;
1119 }
1120 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001121 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001122
Harald Weltec2e302d2009-07-05 14:08:13 +02001123 subscr->equipment.classmark2_len = *classmark2_lv;
1124 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1125 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001126
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001127 /* We received a paging */
1128 conn->expire_timer_stopped = 1;
1129
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001130 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001131 return rc;
1132}
1133
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001134static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001135{
1136 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001137 uint8_t apdu_id_flags;
1138 uint8_t apdu_len;
1139 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001140
1141 apdu_id_flags = gh->data[0];
1142 apdu_len = gh->data[1];
1143 apdu_data = gh->data+2;
1144
Harald Welte70f92052012-07-16 13:22:19 +02001145 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001146 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001147
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001148 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001149}
1150
Harald Weltebf5e8df2009-02-03 12:59:45 +00001151/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001152static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001153{
1154 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001155 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001156
1157 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001158 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001159 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001160 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001161 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001162 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001163 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001164 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001165 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001166 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001167 break;
1168 }
1169
Harald Welte2d35ae62009-02-06 12:02:13 +00001170 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001171}
1172
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001173int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1174 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001175{
1176 struct msgb *msg = gsm48_msgb_alloc();
1177 struct gsm48_hdr *gh;
1178
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001179 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001180
1181 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1182 apdu_id, apdu_len);
1183
1184 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1185 gh->proto_discr = GSM48_PDISC_RR;
1186 gh->msg_type = GSM48_MT_RR_APP_INFO;
1187 gh->data[0] = apdu_id;
1188 gh->data[1] = apdu_len;
1189 memcpy(gh->data+2, apdu, apdu_len);
1190
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001191 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001192}
1193
Harald Welte4bc90a12008-12-27 16:32:52 +00001194/* Call Control */
1195
Harald Welte7584aea2009-02-11 11:44:12 +00001196/* The entire call control code is written in accordance with Figure 7.10c
1197 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1198 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1199 * it for voice */
1200
Harald Welte4bfdfe72009-06-10 23:11:52 +08001201static void new_cc_state(struct gsm_trans *trans, int state)
1202{
1203 if (state > 31 || state < 0)
1204 return;
1205
1206 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001207 gsm48_cc_state_name(trans->cc.state),
1208 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001209
Harald Weltedcaf5652009-07-23 18:56:43 +02001210 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001211}
1212
1213static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001214{
1215 struct msgb *msg = gsm48_msgb_alloc();
1216 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001217 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001218
Harald Welte4bc90a12008-12-27 16:32:52 +00001219 gh->msg_type = GSM48_MT_CC_STATUS;
1220
1221 cause = msgb_put(msg, 3);
1222 cause[0] = 2;
1223 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1224 cause[2] = 0x80 | 30; /* response to status inquiry */
1225
1226 call_state = msgb_put(msg, 1);
1227 call_state[0] = 0xc0 | 0x00;
1228
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001229 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001230}
1231
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001232static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001233 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001234{
1235 struct msgb *msg = gsm48_msgb_alloc();
1236 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1237
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001238 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001239
Harald Welte6f4b7532008-12-29 00:39:37 +00001240 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001241 gh->msg_type = msg_type;
1242
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001243 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001244}
1245
Harald Welte4bfdfe72009-06-10 23:11:52 +08001246static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1247{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001248 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001249 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001250 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001251 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001252 }
1253}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001254
Harald Welte4bfdfe72009-06-10 23:11:52 +08001255static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1256 int msg_type, struct gsm_mncc *mncc)
1257{
1258 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001259 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001260
1261 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001262 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001263 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001265 trans->conn->lchan->ts->trx->bts->nr,
1266 trans->conn->lchan->ts->trx->nr,
1267 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001268 (trans->subscr)?(trans->subscr->extension):"-",
1269 get_mncc_name(msg_type));
1270 else
1271 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1272 "Sending '%s' to MNCC.\n",
1273 (trans->subscr)?(trans->subscr->extension):"-",
1274 get_mncc_name(msg_type));
1275 else
1276 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1277 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1278
1279 mncc->msg_type = msg_type;
1280
Harald Welte966636f2009-06-26 19:39:35 +02001281 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282 if (!msg)
1283 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001284
1285 data = msgb_put(msg, sizeof(struct gsm_mncc));
1286 memcpy(data, mncc, sizeof(struct gsm_mncc));
1287
Harald Welte54209c22010-12-22 23:43:29 +01001288 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001289
1290 return 0;
1291}
1292
1293int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001294 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295{
1296 struct gsm_mncc rel;
1297
Harald Welte92f70c52009-06-12 01:54:08 +08001298 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001300 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001301 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1302 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1304}
1305
Harald Weltedcaf5652009-07-23 18:56:43 +02001306/* Call Control Specific transaction release.
1307 * gets called by trans_free, DO NOT CALL YOURSELF! */
1308void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001309{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001310 gsm48_stop_cc_timer(trans);
1311
1312 /* send release to L4, if callref still exists */
1313 if (trans->callref) {
1314 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001315 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001316 GSM48_CAUSE_LOC_PRN_S_LU,
1317 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001319 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001320 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001321 if (trans->conn)
1322 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323}
1324
1325static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001326
Harald Welte09e38af2009-02-16 22:52:23 +00001327/* call-back from paging the B-end of the connection */
1328static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001329 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001330{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001331 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001332 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001333 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001335
Harald Welte09e38af2009-02-16 22:52:23 +00001336 if (hooknum != GSM_HOOK_RR_PAGING)
1337 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001338
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001339 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 if (!net) {
1341 DEBUGP(DCC, "Error Network not set!\n");
1342 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001343 }
Harald Welte7584aea2009-02-11 11:44:12 +00001344
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 /* check all tranactions (without lchan) for subscriber */
1346 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001347 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 continue;
1349 switch (event) {
1350 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001351 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352 break;
1353 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001354 transt->subscr->extension);
1355 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001357 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001358 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001359 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001360 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 }
1362 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001363 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 break;
1365 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001366 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001368 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001370 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001371 mncc_release_ind(transt->subscr->net, transt,
1372 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001373 GSM48_CAUSE_LOC_PRN_S_LU,
1374 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001376 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001377 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 break;
1379 }
1380 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001381
1382 talloc_free(paging_request);
1383
1384 /*
1385 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1386 * failure and then the subscr will be poke again. This needs a lot of fixing
1387 * in the subscriber queue code.
1388 */
1389 if (!found && conn)
1390 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001391 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001392}
Harald Welte7584aea2009-02-11 11:44:12 +00001393
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001394static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001395
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001396/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001397static int switch_for_handover(struct gsm_lchan *old_lchan,
1398 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001399{
1400 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001401
1402 if (ipacc_rtp_direct) {
1403 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1404 return 0;
1405 }
1406
1407 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001408 new_rs = new_lchan->abis_ip.rtp_socket;
1409 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001410
1411 if (!new_rs) {
1412 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1413 return -EIO;
1414 }
1415
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001416 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001417
1418 if (!old_rs) {
1419 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1420 return -EIO;
1421 }
1422
1423 /* copy rx_action and reference to other sock */
1424 new_rs->rx_action = old_rs->rx_action;
1425 new_rs->tx_action = old_rs->tx_action;
1426 new_rs->transmit = old_rs->transmit;
1427
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001428 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001429 case RTP_PROXY:
1430 other_rs = old_rs->proxy.other_sock;
1431 rtp_socket_proxy(new_rs, other_rs);
1432 /* delete reference to other end socket to prevent
1433 * rtp_socket_free() from removing the inverse reference */
1434 old_rs->proxy.other_sock = NULL;
1435 break;
1436 case RTP_RECV_UPSTREAM:
1437 new_rs->receive = old_rs->receive;
1438 break;
1439 case RTP_NONE:
1440 break;
1441 }
1442
1443 return 0;
1444}
1445
Harald Welte805f6442009-07-28 18:25:29 +02001446/* some other part of the code sends us a signal */
1447static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1448 void *handler_data, void *signal_data)
1449{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001450 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001451 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001452 struct gsm_network *net;
1453 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001454
1455 if (subsys != SS_ABISIP)
1456 return 0;
1457
1458 /* in case we use direct BTS-to-BTS RTP */
1459 if (ipacc_rtp_direct)
1460 return 0;
1461
Harald Welte805f6442009-07-28 18:25:29 +02001462 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001463 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001464 /* in case we don't use direct BTS-to-BTS RTP */
1465 /* the BTS has successfully bound a TCH to a local ip/port,
1466 * which means we can connect our UDP socket to it */
1467 if (lchan->abis_ip.rtp_socket) {
1468 rtp_socket_free(lchan->abis_ip.rtp_socket);
1469 lchan->abis_ip.rtp_socket = NULL;
1470 }
1471
1472 lchan->abis_ip.rtp_socket = rtp_socket_create();
1473 if (!lchan->abis_ip.rtp_socket)
1474 return -EIO;
1475
1476 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1477 lchan->abis_ip.bound_ip,
1478 lchan->abis_ip.bound_port);
1479 if (rc < 0)
1480 return -EIO;
1481
Harald Welteda7ab742009-12-19 22:23:05 +01001482 /* check if any transactions on this lchan still have
1483 * a tch_recv_mncc request pending */
1484 net = lchan->ts->trx->bts->network;
1485 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001486 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001487 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1488 tch_recv_mncc(net, trans->callref, 1);
1489 }
1490 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001491
1492 /*
1493 * TODO: this appears to be too early? Why not until after
1494 * the handover detect or the handover complete?
1495 *
1496 * Do we have a handover pending for this new lchan? In that
1497 * case re-route the audio from the old channel to the new one.
1498 */
1499 old_lchan = bsc_handover_pending(lchan);
1500 if (old_lchan)
1501 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001502 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001503 case S_ABISIP_DLCX_IND:
1504 /* the BTS tells us a RTP stream has been disconnected */
1505 if (lchan->abis_ip.rtp_socket) {
1506 rtp_socket_free(lchan->abis_ip.rtp_socket);
1507 lchan->abis_ip.rtp_socket = NULL;
1508 }
1509
1510 break;
Harald Welte805f6442009-07-28 18:25:29 +02001511 }
1512
1513 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001514}
1515
Harald Welte49f48b82009-02-17 15:29:33 +00001516/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001517static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001518{
Harald Welte11fa29c2009-02-19 17:24:39 +00001519 struct gsm_bts *bts = lchan->ts->trx->bts;
1520 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001521 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001522
Harald Welte11fa29c2009-02-19 17:24:39 +00001523 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1524 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1525 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1526
1527 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001528 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001529 return -EINVAL;
1530 }
Harald Welteda7ab742009-12-19 22:23:05 +01001531
1532 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001533 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001534 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001535 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001536 if (!ipacc_rtp_direct) {
1537 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001538 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001539 if (rc < 0)
1540 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001541 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001542 if (rc < 0)
1543 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001544 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301545 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1546 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001547 } else {
1548 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301549 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1550 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301551 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001552 if (rc < 0)
1553 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301554 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1555 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301556 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001557 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001558 break;
1559 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001560 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001561 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001562 trau_mux_map_lchan(lchan, remote_lchan);
1563 break;
1564 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001565 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001566 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001567 }
Harald Welte49f48b82009-02-17 15:29:33 +00001568
1569 return 0;
1570}
1571
Harald Welte4bfdfe72009-06-10 23:11:52 +08001572/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001573static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001574{
Harald Weltedcaf5652009-07-23 18:56:43 +02001575 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1576 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001577
Harald Welte4bfdfe72009-06-10 23:11:52 +08001578 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001579 return -EIO;
1580
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001581 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001582 return -EIO;
1583
1584 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001585 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001586}
1587
Harald Welteda7ab742009-12-19 22:23:05 +01001588/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001589static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001590{
1591 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001592 struct gsm_lchan *lchan;
1593 struct gsm_bts *bts;
1594 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001595
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001597 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001598 if (!trans)
1599 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001600 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001601 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001602 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001603 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604
Harald Welteda7ab742009-12-19 22:23:05 +01001605 switch (bts->type) {
1606 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001607 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001608 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001609 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001610 return -EINVAL;
1611 }
1612 /* in case, we don't have a RTP socket yet, we note this
1613 * in the transaction and try later */
1614 if (!lchan->abis_ip.rtp_socket) {
1615 trans->tch_recv = enable;
1616 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1617 return 0;
1618 }
1619 if (enable) {
1620 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001621 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001622 if (rc < 0)
1623 return rc;
1624 /* assign socket to application interface */
1625 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1626 net, callref);
1627 } else
1628 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1629 net, 0);
1630 break;
1631 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001632 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001633 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001634 if (enable)
1635 return trau_recv_lchan(lchan, callref);
1636 return trau_mux_unmap(NULL, callref);
1637 break;
1638 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001639 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001640 return -EINVAL;
1641 }
1642
1643 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001644}
1645
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1647{
1648 DEBUGP(DCC, "-> STATUS ENQ\n");
1649 return gsm48_cc_tx_status(trans, msg);
1650}
1651
1652static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1653static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1654
1655static void gsm48_cc_timeout(void *arg)
1656{
1657 struct gsm_trans *trans = arg;
1658 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001659 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1660 int mo_location = GSM48_CAUSE_LOC_USER;
1661 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1662 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 struct gsm_mncc mo_rel, l4_rel;
1664
1665 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1666 mo_rel.callref = trans->callref;
1667 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1668 l4_rel.callref = trans->callref;
1669
Harald Weltedcaf5652009-07-23 18:56:43 +02001670 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 case 0x303:
1672 release = 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 0x310:
1676 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001677 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001678 break;
1679 case 0x313:
1680 disconnect = 1;
1681 /* unknown, did not find it in the specs */
1682 break;
1683 case 0x301:
1684 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001685 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 break;
1687 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001688 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001689 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001690 gsm48_cc_tx_release(trans, &trans->cc.msg);
1691 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692 break; /* stay in release state */
1693 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001694 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 return;
1696// release = 1;
1697// l4_cause = 14;
1698// break;
1699 case 0x306:
1700 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001701 mo_cause = trans->cc.msg.cause.value;
1702 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 break;
1704 case 0x323:
1705 disconnect = 1;
1706 break;
1707 default:
1708 release = 1;
1709 }
1710
1711 if (release && trans->callref) {
1712 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001713 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001714 l4_location, l4_cause);
1715 trans->callref = 0;
1716 }
1717
1718 if (disconnect && trans->callref) {
1719 /* process disconnect towards layer 4 */
1720 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001721 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 }
1723
1724 /* process disconnect towards mobile station */
1725 if (disconnect || release) {
1726 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001727 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1728 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1729 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 mo_rel.cause.diag_len = 3;
1731
1732 if (disconnect)
1733 gsm48_cc_tx_disconnect(trans, &mo_rel);
1734 if (release)
1735 gsm48_cc_tx_release(trans, &mo_rel);
1736 }
1737
1738}
1739
1740static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1741 int sec, int micro)
1742{
1743 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001744 trans->cc.timer.cb = gsm48_cc_timeout;
1745 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001746 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001747 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748}
1749
1750static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1751{
1752 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001753 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1755 struct tlv_parsed tp;
1756 struct gsm_mncc setup;
1757
1758 memset(&setup, 0, sizeof(struct gsm_mncc));
1759 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001760 if (trans->conn && trans->conn->lchan)
1761 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001762 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 /* emergency setup is identified by msg_type */
1764 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1765 setup.emergency = 1;
1766
1767 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001768 setup.fields |= MNCC_F_CALLING;
1769 strncpy(setup.calling.number, trans->subscr->extension,
1770 sizeof(setup.calling.number)-1);
1771 strncpy(setup.imsi, trans->subscr->imsi,
1772 sizeof(setup.imsi)-1);
1773
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 /* bearer capability */
1775 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1776 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001777 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1779 }
1780 /* facility */
1781 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1782 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001783 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001784 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1785 }
1786 /* called party bcd number */
1787 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1788 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001789 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1791 }
1792 /* user-user */
1793 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1794 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001795 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1797 }
1798 /* ss-version */
1799 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1800 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001801 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1803 }
1804 /* CLIR suppression */
1805 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1806 setup.clir.sup = 1;
1807 /* CLIR invocation */
1808 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1809 setup.clir.inv = 1;
1810 /* cc cap */
1811 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1812 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001813 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1815 }
1816
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 new_cc_state(trans, GSM_CSTATE_INITIATED);
1818
Harald Welte (local)47df3992009-12-26 19:45:03 +01001819 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1820 subscr_name(trans->subscr), trans->subscr->extension,
1821 setup.called.number);
1822
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001823 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001824
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001826 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827
Harald Welte13cac662009-07-29 12:10:35 +02001828 /* MNCC code will modify the channel asynchronously, we should
1829 * ipaccess-bind only after the modification has been made to the
1830 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 return 0;
1832}
1833
1834static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001835{
1836 struct msgb *msg = gsm48_msgb_alloc();
1837 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001838 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001839 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001840
Harald Welte7ccf7782009-02-17 01:43:01 +00001841 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001842
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 /* transaction id must not be assigned */
1844 if (trans->transaction_id != 0xff) { /* unasssigned */
1845 DEBUGP(DCC, "TX Setup with assigned transaction. "
1846 "This is not allowed!\n");
1847 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001848 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001849 GSM48_CAUSE_LOC_PRN_S_LU,
1850 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001852 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 return rc;
1854 }
1855
1856 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001857 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1858 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001860 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001861 GSM48_CAUSE_LOC_PRN_S_LU,
1862 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001864 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 return rc;
1866 }
Harald Welte78283ef2009-07-23 21:36:44 +02001867 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001868
Harald Welte65e74cc2008-12-29 01:55:35 +00001869 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001870
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001872
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 /* bearer capability */
1874 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001875 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 /* facility */
1877 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001878 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001879 /* progress */
1880 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001881 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 /* calling party BCD number */
1883 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001884 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001885 /* called party BCD number */
1886 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001887 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 /* user-user */
1889 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001890 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891 /* redirecting party BCD number */
1892 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001893 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 /* signal */
1895 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001896 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897
1898 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001899
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001900 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001901
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001902 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001903}
1904
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1906{
1907 struct gsm48_hdr *gh = msgb_l3(msg);
1908 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1909 struct tlv_parsed tp;
1910 struct gsm_mncc call_conf;
1911
1912 gsm48_stop_cc_timer(trans);
1913 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1914
1915 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1916 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001917 if (trans->conn && trans->conn->lchan)
1918 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001919 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920#if 0
1921 /* repeat */
1922 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1923 call_conf.repeat = 1;
1924 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1925 call_conf.repeat = 2;
1926#endif
1927 /* bearer capability */
1928 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1929 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001930 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1932 }
1933 /* cause */
1934 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1935 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001936 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1938 }
1939 /* cc cap */
1940 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1941 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1944 }
1945
1946 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1947
Harald Welte596fed42009-07-23 19:06:52 +02001948 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1949 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950}
1951
1952static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1953{
1954 struct gsm_mncc *proceeding = arg;
1955 struct msgb *msg = gsm48_msgb_alloc();
1956 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1957
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1959
1960 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1961
1962 /* bearer capability */
1963 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001964 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965 /* facility */
1966 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 /* progress */
1969 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001970 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001972 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973}
1974
1975static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1976{
1977 struct gsm48_hdr *gh = msgb_l3(msg);
1978 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1979 struct tlv_parsed tp;
1980 struct gsm_mncc alerting;
1981
1982 gsm48_stop_cc_timer(trans);
1983 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1984
1985 memset(&alerting, 0, sizeof(struct gsm_mncc));
1986 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001987 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* facility */
1989 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1990 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001991 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1993 }
1994
1995 /* progress */
1996 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1997 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001998 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2000 }
2001 /* ss-version */
2002 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2003 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2006 }
2007
2008 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2009
Harald Welte596fed42009-07-23 19:06:52 +02002010 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2011 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002012}
2013
2014static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2015{
2016 struct gsm_mncc *alerting = arg;
2017 struct msgb *msg = gsm48_msgb_alloc();
2018 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2019
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 gh->msg_type = GSM48_MT_CC_ALERTING;
2021
2022 /* facility */
2023 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 /* progress */
2026 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002027 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 /* user-user */
2029 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002030 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002031
2032 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2033
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002034 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035}
2036
2037static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2038{
2039 struct gsm_mncc *progress = arg;
2040 struct msgb *msg = gsm48_msgb_alloc();
2041 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2042
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 gh->msg_type = GSM48_MT_CC_PROGRESS;
2044
2045 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 /* user-user */
2048 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002049 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002050
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002051 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052}
2053
2054static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2055{
2056 struct gsm_mncc *connect = arg;
2057 struct msgb *msg = gsm48_msgb_alloc();
2058 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2059
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 gh->msg_type = GSM48_MT_CC_CONNECT;
2061
2062 gsm48_stop_cc_timer(trans);
2063 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2064
2065 /* facility */
2066 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002067 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 /* progress */
2069 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002070 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071 /* connected number */
2072 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002073 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002074 /* user-user */
2075 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002076 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002077
2078 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2079
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002080 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081}
2082
2083static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2084{
2085 struct gsm48_hdr *gh = msgb_l3(msg);
2086 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2087 struct tlv_parsed tp;
2088 struct gsm_mncc connect;
2089
2090 gsm48_stop_cc_timer(trans);
2091
2092 memset(&connect, 0, sizeof(struct gsm_mncc));
2093 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002094 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002096 connect.fields |= MNCC_F_CONNECTED;
2097 strncpy(connect.connected.number, trans->subscr->extension,
2098 sizeof(connect.connected.number)-1);
2099 strncpy(connect.imsi, trans->subscr->imsi,
2100 sizeof(connect.imsi)-1);
2101
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102 /* facility */
2103 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2104 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002105 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2107 }
2108 /* user-user */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2110 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2113 }
2114 /* ss-version */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2116 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2119 }
2120
2121 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002122 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123
Harald Welte596fed42009-07-23 19:06:52 +02002124 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002125}
2126
2127
2128static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2129{
2130 struct gsm_mncc connect_ack;
2131
2132 gsm48_stop_cc_timer(trans);
2133
2134 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002135 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136
2137 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2138 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002139
Harald Welte596fed42009-07-23 19:06:52 +02002140 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 &connect_ack);
2142}
2143
2144static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2145{
2146 struct msgb *msg = gsm48_msgb_alloc();
2147 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2148
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2150
2151 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2152
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002153 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002154}
2155
2156static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2157{
2158 struct gsm48_hdr *gh = msgb_l3(msg);
2159 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2160 struct tlv_parsed tp;
2161 struct gsm_mncc disc;
2162
2163 gsm48_stop_cc_timer(trans);
2164
2165 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2166
2167 memset(&disc, 0, sizeof(struct gsm_mncc));
2168 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002169 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002170 /* cause */
2171 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2172 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002173 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002174 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2175 }
2176 /* facility */
2177 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2178 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002179 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002180 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2181 }
2182 /* user-user */
2183 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2184 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002185 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2187 }
2188 /* ss-version */
2189 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2190 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002191 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002192 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2193 }
2194
Harald Welte596fed42009-07-23 19:06:52 +02002195 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196
2197}
2198
Harald Weltec66b71c2009-06-11 14:23:20 +08002199static struct gsm_mncc_cause default_cause = {
2200 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2201 .coding = 0,
2202 .rec = 0,
2203 .rec_val = 0,
2204 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2205 .diag_len = 0,
2206 .diag = { 0 },
2207};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208
2209static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2210{
2211 struct gsm_mncc *disc = arg;
2212 struct msgb *msg = gsm48_msgb_alloc();
2213 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2214
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2216
2217 gsm48_stop_cc_timer(trans);
2218 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2219
2220 /* cause */
2221 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002222 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 else
Harald Welte55c8f352010-03-07 23:40:35 +01002224 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225
2226 /* facility */
2227 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002228 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002229 /* progress */
2230 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002231 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 /* user-user */
2233 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002234 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235
2236 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002237 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238
2239 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2240
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002241 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242}
2243
2244static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2245{
2246 struct gsm48_hdr *gh = msgb_l3(msg);
2247 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2248 struct tlv_parsed tp;
2249 struct gsm_mncc rel;
2250 int rc;
2251
2252 gsm48_stop_cc_timer(trans);
2253
2254 memset(&rel, 0, sizeof(struct gsm_mncc));
2255 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002256 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 /* cause */
2258 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2259 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002260 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2262 }
2263 /* facility */
2264 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2265 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002266 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002267 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2268 }
2269 /* user-user */
2270 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2271 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002272 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2274 }
2275 /* ss-version */
2276 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2277 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002278 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2280 }
2281
Harald Weltedcaf5652009-07-23 18:56:43 +02002282 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002284 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002286 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002287 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002288 GSM48_MT_CC_RELEASE_COMPL);
2289 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 }
2291
2292 new_cc_state(trans, GSM_CSTATE_NULL);
2293
2294 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002295 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296
2297 return rc;
2298}
2299
2300static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2301{
2302 struct gsm_mncc *rel = arg;
2303 struct msgb *msg = gsm48_msgb_alloc();
2304 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2305
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 gh->msg_type = GSM48_MT_CC_RELEASE;
2307
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 gsm48_stop_cc_timer(trans);
2309 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2310
2311 /* cause */
2312 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002313 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 /* facility */
2315 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002316 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317 /* user-user */
2318 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002319 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320
Harald Weltedcaf5652009-07-23 18:56:43 +02002321 trans->cc.T308_second = 0;
2322 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323
Harald Weltedcaf5652009-07-23 18:56:43 +02002324 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2326
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002327 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2331{
2332 struct gsm48_hdr *gh = msgb_l3(msg);
2333 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2334 struct tlv_parsed tp;
2335 struct gsm_mncc rel;
2336 int rc = 0;
2337
2338 gsm48_stop_cc_timer(trans);
2339
2340 memset(&rel, 0, sizeof(struct gsm_mncc));
2341 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002342 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 /* cause */
2344 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2345 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002346 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002347 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2348 }
2349 /* facility */
2350 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2351 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002352 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2354 }
2355 /* user-user */
2356 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2357 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002358 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002359 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2360 }
2361 /* ss-version */
2362 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2363 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002364 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002365 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2366 }
2367
2368 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002369 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002371 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 MNCC_REJ_IND, &rel);
2373 break;
2374 case GSM_CSTATE_RELEASE_REQ:
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_CNF, &rel);
2377 break;
2378 default:
Harald Welte596fed42009-07-23 19:06:52 +02002379 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 MNCC_REL_IND, &rel);
2381 }
2382 }
2383
2384 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002385 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386
2387 return rc;
2388}
2389
2390static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2391{
2392 struct gsm_mncc *rel = arg;
2393 struct msgb *msg = gsm48_msgb_alloc();
2394 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002395 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002396
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2398
2399 trans->callref = 0;
2400
2401 gsm48_stop_cc_timer(trans);
2402
2403 /* cause */
2404 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 /* facility */
2407 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002408 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409 /* user-user */
2410 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002411 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412
Harald Weltec7782de2010-12-21 19:31:41 +01002413 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2414
Harald Weltedcaf5652009-07-23 18:56:43 +02002415 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002416
Harald Weltec7782de2010-12-21 19:31:41 +01002417 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418}
2419
2420static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2421{
2422 struct gsm48_hdr *gh = msgb_l3(msg);
2423 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2424 struct tlv_parsed tp;
2425 struct gsm_mncc fac;
2426
2427 memset(&fac, 0, sizeof(struct gsm_mncc));
2428 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002429 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 /* facility */
2431 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2432 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002433 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2435 }
2436 /* ss-version */
2437 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2438 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2441 }
2442
Harald Welte596fed42009-07-23 19:06:52 +02002443 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002444}
2445
2446static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2447{
2448 struct gsm_mncc *fac = arg;
2449 struct msgb *msg = gsm48_msgb_alloc();
2450 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2451
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 gh->msg_type = GSM48_MT_CC_FACILITY;
2453
2454 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002455 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002456
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002457 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458}
2459
2460static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2461{
2462 struct gsm_mncc hold;
2463
2464 memset(&hold, 0, sizeof(struct gsm_mncc));
2465 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002466 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467}
2468
2469static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2470{
2471 struct msgb *msg = gsm48_msgb_alloc();
2472 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2473
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2475
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002476 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002477}
2478
2479static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2480{
2481 struct gsm_mncc *hold_rej = arg;
2482 struct msgb *msg = gsm48_msgb_alloc();
2483 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2484
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2486
2487 /* cause */
2488 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002489 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 else
Harald Welte55c8f352010-03-07 23:40:35 +01002491 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002493 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2497{
2498 struct gsm_mncc retrieve;
2499
2500 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2501 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002502 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2503 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002504}
2505
2506static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2507{
2508 struct msgb *msg = gsm48_msgb_alloc();
2509 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2510
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2512
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002513 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002514}
2515
2516static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2517{
2518 struct gsm_mncc *retrieve_rej = arg;
2519 struct msgb *msg = gsm48_msgb_alloc();
2520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2523
2524 /* cause */
2525 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002526 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 else
Harald Welte55c8f352010-03-07 23:40:35 +01002528 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002530 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531}
2532
2533static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2534{
2535 struct gsm48_hdr *gh = msgb_l3(msg);
2536 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2537 struct tlv_parsed tp;
2538 struct gsm_mncc dtmf;
2539
2540 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2541 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002542 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 /* keypad facility */
2544 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2545 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002546 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2548 }
2549
Harald Welte596fed42009-07-23 19:06:52 +02002550 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551}
2552
2553static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2554{
2555 struct gsm_mncc *dtmf = arg;
2556 struct msgb *msg = gsm48_msgb_alloc();
2557 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2558
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2560
2561 /* keypad */
2562 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002563 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002565 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566}
2567
2568static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2569{
2570 struct gsm_mncc *dtmf = arg;
2571 struct msgb *msg = gsm48_msgb_alloc();
2572 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2573
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2575
2576 /* cause */
2577 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002578 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579 else
Harald Welte55c8f352010-03-07 23:40:35 +01002580 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002582 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583}
2584
2585static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2586{
2587 struct msgb *msg = gsm48_msgb_alloc();
2588 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2589
Harald Welte4bfdfe72009-06-10 23:11:52 +08002590 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2591
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002592 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593}
2594
2595static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2596{
2597 struct gsm_mncc dtmf;
2598
2599 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2600 dtmf.callref = trans->callref;
2601
Harald Welte596fed42009-07-23 19:06:52 +02002602 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603}
2604
2605static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2606{
2607 struct gsm48_hdr *gh = msgb_l3(msg);
2608 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2609 struct tlv_parsed tp;
2610 struct gsm_mncc modify;
2611
2612 memset(&modify, 0, sizeof(struct gsm_mncc));
2613 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002614 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615 /* bearer capability */
2616 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2617 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002618 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002619 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2620 }
2621
2622 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2623
Harald Welte596fed42009-07-23 19:06:52 +02002624 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625}
2626
2627static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2628{
2629 struct gsm_mncc *modify = arg;
2630 struct msgb *msg = gsm48_msgb_alloc();
2631 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2632
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 gh->msg_type = GSM48_MT_CC_MODIFY;
2634
2635 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2636
2637 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002638 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639
2640 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2641
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002642 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643}
2644
2645static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2646{
2647 struct gsm48_hdr *gh = msgb_l3(msg);
2648 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2649 struct tlv_parsed tp;
2650 struct gsm_mncc modify;
2651
2652 gsm48_stop_cc_timer(trans);
2653
2654 memset(&modify, 0, sizeof(struct gsm_mncc));
2655 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002656 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 /* bearer capability */
2658 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2659 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002660 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2662 }
2663
2664 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2665
Harald Welte596fed42009-07-23 19:06:52 +02002666 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667}
2668
2669static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2670{
2671 struct gsm_mncc *modify = arg;
2672 struct msgb *msg = gsm48_msgb_alloc();
2673 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2674
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2676
2677 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002678 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679
2680 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2681
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002682 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683}
2684
2685static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2686{
2687 struct gsm48_hdr *gh = msgb_l3(msg);
2688 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2689 struct tlv_parsed tp;
2690 struct gsm_mncc modify;
2691
2692 gsm48_stop_cc_timer(trans);
2693
2694 memset(&modify, 0, sizeof(struct gsm_mncc));
2695 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002696 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 /* bearer capability */
2698 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2699 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002700 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2702 }
2703 /* cause */
2704 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2705 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002706 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2708 }
2709
2710 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2711
Harald Welte596fed42009-07-23 19:06:52 +02002712 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002713}
2714
2715static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2716{
2717 struct gsm_mncc *modify = arg;
2718 struct msgb *msg = gsm48_msgb_alloc();
2719 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2720
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2722
2723 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002724 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002726 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727
2728 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2729
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002730 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002731}
2732
2733static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2734{
2735 struct gsm_mncc *notify = arg;
2736 struct msgb *msg = gsm48_msgb_alloc();
2737 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2738
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739 gh->msg_type = GSM48_MT_CC_NOTIFY;
2740
2741 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002742 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002743
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002744 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002745}
2746
2747static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2748{
2749 struct gsm48_hdr *gh = msgb_l3(msg);
2750 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2751// struct tlv_parsed tp;
2752 struct gsm_mncc notify;
2753
2754 memset(&notify, 0, sizeof(struct gsm_mncc));
2755 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002756// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002757 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002758 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002759
Harald Welte596fed42009-07-23 19:06:52 +02002760 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002761}
2762
2763static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2764{
2765 struct gsm_mncc *user = arg;
2766 struct msgb *msg = gsm48_msgb_alloc();
2767 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2768
Harald Welte4bfdfe72009-06-10 23:11:52 +08002769 gh->msg_type = GSM48_MT_CC_USER_INFO;
2770
2771 /* user-user */
2772 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002773 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 /* more data */
2775 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002776 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002778 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779}
2780
2781static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2782{
2783 struct gsm48_hdr *gh = msgb_l3(msg);
2784 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2785 struct tlv_parsed tp;
2786 struct gsm_mncc user;
2787
2788 memset(&user, 0, sizeof(struct gsm_mncc));
2789 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002790 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002791 /* user-user */
2792 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2793 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002794 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2796 }
2797 /* more data */
2798 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2799 user.more = 1;
2800
Harald Welte596fed42009-07-23 19:06:52 +02002801 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802}
2803
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002804static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002805{
2806 struct gsm_mncc *mode = arg;
2807
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002808 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002809}
2810
2811static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002812 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813 int type;
2814 int (*rout) (struct gsm_trans *trans, void *arg);
2815} downstatelist[] = {
2816 /* mobile originating call establishment */
2817 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2818 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2819 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2820 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2821 {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 */
2822 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2823 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2824 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2825 /* mobile terminating call establishment */
2826 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2827 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2828 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2829 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2830 /* signalling during call */
2831 {SBIT(GSM_CSTATE_ACTIVE),
2832 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2833 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2834 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2835 {ALL_STATES,
2836 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2837 {ALL_STATES,
2838 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2839 {ALL_STATES,
2840 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2841 {SBIT(GSM_CSTATE_ACTIVE),
2842 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2843 {SBIT(GSM_CSTATE_ACTIVE),
2844 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2845 {SBIT(GSM_CSTATE_ACTIVE),
2846 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2847 {SBIT(GSM_CSTATE_ACTIVE),
2848 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2849 {SBIT(GSM_CSTATE_ACTIVE),
2850 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2851 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2852 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2853 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2854 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2855 {SBIT(GSM_CSTATE_ACTIVE),
2856 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2857 /* clearing */
2858 {SBIT(GSM_CSTATE_INITIATED),
2859 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2860 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2861 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2862 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2863 MNCC_REL_REQ, gsm48_cc_tx_release},
2864 /* special */
2865 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002866 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867};
2868
2869#define DOWNSLLEN \
2870 (sizeof(downstatelist) / sizeof(struct downstate))
2871
2872
Harald Welte76556372010-12-22 23:57:45 +01002873int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002874{
Harald Welte1a6f7982009-08-09 18:52:33 +02002875 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002877 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002878 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 struct gsm_mncc *data = arg, rel;
2880
Harald Welte04dc88f2010-12-22 21:45:05 +01002881 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2882
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883 /* handle special messages */
2884 switch(msg_type) {
2885 case MNCC_BRIDGE:
2886 return tch_bridge(net, arg);
2887 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002888 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002890 return tch_recv_mncc(net, data->callref, 1);
2891 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002892 case GSM_TCHF_FRAME_EFR:
Harald Welteda7ab742009-12-19 22:23:05 +01002893 /* Find callref */
2894 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002895 if (!trans) {
2896 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002897 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002898 }
2899 if (!trans->conn) {
2900 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002901 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002902 }
2903 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2904 /* This should be LOGL_ERROR or NOTICE, but
2905 * unfortuantely it happens for a couple of frames at
2906 * the beginning of every RTP connection */
2907 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002908 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002909 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002910 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002911 switch (bts->type) {
2912 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002913 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002914 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002915 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002916 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002917 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002918 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002919 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002920 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002921 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002922 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002923 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002924 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002925 }
2926 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927 }
2928
2929 memset(&rel, 0, sizeof(struct gsm_mncc));
2930 rel.callref = data->callref;
2931
2932 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002933 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934
2935 /* Callref unknown */
2936 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002937 struct gsm_subscriber *subscr;
2938
Harald Welte4a3464c2009-07-04 10:11:24 +02002939 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2941 "Received '%s' from MNCC with "
2942 "unknown callref %d\n", data->called.number,
2943 get_mncc_name(msg_type), data->callref);
2944 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002945 return mncc_release_ind(net, NULL, data->callref,
2946 GSM48_CAUSE_LOC_PRN_S_LU,
2947 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002949 if (!data->called.number[0] && !data->imsi[0]) {
2950 DEBUGP(DCC, "(bts - trx - ts - ti) "
2951 "Received '%s' from MNCC with "
2952 "no number or IMSI\n", get_mncc_name(msg_type));
2953 /* Invalid number */
2954 return mncc_release_ind(net, NULL, data->callref,
2955 GSM48_CAUSE_LOC_PRN_S_LU,
2956 GSM48_CC_CAUSE_INV_NR_FORMAT);
2957 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002959 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002960 subscr = subscr_get_by_extension(net,
2961 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002962 else
Harald Welte9176bd42009-07-23 18:46:00 +02002963 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964 /* If subscriber is not found */
2965 if (!subscr) {
2966 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2967 "Received '%s' from MNCC with "
2968 "unknown subscriber %s\n", data->called.number,
2969 get_mncc_name(msg_type), data->called.number);
2970 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002971 return mncc_release_ind(net, NULL, data->callref,
2972 GSM48_CAUSE_LOC_PRN_S_LU,
2973 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002974 }
2975 /* If subscriber is not "attached" */
2976 if (!subscr->lac) {
2977 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2978 "Received '%s' from MNCC with "
2979 "detached subscriber %s\n", data->called.number,
2980 get_mncc_name(msg_type), data->called.number);
2981 subscr_put(subscr);
2982 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002983 return mncc_release_ind(net, NULL, data->callref,
2984 GSM48_CAUSE_LOC_PRN_S_LU,
2985 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002986 }
2987 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002988 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2989 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990 DEBUGP(DCC, "No memory for trans.\n");
2991 subscr_put(subscr);
2992 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002993 mncc_release_ind(net, NULL, data->callref,
2994 GSM48_CAUSE_LOC_PRN_S_LU,
2995 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002996 return -ENOMEM;
2997 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002998 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002999 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003000
Harald Welte4bfdfe72009-06-10 23:11:52 +08003001 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003002 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003003 /* find transaction with this subscriber already paging */
3004 llist_for_each_entry(transt, &net->trans_list, entry) {
3005 /* Transaction of our lchan? */
3006 if (transt == trans ||
3007 transt->subscr != subscr)
3008 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003009 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003010 "Received '%s' from MNCC with "
3011 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003012 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003013 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003014 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003015 subscr_put(subscr);
3016 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003017 return 0;
3018 }
3019 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003020 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003021
3022 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003023 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3024 if (!trans->paging_request) {
3025 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3026 subscr_put(subscr);
3027 trans_free(trans);
3028 return 0;
3029 }
3030
3031 *trans->paging_request = subscr->net;
3032 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003033
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003034 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 return 0;
3036 }
3037 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003038 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003039 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003041
3042 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003043 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044
3045 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003046 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003048 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 (trans->subscr)?(trans->subscr->extension):"-",
3050 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003051 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3052 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003053 if (msg_type == MNCC_REL_REQ)
3054 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3055 else
3056 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3057 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003058 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 return rc;
3060 }
3061
3062 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3063 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003064 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003065 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003066 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003067 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003068 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069
3070 /* Find function for current state and message */
3071 for (i = 0; i < DOWNSLLEN; i++)
3072 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003073 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003074 break;
3075 if (i == DOWNSLLEN) {
3076 DEBUGP(DCC, "Message unhandled at this state.\n");
3077 return 0;
3078 }
3079
3080 rc = downstatelist[i].rout(trans, arg);
3081
3082 return rc;
3083}
3084
3085
3086static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003087 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 int type;
3089 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3090} datastatelist[] = {
3091 /* mobile originating call establishment */
3092 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3093 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3094 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3095 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3096 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3097 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3098 /* mobile terminating call establishment */
3099 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3100 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3101 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3102 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003103 {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 +08003104 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3105 /* signalling during call */
3106 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3107 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3108 {SBIT(GSM_CSTATE_ACTIVE),
3109 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3110 {ALL_STATES,
3111 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3112 {ALL_STATES,
3113 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3114 {ALL_STATES,
3115 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3116 {SBIT(GSM_CSTATE_ACTIVE),
3117 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3118 {SBIT(GSM_CSTATE_ACTIVE),
3119 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3120 {SBIT(GSM_CSTATE_ACTIVE),
3121 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3122 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3123 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3124 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3125 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3126 {SBIT(GSM_CSTATE_ACTIVE),
3127 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3128 /* clearing */
3129 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3130 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3131 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3132 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3133 {ALL_STATES, /* 5.4.3.4 */
3134 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3135};
3136
3137#define DATASLLEN \
3138 (sizeof(datastatelist) / sizeof(struct datastate))
3139
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003140static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003141{
3142 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003143 uint8_t msg_type = gh->msg_type & 0xbf;
3144 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003145 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003146 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003147
Harald Welte4bfdfe72009-06-10 23:11:52 +08003148 if (msg_type & 0x80) {
3149 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3150 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003151 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003152
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003154 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003155
Harald Welte6f5aee02009-07-23 21:21:14 +02003156 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003158 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003159 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003160 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3161 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003162
3163 /* Create transaction */
3164 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003165 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003166 "creating new trans.\n", transaction_id);
3167 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003168 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003169 transaction_id, new_callref++);
3170 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003171 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003172 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003173 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003174 GSM48_MT_CC_RELEASE_COMPL);
3175 return -ENOMEM;
3176 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003178 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003179 }
3180
3181 /* find function for current state and message */
3182 for (i = 0; i < DATASLLEN; i++)
3183 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003184 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003185 break;
3186 if (i == DATASLLEN) {
3187 DEBUGP(DCC, "Message unhandled at this state.\n");
3188 return 0;
3189 }
3190
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003191 assert(trans->subscr);
3192
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003194
3195 return rc;
3196}
3197
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003198/* Create a dummy to wait five seconds */
3199static void release_anchor(struct gsm_subscriber_connection *conn)
3200{
3201 if (!conn->anch_operation)
3202 return;
3203
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003204 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003205 talloc_free(conn->anch_operation);
3206 conn->anch_operation = NULL;
3207}
3208
3209static void anchor_timeout(void *_data)
3210{
3211 struct gsm_subscriber_connection *con = _data;
3212
3213 release_anchor(con);
3214 msc_release_connection(con);
3215}
3216
3217int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3218{
3219 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3220 if (!conn->anch_operation)
3221 return -1;
3222
3223 conn->anch_operation->timeout.data = conn;
3224 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003225 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003226 return 0;
3227}
3228
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003229/* here we get data from the BSC level... */
3230int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003231{
3232 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003233 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003234 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003235
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003236 if (silent_call_reroute(conn, msg))
3237 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003238
3239 switch (pdisc) {
3240 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003241 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003242 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003243 break;
3244 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003245 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003246 break;
3247 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003248 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003249 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003250 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003251 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003252 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003253 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003254 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003255 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003256 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3257 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003258 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003259 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003260 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003261 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003262 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003263 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003264 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3265 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003266 break;
3267 }
3268
3269 return rc;
3270}
Harald Welte8470bf22008-12-25 23:28:35 +00003271
Harald Welte805f6442009-07-28 18:25:29 +02003272/*
3273 * This will be ran by the linker when loading the DSO. We use it to
3274 * do system initialization, e.g. registration of signal handlers.
3275 */
3276static __attribute__((constructor)) void on_dso_load_0408(void)
3277{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003278 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003279}