blob: d6d9b983d1ddfa322a1de7ccd61624ecacdf9afc [file] [log] [blame]
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte59b04682009-06-10 05:40:52 +08002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
4/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +08005 * (C) 2008-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte59b04682009-06-10 05:40:52 +08006 *
7 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (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
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30#include <time.h>
31#include <netinet/in.h>
32
Sylvain Munaut28c47162009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte59b04682009-06-10 05:40:52 +080034#include <openbsc/db.h>
Harald Welte59b04682009-06-10 05:40:52 +080035#include <openbsc/debug.h>
36#include <openbsc/gsm_data.h>
37#include <openbsc/gsm_subscriber.h>
38#include <openbsc/gsm_04_11.h>
39#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080040#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080041#include <openbsc/abis_rsl.h>
42#include <openbsc/chan_alloc.h>
43#include <openbsc/paging.h>
44#include <openbsc/signal.h>
45#include <openbsc/trau_frame.h>
46#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020047#include <openbsc/rtp_proxy.h>
Harald Weltec2189a62009-07-23 18:56:43 +020048#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020049#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010050#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080051#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080052#include <openbsc/osmo_msc.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080053#include <osmocore/bitvec.h>
54
55#include <osmocore/gsm48.h>
Holger Hans Peter Freythera5386d12010-09-30 18:53:47 +080056#include <osmocore/gsm0480.h>
Holger Hans Peter Freyther89253132010-09-30 18:52:23 +080057#include <osmocore/gsm_utils.h>
58#include <osmocore/msgb.h>
59#include <osmocore/talloc.h>
60#include <osmocore/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080061
Harald Welte (local)8751ee92009-08-15 02:30:58 +020062void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010063void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020064
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +080065int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080066static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +080067 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010068static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080069static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080070
71struct gsm_lai {
72 u_int16_t mcc;
73 u_int16_t mnc;
74 u_int16_t lac;
75};
76
Harald Welte03740842009-06-10 23:11:52 +080077static u_int32_t new_callref = 0x80000001;
78
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080079static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
80 struct gsm_trans *trans)
81{
82 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
83
84 /* if we get passed a transaction reference, do some common
85 * work that the caller no longer has to do */
86 if (trans) {
87 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
88 msg->lchan = trans->conn->lchan;
89 }
90
91
92 if (msg->lchan) {
93 msg->trx = msg->lchan->ts->trx;
94 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
95 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
96 "Sending '%s' to MS.\n", msg->trx->bts->nr,
97 msg->trx->nr, msg->lchan->ts->nr,
98 gh->proto_discr & 0xf0,
99 gsm48_cc_msg_name(gh->msg_type));
100 else
101 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
102 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
103 msg->trx->nr, msg->lchan->ts->nr,
104 gh->proto_discr, gh->msg_type);
105 }
106
Holger Hans Peter Freyther6bfcad82010-11-10 10:16:02 +0100107 return gsm0808_submit_dtap(conn, msg, 0, 0);
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800108}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100109
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800110int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
111{
112 struct gsm48_hdr *gh;
113 struct msgb *ss_notify;
114
115 ss_notify = gsm0480_create_notifySS(message);
116 if (!ss_notify)
117 return -1;
118
119 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
120 uint8_t *data = msgb_push(ss_notify, 1);
121 data[0] = ss_notify->len - 1;
122 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
123 gh->msg_type = GSM48_MT_CC_FACILITY;
124 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
125}
126
Sylvain Munaut28c47162009-12-24 00:27:26 +0100127static void release_security_operation(struct gsm_subscriber_connection *conn)
128{
129 if (!conn->sec_operation)
130 return;
131
132 talloc_free(conn->sec_operation);
133 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800134 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100135}
136
137static void allocate_security_operation(struct gsm_subscriber_connection *conn)
138{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100139 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
140 struct gsm_security_operation);
141}
142
Sylvain Munautc0bdbb42010-12-01 22:37:05 +0100143int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
144 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100145{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800146 struct gsm_network *net = conn->bts->network;
147 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100148 struct gsm_security_operation *op;
149 struct gsm_auth_tuple atuple;
150 int status = -1, rc;
151
152 /* Check if we _can_ enable encryption. Cases where we can't:
153 * - Encryption disabled in config
154 * - Channel already secured (nothing to do)
155 * - Subscriber equipment doesn't support configured encryption
156 */
157 if (!net->a5_encryption) {
158 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800159 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100160 DEBUGP(DMM, "Requesting to secure an already secure channel");
161 status = GSM_SECURITY_SUCCEEDED;
162 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
163 net->a5_encryption)) {
164 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
165 status = GSM_SECURITY_NOAVAIL;
166 }
167
168 /* If not done yet, try to get info for this user */
169 if (status < 0) {
170 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
171 if (rc <= 0)
172 status = GSM_SECURITY_NOAVAIL;
173 }
174
175 /* Are we done yet ? */
176 if (status >= 0)
177 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800178 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100179 0;
180
181 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800182 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100183 return -EBUSY;
184
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800185 allocate_security_operation(conn);
186 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100187 op->cb = cb;
188 op->cb_data = cb_data;
189 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
190
191 /* FIXME: Should start a timer for completion ... */
192
193 /* Then do whatever is needed ... */
Harald Welte857e15d2010-12-23 18:07:49 +0100194 if (rc == AUTH_DO_AUTH_THAN_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100195 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800196 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Harald Welte857e15d2010-12-23 18:07:49 +0100197 } else if (rc == AUTH_DO_CIPH) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100198 /* Start ciphering directly */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100199 return gsm0808_cipher_mode(conn, net->a5_encryption,
200 op->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100201 }
202
203 return -EINVAL; /* not reached */
204}
205
Harald Welte59b04682009-06-10 05:40:52 +0800206static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
207 struct gsm_subscriber *subscriber)
208{
209 if (!subscriber)
210 return 0;
211
212 /*
213 * Do not send accept yet as more information should arrive. Some
214 * phones will not send us the information and we will have to check
215 * what we want to do with that.
216 */
217 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
218 return 0;
219
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200220 switch (subscriber->net->auth_policy) {
221 case GSM_AUTH_POLICY_CLOSED:
222 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200223 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200224 if (subscriber->authorized)
225 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200226 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200227 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800228 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200229 default:
230 return 0;
231 }
Harald Welte59b04682009-06-10 05:40:52 +0800232}
233
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100234static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800235{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100236 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800237 return;
238
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800239 /* No need to keep the connection up */
240 release_anchor(conn);
241
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100242 bsc_del_timer(&conn->loc_operation->updating_timer);
243 talloc_free(conn->loc_operation);
Holger Hans Peter Freytherbed07092010-12-22 08:53:28 +0100244 conn->loc_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800245 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800246}
247
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100248static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800249{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800250 if (conn->loc_operation)
251 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100252 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800253
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100254 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200255 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800256}
257
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100258static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
259 struct msgb *msg, void *data, void *param)
260{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800261 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100262 int rc = 0;
263
264 switch (event) {
265 case GSM_SECURITY_AUTH_FAILED:
266 release_loc_updating_req(conn);
267 break;
268
269 case GSM_SECURITY_NOAVAIL:
270 case GSM_SECURITY_SUCCEEDED:
271 /* We're all good */
272 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800273 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800274 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100275 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800276 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100277 }
278
279 /* call subscr_update after putting the loc_upd_acc
280 * in the transmit queue, since S_SUBSCR_ATTACHED might
281 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800282 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100283 GSM_SUBSCRIBER_UPDATE_ATTACHED);
284
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100285 /*
286 * The gsm0408_loc_upd_acc sends a MI with the TMSI. The
287 * MS needs to respond with a TMSI REALLOCATION COMPLETE
288 * (even if the TMSI is the same).
289 */
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100290 break;
291
292 default:
293 rc = -EINVAL;
294 };
295
296 return rc;
297}
298
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100299static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800300{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100301 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800302 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100303 conn->loc_operation->key_seq,
304 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800305 return 0;
306}
307
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800308void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800309{
Harald Welte03740842009-06-10 23:11:52 +0800310 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800311
312 /* avoid someone issuing a clear */
313 conn->in_release = 1;
314
Harald Welte59b04682009-06-10 05:40:52 +0800315 /*
316 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800317 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800318 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800319 release_loc_updating_req(conn);
320 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800321 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800322
Harald Welte03740842009-06-10 23:11:52 +0800323 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200324 /* FIXME: this is not neccessarily the right thing to do, we should
325 * only set trans->lchan to NULL and wait for another lchan to be
326 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800327 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
328 if (trans->conn == conn)
Harald Weltec2189a62009-07-23 18:56:43 +0200329 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800330 }
Harald Welte59b04682009-06-10 05:40:52 +0800331}
332
Harald Welte59b04682009-06-10 05:40:52 +0800333/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800334int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800335{
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800336 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther8073cbd2010-06-15 19:40:05 +0800337 struct msgb *msg;
338
339 counter_inc(bts->network->stats.loc_upd_resp.reject);
340
341 msg = gsm48_create_loc_upd_rej(cause);
342 if (!msg) {
343 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
344 return -1;
345 }
Harald Welte59b04682009-06-10 05:40:52 +0800346
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800347 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800348
Harald Welte (local)38a9e5d2009-12-26 22:15:28 +0100349 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100350 "LAC=%u BTS=%u\n", conn->subscr ?
351 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800352 bts->location_area_code, bts->nr);
Harald Welte3edc5a92009-12-22 00:41:05 +0100353
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800354 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800355}
356
357/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800358int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte59b04682009-06-10 05:40:52 +0800359{
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800360 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800361 struct msgb *msg = gsm48_msgb_alloc();
362 struct gsm48_hdr *gh;
363 struct gsm48_loc_area_id *lai;
364 u_int8_t *mid;
Harald Welte59b04682009-06-10 05:40:52 +0800365
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800366 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800367
368 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
369 gh->proto_discr = GSM48_PDISC_MM;
370 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
371
372 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welted8493ac2010-03-04 10:55:40 +0100373 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte59b04682009-06-10 05:40:52 +0800374 bts->network->network_code, bts->location_area_code);
375
Holger Hans Peter Freyther36897102009-08-01 07:26:59 +0200376 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther20a0f322009-08-20 08:41:24 +0200377 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte59b04682009-06-10 05:40:52 +0800378
379 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
380
Harald Weltebdbb7442009-12-22 19:07:32 +0100381 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte3edc5a92009-12-22 00:41:05 +0100382
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800383 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800384}
385
Harald Welte59b04682009-06-10 05:40:52 +0800386/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800387static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte59b04682009-06-10 05:40:52 +0800388{
389 struct msgb *msg = gsm48_msgb_alloc();
390 struct gsm48_hdr *gh;
391
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800392 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800393
394 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
395 gh->proto_discr = GSM48_PDISC_MM;
396 gh->msg_type = GSM48_MT_MM_ID_REQ;
397 gh->data[0] = id_type;
398
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800399 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800400}
401
Harald Welte59b04682009-06-10 05:40:52 +0800402
403/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800404static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800405{
406 struct gsm48_hdr *gh = msgb_l3(msg);
407 struct gsm_lchan *lchan = msg->lchan;
Harald Welte75350412009-07-23 18:46:00 +0200408 struct gsm_bts *bts = lchan->ts->trx->bts;
409 struct gsm_network *net = bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800410 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200411 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800412
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200413 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte59b04682009-06-10 05:40:52 +0800414 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
415 mi_type, mi_string);
416
Harald Welteedf48af2009-12-13 12:39:18 +0100417 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
418
Harald Welte59b04682009-06-10 05:40:52 +0800419 switch (mi_type) {
420 case GSM_MI_TYPE_IMSI:
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200421 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100422 if (!conn->subscr) {
423 conn->subscr = subscr_get_by_imsi(net, mi_string);
424 if (!conn->subscr)
425 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbee2974032009-08-12 10:12:52 +0200426 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100427 if (conn->loc_operation)
428 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800429 break;
430 case GSM_MI_TYPE_IMEI:
431 case GSM_MI_TYPE_IMEISV:
432 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100433 if (conn->subscr) {
434 db_subscriber_assoc_imei(conn->subscr, mi_string);
435 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200436 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100437 if (conn->loc_operation)
438 conn->loc_operation->waiting_for_imei = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800439 break;
440 }
441
442 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100443 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800444}
445
446
447static void loc_upd_rej_cb(void *data)
448{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100449 struct gsm_subscriber_connection *conn = data;
450 struct gsm_lchan *lchan = conn->lchan;
Harald Welte59936d72009-11-18 20:33:19 +0100451 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800452
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800453 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800454 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800455}
456
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100457static void schedule_reject(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800458{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100459 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
460 conn->loc_operation->updating_timer.data = conn;
461 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Harald Welte59b04682009-06-10 05:40:52 +0800462}
463
464static const char *lupd_name(u_int8_t type)
465{
466 switch (type) {
467 case GSM48_LUPD_NORMAL:
468 return "NORMAL";
469 case GSM48_LUPD_PERIODIC:
470 return "PEROIDOC";
471 case GSM48_LUPD_IMSI_ATT:
472 return "IMSI ATTACH";
473 default:
474 return "UNKNOWN";
475 }
476}
477
Harald Welte59b04682009-06-10 05:40:52 +0800478/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800479static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800480{
481 struct gsm48_hdr *gh = msgb_l3(msg);
482 struct gsm48_loc_upd_req *lu;
Harald Welte03740842009-06-10 23:11:52 +0800483 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800484 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800485 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200486 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800487 int rc;
488
489 lu = (struct gsm48_loc_upd_req *) gh->data;
490
491 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
492
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200493 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800494
Harald Welte79639662009-06-27 02:58:43 +0200495 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte59b04682009-06-10 05:40:52 +0800496 lupd_name(lu->type));
497
Harald Welteedf48af2009-12-13 12:39:18 +0100498 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
499
Harald Welte3edc5a92009-12-22 00:41:05 +0100500 switch (lu->type) {
501 case GSM48_LUPD_NORMAL:
Harald Weltebdbb7442009-12-22 19:07:32 +0100502 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte3edc5a92009-12-22 00:41:05 +0100503 break;
504 case GSM48_LUPD_IMSI_ATT:
Harald Weltebdbb7442009-12-22 19:07:32 +0100505 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100506 break;
507 case GSM48_LUPD_PERIODIC:
Harald Weltebdbb7442009-12-22 19:07:32 +0100508 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte3edc5a92009-12-22 00:41:05 +0100509 break;
510 }
511
Harald Welte59b04682009-06-10 05:40:52 +0800512 /*
513 * Pseudo Spoof detection: Just drop a second/concurrent
514 * location updating request.
515 */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100516 if (conn->loc_operation) {
Harald Welte79639662009-06-27 02:58:43 +0200517 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100518 conn->loc_operation);
Holger Hans Peter Freytherb57d5562010-06-15 14:09:34 +0800519 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Harald Welte59b04682009-06-10 05:40:52 +0800520 return 0;
521 }
522
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800523 allocate_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800524
Sylvain Munaut360fc582010-06-10 13:36:59 +0200525 conn->loc_operation->key_seq = lu->key_seq;
526
Harald Welte59b04682009-06-10 05:40:52 +0800527 switch (mi_type) {
528 case GSM_MI_TYPE_IMSI:
Harald Welte79639662009-06-27 02:58:43 +0200529 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800530 /* we always want the IMEI, too */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800531 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100532 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800533
Jan Luebbe9bdbd622009-08-12 10:19:34 +0200534 /* look up subscriber based on IMSI, create if not found */
535 subscr = subscr_get_by_imsi(bts->network, mi_string);
536 if (!subscr) {
537 subscr = db_create_subscriber(bts->network, mi_string);
538 }
Harald Welte59b04682009-06-10 05:40:52 +0800539 break;
540 case GSM_MI_TYPE_TMSI:
Harald Welte79639662009-06-27 02:58:43 +0200541 DEBUGPC(DMM, "\n");
Harald Welte59b04682009-06-10 05:40:52 +0800542 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200543 subscr = subscr_get_by_tmsi(bts->network,
544 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800545 if (!subscr) {
546 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800547 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100548 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800549 }
Harald Welteca6dfc02010-06-07 17:56:32 +0200550 /* we always want the IMEI, too */
Holger Hans Peter Freythercbecea52010-06-15 14:11:01 +0800551 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Welteca6dfc02010-06-07 17:56:32 +0200552 conn->loc_operation->waiting_for_imei = 1;
Harald Welte59b04682009-06-10 05:40:52 +0800553 break;
554 case GSM_MI_TYPE_IMEI:
555 case GSM_MI_TYPE_IMEISV:
556 /* no sim card... FIXME: what to do ? */
Harald Welte79639662009-06-27 02:58:43 +0200557 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800558 break;
559 default:
Harald Welte79639662009-06-27 02:58:43 +0200560 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte59b04682009-06-10 05:40:52 +0800561 break;
562 }
563
Harald Welteccd69362009-07-04 10:18:00 +0200564 /* schedule the reject timer */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100565 schedule_reject(conn);
Harald Welteccd69362009-07-04 10:18:00 +0200566
Harald Welte03740842009-06-10 23:11:52 +0800567 if (!subscr) {
Harald Welte79639662009-06-27 02:58:43 +0200568 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte03740842009-06-10 23:11:52 +0800569 /* FIXME: request id? close channel? */
570 return -EINVAL;
571 }
572
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100573 conn->subscr = subscr;
574 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte59b04682009-06-10 05:40:52 +0800575
Harald Welteccd69362009-07-04 10:18:00 +0200576 /* check if we can let the subscriber into our network immediately
577 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100578 return gsm0408_authorize(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800579}
580
Harald Welte03740842009-06-10 23:11:52 +0800581#if 0
582static u_int8_t to_bcd8(u_int8_t val)
583{
584 return ((val / 10) << 4) | (val % 10);
585}
586#endif
587
Harald Welte59b04682009-06-10 05:40:52 +0800588/* Section 9.2.15a */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800589int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800590{
591 struct msgb *msg = gsm48_msgb_alloc();
592 struct gsm48_hdr *gh;
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800593 struct gsm_network *net = conn->bts->network;
Harald Welte59b04682009-06-10 05:40:52 +0800594 u_int8_t *ptr8;
Daniel Willmannc8188202009-08-13 03:42:07 +0200595 int name_len, name_pad;
Harald Welte03740842009-06-10 23:11:52 +0800596#if 0
597 time_t cur_t;
598 struct tm* cur_time;
599 int tz15min;
600#endif
Harald Welte59b04682009-06-10 05:40:52 +0800601
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800602 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +0800603
604 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
605 gh->proto_discr = GSM48_PDISC_MM;
606 gh->msg_type = GSM48_MT_MM_INFO;
607
608 if (net->name_long) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200609#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800610 name_len = strlen(net->name_long);
611 /* 10.5.3.5a */
612 ptr8 = msgb_put(msg, 3);
613 ptr8[0] = GSM48_IE_NAME_LONG;
614 ptr8[1] = name_len*2 +1;
615 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
616
617 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
618 for (i = 0; i < name_len; i++)
619 ptr16[i] = htons(net->name_long[i]);
620
621 /* FIXME: Use Cell Broadcast, not UCS-2, since
622 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmannc8188202009-08-13 03:42:07 +0200623#endif
624 name_len = (strlen(net->name_long)*7)/8;
625 name_pad = (8 - strlen(net->name_long)*7)%8;
626 if (name_pad > 0)
627 name_len++;
628 /* 10.5.3.5a */
629 ptr8 = msgb_put(msg, 3);
630 ptr8[0] = GSM48_IE_NAME_LONG;
631 ptr8[1] = name_len +1;
632 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
633
634 ptr8 = msgb_put(msg, name_len);
635 gsm_7bit_encode(ptr8, net->name_long);
636
Harald Welte59b04682009-06-10 05:40:52 +0800637 }
638
639 if (net->name_short) {
Daniel Willmannc8188202009-08-13 03:42:07 +0200640#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800641 name_len = strlen(net->name_short);
642 /* 10.5.3.5a */
643 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Weltef6284712009-07-19 17:51:36 +0200644 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Welte59b04682009-06-10 05:40:52 +0800645 ptr8[1] = name_len*2 + 1;
646 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
647
648 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
649 for (i = 0; i < name_len; i++)
650 ptr16[i] = htons(net->name_short[i]);
Daniel Willmannc8188202009-08-13 03:42:07 +0200651#endif
652 name_len = (strlen(net->name_short)*7)/8;
653 name_pad = (8 - strlen(net->name_short)*7)%8;
654 if (name_pad > 0)
655 name_len++;
656 /* 10.5.3.5a */
657 ptr8 = (u_int8_t *) msgb_put(msg, 3);
658 ptr8[0] = GSM48_IE_NAME_SHORT;
659 ptr8[1] = name_len +1;
660 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
661
662 ptr8 = msgb_put(msg, name_len);
663 gsm_7bit_encode(ptr8, net->name_short);
664
Harald Welte59b04682009-06-10 05:40:52 +0800665 }
666
667#if 0
Harald Welte59b04682009-06-10 05:40:52 +0800668 /* Section 10.5.3.9 */
669 cur_t = time(NULL);
Harald Welte03740842009-06-10 23:11:52 +0800670 cur_time = gmtime(&cur_t);
Harald Welte59b04682009-06-10 05:40:52 +0800671 ptr8 = msgb_put(msg, 8);
672 ptr8[0] = GSM48_IE_NET_TIME_TZ;
673 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
674 ptr8[2] = to_bcd8(cur_time->tm_mon);
675 ptr8[3] = to_bcd8(cur_time->tm_mday);
676 ptr8[4] = to_bcd8(cur_time->tm_hour);
677 ptr8[5] = to_bcd8(cur_time->tm_min);
678 ptr8[6] = to_bcd8(cur_time->tm_sec);
679 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
680 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte03740842009-06-10 23:11:52 +0800681 ptr8[7] = to_bcd8(tz15min);
Harald Welte59b04682009-06-10 05:40:52 +0800682 if (tz15min < 0)
Harald Welte03740842009-06-10 23:11:52 +0800683 ptr8[7] |= 0x80;
Harald Welte59b04682009-06-10 05:40:52 +0800684#endif
685
Daniel Willmannc8188202009-08-13 03:42:07 +0200686 DEBUGP(DMM, "-> MM INFO\n");
687
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800688 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800689}
690
Harald Welte36970f62009-08-12 22:56:50 +0200691/* Section 9.2.2 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800692int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte36970f62009-08-12 22:56:50 +0200693{
694 struct msgb *msg = gsm48_msgb_alloc();
695 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200696 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte36970f62009-08-12 22:56:50 +0200697
Sylvain Munautb1a4f282009-12-24 00:24:29 +0100698 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte36970f62009-08-12 22:56:50 +0200699
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800700 msg->lchan = conn->lchan;
Harald Welte36970f62009-08-12 22:56:50 +0200701 gh->proto_discr = GSM48_PDISC_MM;
702 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
703
Sylvain Munaut0789c592009-12-24 00:23:46 +0100704 ar->key_seq = key_seq;
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200705
Harald Welte36970f62009-08-12 22:56:50 +0200706 /* 16 bytes RAND parameters */
Harald Welte36970f62009-08-12 22:56:50 +0200707 if (rand)
Sylvain Munaut6de6dc12009-09-27 11:10:17 +0200708 memcpy(ar->rand, rand, 16);
Harald Welte36970f62009-08-12 22:56:50 +0200709
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800710 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte36970f62009-08-12 22:56:50 +0200711}
712
713/* Section 9.2.1 */
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800714int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte36970f62009-08-12 22:56:50 +0200715{
716 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800717 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte36970f62009-08-12 22:56:50 +0200718}
719
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800720static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800721{
722 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800723 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte59b04682009-06-10 05:40:52 +0800724}
725
726/* 9.2.6 CM service reject */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100727static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +0800728 enum gsm48_reject_value value)
729{
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800730 struct msgb *msg;
Harald Welte59b04682009-06-10 05:40:52 +0800731
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800732 msg = gsm48_create_mm_serv_rej(value);
733 if (!msg) {
734 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
735 return -1;
736 }
Harald Welte59b04682009-06-10 05:40:52 +0800737
Holger Hans Peter Freythera44eea52010-06-15 19:39:27 +0800738 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100739 msg->lchan = conn->lchan;
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800740 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800741}
742
Sylvain Munautbadc7112009-12-24 00:28:46 +0100743static int _gsm48_rx_mm_serv_req_sec_cb(
744 unsigned int hooknum, unsigned int event,
745 struct msgb *msg, void *data, void *param)
746{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800747 struct gsm_subscriber_connection *conn = data;
Sylvain Munautbadc7112009-12-24 00:28:46 +0100748 int rc = 0;
749
750 switch (event) {
751 case GSM_SECURITY_AUTH_FAILED:
752 /* Nothing to do */
753 break;
754
755 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800756 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautbadc7112009-12-24 00:28:46 +0100757 break;
758
759 case GSM_SECURITY_SUCCEEDED:
760 /* nothing to do. CIPHER MODE COMMAND is
761 * implicit CM SERV ACK */
762 break;
763
764 default:
765 rc = -EINVAL;
766 };
767
768 return rc;
769}
770
Harald Welte59b04682009-06-10 05:40:52 +0800771/*
772 * Handle CM Service Requests
773 * a) Verify that the packet is long enough to contain the information
774 * we require otherwsie reject with INCORRECT_MESSAGE
775 * b) Try to parse the TMSI. If we do not have one reject
776 * c) Check that we know the subscriber with the TMSI otherwise reject
777 * with a HLR cause
778 * d) Set the subscriber on the gsm_lchan and accept
779 */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800780static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800781{
782 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200783 char mi_string[GSM48_MI_SIZE];
Harald Welte59b04682009-06-10 05:40:52 +0800784
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800785 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800786 struct gsm_subscriber *subscr;
787 struct gsm48_hdr *gh = msgb_l3(msg);
788 struct gsm48_service_request *req =
789 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther05708322010-05-14 08:02:08 +0800790 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Welte59b04682009-06-10 05:40:52 +0800791 u_int8_t classmark2_len = gh->data[1];
792 u_int8_t *classmark2 = gh->data+2;
793 u_int8_t mi_len = *(classmark2 + classmark2_len);
794 u_int8_t *mi = (classmark2 + classmark2_len + 1);
795
796 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
797 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
798 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800799 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800800 GSM48_REJECT_INCORRECT_MESSAGE);
801 }
802
803 if (msg->data_len < req->mi_len + 6) {
804 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800805 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800806 GSM48_REJECT_INCORRECT_MESSAGE);
807 }
808
809 mi_type = mi[0] & GSM_MI_TYPE_MASK;
810 if (mi_type != GSM_MI_TYPE_TMSI) {
811 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800812 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800813 GSM48_REJECT_INCORRECT_MESSAGE);
814 }
815
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200816 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800817 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
818 req->cm_service_type, mi_type, mi_string);
819
Harald Welteedf48af2009-12-13 12:39:18 +0100820 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
821
Harald Welte583ceaf2009-08-10 10:12:45 +0200822 if (is_siemens_bts(bts))
823 send_siemens_mrpci(msg->lchan, classmark2-1);
824
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200825 subscr = subscr_get_by_tmsi(bts->network,
826 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800827
828 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
829 if (!subscr)
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800830 return gsm48_tx_mm_serv_rej(conn,
Harald Welte59b04682009-06-10 05:40:52 +0800831 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
832
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800833 if (!conn->subscr)
834 conn->subscr = subscr;
835 else if (conn->subscr == subscr)
Sylvain Munaut7b9852c2009-12-18 18:28:10 +0100836 subscr_put(subscr); /* lchan already has a ref, don't need another one */
837 else {
Harald Welte59b04682009-06-10 05:40:52 +0800838 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
839 subscr_put(subscr);
840 }
841
Harald Weltef6845a72009-07-05 14:08:13 +0200842 subscr->equipment.classmark2_len = classmark2_len;
843 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
844 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +0800845
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800846 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautbadc7112009-12-24 00:28:46 +0100847 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800848}
849
850static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
851{
Harald Welte75350412009-07-23 18:46:00 +0200852 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte59b04682009-06-10 05:40:52 +0800853 struct gsm48_hdr *gh = msgb_l3(msg);
854 struct gsm48_imsi_detach_ind *idi =
855 (struct gsm48_imsi_detach_ind *) gh->data;
856 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200857 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +0800858 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +0800859
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +0200860 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte59b04682009-06-10 05:40:52 +0800861 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
862 mi_type, mi_string);
863
Harald Weltebdbb7442009-12-22 19:07:32 +0100864 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte3edc5a92009-12-22 00:41:05 +0100865
Harald Welte59b04682009-06-10 05:40:52 +0800866 switch (mi_type) {
867 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +0200868 subscr = subscr_get_by_tmsi(bts->network,
869 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +0800870 break;
871 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +0200872 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +0800873 break;
874 case GSM_MI_TYPE_IMEI:
875 case GSM_MI_TYPE_IMEISV:
876 /* no sim card... FIXME: what to do ? */
877 DEBUGPC(DMM, "unimplemented mobile identity type\n");
878 break;
879 default:
880 DEBUGPC(DMM, "unknown mobile identity type\n");
881 break;
882 }
883
884 if (subscr) {
885 subscr_update(subscr, msg->trx->bts,
886 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte0747c6a2009-12-24 14:50:24 +0100887 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ced09ed2009-08-17 09:39:55 +0200888
889 subscr->equipment.classmark1 = idi->classmark1;
890 db_sync_equipment(&subscr->equipment);
891
Harald Welte59b04682009-06-10 05:40:52 +0800892 subscr_put(subscr);
893 } else
894 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
895
Harald Welte43250dd2009-12-20 09:58:40 +0100896 /* FIXME: iterate over all transactions and release them,
897 * imagine an IMSI DETACH happening during an active call! */
898
Harald Welte9ed54ff2009-12-12 21:00:48 +0100899 /* subscriber is detached: should we release lchan? */
Harald Welte59b04682009-06-10 05:40:52 +0800900 return 0;
901}
902
903static int gsm48_rx_mm_status(struct msgb *msg)
904{
905 struct gsm48_hdr *gh = msgb_l3(msg);
906
907 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
908
909 return 0;
910}
911
Sylvain Munaut28c47162009-12-24 00:27:26 +0100912/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800913static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100914{
915 struct gsm48_hdr *gh = msgb_l3(msg);
916 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800917 struct gsm_network *net = conn->bts->network;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100918
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200919 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut28c47162009-12-24 00:27:26 +0100920 hexdump(ar->sres, 4));
921
922 /* Safety check */
923 if (!conn->sec_operation) {
924 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
925 return -EIO;
926 }
927
928 /* Validate SRES */
929 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100930 int rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100931 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200932
933 DEBUGPC(DMM, "Invalid (expected %s)\n",
934 hexdump(conn->sec_operation->atuple.sres, 4));
935
Sylvain Munaut28c47162009-12-24 00:27:26 +0100936 if (cb)
937 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800938 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100939
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100940 rc = gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100941 release_security_operation(conn);
Holger Hans Peter Freyther8f351572010-12-27 10:57:56 +0100942 return rc;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100943 }
944
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200945 DEBUGPC(DMM, "OK\n");
946
Sylvain Munaut28c47162009-12-24 00:27:26 +0100947 /* Start ciphering */
Sylvain Munaut469c28f2010-11-29 08:19:04 +0100948 return gsm0808_cipher_mode(conn, net->a5_encryption,
949 conn->sec_operation->atuple.kc, 8, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100950}
951
Harald Welte59b04682009-06-10 05:40:52 +0800952/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800953static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800954{
955 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +0800956 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800957
958 switch (gh->msg_type & 0xbf) {
959 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +0200960 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800961 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800962 break;
963 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800964 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800965 break;
966 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800967 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800968 break;
969 case GSM48_MT_MM_STATUS:
970 rc = gsm48_rx_mm_status(msg);
971 break;
972 case GSM48_MT_MM_TMSI_REALL_COMPL:
973 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800974 conn->subscr ?
975 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +0800976 "unknown subscriber");
Holger Hans Peter Freytherf08a1d72010-12-22 12:11:01 +0100977 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800978 break;
979 case GSM48_MT_MM_IMSI_DETACH_IND:
980 rc = gsm48_rx_mm_imsi_detach_ind(msg);
981 break;
982 case GSM48_MT_MM_CM_REEST_REQ:
983 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
984 break;
985 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800986 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800987 break;
988 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +0100989 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte59b04682009-06-10 05:40:52 +0800990 gh->msg_type);
991 break;
992 }
993
994 return rc;
995}
996
997/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +0800998static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800999{
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001000 struct gsm_bts *bts = conn->bts;
Harald Welte59b04682009-06-10 05:40:52 +08001001 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001002 struct gsm48_pag_resp *resp;
Harald Welte59b04682009-06-10 05:40:52 +08001003 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther1e43b8d2009-08-21 05:18:21 +02001004 u_int8_t mi_type;
Holger Hans Peter Freyther1e6ef9f2009-08-19 07:54:59 +02001005 char mi_string[GSM48_MI_SIZE];
Harald Welte03740842009-06-10 23:11:52 +08001006 struct gsm_subscriber *subscr = NULL;
Harald Welte59b04682009-06-10 05:40:52 +08001007 int rc = 0;
1008
Holger Hans Peter Freythera943b2a2010-05-16 01:51:14 +08001009 resp = (struct gsm48_pag_resp *) &gh->data[0];
1010 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1011 mi_string, &mi_type);
Harald Welte59b04682009-06-10 05:40:52 +08001012 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1013 mi_type, mi_string);
Harald Welte583ceaf2009-08-10 10:12:45 +02001014
Harald Welte59b04682009-06-10 05:40:52 +08001015 switch (mi_type) {
1016 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freythercd8bacf2009-08-19 12:53:57 +02001017 subscr = subscr_get_by_tmsi(bts->network,
1018 tmsi_from_string(mi_string));
Harald Welte59b04682009-06-10 05:40:52 +08001019 break;
1020 case GSM_MI_TYPE_IMSI:
Harald Welte75350412009-07-23 18:46:00 +02001021 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte59b04682009-06-10 05:40:52 +08001022 break;
1023 }
1024
1025 if (!subscr) {
1026 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
1027 /* FIXME: request id? close channel? */
1028 return -EINVAL;
1029 }
1030 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte68b7df22009-08-08 16:03:15 +02001031 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Harald Welte59b04682009-06-10 05:40:52 +08001032
Harald Weltef6845a72009-07-05 14:08:13 +02001033 subscr->equipment.classmark2_len = *classmark2_lv;
1034 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1035 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001036
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001037 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte59b04682009-06-10 05:40:52 +08001038 return rc;
1039}
1040
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001041static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001042{
1043 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001044 struct gsm_subscriber *subscr = conn->subscr;
Harald Welte59b04682009-06-10 05:40:52 +08001045 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1046 u_int8_t cm2_len, cm3_len = 0;
1047 u_int8_t *cm2, *cm3 = NULL;
1048
1049 DEBUGP(DRR, "CLASSMARK CHANGE ");
1050
1051 /* classmark 2 */
1052 cm2_len = gh->data[0];
1053 cm2 = &gh->data[1];
1054 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1055
1056 if (payload_len > cm2_len + 1) {
1057 /* we must have a classmark3 */
1058 if (gh->data[cm2_len+1] != 0x20) {
1059 DEBUGPC(DRR, "ERR CM3 TAG\n");
1060 return -EINVAL;
1061 }
1062 if (cm2_len > 3) {
1063 DEBUGPC(DRR, "CM2 too long!\n");
1064 return -EINVAL;
1065 }
1066
1067 cm3_len = gh->data[cm2_len+2];
1068 cm3 = &gh->data[cm2_len+3];
1069 if (cm3_len > 14) {
1070 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1071 return -EINVAL;
1072 }
1073 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1074 }
1075 if (subscr) {
Harald Weltef6845a72009-07-05 14:08:13 +02001076 subscr->equipment.classmark2_len = cm2_len;
1077 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Welte59b04682009-06-10 05:40:52 +08001078 if (cm3) {
Harald Weltef6845a72009-07-05 14:08:13 +02001079 subscr->equipment.classmark3_len = cm3_len;
1080 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Welte59b04682009-06-10 05:40:52 +08001081 }
Harald Weltef6845a72009-07-05 14:08:13 +02001082 db_sync_equipment(&subscr->equipment);
Harald Welte59b04682009-06-10 05:40:52 +08001083 }
1084
Harald Welte59b04682009-06-10 05:40:52 +08001085 return 0;
1086}
1087
1088static int gsm48_rx_rr_status(struct msgb *msg)
1089{
1090 struct gsm48_hdr *gh = msgb_l3(msg);
1091
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001092 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Welte59b04682009-06-10 05:40:52 +08001093 rr_cause_name(gh->data[0]));
1094
1095 return 0;
1096}
1097
1098static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1099{
Harald Weltef9476812009-12-15 21:36:05 +01001100 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001101
Harald Weltec20bd1d2009-11-29 19:07:28 +01001102 /* This shouldn't actually end up here, as RSL treats
1103 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1104 * directly into gsm48_parse_meas_rep */
1105 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Weltef9476812009-12-15 21:36:05 +01001106 gsm48_parse_meas_rep(meas_rep, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001107
1108 return 0;
1109}
1110
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001111static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001112{
1113 struct gsm48_hdr *gh = msgb_l3(msg);
1114 u_int8_t apdu_id_flags;
1115 u_int8_t apdu_len;
1116 u_int8_t *apdu_data;
1117
1118 apdu_id_flags = gh->data[0];
1119 apdu_len = gh->data[1];
1120 apdu_data = gh->data+2;
1121
1122 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1123 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1124
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001125 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001126}
1127
Sylvain Munaut28c47162009-12-24 00:27:26 +01001128/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001129static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut28c47162009-12-24 00:27:26 +01001130{
Sylvain Munaut28c47162009-12-24 00:27:26 +01001131 gsm_cbfn *cb;
1132 int rc = 0;
1133
1134 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1135
1136 /* Safety check */
1137 if (!conn->sec_operation) {
1138 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1139 return -EIO;
1140 }
1141
1142 /* FIXME: check for MI (if any) */
1143
1144 /* Call back whatever was in progress (if anything) ... */
1145 cb = conn->sec_operation->cb;
1146 if (cb) {
1147 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +08001148 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +01001149 }
1150
1151 /* Complete the operation */
1152 release_security_operation(conn);
1153
1154 return rc;
1155}
1156
Harald Welte6720a432009-11-29 22:45:52 +01001157/* Chapter 9.1.16 Handover complete */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001158static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001159{
1160 struct gsm48_hdr *gh = msgb_l3(msg);
1161
1162 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1163 rr_cause_name(gh->data[0]));
1164
1165 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1166 /* FIXME: release old channel */
1167
1168 return 0;
1169}
1170
1171/* Chapter 9.1.17 Handover Failure */
Harald Weltec6b3c8c2009-12-14 17:51:15 +01001172static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welte6720a432009-11-29 22:45:52 +01001173{
1174 struct gsm48_hdr *gh = msgb_l3(msg);
1175
1176 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1177 rr_cause_name(gh->data[0]));
1178
1179 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1180 /* FIXME: release allocated new channel */
1181
1182 return 0;
1183}
Harald Welte (local)b5103922009-08-15 23:32:44 +02001184
Harald Welte59b04682009-06-10 05:40:52 +08001185/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001186static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08001187{
1188 struct gsm48_hdr *gh = msgb_l3(msg);
1189 int rc = 0;
1190
1191 switch (gh->msg_type) {
1192 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001193 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001194 break;
1195 case GSM48_MT_RR_GPRS_SUSP_REQ:
1196 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1197 break;
1198 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freyther0b83e542010-06-21 10:46:44 +08001199 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08001200 break;
Harald Welte59b04682009-06-10 05:40:52 +08001201 case GSM48_MT_RR_STATUS:
1202 rc = gsm48_rx_rr_status(msg);
1203 break;
1204 case GSM48_MT_RR_MEAS_REP:
1205 rc = gsm48_rx_rr_meas_rep(msg);
1206 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001207 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001208 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001209 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001210 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001211 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welted2dd9de2009-08-30 15:37:11 +09001212 break;
Harald Welte323f4e02009-11-29 20:02:20 +01001213 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +01001214 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001215 break;
1216 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +01001217 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001218 break;
Harald Welte59b04682009-06-10 05:40:52 +08001219 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001220 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1221 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001222 break;
1223 }
1224
1225 return rc;
1226}
1227
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001228int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +02001229 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001230{
1231 struct msgb *msg = gsm48_msgb_alloc();
1232 struct gsm48_hdr *gh;
1233
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001234 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001235
1236 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1237 apdu_id, apdu_len);
1238
1239 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1240 gh->proto_discr = GSM48_PDISC_RR;
1241 gh->msg_type = GSM48_MT_RR_APP_INFO;
1242 gh->data[0] = apdu_id;
1243 gh->data[1] = apdu_len;
1244 memcpy(gh->data+2, apdu, apdu_len);
1245
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001246 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001247}
1248
Harald Welte59b04682009-06-10 05:40:52 +08001249/* Call Control */
1250
1251/* The entire call control code is written in accordance with Figure 7.10c
1252 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1253 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1254 * it for voice */
1255
Harald Welte03740842009-06-10 23:11:52 +08001256static void new_cc_state(struct gsm_trans *trans, int state)
1257{
1258 if (state > 31 || state < 0)
1259 return;
1260
1261 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001262 gsm48_cc_state_name(trans->cc.state),
1263 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001264
Harald Weltec2189a62009-07-23 18:56:43 +02001265 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001266}
1267
1268static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001269{
1270 struct msgb *msg = gsm48_msgb_alloc();
1271 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1272 u_int8_t *cause, *call_state;
1273
Harald Welte59b04682009-06-10 05:40:52 +08001274 gh->msg_type = GSM48_MT_CC_STATUS;
1275
1276 cause = msgb_put(msg, 3);
1277 cause[0] = 2;
1278 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1279 cause[2] = 0x80 | 30; /* response to status inquiry */
1280
1281 call_state = msgb_put(msg, 1);
1282 call_state[0] = 0xc0 | 0x00;
1283
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001284 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001285}
1286
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001287static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +08001288 u_int8_t pdisc, u_int8_t msg_type)
1289{
1290 struct msgb *msg = gsm48_msgb_alloc();
1291 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1292
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001293 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001294
1295 gh->proto_discr = pdisc;
1296 gh->msg_type = msg_type;
1297
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001298 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001299}
1300
Harald Welte03740842009-06-10 23:11:52 +08001301static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1302{
Harald Weltec2189a62009-07-23 18:56:43 +02001303 if (bsc_timer_pending(&trans->cc.timer)) {
1304 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1305 bsc_del_timer(&trans->cc.timer);
1306 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001307 }
1308}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001309
Harald Welte03740842009-06-10 23:11:52 +08001310static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1311 int msg_type, struct gsm_mncc *mncc)
1312{
1313 struct msgb *msg;
1314
1315 if (trans)
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001316 if (trans->conn)
Harald Welte4861c822009-07-23 21:21:14 +02001317 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001318 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001319 trans->conn->lchan->ts->trx->bts->nr,
1320 trans->conn->lchan->ts->trx->nr,
1321 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001322 (trans->subscr)?(trans->subscr->extension):"-",
1323 get_mncc_name(msg_type));
1324 else
1325 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1326 "Sending '%s' to MNCC.\n",
1327 (trans->subscr)?(trans->subscr->extension):"-",
1328 get_mncc_name(msg_type));
1329 else
1330 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1331 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1332
1333 mncc->msg_type = msg_type;
1334
Harald Welte9cfc9352009-06-26 19:39:35 +02001335 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001336 if (!msg)
1337 return -ENOMEM;
1338 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
Harald Welte26e58862010-12-22 19:02:27 +01001339 mncc_upq_enqueue(net, msg);
Harald Welte03740842009-06-10 23:11:52 +08001340
1341 return 0;
1342}
1343
1344int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1345 u_int32_t callref, int location, int value)
1346{
1347 struct gsm_mncc rel;
1348
Harald Weltecb0595f2009-06-12 01:54:08 +08001349 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001350 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001351 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001352 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1353}
1354
Harald Weltec2189a62009-07-23 18:56:43 +02001355/* Call Control Specific transaction release.
1356 * gets called by trans_free, DO NOT CALL YOURSELF! */
1357void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001358{
Harald Welte03740842009-06-10 23:11:52 +08001359 gsm48_stop_cc_timer(trans);
1360
1361 /* send release to L4, if callref still exists */
1362 if (trans->callref) {
1363 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001364 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001365 GSM48_CAUSE_LOC_PRN_S_LU,
1366 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001367 }
Harald Weltec2189a62009-07-23 18:56:43 +02001368 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001369 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001370 if (trans->conn)
1371 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001372}
1373
1374static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001375
Harald Welte59b04682009-06-10 05:40:52 +08001376/* call-back from paging the B-end of the connection */
1377static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001378 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001379{
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001380 struct gsm_subscriber_connection *conn = _conn;
Harald Welte03740842009-06-10 23:11:52 +08001381 struct gsm_subscriber *subscr = param;
1382 struct gsm_trans *transt, *tmp;
1383 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001384
Harald Welte59b04682009-06-10 05:40:52 +08001385 if (hooknum != GSM_HOOK_RR_PAGING)
1386 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001387
Harald Welte03740842009-06-10 23:11:52 +08001388 if (!subscr)
1389 return -EINVAL;
1390 net = subscr->net;
1391 if (!net) {
1392 DEBUGP(DCC, "Error Network not set!\n");
1393 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001394 }
1395
Harald Welte03740842009-06-10 23:11:52 +08001396 /* check all tranactions (without lchan) for subscriber */
1397 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001398 if (transt->subscr != subscr || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001399 continue;
1400 switch (event) {
1401 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001402 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001403 break;
1404 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1405 subscr->extension);
1406 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001407 if (!transt->conn) {
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001408 transt->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08001409 }
1410 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001411 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001412 break;
1413 case GSM_PAGING_EXPIRED:
Holger Hans Peter Freytherab2a9332010-12-23 18:19:17 +01001414 case GSM_PAGING_BUSY:
Harald Welte03740842009-06-10 23:11:52 +08001415 DEBUGP(DCC, "Paging subscr %s expired!\n",
1416 subscr->extension);
1417 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001418 mncc_release_ind(transt->subscr->net, transt,
1419 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001420 GSM48_CAUSE_LOC_PRN_S_LU,
1421 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001422 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001423 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001424 break;
1425 }
1426 }
Harald Welte59b04682009-06-10 05:40:52 +08001427 return 0;
1428}
1429
Harald Welte3971ad52009-12-19 22:23:05 +01001430static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1431
Harald Welte3c062072009-07-28 18:25:29 +02001432/* some other part of the code sends us a signal */
1433static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1434 void *handler_data, void *signal_data)
1435{
1436 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001437 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001438 struct gsm_network *net;
1439 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001440
1441 if (subsys != SS_ABISIP)
1442 return 0;
1443
1444 /* in case we use direct BTS-to-BTS RTP */
1445 if (ipacc_rtp_direct)
1446 return 0;
1447
Harald Welte3c062072009-07-28 18:25:29 +02001448 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001449 case S_ABISIP_CRCX_ACK:
Harald Welte3971ad52009-12-19 22:23:05 +01001450 /* check if any transactions on this lchan still have
1451 * a tch_recv_mncc request pending */
1452 net = lchan->ts->trx->bts->network;
1453 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001454 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001455 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1456 tch_recv_mncc(net, trans->callref, 1);
1457 }
1458 }
Harald Welte3c062072009-07-28 18:25:29 +02001459 break;
Harald Welte3c062072009-07-28 18:25:29 +02001460 }
1461
1462 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001463}
1464
Harald Welte59b04682009-06-10 05:40:52 +08001465/* map two ipaccess RTP streams onto each other */
1466static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1467{
1468 struct gsm_bts *bts = lchan->ts->trx->bts;
1469 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001470 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001471
1472 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1473 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1474 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1475
1476 if (bts->type != remote_bts->type) {
1477 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1478 return -EINVAL;
1479 }
Harald Welte3971ad52009-12-19 22:23:05 +01001480
1481 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001482 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001483 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001484 if (!ipacc_rtp_direct) {
1485 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001486 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001487 if (rc < 0)
1488 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001489 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001490#warning do we need a check of rc here?
Harald Welte3c062072009-07-28 18:25:29 +02001491
1492 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301493 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1494 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001495 } else {
1496 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301497 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1498 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301499 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001500 if (rc < 0)
1501 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301502 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1503 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301504 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001505 }
Harald Welte59b04682009-06-10 05:40:52 +08001506 break;
1507 case GSM_BTS_TYPE_BS11:
1508 trau_mux_map_lchan(lchan, remote_lchan);
1509 break;
1510 default:
1511 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001512 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001513 }
1514
1515 return 0;
1516}
1517
Harald Welte03740842009-06-10 23:11:52 +08001518/* bridge channels of two transactions */
1519static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001520{
Harald Weltec2189a62009-07-23 18:56:43 +02001521 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1522 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001523
Harald Welte03740842009-06-10 23:11:52 +08001524 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001525 return -EIO;
1526
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001527 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001528 return -EIO;
1529
1530 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001531 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001532}
1533
Harald Welte3971ad52009-12-19 22:23:05 +01001534/* enable receive of channels to MNCC upqueue */
1535static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001536{
1537 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001538 struct gsm_lchan *lchan;
1539 struct gsm_bts *bts;
1540 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001541
Harald Welte03740842009-06-10 23:11:52 +08001542 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001543 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001544 if (!trans)
1545 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001546 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001547 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001548 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001549 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001550
Harald Welte3971ad52009-12-19 22:23:05 +01001551 switch (bts->type) {
1552 case GSM_BTS_TYPE_NANOBTS:
1553 if (ipacc_rtp_direct) {
1554 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1555 return -EINVAL;
1556 }
1557 /* in case, we don't have a RTP socket yet, we note this
1558 * in the transaction and try later */
1559 if (!lchan->abis_ip.rtp_socket) {
1560 trans->tch_recv = enable;
1561 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1562 return 0;
1563 }
1564 if (enable) {
1565 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001566 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001567 if (rc < 0)
1568 return rc;
1569 /* assign socket to application interface */
1570 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1571 net, callref);
1572 } else
1573 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1574 net, 0);
1575 break;
1576 case GSM_BTS_TYPE_BS11:
1577 if (enable)
1578 return trau_recv_lchan(lchan, callref);
1579 return trau_mux_unmap(NULL, callref);
1580 break;
1581 default:
1582 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1583 return -EINVAL;
1584 }
1585
1586 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001587}
1588
Harald Welte03740842009-06-10 23:11:52 +08001589static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1590{
1591 DEBUGP(DCC, "-> STATUS ENQ\n");
1592 return gsm48_cc_tx_status(trans, msg);
1593}
1594
1595static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1596static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1597
1598static void gsm48_cc_timeout(void *arg)
1599{
1600 struct gsm_trans *trans = arg;
1601 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001602 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1603 int mo_location = GSM48_CAUSE_LOC_USER;
1604 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1605 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001606 struct gsm_mncc mo_rel, l4_rel;
1607
1608 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1609 mo_rel.callref = trans->callref;
1610 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1611 l4_rel.callref = trans->callref;
1612
Harald Weltec2189a62009-07-23 18:56:43 +02001613 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001614 case 0x303:
1615 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001616 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001617 break;
1618 case 0x310:
1619 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001620 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001621 break;
1622 case 0x313:
1623 disconnect = 1;
1624 /* unknown, did not find it in the specs */
1625 break;
1626 case 0x301:
1627 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001628 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001629 break;
1630 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001631 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001632 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001633 gsm48_cc_tx_release(trans, &trans->cc.msg);
1634 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001635 break; /* stay in release state */
1636 }
Harald Weltec2189a62009-07-23 18:56:43 +02001637 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001638 return;
1639// release = 1;
1640// l4_cause = 14;
1641// break;
1642 case 0x306:
1643 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001644 mo_cause = trans->cc.msg.cause.value;
1645 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001646 break;
1647 case 0x323:
1648 disconnect = 1;
1649 break;
1650 default:
1651 release = 1;
1652 }
1653
1654 if (release && trans->callref) {
1655 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001656 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001657 l4_location, l4_cause);
1658 trans->callref = 0;
1659 }
1660
1661 if (disconnect && trans->callref) {
1662 /* process disconnect towards layer 4 */
1663 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001664 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001665 }
1666
1667 /* process disconnect towards mobile station */
1668 if (disconnect || release) {
1669 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001670 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1671 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1672 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001673 mo_rel.cause.diag_len = 3;
1674
1675 if (disconnect)
1676 gsm48_cc_tx_disconnect(trans, &mo_rel);
1677 if (release)
1678 gsm48_cc_tx_release(trans, &mo_rel);
1679 }
1680
1681}
1682
1683static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1684 int sec, int micro)
1685{
1686 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001687 trans->cc.timer.cb = gsm48_cc_timeout;
1688 trans->cc.timer.data = trans;
1689 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1690 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001691}
1692
1693static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1694{
1695 struct gsm48_hdr *gh = msgb_l3(msg);
1696 u_int8_t msg_type = gh->msg_type & 0xbf;
1697 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1698 struct tlv_parsed tp;
1699 struct gsm_mncc setup;
1700
1701 memset(&setup, 0, sizeof(struct gsm_mncc));
1702 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001703 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001704 /* emergency setup is identified by msg_type */
1705 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1706 setup.emergency = 1;
1707
1708 /* use subscriber as calling party number */
1709 if (trans->subscr) {
1710 setup.fields |= MNCC_F_CALLING;
1711 strncpy(setup.calling.number, trans->subscr->extension,
1712 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001713 strncpy(setup.imsi, trans->subscr->imsi,
1714 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001715 }
1716 /* bearer capability */
1717 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1718 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001719 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001720 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1721 }
1722 /* facility */
1723 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1724 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001725 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001726 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1727 }
1728 /* called party bcd number */
1729 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1730 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001731 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001732 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1733 }
1734 /* user-user */
1735 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1736 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001737 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001738 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1739 }
1740 /* ss-version */
1741 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1742 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001743 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001744 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1745 }
1746 /* CLIR suppression */
1747 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1748 setup.clir.sup = 1;
1749 /* CLIR invocation */
1750 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1751 setup.clir.inv = 1;
1752 /* cc cap */
1753 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1754 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001755 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001756 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1757 }
1758
Harald Welte03740842009-06-10 23:11:52 +08001759 new_cc_state(trans, GSM_CSTATE_INITIATED);
1760
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001761 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1762 subscr_name(trans->subscr), trans->subscr->extension,
1763 setup.called.number);
1764
Harald Weltec56006b2010-12-24 16:06:33 +01001765 counter_inc(trans->subscr->net->stats.call.mo_setup);
1766
Harald Welte03740842009-06-10 23:11:52 +08001767 /* indicate setup to MNCC */
Harald Welte0abe5a62009-07-23 19:06:52 +02001768 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte03740842009-06-10 23:11:52 +08001769
Harald Welteca745e22009-07-29 12:10:35 +02001770 /* MNCC code will modify the channel asynchronously, we should
1771 * ipaccess-bind only after the modification has been made to the
1772 * lchan->tch_mode */
Harald Welte03740842009-06-10 23:11:52 +08001773 return 0;
1774}
1775
1776static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001777{
1778 struct msgb *msg = gsm48_msgb_alloc();
1779 struct gsm48_hdr *gh;
Harald Welte03740842009-06-10 23:11:52 +08001780 struct gsm_mncc *setup = arg;
Harald Welte165fc332009-07-23 21:36:44 +02001781 int rc, trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001782
1783 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1784
Harald Welte03740842009-06-10 23:11:52 +08001785 /* transaction id must not be assigned */
1786 if (trans->transaction_id != 0xff) { /* unasssigned */
1787 DEBUGP(DCC, "TX Setup with assigned transaction. "
1788 "This is not allowed!\n");
1789 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001790 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001791 GSM48_CAUSE_LOC_PRN_S_LU,
1792 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001793 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001794 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001795 return rc;
1796 }
1797
1798 /* Get free transaction_id */
Harald Welte165fc332009-07-23 21:36:44 +02001799 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1800 if (trans_id < 0) {
Harald Welte03740842009-06-10 23:11:52 +08001801 /* no free transaction ID */
Harald Welte0abe5a62009-07-23 19:06:52 +02001802 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001803 GSM48_CAUSE_LOC_PRN_S_LU,
1804 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001805 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001806 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001807 return rc;
1808 }
Harald Welte165fc332009-07-23 21:36:44 +02001809 trans->transaction_id = trans_id;
Harald Welte59b04682009-06-10 05:40:52 +08001810
Harald Welte59b04682009-06-10 05:40:52 +08001811 gh->msg_type = GSM48_MT_CC_SETUP;
1812
Harald Welte03740842009-06-10 23:11:52 +08001813 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte59b04682009-06-10 05:40:52 +08001814
Harald Welte03740842009-06-10 23:11:52 +08001815 /* bearer capability */
1816 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001817 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001818 /* facility */
1819 if (setup->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001820 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte03740842009-06-10 23:11:52 +08001821 /* progress */
1822 if (setup->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001823 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte03740842009-06-10 23:11:52 +08001824 /* calling party BCD number */
1825 if (setup->fields & MNCC_F_CALLING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001826 gsm48_encode_calling(msg, &setup->calling);
Harald Welte03740842009-06-10 23:11:52 +08001827 /* called party BCD number */
1828 if (setup->fields & MNCC_F_CALLED)
Harald Weltefdc93d92010-03-07 23:40:35 +01001829 gsm48_encode_called(msg, &setup->called);
Harald Welte03740842009-06-10 23:11:52 +08001830 /* user-user */
1831 if (setup->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001832 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001833 /* redirecting party BCD number */
1834 if (setup->fields & MNCC_F_REDIRECTING)
Harald Weltefdc93d92010-03-07 23:40:35 +01001835 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte03740842009-06-10 23:11:52 +08001836 /* signal */
1837 if (setup->fields & MNCC_F_SIGNAL)
Harald Weltefdc93d92010-03-07 23:40:35 +01001838 gsm48_encode_signal(msg, setup->signal);
Harald Welte03740842009-06-10 23:11:52 +08001839
1840 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte59b04682009-06-10 05:40:52 +08001841
Harald Weltec56006b2010-12-24 16:06:33 +01001842 counter_inc(trans->subscr->net->stats.call.mt_setup);
1843
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001844 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001845}
1846
Harald Welte03740842009-06-10 23:11:52 +08001847static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1848{
1849 struct gsm48_hdr *gh = msgb_l3(msg);
1850 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1851 struct tlv_parsed tp;
1852 struct gsm_mncc call_conf;
1853
1854 gsm48_stop_cc_timer(trans);
1855 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1856
1857 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1858 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001859 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001860#if 0
1861 /* repeat */
1862 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1863 call_conf.repeat = 1;
1864 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1865 call_conf.repeat = 2;
1866#endif
1867 /* bearer capability */
1868 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1869 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001870 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001871 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1872 }
1873 /* cause */
1874 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1875 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001876 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001877 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1878 }
1879 /* cc cap */
1880 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1881 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001882 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001883 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1884 }
1885
1886 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1887
Harald Welte0abe5a62009-07-23 19:06:52 +02001888 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1889 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001890}
1891
1892static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1893{
1894 struct gsm_mncc *proceeding = arg;
1895 struct msgb *msg = gsm48_msgb_alloc();
1896 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1897
Harald Welte03740842009-06-10 23:11:52 +08001898 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1899
1900 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1901
1902 /* bearer capability */
1903 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001904 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001905 /* facility */
1906 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001907 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001908 /* progress */
1909 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001910 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001911
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001912 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001913}
1914
1915static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1916{
1917 struct gsm48_hdr *gh = msgb_l3(msg);
1918 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1919 struct tlv_parsed tp;
1920 struct gsm_mncc alerting;
1921
1922 gsm48_stop_cc_timer(trans);
1923 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1924
1925 memset(&alerting, 0, sizeof(struct gsm_mncc));
1926 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001927 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001928 /* facility */
1929 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1930 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001931 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001932 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1933 }
1934
1935 /* progress */
1936 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1937 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001938 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001939 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1940 }
1941 /* ss-version */
1942 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1943 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001944 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001945 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1946 }
1947
1948 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1949
Harald Welte0abe5a62009-07-23 19:06:52 +02001950 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1951 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001952}
1953
1954static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1955{
1956 struct gsm_mncc *alerting = arg;
1957 struct msgb *msg = gsm48_msgb_alloc();
1958 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1959
Harald Welte03740842009-06-10 23:11:52 +08001960 gh->msg_type = GSM48_MT_CC_ALERTING;
1961
1962 /* facility */
1963 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001964 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001965 /* progress */
1966 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001967 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001968 /* user-user */
1969 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001970 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001971
1972 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1973
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001974 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001975}
1976
1977static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1978{
1979 struct gsm_mncc *progress = arg;
1980 struct msgb *msg = gsm48_msgb_alloc();
1981 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1982
Harald Welte03740842009-06-10 23:11:52 +08001983 gh->msg_type = GSM48_MT_CC_PROGRESS;
1984
1985 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01001986 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08001987 /* user-user */
1988 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001989 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001990
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001991 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001992}
1993
1994static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1995{
1996 struct gsm_mncc *connect = arg;
1997 struct msgb *msg = gsm48_msgb_alloc();
1998 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1999
Harald Welte03740842009-06-10 23:11:52 +08002000 gh->msg_type = GSM48_MT_CC_CONNECT;
2001
2002 gsm48_stop_cc_timer(trans);
2003 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2004
2005 /* facility */
2006 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002007 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002008 /* progress */
2009 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002010 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002011 /* connected number */
2012 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002013 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002014 /* user-user */
2015 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002016 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002017
2018 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2019
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002020 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002021}
2022
2023static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2024{
2025 struct gsm48_hdr *gh = msgb_l3(msg);
2026 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2027 struct tlv_parsed tp;
2028 struct gsm_mncc connect;
2029
2030 gsm48_stop_cc_timer(trans);
2031
2032 memset(&connect, 0, sizeof(struct gsm_mncc));
2033 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002034 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002035 /* use subscriber as connected party number */
2036 if (trans->subscr) {
2037 connect.fields |= MNCC_F_CONNECTED;
2038 strncpy(connect.connected.number, trans->subscr->extension,
2039 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002040 strncpy(connect.imsi, trans->subscr->imsi,
2041 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002042 }
2043 /* facility */
2044 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2045 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002046 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002047 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2048 }
2049 /* user-user */
2050 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2051 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002052 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002053 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2054 }
2055 /* ss-version */
2056 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2057 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002058 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002059 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2060 }
2061
2062 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
Harald Weltec56006b2010-12-24 16:06:33 +01002063 counter_inc(trans->subscr->net->stats.call.mt_connect);
Harald Welte03740842009-06-10 23:11:52 +08002064
Harald Welte0abe5a62009-07-23 19:06:52 +02002065 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002066}
2067
2068
2069static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2070{
2071 struct gsm_mncc connect_ack;
2072
2073 gsm48_stop_cc_timer(trans);
2074
2075 new_cc_state(trans, GSM_CSTATE_ACTIVE);
Harald Weltec56006b2010-12-24 16:06:33 +01002076 counter_inc(trans->subscr->net->stats.call.mo_connect_ack);
Harald Welte03740842009-06-10 23:11:52 +08002077
2078 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2079 connect_ack.callref = trans->callref;
Harald Weltec56006b2010-12-24 16:06:33 +01002080
Harald Welte0abe5a62009-07-23 19:06:52 +02002081 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002082 &connect_ack);
2083}
2084
2085static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2086{
2087 struct msgb *msg = gsm48_msgb_alloc();
2088 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2089
Harald Welte03740842009-06-10 23:11:52 +08002090 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2091
2092 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2093
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002094 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002095}
2096
2097static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2098{
2099 struct gsm48_hdr *gh = msgb_l3(msg);
2100 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2101 struct tlv_parsed tp;
2102 struct gsm_mncc disc;
2103
2104 gsm48_stop_cc_timer(trans);
2105
2106 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2107
2108 memset(&disc, 0, sizeof(struct gsm_mncc));
2109 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002110 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002111 /* cause */
2112 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2113 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002114 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002115 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2116 }
2117 /* facility */
2118 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2119 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002120 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002121 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2122 }
2123 /* user-user */
2124 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2125 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002126 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002127 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2128 }
2129 /* ss-version */
2130 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2131 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002132 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002133 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2134 }
2135
Harald Welte0abe5a62009-07-23 19:06:52 +02002136 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002137
2138}
2139
Harald Weltebbc636a2009-06-11 14:23:20 +08002140static struct gsm_mncc_cause default_cause = {
2141 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2142 .coding = 0,
2143 .rec = 0,
2144 .rec_val = 0,
2145 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2146 .diag_len = 0,
2147 .diag = { 0 },
2148};
Harald Welte03740842009-06-10 23:11:52 +08002149
2150static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2151{
2152 struct gsm_mncc *disc = arg;
2153 struct msgb *msg = gsm48_msgb_alloc();
2154 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2155
Harald Welte03740842009-06-10 23:11:52 +08002156 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2157
2158 gsm48_stop_cc_timer(trans);
2159 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2160
2161 /* cause */
2162 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002163 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002164 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002165 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002166
2167 /* facility */
2168 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002169 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002170 /* progress */
2171 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002172 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002173 /* user-user */
2174 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002175 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002176
2177 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002178 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002179
2180 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2181
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002182 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002183}
2184
2185static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2186{
2187 struct gsm48_hdr *gh = msgb_l3(msg);
2188 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2189 struct tlv_parsed tp;
2190 struct gsm_mncc rel;
2191 int rc;
2192
2193 gsm48_stop_cc_timer(trans);
2194
2195 memset(&rel, 0, sizeof(struct gsm_mncc));
2196 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002197 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002198 /* cause */
2199 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2200 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002201 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002202 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2203 }
2204 /* facility */
2205 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2206 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002207 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002208 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2209 }
2210 /* user-user */
2211 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2212 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002213 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002214 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2215 }
2216 /* ss-version */
2217 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2218 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002219 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002220 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2221 }
2222
Harald Weltec2189a62009-07-23 18:56:43 +02002223 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002224 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002225 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002226 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002227 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002228 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002229 GSM48_MT_CC_RELEASE_COMPL);
2230 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002231 }
2232
2233 new_cc_state(trans, GSM_CSTATE_NULL);
2234
2235 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002236 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002237
2238 return rc;
2239}
2240
2241static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2242{
2243 struct gsm_mncc *rel = arg;
2244 struct msgb *msg = gsm48_msgb_alloc();
2245 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2246
Harald Welte03740842009-06-10 23:11:52 +08002247 gh->msg_type = GSM48_MT_CC_RELEASE;
2248
2249 trans->callref = 0;
2250
2251 gsm48_stop_cc_timer(trans);
2252 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2253
2254 /* cause */
2255 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002256 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002257 /* facility */
2258 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002259 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002260 /* user-user */
2261 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002262 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002263
Harald Weltec2189a62009-07-23 18:56:43 +02002264 trans->cc.T308_second = 0;
2265 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002266
Harald Weltec2189a62009-07-23 18:56:43 +02002267 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002268 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2269
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002270 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002271}
2272
2273static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2274{
2275 struct gsm48_hdr *gh = msgb_l3(msg);
2276 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2277 struct tlv_parsed tp;
2278 struct gsm_mncc rel;
2279 int rc = 0;
2280
2281 gsm48_stop_cc_timer(trans);
2282
2283 memset(&rel, 0, sizeof(struct gsm_mncc));
2284 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002285 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002286 /* cause */
2287 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2288 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002289 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002290 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2291 }
2292 /* facility */
2293 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2294 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002295 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002296 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2297 }
2298 /* user-user */
2299 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2300 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002301 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002302 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2303 }
2304 /* ss-version */
2305 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2306 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002307 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002308 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2309 }
2310
2311 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002312 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002313 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002314 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002315 MNCC_REJ_IND, &rel);
2316 break;
2317 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002318 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002319 MNCC_REL_CNF, &rel);
2320 break;
2321 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002322 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002323 MNCC_REL_IND, &rel);
2324 }
2325 }
2326
2327 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002328 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002329
2330 return rc;
2331}
2332
2333static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2334{
2335 struct gsm_mncc *rel = arg;
2336 struct msgb *msg = gsm48_msgb_alloc();
2337 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002338 int ret;
Harald Welte03740842009-06-10 23:11:52 +08002339
Harald Welte03740842009-06-10 23:11:52 +08002340 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2341
2342 trans->callref = 0;
2343
2344 gsm48_stop_cc_timer(trans);
2345
2346 /* cause */
2347 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002348 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002349 /* facility */
2350 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002351 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002352 /* user-user */
2353 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002354 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002355
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002356 ret = gsm48_conn_sendmsg(msg, trans->conn, trans);
2357
Harald Weltec2189a62009-07-23 18:56:43 +02002358 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002359
Harald Welte4a6d4ab2010-12-21 19:31:41 +01002360 return ret;
Harald Welte03740842009-06-10 23:11:52 +08002361}
2362
2363static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2364{
2365 struct gsm48_hdr *gh = msgb_l3(msg);
2366 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2367 struct tlv_parsed tp;
2368 struct gsm_mncc fac;
2369
2370 memset(&fac, 0, sizeof(struct gsm_mncc));
2371 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002372 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002373 /* facility */
2374 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2375 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002376 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002377 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2378 }
2379 /* ss-version */
2380 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2381 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002382 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002383 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2384 }
2385
Harald Welte0abe5a62009-07-23 19:06:52 +02002386 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002387}
2388
2389static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2390{
2391 struct gsm_mncc *fac = arg;
2392 struct msgb *msg = gsm48_msgb_alloc();
2393 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2394
Harald Welte03740842009-06-10 23:11:52 +08002395 gh->msg_type = GSM48_MT_CC_FACILITY;
2396
2397 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002398 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002399
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002400 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002401}
2402
2403static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2404{
2405 struct gsm_mncc hold;
2406
2407 memset(&hold, 0, sizeof(struct gsm_mncc));
2408 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002409 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002410}
2411
2412static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2413{
2414 struct msgb *msg = gsm48_msgb_alloc();
2415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2416
Harald Welte03740842009-06-10 23:11:52 +08002417 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2418
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002419 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002420}
2421
2422static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2423{
2424 struct gsm_mncc *hold_rej = arg;
2425 struct msgb *msg = gsm48_msgb_alloc();
2426 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2427
Harald Welte03740842009-06-10 23:11:52 +08002428 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2429
2430 /* cause */
2431 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002432 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002433 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002434 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002435
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002436 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002437}
2438
2439static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2440{
2441 struct gsm_mncc retrieve;
2442
2443 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2444 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002445 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2446 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2450{
2451 struct msgb *msg = gsm48_msgb_alloc();
2452 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2453
Harald Welte03740842009-06-10 23:11:52 +08002454 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2455
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002456 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002457}
2458
2459static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2460{
2461 struct gsm_mncc *retrieve_rej = arg;
2462 struct msgb *msg = gsm48_msgb_alloc();
2463 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2464
Harald Welte03740842009-06-10 23:11:52 +08002465 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2466
2467 /* cause */
2468 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002469 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002470 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002471 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002472
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002473 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002474}
2475
2476static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2477{
2478 struct gsm48_hdr *gh = msgb_l3(msg);
2479 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2480 struct tlv_parsed tp;
2481 struct gsm_mncc dtmf;
2482
2483 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2484 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002485 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002486 /* keypad facility */
2487 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2488 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002489 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002490 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2491 }
2492
Harald Welte0abe5a62009-07-23 19:06:52 +02002493 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2497{
2498 struct gsm_mncc *dtmf = arg;
2499 struct msgb *msg = gsm48_msgb_alloc();
2500 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2501
Harald Welte03740842009-06-10 23:11:52 +08002502 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2503
2504 /* keypad */
2505 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002506 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002507
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002508 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002509}
2510
2511static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2512{
2513 struct gsm_mncc *dtmf = arg;
2514 struct msgb *msg = gsm48_msgb_alloc();
2515 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2516
Harald Welte03740842009-06-10 23:11:52 +08002517 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2518
2519 /* cause */
2520 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002521 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002522 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002523 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002524
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002525 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002526}
2527
2528static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2529{
2530 struct msgb *msg = gsm48_msgb_alloc();
2531 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2532
Harald Welte03740842009-06-10 23:11:52 +08002533 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2534
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002535 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002536}
2537
2538static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2539{
2540 struct gsm_mncc dtmf;
2541
2542 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2543 dtmf.callref = trans->callref;
2544
Harald Welte0abe5a62009-07-23 19:06:52 +02002545 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002546}
2547
2548static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2549{
2550 struct gsm48_hdr *gh = msgb_l3(msg);
2551 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2552 struct tlv_parsed tp;
2553 struct gsm_mncc modify;
2554
2555 memset(&modify, 0, sizeof(struct gsm_mncc));
2556 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002557 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002558 /* bearer capability */
2559 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2560 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002561 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002562 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2563 }
2564
2565 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2566
Harald Welte0abe5a62009-07-23 19:06:52 +02002567 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002568}
2569
2570static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2571{
2572 struct gsm_mncc *modify = arg;
2573 struct msgb *msg = gsm48_msgb_alloc();
2574 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2575
Harald Welte03740842009-06-10 23:11:52 +08002576 gh->msg_type = GSM48_MT_CC_MODIFY;
2577
2578 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2579
2580 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002581 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002582
2583 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2584
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002585 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002586}
2587
2588static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2589{
2590 struct gsm48_hdr *gh = msgb_l3(msg);
2591 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2592 struct tlv_parsed tp;
2593 struct gsm_mncc modify;
2594
2595 gsm48_stop_cc_timer(trans);
2596
2597 memset(&modify, 0, sizeof(struct gsm_mncc));
2598 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002599 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002600 /* bearer capability */
2601 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2602 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002603 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002604 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2605 }
2606
2607 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2608
Harald Welte0abe5a62009-07-23 19:06:52 +02002609 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002610}
2611
2612static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2613{
2614 struct gsm_mncc *modify = arg;
2615 struct msgb *msg = gsm48_msgb_alloc();
2616 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2617
Harald Welte03740842009-06-10 23:11:52 +08002618 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2619
2620 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002621 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002622
2623 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2624
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002625 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002626}
2627
2628static int gsm48_cc_rx_modify_reject(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 gsm48_stop_cc_timer(trans);
2636
2637 memset(&modify, 0, sizeof(struct gsm_mncc));
2638 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002639 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002640 /* bearer capability */
2641 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2642 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002643 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002644 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2645 }
2646 /* cause */
2647 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2648 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002649 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002650 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2651 }
2652
2653 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2654
Harald Welte0abe5a62009-07-23 19:06:52 +02002655 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002656}
2657
2658static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2659{
2660 struct gsm_mncc *modify = arg;
2661 struct msgb *msg = gsm48_msgb_alloc();
2662 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2663
Harald Welte03740842009-06-10 23:11:52 +08002664 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2665
2666 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002667 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002668 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002669 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002670
2671 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2672
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002673 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002674}
2675
2676static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2677{
2678 struct gsm_mncc *notify = arg;
2679 struct msgb *msg = gsm48_msgb_alloc();
2680 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2681
Harald Welte03740842009-06-10 23:11:52 +08002682 gh->msg_type = GSM48_MT_CC_NOTIFY;
2683
2684 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002685 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002686
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002687 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002688}
2689
2690static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2691{
2692 struct gsm48_hdr *gh = msgb_l3(msg);
2693 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2694// struct tlv_parsed tp;
2695 struct gsm_mncc notify;
2696
2697 memset(&notify, 0, sizeof(struct gsm_mncc));
2698 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002699// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002700 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002701 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002702
Harald Welte0abe5a62009-07-23 19:06:52 +02002703 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002704}
2705
2706static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2707{
2708 struct gsm_mncc *user = arg;
2709 struct msgb *msg = gsm48_msgb_alloc();
2710 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2711
Harald Welte03740842009-06-10 23:11:52 +08002712 gh->msg_type = GSM48_MT_CC_USER_INFO;
2713
2714 /* user-user */
2715 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002716 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002717 /* more data */
2718 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002719 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002720
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002721 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002722}
2723
2724static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2725{
2726 struct gsm48_hdr *gh = msgb_l3(msg);
2727 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2728 struct tlv_parsed tp;
2729 struct gsm_mncc user;
2730
2731 memset(&user, 0, sizeof(struct gsm_mncc));
2732 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002733 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002734 /* user-user */
2735 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2736 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002737 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002738 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2739 }
2740 /* more data */
2741 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2742 user.more = 1;
2743
Harald Welte0abe5a62009-07-23 19:06:52 +02002744 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002745}
2746
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002747static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002748{
2749 struct gsm_mncc *mode = arg;
2750
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002751 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08002752}
2753
2754static struct downstate {
2755 u_int32_t states;
2756 int type;
2757 int (*rout) (struct gsm_trans *trans, void *arg);
2758} downstatelist[] = {
2759 /* mobile originating call establishment */
2760 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2761 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2762 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2763 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2764 {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 */
2765 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2766 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2767 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2768 /* mobile terminating call establishment */
2769 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2770 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2771 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2772 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2773 /* signalling during call */
2774 {SBIT(GSM_CSTATE_ACTIVE),
2775 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2776 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2777 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2778 {ALL_STATES,
2779 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2780 {ALL_STATES,
2781 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2782 {ALL_STATES,
2783 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2784 {SBIT(GSM_CSTATE_ACTIVE),
2785 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2786 {SBIT(GSM_CSTATE_ACTIVE),
2787 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2788 {SBIT(GSM_CSTATE_ACTIVE),
2789 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2790 {SBIT(GSM_CSTATE_ACTIVE),
2791 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2792 {SBIT(GSM_CSTATE_ACTIVE),
2793 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2794 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2795 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2796 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2797 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2798 {SBIT(GSM_CSTATE_ACTIVE),
2799 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2800 /* clearing */
2801 {SBIT(GSM_CSTATE_INITIATED),
2802 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2803 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2804 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2805 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2806 MNCC_REL_REQ, gsm48_cc_tx_release},
2807 /* special */
2808 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002809 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002810};
2811
2812#define DOWNSLLEN \
2813 (sizeof(downstatelist) / sizeof(struct downstate))
2814
2815
2816int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2817{
Harald Welteaa60edb2009-08-09 18:52:33 +02002818 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002819 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002820 struct gsm_subscriber_connection *conn = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002821 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002822 struct gsm_mncc *data = arg, rel;
2823
2824 /* handle special messages */
2825 switch(msg_type) {
2826 case MNCC_BRIDGE:
2827 return tch_bridge(net, arg);
2828 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002829 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002830 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002831 return tch_recv_mncc(net, data->callref, 1);
2832 case GSM_TCHF_FRAME:
2833 /* Find callref */
2834 trans = trans_find_by_callref(net, data->callref);
2835 if (!trans)
2836 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002837 if (!trans->conn)
Harald Welte3971ad52009-12-19 22:23:05 +01002838 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002839 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welte3971ad52009-12-19 22:23:05 +01002840 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002841 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002842 switch (bts->type) {
2843 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002844 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welte3971ad52009-12-19 22:23:05 +01002845 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002846 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002847 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002848 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002849 default:
2850 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2851 }
2852 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002853 }
2854
2855 memset(&rel, 0, sizeof(struct gsm_mncc));
2856 rel.callref = data->callref;
2857
2858 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002859 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002860
2861 /* Callref unknown */
2862 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002863 struct gsm_subscriber *subscr;
2864
Harald Welte6e1536e2009-07-04 10:11:24 +02002865 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002866 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2867 "Received '%s' from MNCC with "
2868 "unknown callref %d\n", data->called.number,
2869 get_mncc_name(msg_type), data->callref);
2870 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002871 return mncc_release_ind(net, NULL, data->callref,
2872 GSM48_CAUSE_LOC_PRN_S_LU,
2873 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002874 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002875 if (!data->called.number[0] && !data->imsi[0]) {
2876 DEBUGP(DCC, "(bts - trx - ts - ti) "
2877 "Received '%s' from MNCC with "
2878 "no number or IMSI\n", get_mncc_name(msg_type));
2879 /* Invalid number */
2880 return mncc_release_ind(net, NULL, data->callref,
2881 GSM48_CAUSE_LOC_PRN_S_LU,
2882 GSM48_CC_CAUSE_INV_NR_FORMAT);
2883 }
Harald Welte03740842009-06-10 23:11:52 +08002884 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002885 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002886 subscr = subscr_get_by_extension(net,
2887 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002888 else
Harald Welte75350412009-07-23 18:46:00 +02002889 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002890 /* If subscriber is not found */
2891 if (!subscr) {
2892 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2893 "Received '%s' from MNCC with "
2894 "unknown subscriber %s\n", data->called.number,
2895 get_mncc_name(msg_type), data->called.number);
2896 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002897 return mncc_release_ind(net, NULL, data->callref,
2898 GSM48_CAUSE_LOC_PRN_S_LU,
2899 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002900 }
2901 /* If subscriber is not "attached" */
2902 if (!subscr->lac) {
2903 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2904 "Received '%s' from MNCC with "
2905 "detached subscriber %s\n", data->called.number,
2906 get_mncc_name(msg_type), data->called.number);
2907 subscr_put(subscr);
2908 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002909 return mncc_release_ind(net, NULL, data->callref,
2910 GSM48_CAUSE_LOC_PRN_S_LU,
2911 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002912 }
2913 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002914 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2915 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002916 DEBUGP(DCC, "No memory for trans.\n");
2917 subscr_put(subscr);
2918 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002919 mncc_release_ind(net, NULL, data->callref,
2920 GSM48_CAUSE_LOC_PRN_S_LU,
2921 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002922 return -ENOMEM;
2923 }
Harald Welte03740842009-06-10 23:11:52 +08002924 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002925 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002926
Harald Welte03740842009-06-10 23:11:52 +08002927 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002928 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002929 /* find transaction with this subscriber already paging */
2930 llist_for_each_entry(transt, &net->trans_list, entry) {
2931 /* Transaction of our lchan? */
2932 if (transt == trans ||
2933 transt->subscr != subscr)
2934 continue;
2935 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2936 "Received '%s' from MNCC with "
2937 "unallocated channel, paging already "
2938 "started.\n", bts->nr,
2939 data->called.number,
2940 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002941 subscr_put(subscr);
2942 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002943 return 0;
2944 }
2945 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002946 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Sylvain Munaut892f6952010-12-01 22:17:36 +01002947
2948 /* Get a channel */
2949 subscr_get_channel(subscr, RSL_CHANNEED_TCH_F, setup_trig_pag_evt, subscr);
2950
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002951 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002952 return 0;
2953 }
2954 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002955 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002956 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002957 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002958
2959 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002960 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08002961
2962 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002963 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002964 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02002965 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08002966 (trans->subscr)?(trans->subscr->extension):"-",
2967 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08002968 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2969 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08002970 if (msg_type == MNCC_REL_REQ)
2971 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2972 else
2973 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2974 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002975 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002976 return rc;
2977 }
2978
2979 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2980 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002981 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08002982 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002983 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02002984 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08002985 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08002986
2987 /* Find function for current state and message */
2988 for (i = 0; i < DOWNSLLEN; i++)
2989 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02002990 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08002991 break;
2992 if (i == DOWNSLLEN) {
2993 DEBUGP(DCC, "Message unhandled at this state.\n");
2994 return 0;
2995 }
2996
2997 rc = downstatelist[i].rout(trans, arg);
2998
2999 return rc;
3000}
3001
3002
3003static struct datastate {
3004 u_int32_t states;
3005 int type;
3006 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
3007} datastatelist[] = {
3008 /* mobile originating call establishment */
3009 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3010 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3011 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3012 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3013 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3014 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3015 /* mobile terminating call establishment */
3016 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3017 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3018 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3019 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003020 {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 Welte03740842009-06-10 23:11:52 +08003021 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3022 /* signalling during call */
3023 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3024 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3025 {SBIT(GSM_CSTATE_ACTIVE),
3026 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3027 {ALL_STATES,
3028 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3029 {ALL_STATES,
3030 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3031 {ALL_STATES,
3032 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3033 {SBIT(GSM_CSTATE_ACTIVE),
3034 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3035 {SBIT(GSM_CSTATE_ACTIVE),
3036 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3037 {SBIT(GSM_CSTATE_ACTIVE),
3038 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3039 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3040 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3041 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3042 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3043 {SBIT(GSM_CSTATE_ACTIVE),
3044 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3045 /* clearing */
3046 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3047 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3048 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3049 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3050 {ALL_STATES, /* 5.4.3.4 */
3051 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3052};
3053
3054#define DATASLLEN \
3055 (sizeof(datastatelist) / sizeof(struct datastate))
3056
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003057static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003058{
3059 struct gsm48_hdr *gh = msgb_l3(msg);
3060 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003061 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003062 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003063 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003064
Harald Welte03740842009-06-10 23:11:52 +08003065 if (msg_type & 0x80) {
3066 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3067 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003068 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003069
Harald Welte03740842009-06-10 23:11:52 +08003070 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003071 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003072
Harald Welte4861c822009-07-23 21:21:14 +02003073 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003074 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003075 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003076 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003077 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3078 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003079
3080 /* Create transaction */
3081 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003082 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003083 "creating new trans.\n", transaction_id);
3084 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003085 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003086 transaction_id, new_callref++);
3087 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003088 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08003089 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02003090 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003091 GSM48_MT_CC_RELEASE_COMPL);
3092 return -ENOMEM;
3093 }
Harald Welte03740842009-06-10 23:11:52 +08003094 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003095 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003096 }
3097
3098 /* find function for current state and message */
3099 for (i = 0; i < DATASLLEN; i++)
3100 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003101 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003102 break;
3103 if (i == DATASLLEN) {
3104 DEBUGP(DCC, "Message unhandled at this state.\n");
3105 return 0;
3106 }
3107
3108 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003109
3110 return rc;
3111}
3112
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003113/* Create a dummy to wait five seconds */
3114static void release_anchor(struct gsm_subscriber_connection *conn)
3115{
3116 if (!conn->anch_operation)
3117 return;
3118
3119 bsc_del_timer(&conn->anch_operation->timeout);
3120 talloc_free(conn->anch_operation);
3121 conn->anch_operation = NULL;
3122}
3123
3124static void anchor_timeout(void *_data)
3125{
3126 struct gsm_subscriber_connection *con = _data;
3127
3128 release_anchor(con);
3129 msc_release_connection(con);
3130}
3131
3132int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3133{
3134 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3135 if (!conn->anch_operation)
3136 return -1;
3137
3138 conn->anch_operation->timeout.data = conn;
3139 conn->anch_operation->timeout.cb = anchor_timeout;
3140 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3141 return 0;
3142}
3143
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003144/* here we get data from the BSC level... */
3145int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003146{
3147 struct gsm48_hdr *gh = msgb_l3(msg);
3148 u_int8_t pdisc = gh->proto_discr & 0x0f;
3149 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003150
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003151 if (silent_call_reroute(conn, msg))
3152 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003153
3154 switch (pdisc) {
3155 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003156 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003157 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003158 break;
3159 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003160 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003161 break;
3162 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003163 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003164 break;
3165 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003166 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003167 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003168 break;
3169 case GSM48_PDISC_MM_GPRS:
3170 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003171 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3172 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003173 break;
Harald Welte03115042009-10-16 08:32:58 +02003174 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003175 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003176 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003177 break;
Harald Welte59b04682009-06-10 05:40:52 +08003178 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003179 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3180 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003181 break;
3182 }
3183
3184 return rc;
3185}
3186
Harald Welte3c062072009-07-28 18:25:29 +02003187/*
3188 * This will be ran by the linker when loading the DSO. We use it to
3189 * do system initialization, e.g. registration of signal handlers.
3190 */
3191static __attribute__((constructor)) void on_dso_load_0408(void)
3192{
Harald Welte3c062072009-07-28 18:25:29 +02003193 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3194}