blob: d1dfa4a72c9cad5272e5e02a6d381eb5a33c1ae4 [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 Freythere0009f12010-08-12 01:41:57 +08005 * (C) 2008-2010 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>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020054#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010055#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080056
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010057#include <osmocom/gsm/gsm48.h>
58#include <osmocom/gsm/gsm0480.h>
59#include <osmocom/gsm/gsm_utils.h>
60#include <osmocom/core/msgb.h>
61#include <osmocom/core/talloc.h>
62#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000063
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020064void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010065void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020066
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020067int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080068static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020069 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010070static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080071static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000072
Harald Welte52b1f982008-12-23 20:25:15 +000073struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020074 uint16_t mcc;
75 uint16_t mnc;
76 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000077};
78
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020079static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080080
Harald Welte31c00f72011-03-03 23:29:05 +010081void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
82{
83 net->mncc_recv(net, msg);
84}
85
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080086static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
87 struct gsm_trans *trans)
88{
89 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
90
91 /* if we get passed a transaction reference, do some common
92 * work that the caller no longer has to do */
93 if (trans) {
94 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
95 msg->lchan = trans->conn->lchan;
96 }
97
98
99 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200100 struct e1inp_sign_link *sign_link =
101 msg->lchan->ts->trx->rsl_link;
102
103 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800104 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
105 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200106 "Sending '%s' to MS.\n",
107 sign_link->trx->bts->nr,
108 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800109 gh->proto_discr & 0xf0,
110 gsm48_cc_msg_name(gh->msg_type));
111 else
112 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200113 "Sending 0x%02x to MS.\n",
114 sign_link->trx->bts->nr,
115 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800116 gh->proto_discr, gh->msg_type);
117 }
118
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100119 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800120}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100121
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800122int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
123{
124 struct gsm48_hdr *gh;
125 struct msgb *ss_notify;
126
127 ss_notify = gsm0480_create_notifySS(message);
128 if (!ss_notify)
129 return -1;
130
131 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
132 uint8_t *data = msgb_push(ss_notify, 1);
133 data[0] = ss_notify->len - 1;
134 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
135 gh->msg_type = GSM48_MT_CC_FACILITY;
136 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
137}
138
Harald Weltecf149ee2012-01-23 16:40:24 +0100139void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100140{
141 if (!conn->sec_operation)
142 return;
143
144 talloc_free(conn->sec_operation);
145 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800146 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100147}
148
Harald Weltecf149ee2012-01-23 16:40:24 +0100149void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100150{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100151 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
152 struct gsm_security_operation);
153}
154
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100155int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
156 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100157{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800158 struct gsm_network *net = conn->bts->network;
159 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100160 struct gsm_security_operation *op;
161 struct gsm_auth_tuple atuple;
162 int status = -1, rc;
163
164 /* Check if we _can_ enable encryption. Cases where we can't:
165 * - Encryption disabled in config
166 * - Channel already secured (nothing to do)
167 * - Subscriber equipment doesn't support configured encryption
168 */
169 if (!net->a5_encryption) {
170 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800171 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100172 DEBUGP(DMM, "Requesting to secure an already secure channel");
173 status = GSM_SECURITY_SUCCEEDED;
174 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
175 net->a5_encryption)) {
176 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
177 status = GSM_SECURITY_NOAVAIL;
178 }
179
180 /* If not done yet, try to get info for this user */
181 if (status < 0) {
182 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
183 if (rc <= 0)
184 status = GSM_SECURITY_NOAVAIL;
185 }
186
187 /* Are we done yet ? */
188 if (status >= 0)
189 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800190 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100191 0;
192
193 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800194 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100195 return -EBUSY;
196
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800197 allocate_security_operation(conn);
198 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100199 op->cb = cb;
200 op->cb_data = cb_data;
201 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
202
203 /* FIXME: Should start a timer for completion ... */
204
205 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100206 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100207 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800208 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100209 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100210 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100211 return gsm0808_cipher_mode(conn, net->a5_encryption,
212 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100213 }
214
215 return -EINVAL; /* not reached */
216}
217
Holger Freyther73487a22008-12-31 18:53:57 +0000218static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
219 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000220{
221 if (!subscriber)
222 return 0;
223
Holger Freyther73487a22008-12-31 18:53:57 +0000224 /*
225 * Do not send accept yet as more information should arrive. Some
226 * phones will not send us the information and we will have to check
227 * what we want to do with that.
228 */
229 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
230 return 0;
231
Jan Luebbe06513f22009-08-12 12:48:00 +0200232 switch (subscriber->net->auth_policy) {
233 case GSM_AUTH_POLICY_CLOSED:
234 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200235 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200236 if (subscriber->authorized)
237 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200238 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200239 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000240 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200241 default:
242 return 0;
243 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000244}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000245
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100246static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000247{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100248 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000249 return;
250
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800251 /* No need to keep the connection up */
252 release_anchor(conn);
253
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200254 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100255 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100256 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800257 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000258}
259
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100260static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000261{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800262 if (conn->loc_operation)
263 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100264 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000265
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100266 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200267 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000268}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000269
Sylvain Munaut267fba02009-12-24 00:28:01 +0100270static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
271 struct msgb *msg, void *data, void *param)
272{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800273 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100274 int rc = 0;
275
276 switch (event) {
277 case GSM_SECURITY_AUTH_FAILED:
278 release_loc_updating_req(conn);
279 break;
280
281 case GSM_SECURITY_NOAVAIL:
282 case GSM_SECURITY_SUCCEEDED:
283 /* We're all good */
284 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800285 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800286 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100287 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800288 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100289 }
290
291 /* call subscr_update after putting the loc_upd_acc
292 * in the transmit queue, since S_SUBSCR_ATTACHED might
293 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800294 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100295 GSM_SUBSCRIBER_UPDATE_ATTACHED);
296
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100297 /*
298 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
299 * MS needs to respond with a TMSI REALLOCATION COMPLETE
300 * (even if the TMSI is the same).
301 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100302 break;
303
304 default:
305 rc = -EINVAL;
306 };
307
308 return rc;
309}
310
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100311static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000312{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100313 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800314 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100315 conn->loc_operation->key_seq,
316 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000317 return 0;
318}
319
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800320void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000321{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800322 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800323
324 /* avoid someone issuing a clear */
325 conn->in_release = 1;
326
Holger Freyther7c19f742009-06-06 13:54:35 +0000327 /*
328 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800329 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000330 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800331 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100332
333 /* We might need to cancel the paging response or such. */
334 if (conn->sec_operation && conn->sec_operation->cb) {
335 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
336 NULL, conn, conn->sec_operation->cb_data);
337 }
338
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800339 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800340 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000341
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100342 /*
343 * Free all transactions that are associated with the released
344 * connection. The transaction code will inform the CC or SMS
345 * facilities that will send the release indications. As part of
346 * the CC REL_IND the remote leg might be released and this will
347 * trigger the call to trans_free. This is something the llist
348 * macro can not handle and we will need to re-iterate the list.
349 *
350 * TODO: Move the trans_list into the subscriber connection and
351 * create a pending list for MT transactions. These exist before
352 * we have a subscriber connection.
353 */
354restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800355 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100356 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200357 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100358 goto restart;
359 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800360 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000361}
362
Harald Welte371efe52010-12-22 23:17:50 +0100363void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
364{
365 struct gsm_trans *trans, *temp;
366
367 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
368
369 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100370 if (trans->protocol == protocol) {
371 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100372 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100373 }
Harald Welte371efe52010-12-22 23:17:50 +0100374 }
375}
376
Holger Freyther429e7762008-12-30 13:28:30 +0000377/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200378int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000379{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800380 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800381 struct msgb *msg;
382
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200383 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800384
385 msg = gsm48_create_loc_upd_rej(cause);
386 if (!msg) {
387 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
388 return -1;
389 }
Harald Welte52b1f982008-12-23 20:25:15 +0000390
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800391 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000392
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100393 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100394 "LAC=%u BTS=%u\n", conn->subscr ?
395 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800396 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100397
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800398 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000399}
400
401/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200402int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000403{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800404 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000405 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000406 struct gsm48_hdr *gh;
407 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200408 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000409
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800410 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000411
412 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
413 gh->proto_discr = GSM48_PDISC_MM;
414 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
415
416 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100417 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000418 bts->network->network_code, bts->location_area_code);
419
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200420 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200421 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000422
423 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
424
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200425 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100426
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800427 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000428}
429
Harald Weltebf5e8df2009-02-03 12:59:45 +0000430/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200431static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000432{
433 struct msgb *msg = gsm48_msgb_alloc();
434 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000435
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800436 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000437
438 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
439 gh->proto_discr = GSM48_PDISC_MM;
440 gh->msg_type = GSM48_MT_MM_ID_REQ;
441 gh->data[0] = id_type;
442
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800443 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000444}
445
Harald Welte231ad4f2008-12-27 11:15:38 +0000446
Harald Weltebf5e8df2009-02-03 12:59:45 +0000447/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800448static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000449{
450 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000451 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200452 struct gsm_bts *bts = lchan->ts->trx->bts;
453 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200454 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200455 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000456
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200457 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000458 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000459 mi_type, mi_string);
460
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200461 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100462
Harald Welte75a983f2008-12-27 21:34:06 +0000463 switch (mi_type) {
464 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200465 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100466 if (!conn->subscr) {
467 conn->subscr = subscr_get_by_imsi(net, mi_string);
468 if (!conn->subscr)
469 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200470 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100471 if (conn->loc_operation)
472 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000473 break;
474 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000475 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000476 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100477 if (conn->subscr) {
478 db_subscriber_assoc_imei(conn->subscr, mi_string);
479 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200480 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100481 if (conn->loc_operation)
482 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000483 break;
484 }
Holger Freyther73487a22008-12-31 18:53:57 +0000485
486 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100487 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000488}
489
Harald Welte255539c2008-12-28 02:26:27 +0000490
491static void loc_upd_rej_cb(void *data)
492{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100493 struct gsm_subscriber_connection *conn = data;
494 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100495 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000496
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100497 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800498 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800499 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000500}
501
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100502static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000503{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100504 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
505 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200506 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000507}
508
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200509static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000510{
511 switch (type) {
512 case GSM48_LUPD_NORMAL:
513 return "NORMAL";
514 case GSM48_LUPD_PERIODIC:
515 return "PEROIDOC";
516 case GSM48_LUPD_IMSI_ATT:
517 return "IMSI ATTACH";
518 default:
519 return "UNKNOWN";
520 }
521}
522
Harald Weltebf5e8df2009-02-03 12:59:45 +0000523/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800524static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000525{
Harald Welte8470bf22008-12-25 23:28:35 +0000526 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000527 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800528 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800529 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200530 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200531 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000532
Harald Welte8470bf22008-12-25 23:28:35 +0000533 lu = (struct gsm48_loc_upd_req *) gh->data;
534
535 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000536
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200537 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000538
Harald Weltea0368542009-06-27 02:58:43 +0200539 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000540 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000541
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200542 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100543
Harald Welte24ff6ee2009-12-22 00:41:05 +0100544 switch (lu->type) {
545 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200546 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100547 break;
548 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200549 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100550 break;
551 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200552 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100553 break;
554 }
555
Holger Freythereaf04692009-06-06 13:54:44 +0000556 /*
557 * Pseudo Spoof detection: Just drop a second/concurrent
558 * location updating request.
559 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100560 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200561 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100562 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800563 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000564 return 0;
565 }
566
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800567 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000568
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200569 conn->loc_operation->key_seq = lu->key_seq;
570
Harald Welte52b1f982008-12-23 20:25:15 +0000571 switch (mi_type) {
572 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200573 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000574 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200575 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100576 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000577
Jan Luebbe370b41d2009-08-12 10:19:34 +0200578 /* look up subscriber based on IMSI, create if not found */
579 subscr = subscr_get_by_imsi(bts->network, mi_string);
580 if (!subscr) {
581 subscr = db_create_subscriber(bts->network, mi_string);
582 }
Harald Welte4b634542008-12-27 01:55:51 +0000583 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000584 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200585 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000586 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200587 subscr = subscr_get_by_tmsi(bts->network,
588 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000589 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000590 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200591 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100592 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000593 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200594 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200595 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200596 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000597 break;
598 case GSM_MI_TYPE_IMEI:
599 case GSM_MI_TYPE_IMEISV:
600 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200601 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000602 break;
603 default:
Harald Weltea0368542009-06-27 02:58:43 +0200604 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000605 break;
606 }
607
Harald Welte24516ea2009-07-04 10:18:00 +0200608 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100609 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200610
Harald Welte4bfdfe72009-06-10 23:11:52 +0800611 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200612 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800613 /* FIXME: request id? close channel? */
614 return -EINVAL;
615 }
616
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100617 conn->subscr = subscr;
618 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000619
Harald Welte24516ea2009-07-04 10:18:00 +0200620 /* check if we can let the subscriber into our network immediately
621 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100622 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000623}
624
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100625/* Turn int into semi-octet representation: 98 => 0x89 */
626static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800627{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100628 uint8_t ret;
629
630 ret = value / 10;
631 ret |= (value % 10) << 4;
632
633 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800634}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100635
Harald Welte4bfdfe72009-06-10 23:11:52 +0800636
Harald Weltedb253af2008-12-30 17:56:55 +0000637/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800638int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000639{
640 struct msgb *msg = gsm48_msgb_alloc();
641 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800642 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100643 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200644 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200645 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100646
Harald Welte4bfdfe72009-06-10 23:11:52 +0800647 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100648 struct tm* gmt_time;
649 struct tm* local_time;
650 int tzunits;
Harald Weltedb253af2008-12-30 17:56:55 +0000651
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800652 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000653
654 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
655 gh->proto_discr = GSM48_PDISC_MM;
656 gh->msg_type = GSM48_MT_MM_INFO;
657
658 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200659#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000660 name_len = strlen(net->name_long);
661 /* 10.5.3.5a */
662 ptr8 = msgb_put(msg, 3);
663 ptr8[0] = GSM48_IE_NAME_LONG;
664 ptr8[1] = name_len*2 +1;
665 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
666
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200667 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000668 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000669 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000670
671 /* FIXME: Use Cell Broadcast, not UCS-2, since
672 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200673#endif
674 name_len = (strlen(net->name_long)*7)/8;
675 name_pad = (8 - strlen(net->name_long)*7)%8;
676 if (name_pad > 0)
677 name_len++;
678 /* 10.5.3.5a */
679 ptr8 = msgb_put(msg, 3);
680 ptr8[0] = GSM48_IE_NAME_LONG;
681 ptr8[1] = name_len +1;
682 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
683
684 ptr8 = msgb_put(msg, name_len);
685 gsm_7bit_encode(ptr8, net->name_long);
686
Harald Weltedb253af2008-12-30 17:56:55 +0000687 }
688
689 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200690#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000691 name_len = strlen(net->name_short);
692 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200693 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200694 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000695 ptr8[1] = name_len*2 + 1;
696 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
697
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200698 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000699 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000700 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200701#endif
702 name_len = (strlen(net->name_short)*7)/8;
703 name_pad = (8 - strlen(net->name_short)*7)%8;
704 if (name_pad > 0)
705 name_len++;
706 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200707 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200708 ptr8[0] = GSM48_IE_NAME_SHORT;
709 ptr8[1] = name_len +1;
710 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
711
712 ptr8 = msgb_put(msg, name_len);
713 gsm_7bit_encode(ptr8, net->name_short);
714
Harald Weltedb253af2008-12-30 17:56:55 +0000715 }
716
Harald Weltedb253af2008-12-30 17:56:55 +0000717 /* Section 10.5.3.9 */
718 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100719 gmt_time = gmtime(&cur_t);
720
Harald Weltedb253af2008-12-30 17:56:55 +0000721 ptr8 = msgb_put(msg, 8);
722 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100723 ptr8[1] = bcdify(gmt_time->tm_year % 100);
724 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
725 ptr8[3] = bcdify(gmt_time->tm_mday);
726 ptr8[4] = bcdify(gmt_time->tm_hour);
727 ptr8[5] = bcdify(gmt_time->tm_min);
728 ptr8[6] = bcdify(gmt_time->tm_sec);
729
Harald Welte45f91712012-07-08 16:48:11 +0200730 if (bts->tz.override) {
731 /* Convert tz.hr and tz.mn to units */
732 if (bts->tz.hr < 0) {
733 tzunits = ((bts->tz.hr/-1)*4);
734 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100735 ptr8[7] = bcdify(tzunits);
736 /* Set negative time */
737 ptr8[7] |= 0x08;
738 }
739 else {
Harald Welte45f91712012-07-08 16:48:11 +0200740 tzunits = bts->tz.hr*4;
741 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100742 ptr8[7] = bcdify(tzunits);
743 }
744 }
745 else {
746 /* Need to get GSM offset and convert into 15 min units */
747 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
748 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200749#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100750 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200751#else
752#warning find a portable way to obtain the timezone offset
753 tzunits = 0;
754#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100755 if (tzunits < 0) {
756 tzunits = tzunits/-1;
757 ptr8[7] = bcdify(tzunits);
758 /* Flip it to negative */
759 ptr8[7] |= 0x08;
760 }
761 else
762 ptr8[7] = bcdify(tzunits);
763 }
Harald Weltedb253af2008-12-30 17:56:55 +0000764
Daniel Willmanneea93372009-08-13 03:42:07 +0200765 DEBUGP(DMM, "-> MM INFO\n");
766
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800767 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000768}
769
Harald Welte7984d5c2009-08-12 22:56:50 +0200770/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200771int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200772{
773 struct msgb *msg = gsm48_msgb_alloc();
774 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200775 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200776
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200777 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200778
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800779 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200780 gh->proto_discr = GSM48_PDISC_MM;
781 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
782
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100783 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200784
Harald Welte7984d5c2009-08-12 22:56:50 +0200785 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200786 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200787 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200788
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800789 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200790}
791
792/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800793int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200794{
795 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800796 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200797}
798
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800799static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000800{
Harald Welte4b634542008-12-27 01:55:51 +0000801 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800802 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000803}
Harald Welteba4cf162009-01-10 01:49:35 +0000804
805/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100806static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000807 enum gsm48_reject_value value)
808{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800809 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000810
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800811 msg = gsm48_create_mm_serv_rej(value);
812 if (!msg) {
813 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
814 return -1;
815 }
Harald Welteba4cf162009-01-10 01:49:35 +0000816
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800817 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100818 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800819 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000820}
821
Sylvain Munautba87f452009-12-24 00:28:46 +0100822static int _gsm48_rx_mm_serv_req_sec_cb(
823 unsigned int hooknum, unsigned int event,
824 struct msgb *msg, void *data, void *param)
825{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800826 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100827 int rc = 0;
828
829 switch (event) {
830 case GSM_SECURITY_AUTH_FAILED:
831 /* Nothing to do */
832 break;
833
834 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800835 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100836 break;
837
838 case GSM_SECURITY_SUCCEEDED:
839 /* nothing to do. CIPHER MODE COMMAND is
840 * implicit CM SERV ACK */
841 break;
842
843 default:
844 rc = -EINVAL;
845 };
846
847 return rc;
848}
849
Harald Welte4ed0e922009-01-10 03:17:30 +0000850/*
851 * Handle CM Service Requests
852 * a) Verify that the packet is long enough to contain the information
853 * we require otherwsie reject with INCORRECT_MESSAGE
854 * b) Try to parse the TMSI. If we do not have one reject
855 * c) Check that we know the subscriber with the TMSI otherwise reject
856 * with a HLR cause
857 * d) Set the subscriber on the gsm_lchan and accept
858 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800859static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000860{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200861 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200862 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000863
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800864 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000865 struct gsm_subscriber *subscr;
866 struct gsm48_hdr *gh = msgb_l3(msg);
867 struct gsm48_service_request *req =
868 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800869 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200870 uint8_t classmark2_len = gh->data[1];
871 uint8_t *classmark2 = gh->data+2;
872 uint8_t mi_len = *(classmark2 + classmark2_len);
873 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000874
Harald Weltec9e02182009-05-01 19:07:53 +0000875 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000876 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000877 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800878 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000879 GSM48_REJECT_INCORRECT_MESSAGE);
880 }
881
882 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000883 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800884 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000885 GSM48_REJECT_INCORRECT_MESSAGE);
886 }
887
Harald Weltec9e02182009-05-01 19:07:53 +0000888 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000889 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000890 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800891 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000892 GSM48_REJECT_INCORRECT_MESSAGE);
893 }
894
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200895 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000896 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000897 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000898
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200899 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100900
Harald Welte3ac7f102009-08-10 10:12:45 +0200901 if (is_siemens_bts(bts))
902 send_siemens_mrpci(msg->lchan, classmark2-1);
903
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200904 subscr = subscr_get_by_tmsi(bts->network,
905 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000906
Harald Welte2a139372009-02-22 21:14:55 +0000907 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000908 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800909 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000910 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
911
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800912 if (!conn->subscr)
913 conn->subscr = subscr;
914 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100915 subscr_put(subscr); /* lchan already has a ref, don't need another one */
916 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000917 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
918 subscr_put(subscr);
919 }
920
Harald Weltec2e302d2009-07-05 14:08:13 +0200921 subscr->equipment.classmark2_len = classmark2_len;
922 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
923 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000924
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800925 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100926 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000927}
928
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200929static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000930{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200931 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000932 struct gsm48_hdr *gh = msgb_l3(msg);
933 struct gsm48_imsi_detach_ind *idi =
934 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200935 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200936 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800937 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000938
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200939 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000940 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
941 mi_type, mi_string);
942
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200943 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100944
Harald Welte2a139372009-02-22 21:14:55 +0000945 switch (mi_type) {
946 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200947 subscr = subscr_get_by_tmsi(bts->network,
948 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000949 break;
950 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200951 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000952 break;
953 case GSM_MI_TYPE_IMEI:
954 case GSM_MI_TYPE_IMEISV:
955 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000956 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000957 break;
958 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000959 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000960 break;
961 }
962
Holger Freyther4a49e772009-04-12 05:37:29 +0000963 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200964 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000965 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100966 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200967
968 subscr->equipment.classmark1 = idi->classmark1;
969 db_sync_equipment(&subscr->equipment);
970
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000971 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000972 } else
Harald Welte2a139372009-02-22 21:14:55 +0000973 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
974
Harald Welte31981a02009-12-20 09:58:40 +0100975 /* FIXME: iterate over all transactions and release them,
976 * imagine an IMSI DETACH happening during an active call! */
977
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200978 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +0000979 return 0;
980}
981
Harald Welted2a7f5a2009-06-05 20:08:20 +0000982static int gsm48_rx_mm_status(struct msgb *msg)
983{
984 struct gsm48_hdr *gh = msgb_l3(msg);
985
986 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
987
988 return 0;
989}
990
Sylvain Munaut30a15382009-12-24 00:27:26 +0100991/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800992static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100993{
994 struct gsm48_hdr *gh = msgb_l3(msg);
995 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800996 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100997
Sylvain Munautc593cf12010-06-10 23:51:41 +0200998 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200999 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001000
1001 /* Safety check */
1002 if (!conn->sec_operation) {
1003 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1004 return -EIO;
1005 }
1006
1007 /* Validate SRES */
1008 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001009 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001010 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001011
1012 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001013 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001014
Sylvain Munaut30a15382009-12-24 00:27:26 +01001015 if (cb)
1016 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001017 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001018
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001019 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001020 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001021 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001022 }
1023
Sylvain Munautc593cf12010-06-10 23:51:41 +02001024 DEBUGPC(DMM, "OK\n");
1025
Sylvain Munaut30a15382009-12-24 00:27:26 +01001026 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001027 return gsm0808_cipher_mode(conn, net->a5_encryption,
1028 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001029}
1030
Harald Weltebf5e8df2009-02-03 12:59:45 +00001031/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001032static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001033{
1034 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001035 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001036
1037 switch (gh->msg_type & 0xbf) {
1038 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001039 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001040 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001041 break;
1042 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001043 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001044 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001045 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001046 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001047 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001048 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001049 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001050 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001051 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001052 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001053 conn->subscr ?
1054 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001055 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001056 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001057 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001058 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001059 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001060 break;
1061 case GSM48_MT_MM_CM_REEST_REQ:
1062 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1063 break;
1064 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001065 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001066 break;
1067 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001068 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001069 gh->msg_type);
1070 break;
1071 }
1072
1073 return rc;
1074}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001075
Harald Welte2d35ae62009-02-06 12:02:13 +00001076/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001077static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001078{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001079 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001080 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001081 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001082 uint8_t *classmark2_lv = gh->data + 1;
1083 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001084 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001085 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001086 int rc = 0;
1087
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001088 resp = (struct gsm48_pag_resp *) &gh->data[0];
1089 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1090 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001091 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1092 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001093
Harald Weltefe18d8f2009-02-22 21:14:24 +00001094 switch (mi_type) {
1095 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001096 subscr = subscr_get_by_tmsi(bts->network,
1097 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001098 break;
1099 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001100 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001101 break;
1102 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001103
1104 if (!subscr) {
1105 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001106 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001107 return -EINVAL;
1108 }
1109 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001110 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001111
Harald Weltec2e302d2009-07-05 14:08:13 +02001112 subscr->equipment.classmark2_len = *classmark2_lv;
1113 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1114 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001115
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001116 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001117 return rc;
1118}
1119
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001120static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001121{
1122 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001123 uint8_t apdu_id_flags;
1124 uint8_t apdu_len;
1125 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001126
1127 apdu_id_flags = gh->data[0];
1128 apdu_len = gh->data[1];
1129 apdu_data = gh->data+2;
1130
Harald Welte70f92052012-07-16 13:22:19 +02001131 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001132 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001133
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001134 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001135}
1136
Harald Weltebf5e8df2009-02-03 12:59:45 +00001137/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001138static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001139{
1140 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001141 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001142
1143 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001144 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001145 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001146 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001147 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001148 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001149 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001150 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001151 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001152 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001153 break;
1154 }
1155
Harald Welte2d35ae62009-02-06 12:02:13 +00001156 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001157}
1158
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001159int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1160 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001161{
1162 struct msgb *msg = gsm48_msgb_alloc();
1163 struct gsm48_hdr *gh;
1164
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001165 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001166
1167 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1168 apdu_id, apdu_len);
1169
1170 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1171 gh->proto_discr = GSM48_PDISC_RR;
1172 gh->msg_type = GSM48_MT_RR_APP_INFO;
1173 gh->data[0] = apdu_id;
1174 gh->data[1] = apdu_len;
1175 memcpy(gh->data+2, apdu, apdu_len);
1176
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001177 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001178}
1179
Harald Welte4bc90a12008-12-27 16:32:52 +00001180/* Call Control */
1181
Harald Welte7584aea2009-02-11 11:44:12 +00001182/* The entire call control code is written in accordance with Figure 7.10c
1183 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1184 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1185 * it for voice */
1186
Harald Welte4bfdfe72009-06-10 23:11:52 +08001187static void new_cc_state(struct gsm_trans *trans, int state)
1188{
1189 if (state > 31 || state < 0)
1190 return;
1191
1192 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf192010-03-25 12:13:02 +08001193 gsm48_cc_state_name(trans->cc.state),
1194 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001195
Harald Weltedcaf5652009-07-23 18:56:43 +02001196 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001197}
1198
1199static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001200{
1201 struct msgb *msg = gsm48_msgb_alloc();
1202 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001203 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001204
Harald Welte4bc90a12008-12-27 16:32:52 +00001205 gh->msg_type = GSM48_MT_CC_STATUS;
1206
1207 cause = msgb_put(msg, 3);
1208 cause[0] = 2;
1209 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1210 cause[2] = 0x80 | 30; /* response to status inquiry */
1211
1212 call_state = msgb_put(msg, 1);
1213 call_state[0] = 0xc0 | 0x00;
1214
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001215 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001216}
1217
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001218static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001219 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001220{
1221 struct msgb *msg = gsm48_msgb_alloc();
1222 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1223
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001224 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001225
Harald Welte6f4b7532008-12-29 00:39:37 +00001226 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001227 gh->msg_type = msg_type;
1228
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001229 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001230}
1231
Harald Welte4bfdfe72009-06-10 23:11:52 +08001232static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1233{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001234 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001235 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001236 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001237 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001238 }
1239}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001240
Harald Welte4bfdfe72009-06-10 23:11:52 +08001241static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1242 int msg_type, struct gsm_mncc *mncc)
1243{
1244 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001245 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001246
1247 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001248 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001249 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001250 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001251 trans->conn->lchan->ts->trx->bts->nr,
1252 trans->conn->lchan->ts->trx->nr,
1253 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001254 (trans->subscr)?(trans->subscr->extension):"-",
1255 get_mncc_name(msg_type));
1256 else
1257 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1258 "Sending '%s' to MNCC.\n",
1259 (trans->subscr)?(trans->subscr->extension):"-",
1260 get_mncc_name(msg_type));
1261 else
1262 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1263 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1264
1265 mncc->msg_type = msg_type;
1266
Harald Welte966636f2009-06-26 19:39:35 +02001267 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001268 if (!msg)
1269 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001270
1271 data = msgb_put(msg, sizeof(struct gsm_mncc));
1272 memcpy(data, mncc, sizeof(struct gsm_mncc));
1273
Harald Welte54209c22010-12-22 23:43:29 +01001274 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275
1276 return 0;
1277}
1278
1279int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001280 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001281{
1282 struct gsm_mncc rel;
1283
Harald Welte92f70c52009-06-12 01:54:08 +08001284 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001286 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001287 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1288}
1289
Harald Weltedcaf5652009-07-23 18:56:43 +02001290/* Call Control Specific transaction release.
1291 * gets called by trans_free, DO NOT CALL YOURSELF! */
1292void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001293{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001294 gsm48_stop_cc_timer(trans);
1295
1296 /* send release to L4, if callref still exists */
1297 if (trans->callref) {
1298 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001299 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001300 GSM48_CAUSE_LOC_PRN_S_LU,
1301 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001303 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001304 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001305 if (trans->conn)
1306 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307}
1308
1309static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001310
Harald Welte09e38af2009-02-16 22:52:23 +00001311/* call-back from paging the B-end of the connection */
1312static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001313 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001314{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001315 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001316 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001317 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001319
Harald Welte09e38af2009-02-16 22:52:23 +00001320 if (hooknum != GSM_HOOK_RR_PAGING)
1321 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001322
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001323 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001324 if (!net) {
1325 DEBUGP(DCC, "Error Network not set!\n");
1326 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001327 }
Harald Welte7584aea2009-02-11 11:44:12 +00001328
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329 /* check all tranactions (without lchan) for subscriber */
1330 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001331 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001332 continue;
1333 switch (event) {
1334 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001335 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336 break;
1337 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001338 transt->subscr->extension);
1339 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001341 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001342 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001343 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001344 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 }
1346 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001347 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 break;
1349 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001350 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001352 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001353 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001354 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001355 mncc_release_ind(transt->subscr->net, transt,
1356 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001357 GSM48_CAUSE_LOC_PRN_S_LU,
1358 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001360 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001361 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 break;
1363 }
1364 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001365
1366 talloc_free(paging_request);
1367
1368 /*
1369 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1370 * failure and then the subscr will be poke again. This needs a lot of fixing
1371 * in the subscriber queue code.
1372 */
1373 if (!found && conn)
1374 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001375 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001376}
Harald Welte7584aea2009-02-11 11:44:12 +00001377
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001378static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001379
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001380/* handle audio path for handover */
1381static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1382 void *handler_data, void *signal_data)
1383{
1384 struct rtp_socket *old_rs, *new_rs, *other_rs;
1385 struct ho_signal_data *sig = signal_data;
1386
1387 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1388 return 0;
1389
1390 if (ipacc_rtp_direct) {
1391 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1392 return 0;
1393 }
1394
1395 /* RTP Proxy mode */
1396 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1397 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1398
1399 if (!new_rs) {
1400 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1401 return -EIO;
1402 }
1403
1404 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1405
1406 if (!old_rs) {
1407 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1408 return -EIO;
1409 }
1410
1411 /* copy rx_action and reference to other sock */
1412 new_rs->rx_action = old_rs->rx_action;
1413 new_rs->tx_action = old_rs->tx_action;
1414 new_rs->transmit = old_rs->transmit;
1415
1416 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1417 case RTP_PROXY:
1418 other_rs = old_rs->proxy.other_sock;
1419 rtp_socket_proxy(new_rs, other_rs);
1420 /* delete reference to other end socket to prevent
1421 * rtp_socket_free() from removing the inverse reference */
1422 old_rs->proxy.other_sock = NULL;
1423 break;
1424 case RTP_RECV_UPSTREAM:
1425 new_rs->receive = old_rs->receive;
1426 break;
1427 case RTP_NONE:
1428 break;
1429 }
1430
1431 return 0;
1432}
1433
Harald Welte805f6442009-07-28 18:25:29 +02001434/* some other part of the code sends us a signal */
1435static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1436 void *handler_data, void *signal_data)
1437{
1438 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001439 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001440 struct gsm_network *net;
1441 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001442
1443 if (subsys != SS_ABISIP)
1444 return 0;
1445
1446 /* in case we use direct BTS-to-BTS RTP */
1447 if (ipacc_rtp_direct)
1448 return 0;
1449
Harald Welte805f6442009-07-28 18:25:29 +02001450 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001451 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001452 /* in case we don't use direct BTS-to-BTS RTP */
1453 /* the BTS has successfully bound a TCH to a local ip/port,
1454 * which means we can connect our UDP socket to it */
1455 if (lchan->abis_ip.rtp_socket) {
1456 rtp_socket_free(lchan->abis_ip.rtp_socket);
1457 lchan->abis_ip.rtp_socket = NULL;
1458 }
1459
1460 lchan->abis_ip.rtp_socket = rtp_socket_create();
1461 if (!lchan->abis_ip.rtp_socket)
1462 return -EIO;
1463
1464 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1465 lchan->abis_ip.bound_ip,
1466 lchan->abis_ip.bound_port);
1467 if (rc < 0)
1468 return -EIO;
1469
Harald Welteda7ab742009-12-19 22:23:05 +01001470 /* check if any transactions on this lchan still have
1471 * a tch_recv_mncc request pending */
1472 net = lchan->ts->trx->bts->network;
1473 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001474 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001475 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1476 tch_recv_mncc(net, trans->callref, 1);
1477 }
1478 }
Harald Welte805f6442009-07-28 18:25:29 +02001479 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001480 case S_ABISIP_DLCX_IND:
1481 /* the BTS tells us a RTP stream has been disconnected */
1482 if (lchan->abis_ip.rtp_socket) {
1483 rtp_socket_free(lchan->abis_ip.rtp_socket);
1484 lchan->abis_ip.rtp_socket = NULL;
1485 }
1486
1487 break;
Harald Welte805f6442009-07-28 18:25:29 +02001488 }
1489
1490 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001491}
1492
Harald Welte49f48b82009-02-17 15:29:33 +00001493/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001494static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001495{
Harald Welte11fa29c2009-02-19 17:24:39 +00001496 struct gsm_bts *bts = lchan->ts->trx->bts;
1497 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001498 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001499
Harald Welte11fa29c2009-02-19 17:24:39 +00001500 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1501 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1502 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1503
1504 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001505 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001506 return -EINVAL;
1507 }
Harald Welteda7ab742009-12-19 22:23:05 +01001508
1509 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001510 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001511 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001512 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001513 if (!ipacc_rtp_direct) {
1514 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001515 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001516 if (rc < 0)
1517 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001518 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001519 if (rc < 0)
1520 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001521 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301522 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1523 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001524 } else {
1525 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301526 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1527 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301528 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001529 if (rc < 0)
1530 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301531 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1532 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301533 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001534 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001535 break;
1536 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001537 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001538 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001539 trau_mux_map_lchan(lchan, remote_lchan);
1540 break;
1541 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001542 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001543 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001544 }
Harald Welte49f48b82009-02-17 15:29:33 +00001545
1546 return 0;
1547}
1548
Harald Welte4bfdfe72009-06-10 23:11:52 +08001549/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001550static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001551{
Harald Weltedcaf5652009-07-23 18:56:43 +02001552 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1553 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001554
Harald Welte4bfdfe72009-06-10 23:11:52 +08001555 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001556 return -EIO;
1557
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001558 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001559 return -EIO;
1560
1561 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001562 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001563}
1564
Harald Welteda7ab742009-12-19 22:23:05 +01001565/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001566static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001567{
1568 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001569 struct gsm_lchan *lchan;
1570 struct gsm_bts *bts;
1571 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001572
Harald Welte4bfdfe72009-06-10 23:11:52 +08001573 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001574 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001575 if (!trans)
1576 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001577 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001578 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001579 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001580 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001581
Harald Welteda7ab742009-12-19 22:23:05 +01001582 switch (bts->type) {
1583 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001584 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001585 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001586 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001587 return -EINVAL;
1588 }
1589 /* in case, we don't have a RTP socket yet, we note this
1590 * in the transaction and try later */
1591 if (!lchan->abis_ip.rtp_socket) {
1592 trans->tch_recv = enable;
1593 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1594 return 0;
1595 }
1596 if (enable) {
1597 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001598 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001599 if (rc < 0)
1600 return rc;
1601 /* assign socket to application interface */
1602 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1603 net, callref);
1604 } else
1605 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1606 net, 0);
1607 break;
1608 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001609 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001610 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001611 if (enable)
1612 return trau_recv_lchan(lchan, callref);
1613 return trau_mux_unmap(NULL, callref);
1614 break;
1615 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001616 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001617 return -EINVAL;
1618 }
1619
1620 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621}
1622
Harald Welte4bfdfe72009-06-10 23:11:52 +08001623static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1624{
1625 DEBUGP(DCC, "-> STATUS ENQ\n");
1626 return gsm48_cc_tx_status(trans, msg);
1627}
1628
1629static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1630static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1631
1632static void gsm48_cc_timeout(void *arg)
1633{
1634 struct gsm_trans *trans = arg;
1635 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001636 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1637 int mo_location = GSM48_CAUSE_LOC_USER;
1638 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1639 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001640 struct gsm_mncc mo_rel, l4_rel;
1641
1642 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1643 mo_rel.callref = trans->callref;
1644 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1645 l4_rel.callref = trans->callref;
1646
Harald Weltedcaf5652009-07-23 18:56:43 +02001647 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001648 case 0x303:
1649 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001650 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001651 break;
1652 case 0x310:
1653 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001654 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655 break;
1656 case 0x313:
1657 disconnect = 1;
1658 /* unknown, did not find it in the specs */
1659 break;
1660 case 0x301:
1661 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001662 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001663 break;
1664 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001665 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001667 gsm48_cc_tx_release(trans, &trans->cc.msg);
1668 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 break; /* stay in release state */
1670 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001671 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001672 return;
1673// release = 1;
1674// l4_cause = 14;
1675// break;
1676 case 0x306:
1677 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001678 mo_cause = trans->cc.msg.cause.value;
1679 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 break;
1681 case 0x323:
1682 disconnect = 1;
1683 break;
1684 default:
1685 release = 1;
1686 }
1687
1688 if (release && trans->callref) {
1689 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001690 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001691 l4_location, l4_cause);
1692 trans->callref = 0;
1693 }
1694
1695 if (disconnect && trans->callref) {
1696 /* process disconnect towards layer 4 */
1697 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001698 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001699 }
1700
1701 /* process disconnect towards mobile station */
1702 if (disconnect || release) {
1703 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001704 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1705 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1706 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707 mo_rel.cause.diag_len = 3;
1708
1709 if (disconnect)
1710 gsm48_cc_tx_disconnect(trans, &mo_rel);
1711 if (release)
1712 gsm48_cc_tx_release(trans, &mo_rel);
1713 }
1714
1715}
1716
1717static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1718 int sec, int micro)
1719{
1720 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001721 trans->cc.timer.cb = gsm48_cc_timeout;
1722 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001723 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001724 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001725}
1726
1727static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1728{
1729 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001730 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1732 struct tlv_parsed tp;
1733 struct gsm_mncc setup;
1734
1735 memset(&setup, 0, sizeof(struct gsm_mncc));
1736 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001737 if (trans->conn && trans->conn->lchan)
1738 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001739 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 /* emergency setup is identified by msg_type */
1741 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1742 setup.emergency = 1;
1743
1744 /* use subscriber as calling party number */
1745 if (trans->subscr) {
1746 setup.fields |= MNCC_F_CALLING;
1747 strncpy(setup.calling.number, trans->subscr->extension,
1748 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001749 strncpy(setup.imsi, trans->subscr->imsi,
1750 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 }
1752 /* bearer capability */
1753 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1754 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001755 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001756 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1757 }
1758 /* facility */
1759 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1760 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001761 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001762 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1763 }
1764 /* called party bcd number */
1765 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1766 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001767 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1769 }
1770 /* user-user */
1771 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1772 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001773 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1775 }
1776 /* ss-version */
1777 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1778 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001779 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1781 }
1782 /* CLIR suppression */
1783 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1784 setup.clir.sup = 1;
1785 /* CLIR invocation */
1786 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1787 setup.clir.inv = 1;
1788 /* cc cap */
1789 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1790 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001791 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1793 }
1794
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 new_cc_state(trans, GSM_CSTATE_INITIATED);
1796
Harald Welte (local)47df3992009-12-26 19:45:03 +01001797 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1798 subscr_name(trans->subscr), trans->subscr->extension,
1799 setup.called.number);
1800
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001801 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001802
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001804 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805
Harald Welte13cac662009-07-29 12:10:35 +02001806 /* MNCC code will modify the channel asynchronously, we should
1807 * ipaccess-bind only after the modification has been made to the
1808 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 return 0;
1810}
1811
1812static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001813{
1814 struct msgb *msg = gsm48_msgb_alloc();
1815 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001816 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001817 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001818
Harald Welte7ccf7782009-02-17 01:43:01 +00001819 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001820
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 /* transaction id must not be assigned */
1822 if (trans->transaction_id != 0xff) { /* unasssigned */
1823 DEBUGP(DCC, "TX Setup with assigned transaction. "
1824 "This is not allowed!\n");
1825 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001826 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001827 GSM48_CAUSE_LOC_PRN_S_LU,
1828 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001830 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 return rc;
1832 }
1833
1834 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001835 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1836 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001838 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001839 GSM48_CAUSE_LOC_PRN_S_LU,
1840 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001841 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001842 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 return rc;
1844 }
Harald Welte78283ef2009-07-23 21:36:44 +02001845 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001846
Harald Welte65e74cc2008-12-29 01:55:35 +00001847 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001848
Harald Welte4bfdfe72009-06-10 23:11:52 +08001849 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001850
Harald Welte4bfdfe72009-06-10 23:11:52 +08001851 /* bearer capability */
1852 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001853 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 /* facility */
1855 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001856 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857 /* progress */
1858 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001859 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860 /* calling party BCD number */
1861 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001862 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 /* called party BCD number */
1864 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001865 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 /* user-user */
1867 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001868 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 /* redirecting party BCD number */
1870 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001871 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001872 /* signal */
1873 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001874 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875
1876 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001877
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001878 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001879
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001880 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001881}
1882
Harald Welte4bfdfe72009-06-10 23:11:52 +08001883static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1884{
1885 struct gsm48_hdr *gh = msgb_l3(msg);
1886 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1887 struct tlv_parsed tp;
1888 struct gsm_mncc call_conf;
1889
1890 gsm48_stop_cc_timer(trans);
1891 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1892
1893 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1894 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001895 if (trans->conn && trans->conn->lchan)
1896 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001897 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898#if 0
1899 /* repeat */
1900 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1901 call_conf.repeat = 1;
1902 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1903 call_conf.repeat = 2;
1904#endif
1905 /* bearer capability */
1906 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1907 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001908 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001909 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1910 }
1911 /* cause */
1912 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1913 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001914 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1916 }
1917 /* cc cap */
1918 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1919 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001920 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1922 }
1923
1924 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1925
Harald Welte596fed42009-07-23 19:06:52 +02001926 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1927 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928}
1929
1930static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1931{
1932 struct gsm_mncc *proceeding = arg;
1933 struct msgb *msg = gsm48_msgb_alloc();
1934 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1935
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1937
1938 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1939
1940 /* bearer capability */
1941 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 /* facility */
1944 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001945 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 /* progress */
1947 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001948 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001950 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951}
1952
1953static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1954{
1955 struct gsm48_hdr *gh = msgb_l3(msg);
1956 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1957 struct tlv_parsed tp;
1958 struct gsm_mncc alerting;
1959
1960 gsm48_stop_cc_timer(trans);
1961 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1962
1963 memset(&alerting, 0, sizeof(struct gsm_mncc));
1964 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001965 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 /* facility */
1967 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1968 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001969 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001970 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1971 }
1972
1973 /* progress */
1974 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1975 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001976 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1978 }
1979 /* ss-version */
1980 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1981 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001982 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001983 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1984 }
1985
1986 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1987
Harald Welte596fed42009-07-23 19:06:52 +02001988 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1989 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990}
1991
1992static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1993{
1994 struct gsm_mncc *alerting = arg;
1995 struct msgb *msg = gsm48_msgb_alloc();
1996 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1997
Harald Welte4bfdfe72009-06-10 23:11:52 +08001998 gh->msg_type = GSM48_MT_CC_ALERTING;
1999
2000 /* facility */
2001 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002002 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 /* progress */
2004 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002005 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 /* user-user */
2007 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002008 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009
2010 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2011
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002012 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013}
2014
2015static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2016{
2017 struct gsm_mncc *progress = arg;
2018 struct msgb *msg = gsm48_msgb_alloc();
2019 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2020
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 gh->msg_type = GSM48_MT_CC_PROGRESS;
2022
2023 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002024 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025 /* user-user */
2026 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002027 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002029 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030}
2031
2032static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2033{
2034 struct gsm_mncc *connect = arg;
2035 struct msgb *msg = gsm48_msgb_alloc();
2036 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2037
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038 gh->msg_type = GSM48_MT_CC_CONNECT;
2039
2040 gsm48_stop_cc_timer(trans);
2041 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2042
2043 /* facility */
2044 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002045 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002046 /* progress */
2047 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 /* connected number */
2050 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* user-user */
2053 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055
2056 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2057
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002058 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002059}
2060
2061static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2062{
2063 struct gsm48_hdr *gh = msgb_l3(msg);
2064 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2065 struct tlv_parsed tp;
2066 struct gsm_mncc connect;
2067
2068 gsm48_stop_cc_timer(trans);
2069
2070 memset(&connect, 0, sizeof(struct gsm_mncc));
2071 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002072 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073 /* use subscriber as connected party number */
2074 if (trans->subscr) {
2075 connect.fields |= MNCC_F_CONNECTED;
2076 strncpy(connect.connected.number, trans->subscr->extension,
2077 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002078 strncpy(connect.imsi, trans->subscr->imsi,
2079 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080 }
2081 /* facility */
2082 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2083 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002084 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2086 }
2087 /* user-user */
2088 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2089 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2092 }
2093 /* ss-version */
2094 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2095 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2098 }
2099
2100 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002101 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002102
Harald Welte596fed42009-07-23 19:06:52 +02002103 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104}
2105
2106
2107static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2108{
2109 struct gsm_mncc connect_ack;
2110
2111 gsm48_stop_cc_timer(trans);
2112
2113 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002114 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115
2116 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2117 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002118
Harald Welte596fed42009-07-23 19:06:52 +02002119 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002120 &connect_ack);
2121}
2122
2123static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2124{
2125 struct msgb *msg = gsm48_msgb_alloc();
2126 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2127
Harald Welte4bfdfe72009-06-10 23:11:52 +08002128 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2129
2130 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2131
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002132 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133}
2134
2135static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2136{
2137 struct gsm48_hdr *gh = msgb_l3(msg);
2138 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2139 struct tlv_parsed tp;
2140 struct gsm_mncc disc;
2141
2142 gsm48_stop_cc_timer(trans);
2143
2144 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2145
2146 memset(&disc, 0, sizeof(struct gsm_mncc));
2147 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002148 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 /* cause */
2150 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2151 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2154 }
2155 /* facility */
2156 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2157 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002158 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2160 }
2161 /* user-user */
2162 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2163 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002164 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2166 }
2167 /* ss-version */
2168 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2169 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2172 }
2173
Harald Welte596fed42009-07-23 19:06:52 +02002174 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175
2176}
2177
Harald Weltec66b71c2009-06-11 14:23:20 +08002178static struct gsm_mncc_cause default_cause = {
2179 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2180 .coding = 0,
2181 .rec = 0,
2182 .rec_val = 0,
2183 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2184 .diag_len = 0,
2185 .diag = { 0 },
2186};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187
2188static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2189{
2190 struct gsm_mncc *disc = arg;
2191 struct msgb *msg = gsm48_msgb_alloc();
2192 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2193
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2195
2196 gsm48_stop_cc_timer(trans);
2197 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2198
2199 /* cause */
2200 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002201 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002202 else
Harald Welte55c8f352010-03-07 23:40:35 +01002203 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204
2205 /* facility */
2206 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002207 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208 /* progress */
2209 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002210 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 /* user-user */
2212 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002213 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214
2215 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002216 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217
2218 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2219
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002220 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221}
2222
2223static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2224{
2225 struct gsm48_hdr *gh = msgb_l3(msg);
2226 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2227 struct tlv_parsed tp;
2228 struct gsm_mncc rel;
2229 int rc;
2230
2231 gsm48_stop_cc_timer(trans);
2232
2233 memset(&rel, 0, sizeof(struct gsm_mncc));
2234 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002235 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 /* cause */
2237 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2238 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002239 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002240 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2241 }
2242 /* facility */
2243 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2244 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002245 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2247 }
2248 /* user-user */
2249 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2250 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002251 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2253 }
2254 /* ss-version */
2255 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2256 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002257 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2259 }
2260
Harald Weltedcaf5652009-07-23 18:56:43 +02002261 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002262 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002263 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002264 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002265 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002266 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002267 GSM48_MT_CC_RELEASE_COMPL);
2268 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 }
2270
2271 new_cc_state(trans, GSM_CSTATE_NULL);
2272
2273 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002274 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275
2276 return rc;
2277}
2278
2279static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2280{
2281 struct gsm_mncc *rel = arg;
2282 struct msgb *msg = gsm48_msgb_alloc();
2283 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2284
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 gh->msg_type = GSM48_MT_CC_RELEASE;
2286
2287 trans->callref = 0;
2288
2289 gsm48_stop_cc_timer(trans);
2290 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2291
2292 /* cause */
2293 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002294 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002295 /* facility */
2296 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002297 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 /* user-user */
2299 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002300 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002301
Harald Weltedcaf5652009-07-23 18:56:43 +02002302 trans->cc.T308_second = 0;
2303 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304
Harald Weltedcaf5652009-07-23 18:56:43 +02002305 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2307
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002308 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309}
2310
2311static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2312{
2313 struct gsm48_hdr *gh = msgb_l3(msg);
2314 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2315 struct tlv_parsed tp;
2316 struct gsm_mncc rel;
2317 int rc = 0;
2318
2319 gsm48_stop_cc_timer(trans);
2320
2321 memset(&rel, 0, sizeof(struct gsm_mncc));
2322 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002323 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* cause */
2325 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2326 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002327 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2329 }
2330 /* facility */
2331 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2332 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002333 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2335 }
2336 /* user-user */
2337 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2338 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002339 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2341 }
2342 /* ss-version */
2343 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2344 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2347 }
2348
2349 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002350 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002352 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002353 MNCC_REJ_IND, &rel);
2354 break;
2355 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002356 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002357 MNCC_REL_CNF, &rel);
2358 break;
2359 default:
Harald Welte596fed42009-07-23 19:06:52 +02002360 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361 MNCC_REL_IND, &rel);
2362 }
2363 }
2364
2365 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002366 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367
2368 return rc;
2369}
2370
2371static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2372{
2373 struct gsm_mncc *rel = arg;
2374 struct msgb *msg = gsm48_msgb_alloc();
2375 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002376 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377
Harald Welte4bfdfe72009-06-10 23:11:52 +08002378 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2379
2380 trans->callref = 0;
2381
2382 gsm48_stop_cc_timer(trans);
2383
2384 /* cause */
2385 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002386 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387 /* facility */
2388 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002389 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390 /* user-user */
2391 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002392 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393
Harald Weltec7782de2010-12-21 19:31:41 +01002394 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2395
Harald Weltedcaf5652009-07-23 18:56:43 +02002396 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002397
Harald Weltec7782de2010-12-21 19:31:41 +01002398 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399}
2400
2401static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2402{
2403 struct gsm48_hdr *gh = msgb_l3(msg);
2404 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2405 struct tlv_parsed tp;
2406 struct gsm_mncc fac;
2407
2408 memset(&fac, 0, sizeof(struct gsm_mncc));
2409 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002410 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 /* facility */
2412 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2413 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002414 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2416 }
2417 /* ss-version */
2418 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2419 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002420 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2422 }
2423
Harald Welte596fed42009-07-23 19:06:52 +02002424 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002425}
2426
2427static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2428{
2429 struct gsm_mncc *fac = arg;
2430 struct msgb *msg = gsm48_msgb_alloc();
2431 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2432
Harald Welte4bfdfe72009-06-10 23:11:52 +08002433 gh->msg_type = GSM48_MT_CC_FACILITY;
2434
2435 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002436 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002438 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439}
2440
2441static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2442{
2443 struct gsm_mncc hold;
2444
2445 memset(&hold, 0, sizeof(struct gsm_mncc));
2446 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002447 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002448}
2449
2450static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2451{
2452 struct msgb *msg = gsm48_msgb_alloc();
2453 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2454
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2456
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002457 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002458}
2459
2460static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2461{
2462 struct gsm_mncc *hold_rej = arg;
2463 struct msgb *msg = gsm48_msgb_alloc();
2464 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2465
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2467
2468 /* cause */
2469 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002470 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 else
Harald Welte55c8f352010-03-07 23:40:35 +01002472 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002474 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475}
2476
2477static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2478{
2479 struct gsm_mncc retrieve;
2480
2481 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2482 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002483 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2484 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485}
2486
2487static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2488{
2489 struct msgb *msg = gsm48_msgb_alloc();
2490 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2491
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2493
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002494 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495}
2496
2497static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2498{
2499 struct gsm_mncc *retrieve_rej = arg;
2500 struct msgb *msg = gsm48_msgb_alloc();
2501 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2502
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2504
2505 /* cause */
2506 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002507 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 else
Harald Welte55c8f352010-03-07 23:40:35 +01002509 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002510
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002511 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002512}
2513
2514static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2515{
2516 struct gsm48_hdr *gh = msgb_l3(msg);
2517 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2518 struct tlv_parsed tp;
2519 struct gsm_mncc dtmf;
2520
2521 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2522 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002523 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002524 /* keypad facility */
2525 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2526 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002527 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002528 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2529 }
2530
Harald Welte596fed42009-07-23 19:06:52 +02002531 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002532}
2533
2534static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2535{
2536 struct gsm_mncc *dtmf = arg;
2537 struct msgb *msg = gsm48_msgb_alloc();
2538 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2539
Harald Welte4bfdfe72009-06-10 23:11:52 +08002540 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2541
2542 /* keypad */
2543 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002544 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002546 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547}
2548
2549static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2550{
2551 struct gsm_mncc *dtmf = arg;
2552 struct msgb *msg = gsm48_msgb_alloc();
2553 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2554
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2556
2557 /* cause */
2558 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002559 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002560 else
Harald Welte55c8f352010-03-07 23:40:35 +01002561 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002562
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002563 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564}
2565
2566static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2567{
2568 struct msgb *msg = gsm48_msgb_alloc();
2569 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2570
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2572
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_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2577{
2578 struct gsm_mncc dtmf;
2579
2580 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2581 dtmf.callref = trans->callref;
2582
Harald Welte596fed42009-07-23 19:06:52 +02002583 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002584}
2585
2586static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2587{
2588 struct gsm48_hdr *gh = msgb_l3(msg);
2589 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2590 struct tlv_parsed tp;
2591 struct gsm_mncc modify;
2592
2593 memset(&modify, 0, sizeof(struct gsm_mncc));
2594 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002595 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596 /* bearer capability */
2597 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2598 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002599 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002600 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2601 }
2602
2603 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2604
Harald Welte596fed42009-07-23 19:06:52 +02002605 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606}
2607
2608static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2609{
2610 struct gsm_mncc *modify = arg;
2611 struct msgb *msg = gsm48_msgb_alloc();
2612 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2613
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614 gh->msg_type = GSM48_MT_CC_MODIFY;
2615
2616 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2617
2618 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002619 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002620
2621 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2622
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002623 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002624}
2625
2626static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2627{
2628 struct gsm48_hdr *gh = msgb_l3(msg);
2629 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2630 struct tlv_parsed tp;
2631 struct gsm_mncc modify;
2632
2633 gsm48_stop_cc_timer(trans);
2634
2635 memset(&modify, 0, sizeof(struct gsm_mncc));
2636 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002637 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 /* bearer capability */
2639 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2640 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002641 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2643 }
2644
2645 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2646
Harald Welte596fed42009-07-23 19:06:52 +02002647 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2651{
2652 struct gsm_mncc *modify = arg;
2653 struct msgb *msg = gsm48_msgb_alloc();
2654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2655
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2657
2658 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002659 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660
2661 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2662
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002663 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002664}
2665
2666static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2667{
2668 struct gsm48_hdr *gh = msgb_l3(msg);
2669 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2670 struct tlv_parsed tp;
2671 struct gsm_mncc modify;
2672
2673 gsm48_stop_cc_timer(trans);
2674
2675 memset(&modify, 0, sizeof(struct gsm_mncc));
2676 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002677 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002678 /* bearer capability */
2679 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2680 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002681 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2683 }
2684 /* cause */
2685 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2686 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002687 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002688 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2689 }
2690
2691 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2692
Harald Welte596fed42009-07-23 19:06:52 +02002693 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002694}
2695
2696static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2697{
2698 struct gsm_mncc *modify = arg;
2699 struct msgb *msg = gsm48_msgb_alloc();
2700 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2701
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2703
2704 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002705 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002707 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002708
2709 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2710
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002711 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712}
2713
2714static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2715{
2716 struct gsm_mncc *notify = arg;
2717 struct msgb *msg = gsm48_msgb_alloc();
2718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2719
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 gh->msg_type = GSM48_MT_CC_NOTIFY;
2721
2722 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002723 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002725 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726}
2727
2728static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2729{
2730 struct gsm48_hdr *gh = msgb_l3(msg);
2731 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2732// struct tlv_parsed tp;
2733 struct gsm_mncc notify;
2734
2735 memset(&notify, 0, sizeof(struct gsm_mncc));
2736 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002737// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002738 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002739 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002740
Harald Welte596fed42009-07-23 19:06:52 +02002741 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742}
2743
2744static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2745{
2746 struct gsm_mncc *user = arg;
2747 struct msgb *msg = gsm48_msgb_alloc();
2748 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2749
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750 gh->msg_type = GSM48_MT_CC_USER_INFO;
2751
2752 /* user-user */
2753 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002754 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002755 /* more data */
2756 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002757 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002758
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002759 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002760}
2761
2762static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2763{
2764 struct gsm48_hdr *gh = msgb_l3(msg);
2765 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2766 struct tlv_parsed tp;
2767 struct gsm_mncc user;
2768
2769 memset(&user, 0, sizeof(struct gsm_mncc));
2770 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002771 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002772 /* user-user */
2773 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2774 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002775 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002776 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2777 }
2778 /* more data */
2779 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2780 user.more = 1;
2781
Harald Welte596fed42009-07-23 19:06:52 +02002782 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783}
2784
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002785static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786{
2787 struct gsm_mncc *mode = arg;
2788
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002789 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002790}
2791
2792static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002793 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 int type;
2795 int (*rout) (struct gsm_trans *trans, void *arg);
2796} downstatelist[] = {
2797 /* mobile originating call establishment */
2798 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2799 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2800 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2801 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2802 {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 */
2803 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2804 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2805 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2806 /* mobile terminating call establishment */
2807 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2808 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2809 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2810 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2811 /* signalling during call */
2812 {SBIT(GSM_CSTATE_ACTIVE),
2813 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2814 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2815 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2816 {ALL_STATES,
2817 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2818 {ALL_STATES,
2819 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2820 {ALL_STATES,
2821 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2822 {SBIT(GSM_CSTATE_ACTIVE),
2823 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2824 {SBIT(GSM_CSTATE_ACTIVE),
2825 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2826 {SBIT(GSM_CSTATE_ACTIVE),
2827 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2828 {SBIT(GSM_CSTATE_ACTIVE),
2829 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2830 {SBIT(GSM_CSTATE_ACTIVE),
2831 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2832 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2833 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2834 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2835 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2836 {SBIT(GSM_CSTATE_ACTIVE),
2837 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2838 /* clearing */
2839 {SBIT(GSM_CSTATE_INITIATED),
2840 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2841 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2842 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2843 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2844 MNCC_REL_REQ, gsm48_cc_tx_release},
2845 /* special */
2846 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002847 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848};
2849
2850#define DOWNSLLEN \
2851 (sizeof(downstatelist) / sizeof(struct downstate))
2852
2853
Harald Welte76556372010-12-22 23:57:45 +01002854int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855{
Harald Welte1a6f7982009-08-09 18:52:33 +02002856 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002858 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002859 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002860 struct gsm_mncc *data = arg, rel;
2861
Harald Welte04dc88f2010-12-22 21:45:05 +01002862 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2863
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864 /* handle special messages */
2865 switch(msg_type) {
2866 case MNCC_BRIDGE:
2867 return tch_bridge(net, arg);
2868 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002869 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002870 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002871 return tch_recv_mncc(net, data->callref, 1);
2872 case GSM_TCHF_FRAME:
2873 /* Find callref */
2874 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002875 if (!trans) {
2876 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002877 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002878 }
2879 if (!trans->conn) {
2880 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002881 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002882 }
2883 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2884 /* This should be LOGL_ERROR or NOTICE, but
2885 * unfortuantely it happens for a couple of frames at
2886 * the beginning of every RTP connection */
2887 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002888 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002889 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002890 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002891 switch (bts->type) {
2892 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002893 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002894 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002895 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002896 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002897 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002898 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002899 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002900 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002901 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002902 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002903 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002904 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002905 }
2906 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002907 }
2908
2909 memset(&rel, 0, sizeof(struct gsm_mncc));
2910 rel.callref = data->callref;
2911
2912 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002913 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002914
2915 /* Callref unknown */
2916 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002917 struct gsm_subscriber *subscr;
2918
Harald Welte4a3464c2009-07-04 10:11:24 +02002919 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002920 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2921 "Received '%s' from MNCC with "
2922 "unknown callref %d\n", data->called.number,
2923 get_mncc_name(msg_type), data->callref);
2924 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002925 return mncc_release_ind(net, NULL, data->callref,
2926 GSM48_CAUSE_LOC_PRN_S_LU,
2927 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002928 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002929 if (!data->called.number[0] && !data->imsi[0]) {
2930 DEBUGP(DCC, "(bts - trx - ts - ti) "
2931 "Received '%s' from MNCC with "
2932 "no number or IMSI\n", get_mncc_name(msg_type));
2933 /* Invalid number */
2934 return mncc_release_ind(net, NULL, data->callref,
2935 GSM48_CAUSE_LOC_PRN_S_LU,
2936 GSM48_CC_CAUSE_INV_NR_FORMAT);
2937 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002938 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002939 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002940 subscr = subscr_get_by_extension(net,
2941 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002942 else
Harald Welte9176bd42009-07-23 18:46:00 +02002943 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002944 /* If subscriber is not found */
2945 if (!subscr) {
2946 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2947 "Received '%s' from MNCC with "
2948 "unknown subscriber %s\n", data->called.number,
2949 get_mncc_name(msg_type), data->called.number);
2950 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002951 return mncc_release_ind(net, NULL, data->callref,
2952 GSM48_CAUSE_LOC_PRN_S_LU,
2953 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954 }
2955 /* If subscriber is not "attached" */
2956 if (!subscr->lac) {
2957 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2958 "Received '%s' from MNCC with "
2959 "detached subscriber %s\n", data->called.number,
2960 get_mncc_name(msg_type), data->called.number);
2961 subscr_put(subscr);
2962 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002963 return mncc_release_ind(net, NULL, data->callref,
2964 GSM48_CAUSE_LOC_PRN_S_LU,
2965 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966 }
2967 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002968 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2969 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970 DEBUGP(DCC, "No memory for trans.\n");
2971 subscr_put(subscr);
2972 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002973 mncc_release_ind(net, NULL, data->callref,
2974 GSM48_CAUSE_LOC_PRN_S_LU,
2975 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 return -ENOMEM;
2977 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002978 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002979 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002980
Harald Welte4bfdfe72009-06-10 23:11:52 +08002981 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002982 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002983 /* find transaction with this subscriber already paging */
2984 llist_for_each_entry(transt, &net->trans_list, entry) {
2985 /* Transaction of our lchan? */
2986 if (transt == trans ||
2987 transt->subscr != subscr)
2988 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002989 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08002990 "Received '%s' from MNCC with "
2991 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002992 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002993 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01002994 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002995 subscr_put(subscr);
2996 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002997 return 0;
2998 }
2999 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003000 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003001
3002 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003003 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3004 if (!trans->paging_request) {
3005 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3006 subscr_put(subscr);
3007 trans_free(trans);
3008 return 0;
3009 }
3010
3011 *trans->paging_request = subscr->net;
3012 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003013
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003014 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003015 return 0;
3016 }
3017 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003018 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003019 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003020 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003021
3022 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003023 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024
3025 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003026 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003027 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003028 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 (trans->subscr)?(trans->subscr->extension):"-",
3030 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003031 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3032 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 if (msg_type == MNCC_REL_REQ)
3034 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3035 else
3036 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3037 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003038 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039 return rc;
3040 }
3041
3042 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3043 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003044 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003045 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003046 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003047 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf192010-03-25 12:13:02 +08003048 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049
3050 /* Find function for current state and message */
3051 for (i = 0; i < DOWNSLLEN; i++)
3052 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003053 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003054 break;
3055 if (i == DOWNSLLEN) {
3056 DEBUGP(DCC, "Message unhandled at this state.\n");
3057 return 0;
3058 }
3059
3060 rc = downstatelist[i].rout(trans, arg);
3061
3062 return rc;
3063}
3064
3065
3066static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003067 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 int type;
3069 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3070} datastatelist[] = {
3071 /* mobile originating call establishment */
3072 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3073 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3074 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3075 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3076 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3077 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3078 /* mobile terminating call establishment */
3079 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3080 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3081 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3082 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003083 {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 +08003084 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3085 /* signalling during call */
3086 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3087 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3088 {SBIT(GSM_CSTATE_ACTIVE),
3089 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3090 {ALL_STATES,
3091 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3092 {ALL_STATES,
3093 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3094 {ALL_STATES,
3095 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3096 {SBIT(GSM_CSTATE_ACTIVE),
3097 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3098 {SBIT(GSM_CSTATE_ACTIVE),
3099 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3100 {SBIT(GSM_CSTATE_ACTIVE),
3101 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3102 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3103 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3104 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3105 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3106 {SBIT(GSM_CSTATE_ACTIVE),
3107 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3108 /* clearing */
3109 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3110 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3111 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3112 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3113 {ALL_STATES, /* 5.4.3.4 */
3114 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3115};
3116
3117#define DATASLLEN \
3118 (sizeof(datastatelist) / sizeof(struct datastate))
3119
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003120static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003121{
3122 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003123 uint8_t msg_type = gh->msg_type & 0xbf;
3124 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003125 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003126 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003127
Harald Welte4bfdfe72009-06-10 23:11:52 +08003128 if (msg_type & 0x80) {
3129 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3130 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003131 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003132
Harald Welte4bfdfe72009-06-10 23:11:52 +08003133 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003134 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003135
Harald Welte6f5aee02009-07-23 21:21:14 +02003136 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003137 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003138 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003139 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf192010-03-25 12:13:02 +08003140 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3141 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003142
3143 /* Create transaction */
3144 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003145 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003146 "creating new trans.\n", transaction_id);
3147 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003148 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003149 transaction_id, new_callref++);
3150 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003151 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003152 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003153 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154 GSM48_MT_CC_RELEASE_COMPL);
3155 return -ENOMEM;
3156 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003157 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003158 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003159 }
3160
3161 /* find function for current state and message */
3162 for (i = 0; i < DATASLLEN; i++)
3163 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003164 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003165 break;
3166 if (i == DATASLLEN) {
3167 DEBUGP(DCC, "Message unhandled at this state.\n");
3168 return 0;
3169 }
3170
3171 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003172
3173 return rc;
3174}
3175
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003176/* Create a dummy to wait five seconds */
3177static void release_anchor(struct gsm_subscriber_connection *conn)
3178{
3179 if (!conn->anch_operation)
3180 return;
3181
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003182 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003183 talloc_free(conn->anch_operation);
3184 conn->anch_operation = NULL;
3185}
3186
3187static void anchor_timeout(void *_data)
3188{
3189 struct gsm_subscriber_connection *con = _data;
3190
3191 release_anchor(con);
3192 msc_release_connection(con);
3193}
3194
3195int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3196{
3197 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3198 if (!conn->anch_operation)
3199 return -1;
3200
3201 conn->anch_operation->timeout.data = conn;
3202 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003203 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003204 return 0;
3205}
3206
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003207/* here we get data from the BSC level... */
3208int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003209{
3210 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003211 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003212 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003213
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003214 if (silent_call_reroute(conn, msg))
3215 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003216
3217 switch (pdisc) {
3218 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003219 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003220 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003221 break;
3222 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003223 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003224 break;
3225 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003226 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003227 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003228 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003229 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003230 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003231 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003232 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003233 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003234 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3235 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003236 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003237 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003238 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003239 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003240 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003241 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003242 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3243 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003244 break;
3245 }
3246
3247 return rc;
3248}
Harald Welte8470bf22008-12-25 23:28:35 +00003249
Harald Welte805f6442009-07-28 18:25:29 +02003250/*
3251 * This will be ran by the linker when loading the DSO. We use it to
3252 * do system initialization, e.g. registration of signal handlers.
3253 */
3254static __attribute__((constructor)) void on_dso_load_0408(void)
3255{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003256 osmo_signal_register_handler(SS_HO, handle_ho_signal, NULL);
3257 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003258}