blob: a2a49aa9a6c0bf417050cebd3572b49cf3b58b36 [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
Sylvain Munaut30a15382009-12-24 00:27:26 +010032#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000033#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000034#include <openbsc/debug.h>
35#include <openbsc/gsm_data.h>
36#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000037#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080039#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000041#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000042#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000043#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020044#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000045#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020046#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020047#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020048#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010049#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080050#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080051#include <openbsc/osmo_msc.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020052#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010053#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080054
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010055#include <osmocom/gsm/gsm48.h>
56#include <osmocom/gsm/gsm0480.h>
57#include <osmocom/gsm/gsm_utils.h>
58#include <osmocom/core/msgb.h>
59#include <osmocom/core/talloc.h>
60#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000061
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020062void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010063void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020064
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020065int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080066static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020067 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010068static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080069static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000070
Harald Welte52b1f982008-12-23 20:25:15 +000071struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020072 uint16_t mcc;
73 uint16_t mnc;
74 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000075};
76
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080078
Harald Welte31c00f72011-03-03 23:29:05 +010079void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
80{
81 net->mncc_recv(net, msg);
82}
83
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080084static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
85 struct gsm_trans *trans)
86{
87 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
88
89 /* if we get passed a transaction reference, do some common
90 * work that the caller no longer has to do */
91 if (trans) {
92 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
93 msg->lchan = trans->conn->lchan;
94 }
95
96
97 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +020098 struct e1inp_sign_link *sign_link =
99 msg->lchan->ts->trx->rsl_link;
100
101 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800102 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
103 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200104 "Sending '%s' to MS.\n",
105 sign_link->trx->bts->nr,
106 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800107 gh->proto_discr & 0xf0,
108 gsm48_cc_msg_name(gh->msg_type));
109 else
110 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200111 "Sending 0x%02x to MS.\n",
112 sign_link->trx->bts->nr,
113 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800114 gh->proto_discr, gh->msg_type);
115 }
116
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100117 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800118}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100119
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800120int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
121{
122 struct gsm48_hdr *gh;
123 struct msgb *ss_notify;
124
125 ss_notify = gsm0480_create_notifySS(message);
126 if (!ss_notify)
127 return -1;
128
129 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
130 uint8_t *data = msgb_push(ss_notify, 1);
131 data[0] = ss_notify->len - 1;
132 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
133 gh->msg_type = GSM48_MT_CC_FACILITY;
134 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
135}
136
Sylvain Munaut30a15382009-12-24 00:27:26 +0100137static void release_security_operation(struct gsm_subscriber_connection *conn)
138{
139 if (!conn->sec_operation)
140 return;
141
142 talloc_free(conn->sec_operation);
143 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800144 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100145}
146
147static void allocate_security_operation(struct gsm_subscriber_connection *conn)
148{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100149 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
150 struct gsm_security_operation);
151}
152
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100153int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
154 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100155{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800156 struct gsm_network *net = conn->bts->network;
157 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100158 struct gsm_security_operation *op;
159 struct gsm_auth_tuple atuple;
160 int status = -1, rc;
161
162 /* Check if we _can_ enable encryption. Cases where we can't:
163 * - Encryption disabled in config
164 * - Channel already secured (nothing to do)
165 * - Subscriber equipment doesn't support configured encryption
166 */
167 if (!net->a5_encryption) {
168 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800169 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100170 DEBUGP(DMM, "Requesting to secure an already secure channel");
171 status = GSM_SECURITY_SUCCEEDED;
172 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
173 net->a5_encryption)) {
174 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
175 status = GSM_SECURITY_NOAVAIL;
176 }
177
178 /* If not done yet, try to get info for this user */
179 if (status < 0) {
180 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
181 if (rc <= 0)
182 status = GSM_SECURITY_NOAVAIL;
183 }
184
185 /* Are we done yet ? */
186 if (status >= 0)
187 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800188 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100189 0;
190
191 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800192 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100193 return -EBUSY;
194
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800195 allocate_security_operation(conn);
196 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100197 op->cb = cb;
198 op->cb_data = cb_data;
199 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
200
201 /* FIXME: Should start a timer for completion ... */
202
203 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100204 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100205 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800206 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100207 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100208 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100209 return gsm0808_cipher_mode(conn, net->a5_encryption,
210 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100211 }
212
213 return -EINVAL; /* not reached */
214}
215
Holger Freyther73487a22008-12-31 18:53:57 +0000216static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
217 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000218{
219 if (!subscriber)
220 return 0;
221
Holger Freyther73487a22008-12-31 18:53:57 +0000222 /*
223 * Do not send accept yet as more information should arrive. Some
224 * phones will not send us the information and we will have to check
225 * what we want to do with that.
226 */
227 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
228 return 0;
229
Jan Luebbe06513f22009-08-12 12:48:00 +0200230 switch (subscriber->net->auth_policy) {
231 case GSM_AUTH_POLICY_CLOSED:
232 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200233 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200234 if (subscriber->authorized)
235 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200236 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200237 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000238 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200239 default:
240 return 0;
241 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000242}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000243
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100244static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000245{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100246 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000247 return;
248
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800249 /* No need to keep the connection up */
250 release_anchor(conn);
251
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200252 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100253 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100254 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800255 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000256}
257
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100258static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000259{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800260 if (conn->loc_operation)
261 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100262 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000263
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100264 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200265 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000266}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000267
Sylvain Munaut267fba02009-12-24 00:28:01 +0100268static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
269 struct msgb *msg, void *data, void *param)
270{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800271 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100272 int rc = 0;
273
274 switch (event) {
275 case GSM_SECURITY_AUTH_FAILED:
276 release_loc_updating_req(conn);
277 break;
278
279 case GSM_SECURITY_NOAVAIL:
280 case GSM_SECURITY_SUCCEEDED:
281 /* We're all good */
282 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800283 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800284 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100285 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800286 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100287 }
288
289 /* call subscr_update after putting the loc_upd_acc
290 * in the transmit queue, since S_SUBSCR_ATTACHED might
291 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800292 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100293 GSM_SUBSCRIBER_UPDATE_ATTACHED);
294
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100295 /*
296 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
297 * MS needs to respond with a TMSI REALLOCATION COMPLETE
298 * (even if the TMSI is the same).
299 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100300 break;
301
302 default:
303 rc = -EINVAL;
304 };
305
306 return rc;
307}
308
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100309static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000310{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100311 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800312 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100313 conn->loc_operation->key_seq,
314 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000315 return 0;
316}
317
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800318void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000319{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800320 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800321
322 /* avoid someone issuing a clear */
323 conn->in_release = 1;
324
Holger Freyther7c19f742009-06-06 13:54:35 +0000325 /*
326 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800327 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000328 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800329 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100330
331 /* We might need to cancel the paging response or such. */
332 if (conn->sec_operation && conn->sec_operation->cb) {
333 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
334 NULL, conn, conn->sec_operation->cb_data);
335 }
336
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800337 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800338 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000339
Harald Welte4bfdfe72009-06-10 23:11:52 +0800340 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200341 /* FIXME: this is not neccessarily the right thing to do, we should
342 * only set trans->lchan to NULL and wait for another lchan to be
343 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800344 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
345 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200346 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800347 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000348}
349
Harald Welte371efe52010-12-22 23:17:50 +0100350void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
351{
352 struct gsm_trans *trans, *temp;
353
354 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
355
356 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100357 if (trans->protocol == protocol) {
358 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100359 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100360 }
Harald Welte371efe52010-12-22 23:17:50 +0100361 }
362}
363
Holger Freyther429e7762008-12-30 13:28:30 +0000364/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200365int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000366{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800367 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800368 struct msgb *msg;
369
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200370 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800371
372 msg = gsm48_create_loc_upd_rej(cause);
373 if (!msg) {
374 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
375 return -1;
376 }
Harald Welte52b1f982008-12-23 20:25:15 +0000377
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800378 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000379
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100380 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100381 "LAC=%u BTS=%u\n", conn->subscr ?
382 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800383 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100384
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800385 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000386}
387
388/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200389int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000390{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800391 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000392 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000393 struct gsm48_hdr *gh;
394 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200395 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000396
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800397 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000398
399 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
400 gh->proto_discr = GSM48_PDISC_MM;
401 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
402
403 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100404 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000405 bts->network->network_code, bts->location_area_code);
406
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200407 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200408 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000409
410 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
411
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200412 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100413
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800414 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000415}
416
Harald Weltebf5e8df2009-02-03 12:59:45 +0000417/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200418static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000419{
420 struct msgb *msg = gsm48_msgb_alloc();
421 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000422
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800423 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000424
425 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
426 gh->proto_discr = GSM48_PDISC_MM;
427 gh->msg_type = GSM48_MT_MM_ID_REQ;
428 gh->data[0] = id_type;
429
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800430 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000431}
432
Harald Welte231ad4f2008-12-27 11:15:38 +0000433
Harald Weltebf5e8df2009-02-03 12:59:45 +0000434/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800435static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000436{
437 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000438 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200439 struct gsm_bts *bts = lchan->ts->trx->bts;
440 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200441 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200442 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000443
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200444 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000445 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000446 mi_type, mi_string);
447
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200448 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100449
Harald Welte75a983f2008-12-27 21:34:06 +0000450 switch (mi_type) {
451 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200452 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100453 if (!conn->subscr) {
454 conn->subscr = subscr_get_by_imsi(net, mi_string);
455 if (!conn->subscr)
456 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200457 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100458 if (conn->loc_operation)
459 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000460 break;
461 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000462 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000463 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100464 if (conn->subscr) {
465 db_subscriber_assoc_imei(conn->subscr, mi_string);
466 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200467 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100468 if (conn->loc_operation)
469 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000470 break;
471 }
Holger Freyther73487a22008-12-31 18:53:57 +0000472
473 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100474 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000475}
476
Harald Welte255539c2008-12-28 02:26:27 +0000477
478static void loc_upd_rej_cb(void *data)
479{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100480 struct gsm_subscriber_connection *conn = data;
481 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100482 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000483
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100484 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800485 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800486 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000487}
488
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100489static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000490{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100491 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
492 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200493 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000494}
495
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200496static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000497{
498 switch (type) {
499 case GSM48_LUPD_NORMAL:
500 return "NORMAL";
501 case GSM48_LUPD_PERIODIC:
502 return "PEROIDOC";
503 case GSM48_LUPD_IMSI_ATT:
504 return "IMSI ATTACH";
505 default:
506 return "UNKNOWN";
507 }
508}
509
Harald Weltebf5e8df2009-02-03 12:59:45 +0000510/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800511static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000512{
Harald Welte8470bf22008-12-25 23:28:35 +0000513 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000514 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800515 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800516 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200517 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200518 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000519 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000520
Harald Welte8470bf22008-12-25 23:28:35 +0000521 lu = (struct gsm48_loc_upd_req *) gh->data;
522
523 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000524
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200525 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000526
Harald Weltea0368542009-06-27 02:58:43 +0200527 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000528 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000529
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200530 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100531
Harald Welte24ff6ee2009-12-22 00:41:05 +0100532 switch (lu->type) {
533 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200534 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100535 break;
536 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200537 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100538 break;
539 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200540 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100541 break;
542 }
543
Holger Freythereaf04692009-06-06 13:54:44 +0000544 /*
545 * Pseudo Spoof detection: Just drop a second/concurrent
546 * location updating request.
547 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100548 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200549 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100550 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800551 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000552 return 0;
553 }
554
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800555 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000556
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200557 conn->loc_operation->key_seq = lu->key_seq;
558
Harald Welte52b1f982008-12-23 20:25:15 +0000559 switch (mi_type) {
560 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200561 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000562 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800563 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100564 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000565
Jan Luebbe370b41d2009-08-12 10:19:34 +0200566 /* look up subscriber based on IMSI, create if not found */
567 subscr = subscr_get_by_imsi(bts->network, mi_string);
568 if (!subscr) {
569 subscr = db_create_subscriber(bts->network, mi_string);
570 }
Harald Welte4b634542008-12-27 01:55:51 +0000571 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000572 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200573 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000574 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200575 subscr = subscr_get_by_tmsi(bts->network,
576 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000577 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000578 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800579 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100580 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000581 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200582 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800583 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200584 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000585 break;
586 case GSM_MI_TYPE_IMEI:
587 case GSM_MI_TYPE_IMEISV:
588 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200589 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000590 break;
591 default:
Harald Weltea0368542009-06-27 02:58:43 +0200592 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000593 break;
594 }
595
Harald Welte24516ea2009-07-04 10:18:00 +0200596 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100597 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200598
Harald Welte4bfdfe72009-06-10 23:11:52 +0800599 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200600 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800601 /* FIXME: request id? close channel? */
602 return -EINVAL;
603 }
604
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100605 conn->subscr = subscr;
606 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000607
Harald Welte24516ea2009-07-04 10:18:00 +0200608 /* check if we can let the subscriber into our network immediately
609 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100610 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000611}
612
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100613/* Turn int into semi-octet representation: 98 => 0x89 */
614static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800615{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100616 uint8_t ret;
617
618 ret = value / 10;
619 ret |= (value % 10) << 4;
620
621 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100623
Harald Welte4bfdfe72009-06-10 23:11:52 +0800624
Harald Weltedb253af2008-12-30 17:56:55 +0000625/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800626int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000627{
628 struct msgb *msg = gsm48_msgb_alloc();
629 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800630 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100631 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200632 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200633 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100634
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100636 struct tm* gmt_time;
637 struct tm* local_time;
638 int tzunits;
Harald Weltedb253af2008-12-30 17:56:55 +0000639
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800640 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000641
642 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
643 gh->proto_discr = GSM48_PDISC_MM;
644 gh->msg_type = GSM48_MT_MM_INFO;
645
646 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200647#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000648 name_len = strlen(net->name_long);
649 /* 10.5.3.5a */
650 ptr8 = msgb_put(msg, 3);
651 ptr8[0] = GSM48_IE_NAME_LONG;
652 ptr8[1] = name_len*2 +1;
653 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
654
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200655 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000656 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000657 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000658
659 /* FIXME: Use Cell Broadcast, not UCS-2, since
660 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200661#endif
662 name_len = (strlen(net->name_long)*7)/8;
663 name_pad = (8 - strlen(net->name_long)*7)%8;
664 if (name_pad > 0)
665 name_len++;
666 /* 10.5.3.5a */
667 ptr8 = msgb_put(msg, 3);
668 ptr8[0] = GSM48_IE_NAME_LONG;
669 ptr8[1] = name_len +1;
670 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
671
672 ptr8 = msgb_put(msg, name_len);
673 gsm_7bit_encode(ptr8, net->name_long);
674
Harald Weltedb253af2008-12-30 17:56:55 +0000675 }
676
677 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200678#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000679 name_len = strlen(net->name_short);
680 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200681 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200682 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000683 ptr8[1] = name_len*2 + 1;
684 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
685
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200686 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000687 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000688 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200689#endif
690 name_len = (strlen(net->name_short)*7)/8;
691 name_pad = (8 - strlen(net->name_short)*7)%8;
692 if (name_pad > 0)
693 name_len++;
694 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200695 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200696 ptr8[0] = GSM48_IE_NAME_SHORT;
697 ptr8[1] = name_len +1;
698 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
699
700 ptr8 = msgb_put(msg, name_len);
701 gsm_7bit_encode(ptr8, net->name_short);
702
Harald Weltedb253af2008-12-30 17:56:55 +0000703 }
704
Harald Weltedb253af2008-12-30 17:56:55 +0000705 /* Section 10.5.3.9 */
706 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100707 gmt_time = gmtime(&cur_t);
708
Harald Weltedb253af2008-12-30 17:56:55 +0000709 ptr8 = msgb_put(msg, 8);
710 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100711 ptr8[1] = bcdify(gmt_time->tm_year % 100);
712 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
713 ptr8[3] = bcdify(gmt_time->tm_mday);
714 ptr8[4] = bcdify(gmt_time->tm_hour);
715 ptr8[5] = bcdify(gmt_time->tm_min);
716 ptr8[6] = bcdify(gmt_time->tm_sec);
717
718 if (bts->tz_bts_specific) {
719 /* Convert tzhr and tzmn to units */
720 if (bts->tzhr < 0) {
721 tzunits = ((bts->tzhr/-1)*4);
722 tzunits = tzunits + (bts->tzmn/15);
723 ptr8[7] = bcdify(tzunits);
724 /* Set negative time */
725 ptr8[7] |= 0x08;
726 }
727 else {
728 tzunits = bts->tzhr*4;
729 tzunits = tzunits + (bts->tzmn/15);
730 ptr8[7] = bcdify(tzunits);
731 }
732 }
733 else {
734 /* Need to get GSM offset and convert into 15 min units */
735 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
736 local_time = localtime(&cur_t);
737 tzunits = (local_time->tm_gmtoff/60)/15;
738 if (tzunits < 0) {
739 tzunits = tzunits/-1;
740 ptr8[7] = bcdify(tzunits);
741 /* Flip it to negative */
742 ptr8[7] |= 0x08;
743 }
744 else
745 ptr8[7] = bcdify(tzunits);
746 }
Harald Weltedb253af2008-12-30 17:56:55 +0000747
Daniel Willmanneea93372009-08-13 03:42:07 +0200748 DEBUGP(DMM, "-> MM INFO\n");
749
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800750 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000751}
752
Harald Welte7984d5c2009-08-12 22:56:50 +0200753/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200754int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200755{
756 struct msgb *msg = gsm48_msgb_alloc();
757 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200758 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200759
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200760 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200761
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800762 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200763 gh->proto_discr = GSM48_PDISC_MM;
764 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
765
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100766 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200767
Harald Welte7984d5c2009-08-12 22:56:50 +0200768 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200769 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200770 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200771
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800772 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200773}
774
775/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800776int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200777{
778 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800779 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200780}
781
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800782static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000783{
Harald Welte4b634542008-12-27 01:55:51 +0000784 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800785 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000786}
Harald Welteba4cf162009-01-10 01:49:35 +0000787
788/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100789static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000790 enum gsm48_reject_value value)
791{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800792 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000793
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800794 msg = gsm48_create_mm_serv_rej(value);
795 if (!msg) {
796 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
797 return -1;
798 }
Harald Welteba4cf162009-01-10 01:49:35 +0000799
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800800 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100801 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800802 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000803}
804
Sylvain Munautba87f452009-12-24 00:28:46 +0100805static int _gsm48_rx_mm_serv_req_sec_cb(
806 unsigned int hooknum, unsigned int event,
807 struct msgb *msg, void *data, void *param)
808{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800809 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100810 int rc = 0;
811
812 switch (event) {
813 case GSM_SECURITY_AUTH_FAILED:
814 /* Nothing to do */
815 break;
816
817 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800818 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100819 break;
820
821 case GSM_SECURITY_SUCCEEDED:
822 /* nothing to do. CIPHER MODE COMMAND is
823 * implicit CM SERV ACK */
824 break;
825
826 default:
827 rc = -EINVAL;
828 };
829
830 return rc;
831}
832
Harald Welte4ed0e922009-01-10 03:17:30 +0000833/*
834 * Handle CM Service Requests
835 * a) Verify that the packet is long enough to contain the information
836 * we require otherwsie reject with INCORRECT_MESSAGE
837 * b) Try to parse the TMSI. If we do not have one reject
838 * c) Check that we know the subscriber with the TMSI otherwise reject
839 * with a HLR cause
840 * d) Set the subscriber on the gsm_lchan and accept
841 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800842static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000843{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200844 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200845 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000846
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800847 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000848 struct gsm_subscriber *subscr;
849 struct gsm48_hdr *gh = msgb_l3(msg);
850 struct gsm48_service_request *req =
851 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800852 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200853 uint8_t classmark2_len = gh->data[1];
854 uint8_t *classmark2 = gh->data+2;
855 uint8_t mi_len = *(classmark2 + classmark2_len);
856 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000857
Harald Weltec9e02182009-05-01 19:07:53 +0000858 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000859 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000860 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800861 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000862 GSM48_REJECT_INCORRECT_MESSAGE);
863 }
864
865 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000866 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800867 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000868 GSM48_REJECT_INCORRECT_MESSAGE);
869 }
870
Harald Weltec9e02182009-05-01 19:07:53 +0000871 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000872 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000873 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800874 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000875 GSM48_REJECT_INCORRECT_MESSAGE);
876 }
877
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200878 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000879 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000880 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000881
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200882 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100883
Harald Welte3ac7f102009-08-10 10:12:45 +0200884 if (is_siemens_bts(bts))
885 send_siemens_mrpci(msg->lchan, classmark2-1);
886
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200887 subscr = subscr_get_by_tmsi(bts->network,
888 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000889
Harald Welte2a139372009-02-22 21:14:55 +0000890 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000891 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800892 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000893 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
894
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800895 if (!conn->subscr)
896 conn->subscr = subscr;
897 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100898 subscr_put(subscr); /* lchan already has a ref, don't need another one */
899 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000900 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
901 subscr_put(subscr);
902 }
903
Harald Weltec2e302d2009-07-05 14:08:13 +0200904 subscr->equipment.classmark2_len = classmark2_len;
905 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
906 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000907
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800908 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100909 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000910}
911
Harald Welte2a139372009-02-22 21:14:55 +0000912static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
913{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200914 struct e1inp_sign_link *sign_link = msg->lchan->ts->trx->rsl_link;
Harald Welte9176bd42009-07-23 18:46:00 +0200915 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000916 struct gsm48_hdr *gh = msgb_l3(msg);
917 struct gsm48_imsi_detach_ind *idi =
918 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200919 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200920 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800921 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000922
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200923 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000924 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
925 mi_type, mi_string);
926
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200927 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100928
Harald Welte2a139372009-02-22 21:14:55 +0000929 switch (mi_type) {
930 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200931 subscr = subscr_get_by_tmsi(bts->network,
932 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000933 break;
934 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200935 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000936 break;
937 case GSM_MI_TYPE_IMEI:
938 case GSM_MI_TYPE_IMEISV:
939 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000940 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000941 break;
942 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000943 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000944 break;
945 }
946
Holger Freyther4a49e772009-04-12 05:37:29 +0000947 if (subscr) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200948 subscr_update(subscr, sign_link->trx->bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000949 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100950 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200951
952 subscr->equipment.classmark1 = idi->classmark1;
953 db_sync_equipment(&subscr->equipment);
954
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000955 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000956 } else
Harald Welte2a139372009-02-22 21:14:55 +0000957 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
958
Harald Welte31981a02009-12-20 09:58:40 +0100959 /* FIXME: iterate over all transactions and release them,
960 * imagine an IMSI DETACH happening during an active call! */
961
Harald Welteb83d9382009-12-12 21:00:48 +0100962 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000963 return 0;
964}
965
Harald Welted2a7f5a2009-06-05 20:08:20 +0000966static int gsm48_rx_mm_status(struct msgb *msg)
967{
968 struct gsm48_hdr *gh = msgb_l3(msg);
969
970 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
971
972 return 0;
973}
974
Sylvain Munaut30a15382009-12-24 00:27:26 +0100975/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800976static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100977{
978 struct gsm48_hdr *gh = msgb_l3(msg);
979 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800980 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100981
Sylvain Munautc593cf12010-06-10 23:51:41 +0200982 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200983 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +0100984
985 /* Safety check */
986 if (!conn->sec_operation) {
987 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
988 return -EIO;
989 }
990
991 /* Validate SRES */
992 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100993 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100994 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200995
996 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200997 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +0200998
Sylvain Munaut30a15382009-12-24 00:27:26 +0100999 if (cb)
1000 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001001 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001002
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001003 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001004 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001005 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001006 }
1007
Sylvain Munautc593cf12010-06-10 23:51:41 +02001008 DEBUGPC(DMM, "OK\n");
1009
Sylvain Munaut30a15382009-12-24 00:27:26 +01001010 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001011 return gsm0808_cipher_mode(conn, net->a5_encryption,
1012 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001013}
1014
Harald Weltebf5e8df2009-02-03 12:59:45 +00001015/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001016static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001017{
1018 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001019 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001020
1021 switch (gh->msg_type & 0xbf) {
1022 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001023 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001024 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001025 break;
1026 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001027 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001028 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001029 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001030 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001031 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001032 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001033 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001034 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001035 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001036 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001037 conn->subscr ?
1038 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001039 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001040 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001041 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001042 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001043 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1044 break;
1045 case GSM48_MT_MM_CM_REEST_REQ:
1046 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1047 break;
1048 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001049 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001050 break;
1051 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001052 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001053 gh->msg_type);
1054 break;
1055 }
1056
1057 return rc;
1058}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001059
Harald Welte2d35ae62009-02-06 12:02:13 +00001060/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001061static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001062{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001063 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001064 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001065 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001066 uint8_t *classmark2_lv = gh->data + 1;
1067 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001068 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001069 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001070 int rc = 0;
1071
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001072 resp = (struct gsm48_pag_resp *) &gh->data[0];
1073 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1074 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001075 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1076 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001077
Harald Weltefe18d8f2009-02-22 21:14:24 +00001078 switch (mi_type) {
1079 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001080 subscr = subscr_get_by_tmsi(bts->network,
1081 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001082 break;
1083 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001084 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001085 break;
1086 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001087
1088 if (!subscr) {
1089 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001090 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001091 return -EINVAL;
1092 }
1093 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001094 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001095
Harald Weltec2e302d2009-07-05 14:08:13 +02001096 subscr->equipment.classmark2_len = *classmark2_lv;
1097 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1098 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001099
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001100 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001101 return rc;
1102}
1103
Harald Weltecf5b3592009-05-01 18:28:42 +00001104static int gsm48_rx_rr_status(struct msgb *msg)
1105{
1106 struct gsm48_hdr *gh = msgb_l3(msg);
1107
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001108 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001109 rr_cause_name(gh->data[0]));
1110
1111 return 0;
1112}
1113
Harald Weltef7c43522009-06-09 20:24:21 +00001114static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1115{
Harald Welted12b0fd2009-12-15 21:36:05 +01001116 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001117
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001118 /* This shouldn't actually end up here, as RSL treats
1119 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1120 * directly into gsm48_parse_meas_rep */
1121 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001122 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001123
1124 return 0;
1125}
1126
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001127static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001128{
1129 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001130 uint8_t apdu_id_flags;
1131 uint8_t apdu_len;
1132 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001133
1134 apdu_id_flags = gh->data[0];
1135 apdu_len = gh->data[1];
1136 apdu_data = gh->data+2;
1137
1138 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001139 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001140
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001141 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001142}
1143
Sylvain Munaut30a15382009-12-24 00:27:26 +01001144/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001145static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001146{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001147 gsm_cbfn *cb;
1148 int rc = 0;
1149
1150 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1151
1152 /* Safety check */
1153 if (!conn->sec_operation) {
1154 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1155 return -EIO;
1156 }
1157
1158 /* FIXME: check for MI (if any) */
1159
1160 /* Call back whatever was in progress (if anything) ... */
1161 cb = conn->sec_operation->cb;
1162 if (cb) {
1163 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001164 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001165 }
1166
1167 /* Complete the operation */
1168 release_security_operation(conn);
1169
1170 return rc;
1171}
1172
Harald Welted011e8b2009-11-29 22:45:52 +01001173/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001174static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001175{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001176 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001177 struct gsm48_hdr *gh = msgb_l3(msg);
1178
1179 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1180 rr_cause_name(gh->data[0]));
1181
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001182 sig.lchan = msg->lchan;
1183 sig.mr = NULL;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001184 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001185 /* FIXME: release old channel */
1186
1187 return 0;
1188}
1189
1190/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001191static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001192{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001193 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001194 struct gsm48_hdr *gh = msgb_l3(msg);
1195
1196 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1197 rr_cause_name(gh->data[0]));
1198
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001199 sig.lchan = msg->lchan;
1200 sig.mr = NULL;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001201 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001202 /* FIXME: release allocated new channel */
1203
1204 return 0;
1205}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001206
Harald Weltebf5e8df2009-02-03 12:59:45 +00001207/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001208static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001209{
1210 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001211 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001212
1213 switch (gh->msg_type) {
Harald Weltefc977a82008-12-27 10:19:37 +00001214 case GSM48_MT_RR_GPRS_SUSP_REQ:
1215 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1216 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001217 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001218 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001219 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001220 case GSM48_MT_RR_STATUS:
1221 rc = gsm48_rx_rr_status(msg);
1222 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001223 case GSM48_MT_RR_MEAS_REP:
1224 rc = gsm48_rx_rr_meas_rep(msg);
1225 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001227 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001228 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001229 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001230 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001231 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001232 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001233 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001234 break;
1235 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001236 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001237 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001238 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001239 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1240 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001241 break;
1242 }
1243
Harald Welte2d35ae62009-02-06 12:02:13 +00001244 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001245}
1246
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001247int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1248 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001249{
1250 struct msgb *msg = gsm48_msgb_alloc();
1251 struct gsm48_hdr *gh;
1252
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001253 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001254
1255 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1256 apdu_id, apdu_len);
1257
1258 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1259 gh->proto_discr = GSM48_PDISC_RR;
1260 gh->msg_type = GSM48_MT_RR_APP_INFO;
1261 gh->data[0] = apdu_id;
1262 gh->data[1] = apdu_len;
1263 memcpy(gh->data+2, apdu, apdu_len);
1264
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001265 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001266}
1267
Harald Welte4bc90a12008-12-27 16:32:52 +00001268/* Call Control */
1269
Harald Welte7584aea2009-02-11 11:44:12 +00001270/* The entire call control code is written in accordance with Figure 7.10c
1271 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1272 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1273 * it for voice */
1274
Harald Welte4bfdfe72009-06-10 23:11:52 +08001275static void new_cc_state(struct gsm_trans *trans, int state)
1276{
1277 if (state > 31 || state < 0)
1278 return;
1279
1280 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001281 gsm48_cc_state_name(trans->cc.state),
1282 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001283
Harald Weltedcaf5652009-07-23 18:56:43 +02001284 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285}
1286
1287static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001288{
1289 struct msgb *msg = gsm48_msgb_alloc();
1290 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001291 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001292
Harald Welte4bc90a12008-12-27 16:32:52 +00001293 gh->msg_type = GSM48_MT_CC_STATUS;
1294
1295 cause = msgb_put(msg, 3);
1296 cause[0] = 2;
1297 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1298 cause[2] = 0x80 | 30; /* response to status inquiry */
1299
1300 call_state = msgb_put(msg, 1);
1301 call_state[0] = 0xc0 | 0x00;
1302
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001303 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001304}
1305
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001306static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001307 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001308{
1309 struct msgb *msg = gsm48_msgb_alloc();
1310 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1311
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001312 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001313
Harald Welte6f4b7532008-12-29 00:39:37 +00001314 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001315 gh->msg_type = msg_type;
1316
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001317 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001318}
1319
Harald Welte4bfdfe72009-06-10 23:11:52 +08001320static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1321{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001322 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001323 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001324 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001325 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001326 }
1327}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001328
Harald Welte4bfdfe72009-06-10 23:11:52 +08001329static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1330 int msg_type, struct gsm_mncc *mncc)
1331{
1332 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001333 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001334
1335 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001336 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001337 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001339 trans->conn->lchan->ts->trx->bts->nr,
1340 trans->conn->lchan->ts->trx->nr,
1341 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342 (trans->subscr)?(trans->subscr->extension):"-",
1343 get_mncc_name(msg_type));
1344 else
1345 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1346 "Sending '%s' to MNCC.\n",
1347 (trans->subscr)?(trans->subscr->extension):"-",
1348 get_mncc_name(msg_type));
1349 else
1350 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1351 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1352
1353 mncc->msg_type = msg_type;
1354
Harald Welte966636f2009-06-26 19:39:35 +02001355 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001356 if (!msg)
1357 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001358
1359 data = msgb_put(msg, sizeof(struct gsm_mncc));
1360 memcpy(data, mncc, sizeof(struct gsm_mncc));
1361
Harald Welte54209c22010-12-22 23:43:29 +01001362 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363
1364 return 0;
1365}
1366
1367int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001368 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001369{
1370 struct gsm_mncc rel;
1371
Harald Welte92f70c52009-06-12 01:54:08 +08001372 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001373 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001374 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001375 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1376}
1377
Harald Weltedcaf5652009-07-23 18:56:43 +02001378/* Call Control Specific transaction release.
1379 * gets called by trans_free, DO NOT CALL YOURSELF! */
1380void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 gsm48_stop_cc_timer(trans);
1383
1384 /* send release to L4, if callref still exists */
1385 if (trans->callref) {
1386 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001387 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001388 GSM48_CAUSE_LOC_PRN_S_LU,
1389 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001391 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001393 if (trans->conn)
1394 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395}
1396
1397static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001398
Harald Welte09e38af2009-02-16 22:52:23 +00001399/* call-back from paging the B-end of the connection */
1400static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001401 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001402{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001403 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001404 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001405 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001407
Harald Welte09e38af2009-02-16 22:52:23 +00001408 if (hooknum != GSM_HOOK_RR_PAGING)
1409 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001410
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001411 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001412 if (!net) {
1413 DEBUGP(DCC, "Error Network not set!\n");
1414 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001415 }
Harald Welte7584aea2009-02-11 11:44:12 +00001416
Harald Welte4bfdfe72009-06-10 23:11:52 +08001417 /* check all tranactions (without lchan) for subscriber */
1418 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001419 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420 continue;
1421 switch (event) {
1422 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001423 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001424 break;
1425 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001426 transt->subscr->extension);
1427 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001428 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001429 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001430 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001431 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001432 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001433 }
1434 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001435 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001436 break;
1437 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001438 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001439 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001440 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001441 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001442 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001443 mncc_release_ind(transt->subscr->net, transt,
1444 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001445 GSM48_CAUSE_LOC_PRN_S_LU,
1446 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001447 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001448 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001449 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001450 break;
1451 }
1452 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001453
1454 talloc_free(paging_request);
1455
1456 /*
1457 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1458 * failure and then the subscr will be poke again. This needs a lot of fixing
1459 * in the subscriber queue code.
1460 */
1461 if (!found && conn)
1462 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001463 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001464}
Harald Welte7584aea2009-02-11 11:44:12 +00001465
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001466static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001467
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001468/* handle audio path for handover */
1469static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1470 void *handler_data, void *signal_data)
1471{
1472 struct rtp_socket *old_rs, *new_rs, *other_rs;
1473 struct ho_signal_data *sig = signal_data;
1474
1475 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1476 return 0;
1477
1478 if (ipacc_rtp_direct) {
1479 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1480 return 0;
1481 }
1482
1483 /* RTP Proxy mode */
1484 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1485 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1486
1487 if (!new_rs) {
1488 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1489 return -EIO;
1490 }
1491
1492 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1493
1494 if (!old_rs) {
1495 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1496 return -EIO;
1497 }
1498
1499 /* copy rx_action and reference to other sock */
1500 new_rs->rx_action = old_rs->rx_action;
1501 new_rs->tx_action = old_rs->tx_action;
1502 new_rs->transmit = old_rs->transmit;
1503
1504 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1505 case RTP_PROXY:
1506 other_rs = old_rs->proxy.other_sock;
1507 rtp_socket_proxy(new_rs, other_rs);
1508 /* delete reference to other end socket to prevent
1509 * rtp_socket_free() from removing the inverse reference */
1510 old_rs->proxy.other_sock = NULL;
1511 break;
1512 case RTP_RECV_UPSTREAM:
1513 new_rs->receive = old_rs->receive;
1514 break;
1515 case RTP_NONE:
1516 break;
1517 }
1518
1519 return 0;
1520}
1521
Harald Welte805f6442009-07-28 18:25:29 +02001522/* some other part of the code sends us a signal */
1523static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1524 void *handler_data, void *signal_data)
1525{
1526 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001527 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001528 struct gsm_network *net;
1529 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001530
1531 if (subsys != SS_ABISIP)
1532 return 0;
1533
1534 /* in case we use direct BTS-to-BTS RTP */
1535 if (ipacc_rtp_direct)
1536 return 0;
1537
Harald Welte805f6442009-07-28 18:25:29 +02001538 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001539 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001540 /* in case we don't use direct BTS-to-BTS RTP */
1541 /* the BTS has successfully bound a TCH to a local ip/port,
1542 * which means we can connect our UDP socket to it */
1543 if (lchan->abis_ip.rtp_socket) {
1544 rtp_socket_free(lchan->abis_ip.rtp_socket);
1545 lchan->abis_ip.rtp_socket = NULL;
1546 }
1547
1548 lchan->abis_ip.rtp_socket = rtp_socket_create();
1549 if (!lchan->abis_ip.rtp_socket)
1550 return -EIO;
1551
1552 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1553 lchan->abis_ip.bound_ip,
1554 lchan->abis_ip.bound_port);
1555 if (rc < 0)
1556 return -EIO;
1557
Harald Welteda7ab742009-12-19 22:23:05 +01001558 /* check if any transactions on this lchan still have
1559 * a tch_recv_mncc request pending */
1560 net = lchan->ts->trx->bts->network;
1561 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001562 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001563 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1564 tch_recv_mncc(net, trans->callref, 1);
1565 }
1566 }
Harald Welte805f6442009-07-28 18:25:29 +02001567 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001568 case S_ABISIP_DLCX_IND:
1569 /* the BTS tells us a RTP stream has been disconnected */
1570 if (lchan->abis_ip.rtp_socket) {
1571 rtp_socket_free(lchan->abis_ip.rtp_socket);
1572 lchan->abis_ip.rtp_socket = NULL;
1573 }
1574
1575 break;
Harald Welte805f6442009-07-28 18:25:29 +02001576 }
1577
1578 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001579}
1580
Harald Welte49f48b82009-02-17 15:29:33 +00001581/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001582static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001583{
Harald Welte11fa29c2009-02-19 17:24:39 +00001584 struct gsm_bts *bts = lchan->ts->trx->bts;
1585 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001586 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001587
Harald Welte11fa29c2009-02-19 17:24:39 +00001588 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1589 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1590 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1591
1592 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001593 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001594 return -EINVAL;
1595 }
Harald Welteda7ab742009-12-19 22:23:05 +01001596
1597 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001598 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001599 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001600 if (!ipacc_rtp_direct) {
1601 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001602 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001603 if (rc < 0)
1604 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001605 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001606 if (rc < 0)
1607 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001608 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301609 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1610 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001611 } else {
1612 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301613 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1614 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301615 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001616 if (rc < 0)
1617 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301618 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1619 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301620 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001621 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001622 break;
1623 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001624 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001625 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001626 trau_mux_map_lchan(lchan, remote_lchan);
1627 break;
1628 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001629 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001630 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001631 }
Harald Welte49f48b82009-02-17 15:29:33 +00001632
1633 return 0;
1634}
1635
Harald Welte4bfdfe72009-06-10 23:11:52 +08001636/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001637static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001638{
Harald Weltedcaf5652009-07-23 18:56:43 +02001639 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1640 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001641
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001643 return -EIO;
1644
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001645 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001646 return -EIO;
1647
1648 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001649 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001650}
1651
Harald Welteda7ab742009-12-19 22:23:05 +01001652/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001653static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001654{
1655 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001656 struct gsm_lchan *lchan;
1657 struct gsm_bts *bts;
1658 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001659
Harald Welte4bfdfe72009-06-10 23:11:52 +08001660 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001661 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 if (!trans)
1663 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001664 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001666 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001667 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668
Harald Welteda7ab742009-12-19 22:23:05 +01001669 switch (bts->type) {
1670 case GSM_BTS_TYPE_NANOBTS:
1671 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001672 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001673 return -EINVAL;
1674 }
1675 /* in case, we don't have a RTP socket yet, we note this
1676 * in the transaction and try later */
1677 if (!lchan->abis_ip.rtp_socket) {
1678 trans->tch_recv = enable;
1679 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1680 return 0;
1681 }
1682 if (enable) {
1683 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001684 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001685 if (rc < 0)
1686 return rc;
1687 /* assign socket to application interface */
1688 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1689 net, callref);
1690 } else
1691 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1692 net, 0);
1693 break;
1694 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001695 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001696 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001697 if (enable)
1698 return trau_recv_lchan(lchan, callref);
1699 return trau_mux_unmap(NULL, callref);
1700 break;
1701 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001702 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001703 return -EINVAL;
1704 }
1705
1706 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707}
1708
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1710{
1711 DEBUGP(DCC, "-> STATUS ENQ\n");
1712 return gsm48_cc_tx_status(trans, msg);
1713}
1714
1715static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1716static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1717
1718static void gsm48_cc_timeout(void *arg)
1719{
1720 struct gsm_trans *trans = arg;
1721 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001722 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1723 int mo_location = GSM48_CAUSE_LOC_USER;
1724 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1725 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 struct gsm_mncc mo_rel, l4_rel;
1727
1728 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1729 mo_rel.callref = trans->callref;
1730 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1731 l4_rel.callref = trans->callref;
1732
Harald Weltedcaf5652009-07-23 18:56:43 +02001733 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 case 0x303:
1735 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001736 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 break;
1738 case 0x310:
1739 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001740 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001741 break;
1742 case 0x313:
1743 disconnect = 1;
1744 /* unknown, did not find it in the specs */
1745 break;
1746 case 0x301:
1747 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001748 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001749 break;
1750 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001751 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001753 gsm48_cc_tx_release(trans, &trans->cc.msg);
1754 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 break; /* stay in release state */
1756 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001757 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758 return;
1759// release = 1;
1760// l4_cause = 14;
1761// break;
1762 case 0x306:
1763 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001764 mo_cause = trans->cc.msg.cause.value;
1765 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001766 break;
1767 case 0x323:
1768 disconnect = 1;
1769 break;
1770 default:
1771 release = 1;
1772 }
1773
1774 if (release && trans->callref) {
1775 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001776 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 l4_location, l4_cause);
1778 trans->callref = 0;
1779 }
1780
1781 if (disconnect && trans->callref) {
1782 /* process disconnect towards layer 4 */
1783 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001784 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 }
1786
1787 /* process disconnect towards mobile station */
1788 if (disconnect || release) {
1789 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001790 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1791 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1792 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793 mo_rel.cause.diag_len = 3;
1794
1795 if (disconnect)
1796 gsm48_cc_tx_disconnect(trans, &mo_rel);
1797 if (release)
1798 gsm48_cc_tx_release(trans, &mo_rel);
1799 }
1800
1801}
1802
1803static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1804 int sec, int micro)
1805{
1806 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001807 trans->cc.timer.cb = gsm48_cc_timeout;
1808 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001809 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001810 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811}
1812
1813static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1814{
1815 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001816 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1818 struct tlv_parsed tp;
1819 struct gsm_mncc setup;
1820
1821 memset(&setup, 0, sizeof(struct gsm_mncc));
1822 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001823 if (trans->conn && trans->conn->lchan)
1824 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001825 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 /* emergency setup is identified by msg_type */
1827 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1828 setup.emergency = 1;
1829
1830 /* use subscriber as calling party number */
1831 if (trans->subscr) {
1832 setup.fields |= MNCC_F_CALLING;
1833 strncpy(setup.calling.number, trans->subscr->extension,
1834 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001835 strncpy(setup.imsi, trans->subscr->imsi,
1836 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 }
1838 /* bearer capability */
1839 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1840 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001841 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001842 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1843 }
1844 /* facility */
1845 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1846 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001847 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1849 }
1850 /* called party bcd number */
1851 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1852 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001853 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1855 }
1856 /* user-user */
1857 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1858 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001859 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001860 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1861 }
1862 /* ss-version */
1863 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1864 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001865 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1867 }
1868 /* CLIR suppression */
1869 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1870 setup.clir.sup = 1;
1871 /* CLIR invocation */
1872 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1873 setup.clir.inv = 1;
1874 /* cc cap */
1875 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1876 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1879 }
1880
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 new_cc_state(trans, GSM_CSTATE_INITIATED);
1882
Harald Welte (local)47df3992009-12-26 19:45:03 +01001883 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1884 subscr_name(trans->subscr), trans->subscr->extension,
1885 setup.called.number);
1886
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001887 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001888
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001890 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001891
Harald Welte13cac662009-07-29 12:10:35 +02001892 /* MNCC code will modify the channel asynchronously, we should
1893 * ipaccess-bind only after the modification has been made to the
1894 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001895 return 0;
1896}
1897
1898static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001899{
1900 struct msgb *msg = gsm48_msgb_alloc();
1901 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001903 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001904
Harald Welte7ccf7782009-02-17 01:43:01 +00001905 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001906
Harald Welte4bfdfe72009-06-10 23:11:52 +08001907 /* transaction id must not be assigned */
1908 if (trans->transaction_id != 0xff) { /* unasssigned */
1909 DEBUGP(DCC, "TX Setup with assigned transaction. "
1910 "This is not allowed!\n");
1911 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001912 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001913 GSM48_CAUSE_LOC_PRN_S_LU,
1914 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001916 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 return rc;
1918 }
1919
1920 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001921 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1922 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001923 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001924 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001925 GSM48_CAUSE_LOC_PRN_S_LU,
1926 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001928 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001929 return rc;
1930 }
Harald Welte78283ef2009-07-23 21:36:44 +02001931 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001932
Harald Welte65e74cc2008-12-29 01:55:35 +00001933 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001934
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001936
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 /* bearer capability */
1938 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001939 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940 /* facility */
1941 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001942 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 /* progress */
1944 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001945 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001946 /* calling party BCD number */
1947 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001948 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 /* called party BCD number */
1950 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001951 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 /* user-user */
1953 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001954 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 /* redirecting party BCD number */
1956 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001957 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001958 /* signal */
1959 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001960 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001961
1962 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001963
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001964 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001965
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001966 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001967}
1968
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1970{
1971 struct gsm48_hdr *gh = msgb_l3(msg);
1972 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1973 struct tlv_parsed tp;
1974 struct gsm_mncc call_conf;
1975
1976 gsm48_stop_cc_timer(trans);
1977 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1978
1979 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1980 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001981 if (trans->conn && trans->conn->lchan)
1982 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001983 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984#if 0
1985 /* repeat */
1986 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1987 call_conf.repeat = 1;
1988 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1989 call_conf.repeat = 2;
1990#endif
1991 /* bearer capability */
1992 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1993 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001994 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1996 }
1997 /* cause */
1998 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1999 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002000 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002001 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2002 }
2003 /* cc cap */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2005 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2008 }
2009
2010 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2011
Harald Welte596fed42009-07-23 19:06:52 +02002012 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2013 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002014}
2015
2016static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2017{
2018 struct gsm_mncc *proceeding = arg;
2019 struct msgb *msg = gsm48_msgb_alloc();
2020 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2021
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2023
2024 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2025
2026 /* bearer capability */
2027 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002028 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002029 /* facility */
2030 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002031 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 /* progress */
2033 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002036 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002037}
2038
2039static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2040{
2041 struct gsm48_hdr *gh = msgb_l3(msg);
2042 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2043 struct tlv_parsed tp;
2044 struct gsm_mncc alerting;
2045
2046 gsm48_stop_cc_timer(trans);
2047 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2048
2049 memset(&alerting, 0, sizeof(struct gsm_mncc));
2050 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002051 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* facility */
2053 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2054 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002055 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002056 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2057 }
2058
2059 /* progress */
2060 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2061 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002062 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2064 }
2065 /* ss-version */
2066 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2067 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002068 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002069 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2070 }
2071
2072 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2073
Harald Welte596fed42009-07-23 19:06:52 +02002074 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2075 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076}
2077
2078static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2079{
2080 struct gsm_mncc *alerting = arg;
2081 struct msgb *msg = gsm48_msgb_alloc();
2082 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2083
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084 gh->msg_type = GSM48_MT_CC_ALERTING;
2085
2086 /* facility */
2087 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002088 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 /* progress */
2090 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002091 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002092 /* user-user */
2093 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002094 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002095
2096 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2097
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002098 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002099}
2100
2101static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2102{
2103 struct gsm_mncc *progress = arg;
2104 struct msgb *msg = gsm48_msgb_alloc();
2105 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2106
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107 gh->msg_type = GSM48_MT_CC_PROGRESS;
2108
2109 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002110 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002111 /* user-user */
2112 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002113 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002114
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002115 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116}
2117
2118static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2119{
2120 struct gsm_mncc *connect = arg;
2121 struct msgb *msg = gsm48_msgb_alloc();
2122 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2123
Harald Welte4bfdfe72009-06-10 23:11:52 +08002124 gh->msg_type = GSM48_MT_CC_CONNECT;
2125
2126 gsm48_stop_cc_timer(trans);
2127 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2128
2129 /* facility */
2130 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002131 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002132 /* progress */
2133 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002134 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 /* connected number */
2136 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002137 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138 /* user-user */
2139 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141
2142 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2143
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002144 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145}
2146
2147static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2148{
2149 struct gsm48_hdr *gh = msgb_l3(msg);
2150 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2151 struct tlv_parsed tp;
2152 struct gsm_mncc connect;
2153
2154 gsm48_stop_cc_timer(trans);
2155
2156 memset(&connect, 0, sizeof(struct gsm_mncc));
2157 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002158 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 /* use subscriber as connected party number */
2160 if (trans->subscr) {
2161 connect.fields |= MNCC_F_CONNECTED;
2162 strncpy(connect.connected.number, trans->subscr->extension,
2163 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002164 strncpy(connect.imsi, trans->subscr->imsi,
2165 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002166 }
2167 /* facility */
2168 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2169 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2172 }
2173 /* user-user */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2175 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002176 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2178 }
2179 /* ss-version */
2180 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2181 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002182 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2184 }
2185
2186 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002187 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188
Harald Welte596fed42009-07-23 19:06:52 +02002189 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190}
2191
2192
2193static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2194{
2195 struct gsm_mncc connect_ack;
2196
2197 gsm48_stop_cc_timer(trans);
2198
2199 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002200 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201
2202 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2203 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002204
Harald Welte596fed42009-07-23 19:06:52 +02002205 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002206 &connect_ack);
2207}
2208
2209static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2210{
2211 struct msgb *msg = gsm48_msgb_alloc();
2212 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2213
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2215
2216 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2217
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002218 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219}
2220
2221static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2222{
2223 struct gsm48_hdr *gh = msgb_l3(msg);
2224 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2225 struct tlv_parsed tp;
2226 struct gsm_mncc disc;
2227
2228 gsm48_stop_cc_timer(trans);
2229
2230 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2231
2232 memset(&disc, 0, sizeof(struct gsm_mncc));
2233 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002234 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002235 /* cause */
2236 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2237 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002238 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2240 }
2241 /* facility */
2242 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2243 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002244 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2246 }
2247 /* user-user */
2248 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2249 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002250 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2252 }
2253 /* ss-version */
2254 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2255 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002256 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2258 }
2259
Harald Welte596fed42009-07-23 19:06:52 +02002260 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261
2262}
2263
Harald Weltec66b71c2009-06-11 14:23:20 +08002264static struct gsm_mncc_cause default_cause = {
2265 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2266 .coding = 0,
2267 .rec = 0,
2268 .rec_val = 0,
2269 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2270 .diag_len = 0,
2271 .diag = { 0 },
2272};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002273
2274static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2275{
2276 struct gsm_mncc *disc = arg;
2277 struct msgb *msg = gsm48_msgb_alloc();
2278 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2279
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2281
2282 gsm48_stop_cc_timer(trans);
2283 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2284
2285 /* cause */
2286 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002287 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 else
Harald Welte55c8f352010-03-07 23:40:35 +01002289 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290
2291 /* facility */
2292 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002293 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002294 /* progress */
2295 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002296 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 /* user-user */
2298 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002299 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300
2301 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002302 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002303
2304 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2305
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002306 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002307}
2308
2309static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2310{
2311 struct gsm48_hdr *gh = msgb_l3(msg);
2312 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2313 struct tlv_parsed tp;
2314 struct gsm_mncc rel;
2315 int rc;
2316
2317 gsm48_stop_cc_timer(trans);
2318
2319 memset(&rel, 0, sizeof(struct gsm_mncc));
2320 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002321 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002322 /* cause */
2323 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2324 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002325 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2327 }
2328 /* facility */
2329 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2330 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002331 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2333 }
2334 /* user-user */
2335 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2336 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002337 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002338 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2339 }
2340 /* ss-version */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2342 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2345 }
2346
Harald Weltedcaf5652009-07-23 18:56:43 +02002347 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002349 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002351 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002352 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002353 GSM48_MT_CC_RELEASE_COMPL);
2354 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002355 }
2356
2357 new_cc_state(trans, GSM_CSTATE_NULL);
2358
2359 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002360 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002361
2362 return rc;
2363}
2364
2365static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2366{
2367 struct gsm_mncc *rel = arg;
2368 struct msgb *msg = gsm48_msgb_alloc();
2369 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2370
Harald Welte4bfdfe72009-06-10 23:11:52 +08002371 gh->msg_type = GSM48_MT_CC_RELEASE;
2372
2373 trans->callref = 0;
2374
2375 gsm48_stop_cc_timer(trans);
2376 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2377
2378 /* cause */
2379 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002380 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381 /* facility */
2382 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002383 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 /* user-user */
2385 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002386 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387
Harald Weltedcaf5652009-07-23 18:56:43 +02002388 trans->cc.T308_second = 0;
2389 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390
Harald Weltedcaf5652009-07-23 18:56:43 +02002391 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2393
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002394 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395}
2396
2397static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2398{
2399 struct gsm48_hdr *gh = msgb_l3(msg);
2400 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2401 struct tlv_parsed tp;
2402 struct gsm_mncc rel;
2403 int rc = 0;
2404
2405 gsm48_stop_cc_timer(trans);
2406
2407 memset(&rel, 0, sizeof(struct gsm_mncc));
2408 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002409 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410 /* cause */
2411 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2412 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002413 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2415 }
2416 /* facility */
2417 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2418 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002419 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2421 }
2422 /* user-user */
2423 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2424 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002425 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2427 }
2428 /* ss-version */
2429 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2430 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2433 }
2434
2435 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002436 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002438 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 MNCC_REJ_IND, &rel);
2440 break;
2441 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002442 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 MNCC_REL_CNF, &rel);
2444 break;
2445 default:
Harald Welte596fed42009-07-23 19:06:52 +02002446 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447 MNCC_REL_IND, &rel);
2448 }
2449 }
2450
2451 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002452 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453
2454 return rc;
2455}
2456
2457static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2458{
2459 struct gsm_mncc *rel = arg;
2460 struct msgb *msg = gsm48_msgb_alloc();
2461 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002462 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2465
2466 trans->callref = 0;
2467
2468 gsm48_stop_cc_timer(trans);
2469
2470 /* cause */
2471 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002472 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473 /* facility */
2474 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002475 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476 /* user-user */
2477 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479
Harald Weltec7782de2010-12-21 19:31:41 +01002480 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2481
Harald Weltedcaf5652009-07-23 18:56:43 +02002482 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483
Harald Weltec7782de2010-12-21 19:31:41 +01002484 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002485}
2486
2487static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2488{
2489 struct gsm48_hdr *gh = msgb_l3(msg);
2490 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2491 struct tlv_parsed tp;
2492 struct gsm_mncc fac;
2493
2494 memset(&fac, 0, sizeof(struct gsm_mncc));
2495 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002496 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497 /* facility */
2498 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2499 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002500 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002501 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2502 }
2503 /* ss-version */
2504 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2505 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002506 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2508 }
2509
Harald Welte596fed42009-07-23 19:06:52 +02002510 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2514{
2515 struct gsm_mncc *fac = arg;
2516 struct msgb *msg = gsm48_msgb_alloc();
2517 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2518
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 gh->msg_type = GSM48_MT_CC_FACILITY;
2520
2521 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002522 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002523
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002524 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525}
2526
2527static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2528{
2529 struct gsm_mncc hold;
2530
2531 memset(&hold, 0, sizeof(struct gsm_mncc));
2532 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002533 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534}
2535
2536static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2537{
2538 struct msgb *msg = gsm48_msgb_alloc();
2539 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2540
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2542
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002543 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002544}
2545
2546static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2547{
2548 struct gsm_mncc *hold_rej = arg;
2549 struct msgb *msg = gsm48_msgb_alloc();
2550 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2551
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2553
2554 /* cause */
2555 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002556 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 else
Harald Welte55c8f352010-03-07 23:40:35 +01002558 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002560 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561}
2562
2563static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2564{
2565 struct gsm_mncc retrieve;
2566
2567 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2568 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002569 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2570 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571}
2572
2573static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2574{
2575 struct msgb *msg = gsm48_msgb_alloc();
2576 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2577
Harald Welte4bfdfe72009-06-10 23:11:52 +08002578 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2579
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002580 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002581}
2582
2583static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2584{
2585 struct gsm_mncc *retrieve_rej = arg;
2586 struct msgb *msg = gsm48_msgb_alloc();
2587 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2588
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2590
2591 /* cause */
2592 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002593 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002594 else
Harald Welte55c8f352010-03-07 23:40:35 +01002595 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002596
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002597 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598}
2599
2600static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2601{
2602 struct gsm48_hdr *gh = msgb_l3(msg);
2603 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2604 struct tlv_parsed tp;
2605 struct gsm_mncc dtmf;
2606
2607 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2608 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002609 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002610 /* keypad facility */
2611 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2612 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002613 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2615 }
2616
Harald Welte596fed42009-07-23 19:06:52 +02002617 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002618}
2619
2620static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2621{
2622 struct gsm_mncc *dtmf = arg;
2623 struct msgb *msg = gsm48_msgb_alloc();
2624 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2625
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2627
2628 /* keypad */
2629 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002630 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002632 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633}
2634
2635static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2636{
2637 struct gsm_mncc *dtmf = arg;
2638 struct msgb *msg = gsm48_msgb_alloc();
2639 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2640
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2642
2643 /* cause */
2644 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002645 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 else
Harald Welte55c8f352010-03-07 23:40:35 +01002647 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002649 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002650}
2651
2652static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2653{
2654 struct msgb *msg = gsm48_msgb_alloc();
2655 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2656
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2658
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002659 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002660}
2661
2662static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2663{
2664 struct gsm_mncc dtmf;
2665
2666 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2667 dtmf.callref = trans->callref;
2668
Harald Welte596fed42009-07-23 19:06:52 +02002669 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670}
2671
2672static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2673{
2674 struct gsm48_hdr *gh = msgb_l3(msg);
2675 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2676 struct tlv_parsed tp;
2677 struct gsm_mncc modify;
2678
2679 memset(&modify, 0, sizeof(struct gsm_mncc));
2680 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002681 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002682 /* bearer capability */
2683 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2684 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002685 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002686 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2687 }
2688
2689 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2690
Harald Welte596fed42009-07-23 19:06:52 +02002691 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002692}
2693
2694static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2695{
2696 struct gsm_mncc *modify = arg;
2697 struct msgb *msg = gsm48_msgb_alloc();
2698 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2699
Harald Welte4bfdfe72009-06-10 23:11:52 +08002700 gh->msg_type = GSM48_MT_CC_MODIFY;
2701
2702 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
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
2707 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2708
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002709 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002710}
2711
2712static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2713{
2714 struct gsm48_hdr *gh = msgb_l3(msg);
2715 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2716 struct tlv_parsed tp;
2717 struct gsm_mncc modify;
2718
2719 gsm48_stop_cc_timer(trans);
2720
2721 memset(&modify, 0, sizeof(struct gsm_mncc));
2722 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002723 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 /* bearer capability */
2725 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2726 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002727 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002728 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2729 }
2730
2731 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2732
Harald Welte596fed42009-07-23 19:06:52 +02002733 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734}
2735
2736static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2737{
2738 struct gsm_mncc *modify = arg;
2739 struct msgb *msg = gsm48_msgb_alloc();
2740 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2741
Harald Welte4bfdfe72009-06-10 23:11:52 +08002742 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2743
2744 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002745 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002746
2747 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2748
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002749 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750}
2751
2752static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2753{
2754 struct gsm48_hdr *gh = msgb_l3(msg);
2755 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2756 struct tlv_parsed tp;
2757 struct gsm_mncc modify;
2758
2759 gsm48_stop_cc_timer(trans);
2760
2761 memset(&modify, 0, sizeof(struct gsm_mncc));
2762 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002763 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002764 /* bearer capability */
2765 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2766 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002767 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2769 }
2770 /* cause */
2771 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2772 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002773 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002774 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2775 }
2776
2777 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2778
Harald Welte596fed42009-07-23 19:06:52 +02002779 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780}
2781
2782static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2783{
2784 struct gsm_mncc *modify = arg;
2785 struct msgb *msg = gsm48_msgb_alloc();
2786 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2787
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2789
2790 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002791 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002793 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794
2795 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2796
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002797 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002798}
2799
2800static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2801{
2802 struct gsm_mncc *notify = arg;
2803 struct msgb *msg = gsm48_msgb_alloc();
2804 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2805
Harald Welte4bfdfe72009-06-10 23:11:52 +08002806 gh->msg_type = GSM48_MT_CC_NOTIFY;
2807
2808 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002809 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002811 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812}
2813
2814static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2815{
2816 struct gsm48_hdr *gh = msgb_l3(msg);
2817 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2818// struct tlv_parsed tp;
2819 struct gsm_mncc notify;
2820
2821 memset(&notify, 0, sizeof(struct gsm_mncc));
2822 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002823// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002824 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002825 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826
Harald Welte596fed42009-07-23 19:06:52 +02002827 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828}
2829
2830static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2831{
2832 struct gsm_mncc *user = arg;
2833 struct msgb *msg = gsm48_msgb_alloc();
2834 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2835
Harald Welte4bfdfe72009-06-10 23:11:52 +08002836 gh->msg_type = GSM48_MT_CC_USER_INFO;
2837
2838 /* user-user */
2839 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002840 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 /* more data */
2842 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002843 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002845 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846}
2847
2848static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2849{
2850 struct gsm48_hdr *gh = msgb_l3(msg);
2851 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2852 struct tlv_parsed tp;
2853 struct gsm_mncc user;
2854
2855 memset(&user, 0, sizeof(struct gsm_mncc));
2856 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002857 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002858 /* user-user */
2859 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2860 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002861 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002862 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2863 }
2864 /* more data */
2865 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2866 user.more = 1;
2867
Harald Welte596fed42009-07-23 19:06:52 +02002868 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869}
2870
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002871static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002872{
2873 struct gsm_mncc *mode = arg;
2874
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002875 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002876}
2877
2878static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002879 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002880 int type;
2881 int (*rout) (struct gsm_trans *trans, void *arg);
2882} downstatelist[] = {
2883 /* mobile originating call establishment */
2884 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2885 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2886 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2887 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2888 {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 */
2889 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2890 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2891 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2892 /* mobile terminating call establishment */
2893 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2894 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2895 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2896 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2897 /* signalling during call */
2898 {SBIT(GSM_CSTATE_ACTIVE),
2899 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2900 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2901 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2902 {ALL_STATES,
2903 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2904 {ALL_STATES,
2905 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2906 {ALL_STATES,
2907 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2908 {SBIT(GSM_CSTATE_ACTIVE),
2909 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2910 {SBIT(GSM_CSTATE_ACTIVE),
2911 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2912 {SBIT(GSM_CSTATE_ACTIVE),
2913 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2914 {SBIT(GSM_CSTATE_ACTIVE),
2915 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2916 {SBIT(GSM_CSTATE_ACTIVE),
2917 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2918 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2919 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2920 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2921 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2922 {SBIT(GSM_CSTATE_ACTIVE),
2923 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2924 /* clearing */
2925 {SBIT(GSM_CSTATE_INITIATED),
2926 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2927 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2928 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2929 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2930 MNCC_REL_REQ, gsm48_cc_tx_release},
2931 /* special */
2932 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002933 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934};
2935
2936#define DOWNSLLEN \
2937 (sizeof(downstatelist) / sizeof(struct downstate))
2938
2939
Harald Welte76556372010-12-22 23:57:45 +01002940int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002941{
Harald Welte1a6f7982009-08-09 18:52:33 +02002942 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002944 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002945 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002946 struct gsm_mncc *data = arg, rel;
2947
Harald Welte04dc88f2010-12-22 21:45:05 +01002948 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2949
Harald Welte4bfdfe72009-06-10 23:11:52 +08002950 /* handle special messages */
2951 switch(msg_type) {
2952 case MNCC_BRIDGE:
2953 return tch_bridge(net, arg);
2954 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002955 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002956 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002957 return tch_recv_mncc(net, data->callref, 1);
2958 case GSM_TCHF_FRAME:
2959 /* Find callref */
2960 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002961 if (!trans) {
2962 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002963 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002964 }
2965 if (!trans->conn) {
2966 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002967 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002968 }
2969 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2970 /* This should be LOGL_ERROR or NOTICE, but
2971 * unfortuantely it happens for a couple of frames at
2972 * the beginning of every RTP connection */
2973 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002974 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002975 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002976 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002977 switch (bts->type) {
2978 case GSM_BTS_TYPE_NANOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01002979 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002980 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002981 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002982 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002983 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002984 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002985 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002986 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002987 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002988 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002989 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002990 }
2991 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992 }
2993
2994 memset(&rel, 0, sizeof(struct gsm_mncc));
2995 rel.callref = data->callref;
2996
2997 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002998 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002999
3000 /* Callref unknown */
3001 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003002 struct gsm_subscriber *subscr;
3003
Harald Welte4a3464c2009-07-04 10:11:24 +02003004 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003005 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3006 "Received '%s' from MNCC with "
3007 "unknown callref %d\n", data->called.number,
3008 get_mncc_name(msg_type), data->callref);
3009 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003010 return mncc_release_ind(net, NULL, data->callref,
3011 GSM48_CAUSE_LOC_PRN_S_LU,
3012 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003013 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003014 if (!data->called.number[0] && !data->imsi[0]) {
3015 DEBUGP(DCC, "(bts - trx - ts - ti) "
3016 "Received '%s' from MNCC with "
3017 "no number or IMSI\n", get_mncc_name(msg_type));
3018 /* Invalid number */
3019 return mncc_release_ind(net, NULL, data->callref,
3020 GSM48_CAUSE_LOC_PRN_S_LU,
3021 GSM48_CC_CAUSE_INV_NR_FORMAT);
3022 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003023 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003024 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003025 subscr = subscr_get_by_extension(net,
3026 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003027 else
Harald Welte9176bd42009-07-23 18:46:00 +02003028 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 /* If subscriber is not found */
3030 if (!subscr) {
3031 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3032 "Received '%s' from MNCC with "
3033 "unknown subscriber %s\n", data->called.number,
3034 get_mncc_name(msg_type), data->called.number);
3035 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003036 return mncc_release_ind(net, NULL, data->callref,
3037 GSM48_CAUSE_LOC_PRN_S_LU,
3038 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003039 }
3040 /* If subscriber is not "attached" */
3041 if (!subscr->lac) {
3042 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3043 "Received '%s' from MNCC with "
3044 "detached subscriber %s\n", data->called.number,
3045 get_mncc_name(msg_type), data->called.number);
3046 subscr_put(subscr);
3047 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003048 return mncc_release_ind(net, NULL, data->callref,
3049 GSM48_CAUSE_LOC_PRN_S_LU,
3050 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003051 }
3052 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003053 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3054 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003055 DEBUGP(DCC, "No memory for trans.\n");
3056 subscr_put(subscr);
3057 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003058 mncc_release_ind(net, NULL, data->callref,
3059 GSM48_CAUSE_LOC_PRN_S_LU,
3060 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003061 return -ENOMEM;
3062 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003064 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003065
Harald Welte4bfdfe72009-06-10 23:11:52 +08003066 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003067 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 /* find transaction with this subscriber already paging */
3069 llist_for_each_entry(transt, &net->trans_list, entry) {
3070 /* Transaction of our lchan? */
3071 if (transt == trans ||
3072 transt->subscr != subscr)
3073 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003074 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 "Received '%s' from MNCC with "
3076 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003077 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003079 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003080 subscr_put(subscr);
3081 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003082 return 0;
3083 }
3084 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003085 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003086
3087 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003088 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3089 if (!trans->paging_request) {
3090 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3091 subscr_put(subscr);
3092 trans_free(trans);
3093 return 0;
3094 }
3095
3096 *trans->paging_request = subscr->net;
3097 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003098
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003099 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100 return 0;
3101 }
3102 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003103 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003104 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003105 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003106
3107 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003108 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003109
3110 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003111 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003112 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003113 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003114 (trans->subscr)?(trans->subscr->extension):"-",
3115 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003116 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3117 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003118 if (msg_type == MNCC_REL_REQ)
3119 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3120 else
3121 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3122 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003123 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003124 return rc;
3125 }
3126
3127 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3128 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003129 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003130 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003131 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003132 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003133 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003134
3135 /* Find function for current state and message */
3136 for (i = 0; i < DOWNSLLEN; i++)
3137 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003138 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003139 break;
3140 if (i == DOWNSLLEN) {
3141 DEBUGP(DCC, "Message unhandled at this state.\n");
3142 return 0;
3143 }
3144
3145 rc = downstatelist[i].rout(trans, arg);
3146
3147 return rc;
3148}
3149
3150
3151static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003152 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003153 int type;
3154 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3155} datastatelist[] = {
3156 /* mobile originating call establishment */
3157 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3158 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3159 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3160 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3161 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3162 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3163 /* mobile terminating call establishment */
3164 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3165 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3166 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3167 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003168 {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 +08003169 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3170 /* signalling during call */
3171 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3172 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3173 {SBIT(GSM_CSTATE_ACTIVE),
3174 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3175 {ALL_STATES,
3176 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3177 {ALL_STATES,
3178 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3179 {ALL_STATES,
3180 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3181 {SBIT(GSM_CSTATE_ACTIVE),
3182 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3183 {SBIT(GSM_CSTATE_ACTIVE),
3184 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3185 {SBIT(GSM_CSTATE_ACTIVE),
3186 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3187 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3188 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3189 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3190 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3191 {SBIT(GSM_CSTATE_ACTIVE),
3192 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3193 /* clearing */
3194 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3195 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3196 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3197 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3198 {ALL_STATES, /* 5.4.3.4 */
3199 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3200};
3201
3202#define DATASLLEN \
3203 (sizeof(datastatelist) / sizeof(struct datastate))
3204
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003205static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003206{
3207 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003208 uint8_t msg_type = gh->msg_type & 0xbf;
3209 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003210 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003211 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003212
Harald Welte4bfdfe72009-06-10 23:11:52 +08003213 if (msg_type & 0x80) {
3214 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3215 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003216 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003217
Harald Welte4bfdfe72009-06-10 23:11:52 +08003218 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003219 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003220
Harald Welte6f5aee02009-07-23 21:21:14 +02003221 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003222 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003223 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003224 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003225 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3226 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003227
3228 /* Create transaction */
3229 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003230 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003231 "creating new trans.\n", transaction_id);
3232 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003233 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003234 transaction_id, new_callref++);
3235 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003236 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003237 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003238 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003239 GSM48_MT_CC_RELEASE_COMPL);
3240 return -ENOMEM;
3241 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003242 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003243 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003244 }
3245
3246 /* find function for current state and message */
3247 for (i = 0; i < DATASLLEN; i++)
3248 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003249 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003250 break;
3251 if (i == DATASLLEN) {
3252 DEBUGP(DCC, "Message unhandled at this state.\n");
3253 return 0;
3254 }
3255
3256 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003257
3258 return rc;
3259}
3260
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003261/* Create a dummy to wait five seconds */
3262static void release_anchor(struct gsm_subscriber_connection *conn)
3263{
3264 if (!conn->anch_operation)
3265 return;
3266
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003267 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003268 talloc_free(conn->anch_operation);
3269 conn->anch_operation = NULL;
3270}
3271
3272static void anchor_timeout(void *_data)
3273{
3274 struct gsm_subscriber_connection *con = _data;
3275
3276 release_anchor(con);
3277 msc_release_connection(con);
3278}
3279
3280int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3281{
3282 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3283 if (!conn->anch_operation)
3284 return -1;
3285
3286 conn->anch_operation->timeout.data = conn;
3287 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003288 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003289 return 0;
3290}
3291
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003292/* here we get data from the BSC level... */
3293int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003294{
3295 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003296 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003297 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003298
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003299 if (silent_call_reroute(conn, msg))
3300 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003301
3302 switch (pdisc) {
3303 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003304 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003305 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003306 break;
3307 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003308 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003309 break;
3310 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003311 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003312 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003313 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003314 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003315 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003316 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003317 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003318 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003319 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3320 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003321 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003322 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003323 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003324 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003325 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003326 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003327 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3328 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003329 break;
3330 }
3331
3332 return rc;
3333}
Harald Welte8470bf22008-12-25 23:28:35 +00003334
Harald Welte805f6442009-07-28 18:25:29 +02003335/*
3336 * This will be ran by the linker when loading the DSO. We use it to
3337 * do system initialization, e.g. registration of signal handlers.
3338 */
3339static __attribute__((constructor)) void on_dso_load_0408(void)
3340{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003341 osmo_signal_register_handler(SS_HO, handle_ho_signal, NULL);
3342 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003343}