blob: 7bf62b784aa4a95797c8c797656371a8cac6f90a [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
Harald Welte4bfdfe72009-06-10 23:11:52 +0800613#if 0
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200614static uint8_t to_bcd8(uint8_t val)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800615{
616 return ((val / 10) << 4) | (val % 10);
617}
618#endif
619
Harald Weltedb253af2008-12-30 17:56:55 +0000620/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800621int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000622{
623 struct msgb *msg = gsm48_msgb_alloc();
624 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800625 struct gsm_network *net = conn->bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200626 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200627 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800628#if 0
629 time_t cur_t;
630 struct tm* cur_time;
631 int tz15min;
632#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000633
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800634 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000635
636 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
637 gh->proto_discr = GSM48_PDISC_MM;
638 gh->msg_type = GSM48_MT_MM_INFO;
639
640 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200641#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000642 name_len = strlen(net->name_long);
643 /* 10.5.3.5a */
644 ptr8 = msgb_put(msg, 3);
645 ptr8[0] = GSM48_IE_NAME_LONG;
646 ptr8[1] = name_len*2 +1;
647 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
648
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200649 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000650 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000651 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000652
653 /* FIXME: Use Cell Broadcast, not UCS-2, since
654 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200655#endif
656 name_len = (strlen(net->name_long)*7)/8;
657 name_pad = (8 - strlen(net->name_long)*7)%8;
658 if (name_pad > 0)
659 name_len++;
660 /* 10.5.3.5a */
661 ptr8 = msgb_put(msg, 3);
662 ptr8[0] = GSM48_IE_NAME_LONG;
663 ptr8[1] = name_len +1;
664 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
665
666 ptr8 = msgb_put(msg, name_len);
667 gsm_7bit_encode(ptr8, net->name_long);
668
Harald Weltedb253af2008-12-30 17:56:55 +0000669 }
670
671 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200672#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000673 name_len = strlen(net->name_short);
674 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200675 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200676 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000677 ptr8[1] = name_len*2 + 1;
678 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
679
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200680 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000681 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000682 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200683#endif
684 name_len = (strlen(net->name_short)*7)/8;
685 name_pad = (8 - strlen(net->name_short)*7)%8;
686 if (name_pad > 0)
687 name_len++;
688 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200689 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200690 ptr8[0] = GSM48_IE_NAME_SHORT;
691 ptr8[1] = name_len +1;
692 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
693
694 ptr8 = msgb_put(msg, name_len);
695 gsm_7bit_encode(ptr8, net->name_short);
696
Harald Weltedb253af2008-12-30 17:56:55 +0000697 }
698
699#if 0
700 /* Section 10.5.3.9 */
701 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800702 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000703 ptr8 = msgb_put(msg, 8);
704 ptr8[0] = GSM48_IE_NET_TIME_TZ;
705 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
706 ptr8[2] = to_bcd8(cur_time->tm_mon);
707 ptr8[3] = to_bcd8(cur_time->tm_mday);
708 ptr8[4] = to_bcd8(cur_time->tm_hour);
709 ptr8[5] = to_bcd8(cur_time->tm_min);
710 ptr8[6] = to_bcd8(cur_time->tm_sec);
711 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
712 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800713 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000714 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800715 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000716#endif
717
Daniel Willmanneea93372009-08-13 03:42:07 +0200718 DEBUGP(DMM, "-> MM INFO\n");
719
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800720 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000721}
722
Harald Welte7984d5c2009-08-12 22:56:50 +0200723/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200724int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200725{
726 struct msgb *msg = gsm48_msgb_alloc();
727 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200728 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200729
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200730 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200731
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800732 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200733 gh->proto_discr = GSM48_PDISC_MM;
734 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
735
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100736 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200737
Harald Welte7984d5c2009-08-12 22:56:50 +0200738 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200739 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200740 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200741
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800742 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200743}
744
745/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800746int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200747{
748 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800749 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200750}
751
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800752static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000753{
Harald Welte4b634542008-12-27 01:55:51 +0000754 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800755 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000756}
Harald Welteba4cf162009-01-10 01:49:35 +0000757
758/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100759static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000760 enum gsm48_reject_value value)
761{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800762 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000763
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800764 msg = gsm48_create_mm_serv_rej(value);
765 if (!msg) {
766 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
767 return -1;
768 }
Harald Welteba4cf162009-01-10 01:49:35 +0000769
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800770 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100771 msg->lchan = conn->lchan;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800772 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000773}
774
Sylvain Munautba87f452009-12-24 00:28:46 +0100775static int _gsm48_rx_mm_serv_req_sec_cb(
776 unsigned int hooknum, unsigned int event,
777 struct msgb *msg, void *data, void *param)
778{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800779 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100780 int rc = 0;
781
782 switch (event) {
783 case GSM_SECURITY_AUTH_FAILED:
784 /* Nothing to do */
785 break;
786
787 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800788 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100789 break;
790
791 case GSM_SECURITY_SUCCEEDED:
792 /* nothing to do. CIPHER MODE COMMAND is
793 * implicit CM SERV ACK */
794 break;
795
796 default:
797 rc = -EINVAL;
798 };
799
800 return rc;
801}
802
Harald Welte4ed0e922009-01-10 03:17:30 +0000803/*
804 * Handle CM Service Requests
805 * a) Verify that the packet is long enough to contain the information
806 * we require otherwsie reject with INCORRECT_MESSAGE
807 * b) Try to parse the TMSI. If we do not have one reject
808 * c) Check that we know the subscriber with the TMSI otherwise reject
809 * with a HLR cause
810 * d) Set the subscriber on the gsm_lchan and accept
811 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800812static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000813{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200814 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200815 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000816
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800817 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000818 struct gsm_subscriber *subscr;
819 struct gsm48_hdr *gh = msgb_l3(msg);
820 struct gsm48_service_request *req =
821 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800822 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200823 uint8_t classmark2_len = gh->data[1];
824 uint8_t *classmark2 = gh->data+2;
825 uint8_t mi_len = *(classmark2 + classmark2_len);
826 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000827
Harald Weltec9e02182009-05-01 19:07:53 +0000828 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000829 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000830 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800831 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000832 GSM48_REJECT_INCORRECT_MESSAGE);
833 }
834
835 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000836 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800837 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000838 GSM48_REJECT_INCORRECT_MESSAGE);
839 }
840
Harald Weltec9e02182009-05-01 19:07:53 +0000841 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000842 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000843 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800844 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000845 GSM48_REJECT_INCORRECT_MESSAGE);
846 }
847
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200848 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000849 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000850 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000851
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200852 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100853
Harald Welte3ac7f102009-08-10 10:12:45 +0200854 if (is_siemens_bts(bts))
855 send_siemens_mrpci(msg->lchan, classmark2-1);
856
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200857 subscr = subscr_get_by_tmsi(bts->network,
858 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000859
Harald Welte2a139372009-02-22 21:14:55 +0000860 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000861 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800862 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000863 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
864
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800865 if (!conn->subscr)
866 conn->subscr = subscr;
867 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100868 subscr_put(subscr); /* lchan already has a ref, don't need another one */
869 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000870 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
871 subscr_put(subscr);
872 }
873
Harald Weltec2e302d2009-07-05 14:08:13 +0200874 subscr->equipment.classmark2_len = classmark2_len;
875 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
876 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000877
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800878 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100879 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000880}
881
Harald Welte2a139372009-02-22 21:14:55 +0000882static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
883{
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200884 struct e1inp_sign_link *sign_link = msg->lchan->ts->trx->rsl_link;
Harald Welte9176bd42009-07-23 18:46:00 +0200885 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000886 struct gsm48_hdr *gh = msgb_l3(msg);
887 struct gsm48_imsi_detach_ind *idi =
888 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200889 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200890 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800891 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000892
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200893 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000894 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
895 mi_type, mi_string);
896
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200897 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100898
Harald Welte2a139372009-02-22 21:14:55 +0000899 switch (mi_type) {
900 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200901 subscr = subscr_get_by_tmsi(bts->network,
902 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000903 break;
904 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200905 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000906 break;
907 case GSM_MI_TYPE_IMEI:
908 case GSM_MI_TYPE_IMEISV:
909 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000910 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000911 break;
912 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000913 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000914 break;
915 }
916
Holger Freyther4a49e772009-04-12 05:37:29 +0000917 if (subscr) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200918 subscr_update(subscr, sign_link->trx->bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000919 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100920 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200921
922 subscr->equipment.classmark1 = idi->classmark1;
923 db_sync_equipment(&subscr->equipment);
924
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000925 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000926 } else
Harald Welte2a139372009-02-22 21:14:55 +0000927 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
928
Harald Welte31981a02009-12-20 09:58:40 +0100929 /* FIXME: iterate over all transactions and release them,
930 * imagine an IMSI DETACH happening during an active call! */
931
Harald Welteb83d9382009-12-12 21:00:48 +0100932 /* subscriber is detached: should we release lchan? */
Harald Welte2a139372009-02-22 21:14:55 +0000933 return 0;
934}
935
Harald Welted2a7f5a2009-06-05 20:08:20 +0000936static int gsm48_rx_mm_status(struct msgb *msg)
937{
938 struct gsm48_hdr *gh = msgb_l3(msg);
939
940 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
941
942 return 0;
943}
944
Sylvain Munaut30a15382009-12-24 00:27:26 +0100945/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800946static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100947{
948 struct gsm48_hdr *gh = msgb_l3(msg);
949 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800950 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100951
Sylvain Munautc593cf12010-06-10 23:51:41 +0200952 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200953 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +0100954
955 /* Safety check */
956 if (!conn->sec_operation) {
957 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
958 return -EIO;
959 }
960
961 /* Validate SRES */
962 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100963 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100964 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200965
966 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200967 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +0200968
Sylvain Munaut30a15382009-12-24 00:27:26 +0100969 if (cb)
970 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800971 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100972
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100973 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100974 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +0100975 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100976 }
977
Sylvain Munautc593cf12010-06-10 23:51:41 +0200978 DEBUGPC(DMM, "OK\n");
979
Sylvain Munaut30a15382009-12-24 00:27:26 +0100980 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100981 return gsm0808_cipher_mode(conn, net->a5_encryption,
982 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100983}
984
Harald Weltebf5e8df2009-02-03 12:59:45 +0000985/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800986static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000987{
988 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800989 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000990
991 switch (gh->msg_type & 0xbf) {
992 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200993 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800994 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000995 break;
996 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800997 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000998 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000999 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001000 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001001 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001002 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001003 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001004 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001005 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001006 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001007 conn->subscr ?
1008 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001009 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001010 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001011 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001012 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +00001013 rc = gsm48_rx_mm_imsi_detach_ind(msg);
1014 break;
1015 case GSM48_MT_MM_CM_REEST_REQ:
1016 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1017 break;
1018 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001019 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001020 break;
1021 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001022 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001023 gh->msg_type);
1024 break;
1025 }
1026
1027 return rc;
1028}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001029
Harald Welte2d35ae62009-02-06 12:02:13 +00001030/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001031static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001032{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001033 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001034 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001035 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001036 uint8_t *classmark2_lv = gh->data + 1;
1037 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001038 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001039 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001040 int rc = 0;
1041
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001042 resp = (struct gsm48_pag_resp *) &gh->data[0];
1043 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1044 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001045 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1046 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001047
Harald Weltefe18d8f2009-02-22 21:14:24 +00001048 switch (mi_type) {
1049 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001050 subscr = subscr_get_by_tmsi(bts->network,
1051 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001052 break;
1053 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001054 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001055 break;
1056 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001057
1058 if (!subscr) {
1059 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001060 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001061 return -EINVAL;
1062 }
1063 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001064 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001065
Harald Weltec2e302d2009-07-05 14:08:13 +02001066 subscr->equipment.classmark2_len = *classmark2_lv;
1067 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1068 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001069
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001070 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001071 return rc;
1072}
1073
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001074static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001075{
1076 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001077 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001078 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001079 uint8_t cm2_len, cm3_len = 0;
1080 uint8_t *cm2, *cm3 = NULL;
Harald Weltef7c43522009-06-09 20:24:21 +00001081
1082 DEBUGP(DRR, "CLASSMARK CHANGE ");
1083
1084 /* classmark 2 */
1085 cm2_len = gh->data[0];
1086 cm2 = &gh->data[1];
1087 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1088
1089 if (payload_len > cm2_len + 1) {
1090 /* we must have a classmark3 */
1091 if (gh->data[cm2_len+1] != 0x20) {
1092 DEBUGPC(DRR, "ERR CM3 TAG\n");
1093 return -EINVAL;
1094 }
1095 if (cm2_len > 3) {
1096 DEBUGPC(DRR, "CM2 too long!\n");
1097 return -EINVAL;
1098 }
1099
1100 cm3_len = gh->data[cm2_len+2];
1101 cm3 = &gh->data[cm2_len+3];
1102 if (cm3_len > 14) {
1103 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1104 return -EINVAL;
1105 }
1106 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1107 }
1108 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001109 subscr->equipment.classmark2_len = cm2_len;
1110 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001111 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001112 subscr->equipment.classmark3_len = cm3_len;
1113 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001114 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001115 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001116 }
1117
Harald Weltef7c43522009-06-09 20:24:21 +00001118 return 0;
1119}
1120
Harald Weltecf5b3592009-05-01 18:28:42 +00001121static int gsm48_rx_rr_status(struct msgb *msg)
1122{
1123 struct gsm48_hdr *gh = msgb_l3(msg);
1124
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001125 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001126 rr_cause_name(gh->data[0]));
1127
1128 return 0;
1129}
1130
Harald Weltef7c43522009-06-09 20:24:21 +00001131static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1132{
Harald Welted12b0fd2009-12-15 21:36:05 +01001133 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001134
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001135 /* This shouldn't actually end up here, as RSL treats
1136 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1137 * directly into gsm48_parse_meas_rep */
1138 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001139 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001140
1141 return 0;
1142}
1143
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001144static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145{
1146 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001147 uint8_t apdu_id_flags;
1148 uint8_t apdu_len;
1149 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001150
1151 apdu_id_flags = gh->data[0];
1152 apdu_len = gh->data[1];
1153 apdu_data = gh->data+2;
1154
1155 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001156 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001157
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001158 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001159}
1160
Sylvain Munaut30a15382009-12-24 00:27:26 +01001161/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001162static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001163{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001164 gsm_cbfn *cb;
1165 int rc = 0;
1166
1167 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1168
1169 /* Safety check */
1170 if (!conn->sec_operation) {
1171 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1172 return -EIO;
1173 }
1174
1175 /* FIXME: check for MI (if any) */
1176
1177 /* Call back whatever was in progress (if anything) ... */
1178 cb = conn->sec_operation->cb;
1179 if (cb) {
1180 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001181 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001182 }
1183
1184 /* Complete the operation */
1185 release_security_operation(conn);
1186
1187 return rc;
1188}
1189
Harald Welted011e8b2009-11-29 22:45:52 +01001190/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001191static int gsm48_rx_rr_ho_compl(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 COMPLETE 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_COMPL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001202 /* FIXME: release old channel */
1203
1204 return 0;
1205}
1206
1207/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001208static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001209{
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001210 struct lchan_signal_data sig;
Harald Welted011e8b2009-11-29 22:45:52 +01001211 struct gsm48_hdr *gh = msgb_l3(msg);
1212
1213 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1214 rr_cause_name(gh->data[0]));
1215
Holger Hans Peter Freyther08eebd52010-12-27 13:28:20 +01001216 sig.lchan = msg->lchan;
1217 sig.mr = NULL;
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02001218 osmo_signal_dispatch(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, &sig);
Harald Welted011e8b2009-11-29 22:45:52 +01001219 /* FIXME: release allocated new channel */
1220
1221 return 0;
1222}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001223
Harald Weltebf5e8df2009-02-03 12:59:45 +00001224/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001225static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001226{
1227 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001228 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001229
1230 switch (gh->msg_type) {
1231 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001232 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001233 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001234 case GSM48_MT_RR_GPRS_SUSP_REQ:
1235 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1236 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001237 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001238 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001239 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001240 case GSM48_MT_RR_STATUS:
1241 rc = gsm48_rx_rr_status(msg);
1242 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001243 case GSM48_MT_RR_MEAS_REP:
1244 rc = gsm48_rx_rr_meas_rep(msg);
1245 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001246 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001247 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001248 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001249 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001250 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001251 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001252 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001253 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001254 break;
1255 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001256 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001257 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001258 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001259 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1260 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001261 break;
1262 }
1263
Harald Welte2d35ae62009-02-06 12:02:13 +00001264 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001265}
1266
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001267int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1268 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001269{
1270 struct msgb *msg = gsm48_msgb_alloc();
1271 struct gsm48_hdr *gh;
1272
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001273 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001274
1275 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1276 apdu_id, apdu_len);
1277
1278 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1279 gh->proto_discr = GSM48_PDISC_RR;
1280 gh->msg_type = GSM48_MT_RR_APP_INFO;
1281 gh->data[0] = apdu_id;
1282 gh->data[1] = apdu_len;
1283 memcpy(gh->data+2, apdu, apdu_len);
1284
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001285 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001286}
1287
Harald Welte4bc90a12008-12-27 16:32:52 +00001288/* Call Control */
1289
Harald Welte7584aea2009-02-11 11:44:12 +00001290/* The entire call control code is written in accordance with Figure 7.10c
1291 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1292 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1293 * it for voice */
1294
Harald Welte4bfdfe72009-06-10 23:11:52 +08001295static void new_cc_state(struct gsm_trans *trans, int state)
1296{
1297 if (state > 31 || state < 0)
1298 return;
1299
1300 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001301 gsm48_cc_state_name(trans->cc.state),
1302 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303
Harald Weltedcaf5652009-07-23 18:56:43 +02001304 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001305}
1306
1307static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
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));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001311 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001312
Harald Welte4bc90a12008-12-27 16:32:52 +00001313 gh->msg_type = GSM48_MT_CC_STATUS;
1314
1315 cause = msgb_put(msg, 3);
1316 cause[0] = 2;
1317 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1318 cause[2] = 0x80 | 30; /* response to status inquiry */
1319
1320 call_state = msgb_put(msg, 1);
1321 call_state[0] = 0xc0 | 0x00;
1322
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001323 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001324}
1325
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001326static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001327 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001328{
1329 struct msgb *msg = gsm48_msgb_alloc();
1330 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1331
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001332 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001333
Harald Welte6f4b7532008-12-29 00:39:37 +00001334 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001335 gh->msg_type = msg_type;
1336
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001337 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001338}
1339
Harald Welte4bfdfe72009-06-10 23:11:52 +08001340static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1341{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001342 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001343 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001344 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001345 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346 }
1347}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001348
Harald Welte4bfdfe72009-06-10 23:11:52 +08001349static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1350 int msg_type, struct gsm_mncc *mncc)
1351{
1352 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001353 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354
1355 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001356 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001357 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001359 trans->conn->lchan->ts->trx->bts->nr,
1360 trans->conn->lchan->ts->trx->nr,
1361 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001362 (trans->subscr)?(trans->subscr->extension):"-",
1363 get_mncc_name(msg_type));
1364 else
1365 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1366 "Sending '%s' to MNCC.\n",
1367 (trans->subscr)?(trans->subscr->extension):"-",
1368 get_mncc_name(msg_type));
1369 else
1370 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1371 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1372
1373 mncc->msg_type = msg_type;
1374
Harald Welte966636f2009-06-26 19:39:35 +02001375 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001376 if (!msg)
1377 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001378
1379 data = msgb_put(msg, sizeof(struct gsm_mncc));
1380 memcpy(data, mncc, sizeof(struct gsm_mncc));
1381
Harald Welte54209c22010-12-22 23:43:29 +01001382 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383
1384 return 0;
1385}
1386
1387int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001388 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001389{
1390 struct gsm_mncc rel;
1391
Harald Welte92f70c52009-06-12 01:54:08 +08001392 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001394 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1396}
1397
Harald Weltedcaf5652009-07-23 18:56:43 +02001398/* Call Control Specific transaction release.
1399 * gets called by trans_free, DO NOT CALL YOURSELF! */
1400void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 gsm48_stop_cc_timer(trans);
1403
1404 /* send release to L4, if callref still exists */
1405 if (trans->callref) {
1406 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001407 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001408 GSM48_CAUSE_LOC_PRN_S_LU,
1409 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001410 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001411 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001412 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001413 if (trans->conn)
1414 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001415}
1416
1417static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001418
Harald Welte09e38af2009-02-16 22:52:23 +00001419/* call-back from paging the B-end of the connection */
1420static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001421 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001422{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001423 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001424 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001425 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001426 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001427
Harald Welte09e38af2009-02-16 22:52:23 +00001428 if (hooknum != GSM_HOOK_RR_PAGING)
1429 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001430
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001431 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001432 if (!net) {
1433 DEBUGP(DCC, "Error Network not set!\n");
1434 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001435 }
Harald Welte7584aea2009-02-11 11:44:12 +00001436
Harald Welte4bfdfe72009-06-10 23:11:52 +08001437 /* check all tranactions (without lchan) for subscriber */
1438 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001439 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001440 continue;
1441 switch (event) {
1442 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001443 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001444 break;
1445 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001446 transt->subscr->extension);
1447 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001448 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001449 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001450 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001451 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001452 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001453 }
1454 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001455 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001456 break;
1457 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001458 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001459 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001460 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001461 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001462 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001463 mncc_release_ind(transt->subscr->net, transt,
1464 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001465 GSM48_CAUSE_LOC_PRN_S_LU,
1466 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001467 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001468 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001469 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001470 break;
1471 }
1472 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001473
1474 talloc_free(paging_request);
1475
1476 /*
1477 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1478 * failure and then the subscr will be poke again. This needs a lot of fixing
1479 * in the subscriber queue code.
1480 */
1481 if (!found && conn)
1482 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001483 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001484}
Harald Welte7584aea2009-02-11 11:44:12 +00001485
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001486static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001487
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001488/* handle audio path for handover */
1489static int handle_ho_signal(unsigned int subsys, unsigned int signal,
1490 void *handler_data, void *signal_data)
1491{
1492 struct rtp_socket *old_rs, *new_rs, *other_rs;
1493 struct ho_signal_data *sig = signal_data;
1494
1495 if (subsys != SS_HO || signal != S_HANDOVER_ACK)
1496 return 0;
1497
1498 if (ipacc_rtp_direct) {
1499 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1500 return 0;
1501 }
1502
1503 /* RTP Proxy mode */
1504 new_rs = sig->new_lchan->abis_ip.rtp_socket;
1505 old_rs = sig->old_lchan->abis_ip.rtp_socket;
1506
1507 if (!new_rs) {
1508 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1509 return -EIO;
1510 }
1511
1512 rsl_ipacc_mdcx_to_rtpsock(sig->new_lchan);
1513
1514 if (!old_rs) {
1515 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1516 return -EIO;
1517 }
1518
1519 /* copy rx_action and reference to other sock */
1520 new_rs->rx_action = old_rs->rx_action;
1521 new_rs->tx_action = old_rs->tx_action;
1522 new_rs->transmit = old_rs->transmit;
1523
1524 switch (sig->old_lchan->abis_ip.rtp_socket->rx_action) {
1525 case RTP_PROXY:
1526 other_rs = old_rs->proxy.other_sock;
1527 rtp_socket_proxy(new_rs, other_rs);
1528 /* delete reference to other end socket to prevent
1529 * rtp_socket_free() from removing the inverse reference */
1530 old_rs->proxy.other_sock = NULL;
1531 break;
1532 case RTP_RECV_UPSTREAM:
1533 new_rs->receive = old_rs->receive;
1534 break;
1535 case RTP_NONE:
1536 break;
1537 }
1538
1539 return 0;
1540}
1541
Harald Welte805f6442009-07-28 18:25:29 +02001542/* some other part of the code sends us a signal */
1543static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1544 void *handler_data, void *signal_data)
1545{
1546 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001547 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001548 struct gsm_network *net;
1549 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001550
1551 if (subsys != SS_ABISIP)
1552 return 0;
1553
1554 /* in case we use direct BTS-to-BTS RTP */
1555 if (ipacc_rtp_direct)
1556 return 0;
1557
Harald Welte805f6442009-07-28 18:25:29 +02001558 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001559 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001560 /* in case we don't use direct BTS-to-BTS RTP */
1561 /* the BTS has successfully bound a TCH to a local ip/port,
1562 * which means we can connect our UDP socket to it */
1563 if (lchan->abis_ip.rtp_socket) {
1564 rtp_socket_free(lchan->abis_ip.rtp_socket);
1565 lchan->abis_ip.rtp_socket = NULL;
1566 }
1567
1568 lchan->abis_ip.rtp_socket = rtp_socket_create();
1569 if (!lchan->abis_ip.rtp_socket)
1570 return -EIO;
1571
1572 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1573 lchan->abis_ip.bound_ip,
1574 lchan->abis_ip.bound_port);
1575 if (rc < 0)
1576 return -EIO;
1577
Harald Welteda7ab742009-12-19 22:23:05 +01001578 /* check if any transactions on this lchan still have
1579 * a tch_recv_mncc request pending */
1580 net = lchan->ts->trx->bts->network;
1581 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001582 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001583 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1584 tch_recv_mncc(net, trans->callref, 1);
1585 }
1586 }
Harald Welte805f6442009-07-28 18:25:29 +02001587 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001588 case S_ABISIP_DLCX_IND:
1589 /* the BTS tells us a RTP stream has been disconnected */
1590 if (lchan->abis_ip.rtp_socket) {
1591 rtp_socket_free(lchan->abis_ip.rtp_socket);
1592 lchan->abis_ip.rtp_socket = NULL;
1593 }
1594
1595 break;
Harald Welte805f6442009-07-28 18:25:29 +02001596 }
1597
1598 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001599}
1600
Harald Welte49f48b82009-02-17 15:29:33 +00001601/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001602static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001603{
Harald Welte11fa29c2009-02-19 17:24:39 +00001604 struct gsm_bts *bts = lchan->ts->trx->bts;
1605 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001606 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001607
Harald Welte11fa29c2009-02-19 17:24:39 +00001608 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1609 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1610 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1611
1612 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001613 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001614 return -EINVAL;
1615 }
Harald Welteda7ab742009-12-19 22:23:05 +01001616
1617 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001618 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001619 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001620 if (!ipacc_rtp_direct) {
1621 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001622 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001623 if (rc < 0)
1624 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001625 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001626 if (rc < 0)
1627 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001628 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301629 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1630 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001631 } else {
1632 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301633 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1634 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301635 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001636 if (rc < 0)
1637 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301638 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1639 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301640 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001641 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001642 break;
1643 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001644 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001645 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001646 trau_mux_map_lchan(lchan, remote_lchan);
1647 break;
1648 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001649 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001650 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001651 }
Harald Welte49f48b82009-02-17 15:29:33 +00001652
1653 return 0;
1654}
1655
Harald Welte4bfdfe72009-06-10 23:11:52 +08001656/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001657static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001658{
Harald Weltedcaf5652009-07-23 18:56:43 +02001659 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1660 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001661
Harald Welte4bfdfe72009-06-10 23:11:52 +08001662 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001663 return -EIO;
1664
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001665 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001666 return -EIO;
1667
1668 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001669 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001670}
1671
Harald Welteda7ab742009-12-19 22:23:05 +01001672/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001673static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001674{
1675 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001676 struct gsm_lchan *lchan;
1677 struct gsm_bts *bts;
1678 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001679
Harald Welte4bfdfe72009-06-10 23:11:52 +08001680 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001681 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001682 if (!trans)
1683 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001684 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001685 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001686 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001687 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001688
Harald Welteda7ab742009-12-19 22:23:05 +01001689 switch (bts->type) {
1690 case GSM_BTS_TYPE_NANOBTS:
1691 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001692 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001693 return -EINVAL;
1694 }
1695 /* in case, we don't have a RTP socket yet, we note this
1696 * in the transaction and try later */
1697 if (!lchan->abis_ip.rtp_socket) {
1698 trans->tch_recv = enable;
1699 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1700 return 0;
1701 }
1702 if (enable) {
1703 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001704 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001705 if (rc < 0)
1706 return rc;
1707 /* assign socket to application interface */
1708 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1709 net, callref);
1710 } else
1711 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1712 net, 0);
1713 break;
1714 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001715 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001716 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001717 if (enable)
1718 return trau_recv_lchan(lchan, callref);
1719 return trau_mux_unmap(NULL, callref);
1720 break;
1721 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001722 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001723 return -EINVAL;
1724 }
1725
1726 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001727}
1728
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1730{
1731 DEBUGP(DCC, "-> STATUS ENQ\n");
1732 return gsm48_cc_tx_status(trans, msg);
1733}
1734
1735static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1736static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1737
1738static void gsm48_cc_timeout(void *arg)
1739{
1740 struct gsm_trans *trans = arg;
1741 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001742 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1743 int mo_location = GSM48_CAUSE_LOC_USER;
1744 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1745 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001746 struct gsm_mncc mo_rel, l4_rel;
1747
1748 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1749 mo_rel.callref = trans->callref;
1750 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1751 l4_rel.callref = trans->callref;
1752
Harald Weltedcaf5652009-07-23 18:56:43 +02001753 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001754 case 0x303:
1755 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001756 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 break;
1758 case 0x310:
1759 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001760 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 break;
1762 case 0x313:
1763 disconnect = 1;
1764 /* unknown, did not find it in the specs */
1765 break;
1766 case 0x301:
1767 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001768 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001769 break;
1770 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001771 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001772 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001773 gsm48_cc_tx_release(trans, &trans->cc.msg);
1774 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 break; /* stay in release state */
1776 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001777 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001778 return;
1779// release = 1;
1780// l4_cause = 14;
1781// break;
1782 case 0x306:
1783 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001784 mo_cause = trans->cc.msg.cause.value;
1785 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 break;
1787 case 0x323:
1788 disconnect = 1;
1789 break;
1790 default:
1791 release = 1;
1792 }
1793
1794 if (release && trans->callref) {
1795 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001796 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 l4_location, l4_cause);
1798 trans->callref = 0;
1799 }
1800
1801 if (disconnect && trans->callref) {
1802 /* process disconnect towards layer 4 */
1803 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001804 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 }
1806
1807 /* process disconnect towards mobile station */
1808 if (disconnect || release) {
1809 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001810 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1811 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1812 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 mo_rel.cause.diag_len = 3;
1814
1815 if (disconnect)
1816 gsm48_cc_tx_disconnect(trans, &mo_rel);
1817 if (release)
1818 gsm48_cc_tx_release(trans, &mo_rel);
1819 }
1820
1821}
1822
1823static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1824 int sec, int micro)
1825{
1826 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001827 trans->cc.timer.cb = gsm48_cc_timeout;
1828 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001829 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001830 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831}
1832
1833static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1834{
1835 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001836 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1838 struct tlv_parsed tp;
1839 struct gsm_mncc setup;
1840
1841 memset(&setup, 0, sizeof(struct gsm_mncc));
1842 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001843 if (trans->conn && trans->conn->lchan)
1844 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001845 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001846 /* emergency setup is identified by msg_type */
1847 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1848 setup.emergency = 1;
1849
1850 /* use subscriber as calling party number */
1851 if (trans->subscr) {
1852 setup.fields |= MNCC_F_CALLING;
1853 strncpy(setup.calling.number, trans->subscr->extension,
1854 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001855 strncpy(setup.imsi, trans->subscr->imsi,
1856 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001857 }
1858 /* bearer capability */
1859 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1860 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001861 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001862 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1863 }
1864 /* facility */
1865 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1866 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1869 }
1870 /* called party bcd number */
1871 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1872 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001873 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1875 }
1876 /* user-user */
1877 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1878 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001879 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001880 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1881 }
1882 /* ss-version */
1883 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1884 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001885 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1887 }
1888 /* CLIR suppression */
1889 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1890 setup.clir.sup = 1;
1891 /* CLIR invocation */
1892 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1893 setup.clir.inv = 1;
1894 /* cc cap */
1895 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1896 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001897 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001898 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1899 }
1900
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 new_cc_state(trans, GSM_CSTATE_INITIATED);
1902
Harald Welte (local)47df3992009-12-26 19:45:03 +01001903 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1904 subscr_name(trans->subscr), trans->subscr->extension,
1905 setup.called.number);
1906
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001907 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001908
Harald Welte4bfdfe72009-06-10 23:11:52 +08001909 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001910 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911
Harald Welte13cac662009-07-29 12:10:35 +02001912 /* MNCC code will modify the channel asynchronously, we should
1913 * ipaccess-bind only after the modification has been made to the
1914 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 return 0;
1916}
1917
1918static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001919{
1920 struct msgb *msg = gsm48_msgb_alloc();
1921 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001922 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001923 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001924
Harald Welte7ccf7782009-02-17 01:43:01 +00001925 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001926
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 /* transaction id must not be assigned */
1928 if (trans->transaction_id != 0xff) { /* unasssigned */
1929 DEBUGP(DCC, "TX Setup with assigned transaction. "
1930 "This is not allowed!\n");
1931 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001932 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001933 GSM48_CAUSE_LOC_PRN_S_LU,
1934 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001936 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 return rc;
1938 }
1939
1940 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001941 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1942 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001944 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001945 GSM48_CAUSE_LOC_PRN_S_LU,
1946 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001948 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001949 return rc;
1950 }
Harald Welte78283ef2009-07-23 21:36:44 +02001951 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001952
Harald Welte65e74cc2008-12-29 01:55:35 +00001953 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001954
Harald Welte4bfdfe72009-06-10 23:11:52 +08001955 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001956
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* bearer capability */
1958 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* facility */
1961 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 /* progress */
1964 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 /* calling party BCD number */
1967 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001968 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001969 /* called party BCD number */
1970 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001971 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001972 /* user-user */
1973 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001974 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 /* redirecting party BCD number */
1976 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001977 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978 /* signal */
1979 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001980 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981
1982 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001983
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001984 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001985
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001986 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001987}
1988
Harald Welte4bfdfe72009-06-10 23:11:52 +08001989static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1990{
1991 struct gsm48_hdr *gh = msgb_l3(msg);
1992 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1993 struct tlv_parsed tp;
1994 struct gsm_mncc call_conf;
1995
1996 gsm48_stop_cc_timer(trans);
1997 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1998
1999 memset(&call_conf, 0, sizeof(struct gsm_mncc));
2000 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02002001 if (trans->conn && trans->conn->lchan)
2002 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01002003 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002004#if 0
2005 /* repeat */
2006 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
2007 call_conf.repeat = 1;
2008 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
2009 call_conf.repeat = 2;
2010#endif
2011 /* bearer capability */
2012 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2013 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002014 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2016 }
2017 /* cause */
2018 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2019 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002020 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002021 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2022 }
2023 /* cc cap */
2024 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
2025 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002026 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
2028 }
2029
2030 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
2031
Harald Welte596fed42009-07-23 19:06:52 +02002032 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
2033 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034}
2035
2036static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
2037{
2038 struct gsm_mncc *proceeding = arg;
2039 struct msgb *msg = gsm48_msgb_alloc();
2040 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2041
Harald Welte4bfdfe72009-06-10 23:11:52 +08002042 gh->msg_type = GSM48_MT_CC_CALL_PROC;
2043
2044 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
2045
2046 /* bearer capability */
2047 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01002048 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002049 /* facility */
2050 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002051 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 /* progress */
2053 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002054 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002056 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057}
2058
2059static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
2060{
2061 struct gsm48_hdr *gh = msgb_l3(msg);
2062 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2063 struct tlv_parsed tp;
2064 struct gsm_mncc alerting;
2065
2066 gsm48_stop_cc_timer(trans);
2067 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2068
2069 memset(&alerting, 0, sizeof(struct gsm_mncc));
2070 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002071 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 /* facility */
2073 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2074 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002075 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002076 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2077 }
2078
2079 /* progress */
2080 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2081 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002082 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2084 }
2085 /* ss-version */
2086 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2087 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002088 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002089 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2090 }
2091
2092 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2093
Harald Welte596fed42009-07-23 19:06:52 +02002094 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2095 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096}
2097
2098static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2099{
2100 struct gsm_mncc *alerting = arg;
2101 struct msgb *msg = gsm48_msgb_alloc();
2102 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2103
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 gh->msg_type = GSM48_MT_CC_ALERTING;
2105
2106 /* facility */
2107 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002108 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002109 /* progress */
2110 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 /* user-user */
2113 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002114 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002115
2116 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2117
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002118 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119}
2120
2121static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2122{
2123 struct gsm_mncc *progress = arg;
2124 struct msgb *msg = gsm48_msgb_alloc();
2125 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2126
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 gh->msg_type = GSM48_MT_CC_PROGRESS;
2128
2129 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002130 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002131 /* user-user */
2132 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002133 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002135 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136}
2137
2138static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2139{
2140 struct gsm_mncc *connect = arg;
2141 struct msgb *msg = gsm48_msgb_alloc();
2142 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2143
Harald Welte4bfdfe72009-06-10 23:11:52 +08002144 gh->msg_type = GSM48_MT_CC_CONNECT;
2145
2146 gsm48_stop_cc_timer(trans);
2147 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2148
2149 /* facility */
2150 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002151 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152 /* progress */
2153 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002154 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 /* connected number */
2156 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002157 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 /* user-user */
2159 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002160 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161
2162 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2163
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002164 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165}
2166
2167static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2168{
2169 struct gsm48_hdr *gh = msgb_l3(msg);
2170 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2171 struct tlv_parsed tp;
2172 struct gsm_mncc connect;
2173
2174 gsm48_stop_cc_timer(trans);
2175
2176 memset(&connect, 0, sizeof(struct gsm_mncc));
2177 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002178 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002179 /* use subscriber as connected party number */
2180 if (trans->subscr) {
2181 connect.fields |= MNCC_F_CONNECTED;
2182 strncpy(connect.connected.number, trans->subscr->extension,
2183 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002184 strncpy(connect.imsi, trans->subscr->imsi,
2185 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002186 }
2187 /* facility */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2189 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002190 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2192 }
2193 /* user-user */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2195 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002196 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2198 }
2199 /* ss-version */
2200 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2201 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002202 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2204 }
2205
2206 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002207 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002208
Harald Welte596fed42009-07-23 19:06:52 +02002209 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210}
2211
2212
2213static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2214{
2215 struct gsm_mncc connect_ack;
2216
2217 gsm48_stop_cc_timer(trans);
2218
2219 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002220 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002221
2222 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2223 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002224
Harald Welte596fed42009-07-23 19:06:52 +02002225 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226 &connect_ack);
2227}
2228
2229static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2230{
2231 struct msgb *msg = gsm48_msgb_alloc();
2232 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2233
Harald Welte4bfdfe72009-06-10 23:11:52 +08002234 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2235
2236 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2237
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002238 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239}
2240
2241static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2242{
2243 struct gsm48_hdr *gh = msgb_l3(msg);
2244 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2245 struct tlv_parsed tp;
2246 struct gsm_mncc disc;
2247
2248 gsm48_stop_cc_timer(trans);
2249
2250 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2251
2252 memset(&disc, 0, sizeof(struct gsm_mncc));
2253 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002254 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 /* cause */
2256 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2257 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002258 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2260 }
2261 /* facility */
2262 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2263 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2266 }
2267 /* user-user */
2268 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2269 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002270 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2272 }
2273 /* ss-version */
2274 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2275 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002276 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002277 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2278 }
2279
Harald Welte596fed42009-07-23 19:06:52 +02002280 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281
2282}
2283
Harald Weltec66b71c2009-06-11 14:23:20 +08002284static struct gsm_mncc_cause default_cause = {
2285 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2286 .coding = 0,
2287 .rec = 0,
2288 .rec_val = 0,
2289 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2290 .diag_len = 0,
2291 .diag = { 0 },
2292};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293
2294static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2295{
2296 struct gsm_mncc *disc = arg;
2297 struct msgb *msg = gsm48_msgb_alloc();
2298 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2299
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2301
2302 gsm48_stop_cc_timer(trans);
2303 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2304
2305 /* cause */
2306 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002307 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 else
Harald Welte55c8f352010-03-07 23:40:35 +01002309 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002310
2311 /* facility */
2312 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002313 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314 /* progress */
2315 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002316 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317 /* user-user */
2318 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002319 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002320
2321 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002322 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002323
2324 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2325
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002326 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327}
2328
2329static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2330{
2331 struct gsm48_hdr *gh = msgb_l3(msg);
2332 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2333 struct tlv_parsed tp;
2334 struct gsm_mncc rel;
2335 int rc;
2336
2337 gsm48_stop_cc_timer(trans);
2338
2339 memset(&rel, 0, sizeof(struct gsm_mncc));
2340 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002341 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342 /* cause */
2343 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2344 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2347 }
2348 /* facility */
2349 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2350 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002351 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2353 }
2354 /* user-user */
2355 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2356 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002357 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2359 }
2360 /* ss-version */
2361 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2362 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002363 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002364 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2365 }
2366
Harald Weltedcaf5652009-07-23 18:56:43 +02002367 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002369 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002371 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002372 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002373 GSM48_MT_CC_RELEASE_COMPL);
2374 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002375 }
2376
2377 new_cc_state(trans, GSM_CSTATE_NULL);
2378
2379 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002380 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002381
2382 return rc;
2383}
2384
2385static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2386{
2387 struct gsm_mncc *rel = arg;
2388 struct msgb *msg = gsm48_msgb_alloc();
2389 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2390
Harald Welte4bfdfe72009-06-10 23:11:52 +08002391 gh->msg_type = GSM48_MT_CC_RELEASE;
2392
2393 trans->callref = 0;
2394
2395 gsm48_stop_cc_timer(trans);
2396 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2397
2398 /* cause */
2399 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002400 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 /* facility */
2402 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002403 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404 /* user-user */
2405 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002406 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002407
Harald Weltedcaf5652009-07-23 18:56:43 +02002408 trans->cc.T308_second = 0;
2409 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410
Harald Weltedcaf5652009-07-23 18:56:43 +02002411 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002412 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2413
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002414 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002415}
2416
2417static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2418{
2419 struct gsm48_hdr *gh = msgb_l3(msg);
2420 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2421 struct tlv_parsed tp;
2422 struct gsm_mncc rel;
2423 int rc = 0;
2424
2425 gsm48_stop_cc_timer(trans);
2426
2427 memset(&rel, 0, sizeof(struct gsm_mncc));
2428 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002429 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430 /* cause */
2431 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2432 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002433 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002434 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2435 }
2436 /* facility */
2437 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2438 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002439 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002440 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2441 }
2442 /* user-user */
2443 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2444 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002445 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2447 }
2448 /* ss-version */
2449 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2450 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002451 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002452 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2453 }
2454
2455 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002456 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002458 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 MNCC_REJ_IND, &rel);
2460 break;
2461 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002462 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 MNCC_REL_CNF, &rel);
2464 break;
2465 default:
Harald Welte596fed42009-07-23 19:06:52 +02002466 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467 MNCC_REL_IND, &rel);
2468 }
2469 }
2470
2471 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002472 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002473
2474 return rc;
2475}
2476
2477static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2478{
2479 struct gsm_mncc *rel = arg;
2480 struct msgb *msg = gsm48_msgb_alloc();
2481 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002482 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2485
2486 trans->callref = 0;
2487
2488 gsm48_stop_cc_timer(trans);
2489
2490 /* cause */
2491 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002492 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493 /* facility */
2494 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496 /* user-user */
2497 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002498 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499
Harald Weltec7782de2010-12-21 19:31:41 +01002500 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2501
Harald Weltedcaf5652009-07-23 18:56:43 +02002502 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503
Harald Weltec7782de2010-12-21 19:31:41 +01002504 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002505}
2506
2507static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2508{
2509 struct gsm48_hdr *gh = msgb_l3(msg);
2510 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2511 struct tlv_parsed tp;
2512 struct gsm_mncc fac;
2513
2514 memset(&fac, 0, sizeof(struct gsm_mncc));
2515 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002516 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517 /* facility */
2518 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2519 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002520 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2522 }
2523 /* ss-version */
2524 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2525 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002526 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2528 }
2529
Harald Welte596fed42009-07-23 19:06:52 +02002530 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531}
2532
2533static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2534{
2535 struct gsm_mncc *fac = arg;
2536 struct msgb *msg = gsm48_msgb_alloc();
2537 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2538
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539 gh->msg_type = GSM48_MT_CC_FACILITY;
2540
2541 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002542 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002544 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545}
2546
2547static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2548{
2549 struct gsm_mncc hold;
2550
2551 memset(&hold, 0, sizeof(struct gsm_mncc));
2552 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002553 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554}
2555
2556static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2557{
2558 struct msgb *msg = gsm48_msgb_alloc();
2559 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2560
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2562
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002563 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002564}
2565
2566static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2567{
2568 struct gsm_mncc *hold_rej = arg;
2569 struct msgb *msg = gsm48_msgb_alloc();
2570 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2571
Harald Welte4bfdfe72009-06-10 23:11:52 +08002572 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2573
2574 /* cause */
2575 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002576 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 else
Harald Welte55c8f352010-03-07 23:40:35 +01002578 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579
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_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2584{
2585 struct gsm_mncc retrieve;
2586
2587 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2588 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002589 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2590 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591}
2592
2593static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2594{
2595 struct msgb *msg = gsm48_msgb_alloc();
2596 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2597
Harald Welte4bfdfe72009-06-10 23:11:52 +08002598 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2599
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002600 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601}
2602
2603static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2604{
2605 struct gsm_mncc *retrieve_rej = arg;
2606 struct msgb *msg = gsm48_msgb_alloc();
2607 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2608
Harald Welte4bfdfe72009-06-10 23:11:52 +08002609 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2610
2611 /* cause */
2612 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002613 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002614 else
Harald Welte55c8f352010-03-07 23:40:35 +01002615 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002617 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002618}
2619
2620static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2621{
2622 struct gsm48_hdr *gh = msgb_l3(msg);
2623 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2624 struct tlv_parsed tp;
2625 struct gsm_mncc dtmf;
2626
2627 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2628 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002629 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002630 /* keypad facility */
2631 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2632 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002633 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002634 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2635 }
2636
Harald Welte596fed42009-07-23 19:06:52 +02002637 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638}
2639
2640static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2641{
2642 struct gsm_mncc *dtmf = arg;
2643 struct msgb *msg = gsm48_msgb_alloc();
2644 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2645
Harald Welte4bfdfe72009-06-10 23:11:52 +08002646 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2647
2648 /* keypad */
2649 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002650 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002651
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002652 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653}
2654
2655static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2656{
2657 struct gsm_mncc *dtmf = arg;
2658 struct msgb *msg = gsm48_msgb_alloc();
2659 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2660
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2662
2663 /* cause */
2664 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002665 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666 else
Harald Welte55c8f352010-03-07 23:40:35 +01002667 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002668
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002669 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002670}
2671
2672static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2673{
2674 struct msgb *msg = gsm48_msgb_alloc();
2675 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2676
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2678
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002679 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680}
2681
2682static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2683{
2684 struct gsm_mncc dtmf;
2685
2686 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2687 dtmf.callref = trans->callref;
2688
Harald Welte596fed42009-07-23 19:06:52 +02002689 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690}
2691
2692static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2693{
2694 struct gsm48_hdr *gh = msgb_l3(msg);
2695 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2696 struct tlv_parsed tp;
2697 struct gsm_mncc modify;
2698
2699 memset(&modify, 0, sizeof(struct gsm_mncc));
2700 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002701 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 /* bearer capability */
2703 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2704 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002705 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2707 }
2708
2709 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2710
Harald Welte596fed42009-07-23 19:06:52 +02002711 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712}
2713
2714static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2715{
2716 struct gsm_mncc *modify = arg;
2717 struct msgb *msg = gsm48_msgb_alloc();
2718 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2719
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 gh->msg_type = GSM48_MT_CC_MODIFY;
2721
2722 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2723
2724 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002725 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002726
2727 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2728
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002729 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730}
2731
2732static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2733{
2734 struct gsm48_hdr *gh = msgb_l3(msg);
2735 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2736 struct tlv_parsed tp;
2737 struct gsm_mncc modify;
2738
2739 gsm48_stop_cc_timer(trans);
2740
2741 memset(&modify, 0, sizeof(struct gsm_mncc));
2742 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002743 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 /* bearer capability */
2745 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2746 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002747 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2749 }
2750
2751 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2752
Harald Welte596fed42009-07-23 19:06:52 +02002753 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754}
2755
2756static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2757{
2758 struct gsm_mncc *modify = arg;
2759 struct msgb *msg = gsm48_msgb_alloc();
2760 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2761
Harald Welte4bfdfe72009-06-10 23:11:52 +08002762 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2763
2764 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002765 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766
2767 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2768
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002769 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002770}
2771
2772static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2773{
2774 struct gsm48_hdr *gh = msgb_l3(msg);
2775 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2776 struct tlv_parsed tp;
2777 struct gsm_mncc modify;
2778
2779 gsm48_stop_cc_timer(trans);
2780
2781 memset(&modify, 0, sizeof(struct gsm_mncc));
2782 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002783 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784 /* bearer capability */
2785 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2786 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002787 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2789 }
2790 /* cause */
2791 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2792 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002793 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2795 }
2796
2797 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2798
Harald Welte596fed42009-07-23 19:06:52 +02002799 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800}
2801
2802static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2803{
2804 struct gsm_mncc *modify = arg;
2805 struct msgb *msg = gsm48_msgb_alloc();
2806 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2807
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2809
2810 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002811 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002812 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002813 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814
2815 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2816
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818}
2819
2820static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2821{
2822 struct gsm_mncc *notify = arg;
2823 struct msgb *msg = gsm48_msgb_alloc();
2824 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2825
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826 gh->msg_type = GSM48_MT_CC_NOTIFY;
2827
2828 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002829 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002830
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002831 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2835{
2836 struct gsm48_hdr *gh = msgb_l3(msg);
2837 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2838// struct tlv_parsed tp;
2839 struct gsm_mncc notify;
2840
2841 memset(&notify, 0, sizeof(struct gsm_mncc));
2842 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002843// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002844 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002845 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002846
Harald Welte596fed42009-07-23 19:06:52 +02002847 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002848}
2849
2850static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2851{
2852 struct gsm_mncc *user = arg;
2853 struct msgb *msg = gsm48_msgb_alloc();
2854 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2855
Harald Welte4bfdfe72009-06-10 23:11:52 +08002856 gh->msg_type = GSM48_MT_CC_USER_INFO;
2857
2858 /* user-user */
2859 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002860 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002861 /* more data */
2862 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002863 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002864
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002865 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002866}
2867
2868static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2869{
2870 struct gsm48_hdr *gh = msgb_l3(msg);
2871 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2872 struct tlv_parsed tp;
2873 struct gsm_mncc user;
2874
2875 memset(&user, 0, sizeof(struct gsm_mncc));
2876 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002877 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002878 /* user-user */
2879 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2880 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002881 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002882 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2883 }
2884 /* more data */
2885 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2886 user.more = 1;
2887
Harald Welte596fed42009-07-23 19:06:52 +02002888 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889}
2890
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002891static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002892{
2893 struct gsm_mncc *mode = arg;
2894
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002895 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896}
2897
2898static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002899 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 int type;
2901 int (*rout) (struct gsm_trans *trans, void *arg);
2902} downstatelist[] = {
2903 /* mobile originating call establishment */
2904 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2905 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2906 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2907 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2908 {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 */
2909 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2910 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2911 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2912 /* mobile terminating call establishment */
2913 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2914 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2915 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2916 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2917 /* signalling during call */
2918 {SBIT(GSM_CSTATE_ACTIVE),
2919 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2920 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2921 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2922 {ALL_STATES,
2923 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2924 {ALL_STATES,
2925 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2926 {ALL_STATES,
2927 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2928 {SBIT(GSM_CSTATE_ACTIVE),
2929 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2930 {SBIT(GSM_CSTATE_ACTIVE),
2931 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2932 {SBIT(GSM_CSTATE_ACTIVE),
2933 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2934 {SBIT(GSM_CSTATE_ACTIVE),
2935 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2936 {SBIT(GSM_CSTATE_ACTIVE),
2937 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2938 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2939 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2940 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2941 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2942 {SBIT(GSM_CSTATE_ACTIVE),
2943 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2944 /* clearing */
2945 {SBIT(GSM_CSTATE_INITIATED),
2946 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2947 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2948 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2949 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2950 MNCC_REL_REQ, gsm48_cc_tx_release},
2951 /* special */
2952 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002953 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002954};
2955
2956#define DOWNSLLEN \
2957 (sizeof(downstatelist) / sizeof(struct downstate))
2958
2959
Harald Welte76556372010-12-22 23:57:45 +01002960int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961{
Harald Welte1a6f7982009-08-09 18:52:33 +02002962 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002963 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002964 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002965 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002966 struct gsm_mncc *data = arg, rel;
2967
Harald Welte04dc88f2010-12-22 21:45:05 +01002968 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2969
Harald Welte4bfdfe72009-06-10 23:11:52 +08002970 /* handle special messages */
2971 switch(msg_type) {
2972 case MNCC_BRIDGE:
2973 return tch_bridge(net, arg);
2974 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002975 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002977 return tch_recv_mncc(net, data->callref, 1);
2978 case GSM_TCHF_FRAME:
2979 /* Find callref */
2980 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002981 if (!trans) {
2982 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002983 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002984 }
2985 if (!trans->conn) {
2986 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002987 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002988 }
2989 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2990 /* This should be LOGL_ERROR or NOTICE, but
2991 * unfortuantely it happens for a couple of frames at
2992 * the beginning of every RTP connection */
2993 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002994 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002995 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002996 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002997 switch (bts->type) {
2998 case GSM_BTS_TYPE_NANOBTS:
Harald Welte04dc88f2010-12-22 21:45:05 +01002999 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01003000 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01003001 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01003002 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003003 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003004 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03003005 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02003006 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003007 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01003008 default:
Harald Weltee5215b52011-08-09 21:53:20 +02003009 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01003010 }
3011 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003012 }
3013
3014 memset(&rel, 0, sizeof(struct gsm_mncc));
3015 rel.callref = data->callref;
3016
3017 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02003018 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003019
3020 /* Callref unknown */
3021 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003022 struct gsm_subscriber *subscr;
3023
Harald Welte4a3464c2009-07-04 10:11:24 +02003024 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003025 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3026 "Received '%s' from MNCC with "
3027 "unknown callref %d\n", data->called.number,
3028 get_mncc_name(msg_type), data->callref);
3029 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003030 return mncc_release_ind(net, NULL, data->callref,
3031 GSM48_CAUSE_LOC_PRN_S_LU,
3032 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003033 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02003034 if (!data->called.number[0] && !data->imsi[0]) {
3035 DEBUGP(DCC, "(bts - trx - ts - ti) "
3036 "Received '%s' from MNCC with "
3037 "no number or IMSI\n", get_mncc_name(msg_type));
3038 /* Invalid number */
3039 return mncc_release_ind(net, NULL, data->callref,
3040 GSM48_CAUSE_LOC_PRN_S_LU,
3041 GSM48_CC_CAUSE_INV_NR_FORMAT);
3042 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003043 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02003044 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02003045 subscr = subscr_get_by_extension(net,
3046 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02003047 else
Harald Welte9176bd42009-07-23 18:46:00 +02003048 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 /* If subscriber is not found */
3050 if (!subscr) {
3051 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3052 "Received '%s' from MNCC with "
3053 "unknown subscriber %s\n", data->called.number,
3054 get_mncc_name(msg_type), data->called.number);
3055 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003056 return mncc_release_ind(net, NULL, data->callref,
3057 GSM48_CAUSE_LOC_PRN_S_LU,
3058 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 }
3060 /* If subscriber is not "attached" */
3061 if (!subscr->lac) {
3062 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3063 "Received '%s' from MNCC with "
3064 "detached subscriber %s\n", data->called.number,
3065 get_mncc_name(msg_type), data->called.number);
3066 subscr_put(subscr);
3067 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003068 return mncc_release_ind(net, NULL, data->callref,
3069 GSM48_CAUSE_LOC_PRN_S_LU,
3070 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071 }
3072 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003073 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3074 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075 DEBUGP(DCC, "No memory for trans.\n");
3076 subscr_put(subscr);
3077 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003078 mncc_release_ind(net, NULL, data->callref,
3079 GSM48_CAUSE_LOC_PRN_S_LU,
3080 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003081 return -ENOMEM;
3082 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003084 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003085
Harald Welte4bfdfe72009-06-10 23:11:52 +08003086 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003087 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003088 /* find transaction with this subscriber already paging */
3089 llist_for_each_entry(transt, &net->trans_list, entry) {
3090 /* Transaction of our lchan? */
3091 if (transt == trans ||
3092 transt->subscr != subscr)
3093 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003094 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003095 "Received '%s' from MNCC with "
3096 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003097 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003098 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003099 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003100 subscr_put(subscr);
3101 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003102 return 0;
3103 }
3104 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003105 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003106
3107 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003108 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3109 if (!trans->paging_request) {
3110 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3111 subscr_put(subscr);
3112 trans_free(trans);
3113 return 0;
3114 }
3115
3116 *trans->paging_request = subscr->net;
3117 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003118
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003119 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003120 return 0;
3121 }
3122 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003123 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003124 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003125 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003126
3127 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003128 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003129
3130 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003131 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003132 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003133 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003134 (trans->subscr)?(trans->subscr->extension):"-",
3135 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003136 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3137 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003138 if (msg_type == MNCC_REL_REQ)
3139 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3140 else
3141 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3142 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003143 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003144 return rc;
3145 }
3146
3147 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3148 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003149 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003150 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003151 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003152 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003153 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003154
3155 /* Find function for current state and message */
3156 for (i = 0; i < DOWNSLLEN; i++)
3157 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003158 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003159 break;
3160 if (i == DOWNSLLEN) {
3161 DEBUGP(DCC, "Message unhandled at this state.\n");
3162 return 0;
3163 }
3164
3165 rc = downstatelist[i].rout(trans, arg);
3166
3167 return rc;
3168}
3169
3170
3171static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003172 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003173 int type;
3174 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3175} datastatelist[] = {
3176 /* mobile originating call establishment */
3177 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3178 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3179 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3180 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3181 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3182 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3183 /* mobile terminating call establishment */
3184 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3185 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3186 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3187 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003188 {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 +08003189 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3190 /* signalling during call */
3191 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3192 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3193 {SBIT(GSM_CSTATE_ACTIVE),
3194 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3195 {ALL_STATES,
3196 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3197 {ALL_STATES,
3198 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3199 {ALL_STATES,
3200 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3201 {SBIT(GSM_CSTATE_ACTIVE),
3202 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3203 {SBIT(GSM_CSTATE_ACTIVE),
3204 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3205 {SBIT(GSM_CSTATE_ACTIVE),
3206 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3207 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3208 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3209 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3210 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3211 {SBIT(GSM_CSTATE_ACTIVE),
3212 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3213 /* clearing */
3214 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3215 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3216 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3217 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3218 {ALL_STATES, /* 5.4.3.4 */
3219 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3220};
3221
3222#define DATASLLEN \
3223 (sizeof(datastatelist) / sizeof(struct datastate))
3224
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003225static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003226{
3227 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003228 uint8_t msg_type = gh->msg_type & 0xbf;
3229 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003230 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003231 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003232
Harald Welte4bfdfe72009-06-10 23:11:52 +08003233 if (msg_type & 0x80) {
3234 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3235 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003236 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003237
Harald Welte4bfdfe72009-06-10 23:11:52 +08003238 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003239 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003240
Harald Welte6f5aee02009-07-23 21:21:14 +02003241 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003242 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003243 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003244 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003245 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3246 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003247
3248 /* Create transaction */
3249 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003250 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003251 "creating new trans.\n", transaction_id);
3252 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003253 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003254 transaction_id, new_callref++);
3255 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003256 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003257 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003258 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003259 GSM48_MT_CC_RELEASE_COMPL);
3260 return -ENOMEM;
3261 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003262 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003263 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003264 }
3265
3266 /* find function for current state and message */
3267 for (i = 0; i < DATASLLEN; i++)
3268 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003269 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003270 break;
3271 if (i == DATASLLEN) {
3272 DEBUGP(DCC, "Message unhandled at this state.\n");
3273 return 0;
3274 }
3275
3276 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003277
3278 return rc;
3279}
3280
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003281/* Create a dummy to wait five seconds */
3282static void release_anchor(struct gsm_subscriber_connection *conn)
3283{
3284 if (!conn->anch_operation)
3285 return;
3286
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003287 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003288 talloc_free(conn->anch_operation);
3289 conn->anch_operation = NULL;
3290}
3291
3292static void anchor_timeout(void *_data)
3293{
3294 struct gsm_subscriber_connection *con = _data;
3295
3296 release_anchor(con);
3297 msc_release_connection(con);
3298}
3299
3300int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3301{
3302 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3303 if (!conn->anch_operation)
3304 return -1;
3305
3306 conn->anch_operation->timeout.data = conn;
3307 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003308 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003309 return 0;
3310}
3311
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003312/* here we get data from the BSC level... */
3313int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003314{
3315 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003316 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003317 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003318
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003319 if (silent_call_reroute(conn, msg))
3320 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003321
3322 switch (pdisc) {
3323 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003324 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003325 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003326 break;
3327 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003328 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003329 break;
3330 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003331 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003332 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003333 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003334 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003335 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003336 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003337 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003338 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003339 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3340 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003341 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003342 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003343 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003344 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003345 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003346 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003347 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3348 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003349 break;
3350 }
3351
3352 return rc;
3353}
Harald Welte8470bf22008-12-25 23:28:35 +00003354
Harald Welte805f6442009-07-28 18:25:29 +02003355/*
3356 * This will be ran by the linker when loading the DSO. We use it to
3357 * do system initialization, e.g. registration of signal handlers.
3358 */
3359static __attribute__((constructor)) void on_dso_load_0408(void)
3360{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003361 osmo_signal_register_handler(SS_HO, handle_ho_signal, NULL);
3362 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003363}