blob: 997e996bc7c118b9f51419af78899f62aa1ab058 [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{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100321 if (!conn->loc_operation)
322 return 0;
323
Sylvain Munaut267fba02009-12-24 00:28:01 +0100324 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800325 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100326 conn->loc_operation->key_seq,
327 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000328 return 0;
329}
330
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800331void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000332{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800333 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800334
335 /* avoid someone issuing a clear */
336 conn->in_release = 1;
337
Holger Freyther7c19f742009-06-06 13:54:35 +0000338 /*
339 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800340 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000341 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800342 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100343
344 /* We might need to cancel the paging response or such. */
345 if (conn->sec_operation && conn->sec_operation->cb) {
346 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
347 NULL, conn, conn->sec_operation->cb_data);
348 }
349
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800350 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800351 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000352
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100353 /*
354 * Free all transactions that are associated with the released
355 * connection. The transaction code will inform the CC or SMS
356 * facilities that will send the release indications. As part of
357 * the CC REL_IND the remote leg might be released and this will
358 * trigger the call to trans_free. This is something the llist
359 * macro can not handle and we will need to re-iterate the list.
360 *
361 * TODO: Move the trans_list into the subscriber connection and
362 * create a pending list for MT transactions. These exist before
363 * we have a subscriber connection.
364 */
365restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800366 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100367 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200368 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100369 goto restart;
370 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800371 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000372}
373
Harald Welte371efe52010-12-22 23:17:50 +0100374void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
375{
376 struct gsm_trans *trans, *temp;
377
378 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
379
380 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100381 if (trans->protocol == protocol) {
382 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100383 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100384 }
Harald Welte371efe52010-12-22 23:17:50 +0100385 }
386}
387
Holger Freyther429e7762008-12-30 13:28:30 +0000388/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200389int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000390{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800391 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800392 struct msgb *msg;
393
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200394 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800395
396 msg = gsm48_create_loc_upd_rej(cause);
397 if (!msg) {
398 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
399 return -1;
400 }
Harald Welte52b1f982008-12-23 20:25:15 +0000401
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800402 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000403
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100404 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100405 "LAC=%u BTS=%u\n", conn->subscr ?
406 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800407 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100408
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800409 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000410}
411
412/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200413int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000414{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800415 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000416 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000417 struct gsm48_hdr *gh;
418 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200419 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000420
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800421 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000422
423 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
424 gh->proto_discr = GSM48_PDISC_MM;
425 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
426
427 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100428 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000429 bts->network->network_code, bts->location_area_code);
430
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200431 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200432 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000433
434 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
435
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200436 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100437
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800438 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000439}
440
Harald Weltebf5e8df2009-02-03 12:59:45 +0000441/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000443{
444 struct msgb *msg = gsm48_msgb_alloc();
445 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000446
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800447 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000448
449 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
450 gh->proto_discr = GSM48_PDISC_MM;
451 gh->msg_type = GSM48_MT_MM_ID_REQ;
452 gh->data[0] = id_type;
453
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800454 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000455}
456
Harald Welte231ad4f2008-12-27 11:15:38 +0000457
Harald Weltebf5e8df2009-02-03 12:59:45 +0000458/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800459static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000460{
461 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000462 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200463 struct gsm_bts *bts = lchan->ts->trx->bts;
464 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200465 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200466 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000467
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200468 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000469 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000470 mi_type, mi_string);
471
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200472 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100473
Harald Welte75a983f2008-12-27 21:34:06 +0000474 switch (mi_type) {
475 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200476 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100477 if (!conn->subscr) {
478 conn->subscr = subscr_get_by_imsi(net, mi_string);
479 if (!conn->subscr)
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200480 conn->subscr = subscr_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200481 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100482 if (conn->loc_operation)
483 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000484 break;
485 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000486 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000487 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100488 if (conn->subscr) {
489 db_subscriber_assoc_imei(conn->subscr, mi_string);
490 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200491 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100492 if (conn->loc_operation)
493 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000494 break;
495 }
Holger Freyther73487a22008-12-31 18:53:57 +0000496
497 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100498 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000499}
500
Harald Welte255539c2008-12-28 02:26:27 +0000501
502static void loc_upd_rej_cb(void *data)
503{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100504 struct gsm_subscriber_connection *conn = data;
505 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100506 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000507
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100508 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800509 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800510 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000511}
512
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100513static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000514{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100515 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
516 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200517 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000518}
519
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200520static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000521{
522 switch (type) {
523 case GSM48_LUPD_NORMAL:
524 return "NORMAL";
525 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200526 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000527 case GSM48_LUPD_IMSI_ATT:
528 return "IMSI ATTACH";
529 default:
530 return "UNKNOWN";
531 }
532}
533
Harald Weltebf5e8df2009-02-03 12:59:45 +0000534/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800535static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000536{
Harald Welte8470bf22008-12-25 23:28:35 +0000537 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000538 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800539 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800540 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200541 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200542 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000543
Harald Welte8470bf22008-12-25 23:28:35 +0000544 lu = (struct gsm48_loc_upd_req *) gh->data;
545
546 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000547
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200548 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000549
Harald Weltea0368542009-06-27 02:58:43 +0200550 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000551 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000552
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200553 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100554
Harald Welte24ff6ee2009-12-22 00:41:05 +0100555 switch (lu->type) {
556 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200557 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100558 break;
559 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200560 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100561 break;
562 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200563 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100564 break;
565 }
566
Holger Freythereaf04692009-06-06 13:54:44 +0000567 /*
568 * Pseudo Spoof detection: Just drop a second/concurrent
569 * location updating request.
570 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100571 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200572 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100573 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800574 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000575 return 0;
576 }
577
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800578 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000579
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200580 conn->loc_operation->key_seq = lu->key_seq;
581
Harald Welte52b1f982008-12-23 20:25:15 +0000582 switch (mi_type) {
583 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200584 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000585 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200586 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100587 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000588
Jan Luebbe370b41d2009-08-12 10:19:34 +0200589 /* look up subscriber based on IMSI, create if not found */
590 subscr = subscr_get_by_imsi(bts->network, mi_string);
591 if (!subscr) {
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200592 subscr = subscr_create_subscriber(bts->network, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200593 }
Harald Welte4b634542008-12-27 01:55:51 +0000594 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000595 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200596 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000597 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200598 subscr = subscr_get_by_tmsi(bts->network,
599 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000600 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000601 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200602 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100603 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000604 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200605 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200606 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200607 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000608 break;
609 case GSM_MI_TYPE_IMEI:
610 case GSM_MI_TYPE_IMEISV:
611 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200612 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000613 break;
614 default:
Harald Weltea0368542009-06-27 02:58:43 +0200615 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000616 break;
617 }
618
Harald Welte24516ea2009-07-04 10:18:00 +0200619 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100620 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200621
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200623 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800624 /* FIXME: request id? close channel? */
625 return -EINVAL;
626 }
627
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100628 conn->subscr = subscr;
629 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000630
Harald Welte24516ea2009-07-04 10:18:00 +0200631 /* check if we can let the subscriber into our network immediately
632 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100633 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000634}
635
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100636/* Turn int into semi-octet representation: 98 => 0x89 */
637static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100639 uint8_t ret;
640
641 ret = value / 10;
642 ret |= (value % 10) << 4;
643
644 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800645}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100646
Harald Welte4bfdfe72009-06-10 23:11:52 +0800647
Harald Weltedb253af2008-12-30 17:56:55 +0000648/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800649int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000650{
651 struct msgb *msg = gsm48_msgb_alloc();
652 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800653 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100654 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200655 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200656 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100657
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100659 struct tm* gmt_time;
660 struct tm* local_time;
661 int tzunits;
Harald Weltedb253af2008-12-30 17:56:55 +0000662
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800663 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000664
665 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
666 gh->proto_discr = GSM48_PDISC_MM;
667 gh->msg_type = GSM48_MT_MM_INFO;
668
669 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200670#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000671 name_len = strlen(net->name_long);
672 /* 10.5.3.5a */
673 ptr8 = msgb_put(msg, 3);
674 ptr8[0] = GSM48_IE_NAME_LONG;
675 ptr8[1] = name_len*2 +1;
676 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
677
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200678 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000679 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000680 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000681
682 /* FIXME: Use Cell Broadcast, not UCS-2, since
683 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200684#endif
685 name_len = (strlen(net->name_long)*7)/8;
686 name_pad = (8 - strlen(net->name_long)*7)%8;
687 if (name_pad > 0)
688 name_len++;
689 /* 10.5.3.5a */
690 ptr8 = msgb_put(msg, 3);
691 ptr8[0] = GSM48_IE_NAME_LONG;
692 ptr8[1] = name_len +1;
693 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
694
695 ptr8 = msgb_put(msg, name_len);
696 gsm_7bit_encode(ptr8, net->name_long);
697
Harald Weltedb253af2008-12-30 17:56:55 +0000698 }
699
700 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200701#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000702 name_len = strlen(net->name_short);
703 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200704 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200705 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000706 ptr8[1] = name_len*2 + 1;
707 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
708
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200709 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000710 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000711 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200712#endif
713 name_len = (strlen(net->name_short)*7)/8;
714 name_pad = (8 - strlen(net->name_short)*7)%8;
715 if (name_pad > 0)
716 name_len++;
717 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200718 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200719 ptr8[0] = GSM48_IE_NAME_SHORT;
720 ptr8[1] = name_len +1;
721 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
722
723 ptr8 = msgb_put(msg, name_len);
724 gsm_7bit_encode(ptr8, net->name_short);
725
Harald Weltedb253af2008-12-30 17:56:55 +0000726 }
727
Harald Weltedb253af2008-12-30 17:56:55 +0000728 /* Section 10.5.3.9 */
729 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100730 gmt_time = gmtime(&cur_t);
731
Harald Weltedb253af2008-12-30 17:56:55 +0000732 ptr8 = msgb_put(msg, 8);
733 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100734 ptr8[1] = bcdify(gmt_time->tm_year % 100);
735 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
736 ptr8[3] = bcdify(gmt_time->tm_mday);
737 ptr8[4] = bcdify(gmt_time->tm_hour);
738 ptr8[5] = bcdify(gmt_time->tm_min);
739 ptr8[6] = bcdify(gmt_time->tm_sec);
740
Harald Welte45f91712012-07-08 16:48:11 +0200741 if (bts->tz.override) {
742 /* Convert tz.hr and tz.mn to units */
743 if (bts->tz.hr < 0) {
744 tzunits = ((bts->tz.hr/-1)*4);
745 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100746 ptr8[7] = bcdify(tzunits);
747 /* Set negative time */
748 ptr8[7] |= 0x08;
749 }
750 else {
Harald Welte45f91712012-07-08 16:48:11 +0200751 tzunits = bts->tz.hr*4;
752 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100753 ptr8[7] = bcdify(tzunits);
754 }
755 }
756 else {
757 /* Need to get GSM offset and convert into 15 min units */
758 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
759 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200760#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100761 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200762#else
763#warning find a portable way to obtain the timezone offset
764 tzunits = 0;
765#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100766 if (tzunits < 0) {
767 tzunits = tzunits/-1;
768 ptr8[7] = bcdify(tzunits);
769 /* Flip it to negative */
770 ptr8[7] |= 0x08;
771 }
772 else
773 ptr8[7] = bcdify(tzunits);
774 }
Harald Weltedb253af2008-12-30 17:56:55 +0000775
Daniel Willmanneea93372009-08-13 03:42:07 +0200776 DEBUGP(DMM, "-> MM INFO\n");
777
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800778 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000779}
780
Harald Welte7984d5c2009-08-12 22:56:50 +0200781/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200782int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200783{
784 struct msgb *msg = gsm48_msgb_alloc();
785 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200786 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200787
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200788 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200789
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800790 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200791 gh->proto_discr = GSM48_PDISC_MM;
792 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
793
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100794 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200795
Harald Welte7984d5c2009-08-12 22:56:50 +0200796 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200797 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200798 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200799
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800800 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200801}
802
803/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800804int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200805{
806 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800807 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200808}
809
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100810/*
811 * At the 30C3 phones miss their periodic update
812 * interval a lot and then remain unreachable. In case
813 * we still know the TMSI we can just attach it again.
814 */
815static void implit_attach(struct gsm_subscriber_connection *conn)
816{
817 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
818 return;
819
820 subscr_update(conn->subscr, conn->bts,
821 GSM_SUBSCRIBER_UPDATE_ATTACHED);
822}
823
824
Sylvain Munautba87f452009-12-24 00:28:46 +0100825static int _gsm48_rx_mm_serv_req_sec_cb(
826 unsigned int hooknum, unsigned int event,
827 struct msgb *msg, void *data, void *param)
828{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800829 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100830 int rc = 0;
831
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200832 /* auth failed or succeeded, the timer was stopped */
833 conn->expire_timer_stopped = 1;
834
Sylvain Munautba87f452009-12-24 00:28:46 +0100835 switch (event) {
836 case GSM_SECURITY_AUTH_FAILED:
837 /* Nothing to do */
838 break;
839
840 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200841 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800842 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100843 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100844 break;
845
846 case GSM_SECURITY_SUCCEEDED:
847 /* nothing to do. CIPHER MODE COMMAND is
848 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100849 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100850 break;
851
852 default:
853 rc = -EINVAL;
854 };
855
856 return rc;
857}
858
Harald Welte4ed0e922009-01-10 03:17:30 +0000859/*
860 * Handle CM Service Requests
861 * a) Verify that the packet is long enough to contain the information
862 * we require otherwsie reject with INCORRECT_MESSAGE
863 * b) Try to parse the TMSI. If we do not have one reject
864 * c) Check that we know the subscriber with the TMSI otherwise reject
865 * with a HLR cause
866 * d) Set the subscriber on the gsm_lchan and accept
867 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800868static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000869{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200870 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200871 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000872
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800873 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000874 struct gsm_subscriber *subscr;
875 struct gsm48_hdr *gh = msgb_l3(msg);
876 struct gsm48_service_request *req =
877 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800878 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200879 uint8_t classmark2_len = gh->data[1];
880 uint8_t *classmark2 = gh->data+2;
881 uint8_t mi_len = *(classmark2 + classmark2_len);
882 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000883
Harald Weltec9e02182009-05-01 19:07:53 +0000884 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000885 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000886 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800887 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000888 GSM48_REJECT_INCORRECT_MESSAGE);
889 }
890
891 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000892 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800893 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000894 GSM48_REJECT_INCORRECT_MESSAGE);
895 }
896
Harald Weltec9e02182009-05-01 19:07:53 +0000897 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000898 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000899 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800900 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000901 GSM48_REJECT_INCORRECT_MESSAGE);
902 }
903
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200904 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000905 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000906 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000907
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200908 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100909
Harald Welte3ac7f102009-08-10 10:12:45 +0200910 if (is_siemens_bts(bts))
911 send_siemens_mrpci(msg->lchan, classmark2-1);
912
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200913 subscr = subscr_get_by_tmsi(bts->network,
914 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000915
Harald Welte2a139372009-02-22 21:14:55 +0000916 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000917 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800918 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000919 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
920
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800921 if (!conn->subscr)
922 conn->subscr = subscr;
923 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100924 subscr_put(subscr); /* lchan already has a ref, don't need another one */
925 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000926 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
927 subscr_put(subscr);
928 }
929
Harald Weltec2e302d2009-07-05 14:08:13 +0200930 subscr->equipment.classmark2_len = classmark2_len;
931 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
932 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000933
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200934 /* we will send a MM message soon */
935 conn->expire_timer_stopped = 1;
936
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800937 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100938 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000939}
940
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200941static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000942{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200943 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000944 struct gsm48_hdr *gh = msgb_l3(msg);
945 struct gsm48_imsi_detach_ind *idi =
946 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200947 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200948 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800949 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000950
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200951 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100952 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000953 mi_type, mi_string);
954
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200955 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100956
Harald Welte2a139372009-02-22 21:14:55 +0000957 switch (mi_type) {
958 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100959 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200960 subscr = subscr_get_by_tmsi(bts->network,
961 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000962 break;
963 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100964 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +0200965 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000966 break;
967 case GSM_MI_TYPE_IMEI:
968 case GSM_MI_TYPE_IMEISV:
969 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +0100970 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000971 break;
972 default:
Daniel Willmann977cd132012-12-28 19:29:44 +0100973 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000974 break;
975 }
976
Holger Freyther4a49e772009-04-12 05:37:29 +0000977 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200978 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000979 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100980 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200981
982 subscr->equipment.classmark1 = idi->classmark1;
983 db_sync_equipment(&subscr->equipment);
984
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000985 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000986 } else
Harald Welte2a139372009-02-22 21:14:55 +0000987 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
988
Harald Welte31981a02009-12-20 09:58:40 +0100989 /* FIXME: iterate over all transactions and release them,
990 * imagine an IMSI DETACH happening during an active call! */
991
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200992 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +0000993 return 0;
994}
995
Harald Welted2a7f5a2009-06-05 20:08:20 +0000996static int gsm48_rx_mm_status(struct msgb *msg)
997{
998 struct gsm48_hdr *gh = msgb_l3(msg);
999
1000 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1001
1002 return 0;
1003}
1004
Sylvain Munaut30a15382009-12-24 00:27:26 +01001005/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001006static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001007{
1008 struct gsm48_hdr *gh = msgb_l3(msg);
1009 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001010 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001011
Sylvain Munautc593cf12010-06-10 23:51:41 +02001012 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001013 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001014
1015 /* Safety check */
1016 if (!conn->sec_operation) {
1017 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1018 return -EIO;
1019 }
1020
1021 /* Validate SRES */
1022 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001023 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001024 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001025
1026 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001027 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001028
Sylvain Munaut30a15382009-12-24 00:27:26 +01001029 if (cb)
1030 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001031 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001032
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001033 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001034 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001035 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001036 }
1037
Sylvain Munautc593cf12010-06-10 23:51:41 +02001038 DEBUGPC(DMM, "OK\n");
1039
Sylvain Munaut30a15382009-12-24 00:27:26 +01001040 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001041 return gsm0808_cipher_mode(conn, net->a5_encryption,
1042 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001043}
1044
Harald Weltebf5e8df2009-02-03 12:59:45 +00001045/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001046static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001047{
1048 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001049 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001050
1051 switch (gh->msg_type & 0xbf) {
1052 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001053 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001054 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001055 break;
1056 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001057 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001058 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001059 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001060 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001061 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001062 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001063 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001064 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001065 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001066 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001067 conn->subscr ?
1068 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001069 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001070 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001071 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001072 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001073 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001074 break;
1075 case GSM48_MT_MM_CM_REEST_REQ:
1076 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1077 break;
1078 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001079 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001080 break;
1081 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001082 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001083 gh->msg_type);
1084 break;
1085 }
1086
1087 return rc;
1088}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001089
Harald Welte2d35ae62009-02-06 12:02:13 +00001090/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001091static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001092{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001093 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001094 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001095 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001096 uint8_t *classmark2_lv = gh->data + 1;
1097 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001098 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001099 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001100 int rc = 0;
1101
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001102 resp = (struct gsm48_pag_resp *) &gh->data[0];
1103 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1104 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001105 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1106 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001107
Harald Weltefe18d8f2009-02-22 21:14:24 +00001108 switch (mi_type) {
1109 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001110 subscr = subscr_get_by_tmsi(bts->network,
1111 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001112 break;
1113 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001114 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001115 break;
1116 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001117
1118 if (!subscr) {
1119 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001120 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001121 return -EINVAL;
1122 }
1123 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001124 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001125
Harald Weltec2e302d2009-07-05 14:08:13 +02001126 subscr->equipment.classmark2_len = *classmark2_lv;
1127 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1128 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001129
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001130 /* We received a paging */
1131 conn->expire_timer_stopped = 1;
1132
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001133 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001134 return rc;
1135}
1136
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001137static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001138{
1139 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001140 uint8_t apdu_id_flags;
1141 uint8_t apdu_len;
1142 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001143
1144 apdu_id_flags = gh->data[0];
1145 apdu_len = gh->data[1];
1146 apdu_data = gh->data+2;
1147
Harald Welte70f92052012-07-16 13:22:19 +02001148 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001149 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001150
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001151 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001152}
1153
Harald Weltebf5e8df2009-02-03 12:59:45 +00001154/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001155static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001156{
1157 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001158 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001159
1160 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001161 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001162 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001163 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001164 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001165 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001166 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001167 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001168 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001169 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001170 break;
1171 }
1172
Harald Welte2d35ae62009-02-06 12:02:13 +00001173 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001174}
1175
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001176int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1177 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001178{
1179 struct msgb *msg = gsm48_msgb_alloc();
1180 struct gsm48_hdr *gh;
1181
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001182 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001183
1184 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1185 apdu_id, apdu_len);
1186
1187 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1188 gh->proto_discr = GSM48_PDISC_RR;
1189 gh->msg_type = GSM48_MT_RR_APP_INFO;
1190 gh->data[0] = apdu_id;
1191 gh->data[1] = apdu_len;
1192 memcpy(gh->data+2, apdu, apdu_len);
1193
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001194 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001195}
1196
Harald Welte4bc90a12008-12-27 16:32:52 +00001197/* Call Control */
1198
Harald Welte7584aea2009-02-11 11:44:12 +00001199/* The entire call control code is written in accordance with Figure 7.10c
1200 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1201 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1202 * it for voice */
1203
Harald Welte4bfdfe72009-06-10 23:11:52 +08001204static void new_cc_state(struct gsm_trans *trans, int state)
1205{
1206 if (state > 31 || state < 0)
1207 return;
1208
1209 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001210 gsm48_cc_state_name(trans->cc.state),
1211 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001212
Harald Weltedcaf5652009-07-23 18:56:43 +02001213 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001214}
1215
1216static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001217{
1218 struct msgb *msg = gsm48_msgb_alloc();
1219 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001220 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001221
Harald Welte4bc90a12008-12-27 16:32:52 +00001222 gh->msg_type = GSM48_MT_CC_STATUS;
1223
1224 cause = msgb_put(msg, 3);
1225 cause[0] = 2;
1226 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1227 cause[2] = 0x80 | 30; /* response to status inquiry */
1228
1229 call_state = msgb_put(msg, 1);
1230 call_state[0] = 0xc0 | 0x00;
1231
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001232 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001233}
1234
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001235static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001236 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001237{
1238 struct msgb *msg = gsm48_msgb_alloc();
1239 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1240
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001241 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001242
Harald Welte6f4b7532008-12-29 00:39:37 +00001243 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001244 gh->msg_type = msg_type;
1245
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001246 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001247}
1248
Harald Welte4bfdfe72009-06-10 23:11:52 +08001249static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1250{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001251 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001252 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001253 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001254 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001255 }
1256}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001257
Harald Welte4bfdfe72009-06-10 23:11:52 +08001258static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1259 int msg_type, struct gsm_mncc *mncc)
1260{
1261 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001262 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001263
1264 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001265 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001266 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001267 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001268 trans->conn->lchan->ts->trx->bts->nr,
1269 trans->conn->lchan->ts->trx->nr,
1270 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001271 (trans->subscr)?(trans->subscr->extension):"-",
1272 get_mncc_name(msg_type));
1273 else
1274 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1275 "Sending '%s' to MNCC.\n",
1276 (trans->subscr)?(trans->subscr->extension):"-",
1277 get_mncc_name(msg_type));
1278 else
1279 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1280 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1281
1282 mncc->msg_type = msg_type;
1283
Harald Welte966636f2009-06-26 19:39:35 +02001284 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285 if (!msg)
1286 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001287
1288 data = msgb_put(msg, sizeof(struct gsm_mncc));
1289 memcpy(data, mncc, sizeof(struct gsm_mncc));
1290
Harald Welte54209c22010-12-22 23:43:29 +01001291 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001292
1293 return 0;
1294}
1295
1296int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001297 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298{
1299 struct gsm_mncc rel;
1300
Harald Welte92f70c52009-06-12 01:54:08 +08001301 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001303 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001304 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1305 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1307}
1308
Harald Weltedcaf5652009-07-23 18:56:43 +02001309/* Call Control Specific transaction release.
1310 * gets called by trans_free, DO NOT CALL YOURSELF! */
1311void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001312{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001313 gsm48_stop_cc_timer(trans);
1314
1315 /* send release to L4, if callref still exists */
1316 if (trans->callref) {
1317 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001318 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001319 GSM48_CAUSE_LOC_PRN_S_LU,
1320 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001321 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001322 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001324 if (trans->conn)
1325 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326}
1327
1328static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001329
Harald Welte09e38af2009-02-16 22:52:23 +00001330/* call-back from paging the B-end of the connection */
1331static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001332 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001333{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001334 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001335 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001336 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001338
Harald Welte09e38af2009-02-16 22:52:23 +00001339 if (hooknum != GSM_HOOK_RR_PAGING)
1340 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001341
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001342 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001343 if (!net) {
1344 DEBUGP(DCC, "Error Network not set!\n");
1345 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001346 }
Harald Welte7584aea2009-02-11 11:44:12 +00001347
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 /* check all tranactions (without lchan) for subscriber */
1349 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001350 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351 continue;
1352 switch (event) {
1353 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001354 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 break;
1356 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001357 transt->subscr->extension);
1358 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001360 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001361 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001362 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001363 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364 }
1365 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001366 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001367 break;
1368 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001369 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001371 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001372 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001373 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001374 mncc_release_ind(transt->subscr->net, transt,
1375 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001376 GSM48_CAUSE_LOC_PRN_S_LU,
1377 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001378 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001379 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001380 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 break;
1382 }
1383 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001384
1385 talloc_free(paging_request);
1386
1387 /*
1388 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1389 * failure and then the subscr will be poke again. This needs a lot of fixing
1390 * in the subscriber queue code.
1391 */
1392 if (!found && conn)
1393 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001394 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001395}
Harald Welte7584aea2009-02-11 11:44:12 +00001396
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001397static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001398
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001399/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001400static int switch_for_handover(struct gsm_lchan *old_lchan,
1401 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001402{
1403 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001404
1405 if (ipacc_rtp_direct) {
1406 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1407 return 0;
1408 }
1409
1410 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001411 new_rs = new_lchan->abis_ip.rtp_socket;
1412 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001413
1414 if (!new_rs) {
1415 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1416 return -EIO;
1417 }
1418
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001419 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001420
1421 if (!old_rs) {
1422 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1423 return -EIO;
1424 }
1425
1426 /* copy rx_action and reference to other sock */
1427 new_rs->rx_action = old_rs->rx_action;
1428 new_rs->tx_action = old_rs->tx_action;
1429 new_rs->transmit = old_rs->transmit;
1430
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001431 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001432 case RTP_PROXY:
1433 other_rs = old_rs->proxy.other_sock;
1434 rtp_socket_proxy(new_rs, other_rs);
1435 /* delete reference to other end socket to prevent
1436 * rtp_socket_free() from removing the inverse reference */
1437 old_rs->proxy.other_sock = NULL;
1438 break;
1439 case RTP_RECV_UPSTREAM:
1440 new_rs->receive = old_rs->receive;
1441 break;
1442 case RTP_NONE:
1443 break;
1444 }
1445
1446 return 0;
1447}
1448
Harald Welte805f6442009-07-28 18:25:29 +02001449/* some other part of the code sends us a signal */
1450static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1451 void *handler_data, void *signal_data)
1452{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001453 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001454 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001455 struct gsm_network *net;
1456 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001457
1458 if (subsys != SS_ABISIP)
1459 return 0;
1460
1461 /* in case we use direct BTS-to-BTS RTP */
1462 if (ipacc_rtp_direct)
1463 return 0;
1464
Harald Welte805f6442009-07-28 18:25:29 +02001465 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001466 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001467 /* in case we don't use direct BTS-to-BTS RTP */
1468 /* the BTS has successfully bound a TCH to a local ip/port,
1469 * which means we can connect our UDP socket to it */
1470 if (lchan->abis_ip.rtp_socket) {
1471 rtp_socket_free(lchan->abis_ip.rtp_socket);
1472 lchan->abis_ip.rtp_socket = NULL;
1473 }
1474
1475 lchan->abis_ip.rtp_socket = rtp_socket_create();
1476 if (!lchan->abis_ip.rtp_socket)
1477 return -EIO;
1478
1479 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1480 lchan->abis_ip.bound_ip,
1481 lchan->abis_ip.bound_port);
1482 if (rc < 0)
1483 return -EIO;
1484
Harald Welteda7ab742009-12-19 22:23:05 +01001485 /* check if any transactions on this lchan still have
1486 * a tch_recv_mncc request pending */
1487 net = lchan->ts->trx->bts->network;
1488 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001489 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001490 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1491 tch_recv_mncc(net, trans->callref, 1);
1492 }
1493 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001494
1495 /*
1496 * TODO: this appears to be too early? Why not until after
1497 * the handover detect or the handover complete?
1498 *
1499 * Do we have a handover pending for this new lchan? In that
1500 * case re-route the audio from the old channel to the new one.
1501 */
1502 old_lchan = bsc_handover_pending(lchan);
1503 if (old_lchan)
1504 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001505 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001506 case S_ABISIP_DLCX_IND:
1507 /* the BTS tells us a RTP stream has been disconnected */
1508 if (lchan->abis_ip.rtp_socket) {
1509 rtp_socket_free(lchan->abis_ip.rtp_socket);
1510 lchan->abis_ip.rtp_socket = NULL;
1511 }
1512
1513 break;
Harald Welte805f6442009-07-28 18:25:29 +02001514 }
1515
1516 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001517}
1518
Harald Welte49f48b82009-02-17 15:29:33 +00001519/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001520static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001521{
Harald Welte11fa29c2009-02-19 17:24:39 +00001522 struct gsm_bts *bts = lchan->ts->trx->bts;
1523 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001524 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001525
Harald Welte11fa29c2009-02-19 17:24:39 +00001526 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1527 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1528 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1529
1530 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001531 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001532 return -EINVAL;
1533 }
Harald Welteda7ab742009-12-19 22:23:05 +01001534
1535 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001536 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001537 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001538 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001539 if (!ipacc_rtp_direct) {
1540 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001541 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001542 if (rc < 0)
1543 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001544 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001545 if (rc < 0)
1546 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001547 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301548 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1549 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001550 } else {
1551 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301552 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1553 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301554 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001555 if (rc < 0)
1556 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301557 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1558 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301559 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001560 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001561 break;
1562 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001563 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001564 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001565 trau_mux_map_lchan(lchan, remote_lchan);
1566 break;
1567 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001568 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001569 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001570 }
Harald Welte49f48b82009-02-17 15:29:33 +00001571
1572 return 0;
1573}
1574
Harald Welte4bfdfe72009-06-10 23:11:52 +08001575/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001576static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001577{
Harald Weltedcaf5652009-07-23 18:56:43 +02001578 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1579 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001580
Harald Welte4bfdfe72009-06-10 23:11:52 +08001581 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001582 return -EIO;
1583
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001584 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 return -EIO;
1586
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001587 /* Which subscriber do we want to track trans1 or trans2? */
1588 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1589
Harald Welte4bfdfe72009-06-10 23:11:52 +08001590 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001591 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001592}
1593
Harald Welteda7ab742009-12-19 22:23:05 +01001594/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001595static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596{
1597 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001598 struct gsm_lchan *lchan;
1599 struct gsm_bts *bts;
1600 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001601
Harald Welte4bfdfe72009-06-10 23:11:52 +08001602 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001603 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001604 if (!trans)
1605 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001606 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001607 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001608
1609 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001610 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001611 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001612
Harald Welteda7ab742009-12-19 22:23:05 +01001613 switch (bts->type) {
1614 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001615 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001616 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001617 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001618 return -EINVAL;
1619 }
1620 /* in case, we don't have a RTP socket yet, we note this
1621 * in the transaction and try later */
1622 if (!lchan->abis_ip.rtp_socket) {
1623 trans->tch_recv = enable;
1624 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1625 return 0;
1626 }
1627 if (enable) {
1628 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001629 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001630 if (rc < 0)
1631 return rc;
1632 /* assign socket to application interface */
1633 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1634 net, callref);
1635 } else
1636 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1637 net, 0);
1638 break;
1639 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001640 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001641 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001642 if (enable)
1643 return trau_recv_lchan(lchan, callref);
1644 return trau_mux_unmap(NULL, callref);
1645 break;
1646 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001647 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001648 return -EINVAL;
1649 }
1650
1651 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001652}
1653
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1655{
1656 DEBUGP(DCC, "-> STATUS ENQ\n");
1657 return gsm48_cc_tx_status(trans, msg);
1658}
1659
1660static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1661static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1662
1663static void gsm48_cc_timeout(void *arg)
1664{
1665 struct gsm_trans *trans = arg;
1666 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001667 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1668 int mo_location = GSM48_CAUSE_LOC_USER;
1669 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1670 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671 struct gsm_mncc mo_rel, l4_rel;
1672
1673 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1674 mo_rel.callref = trans->callref;
1675 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1676 l4_rel.callref = trans->callref;
1677
Harald Weltedcaf5652009-07-23 18:56:43 +02001678 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 case 0x303:
1680 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001681 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 break;
1683 case 0x310:
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 0x313:
1688 disconnect = 1;
1689 /* unknown, did not find it in the specs */
1690 break;
1691 case 0x301:
1692 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001693 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 break;
1695 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001696 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001698 gsm48_cc_tx_release(trans, &trans->cc.msg);
1699 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 break; /* stay in release state */
1701 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001702 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 return;
1704// release = 1;
1705// l4_cause = 14;
1706// break;
1707 case 0x306:
1708 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001709 mo_cause = trans->cc.msg.cause.value;
1710 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 break;
1712 case 0x323:
1713 disconnect = 1;
1714 break;
1715 default:
1716 release = 1;
1717 }
1718
1719 if (release && trans->callref) {
1720 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001721 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 l4_location, l4_cause);
1723 trans->callref = 0;
1724 }
1725
1726 if (disconnect && trans->callref) {
1727 /* process disconnect towards layer 4 */
1728 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001729 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 }
1731
1732 /* process disconnect towards mobile station */
1733 if (disconnect || release) {
1734 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001735 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1736 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1737 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001738 mo_rel.cause.diag_len = 3;
1739
1740 if (disconnect)
1741 gsm48_cc_tx_disconnect(trans, &mo_rel);
1742 if (release)
1743 gsm48_cc_tx_release(trans, &mo_rel);
1744 }
1745
1746}
1747
1748static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1749 int sec, int micro)
1750{
1751 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001752 trans->cc.timer.cb = gsm48_cc_timeout;
1753 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001754 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001755 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756}
1757
1758static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1759{
1760 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001761 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1763 struct tlv_parsed tp;
1764 struct gsm_mncc setup;
1765
1766 memset(&setup, 0, sizeof(struct gsm_mncc));
1767 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001768 if (trans->conn && trans->conn->lchan)
1769 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001770 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 /* emergency setup is identified by msg_type */
1772 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1773 setup.emergency = 1;
1774
1775 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001776 setup.fields |= MNCC_F_CALLING;
1777 strncpy(setup.calling.number, trans->subscr->extension,
1778 sizeof(setup.calling.number)-1);
1779 strncpy(setup.imsi, trans->subscr->imsi,
1780 sizeof(setup.imsi)-1);
1781
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 /* bearer capability */
1783 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1784 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001785 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1787 }
1788 /* facility */
1789 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1790 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001791 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1793 }
1794 /* called party bcd number */
1795 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1796 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001797 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1799 }
1800 /* user-user */
1801 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1802 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001803 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001804 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1805 }
1806 /* ss-version */
1807 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1808 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001809 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001810 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1811 }
1812 /* CLIR suppression */
1813 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1814 setup.clir.sup = 1;
1815 /* CLIR invocation */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1817 setup.clir.inv = 1;
1818 /* cc cap */
1819 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1820 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001821 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001822 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1823 }
1824
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 new_cc_state(trans, GSM_CSTATE_INITIATED);
1826
Harald Welte (local)47df3992009-12-26 19:45:03 +01001827 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1828 subscr_name(trans->subscr), trans->subscr->extension,
1829 setup.called.number);
1830
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001831 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001832
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001834 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835
Harald Welte13cac662009-07-29 12:10:35 +02001836 /* MNCC code will modify the channel asynchronously, we should
1837 * ipaccess-bind only after the modification has been made to the
1838 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001839 return 0;
1840}
1841
1842static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001843{
1844 struct msgb *msg = gsm48_msgb_alloc();
1845 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001847 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001848
Harald Welte7ccf7782009-02-17 01:43:01 +00001849 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001850
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 /* transaction id must not be assigned */
1852 if (trans->transaction_id != 0xff) { /* unasssigned */
1853 DEBUGP(DCC, "TX Setup with assigned transaction. "
1854 "This is not allowed!\n");
1855 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001856 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001857 GSM48_CAUSE_LOC_PRN_S_LU,
1858 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001860 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861 return rc;
1862 }
1863
1864 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001865 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1866 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001867 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001868 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001869 GSM48_CAUSE_LOC_PRN_S_LU,
1870 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001872 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873 return rc;
1874 }
Harald Welte78283ef2009-07-23 21:36:44 +02001875 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001876
Harald Welte65e74cc2008-12-29 01:55:35 +00001877 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001878
Harald Welte4bfdfe72009-06-10 23:11:52 +08001879 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001880
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* bearer capability */
1882 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 /* facility */
1885 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887 /* progress */
1888 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001889 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 /* calling party BCD number */
1891 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 /* called party BCD number */
1894 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001895 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 /* user-user */
1897 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 /* redirecting party BCD number */
1900 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001901 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902 /* signal */
1903 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905
1906 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001907
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001908 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001909
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001910 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001911}
1912
Harald Welte4bfdfe72009-06-10 23:11:52 +08001913static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1914{
1915 struct gsm48_hdr *gh = msgb_l3(msg);
1916 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1917 struct tlv_parsed tp;
1918 struct gsm_mncc call_conf;
1919
1920 gsm48_stop_cc_timer(trans);
1921 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1922
1923 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1924 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001925 if (trans->conn && trans->conn->lchan)
1926 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001927 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928#if 0
1929 /* repeat */
1930 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1931 call_conf.repeat = 1;
1932 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1933 call_conf.repeat = 2;
1934#endif
1935 /* bearer capability */
1936 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1937 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001938 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001939 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1940 }
1941 /* cause */
1942 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1943 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001944 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001945 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1946 }
1947 /* cc cap */
1948 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1949 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001950 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1952 }
1953
1954 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1955
Harald Welte596fed42009-07-23 19:06:52 +02001956 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1957 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958}
1959
1960static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1961{
1962 struct gsm_mncc *proceeding = arg;
1963 struct msgb *msg = gsm48_msgb_alloc();
1964 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1965
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1967
1968 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1969
1970 /* bearer capability */
1971 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001972 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 /* facility */
1974 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001975 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 /* progress */
1977 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001980 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981}
1982
1983static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1984{
1985 struct gsm48_hdr *gh = msgb_l3(msg);
1986 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1987 struct tlv_parsed tp;
1988 struct gsm_mncc alerting;
1989
1990 gsm48_stop_cc_timer(trans);
1991 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1992
1993 memset(&alerting, 0, sizeof(struct gsm_mncc));
1994 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001995 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 /* facility */
1997 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1998 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2001 }
2002
2003 /* progress */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2005 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2008 }
2009 /* ss-version */
2010 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2011 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2014 }
2015
2016 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2017
Harald Welte596fed42009-07-23 19:06:52 +02002018 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2019 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020}
2021
2022static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2023{
2024 struct gsm_mncc *alerting = arg;
2025 struct msgb *msg = gsm48_msgb_alloc();
2026 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2027
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 gh->msg_type = GSM48_MT_CC_ALERTING;
2029
2030 /* facility */
2031 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002032 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002033 /* progress */
2034 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002035 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002036 /* user-user */
2037 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002038 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039
2040 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2041
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002042 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043}
2044
2045static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2046{
2047 struct gsm_mncc *progress = arg;
2048 struct msgb *msg = gsm48_msgb_alloc();
2049 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2050
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 gh->msg_type = GSM48_MT_CC_PROGRESS;
2052
2053 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 /* user-user */
2056 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002057 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002059 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060}
2061
2062static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2063{
2064 struct gsm_mncc *connect = arg;
2065 struct msgb *msg = gsm48_msgb_alloc();
2066 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2067
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 gh->msg_type = GSM48_MT_CC_CONNECT;
2069
2070 gsm48_stop_cc_timer(trans);
2071 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2072
2073 /* facility */
2074 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 /* progress */
2077 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002078 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 /* connected number */
2080 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002081 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002082 /* user-user */
2083 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002084 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085
2086 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2087
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002088 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089}
2090
2091static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2092{
2093 struct gsm48_hdr *gh = msgb_l3(msg);
2094 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2095 struct tlv_parsed tp;
2096 struct gsm_mncc connect;
2097
2098 gsm48_stop_cc_timer(trans);
2099
2100 memset(&connect, 0, sizeof(struct gsm_mncc));
2101 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002102 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002104 connect.fields |= MNCC_F_CONNECTED;
2105 strncpy(connect.connected.number, trans->subscr->extension,
2106 sizeof(connect.connected.number)-1);
2107 strncpy(connect.imsi, trans->subscr->imsi,
2108 sizeof(connect.imsi)-1);
2109
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 /* facility */
2111 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2112 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002113 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2115 }
2116 /* user-user */
2117 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2118 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002119 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2121 }
2122 /* ss-version */
2123 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2124 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002125 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2127 }
2128
2129 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002130 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131
Harald Welte596fed42009-07-23 19:06:52 +02002132 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133}
2134
2135
2136static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2137{
2138 struct gsm_mncc connect_ack;
2139
2140 gsm48_stop_cc_timer(trans);
2141
2142 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002143 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144
2145 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2146 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002147
Harald Welte596fed42009-07-23 19:06:52 +02002148 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 &connect_ack);
2150}
2151
2152static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2153{
2154 struct msgb *msg = gsm48_msgb_alloc();
2155 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2156
Harald Welte4bfdfe72009-06-10 23:11:52 +08002157 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2158
2159 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2160
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002161 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162}
2163
2164static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2165{
2166 struct gsm48_hdr *gh = msgb_l3(msg);
2167 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2168 struct tlv_parsed tp;
2169 struct gsm_mncc disc;
2170
2171 gsm48_stop_cc_timer(trans);
2172
2173 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2174
2175 memset(&disc, 0, sizeof(struct gsm_mncc));
2176 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002177 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 /* cause */
2179 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2180 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002181 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002182 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2183 }
2184 /* facility */
2185 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2186 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002187 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2189 }
2190 /* user-user */
2191 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2192 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002193 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2195 }
2196 /* ss-version */
2197 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2198 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002199 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002200 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2201 }
2202
Harald Welte596fed42009-07-23 19:06:52 +02002203 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204
2205}
2206
Harald Weltec66b71c2009-06-11 14:23:20 +08002207static struct gsm_mncc_cause default_cause = {
2208 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2209 .coding = 0,
2210 .rec = 0,
2211 .rec_val = 0,
2212 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2213 .diag_len = 0,
2214 .diag = { 0 },
2215};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216
2217static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2218{
2219 struct gsm_mncc *disc = arg;
2220 struct msgb *msg = gsm48_msgb_alloc();
2221 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2222
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2224
2225 gsm48_stop_cc_timer(trans);
2226 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2227
2228 /* cause */
2229 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002230 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231 else
Harald Welte55c8f352010-03-07 23:40:35 +01002232 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233
2234 /* facility */
2235 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002236 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002237 /* progress */
2238 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002239 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 /* user-user */
2241 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002242 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243
2244 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002245 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246
2247 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2248
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002249 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002250}
2251
2252static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2253{
2254 struct gsm48_hdr *gh = msgb_l3(msg);
2255 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2256 struct tlv_parsed tp;
2257 struct gsm_mncc rel;
2258 int rc;
2259
2260 gsm48_stop_cc_timer(trans);
2261
2262 memset(&rel, 0, sizeof(struct gsm_mncc));
2263 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002264 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 /* cause */
2266 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2267 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002268 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2270 }
2271 /* facility */
2272 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2273 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2276 }
2277 /* user-user */
2278 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2279 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002280 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2282 }
2283 /* ss-version */
2284 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2285 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002286 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2288 }
2289
Harald Weltedcaf5652009-07-23 18:56:43 +02002290 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002292 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002294 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002295 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002296 GSM48_MT_CC_RELEASE_COMPL);
2297 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 }
2299
2300 new_cc_state(trans, GSM_CSTATE_NULL);
2301
2302 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002303 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304
2305 return rc;
2306}
2307
2308static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2309{
2310 struct gsm_mncc *rel = arg;
2311 struct msgb *msg = gsm48_msgb_alloc();
2312 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2313
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 gh->msg_type = GSM48_MT_CC_RELEASE;
2315
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316 gsm48_stop_cc_timer(trans);
2317 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2318
2319 /* cause */
2320 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002321 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 /* facility */
2323 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002324 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002325 /* user-user */
2326 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002327 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328
Harald Weltedcaf5652009-07-23 18:56:43 +02002329 trans->cc.T308_second = 0;
2330 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331
Harald Weltedcaf5652009-07-23 18:56:43 +02002332 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002333 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2334
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002335 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336}
2337
2338static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2339{
2340 struct gsm48_hdr *gh = msgb_l3(msg);
2341 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2342 struct tlv_parsed tp;
2343 struct gsm_mncc rel;
2344 int rc = 0;
2345
2346 gsm48_stop_cc_timer(trans);
2347
2348 memset(&rel, 0, sizeof(struct gsm_mncc));
2349 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002350 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 /* cause */
2352 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2353 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002354 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2356 }
2357 /* facility */
2358 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2359 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002360 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2362 }
2363 /* user-user */
2364 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2365 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002366 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2368 }
2369 /* ss-version */
2370 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2371 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002372 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002373 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2374 }
2375
2376 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002377 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002379 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 MNCC_REJ_IND, &rel);
2381 break;
2382 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002383 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 MNCC_REL_CNF, &rel);
2385 break;
2386 default:
Harald Welte596fed42009-07-23 19:06:52 +02002387 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 MNCC_REL_IND, &rel);
2389 }
2390 }
2391
2392 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002393 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394
2395 return rc;
2396}
2397
2398static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2399{
2400 struct gsm_mncc *rel = arg;
2401 struct msgb *msg = gsm48_msgb_alloc();
2402 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002403 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2406
2407 trans->callref = 0;
2408
2409 gsm48_stop_cc_timer(trans);
2410
2411 /* cause */
2412 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002413 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 /* facility */
2415 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002416 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417 /* user-user */
2418 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002419 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420
Harald Weltec7782de2010-12-21 19:31:41 +01002421 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2422
Harald Weltedcaf5652009-07-23 18:56:43 +02002423 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424
Harald Weltec7782de2010-12-21 19:31:41 +01002425 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426}
2427
2428static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2429{
2430 struct gsm48_hdr *gh = msgb_l3(msg);
2431 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2432 struct tlv_parsed tp;
2433 struct gsm_mncc fac;
2434
2435 memset(&fac, 0, sizeof(struct gsm_mncc));
2436 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002437 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 /* facility */
2439 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2440 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002441 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2443 }
2444 /* ss-version */
2445 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2446 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002447 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2449 }
2450
Harald Welte596fed42009-07-23 19:06:52 +02002451 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452}
2453
2454static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2455{
2456 struct gsm_mncc *fac = arg;
2457 struct msgb *msg = gsm48_msgb_alloc();
2458 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2459
Harald Welte4bfdfe72009-06-10 23:11:52 +08002460 gh->msg_type = GSM48_MT_CC_FACILITY;
2461
2462 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002463 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002465 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466}
2467
2468static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2469{
2470 struct gsm_mncc hold;
2471
2472 memset(&hold, 0, sizeof(struct gsm_mncc));
2473 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002474 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475}
2476
2477static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2478{
2479 struct msgb *msg = gsm48_msgb_alloc();
2480 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2481
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2483
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002484 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485}
2486
2487static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2488{
2489 struct gsm_mncc *hold_rej = arg;
2490 struct msgb *msg = gsm48_msgb_alloc();
2491 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2492
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2494
2495 /* cause */
2496 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002497 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498 else
Harald Welte55c8f352010-03-07 23:40:35 +01002499 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002501 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502}
2503
2504static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2505{
2506 struct gsm_mncc retrieve;
2507
2508 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2509 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002510 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2511 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512}
2513
2514static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2515{
2516 struct msgb *msg = gsm48_msgb_alloc();
2517 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2518
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2520
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002521 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522}
2523
2524static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2525{
2526 struct gsm_mncc *retrieve_rej = arg;
2527 struct msgb *msg = gsm48_msgb_alloc();
2528 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2529
Harald Welte4bfdfe72009-06-10 23:11:52 +08002530 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2531
2532 /* cause */
2533 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002534 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535 else
Harald Welte55c8f352010-03-07 23:40:35 +01002536 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002538 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539}
2540
2541static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2542{
2543 struct gsm48_hdr *gh = msgb_l3(msg);
2544 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2545 struct tlv_parsed tp;
2546 struct gsm_mncc dtmf;
2547
2548 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2549 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002550 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 /* keypad facility */
2552 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2553 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002554 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2556 }
2557
Harald Welte596fed42009-07-23 19:06:52 +02002558 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2562{
2563 struct gsm_mncc *dtmf = arg;
2564 struct msgb *msg = gsm48_msgb_alloc();
2565 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2566
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2568
2569 /* keypad */
2570 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002571 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002573 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574}
2575
2576static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2577{
2578 struct gsm_mncc *dtmf = arg;
2579 struct msgb *msg = gsm48_msgb_alloc();
2580 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2581
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2583
2584 /* cause */
2585 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002586 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587 else
Harald Welte55c8f352010-03-07 23:40:35 +01002588 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002590 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591}
2592
2593static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2594{
2595 struct msgb *msg = gsm48_msgb_alloc();
2596 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2597
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2599
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002600 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601}
2602
2603static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2604{
2605 struct gsm_mncc dtmf;
2606
2607 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2608 dtmf.callref = trans->callref;
2609
Harald Welte596fed42009-07-23 19:06:52 +02002610 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611}
2612
2613static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2614{
2615 struct gsm48_hdr *gh = msgb_l3(msg);
2616 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2617 struct tlv_parsed tp;
2618 struct gsm_mncc modify;
2619
2620 memset(&modify, 0, sizeof(struct gsm_mncc));
2621 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002622 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 /* bearer capability */
2624 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2625 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002626 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2628 }
2629
2630 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2631
Harald Welte596fed42009-07-23 19:06:52 +02002632 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *modify = arg;
2638 struct msgb *msg = gsm48_msgb_alloc();
2639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_MODIFY;
2642
2643 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2644
2645 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002646 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647
2648 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2649
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002650 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651}
2652
2653static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2654{
2655 struct gsm48_hdr *gh = msgb_l3(msg);
2656 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2657 struct tlv_parsed tp;
2658 struct gsm_mncc modify;
2659
2660 gsm48_stop_cc_timer(trans);
2661
2662 memset(&modify, 0, sizeof(struct gsm_mncc));
2663 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002664 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002665 /* bearer capability */
2666 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2667 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002668 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2670 }
2671
2672 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2673
Harald Welte596fed42009-07-23 19:06:52 +02002674 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675}
2676
2677static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2678{
2679 struct gsm_mncc *modify = arg;
2680 struct msgb *msg = gsm48_msgb_alloc();
2681 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2682
Harald Welte4bfdfe72009-06-10 23:11:52 +08002683 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2684
2685 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002686 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687
2688 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2689
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002690 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691}
2692
2693static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2694{
2695 struct gsm48_hdr *gh = msgb_l3(msg);
2696 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2697 struct tlv_parsed tp;
2698 struct gsm_mncc modify;
2699
2700 gsm48_stop_cc_timer(trans);
2701
2702 memset(&modify, 0, sizeof(struct gsm_mncc));
2703 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002704 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 /* bearer capability */
2706 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2707 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002708 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2710 }
2711 /* cause */
2712 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2713 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002714 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2716 }
2717
2718 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2719
Harald Welte596fed42009-07-23 19:06:52 +02002720 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721}
2722
2723static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2724{
2725 struct gsm_mncc *modify = arg;
2726 struct msgb *msg = gsm48_msgb_alloc();
2727 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2728
Harald Welte4bfdfe72009-06-10 23:11:52 +08002729 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2730
2731 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002732 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002734 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002735
2736 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2737
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002738 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002739}
2740
2741static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2742{
2743 struct gsm_mncc *notify = arg;
2744 struct msgb *msg = gsm48_msgb_alloc();
2745 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2746
Harald Welte4bfdfe72009-06-10 23:11:52 +08002747 gh->msg_type = GSM48_MT_CC_NOTIFY;
2748
2749 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002750 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002751
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002752 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002753}
2754
2755static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2756{
2757 struct gsm48_hdr *gh = msgb_l3(msg);
2758 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2759// struct tlv_parsed tp;
2760 struct gsm_mncc notify;
2761
2762 memset(&notify, 0, sizeof(struct gsm_mncc));
2763 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002764// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002765 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002766 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002767
Harald Welte596fed42009-07-23 19:06:52 +02002768 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002769}
2770
2771static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2772{
2773 struct gsm_mncc *user = arg;
2774 struct msgb *msg = gsm48_msgb_alloc();
2775 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2776
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777 gh->msg_type = GSM48_MT_CC_USER_INFO;
2778
2779 /* user-user */
2780 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002781 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782 /* more data */
2783 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002784 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002785
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002786 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002787}
2788
2789static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2790{
2791 struct gsm48_hdr *gh = msgb_l3(msg);
2792 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2793 struct tlv_parsed tp;
2794 struct gsm_mncc user;
2795
2796 memset(&user, 0, sizeof(struct gsm_mncc));
2797 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002798 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002799 /* user-user */
2800 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2801 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002802 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2804 }
2805 /* more data */
2806 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2807 user.more = 1;
2808
Harald Welte596fed42009-07-23 19:06:52 +02002809 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810}
2811
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002812static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002813{
2814 struct gsm_mncc *mode = arg;
2815
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002816 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817}
2818
2819static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002820 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002821 int type;
2822 int (*rout) (struct gsm_trans *trans, void *arg);
2823} downstatelist[] = {
2824 /* mobile originating call establishment */
2825 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2826 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2827 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2828 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2829 {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 */
2830 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2831 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2832 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2833 /* mobile terminating call establishment */
2834 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2835 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2836 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2837 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2838 /* signalling during call */
2839 {SBIT(GSM_CSTATE_ACTIVE),
2840 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2841 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2842 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2843 {ALL_STATES,
2844 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2845 {ALL_STATES,
2846 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2847 {ALL_STATES,
2848 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2849 {SBIT(GSM_CSTATE_ACTIVE),
2850 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2851 {SBIT(GSM_CSTATE_ACTIVE),
2852 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2853 {SBIT(GSM_CSTATE_ACTIVE),
2854 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2855 {SBIT(GSM_CSTATE_ACTIVE),
2856 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2857 {SBIT(GSM_CSTATE_ACTIVE),
2858 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2859 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2860 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2861 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2862 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2863 {SBIT(GSM_CSTATE_ACTIVE),
2864 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2865 /* clearing */
2866 {SBIT(GSM_CSTATE_INITIATED),
2867 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2868 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2869 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2870 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2871 MNCC_REL_REQ, gsm48_cc_tx_release},
2872 /* special */
2873 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002874 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875};
2876
2877#define DOWNSLLEN \
2878 (sizeof(downstatelist) / sizeof(struct downstate))
2879
2880
Harald Welte76556372010-12-22 23:57:45 +01002881int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002882{
Harald Welte1a6f7982009-08-09 18:52:33 +02002883 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002885 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002886 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002887 struct gsm_mncc *data = arg, rel;
2888
Harald Welte04dc88f2010-12-22 21:45:05 +01002889 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2890
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 /* handle special messages */
2892 switch(msg_type) {
2893 case MNCC_BRIDGE:
2894 return tch_bridge(net, arg);
2895 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002896 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002898 return tch_recv_mncc(net, data->callref, 1);
2899 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002900 case GSM_TCHF_FRAME_EFR:
Harald Welteda7ab742009-12-19 22:23:05 +01002901 /* Find callref */
2902 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002903 if (!trans) {
2904 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002905 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002906 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002907 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01002908 if (!trans->conn) {
2909 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002910 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002911 }
2912 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2913 /* This should be LOGL_ERROR or NOTICE, but
2914 * unfortuantely it happens for a couple of frames at
2915 * the beginning of every RTP connection */
2916 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002917 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002918 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002919 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002920 switch (bts->type) {
2921 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002922 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002923 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002924 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002925 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002926 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002927 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002928 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002929 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002930 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002931 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002932 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002933 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002934 }
2935 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 }
2937
2938 memset(&rel, 0, sizeof(struct gsm_mncc));
2939 rel.callref = data->callref;
2940
2941 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002942 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943
2944 /* Callref unknown */
2945 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002946 struct gsm_subscriber *subscr;
2947
Harald Welte4a3464c2009-07-04 10:11:24 +02002948 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2950 "Received '%s' from MNCC with "
2951 "unknown callref %d\n", data->called.number,
2952 get_mncc_name(msg_type), data->callref);
2953 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002954 return mncc_release_ind(net, NULL, data->callref,
2955 GSM48_CAUSE_LOC_PRN_S_LU,
2956 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002958 if (!data->called.number[0] && !data->imsi[0]) {
2959 DEBUGP(DCC, "(bts - trx - ts - ti) "
2960 "Received '%s' from MNCC with "
2961 "no number or IMSI\n", get_mncc_name(msg_type));
2962 /* Invalid number */
2963 return mncc_release_ind(net, NULL, data->callref,
2964 GSM48_CAUSE_LOC_PRN_S_LU,
2965 GSM48_CC_CAUSE_INV_NR_FORMAT);
2966 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002967 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002968 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002969 subscr = subscr_get_by_extension(net,
2970 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002971 else
Harald Welte9176bd42009-07-23 18:46:00 +02002972 subscr = subscr_get_by_imsi(net, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002973
2974 /* update the subscriber we deal with */
2975 log_set_context(BSC_CTX_SUBSCR, subscr);
2976
Harald Welte4bfdfe72009-06-10 23:11:52 +08002977 /* If subscriber is not found */
2978 if (!subscr) {
2979 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2980 "Received '%s' from MNCC with "
2981 "unknown subscriber %s\n", data->called.number,
2982 get_mncc_name(msg_type), data->called.number);
2983 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002984 return mncc_release_ind(net, NULL, data->callref,
2985 GSM48_CAUSE_LOC_PRN_S_LU,
2986 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002987 }
2988 /* If subscriber is not "attached" */
2989 if (!subscr->lac) {
2990 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2991 "Received '%s' from MNCC with "
2992 "detached subscriber %s\n", data->called.number,
2993 get_mncc_name(msg_type), data->called.number);
2994 subscr_put(subscr);
2995 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002996 return mncc_release_ind(net, NULL, data->callref,
2997 GSM48_CAUSE_LOC_PRN_S_LU,
2998 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002999 }
3000 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003001 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3002 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003003 DEBUGP(DCC, "No memory for trans.\n");
3004 subscr_put(subscr);
3005 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003006 mncc_release_ind(net, NULL, data->callref,
3007 GSM48_CAUSE_LOC_PRN_S_LU,
3008 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003009 return -ENOMEM;
3010 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003011 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003012 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003013
Harald Welte4bfdfe72009-06-10 23:11:52 +08003014 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003015 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003016 /* find transaction with this subscriber already paging */
3017 llist_for_each_entry(transt, &net->trans_list, entry) {
3018 /* Transaction of our lchan? */
3019 if (transt == trans ||
3020 transt->subscr != subscr)
3021 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003022 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023 "Received '%s' from MNCC with "
3024 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003025 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003026 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003027 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003028 subscr_put(subscr);
3029 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003030 return 0;
3031 }
3032 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003033 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003034
3035 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003036 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3037 if (!trans->paging_request) {
3038 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3039 subscr_put(subscr);
3040 trans_free(trans);
3041 return 0;
3042 }
3043
3044 *trans->paging_request = subscr->net;
3045 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003046
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003047 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 return 0;
3049 }
3050 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003051 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003052 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003053 } else {
3054 /* update the subscriber we deal with */
3055 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003057
3058 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003059 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060
3061 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003062 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003064 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003065 (trans->subscr)?(trans->subscr->extension):"-",
3066 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003067 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3068 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003069 if (msg_type == MNCC_REL_REQ)
3070 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3071 else
3072 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3073 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003074 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 return rc;
3076 }
3077
3078 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3079 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003080 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003081 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003082 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003083 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003084 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003085
3086 /* Find function for current state and message */
3087 for (i = 0; i < DOWNSLLEN; i++)
3088 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003089 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003090 break;
3091 if (i == DOWNSLLEN) {
3092 DEBUGP(DCC, "Message unhandled at this state.\n");
3093 return 0;
3094 }
3095
3096 rc = downstatelist[i].rout(trans, arg);
3097
3098 return rc;
3099}
3100
3101
3102static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003103 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003104 int type;
3105 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3106} datastatelist[] = {
3107 /* mobile originating call establishment */
3108 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3109 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3110 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3111 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3112 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3113 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3114 /* mobile terminating call establishment */
3115 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3116 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3117 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3118 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003119 {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 +08003120 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3121 /* signalling during call */
3122 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3123 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3124 {SBIT(GSM_CSTATE_ACTIVE),
3125 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3126 {ALL_STATES,
3127 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3128 {ALL_STATES,
3129 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3130 {ALL_STATES,
3131 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3132 {SBIT(GSM_CSTATE_ACTIVE),
3133 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3134 {SBIT(GSM_CSTATE_ACTIVE),
3135 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3136 {SBIT(GSM_CSTATE_ACTIVE),
3137 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3138 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3139 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3140 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3141 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3142 {SBIT(GSM_CSTATE_ACTIVE),
3143 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3144 /* clearing */
3145 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3146 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3147 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3148 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3149 {ALL_STATES, /* 5.4.3.4 */
3150 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3151};
3152
3153#define DATASLLEN \
3154 (sizeof(datastatelist) / sizeof(struct datastate))
3155
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003156static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003157{
3158 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003159 uint8_t msg_type = gh->msg_type & 0xbf;
3160 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003161 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003162 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003163
Harald Welte4bfdfe72009-06-10 23:11:52 +08003164 if (msg_type & 0x80) {
3165 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3166 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003167 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003168
Harald Welte4bfdfe72009-06-10 23:11:52 +08003169 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003170 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003171
Harald Welte6f5aee02009-07-23 21:21:14 +02003172 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003174 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003175 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003176 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3177 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003178
3179 /* Create transaction */
3180 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003181 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003182 "creating new trans.\n", transaction_id);
3183 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003184 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003185 transaction_id, new_callref++);
3186 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003187 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003188 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003189 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003190 GSM48_MT_CC_RELEASE_COMPL);
3191 return -ENOMEM;
3192 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003194 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003195 }
3196
3197 /* find function for current state and message */
3198 for (i = 0; i < DATASLLEN; i++)
3199 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003200 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003201 break;
3202 if (i == DATASLLEN) {
3203 DEBUGP(DCC, "Message unhandled at this state.\n");
3204 return 0;
3205 }
3206
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003207 assert(trans->subscr);
3208
Harald Welte4bfdfe72009-06-10 23:11:52 +08003209 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003210
3211 return rc;
3212}
3213
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003214/* Create a dummy to wait five seconds */
3215static void release_anchor(struct gsm_subscriber_connection *conn)
3216{
3217 if (!conn->anch_operation)
3218 return;
3219
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003220 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003221 talloc_free(conn->anch_operation);
3222 conn->anch_operation = NULL;
3223}
3224
3225static void anchor_timeout(void *_data)
3226{
3227 struct gsm_subscriber_connection *con = _data;
3228
3229 release_anchor(con);
3230 msc_release_connection(con);
3231}
3232
3233int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3234{
3235 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3236 if (!conn->anch_operation)
3237 return -1;
3238
3239 conn->anch_operation->timeout.data = conn;
3240 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003241 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003242 return 0;
3243}
3244
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003245/* here we get data from the BSC level... */
3246int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003247{
3248 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003249 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003250 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003251
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003252 if (silent_call_reroute(conn, msg))
3253 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003254
3255 switch (pdisc) {
3256 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003257 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003258 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003259 break;
3260 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003261 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003262 break;
3263 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003264 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003265 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003266 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003267 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003268 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003269 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003270 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003271 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003272 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3273 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003274 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003275 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003276 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003277 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003278 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003279 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003280 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3281 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003282 break;
3283 }
3284
3285 return rc;
3286}
Harald Welte8470bf22008-12-25 23:28:35 +00003287
Harald Welte805f6442009-07-28 18:25:29 +02003288/*
3289 * This will be ran by the linker when loading the DSO. We use it to
3290 * do system initialization, e.g. registration of signal handlers.
3291 */
3292static __attribute__((constructor)) void on_dso_load_0408(void)
3293{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003294 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003295}