blob: 9063f981556c5b82453c7c115c71c8780a774946 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01005 * (C) 2008-2012 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
Harald Welte9af6ddf2011-01-01 15:25:50 +010010 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
Harald Welte52b1f982008-12-23 20:25:15 +000012 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Harald Welte9af6ddf2011-01-01 15:25:50 +010017 * GNU Affero General Public License for more details.
Harald Welte52b1f982008-12-23 20:25:15 +000018 *
Harald Welte9af6ddf2011-01-01 15:25:50 +010019 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Harald Welte52b1f982008-12-23 20:25:15 +000021 *
22 */
23
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000029#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000030#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte9c3dc902012-04-08 16:59:24 +020032#include "bscconfig.h"
33
Sylvain Munaut30a15382009-12-24 00:27:26 +010034#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000035#include <openbsc/db.h>
Harald Welte8470bf22008-12-25 23:28:35 +000036#include <openbsc/debug.h>
37#include <openbsc/gsm_data.h>
38#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000039#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000040#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +080041#include <openbsc/gsm_04_80.h>
Harald Welte8470bf22008-12-25 23:28:35 +000042#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000043#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000044#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000045#include <openbsc/signal.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020046#include <osmocom/abis/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000047#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020048#include <openbsc/rtp_proxy.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020049#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020050#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010051#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080052#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080053#include <openbsc/osmo_msc.h>
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +010054#include <openbsc/handover.h>
Pablo Neira Ayusoed5cacb2011-08-17 22:44:07 +020055#include <osmocom/abis/e1_input.h>
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010056#include <osmocom/core/bitvec.h>
Holger Hans Peter Freyther841c2002010-09-30 18:52:23 +080057
Pablo Neira Ayuso136f4532011-03-22 16:47:59 +010058#include <osmocom/gsm/gsm48.h>
59#include <osmocom/gsm/gsm0480.h>
60#include <osmocom/gsm/gsm_utils.h>
61#include <osmocom/core/msgb.h>
62#include <osmocom/core/talloc.h>
63#include <osmocom/gsm/tlv.h>
Harald Welte52b1f982008-12-23 20:25:15 +000064
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +020065#include <assert.h>
66
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020067void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010068void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020069
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020070int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080071static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020072 uint8_t pdisc, uint8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010073static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +080074static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000075
Harald Welte52b1f982008-12-23 20:25:15 +000076struct gsm_lai {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020077 uint16_t mcc;
78 uint16_t mnc;
79 uint16_t lac;
Harald Welte52b1f982008-12-23 20:25:15 +000080};
81
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +020082static uint32_t new_callref = 0x80000001;
Harald Welte4bfdfe72009-06-10 23:11:52 +080083
Harald Welte31c00f72011-03-03 23:29:05 +010084void cc_tx_to_mncc(struct gsm_network *net, struct msgb *msg)
85{
86 net->mncc_recv(net, msg);
87}
88
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080089static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
90 struct gsm_trans *trans)
91{
92 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
93
94 /* if we get passed a transaction reference, do some common
95 * work that the caller no longer has to do */
96 if (trans) {
97 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
98 msg->lchan = trans->conn->lchan;
99 }
100
101
102 if (msg->lchan) {
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200103 struct e1inp_sign_link *sign_link =
104 msg->lchan->ts->trx->rsl_link;
105
106 msg->dst = sign_link;
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800107 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
108 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200109 "Sending '%s' to MS.\n",
110 sign_link->trx->bts->nr,
111 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800112 gh->proto_discr & 0xf0,
113 gsm48_cc_msg_name(gh->msg_type));
114 else
115 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
Pablo Neira Ayuso7abecfc2011-08-17 22:43:54 +0200116 "Sending 0x%02x to MS.\n",
117 sign_link->trx->bts->nr,
118 sign_link->trx->nr, msg->lchan->ts->nr,
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800119 gh->proto_discr, gh->msg_type);
120 }
121
Holger Hans Peter Freyther8d380dc2010-11-10 10:16:02 +0100122 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800123}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100124
Holger Hans Peter Freythere0009f12010-08-12 01:41:57 +0800125int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
126{
127 struct gsm48_hdr *gh;
128 struct msgb *ss_notify;
129
130 ss_notify = gsm0480_create_notifySS(message);
131 if (!ss_notify)
132 return -1;
133
134 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
135 uint8_t *data = msgb_push(ss_notify, 1);
136 data[0] = ss_notify->len - 1;
137 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
138 gh->msg_type = GSM48_MT_CC_FACILITY;
139 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
140}
141
Harald Weltecf149ee2012-01-23 16:40:24 +0100142void release_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100143{
144 if (!conn->sec_operation)
145 return;
146
147 talloc_free(conn->sec_operation);
148 conn->sec_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800149 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100150}
151
Harald Weltecf149ee2012-01-23 16:40:24 +0100152void allocate_security_operation(struct gsm_subscriber_connection *conn)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100153{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100154 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
155 struct gsm_security_operation);
156}
157
Sylvain Munaut0fbfd1b2010-12-01 22:37:05 +0100158int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
159 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100160{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800161 struct gsm_network *net = conn->bts->network;
162 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100163 struct gsm_security_operation *op;
164 struct gsm_auth_tuple atuple;
165 int status = -1, rc;
166
167 /* Check if we _can_ enable encryption. Cases where we can't:
168 * - Encryption disabled in config
169 * - Channel already secured (nothing to do)
170 * - Subscriber equipment doesn't support configured encryption
171 */
172 if (!net->a5_encryption) {
173 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800174 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100175 DEBUGP(DMM, "Requesting to secure an already secure channel");
Andreas Eversberg641475c2013-07-10 08:58:03 +0200176 status = GSM_SECURITY_ALREADY;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100177 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
178 net->a5_encryption)) {
179 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
180 status = GSM_SECURITY_NOAVAIL;
181 }
182
183 /* If not done yet, try to get info for this user */
184 if (status < 0) {
185 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
186 if (rc <= 0)
187 status = GSM_SECURITY_NOAVAIL;
188 }
189
190 /* Are we done yet ? */
191 if (status >= 0)
192 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800193 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100194 0;
195
196 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800197 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100198 return -EBUSY;
199
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800200 allocate_security_operation(conn);
201 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100202 op->cb = cb;
203 op->cb_data = cb_data;
204 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
205
206 /* FIXME: Should start a timer for completion ... */
207
208 /* Then do whatever is needed ... */
Harald Welte86dda082010-12-23 18:07:49 +0100209 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100210 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800211 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte86dda082010-12-23 18:07:49 +0100212 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100213 /* Start ciphering directly */
Sylvain Munaut67706df2010-11-29 08:19:04 +0100214 return gsm0808_cipher_mode(conn, net->a5_encryption,
215 op->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100216 }
217
218 return -EINVAL; /* not reached */
219}
220
Holger Freyther73487a22008-12-31 18:53:57 +0000221static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
222 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000223{
224 if (!subscriber)
225 return 0;
226
Holger Freyther73487a22008-12-31 18:53:57 +0000227 /*
228 * Do not send accept yet as more information should arrive. Some
229 * phones will not send us the information and we will have to check
230 * what we want to do with that.
231 */
232 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
233 return 0;
234
Jan Luebbe06513f22009-08-12 12:48:00 +0200235 switch (subscriber->net->auth_policy) {
236 case GSM_AUTH_POLICY_CLOSED:
237 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200238 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200239 if (subscriber->authorized)
240 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200241 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200242 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000243 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200244 default:
245 return 0;
246 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000247}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000248
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100249static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000250{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100251 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000252 return;
253
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800254 /* No need to keep the connection up */
255 release_anchor(conn);
256
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200257 osmo_timer_del(&conn->loc_operation->updating_timer);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100258 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherf93e8fa2010-12-22 08:53:28 +0100259 conn->loc_operation = NULL;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800260 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000261}
262
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100263static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000264{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800265 if (conn->loc_operation)
266 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100267 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000268
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100269 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200270 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000271}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000272
Sylvain Munaut267fba02009-12-24 00:28:01 +0100273static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
274 struct msgb *msg, void *data, void *param)
275{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800276 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100277 int rc = 0;
278
279 switch (event) {
280 case GSM_SECURITY_AUTH_FAILED:
281 release_loc_updating_req(conn);
282 break;
283
Andreas Eversberga874b8d2013-07-10 08:58:03 +0200284 case GSM_SECURITY_ALREADY:
285 LOGP(DMM, LOGL_ERROR, "We don't expect LOCATION "
286 "UPDATING after CM SERVICE REQUEST\n");
287 /* fall through */
288
Sylvain Munaut267fba02009-12-24 00:28:01 +0100289 case GSM_SECURITY_NOAVAIL:
290 case GSM_SECURITY_SUCCEEDED:
291 /* We're all good */
292 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800293 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800294 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100295 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800296 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100297 }
298
299 /* call subscr_update after putting the loc_upd_acc
300 * in the transmit queue, since S_SUBSCR_ATTACHED might
301 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800302 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100303 GSM_SUBSCRIBER_UPDATE_ATTACHED);
304
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +0100305 /*
306 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
307 * MS needs to respond with a TMSI REALLOCATION COMPLETE
308 * (even if the TMSI is the same).
309 */
Sylvain Munaut267fba02009-12-24 00:28:01 +0100310 break;
311
312 default:
313 rc = -EINVAL;
314 };
315
316 return rc;
317}
318
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100319static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000320{
Holger Hans Peter Freyther1a345ca2013-12-27 17:37:27 +0100321 if (!conn->loc_operation)
322 return 0;
323
Sylvain Munaut267fba02009-12-24 00:28:01 +0100324 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800325 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100326 conn->loc_operation->key_seq,
327 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000328 return 0;
329}
330
Holger Hans Peter Freytheradb6e1c2010-09-18 06:44:24 +0800331void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000332{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800333 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800334
335 /* avoid someone issuing a clear */
336 conn->in_release = 1;
337
Holger Freyther7c19f742009-06-06 13:54:35 +0000338 /*
339 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800340 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000341 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800342 release_loc_updating_req(conn);
Holger Hans Peter Freytherc29043e2010-12-28 14:57:20 +0100343
344 /* We might need to cancel the paging response or such. */
345 if (conn->sec_operation && conn->sec_operation->cb) {
346 conn->sec_operation->cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
347 NULL, conn, conn->sec_operation->cb_data);
348 }
349
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800350 release_security_operation(conn);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +0800351 release_anchor(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000352
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100353 /*
354 * Free all transactions that are associated with the released
355 * connection. The transaction code will inform the CC or SMS
356 * facilities that will send the release indications. As part of
357 * the CC REL_IND the remote leg might be released and this will
358 * trigger the call to trans_free. This is something the llist
359 * macro can not handle and we will need to re-iterate the list.
360 *
361 * TODO: Move the trans_list into the subscriber connection and
362 * create a pending list for MT transactions. These exist before
363 * we have a subscriber connection.
364 */
365restart:
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800366 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100367 if (trans->conn == conn) {
Harald Weltedcaf5652009-07-23 18:56:43 +0200368 trans_free(trans);
Holger Hans Peter Freyther3e9b2ec2012-12-22 18:45:27 +0100369 goto restart;
370 }
Harald Welte4bfdfe72009-06-10 23:11:52 +0800371 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000372}
373
Harald Welte371efe52010-12-22 23:17:50 +0100374void gsm0408_clear_all_trans(struct gsm_network *net, int protocol)
375{
376 struct gsm_trans *trans, *temp;
377
378 LOGP(DCC, LOGL_NOTICE, "Clearing all currently active transactions!!!\n");
379
380 llist_for_each_entry_safe(trans, temp, &net->trans_list, entry) {
Harald Welteeb76c7a2010-12-23 02:47:53 +0100381 if (trans->protocol == protocol) {
382 trans->callref = 0;
Harald Welte371efe52010-12-22 23:17:50 +0100383 trans_free(trans);
Harald Welteeb76c7a2010-12-23 02:47:53 +0100384 }
Harald Welte371efe52010-12-22 23:17:50 +0100385 }
386}
387
Holger Freyther429e7762008-12-30 13:28:30 +0000388/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200389int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, uint8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000390{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800391 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800392 struct msgb *msg;
393
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200394 osmo_counter_inc(bts->network->stats.loc_upd_resp.reject);
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800395
396 msg = gsm48_create_loc_upd_rej(cause);
397 if (!msg) {
398 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
399 return -1;
400 }
Harald Welte52b1f982008-12-23 20:25:15 +0000401
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800402 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000403
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100404 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100405 "LAC=%u BTS=%u\n", conn->subscr ?
406 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800407 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100408
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800409 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000410}
411
412/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200413int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, uint32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000414{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800415 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000416 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000417 struct gsm48_hdr *gh;
418 struct gsm48_loc_area_id *lai;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200419 uint8_t *mid;
Harald Welte52b1f982008-12-23 20:25:15 +0000420
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800421 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000422
423 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
424 gh->proto_discr = GSM48_PDISC_MM;
425 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
426
427 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100428 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000429 bts->network->network_code, bts->location_area_code);
430
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200431 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200432 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000433
434 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
435
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200436 osmo_counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100437
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800438 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000439}
440
Harald Weltebf5e8df2009-02-03 12:59:45 +0000441/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200442static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, uint8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000443{
444 struct msgb *msg = gsm48_msgb_alloc();
445 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000446
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800447 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000448
449 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
450 gh->proto_discr = GSM48_PDISC_MM;
451 gh->msg_type = GSM48_MT_MM_ID_REQ;
452 gh->data[0] = id_type;
453
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800454 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000455}
456
Harald Welte231ad4f2008-12-27 11:15:38 +0000457
Harald Weltebf5e8df2009-02-03 12:59:45 +0000458/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800459static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000460{
461 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000462 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200463 struct gsm_bts *bts = lchan->ts->trx->bts;
464 struct gsm_network *net = bts->network;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200465 uint8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200466 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000467
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200468 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000469 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000470 mi_type, mi_string);
471
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200472 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
Harald Welte7659de12009-12-13 12:39:18 +0100473
Harald Welte75a983f2008-12-27 21:34:06 +0000474 switch (mi_type) {
475 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200476 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100477 if (!conn->subscr) {
478 conn->subscr = subscr_get_by_imsi(net, mi_string);
479 if (!conn->subscr)
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200480 conn->subscr = subscr_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200481 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100482 if (conn->loc_operation)
483 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000484 break;
485 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000486 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000487 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100488 if (conn->subscr) {
489 db_subscriber_assoc_imei(conn->subscr, mi_string);
490 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200491 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100492 if (conn->loc_operation)
493 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000494 break;
495 }
Holger Freyther73487a22008-12-31 18:53:57 +0000496
497 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100498 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000499}
500
Harald Welte255539c2008-12-28 02:26:27 +0000501
502static void loc_upd_rej_cb(void *data)
503{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100504 struct gsm_subscriber_connection *conn = data;
505 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100506 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000507
Holger Hans Peter Freyther2692e3b2011-11-07 12:26:29 +0100508 LOGP(DMM, LOGL_DEBUG, "Location Updating Request procedure timedout.\n");
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800509 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800510 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000511}
512
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100513static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000514{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100515 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
516 conn->loc_operation->updating_timer.data = conn;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +0200517 osmo_timer_schedule(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000518}
519
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200520static const char *lupd_name(uint8_t type)
Harald Welte2a139372009-02-22 21:14:55 +0000521{
522 switch (type) {
523 case GSM48_LUPD_NORMAL:
524 return "NORMAL";
525 case GSM48_LUPD_PERIODIC:
Alexander Chemerisa3d41c92013-10-04 02:42:26 +0200526 return "PERIODIC";
Harald Welte2a139372009-02-22 21:14:55 +0000527 case GSM48_LUPD_IMSI_ATT:
528 return "IMSI ATTACH";
529 default:
530 return "UNKNOWN";
531 }
532}
533
Harald Weltebf5e8df2009-02-03 12:59:45 +0000534/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800535static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000536{
Harald Welte8470bf22008-12-25 23:28:35 +0000537 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000538 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800539 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800540 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200541 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200542 char mi_string[GSM48_MI_SIZE];
Harald Welte52b1f982008-12-23 20:25:15 +0000543
Harald Welte8470bf22008-12-25 23:28:35 +0000544 lu = (struct gsm48_loc_upd_req *) gh->data;
545
546 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000547
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200548 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000549
Harald Weltea0368542009-06-27 02:58:43 +0200550 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000551 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000552
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200553 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
Harald Welte7659de12009-12-13 12:39:18 +0100554
Harald Welte24ff6ee2009-12-22 00:41:05 +0100555 switch (lu->type) {
556 case GSM48_LUPD_NORMAL:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200557 osmo_counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100558 break;
559 case GSM48_LUPD_IMSI_ATT:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200560 osmo_counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100561 break;
562 case GSM48_LUPD_PERIODIC:
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200563 osmo_counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100564 break;
565 }
566
Holger Freythereaf04692009-06-06 13:54:44 +0000567 /*
568 * Pseudo Spoof detection: Just drop a second/concurrent
569 * location updating request.
570 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100571 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200572 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100573 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800574 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000575 return 0;
576 }
577
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800578 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000579
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200580 conn->loc_operation->key_seq = lu->key_seq;
581
Harald Welte52b1f982008-12-23 20:25:15 +0000582 switch (mi_type) {
583 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200584 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000585 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200586 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100587 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000588
Jan Luebbe370b41d2009-08-12 10:19:34 +0200589 /* look up subscriber based on IMSI, create if not found */
590 subscr = subscr_get_by_imsi(bts->network, mi_string);
591 if (!subscr) {
Holger Hans Peter Freyther7634ec12013-10-04 08:35:11 +0200592 subscr = subscr_create_subscriber(bts->network, mi_string);
Jan Luebbe370b41d2009-08-12 10:19:34 +0200593 }
Harald Welte4b634542008-12-27 01:55:51 +0000594 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000595 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200596 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000597 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200598 subscr = subscr_get_by_tmsi(bts->network,
599 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000600 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000601 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200602 mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100603 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000604 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200605 /* we always want the IMEI, too */
Holger Hans Peter Freythera5050b12012-09-11 11:55:03 +0200606 mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200607 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000608 break;
609 case GSM_MI_TYPE_IMEI:
610 case GSM_MI_TYPE_IMEISV:
611 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200612 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000613 break;
614 default:
Harald Weltea0368542009-06-27 02:58:43 +0200615 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000616 break;
617 }
618
Harald Welte24516ea2009-07-04 10:18:00 +0200619 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100620 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200621
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200623 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800624 /* FIXME: request id? close channel? */
625 return -EINVAL;
626 }
627
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100628 conn->subscr = subscr;
629 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000630
Harald Welte24516ea2009-07-04 10:18:00 +0200631 /* check if we can let the subscriber into our network immediately
632 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100633 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000634}
635
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100636/* Turn int into semi-octet representation: 98 => 0x89 */
637static uint8_t bcdify(uint8_t value)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800638{
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100639 uint8_t ret;
640
641 ret = value / 10;
642 ret |= (value % 10) << 4;
643
644 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800645}
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100646
Harald Welte4bfdfe72009-06-10 23:11:52 +0800647
Harald Weltedb253af2008-12-30 17:56:55 +0000648/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800649int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000650{
651 struct msgb *msg = gsm48_msgb_alloc();
652 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800653 struct gsm_network *net = conn->bts->network;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100654 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200655 uint8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200656 int name_len, name_pad;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100657
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 time_t cur_t;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100659 struct tm* gmt_time;
660 struct tm* local_time;
661 int tzunits;
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100662 int dst = 0;
Harald Weltedb253af2008-12-30 17:56:55 +0000663
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800664 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000665
666 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
667 gh->proto_discr = GSM48_PDISC_MM;
668 gh->msg_type = GSM48_MT_MM_INFO;
669
670 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200671#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000672 name_len = strlen(net->name_long);
673 /* 10.5.3.5a */
674 ptr8 = msgb_put(msg, 3);
675 ptr8[0] = GSM48_IE_NAME_LONG;
676 ptr8[1] = name_len*2 +1;
677 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
678
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200679 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000680 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000681 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000682
683 /* FIXME: Use Cell Broadcast, not UCS-2, since
684 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200685#endif
686 name_len = (strlen(net->name_long)*7)/8;
687 name_pad = (8 - strlen(net->name_long)*7)%8;
688 if (name_pad > 0)
689 name_len++;
690 /* 10.5.3.5a */
691 ptr8 = msgb_put(msg, 3);
692 ptr8[0] = GSM48_IE_NAME_LONG;
693 ptr8[1] = name_len +1;
694 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
695
696 ptr8 = msgb_put(msg, name_len);
697 gsm_7bit_encode(ptr8, net->name_long);
698
Harald Weltedb253af2008-12-30 17:56:55 +0000699 }
700
701 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200702#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000703 name_len = strlen(net->name_short);
704 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200705 ptr8 = (uint8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200706 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000707 ptr8[1] = name_len*2 + 1;
708 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
709
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200710 ptr16 = (uint16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000711 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000712 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200713#endif
714 name_len = (strlen(net->name_short)*7)/8;
715 name_pad = (8 - strlen(net->name_short)*7)%8;
716 if (name_pad > 0)
717 name_len++;
718 /* 10.5.3.5a */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200719 ptr8 = (uint8_t *) msgb_put(msg, 3);
Daniel Willmanneea93372009-08-13 03:42:07 +0200720 ptr8[0] = GSM48_IE_NAME_SHORT;
721 ptr8[1] = name_len +1;
722 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
723
724 ptr8 = msgb_put(msg, name_len);
725 gsm_7bit_encode(ptr8, net->name_short);
726
Harald Weltedb253af2008-12-30 17:56:55 +0000727 }
728
Harald Weltedb253af2008-12-30 17:56:55 +0000729 /* Section 10.5.3.9 */
730 cur_t = time(NULL);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100731 gmt_time = gmtime(&cur_t);
732
Harald Weltedb253af2008-12-30 17:56:55 +0000733 ptr8 = msgb_put(msg, 8);
734 ptr8[0] = GSM48_IE_NET_TIME_TZ;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100735 ptr8[1] = bcdify(gmt_time->tm_year % 100);
736 ptr8[2] = bcdify(gmt_time->tm_mon + 1);
737 ptr8[3] = bcdify(gmt_time->tm_mday);
738 ptr8[4] = bcdify(gmt_time->tm_hour);
739 ptr8[5] = bcdify(gmt_time->tm_min);
740 ptr8[6] = bcdify(gmt_time->tm_sec);
741
Harald Welte45f91712012-07-08 16:48:11 +0200742 if (bts->tz.override) {
743 /* Convert tz.hr and tz.mn to units */
744 if (bts->tz.hr < 0) {
745 tzunits = ((bts->tz.hr/-1)*4);
746 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100747 ptr8[7] = bcdify(tzunits);
748 /* Set negative time */
749 ptr8[7] |= 0x08;
750 }
751 else {
Harald Welte45f91712012-07-08 16:48:11 +0200752 tzunits = bts->tz.hr*4;
753 tzunits = tzunits + (bts->tz.mn/15);
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100754 ptr8[7] = bcdify(tzunits);
755 }
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100756 /* Convert DST value */
757 if (bts->tz.dst >= 0 && bts->tz.dst <= 2)
758 dst = bts->tz.dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100759 }
760 else {
761 /* Need to get GSM offset and convert into 15 min units */
762 /* This probably breaks if gmtoff returns a value not evenly divisible by 15? */
763 local_time = localtime(&cur_t);
Harald Welte9c3dc902012-04-08 16:59:24 +0200764#ifdef HAVE_TM_GMTOFF_IN_TM
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100765 tzunits = (local_time->tm_gmtoff/60)/15;
Harald Welte9c3dc902012-04-08 16:59:24 +0200766#else
767#warning find a portable way to obtain the timezone offset
768 tzunits = 0;
769#endif
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100770 if (tzunits < 0) {
771 tzunits = tzunits/-1;
772 ptr8[7] = bcdify(tzunits);
773 /* Flip it to negative */
774 ptr8[7] |= 0x08;
775 }
776 else
777 ptr8[7] = bcdify(tzunits);
Jacob Erlbeckf46e2262014-01-07 15:05:16 +0100778
779 /* Does not support DST +2 */
780 if (local_time->tm_isdst)
781 dst = 1;
782 }
783
784 if (dst) {
785 ptr8 = msgb_put(msg, 3);
786 ptr8[0] = GSM48_IE_NET_DST;
787 ptr8[1] = 1;
788 ptr8[2] = dst;
Gus Bourg1c5dd2c2011-12-02 10:18:17 +0100789 }
Harald Weltedb253af2008-12-30 17:56:55 +0000790
Daniel Willmanneea93372009-08-13 03:42:07 +0200791 DEBUGP(DMM, "-> MM INFO\n");
792
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800793 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000794}
795
Harald Welte7984d5c2009-08-12 22:56:50 +0200796/* Section 9.2.2 */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200797int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, uint8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200798{
799 struct msgb *msg = gsm48_msgb_alloc();
800 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200801 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200802
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +0200803 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", osmo_hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200804
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800805 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200806 gh->proto_discr = GSM48_PDISC_MM;
807 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
808
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100809 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200810
Harald Welte7984d5c2009-08-12 22:56:50 +0200811 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200812 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200813 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200814
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800815 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200816}
817
818/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800819int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200820{
821 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800822 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200823}
824
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100825/*
826 * At the 30C3 phones miss their periodic update
827 * interval a lot and then remain unreachable. In case
828 * we still know the TMSI we can just attach it again.
829 */
830static void implit_attach(struct gsm_subscriber_connection *conn)
831{
832 if (conn->subscr->lac != GSM_LAC_RESERVED_DETACHED)
833 return;
834
835 subscr_update(conn->subscr, conn->bts,
836 GSM_SUBSCRIBER_UPDATE_ATTACHED);
837}
838
839
Sylvain Munautba87f452009-12-24 00:28:46 +0100840static int _gsm48_rx_mm_serv_req_sec_cb(
841 unsigned int hooknum, unsigned int event,
842 struct msgb *msg, void *data, void *param)
843{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800844 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100845 int rc = 0;
846
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200847 /* auth failed or succeeded, the timer was stopped */
848 conn->expire_timer_stopped = 1;
849
Sylvain Munautba87f452009-12-24 00:28:46 +0100850 switch (event) {
851 case GSM_SECURITY_AUTH_FAILED:
852 /* Nothing to do */
853 break;
854
855 case GSM_SECURITY_NOAVAIL:
Andreas Eversberg641475c2013-07-10 08:58:03 +0200856 case GSM_SECURITY_ALREADY:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800857 rc = gsm48_tx_mm_serv_ack(conn);
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100858 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100859 break;
860
861 case GSM_SECURITY_SUCCEEDED:
862 /* nothing to do. CIPHER MODE COMMAND is
863 * implicit CM SERV ACK */
Holger Hans Peter Freyther2147bc42013-12-27 17:19:19 +0100864 implit_attach(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100865 break;
866
867 default:
868 rc = -EINVAL;
869 };
870
871 return rc;
872}
873
Harald Welte4ed0e922009-01-10 03:17:30 +0000874/*
875 * Handle CM Service Requests
876 * a) Verify that the packet is long enough to contain the information
877 * we require otherwsie reject with INCORRECT_MESSAGE
878 * b) Try to parse the TMSI. If we do not have one reject
879 * c) Check that we know the subscriber with the TMSI otherwise reject
880 * with a HLR cause
881 * d) Set the subscriber on the gsm_lchan and accept
882 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800883static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000884{
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200885 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200886 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000887
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800888 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000889 struct gsm_subscriber *subscr;
890 struct gsm48_hdr *gh = msgb_l3(msg);
891 struct gsm48_service_request *req =
892 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800893 /* unfortunately in Phase1 the classmark2 length is variable */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200894 uint8_t classmark2_len = gh->data[1];
895 uint8_t *classmark2 = gh->data+2;
896 uint8_t mi_len = *(classmark2 + classmark2_len);
897 uint8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000898
Harald Weltec9e02182009-05-01 19:07:53 +0000899 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000900 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000901 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800902 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000903 GSM48_REJECT_INCORRECT_MESSAGE);
904 }
905
906 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000907 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800908 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000909 GSM48_REJECT_INCORRECT_MESSAGE);
910 }
911
Harald Weltec9e02182009-05-01 19:07:53 +0000912 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000913 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000914 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800915 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000916 GSM48_REJECT_INCORRECT_MESSAGE);
917 }
918
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200919 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000920 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000921 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000922
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +0200923 osmo_signal_dispatch(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
Harald Welte7659de12009-12-13 12:39:18 +0100924
Harald Welte3ac7f102009-08-10 10:12:45 +0200925 if (is_siemens_bts(bts))
926 send_siemens_mrpci(msg->lchan, classmark2-1);
927
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200928 subscr = subscr_get_by_tmsi(bts->network,
929 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000930
Harald Welte2a139372009-02-22 21:14:55 +0000931 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000932 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800933 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000934 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
935
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800936 if (!conn->subscr)
937 conn->subscr = subscr;
938 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100939 subscr_put(subscr); /* lchan already has a ref, don't need another one */
940 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000941 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
942 subscr_put(subscr);
943 }
944
Harald Weltec2e302d2009-07-05 14:08:13 +0200945 subscr->equipment.classmark2_len = classmark2_len;
946 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
947 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000948
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +0200949 /* we will send a MM message soon */
950 conn->expire_timer_stopped = 1;
951
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800952 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100953 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000954}
955
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200956static int gsm48_rx_mm_imsi_detach_ind(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2a139372009-02-22 21:14:55 +0000957{
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200958 struct gsm_bts *bts = conn->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000959 struct gsm48_hdr *gh = msgb_l3(msg);
960 struct gsm48_imsi_detach_ind *idi =
961 (struct gsm48_imsi_detach_ind *) gh->data;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +0200962 uint8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200963 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800964 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000965
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200966 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Daniel Willmann977cd132012-12-28 19:29:44 +0100967 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s)",
Harald Welte2a139372009-02-22 21:14:55 +0000968 mi_type, mi_string);
969
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +0200970 osmo_counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100971
Harald Welte2a139372009-02-22 21:14:55 +0000972 switch (mi_type) {
973 case GSM_MI_TYPE_TMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100974 DEBUGPC(DMM, "\n");
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200975 subscr = subscr_get_by_tmsi(bts->network,
976 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000977 break;
978 case GSM_MI_TYPE_IMSI:
Daniel Willmann977cd132012-12-28 19:29:44 +0100979 DEBUGPC(DMM, "\n");
Harald Welte9176bd42009-07-23 18:46:00 +0200980 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000981 break;
982 case GSM_MI_TYPE_IMEI:
983 case GSM_MI_TYPE_IMEISV:
984 /* no sim card... FIXME: what to do ? */
Daniel Willmann977cd132012-12-28 19:29:44 +0100985 DEBUGPC(DMM, ": unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000986 break;
987 default:
Daniel Willmann977cd132012-12-28 19:29:44 +0100988 DEBUGPC(DMM, ": unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000989 break;
990 }
991
Holger Freyther4a49e772009-04-12 05:37:29 +0000992 if (subscr) {
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +0200993 subscr_update(subscr, bts,
Holger Freyther4a49e772009-04-12 05:37:29 +0000994 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100995 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200996
997 subscr->equipment.classmark1 = idi->classmark1;
998 db_sync_equipment(&subscr->equipment);
999
Holger Freytherc21cfbc2009-06-02 02:54:57 +00001000 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +00001001 } else
Harald Welte2a139372009-02-22 21:14:55 +00001002 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
1003
Harald Welte31981a02009-12-20 09:58:40 +01001004 /* FIXME: iterate over all transactions and release them,
1005 * imagine an IMSI DETACH happening during an active call! */
1006
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001007 release_anchor(conn);
Harald Welte2a139372009-02-22 21:14:55 +00001008 return 0;
1009}
1010
Harald Welted2a7f5a2009-06-05 20:08:20 +00001011static int gsm48_rx_mm_status(struct msgb *msg)
1012{
1013 struct gsm48_hdr *gh = msgb_l3(msg);
1014
1015 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
1016
1017 return 0;
1018}
1019
Sylvain Munaut30a15382009-12-24 00:27:26 +01001020/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001021static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001022{
1023 struct gsm48_hdr *gh = msgb_l3(msg);
1024 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001025 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001026
Sylvain Munautc593cf12010-06-10 23:51:41 +02001027 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001028 osmo_hexdump(ar->sres, 4));
Sylvain Munaut30a15382009-12-24 00:27:26 +01001029
1030 /* Safety check */
1031 if (!conn->sec_operation) {
1032 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
1033 return -EIO;
1034 }
1035
1036 /* Validate SRES */
1037 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001038 int rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001039 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +02001040
1041 DEBUGPC(DMM, "Invalid (expected %s)\n",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001042 osmo_hexdump(conn->sec_operation->atuple.sres, 4));
Sylvain Munautc593cf12010-06-10 23:51:41 +02001043
Sylvain Munaut30a15382009-12-24 00:27:26 +01001044 if (cb)
1045 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001046 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001047
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001048 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001049 release_security_operation(conn);
Holger Hans Peter Freyther59f787a2010-12-27 10:57:56 +01001050 return rc;
Sylvain Munaut30a15382009-12-24 00:27:26 +01001051 }
1052
Sylvain Munautc593cf12010-06-10 23:51:41 +02001053 DEBUGPC(DMM, "OK\n");
1054
Sylvain Munaut30a15382009-12-24 00:27:26 +01001055 /* Start ciphering */
Sylvain Munaut67706df2010-11-29 08:19:04 +01001056 return gsm0808_cipher_mode(conn, net->a5_encryption,
1057 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001058}
1059
Harald Weltebf5e8df2009-02-03 12:59:45 +00001060/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001061static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001062{
1063 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001064 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001065
1066 switch (gh->msg_type & 0xbf) {
1067 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +02001068 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001069 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001070 break;
1071 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001072 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001073 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001074 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001075 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +00001076 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001077 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +00001078 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +00001079 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001080 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +00001081 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001082 conn->subscr ?
1083 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +00001084 "unknown subscriber");
Holger Hans Peter Freytherd4e68722010-12-22 12:11:01 +01001085 release_loc_updating_req(conn);
Harald Welte69b2af22009-01-06 19:47:00 +00001086 break;
Harald Welte231ad4f2008-12-27 11:15:38 +00001087 case GSM48_MT_MM_IMSI_DETACH_IND:
Holger Hans Peter Freyther153b13b2012-07-10 08:53:27 +02001088 rc = gsm48_rx_mm_imsi_detach_ind(conn, msg);
Harald Welte2a139372009-02-22 21:14:55 +00001089 break;
1090 case GSM48_MT_MM_CM_REEST_REQ:
1091 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
1092 break;
1093 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001094 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001095 break;
1096 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001097 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +00001098 gh->msg_type);
1099 break;
1100 }
1101
1102 return rc;
1103}
Harald Weltebf5e8df2009-02-03 12:59:45 +00001104
Harald Welte2d35ae62009-02-06 12:02:13 +00001105/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001106static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +00001107{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001108 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +00001109 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001110 struct gsm48_pag_resp *resp;
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001111 uint8_t *classmark2_lv = gh->data + 1;
1112 uint8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +02001113 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +08001114 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +00001115 int rc = 0;
1116
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001117 resp = (struct gsm48_pag_resp *) &gh->data[0];
1118 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1119 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001120 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1121 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001122
Harald Weltefe18d8f2009-02-22 21:14:24 +00001123 switch (mi_type) {
1124 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001125 subscr = subscr_get_by_tmsi(bts->network,
1126 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001127 break;
1128 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001129 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001130 break;
1131 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001132
1133 if (!subscr) {
1134 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001135 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001136 return -EINVAL;
1137 }
1138 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001139 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001140
Harald Weltec2e302d2009-07-05 14:08:13 +02001141 subscr->equipment.classmark2_len = *classmark2_lv;
1142 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1143 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001144
Holger Hans Peter Freythere7bd8632013-06-30 15:30:47 +02001145 /* We received a paging */
1146 conn->expire_timer_stopped = 1;
1147
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001148 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001149 return rc;
1150}
1151
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001152static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001153{
1154 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001155 uint8_t apdu_id_flags;
1156 uint8_t apdu_len;
1157 uint8_t *apdu_data;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001158
1159 apdu_id_flags = gh->data[0];
1160 apdu_len = gh->data[1];
1161 apdu_data = gh->data+2;
1162
Harald Welte70f92052012-07-16 13:22:19 +02001163 DEBUGP(DRR, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
Pablo Neira Ayusoc0d17f22011-05-07 12:12:48 +02001164 apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001165
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001166 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001167}
1168
Harald Weltebf5e8df2009-02-03 12:59:45 +00001169/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001170static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001171{
1172 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001173 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001174
1175 switch (gh->msg_type) {
Harald Welte52b1f982008-12-23 20:25:15 +00001176 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001177 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001178 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001179 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001180 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001181 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001182 default:
Harald Weltecf149ee2012-01-23 16:40:24 +01001183 LOGP(DRR, LOGL_NOTICE, "MSC: Unimplemented "
Harald Welte5d24ba12009-12-24 12:13:17 +01001184 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001185 break;
1186 }
1187
Harald Welte2d35ae62009-02-06 12:02:13 +00001188 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001189}
1190
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001191int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, uint8_t apdu_id,
1192 uint8_t apdu_len, const uint8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001193{
1194 struct msgb *msg = gsm48_msgb_alloc();
1195 struct gsm48_hdr *gh;
1196
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001197 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001198
1199 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1200 apdu_id, apdu_len);
1201
1202 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1203 gh->proto_discr = GSM48_PDISC_RR;
1204 gh->msg_type = GSM48_MT_RR_APP_INFO;
1205 gh->data[0] = apdu_id;
1206 gh->data[1] = apdu_len;
1207 memcpy(gh->data+2, apdu, apdu_len);
1208
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001209 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001210}
1211
Harald Welte4bc90a12008-12-27 16:32:52 +00001212/* Call Control */
1213
Harald Welte7584aea2009-02-11 11:44:12 +00001214/* The entire call control code is written in accordance with Figure 7.10c
1215 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1216 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1217 * it for voice */
1218
Harald Welte4bfdfe72009-06-10 23:11:52 +08001219static void new_cc_state(struct gsm_trans *trans, int state)
1220{
1221 if (state > 31 || state < 0)
1222 return;
1223
1224 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001225 gsm48_cc_state_name(trans->cc.state),
1226 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001227
Harald Weltedcaf5652009-07-23 18:56:43 +02001228 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001229}
1230
1231static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001232{
1233 struct msgb *msg = gsm48_msgb_alloc();
1234 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001235 uint8_t *cause, *call_state;
Harald Welte4bc90a12008-12-27 16:32:52 +00001236
Harald Welte4bc90a12008-12-27 16:32:52 +00001237 gh->msg_type = GSM48_MT_CC_STATUS;
1238
1239 cause = msgb_put(msg, 3);
1240 cause[0] = 2;
1241 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1242 cause[2] = 0x80 | 30; /* response to status inquiry */
1243
1244 call_state = msgb_put(msg, 1);
1245 call_state[0] = 0xc0 | 0x00;
1246
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001247 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001248}
1249
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001250static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001251 uint8_t pdisc, uint8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001252{
1253 struct msgb *msg = gsm48_msgb_alloc();
1254 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1255
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001256 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001257
Harald Welte6f4b7532008-12-29 00:39:37 +00001258 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001259 gh->msg_type = msg_type;
1260
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001261 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001262}
1263
Harald Welte4bfdfe72009-06-10 23:11:52 +08001264static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1265{
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001266 if (osmo_timer_pending(&trans->cc.timer)) {
Harald Weltedcaf5652009-07-23 18:56:43 +02001267 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001268 osmo_timer_del(&trans->cc.timer);
Harald Weltedcaf5652009-07-23 18:56:43 +02001269 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001270 }
1271}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001272
Harald Welte4bfdfe72009-06-10 23:11:52 +08001273static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1274 int msg_type, struct gsm_mncc *mncc)
1275{
1276 struct msgb *msg;
Harald Welte04dc88f2010-12-22 21:45:05 +01001277 unsigned char *data;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001278
1279 if (trans)
Harald Welteb854b292010-12-26 19:06:37 +01001280 if (trans->conn && trans->conn->lchan)
Harald Welte6f5aee02009-07-23 21:21:14 +02001281 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001283 trans->conn->lchan->ts->trx->bts->nr,
1284 trans->conn->lchan->ts->trx->nr,
1285 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 (trans->subscr)?(trans->subscr->extension):"-",
1287 get_mncc_name(msg_type));
1288 else
1289 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1290 "Sending '%s' to MNCC.\n",
1291 (trans->subscr)?(trans->subscr->extension):"-",
1292 get_mncc_name(msg_type));
1293 else
1294 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1295 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1296
1297 mncc->msg_type = msg_type;
1298
Harald Welte966636f2009-06-26 19:39:35 +02001299 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300 if (!msg)
1301 return -ENOMEM;
Harald Welte04dc88f2010-12-22 21:45:05 +01001302
1303 data = msgb_put(msg, sizeof(struct gsm_mncc));
1304 memcpy(data, mncc, sizeof(struct gsm_mncc));
1305
Harald Welte54209c22010-12-22 23:43:29 +01001306 cc_tx_to_mncc(net, msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001307
1308 return 0;
1309}
1310
1311int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001312 uint32_t callref, int location, int value)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001313{
1314 struct gsm_mncc rel;
1315
Harald Welte92f70c52009-06-12 01:54:08 +08001316 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001317 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001318 mncc_set_cause(&rel, location, value);
Andreas Eversberg15907272013-01-25 08:38:29 +01001319 if (trans && trans->cc.state == GSM_CSTATE_RELEASE_REQ)
1320 return mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001321 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1322}
1323
Harald Weltedcaf5652009-07-23 18:56:43 +02001324/* Call Control Specific transaction release.
1325 * gets called by trans_free, DO NOT CALL YOURSELF! */
1326void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001327{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328 gsm48_stop_cc_timer(trans);
1329
1330 /* send release to L4, if callref still exists */
1331 if (trans->callref) {
1332 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001333 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001334 GSM48_CAUSE_LOC_PRN_S_LU,
1335 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001337 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001339 if (trans->conn)
1340 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001341}
1342
1343static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001344
Harald Welte09e38af2009-02-16 22:52:23 +00001345/* call-back from paging the B-end of the connection */
1346static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001347 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001348{
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001349 int found = 0;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001350 struct gsm_subscriber_connection *conn = _conn;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001351 struct gsm_network **paging_request = param, *net;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352 struct gsm_trans *transt, *tmp;
Harald Weltec05677b2009-06-26 20:17:06 +02001353
Harald Welte09e38af2009-02-16 22:52:23 +00001354 if (hooknum != GSM_HOOK_RR_PAGING)
1355 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001356
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001357 net = *paging_request;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001358 if (!net) {
1359 DEBUGP(DCC, "Error Network not set!\n");
1360 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001361 }
Harald Welte7584aea2009-02-11 11:44:12 +00001362
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 /* check all tranactions (without lchan) for subscriber */
1364 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001365 if (transt->paging_request != paging_request || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 continue;
1367 switch (event) {
1368 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001369 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001370 break;
1371 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001372 transt->subscr->extension);
1373 found = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001375 if (!transt->conn) {
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001376 transt->paging_request = NULL;
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001377 transt->conn = conn;
Holger Hans Peter Freyther182c81f2010-12-29 16:28:33 +01001378 conn->put_channel = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001379 }
1380 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001381 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001382 break;
1383 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherd3baf412010-12-23 18:19:17 +01001384 case GSM_PAGING_BUSY:
Harald Welte4bfdfe72009-06-10 23:11:52 +08001385 DEBUGP(DCC, "Paging subscr %s expired!\n",
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001386 transt->subscr->extension);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 /* Temporarily out of order */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001388 found = 1;
Harald Welte596fed42009-07-23 19:06:52 +02001389 mncc_release_ind(transt->subscr->net, transt,
1390 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001391 GSM48_CAUSE_LOC_PRN_S_LU,
1392 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 transt->callref = 0;
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001394 transt->paging_request = NULL;
Harald Weltedcaf5652009-07-23 18:56:43 +02001395 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001396 break;
1397 }
1398 }
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01001399
1400 talloc_free(paging_request);
1401
1402 /*
1403 * FIXME: The queue needs to be kicked. This is likely to go through a RF
1404 * failure and then the subscr will be poke again. This needs a lot of fixing
1405 * in the subscriber queue code.
1406 */
1407 if (!found && conn)
1408 conn->put_channel = 1;
Harald Welte09e38af2009-02-16 22:52:23 +00001409 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001410}
Harald Welte7584aea2009-02-11 11:44:12 +00001411
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001412static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable);
Harald Welteda7ab742009-12-19 22:23:05 +01001413
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001414/* handle audio path for handover */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001415static int switch_for_handover(struct gsm_lchan *old_lchan,
1416 struct gsm_lchan *new_lchan)
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001417{
1418 struct rtp_socket *old_rs, *new_rs, *other_rs;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001419
1420 if (ipacc_rtp_direct) {
1421 LOGP(DHO, LOGL_ERROR, "unable to handover in direct RTP mode\n");
1422 return 0;
1423 }
1424
1425 /* RTP Proxy mode */
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001426 new_rs = new_lchan->abis_ip.rtp_socket;
1427 old_rs = old_lchan->abis_ip.rtp_socket;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001428
1429 if (!new_rs) {
1430 LOGP(DHO, LOGL_ERROR, "no RTP socket for new_lchan\n");
1431 return -EIO;
1432 }
1433
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001434 rsl_ipacc_mdcx_to_rtpsock(new_lchan);
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001435
1436 if (!old_rs) {
1437 LOGP(DHO, LOGL_ERROR, "no RTP socket for old_lchan\n");
1438 return -EIO;
1439 }
1440
1441 /* copy rx_action and reference to other sock */
1442 new_rs->rx_action = old_rs->rx_action;
1443 new_rs->tx_action = old_rs->tx_action;
1444 new_rs->transmit = old_rs->transmit;
1445
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001446 switch (old_lchan->abis_ip.rtp_socket->rx_action) {
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001447 case RTP_PROXY:
1448 other_rs = old_rs->proxy.other_sock;
1449 rtp_socket_proxy(new_rs, other_rs);
1450 /* delete reference to other end socket to prevent
1451 * rtp_socket_free() from removing the inverse reference */
1452 old_rs->proxy.other_sock = NULL;
1453 break;
1454 case RTP_RECV_UPSTREAM:
1455 new_rs->receive = old_rs->receive;
1456 break;
1457 case RTP_NONE:
1458 break;
1459 }
1460
1461 return 0;
1462}
1463
Harald Welte805f6442009-07-28 18:25:29 +02001464/* some other part of the code sends us a signal */
1465static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1466 void *handler_data, void *signal_data)
1467{
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001468 struct gsm_lchan *lchan = signal_data, *old_lchan;
Harald Welte805f6442009-07-28 18:25:29 +02001469 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001470 struct gsm_network *net;
1471 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001472
1473 if (subsys != SS_ABISIP)
1474 return 0;
1475
1476 /* in case we use direct BTS-to-BTS RTP */
1477 if (ipacc_rtp_direct)
1478 return 0;
1479
Harald Welte805f6442009-07-28 18:25:29 +02001480 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001481 case S_ABISIP_CRCX_ACK:
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001482 /* in case we don't use direct BTS-to-BTS RTP */
1483 /* the BTS has successfully bound a TCH to a local ip/port,
1484 * which means we can connect our UDP socket to it */
1485 if (lchan->abis_ip.rtp_socket) {
1486 rtp_socket_free(lchan->abis_ip.rtp_socket);
1487 lchan->abis_ip.rtp_socket = NULL;
1488 }
1489
1490 lchan->abis_ip.rtp_socket = rtp_socket_create();
1491 if (!lchan->abis_ip.rtp_socket)
1492 return -EIO;
1493
1494 rc = rtp_socket_connect(lchan->abis_ip.rtp_socket,
1495 lchan->abis_ip.bound_ip,
1496 lchan->abis_ip.bound_port);
1497 if (rc < 0)
1498 return -EIO;
1499
Harald Welteda7ab742009-12-19 22:23:05 +01001500 /* check if any transactions on this lchan still have
1501 * a tch_recv_mncc request pending */
1502 net = lchan->ts->trx->bts->network;
1503 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001504 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001505 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1506 tch_recv_mncc(net, trans->callref, 1);
1507 }
1508 }
Holger Hans Peter Freytherc121bb32012-12-26 10:17:42 +01001509
1510 /*
1511 * TODO: this appears to be too early? Why not until after
1512 * the handover detect or the handover complete?
1513 *
1514 * Do we have a handover pending for this new lchan? In that
1515 * case re-route the audio from the old channel to the new one.
1516 */
1517 old_lchan = bsc_handover_pending(lchan);
1518 if (old_lchan)
1519 switch_for_handover(old_lchan, lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001520 break;
Holger Hans Peter Freyther6c4d2442011-01-06 13:31:41 +01001521 case S_ABISIP_DLCX_IND:
1522 /* the BTS tells us a RTP stream has been disconnected */
1523 if (lchan->abis_ip.rtp_socket) {
1524 rtp_socket_free(lchan->abis_ip.rtp_socket);
1525 lchan->abis_ip.rtp_socket = NULL;
1526 }
1527
1528 break;
Harald Welte805f6442009-07-28 18:25:29 +02001529 }
1530
1531 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001532}
1533
Harald Welte49f48b82009-02-17 15:29:33 +00001534/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001535static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001536{
Harald Welte11fa29c2009-02-19 17:24:39 +00001537 struct gsm_bts *bts = lchan->ts->trx->bts;
1538 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001539 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001540
Harald Welte11fa29c2009-02-19 17:24:39 +00001541 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1542 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1543 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1544
1545 if (bts->type != remote_bts->type) {
Harald Weltee5215b52011-08-09 21:53:20 +02001546 LOGP(DCC, LOGL_ERROR, "Cannot switch calls between different BTS types yet\n");
Harald Welte11fa29c2009-02-19 17:24:39 +00001547 return -EINVAL;
1548 }
Harald Welteda7ab742009-12-19 22:23:05 +01001549
1550 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001551 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001552 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001553 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte805f6442009-07-28 18:25:29 +02001554 if (!ipacc_rtp_direct) {
1555 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001556 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001557 if (rc < 0)
1558 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001559 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte4d54d0b2011-02-19 16:48:17 +01001560 if (rc < 0)
1561 return rc;
Harald Welte805f6442009-07-28 18:25:29 +02001562 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301563 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1564 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001565 } else {
1566 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301567 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1568 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301569 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001570 if (rc < 0)
1571 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301572 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1573 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301574 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001575 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001576 break;
1577 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001578 case GSM_BTS_TYPE_RBS2000:
Dieter Spaar16646022011-07-28 00:01:50 +02001579 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welte11fa29c2009-02-19 17:24:39 +00001580 trau_mux_map_lchan(lchan, remote_lchan);
1581 break;
1582 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001583 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001584 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001585 }
Harald Welte49f48b82009-02-17 15:29:33 +00001586
1587 return 0;
1588}
1589
Harald Welte4bfdfe72009-06-10 23:11:52 +08001590/* bridge channels of two transactions */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001591static int tch_bridge(struct gsm_network *net, uint32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001592{
Harald Weltedcaf5652009-07-23 18:56:43 +02001593 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1594 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001595
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001597 return -EIO;
1598
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001599 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001600 return -EIO;
1601
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001602 /* Which subscriber do we want to track trans1 or trans2? */
1603 log_set_context(BSC_CTX_SUBSCR, trans1->subscr);
1604
Harald Welte4bfdfe72009-06-10 23:11:52 +08001605 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001606 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001607}
1608
Harald Welteda7ab742009-12-19 22:23:05 +01001609/* enable receive of channels to MNCC upqueue */
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001610static int tch_recv_mncc(struct gsm_network *net, uint32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001611{
1612 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001613 struct gsm_lchan *lchan;
1614 struct gsm_bts *bts;
1615 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001616
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001618 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001619 if (!trans)
1620 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001621 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001622 return 0;
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01001623
1624 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001625 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001626 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001627
Harald Welteda7ab742009-12-19 22:23:05 +01001628 switch (bts->type) {
1629 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02001630 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welteda7ab742009-12-19 22:23:05 +01001631 if (ipacc_rtp_direct) {
Harald Weltee5215b52011-08-09 21:53:20 +02001632 LOGP(DCC, LOGL_ERROR, "Error: RTP proxy is disabled\n");
Harald Welteda7ab742009-12-19 22:23:05 +01001633 return -EINVAL;
1634 }
1635 /* in case, we don't have a RTP socket yet, we note this
1636 * in the transaction and try later */
1637 if (!lchan->abis_ip.rtp_socket) {
1638 trans->tch_recv = enable;
1639 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1640 return 0;
1641 }
1642 if (enable) {
1643 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001644 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001645 if (rc < 0)
1646 return rc;
1647 /* assign socket to application interface */
1648 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1649 net, callref);
1650 } else
1651 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1652 net, 0);
1653 break;
1654 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03001655 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02001656 case GSM_BTS_TYPE_NOKIA_SITE:
Harald Welteda7ab742009-12-19 22:23:05 +01001657 if (enable)
1658 return trau_recv_lchan(lchan, callref);
1659 return trau_mux_unmap(NULL, callref);
1660 break;
1661 default:
Harald Weltee5215b52011-08-09 21:53:20 +02001662 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001663 return -EINVAL;
1664 }
1665
1666 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001667}
1668
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1670{
1671 DEBUGP(DCC, "-> STATUS ENQ\n");
1672 return gsm48_cc_tx_status(trans, msg);
1673}
1674
1675static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1676static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1677
1678static void gsm48_cc_timeout(void *arg)
1679{
1680 struct gsm_trans *trans = arg;
1681 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001682 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1683 int mo_location = GSM48_CAUSE_LOC_USER;
1684 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1685 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001686 struct gsm_mncc mo_rel, l4_rel;
1687
1688 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1689 mo_rel.callref = trans->callref;
1690 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1691 l4_rel.callref = trans->callref;
1692
Harald Weltedcaf5652009-07-23 18:56:43 +02001693 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001694 case 0x303:
1695 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001696 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 break;
1698 case 0x310:
1699 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001700 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001701 break;
1702 case 0x313:
1703 disconnect = 1;
1704 /* unknown, did not find it in the specs */
1705 break;
1706 case 0x301:
1707 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001708 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 break;
1710 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001711 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001713 gsm48_cc_tx_release(trans, &trans->cc.msg);
1714 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 break; /* stay in release state */
1716 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001717 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 return;
1719// release = 1;
1720// l4_cause = 14;
1721// break;
1722 case 0x306:
1723 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001724 mo_cause = trans->cc.msg.cause.value;
1725 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001726 break;
1727 case 0x323:
1728 disconnect = 1;
1729 break;
1730 default:
1731 release = 1;
1732 }
1733
1734 if (release && trans->callref) {
1735 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001736 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001737 l4_location, l4_cause);
1738 trans->callref = 0;
1739 }
1740
1741 if (disconnect && trans->callref) {
1742 /* process disconnect towards layer 4 */
1743 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001744 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 }
1746
1747 /* process disconnect towards mobile station */
1748 if (disconnect || release) {
1749 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001750 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1751 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1752 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001753 mo_rel.cause.diag_len = 3;
1754
1755 if (disconnect)
1756 gsm48_cc_tx_disconnect(trans, &mo_rel);
1757 if (release)
1758 gsm48_cc_tx_release(trans, &mo_rel);
1759 }
1760
1761}
1762
1763static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1764 int sec, int micro)
1765{
1766 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001767 trans->cc.timer.cb = gsm48_cc_timeout;
1768 trans->cc.timer.data = trans;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02001769 osmo_timer_schedule(&trans->cc.timer, sec, micro);
Harald Weltedcaf5652009-07-23 18:56:43 +02001770 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771}
1772
1773static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1774{
1775 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02001776 uint8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1778 struct tlv_parsed tp;
1779 struct gsm_mncc setup;
1780
1781 memset(&setup, 0, sizeof(struct gsm_mncc));
1782 setup.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001783 if (trans->conn && trans->conn->lchan)
1784 setup.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001785 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 /* emergency setup is identified by msg_type */
1787 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1788 setup.emergency = 1;
1789
1790 /* use subscriber as calling party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02001791 setup.fields |= MNCC_F_CALLING;
1792 strncpy(setup.calling.number, trans->subscr->extension,
1793 sizeof(setup.calling.number)-1);
1794 strncpy(setup.imsi, trans->subscr->imsi,
1795 sizeof(setup.imsi)-1);
1796
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 /* bearer capability */
1798 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1799 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001800 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1802 }
1803 /* facility */
1804 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1805 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001806 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1808 }
1809 /* called party bcd number */
1810 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1811 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001812 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001813 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1814 }
1815 /* user-user */
1816 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1817 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001818 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001819 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1820 }
1821 /* ss-version */
1822 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1823 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001824 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001825 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1826 }
1827 /* CLIR suppression */
1828 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1829 setup.clir.sup = 1;
1830 /* CLIR invocation */
1831 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1832 setup.clir.inv = 1;
1833 /* cc cap */
1834 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1835 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001836 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1838 }
1839
Harald Welte4bfdfe72009-06-10 23:11:52 +08001840 new_cc_state(trans, GSM_CSTATE_INITIATED);
1841
Harald Welte (local)47df3992009-12-26 19:45:03 +01001842 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1843 subscr_name(trans->subscr), trans->subscr->extension,
1844 setup.called.number);
1845
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001846 osmo_counter_inc(trans->subscr->net->stats.call.mo_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001847
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001849 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850
Harald Welte13cac662009-07-29 12:10:35 +02001851 /* MNCC code will modify the channel asynchronously, we should
1852 * ipaccess-bind only after the modification has been made to the
1853 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001854 return 0;
1855}
1856
1857static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001858{
1859 struct msgb *msg = gsm48_msgb_alloc();
1860 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001861 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001862 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001863
Harald Welte7ccf7782009-02-17 01:43:01 +00001864 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001865
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866 /* transaction id must not be assigned */
1867 if (trans->transaction_id != 0xff) { /* unasssigned */
1868 DEBUGP(DCC, "TX Setup with assigned transaction. "
1869 "This is not allowed!\n");
1870 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001871 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001872 GSM48_CAUSE_LOC_PRN_S_LU,
1873 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001875 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001876 return rc;
1877 }
1878
1879 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001880 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1881 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001883 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001884 GSM48_CAUSE_LOC_PRN_S_LU,
1885 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001887 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 return rc;
1889 }
Harald Welte78283ef2009-07-23 21:36:44 +02001890 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001891
Harald Welte65e74cc2008-12-29 01:55:35 +00001892 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001893
Harald Welte4bfdfe72009-06-10 23:11:52 +08001894 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001895
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 /* bearer capability */
1897 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 /* facility */
1900 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001901 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001902 /* progress */
1903 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 /* calling party BCD number */
1906 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001907 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908 /* called party BCD number */
1909 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001910 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001911 /* user-user */
1912 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001913 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001914 /* redirecting party BCD number */
1915 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001916 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001917 /* signal */
1918 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001919 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920
1921 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001922
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02001923 osmo_counter_inc(trans->subscr->net->stats.call.mt_setup);
Harald Weltea29e43a2010-12-24 16:06:33 +01001924
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001925 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001926}
1927
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1929{
1930 struct gsm48_hdr *gh = msgb_l3(msg);
1931 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1932 struct tlv_parsed tp;
1933 struct gsm_mncc call_conf;
1934
1935 gsm48_stop_cc_timer(trans);
1936 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1937
1938 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1939 call_conf.callref = trans->callref;
Harald Welteab386e62011-09-01 18:18:43 +02001940 if (trans->conn && trans->conn->lchan)
1941 call_conf.lchan_type = trans->conn->lchan->type;
Harald Welte474d19f2010-03-02 23:18:30 +01001942 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943#if 0
1944 /* repeat */
1945 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1946 call_conf.repeat = 1;
1947 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1948 call_conf.repeat = 2;
1949#endif
1950 /* bearer capability */
1951 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1952 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001953 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001954 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1955 }
1956 /* cause */
1957 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1958 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1961 }
1962 /* cc cap */
1963 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1964 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1967 }
1968
1969 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1970
Harald Welte596fed42009-07-23 19:06:52 +02001971 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1972 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973}
1974
1975static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1976{
1977 struct gsm_mncc *proceeding = arg;
1978 struct msgb *msg = gsm48_msgb_alloc();
1979 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1980
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1982
1983 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1984
1985 /* bearer capability */
1986 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001987 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 /* facility */
1989 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001990 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001991 /* progress */
1992 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001993 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001995 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996}
1997
1998static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1999{
2000 struct gsm48_hdr *gh = msgb_l3(msg);
2001 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2002 struct tlv_parsed tp;
2003 struct gsm_mncc alerting;
2004
2005 gsm48_stop_cc_timer(trans);
2006 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
2007
2008 memset(&alerting, 0, sizeof(struct gsm_mncc));
2009 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002010 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 /* facility */
2012 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2013 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002014 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2016 }
2017
2018 /* progress */
2019 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
2020 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01002021 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002022 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
2023 }
2024 /* ss-version */
2025 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2026 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002027 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002028 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2029 }
2030
2031 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
2032
Harald Welte596fed42009-07-23 19:06:52 +02002033 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
2034 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035}
2036
2037static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
2038{
2039 struct gsm_mncc *alerting = arg;
2040 struct msgb *msg = gsm48_msgb_alloc();
2041 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2042
Harald Welte4bfdfe72009-06-10 23:11:52 +08002043 gh->msg_type = GSM48_MT_CC_ALERTING;
2044
2045 /* facility */
2046 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002047 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002048 /* progress */
2049 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002050 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 /* user-user */
2052 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002053 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002054
2055 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
2056
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002057 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002058}
2059
2060static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
2061{
2062 struct gsm_mncc *progress = arg;
2063 struct msgb *msg = gsm48_msgb_alloc();
2064 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2065
Harald Welte4bfdfe72009-06-10 23:11:52 +08002066 gh->msg_type = GSM48_MT_CC_PROGRESS;
2067
2068 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01002069 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002070 /* user-user */
2071 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002072 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002073
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002074 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075}
2076
2077static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
2078{
2079 struct gsm_mncc *connect = arg;
2080 struct msgb *msg = gsm48_msgb_alloc();
2081 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2082
Harald Welte4bfdfe72009-06-10 23:11:52 +08002083 gh->msg_type = GSM48_MT_CC_CONNECT;
2084
2085 gsm48_stop_cc_timer(trans);
2086 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2087
2088 /* facility */
2089 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 /* progress */
2092 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002093 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 /* connected number */
2095 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 /* user-user */
2098 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002099 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100
2101 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2102
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002103 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104}
2105
2106static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2107{
2108 struct gsm48_hdr *gh = msgb_l3(msg);
2109 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2110 struct tlv_parsed tp;
2111 struct gsm_mncc connect;
2112
2113 gsm48_stop_cc_timer(trans);
2114
2115 memset(&connect, 0, sizeof(struct gsm_mncc));
2116 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002117 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 /* use subscriber as connected party number */
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02002119 connect.fields |= MNCC_F_CONNECTED;
2120 strncpy(connect.connected.number, trans->subscr->extension,
2121 sizeof(connect.connected.number)-1);
2122 strncpy(connect.imsi, trans->subscr->imsi,
2123 sizeof(connect.imsi)-1);
2124
Harald Welte4bfdfe72009-06-10 23:11:52 +08002125 /* facility */
2126 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2127 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002128 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2130 }
2131 /* user-user */
2132 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2133 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002134 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002135 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2136 }
2137 /* ss-version */
2138 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2139 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002140 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2142 }
2143
2144 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002145 osmo_counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146
Harald Welte596fed42009-07-23 19:06:52 +02002147 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002148}
2149
2150
2151static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2152{
2153 struct gsm_mncc connect_ack;
2154
2155 gsm48_stop_cc_timer(trans);
2156
2157 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Pablo Neira Ayusodfb342c2011-05-06 12:13:10 +02002158 osmo_counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159
2160 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2161 connect_ack.callref = trans->callref;
Harald Weltea29e43a2010-12-24 16:06:33 +01002162
Harald Welte596fed42009-07-23 19:06:52 +02002163 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164 &connect_ack);
2165}
2166
2167static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2168{
2169 struct msgb *msg = gsm48_msgb_alloc();
2170 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2171
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2173
2174 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2175
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002176 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177}
2178
2179static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2180{
2181 struct gsm48_hdr *gh = msgb_l3(msg);
2182 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2183 struct tlv_parsed tp;
2184 struct gsm_mncc disc;
2185
2186 gsm48_stop_cc_timer(trans);
2187
2188 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2189
2190 memset(&disc, 0, sizeof(struct gsm_mncc));
2191 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002192 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 /* cause */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2195 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002196 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2198 }
2199 /* facility */
2200 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2201 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002202 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2204 }
2205 /* user-user */
2206 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2207 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002208 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2210 }
2211 /* ss-version */
2212 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2213 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002214 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2216 }
2217
Harald Welte596fed42009-07-23 19:06:52 +02002218 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002219
2220}
2221
Harald Weltec66b71c2009-06-11 14:23:20 +08002222static struct gsm_mncc_cause default_cause = {
2223 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2224 .coding = 0,
2225 .rec = 0,
2226 .rec_val = 0,
2227 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2228 .diag_len = 0,
2229 .diag = { 0 },
2230};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002231
2232static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2233{
2234 struct gsm_mncc *disc = arg;
2235 struct msgb *msg = gsm48_msgb_alloc();
2236 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2237
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2239
2240 gsm48_stop_cc_timer(trans);
2241 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2242
2243 /* cause */
2244 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002245 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 else
Harald Welte55c8f352010-03-07 23:40:35 +01002247 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248
2249 /* facility */
2250 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002251 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252 /* progress */
2253 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002254 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255 /* user-user */
2256 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002257 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002258
2259 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002260 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002261
2262 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2263
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002264 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265}
2266
2267static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2268{
2269 struct gsm48_hdr *gh = msgb_l3(msg);
2270 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2271 struct tlv_parsed tp;
2272 struct gsm_mncc rel;
2273 int rc;
2274
2275 gsm48_stop_cc_timer(trans);
2276
2277 memset(&rel, 0, sizeof(struct gsm_mncc));
2278 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002279 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 /* cause */
2281 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2282 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002283 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002284 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2285 }
2286 /* facility */
2287 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2288 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002289 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002290 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2291 }
2292 /* user-user */
2293 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2294 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002295 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2297 }
2298 /* ss-version */
2299 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2300 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002301 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2303 }
2304
Harald Weltedcaf5652009-07-23 18:56:43 +02002305 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002306 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002307 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002309 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002310 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002311 GSM48_MT_CC_RELEASE_COMPL);
2312 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002313 }
2314
2315 new_cc_state(trans, GSM_CSTATE_NULL);
2316
2317 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002318 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002319
2320 return rc;
2321}
2322
2323static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2324{
2325 struct gsm_mncc *rel = arg;
2326 struct msgb *msg = gsm48_msgb_alloc();
2327 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2328
Harald Welte4bfdfe72009-06-10 23:11:52 +08002329 gh->msg_type = GSM48_MT_CC_RELEASE;
2330
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 gsm48_stop_cc_timer(trans);
2332 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2333
2334 /* cause */
2335 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002336 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002337 /* facility */
2338 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002339 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 /* user-user */
2341 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343
Harald Weltedcaf5652009-07-23 18:56:43 +02002344 trans->cc.T308_second = 0;
2345 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346
Harald Weltedcaf5652009-07-23 18:56:43 +02002347 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2349
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002350 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002351}
2352
2353static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2354{
2355 struct gsm48_hdr *gh = msgb_l3(msg);
2356 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2357 struct tlv_parsed tp;
2358 struct gsm_mncc rel;
2359 int rc = 0;
2360
2361 gsm48_stop_cc_timer(trans);
2362
2363 memset(&rel, 0, sizeof(struct gsm_mncc));
2364 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002365 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 /* cause */
2367 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2368 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002369 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002370 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2371 }
2372 /* facility */
2373 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2374 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002375 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2377 }
2378 /* user-user */
2379 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2380 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002381 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002382 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2383 }
2384 /* ss-version */
2385 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2386 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002387 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2389 }
2390
2391 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002392 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002394 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 MNCC_REJ_IND, &rel);
2396 break;
2397 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002398 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399 MNCC_REL_CNF, &rel);
2400 break;
2401 default:
Harald Welte596fed42009-07-23 19:06:52 +02002402 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002403 MNCC_REL_IND, &rel);
2404 }
2405 }
2406
2407 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002408 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409
2410 return rc;
2411}
2412
2413static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2414{
2415 struct gsm_mncc *rel = arg;
2416 struct msgb *msg = gsm48_msgb_alloc();
2417 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Weltec7782de2010-12-21 19:31:41 +01002418 int ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002419
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2421
2422 trans->callref = 0;
2423
2424 gsm48_stop_cc_timer(trans);
2425
2426 /* cause */
2427 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002428 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429 /* facility */
2430 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002431 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 /* user-user */
2433 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002434 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002435
Harald Weltec7782de2010-12-21 19:31:41 +01002436 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2437
Harald Weltedcaf5652009-07-23 18:56:43 +02002438 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439
Harald Weltec7782de2010-12-21 19:31:41 +01002440 return ret;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441}
2442
2443static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2444{
2445 struct gsm48_hdr *gh = msgb_l3(msg);
2446 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2447 struct tlv_parsed tp;
2448 struct gsm_mncc fac;
2449
2450 memset(&fac, 0, sizeof(struct gsm_mncc));
2451 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002452 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 /* facility */
2454 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2455 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2458 }
2459 /* ss-version */
2460 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2461 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002462 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2464 }
2465
Harald Welte596fed42009-07-23 19:06:52 +02002466 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467}
2468
2469static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2470{
2471 struct gsm_mncc *fac = arg;
2472 struct msgb *msg = gsm48_msgb_alloc();
2473 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2474
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 gh->msg_type = GSM48_MT_CC_FACILITY;
2476
2477 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002480 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481}
2482
2483static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2484{
2485 struct gsm_mncc hold;
2486
2487 memset(&hold, 0, sizeof(struct gsm_mncc));
2488 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002489 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490}
2491
2492static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2493{
2494 struct msgb *msg = gsm48_msgb_alloc();
2495 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2496
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2498
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002499 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500}
2501
2502static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2503{
2504 struct gsm_mncc *hold_rej = arg;
2505 struct msgb *msg = gsm48_msgb_alloc();
2506 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2507
Harald Welte4bfdfe72009-06-10 23:11:52 +08002508 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2509
2510 /* cause */
2511 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002512 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513 else
Harald Welte55c8f352010-03-07 23:40:35 +01002514 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002516 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002517}
2518
2519static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2520{
2521 struct gsm_mncc retrieve;
2522
2523 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2524 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002525 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2526 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002527}
2528
2529static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2530{
2531 struct msgb *msg = gsm48_msgb_alloc();
2532 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2533
Harald Welte4bfdfe72009-06-10 23:11:52 +08002534 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2535
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002536 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002537}
2538
2539static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2540{
2541 struct gsm_mncc *retrieve_rej = arg;
2542 struct msgb *msg = gsm48_msgb_alloc();
2543 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2544
Harald Welte4bfdfe72009-06-10 23:11:52 +08002545 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2546
2547 /* cause */
2548 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002549 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002550 else
Harald Welte55c8f352010-03-07 23:40:35 +01002551 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002552
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002553 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002554}
2555
2556static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2557{
2558 struct gsm48_hdr *gh = msgb_l3(msg);
2559 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2560 struct tlv_parsed tp;
2561 struct gsm_mncc dtmf;
2562
2563 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2564 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002565 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002566 /* keypad facility */
2567 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2568 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002569 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002570 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2571 }
2572
Harald Welte596fed42009-07-23 19:06:52 +02002573 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002574}
2575
2576static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2577{
2578 struct gsm_mncc *dtmf = arg;
2579 struct msgb *msg = gsm48_msgb_alloc();
2580 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2581
Harald Welte4bfdfe72009-06-10 23:11:52 +08002582 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2583
2584 /* keypad */
2585 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002586 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002587
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002588 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589}
2590
2591static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2592{
2593 struct gsm_mncc *dtmf = arg;
2594 struct msgb *msg = gsm48_msgb_alloc();
2595 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2596
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2598
2599 /* cause */
2600 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002601 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002602 else
Harald Welte55c8f352010-03-07 23:40:35 +01002603 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002604
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002605 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002606}
2607
2608static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2609{
2610 struct msgb *msg = gsm48_msgb_alloc();
2611 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2612
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2614
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002615 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002616}
2617
2618static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2619{
2620 struct gsm_mncc dtmf;
2621
2622 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2623 dtmf.callref = trans->callref;
2624
Harald Welte596fed42009-07-23 19:06:52 +02002625 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002626}
2627
2628static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2629{
2630 struct gsm48_hdr *gh = msgb_l3(msg);
2631 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2632 struct tlv_parsed tp;
2633 struct gsm_mncc modify;
2634
2635 memset(&modify, 0, sizeof(struct gsm_mncc));
2636 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002637 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002638 /* bearer capability */
2639 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2640 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002641 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002642 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2643 }
2644
2645 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2646
Harald Welte596fed42009-07-23 19:06:52 +02002647 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2651{
2652 struct gsm_mncc *modify = arg;
2653 struct msgb *msg = gsm48_msgb_alloc();
2654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2655
Harald Welte4bfdfe72009-06-10 23:11:52 +08002656 gh->msg_type = GSM48_MT_CC_MODIFY;
2657
2658 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2659
2660 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002661 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002662
2663 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2664
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002665 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002666}
2667
2668static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2669{
2670 struct gsm48_hdr *gh = msgb_l3(msg);
2671 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2672 struct tlv_parsed tp;
2673 struct gsm_mncc modify;
2674
2675 gsm48_stop_cc_timer(trans);
2676
2677 memset(&modify, 0, sizeof(struct gsm_mncc));
2678 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002679 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002680 /* bearer capability */
2681 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2682 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002683 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2685 }
2686
2687 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2688
Harald Welte596fed42009-07-23 19:06:52 +02002689 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690}
2691
2692static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2693{
2694 struct gsm_mncc *modify = arg;
2695 struct msgb *msg = gsm48_msgb_alloc();
2696 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2697
Harald Welte4bfdfe72009-06-10 23:11:52 +08002698 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2699
2700 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002701 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702
2703 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2704
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002705 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706}
2707
2708static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2709{
2710 struct gsm48_hdr *gh = msgb_l3(msg);
2711 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2712 struct tlv_parsed tp;
2713 struct gsm_mncc modify;
2714
2715 gsm48_stop_cc_timer(trans);
2716
2717 memset(&modify, 0, sizeof(struct gsm_mncc));
2718 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002719 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002720 /* bearer capability */
2721 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2722 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002723 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002724 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2725 }
2726 /* cause */
2727 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2728 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002729 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2731 }
2732
2733 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2734
Harald Welte596fed42009-07-23 19:06:52 +02002735 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002736}
2737
2738static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2739{
2740 struct gsm_mncc *modify = arg;
2741 struct msgb *msg = gsm48_msgb_alloc();
2742 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2743
Harald Welte4bfdfe72009-06-10 23:11:52 +08002744 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2745
2746 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002747 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002748 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002749 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002750
2751 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2752
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002753 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002754}
2755
2756static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2757{
2758 struct gsm_mncc *notify = 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_NOTIFY;
2763
2764 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002765 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002766
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002767 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002768}
2769
2770static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2771{
2772 struct gsm48_hdr *gh = msgb_l3(msg);
2773 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2774// struct tlv_parsed tp;
2775 struct gsm_mncc notify;
2776
2777 memset(&notify, 0, sizeof(struct gsm_mncc));
2778 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002779// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002780 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002781 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002782
Harald Welte596fed42009-07-23 19:06:52 +02002783 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002784}
2785
2786static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2787{
2788 struct gsm_mncc *user = arg;
2789 struct msgb *msg = gsm48_msgb_alloc();
2790 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2791
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 gh->msg_type = GSM48_MT_CC_USER_INFO;
2793
2794 /* user-user */
2795 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002796 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797 /* more data */
2798 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002799 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002800
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002801 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002802}
2803
2804static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2805{
2806 struct gsm48_hdr *gh = msgb_l3(msg);
2807 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2808 struct tlv_parsed tp;
2809 struct gsm_mncc user;
2810
2811 memset(&user, 0, sizeof(struct gsm_mncc));
2812 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002813 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002814 /* user-user */
2815 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2816 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002817 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002818 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2819 }
2820 /* more data */
2821 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2822 user.more = 1;
2823
Harald Welte596fed42009-07-23 19:06:52 +02002824 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002825}
2826
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002827static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002828{
2829 struct gsm_mncc *mode = arg;
2830
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01002831 return gsm0808_assign_req(trans->conn, mode->lchan_mode, 1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002832}
2833
2834static struct downstate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02002835 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002836 int type;
2837 int (*rout) (struct gsm_trans *trans, void *arg);
2838} downstatelist[] = {
2839 /* mobile originating call establishment */
2840 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2841 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2842 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2843 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2844 {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 */
2845 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2846 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2847 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2848 /* mobile terminating call establishment */
2849 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2850 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2851 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2852 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2853 /* signalling during call */
2854 {SBIT(GSM_CSTATE_ACTIVE),
2855 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2856 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2857 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2858 {ALL_STATES,
2859 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2860 {ALL_STATES,
2861 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2862 {ALL_STATES,
2863 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2864 {SBIT(GSM_CSTATE_ACTIVE),
2865 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2866 {SBIT(GSM_CSTATE_ACTIVE),
2867 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2868 {SBIT(GSM_CSTATE_ACTIVE),
2869 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2870 {SBIT(GSM_CSTATE_ACTIVE),
2871 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2872 {SBIT(GSM_CSTATE_ACTIVE),
2873 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2874 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2875 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2876 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2877 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2878 {SBIT(GSM_CSTATE_ACTIVE),
2879 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2880 /* clearing */
2881 {SBIT(GSM_CSTATE_INITIATED),
2882 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2883 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2884 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2885 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2886 MNCC_REL_REQ, gsm48_cc_tx_release},
2887 /* special */
2888 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002889 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002890};
2891
2892#define DOWNSLLEN \
2893 (sizeof(downstatelist) / sizeof(struct downstate))
2894
2895
Harald Welte76556372010-12-22 23:57:45 +01002896int mncc_tx_to_cc(struct gsm_network *net, int msg_type, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897{
Harald Welte1a6f7982009-08-09 18:52:33 +02002898 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002899 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002900 struct gsm_subscriber_connection *conn = NULL;
Holger Hans Peter Freytherb56a6bb2010-12-27 16:02:25 +01002901 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 struct gsm_mncc *data = arg, rel;
2903
Harald Welte04dc88f2010-12-22 21:45:05 +01002904 DEBUGP(DMNCC, "receive message %s\n", get_mncc_name(msg_type));
2905
Harald Welte4bfdfe72009-06-10 23:11:52 +08002906 /* handle special messages */
2907 switch(msg_type) {
2908 case MNCC_BRIDGE:
2909 return tch_bridge(net, arg);
2910 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002911 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002913 return tch_recv_mncc(net, data->callref, 1);
2914 case GSM_TCHF_FRAME:
Andreas Eversbergd074f8f2013-12-06 16:59:10 +01002915 case GSM_TCHF_FRAME_EFR:
Harald Welteda7ab742009-12-19 22:23:05 +01002916 /* Find callref */
2917 trans = trans_find_by_callref(net, data->callref);
Harald Welte04dc88f2010-12-22 21:45:05 +01002918 if (!trans) {
2919 LOGP(DMNCC, LOGL_ERROR, "TCH frame for non-existing trans\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002920 return -EIO;
Harald Welte04dc88f2010-12-22 21:45:05 +01002921 }
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002922 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte04dc88f2010-12-22 21:45:05 +01002923 if (!trans->conn) {
2924 LOGP(DMNCC, LOGL_NOTICE, "TCH frame for trans without conn\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002925 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002926 }
2927 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F) {
2928 /* This should be LOGL_ERROR or NOTICE, but
2929 * unfortuantely it happens for a couple of frames at
2930 * the beginning of every RTP connection */
2931 LOGP(DMNCC, LOGL_DEBUG, "TCH frame for lchan != TCH_F\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002932 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002933 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002934 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002935 switch (bts->type) {
2936 case GSM_BTS_TYPE_NANOBTS:
Harald Weltef383aa12012-07-02 19:51:55 +02002937 case GSM_BTS_TYPE_OSMO_SYSMO:
Harald Welte04dc88f2010-12-22 21:45:05 +01002938 if (!trans->conn->lchan->abis_ip.rtp_socket) {
Harald Welte91c59c82010-12-24 12:40:21 +01002939 DEBUGP(DMNCC, "TCH frame to lchan without RTP connection\n");
Harald Welteda7ab742009-12-19 22:23:05 +01002940 return 0;
Harald Welte04dc88f2010-12-22 21:45:05 +01002941 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002942 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002943 case GSM_BTS_TYPE_BS11:
Harald Weltec76fb5d2011-03-20 06:27:31 -03002944 case GSM_BTS_TYPE_RBS2000:
Harald Welte10456972011-08-05 20:11:18 +02002945 case GSM_BTS_TYPE_NOKIA_SITE:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002946 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002947 default:
Harald Weltee5215b52011-08-09 21:53:20 +02002948 LOGP(DCC, LOGL_ERROR, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01002949 }
2950 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951 }
2952
2953 memset(&rel, 0, sizeof(struct gsm_mncc));
2954 rel.callref = data->callref;
2955
2956 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002957 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002958
2959 /* Callref unknown */
2960 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002961 struct gsm_subscriber *subscr;
2962
Harald Welte4a3464c2009-07-04 10:11:24 +02002963 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2965 "Received '%s' from MNCC with "
2966 "unknown callref %d\n", data->called.number,
2967 get_mncc_name(msg_type), data->callref);
2968 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002969 return mncc_release_ind(net, NULL, data->callref,
2970 GSM48_CAUSE_LOC_PRN_S_LU,
2971 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002972 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002973 if (!data->called.number[0] && !data->imsi[0]) {
2974 DEBUGP(DCC, "(bts - trx - ts - ti) "
2975 "Received '%s' from MNCC with "
2976 "no number or IMSI\n", get_mncc_name(msg_type));
2977 /* Invalid number */
2978 return mncc_release_ind(net, NULL, data->callref,
2979 GSM48_CAUSE_LOC_PRN_S_LU,
2980 GSM48_CC_CAUSE_INV_NR_FORMAT);
2981 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002982 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002983 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002984 subscr = subscr_get_by_extension(net,
2985 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002986 else
Harald Welte9176bd42009-07-23 18:46:00 +02002987 subscr = subscr_get_by_imsi(net, data->imsi);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01002988
2989 /* update the subscriber we deal with */
2990 log_set_context(BSC_CTX_SUBSCR, subscr);
2991
Harald Welte4bfdfe72009-06-10 23:11:52 +08002992 /* If subscriber is not found */
2993 if (!subscr) {
2994 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2995 "Received '%s' from MNCC with "
2996 "unknown subscriber %s\n", data->called.number,
2997 get_mncc_name(msg_type), data->called.number);
2998 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002999 return mncc_release_ind(net, NULL, data->callref,
3000 GSM48_CAUSE_LOC_PRN_S_LU,
3001 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003002 }
3003 /* If subscriber is not "attached" */
3004 if (!subscr->lac) {
3005 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
3006 "Received '%s' from MNCC with "
3007 "detached subscriber %s\n", data->called.number,
3008 get_mncc_name(msg_type), data->called.number);
3009 subscr_put(subscr);
3010 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003011 return mncc_release_ind(net, NULL, data->callref,
3012 GSM48_CAUSE_LOC_PRN_S_LU,
3013 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003014 }
3015 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02003016 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
3017 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003018 DEBUGP(DCC, "No memory for trans.\n");
3019 subscr_put(subscr);
3020 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08003021 mncc_release_ind(net, NULL, data->callref,
3022 GSM48_CAUSE_LOC_PRN_S_LU,
3023 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024 return -ENOMEM;
3025 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003026 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003027 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003028
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003030 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003031 /* find transaction with this subscriber already paging */
3032 llist_for_each_entry(transt, &net->trans_list, entry) {
3033 /* Transaction of our lchan? */
3034 if (transt == trans ||
3035 transt->subscr != subscr)
3036 continue;
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003037 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038 "Received '%s' from MNCC with "
3039 "unallocated channel, paging already "
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003040 "started for lac %d.\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003041 data->called.number,
Holger Hans Peter Freyther8e3eb582010-12-27 16:08:34 +01003042 get_mncc_name(msg_type), subscr->lac);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003043 subscr_put(subscr);
3044 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003045 return 0;
3046 }
3047 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02003048 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003049
3050 /* Get a channel */
Holger Hans Peter Freyther49b3ed22010-12-29 17:09:07 +01003051 trans->paging_request = talloc_zero(subscr->net, struct gsm_network*);
3052 if (!trans->paging_request) {
3053 LOGP(DCC, LOGL_ERROR, "Failed to allocate paging token.\n");
3054 subscr_put(subscr);
3055 trans_free(trans);
3056 return 0;
3057 }
3058
3059 *trans->paging_request = subscr->net;
3060 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, trans->paging_request);
Sylvain Munaut567c8dc2010-12-01 22:17:36 +01003061
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003062 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 return 0;
3064 }
3065 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003066 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01003067 subscr_put(subscr);
Holger Hans Peter Freyther249b3f32013-12-29 20:24:37 +01003068 } else {
3069 /* update the subscriber we deal with */
3070 log_set_context(BSC_CTX_SUBSCR, trans->subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003071 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003072
3073 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003074 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003075
3076 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003077 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003078 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003079 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 (trans->subscr)?(trans->subscr->extension):"-",
3081 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08003082 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
3083 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 if (msg_type == MNCC_REL_REQ)
3085 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
3086 else
3087 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
3088 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02003089 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003090 return rc;
3091 }
3092
3093 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
3094 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08003095 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003097 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02003098 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08003099 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003100
3101 /* Find function for current state and message */
3102 for (i = 0; i < DOWNSLLEN; i++)
3103 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003104 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003105 break;
3106 if (i == DOWNSLLEN) {
3107 DEBUGP(DCC, "Message unhandled at this state.\n");
3108 return 0;
3109 }
3110
3111 rc = downstatelist[i].rout(trans, arg);
3112
3113 return rc;
3114}
3115
3116
3117static struct datastate {
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003118 uint32_t states;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003119 int type;
3120 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3121} datastatelist[] = {
3122 /* mobile originating call establishment */
3123 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3124 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3125 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3126 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3127 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3128 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3129 /* mobile terminating call establishment */
3130 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3131 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3132 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3133 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003134 {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 +08003135 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3136 /* signalling during call */
3137 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3138 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3139 {SBIT(GSM_CSTATE_ACTIVE),
3140 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3141 {ALL_STATES,
3142 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3143 {ALL_STATES,
3144 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3145 {ALL_STATES,
3146 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3147 {SBIT(GSM_CSTATE_ACTIVE),
3148 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3149 {SBIT(GSM_CSTATE_ACTIVE),
3150 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3151 {SBIT(GSM_CSTATE_ACTIVE),
3152 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3153 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3154 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3155 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3156 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3157 {SBIT(GSM_CSTATE_ACTIVE),
3158 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3159 /* clearing */
3160 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3161 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3162 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3163 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3164 {ALL_STATES, /* 5.4.3.4 */
3165 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3166};
3167
3168#define DATASLLEN \
3169 (sizeof(datastatelist) / sizeof(struct datastate))
3170
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003171static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003172{
3173 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003174 uint8_t msg_type = gh->msg_type & 0xbf;
3175 uint8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003176 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003177 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003178
Harald Welte4bfdfe72009-06-10 23:11:52 +08003179 if (msg_type & 0x80) {
3180 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3181 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003182 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003183
Harald Welte4bfdfe72009-06-10 23:11:52 +08003184 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003185 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003186
Harald Welte6f5aee02009-07-23 21:21:14 +02003187 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003188 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003189 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003190 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003191 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3192 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003193
3194 /* Create transaction */
3195 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003196 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003197 "creating new trans.\n", transaction_id);
3198 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003199 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003200 transaction_id, new_callref++);
3201 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003202 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freytherb549ddf2011-01-16 18:17:04 +01003203 rc = gsm48_tx_simple(conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003204 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003205 GSM48_MT_CC_RELEASE_COMPL);
3206 return -ENOMEM;
3207 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003208 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003209 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003210 }
3211
3212 /* find function for current state and message */
3213 for (i = 0; i < DATASLLEN; i++)
3214 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003215 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003216 break;
3217 if (i == DATASLLEN) {
3218 DEBUGP(DCC, "Message unhandled at this state.\n");
3219 return 0;
3220 }
3221
Holger Hans Peter Freyther1e61b252013-07-06 11:45:38 +02003222 assert(trans->subscr);
3223
Harald Welte4bfdfe72009-06-10 23:11:52 +08003224 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003225
3226 return rc;
3227}
3228
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003229/* Create a dummy to wait five seconds */
3230static void release_anchor(struct gsm_subscriber_connection *conn)
3231{
3232 if (!conn->anch_operation)
3233 return;
3234
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003235 osmo_timer_del(&conn->anch_operation->timeout);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003236 talloc_free(conn->anch_operation);
3237 conn->anch_operation = NULL;
3238}
3239
3240static void anchor_timeout(void *_data)
3241{
3242 struct gsm_subscriber_connection *con = _data;
3243
3244 release_anchor(con);
3245 msc_release_connection(con);
3246}
3247
3248int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3249{
3250 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3251 if (!conn->anch_operation)
3252 return -1;
3253
3254 conn->anch_operation->timeout.data = conn;
3255 conn->anch_operation->timeout.cb = anchor_timeout;
Pablo Neira Ayusobf540cb2011-05-06 12:11:06 +02003256 osmo_timer_schedule(&conn->anch_operation->timeout, 5, 0);
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003257 return 0;
3258}
3259
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003260/* here we get data from the BSC level... */
3261int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003262{
3263 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherc42ad8b2011-04-18 17:04:00 +02003264 uint8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003265 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003266
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003267 if (silent_call_reroute(conn, msg))
3268 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003269
3270 switch (pdisc) {
3271 case GSM48_PDISC_CC:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003272 release_anchor(conn);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003273 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003274 break;
3275 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003276 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003277 break;
3278 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003279 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003280 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003281 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003282 release_anchor(conn);
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003283 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003284 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003285 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003286 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003287 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3288 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003289 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003290 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther02d39b22010-07-05 15:34:16 +08003291 release_anchor(conn);
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003292 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003293 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003294 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003295 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3296 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003297 break;
3298 }
3299
3300 return rc;
3301}
Harald Welte8470bf22008-12-25 23:28:35 +00003302
Harald Welte805f6442009-07-28 18:25:29 +02003303/*
3304 * This will be ran by the linker when loading the DSO. We use it to
3305 * do system initialization, e.g. registration of signal handlers.
3306 */
3307static __attribute__((constructor)) void on_dso_load_0408(void)
3308{
Pablo Neira Ayusobbc5b992011-05-06 12:12:31 +02003309 osmo_signal_register_handler(SS_ABISIP, handle_abisip_signal, NULL);
Harald Welte805f6442009-07-28 18:25:29 +02003310}