blob: dcbf168cc8f6c7ea312ac16e2d1d1c561307fe77 [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 Weltef4625b12010-02-20 16:24:02 +010035#include <osmocore/msgb.h>
36#include <osmocore/bitvec.h>
37#include <osmocore/tlv.h>
Harald Welte59b04682009-06-10 05:40:52 +080038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Harald Weltef4625b12010-02-20 16:24:02 +010040#include <osmocore/gsm_utils.h>
Harald Welte59b04682009-06-10 05:40:52 +080041#include <openbsc/gsm_subscriber.h>
42#include <openbsc/gsm_04_11.h>
43#include <openbsc/gsm_04_08.h>
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +080044#include <openbsc/gsm_04_80.h>
Harald Welte59b04682009-06-10 05:40:52 +080045#include <openbsc/abis_rsl.h>
46#include <openbsc/chan_alloc.h>
47#include <openbsc/paging.h>
48#include <openbsc/signal.h>
49#include <openbsc/trau_frame.h>
50#include <openbsc/trau_mux.h>
Harald Welte3c062072009-07-28 18:25:29 +020051#include <openbsc/rtp_proxy.h>
Harald Weltef4625b12010-02-20 16:24:02 +010052#include <osmocore/talloc.h>
Harald Weltefdc93d92010-03-07 23:40:35 +010053#include <osmocore/gsm48.h>
Harald Weltec2189a62009-07-23 18:56:43 +020054#include <openbsc/transaction.h>
Harald Welte03115042009-10-16 08:32:58 +020055#include <openbsc/ussd.h>
Harald Weltef6a38ec2009-12-29 11:49:12 +010056#include <openbsc/silent_call.h>
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080057#include <openbsc/bsc_api.h>
Holger Hans Peter Freyther3a0ec172010-06-30 12:44:07 +080058#include <openbsc/osmo_msc.h>
Harald Welte59b04682009-06-10 05:40:52 +080059
Harald Welte (local)8751ee92009-08-15 02:30:58 +020060void *tall_locop_ctx;
Sylvain Munaut28c47162009-12-24 00:27:26 +010061void *tall_authciphop_ctx;
Harald Weltea8379772009-06-20 22:36:41 +020062
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +080063int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +080064static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +080065 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +010066static void schedule_reject(struct gsm_subscriber_connection *conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +080067static void release_anchor(struct gsm_subscriber_connection *conn);
Harald Welte59b04682009-06-10 05:40:52 +080068
69struct gsm_lai {
70 u_int16_t mcc;
71 u_int16_t mnc;
72 u_int16_t lac;
73};
74
Harald Welte03740842009-06-10 23:11:52 +080075static u_int32_t new_callref = 0x80000001;
76
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +080077static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
78 struct gsm_trans *trans)
79{
80 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
81
82 /* if we get passed a transaction reference, do some common
83 * work that the caller no longer has to do */
84 if (trans) {
85 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
86 msg->lchan = trans->conn->lchan;
87 }
88
89
90 if (msg->lchan) {
91 msg->trx = msg->lchan->ts->trx;
92 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
93 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
94 "Sending '%s' to MS.\n", msg->trx->bts->nr,
95 msg->trx->nr, msg->lchan->ts->nr,
96 gh->proto_discr & 0xf0,
97 gsm48_cc_msg_name(gh->msg_type));
98 else
99 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
100 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
101 msg->trx->nr, msg->lchan->ts->nr,
102 gh->proto_discr, gh->msg_type);
103 }
104
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +0800105 return gsm0808_submit_dtap(conn, msg, 0);
106}
Sylvain Munaut28c47162009-12-24 00:27:26 +0100107
Holger Hans Peter Freyther9aa29b52010-08-12 01:41:57 +0800108int gsm48_cc_tx_notify_ss(struct gsm_trans *trans, const char *message)
109{
110 struct gsm48_hdr *gh;
111 struct msgb *ss_notify;
112
113 ss_notify = gsm0480_create_notifySS(message);
114 if (!ss_notify)
115 return -1;
116
117 gsm0480_wrap_invoke(ss_notify, GSM0480_OP_CODE_NOTIFY_SS, 0);
118 uint8_t *data = msgb_push(ss_notify, 1);
119 data[0] = ss_notify->len - 1;
120 gh = (struct gsm48_hdr *) msgb_push(ss_notify, sizeof(*gh));
121 gh->msg_type = GSM48_MT_CC_FACILITY;
122 return gsm48_conn_sendmsg(ss_notify, trans->conn, trans);
123}
124
Sylvain Munaut28c47162009-12-24 00:27:26 +0100125static void release_security_operation(struct gsm_subscriber_connection *conn)
126{
127 if (!conn->sec_operation)
128 return;
129
130 talloc_free(conn->sec_operation);
131 conn->sec_operation = NULL;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800132 msc_release_connection(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100133}
134
135static void allocate_security_operation(struct gsm_subscriber_connection *conn)
136{
Sylvain Munaut28c47162009-12-24 00:27:26 +0100137 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
138 struct gsm_security_operation);
139}
140
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800141static int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
142 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100143{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800144 struct gsm_network *net = conn->bts->network;
145 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100146 struct gsm_security_operation *op;
147 struct gsm_auth_tuple atuple;
148 int status = -1, rc;
149
150 /* Check if we _can_ enable encryption. Cases where we can't:
151 * - Encryption disabled in config
152 * - Channel already secured (nothing to do)
153 * - Subscriber equipment doesn't support configured encryption
154 */
155 if (!net->a5_encryption) {
156 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800157 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut28c47162009-12-24 00:27:26 +0100158 DEBUGP(DMM, "Requesting to secure an already secure channel");
159 status = GSM_SECURITY_SUCCEEDED;
160 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
161 net->a5_encryption)) {
162 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
163 status = GSM_SECURITY_NOAVAIL;
164 }
165
166 /* If not done yet, try to get info for this user */
167 if (status < 0) {
168 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
169 if (rc <= 0)
170 status = GSM_SECURITY_NOAVAIL;
171 }
172
173 /* Are we done yet ? */
174 if (status >= 0)
175 return cb ?
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800176 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut28c47162009-12-24 00:27:26 +0100177 0;
178
179 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800180 if (conn->sec_operation)
Sylvain Munaut28c47162009-12-24 00:27:26 +0100181 return -EBUSY;
182
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800183 allocate_security_operation(conn);
184 op = conn->sec_operation;
Sylvain Munaut28c47162009-12-24 00:27:26 +0100185 op->cb = cb;
186 op->cb_data = cb_data;
187 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
188
189 /* FIXME: Should start a timer for completion ... */
190
191 /* Then do whatever is needed ... */
192 if (rc == 1) {
193 /* Start authentication */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800194 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100195 } else if (rc == 2) {
196 /* Start ciphering directly */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800197 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
198 conn->lchan->encr.key_len = 8;
199 memcpy(conn->lchan->encr.key, op->atuple.kc, 8);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100200
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800201 return gsm48_send_rr_ciph_mode(conn->lchan, 0);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100202 }
203
204 return -EINVAL; /* not reached */
205}
206
Harald Welte59b04682009-06-10 05:40:52 +0800207static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
208 struct gsm_subscriber *subscriber)
209{
210 if (!subscriber)
211 return 0;
212
213 /*
214 * Do not send accept yet as more information should arrive. Some
215 * phones will not send us the information and we will have to check
216 * what we want to do with that.
217 */
218 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
219 return 0;
220
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200221 switch (subscriber->net->auth_policy) {
222 case GSM_AUTH_POLICY_CLOSED:
223 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200224 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)272f5c72009-08-13 20:44:23 +0200225 if (subscriber->authorized)
226 return subscriber->authorized;
Harald Welte (local)16480eb2009-08-13 13:49:51 +0200227 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200228 case GSM_AUTH_POLICY_ACCEPT_ALL:
Harald Welte59b04682009-06-10 05:40:52 +0800229 return 1;
Jan Luebbe3d6294e2009-08-12 12:48:00 +0200230 default:
231 return 0;
232 }
Harald Welte59b04682009-06-10 05:40:52 +0800233}
234
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100235static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800236{
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100237 if (!conn->loc_operation)
Harald Welte59b04682009-06-10 05:40:52 +0800238 return;
239
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800240 /* No need to keep the connection up */
241 release_anchor(conn);
242
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100243 bsc_del_timer(&conn->loc_operation->updating_timer);
244 talloc_free(conn->loc_operation);
245 conn->loc_operation = 0;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800246 msc_release_connection(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800247}
248
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100249static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Harald Welte59b04682009-06-10 05:40:52 +0800250{
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800251 if (conn->loc_operation)
252 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100253 release_loc_updating_req(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800254
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100255 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte857e00d2009-06-26 20:25:23 +0200256 struct gsm_loc_updating_operation);
Harald Welte59b04682009-06-10 05:40:52 +0800257}
258
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100259static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
260 struct msgb *msg, void *data, void *param)
261{
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800262 struct gsm_subscriber_connection *conn = data;
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100263 int rc = 0;
264
265 switch (event) {
266 case GSM_SECURITY_AUTH_FAILED:
267 release_loc_updating_req(conn);
268 break;
269
270 case GSM_SECURITY_NOAVAIL:
271 case GSM_SECURITY_SUCCEEDED:
272 /* We're all good */
273 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherfa2bafd2010-06-15 14:07:27 +0800274 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800275 if (conn->bts->network->send_mm_info) {
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100276 /* send MM INFO with network name */
Holger Hans Peter Freyther3d315272010-06-15 14:16:02 +0800277 rc = gsm48_tx_mm_info(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100278 }
279
280 /* call subscr_update after putting the loc_upd_acc
281 * in the transmit queue, since S_SUBSCR_ATTACHED might
282 * trigger further action like SMS delivery */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800283 subscr_update(conn->subscr, conn->bts,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100284 GSM_SUBSCRIBER_UPDATE_ATTACHED);
285
286 /* try to close channel ASAP */
Holger Hans Peter Freyther1817b0b2010-06-16 12:52:28 +0800287 release_loc_updating_req(conn);
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100288 break;
289
290 default:
291 rc = -EINVAL;
292 };
293
294 return rc;
295}
296
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +0100297static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800298{
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100299 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800300 return gsm48_secure_channel(conn,
Sylvain Munautcb321fb2009-12-24 00:28:01 +0100301 conn->loc_operation->key_seq,
302 _gsm0408_authorize_sec_cb, NULL);
Harald Welte59b04682009-06-10 05:40:52 +0800303 return 0;
304}
305
Holger Hans Peter Freytherdfd61a32010-09-18 06:44:24 +0800306void gsm0408_clear_request(struct gsm_subscriber_connection *conn, uint32_t cause)
Harald Welte59b04682009-06-10 05:40:52 +0800307{
Harald Welte03740842009-06-10 23:11:52 +0800308 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther6e5c50f2010-06-28 17:09:29 +0800309
310 /* avoid someone issuing a clear */
311 conn->in_release = 1;
312
Harald Welte59b04682009-06-10 05:40:52 +0800313 /*
314 * Cancel any outstanding location updating request
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800315 * operation taking place on the subscriber connection.
Harald Welte59b04682009-06-10 05:40:52 +0800316 */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800317 release_loc_updating_req(conn);
318 release_security_operation(conn);
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +0800319 release_anchor(conn);
Harald Welte59b04682009-06-10 05:40:52 +0800320
Harald Welte03740842009-06-10 23:11:52 +0800321 /* Free all transactions that are associated with the released lchan */
Harald Weltec2189a62009-07-23 18:56:43 +0200322 /* FIXME: this is not neccessarily the right thing to do, we should
323 * only set trans->lchan to NULL and wait for another lchan to be
324 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freythered4e3bd2010-06-15 13:16:52 +0800325 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
326 if (trans->conn == conn)
Harald Weltec2189a62009-07-23 18:56:43 +0200327 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +0800328 }
Holger Hans Peter Freyther7d855642010-09-16 20:48:15 +0800329
330 subscr_con_free(conn);
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)) {
930 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200931
932 DEBUGPC(DMM, "Invalid (expected %s)\n",
933 hexdump(conn->sec_operation->atuple.sres, 4));
934
Sylvain Munaut28c47162009-12-24 00:27:26 +0100935 if (cb)
936 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800937 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100938
939 release_security_operation(conn);
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +0800940 return gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100941 }
942
Sylvain Munaut20d99c62010-06-10 23:51:41 +0200943 DEBUGPC(DMM, "OK\n");
944
Sylvain Munaut28c47162009-12-24 00:27:26 +0100945 /* Start ciphering */
Holger Hans Peter Freyther9acd1b12010-06-16 12:47:59 +0800946 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
947 conn->lchan->encr.key_len = 8;
948 memcpy(conn->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
Sylvain Munaut28c47162009-12-24 00:27:26 +0100949
950 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
951}
952
Harald Welte59b04682009-06-10 05:40:52 +0800953/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800954static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +0800955{
956 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte03740842009-06-10 23:11:52 +0800957 int rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +0800958
959 switch (gh->msg_type & 0xbf) {
960 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Welte79639662009-06-27 02:58:43 +0200961 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800962 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800963 break;
964 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800965 rc = mm_rx_id_resp(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800966 break;
967 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800968 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +0800969 break;
970 case GSM48_MT_MM_STATUS:
971 rc = gsm48_rx_mm_status(msg);
972 break;
973 case GSM48_MT_MM_TMSI_REALL_COMPL:
974 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +0800975 conn->subscr ?
976 subscr_name(conn->subscr) :
Harald Welte59b04682009-06-10 05:40:52 +0800977 "unknown subscriber");
978 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;
1201 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freyther9e6d6ce2009-10-22 15:23:11 +02001202 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte59b04682009-06-10 05:40:52 +08001203 break;
1204 case GSM48_MT_RR_STATUS:
1205 rc = gsm48_rx_rr_status(msg);
1206 break;
1207 case GSM48_MT_RR_MEAS_REP:
1208 rc = gsm48_rx_rr_meas_rep(msg);
1209 break;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001210 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001211 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001212 break;
Harald Welted2dd9de2009-08-30 15:37:11 +09001213 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08001214 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welted2dd9de2009-08-30 15:37:11 +09001215 break;
Harald Welte323f4e02009-11-29 20:02:20 +01001216 case GSM48_MT_RR_HANDO_COMPL:
Harald Welte6720a432009-11-29 22:45:52 +01001217 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001218 break;
1219 case GSM48_MT_RR_HANDO_FAIL:
Harald Welte6720a432009-11-29 22:45:52 +01001220 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welte323f4e02009-11-29 20:02:20 +01001221 break;
Harald Welte59b04682009-06-10 05:40:52 +08001222 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01001223 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1224 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte59b04682009-06-10 05:40:52 +08001225 break;
1226 }
1227
1228 return rc;
1229}
1230
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001231int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freytherd70da872009-10-22 15:42:19 +02001232 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)b5103922009-08-15 23:32:44 +02001233{
1234 struct msgb *msg = gsm48_msgb_alloc();
1235 struct gsm48_hdr *gh;
1236
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001237 msg->lchan = conn->lchan;
Harald Welte (local)b5103922009-08-15 23:32:44 +02001238
1239 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1240 apdu_id, apdu_len);
1241
1242 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1243 gh->proto_discr = GSM48_PDISC_RR;
1244 gh->msg_type = GSM48_MT_RR_APP_INFO;
1245 gh->data[0] = apdu_id;
1246 gh->data[1] = apdu_len;
1247 memcpy(gh->data+2, apdu, apdu_len);
1248
Holger Hans Peter Freyther92f1b2d2010-06-16 13:52:55 +08001249 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)b5103922009-08-15 23:32:44 +02001250}
1251
Harald Welte59b04682009-06-10 05:40:52 +08001252/* Call Control */
1253
1254/* The entire call control code is written in accordance with Figure 7.10c
1255 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1256 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1257 * it for voice */
1258
Harald Welte03740842009-06-10 23:11:52 +08001259static void new_cc_state(struct gsm_trans *trans, int state)
1260{
1261 if (state > 31 || state < 0)
1262 return;
1263
1264 DEBUGP(DCC, "new state %s -> %s\n",
Harald Welteb30935e2010-03-25 12:13:02 +08001265 gsm48_cc_state_name(trans->cc.state),
1266 gsm48_cc_state_name(state));
Harald Welte03740842009-06-10 23:11:52 +08001267
Harald Weltec2189a62009-07-23 18:56:43 +02001268 trans->cc.state = state;
Harald Welte03740842009-06-10 23:11:52 +08001269}
1270
1271static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte59b04682009-06-10 05:40:52 +08001272{
1273 struct msgb *msg = gsm48_msgb_alloc();
1274 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1275 u_int8_t *cause, *call_state;
1276
Harald Welte59b04682009-06-10 05:40:52 +08001277 gh->msg_type = GSM48_MT_CC_STATUS;
1278
1279 cause = msgb_put(msg, 3);
1280 cause[0] = 2;
1281 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1282 cause[2] = 0x80 | 30; /* response to status inquiry */
1283
1284 call_state = msgb_put(msg, 1);
1285 call_state[0] = 0xc0 | 0x00;
1286
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001287 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001288}
1289
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001290static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte59b04682009-06-10 05:40:52 +08001291 u_int8_t pdisc, u_int8_t msg_type)
1292{
1293 struct msgb *msg = gsm48_msgb_alloc();
1294 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1295
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001296 msg->lchan = conn->lchan;
Harald Welte59b04682009-06-10 05:40:52 +08001297
1298 gh->proto_discr = pdisc;
1299 gh->msg_type = msg_type;
1300
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08001301 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte59b04682009-06-10 05:40:52 +08001302}
1303
Harald Welte03740842009-06-10 23:11:52 +08001304static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1305{
Harald Weltec2189a62009-07-23 18:56:43 +02001306 if (bsc_timer_pending(&trans->cc.timer)) {
1307 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1308 bsc_del_timer(&trans->cc.timer);
1309 trans->cc.Tcurrent = 0;
Harald Welte03740842009-06-10 23:11:52 +08001310 }
1311}
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001312
Harald Welte03740842009-06-10 23:11:52 +08001313static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1314 int msg_type, struct gsm_mncc *mncc)
1315{
1316 struct msgb *msg;
1317
1318 if (trans)
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001319 if (trans->conn)
Harald Welte4861c822009-07-23 21:21:14 +02001320 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08001321 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001322 trans->conn->lchan->ts->trx->bts->nr,
1323 trans->conn->lchan->ts->trx->nr,
1324 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte03740842009-06-10 23:11:52 +08001325 (trans->subscr)?(trans->subscr->extension):"-",
1326 get_mncc_name(msg_type));
1327 else
1328 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1329 "Sending '%s' to MNCC.\n",
1330 (trans->subscr)?(trans->subscr->extension):"-",
1331 get_mncc_name(msg_type));
1332 else
1333 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1334 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1335
1336 mncc->msg_type = msg_type;
1337
Harald Welte9cfc9352009-06-26 19:39:35 +02001338 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte03740842009-06-10 23:11:52 +08001339 if (!msg)
1340 return -ENOMEM;
1341 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1342 msgb_enqueue(&net->upqueue, msg);
1343
1344 return 0;
1345}
1346
1347int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1348 u_int32_t callref, int location, int value)
1349{
1350 struct gsm_mncc rel;
1351
Harald Weltecb0595f2009-06-12 01:54:08 +08001352 memset(&rel, 0, sizeof(rel));
Harald Welte03740842009-06-10 23:11:52 +08001353 rel.callref = callref;
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001354 mncc_set_cause(&rel, location, value);
Harald Welte03740842009-06-10 23:11:52 +08001355 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1356}
1357
Harald Weltec2189a62009-07-23 18:56:43 +02001358/* Call Control Specific transaction release.
1359 * gets called by trans_free, DO NOT CALL YOURSELF! */
1360void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte03740842009-06-10 23:11:52 +08001361{
Harald Welte03740842009-06-10 23:11:52 +08001362 gsm48_stop_cc_timer(trans);
1363
1364 /* send release to L4, if callref still exists */
1365 if (trans->callref) {
1366 /* Ressource unavailable */
Harald Welte0abe5a62009-07-23 19:06:52 +02001367 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001368 GSM48_CAUSE_LOC_PRN_S_LU,
1369 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08001370 }
Harald Weltec2189a62009-07-23 18:56:43 +02001371 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte03740842009-06-10 23:11:52 +08001372 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001373 if (trans->conn)
1374 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte03740842009-06-10 23:11:52 +08001375}
1376
1377static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001378
Harald Welte59b04682009-06-10 05:40:52 +08001379/* call-back from paging the B-end of the connection */
1380static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001381 struct msgb *msg, void *_conn, void *param)
Harald Welte59b04682009-06-10 05:40:52 +08001382{
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001383 struct gsm_subscriber_connection *conn = _conn;
Harald Welte03740842009-06-10 23:11:52 +08001384 struct gsm_subscriber *subscr = param;
1385 struct gsm_trans *transt, *tmp;
1386 struct gsm_network *net;
Harald Welte1ff81b52009-06-26 20:17:06 +02001387
Harald Welte59b04682009-06-10 05:40:52 +08001388 if (hooknum != GSM_HOOK_RR_PAGING)
1389 return -EINVAL;
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02001390
Harald Welte03740842009-06-10 23:11:52 +08001391 if (!subscr)
1392 return -EINVAL;
1393 net = subscr->net;
1394 if (!net) {
1395 DEBUGP(DCC, "Error Network not set!\n");
1396 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001397 }
1398
Harald Welte03740842009-06-10 23:11:52 +08001399 /* check all tranactions (without lchan) for subscriber */
1400 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001401 if (transt->subscr != subscr || transt->conn)
Harald Welte03740842009-06-10 23:11:52 +08001402 continue;
1403 switch (event) {
1404 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001405 if (!conn) // paranoid
Harald Welte03740842009-06-10 23:11:52 +08001406 break;
1407 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1408 subscr->extension);
1409 /* Assign lchan */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001410 if (!transt->conn) {
Holger Hans Peter Freytherd3aaf662010-06-17 15:05:57 +08001411 transt->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08001412 }
1413 /* send SETUP request to called party */
Harald Weltec2189a62009-07-23 18:56:43 +02001414 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte03740842009-06-10 23:11:52 +08001415 break;
1416 case GSM_PAGING_EXPIRED:
1417 DEBUGP(DCC, "Paging subscr %s expired!\n",
1418 subscr->extension);
1419 /* Temporarily out of order */
Harald Welte0abe5a62009-07-23 19:06:52 +02001420 mncc_release_ind(transt->subscr->net, transt,
1421 transt->callref,
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08001422 GSM48_CAUSE_LOC_PRN_S_LU,
1423 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08001424 transt->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02001425 trans_free(transt);
Harald Welte03740842009-06-10 23:11:52 +08001426 break;
1427 }
1428 }
Harald Welte59b04682009-06-10 05:40:52 +08001429 return 0;
1430}
1431
Harald Welte3971ad52009-12-19 22:23:05 +01001432static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1433
Harald Welte3c062072009-07-28 18:25:29 +02001434/* some other part of the code sends us a signal */
1435static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1436 void *handler_data, void *signal_data)
1437{
1438 struct gsm_lchan *lchan = signal_data;
Harald Welte3c062072009-07-28 18:25:29 +02001439 int rc;
Harald Welte3971ad52009-12-19 22:23:05 +01001440 struct gsm_network *net;
1441 struct gsm_trans *trans;
Harald Welte3c062072009-07-28 18:25:29 +02001442
1443 if (subsys != SS_ABISIP)
1444 return 0;
1445
1446 /* in case we use direct BTS-to-BTS RTP */
1447 if (ipacc_rtp_direct)
1448 return 0;
1449
Harald Welte3c062072009-07-28 18:25:29 +02001450 switch (signal) {
Holger Hans Peter Freyther5ea7ea62009-11-18 21:06:12 +01001451 case S_ABISIP_CRCX_ACK:
Harald Welte3971ad52009-12-19 22:23:05 +01001452 /* check if any transactions on this lchan still have
1453 * a tch_recv_mncc request pending */
1454 net = lchan->ts->trx->bts->network;
1455 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001456 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welte3971ad52009-12-19 22:23:05 +01001457 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1458 tch_recv_mncc(net, trans->callref, 1);
1459 }
1460 }
Harald Welte3c062072009-07-28 18:25:29 +02001461 break;
Harald Welte3c062072009-07-28 18:25:29 +02001462 }
1463
1464 return 0;
Harald Welte3c062072009-07-28 18:25:29 +02001465}
1466
Harald Welte59b04682009-06-10 05:40:52 +08001467/* map two ipaccess RTP streams onto each other */
1468static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
1469{
1470 struct gsm_bts *bts = lchan->ts->trx->bts;
1471 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte3c062072009-07-28 18:25:29 +02001472 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001473
1474 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1475 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1476 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1477
1478 if (bts->type != remote_bts->type) {
1479 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1480 return -EINVAL;
1481 }
Harald Welte3971ad52009-12-19 22:23:05 +01001482
1483 // todo: map between different bts types
Harald Welte59b04682009-06-10 05:40:52 +08001484 switch (bts->type) {
Mike Haben66e0ba02009-10-02 12:19:34 +01001485 case GSM_BTS_TYPE_NANOBTS:
Harald Welte3c062072009-07-28 18:25:29 +02001486 if (!ipacc_rtp_direct) {
1487 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001488 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3c062072009-07-28 18:25:29 +02001489 if (rc < 0)
1490 return rc;
Harald Welte9947d9f2009-12-20 16:51:09 +01001491 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001492#warning do we need a check of rc here?
Harald Welte3c062072009-07-28 18:25:29 +02001493
1494 /* connect them with each other */
Harald Welte87504212009-12-02 01:56:49 +05301495 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1496 remote_lchan->abis_ip.rtp_socket);
Harald Welte3c062072009-07-28 18:25:29 +02001497 } else {
1498 /* directly connect TCH RTP streams to each other */
Harald Welte87504212009-12-02 01:56:49 +05301499 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1500 remote_lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301501 remote_lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001502 if (rc < 0)
1503 return rc;
Harald Welte87504212009-12-02 01:56:49 +05301504 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1505 lchan->abis_ip.bound_port,
Harald Welte87504212009-12-02 01:56:49 +05301506 lchan->abis_ip.rtp_payload2);
Harald Welte3c062072009-07-28 18:25:29 +02001507 }
Harald Welte59b04682009-06-10 05:40:52 +08001508 break;
1509 case GSM_BTS_TYPE_BS11:
1510 trau_mux_map_lchan(lchan, remote_lchan);
1511 break;
1512 default:
1513 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welte3971ad52009-12-19 22:23:05 +01001514 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08001515 }
1516
1517 return 0;
1518}
1519
Harald Welte03740842009-06-10 23:11:52 +08001520/* bridge channels of two transactions */
1521static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte59b04682009-06-10 05:40:52 +08001522{
Harald Weltec2189a62009-07-23 18:56:43 +02001523 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1524 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte59b04682009-06-10 05:40:52 +08001525
Harald Welte03740842009-06-10 23:11:52 +08001526 if (!trans1 || !trans2)
Harald Welte59b04682009-06-10 05:40:52 +08001527 return -EIO;
1528
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001529 if (!trans1->conn || !trans2->conn)
Harald Welte03740842009-06-10 23:11:52 +08001530 return -EIO;
1531
1532 /* through-connect channel */
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001533 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte59b04682009-06-10 05:40:52 +08001534}
1535
Harald Welte3971ad52009-12-19 22:23:05 +01001536/* enable receive of channels to MNCC upqueue */
1537static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte03740842009-06-10 23:11:52 +08001538{
1539 struct gsm_trans *trans;
Harald Welte3971ad52009-12-19 22:23:05 +01001540 struct gsm_lchan *lchan;
1541 struct gsm_bts *bts;
1542 int rc;
Harald Welte59b04682009-06-10 05:40:52 +08001543
Harald Welte03740842009-06-10 23:11:52 +08001544 /* Find callref */
Harald Welte3971ad52009-12-19 22:23:05 +01001545 trans = trans_find_by_callref(net, callref);
Harald Welte03740842009-06-10 23:11:52 +08001546 if (!trans)
1547 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001548 if (!trans->conn)
Harald Welte03740842009-06-10 23:11:52 +08001549 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01001550 lchan = trans->conn->lchan;
Harald Welte3971ad52009-12-19 22:23:05 +01001551 bts = lchan->ts->trx->bts;
Harald Welte03740842009-06-10 23:11:52 +08001552
Harald Welte3971ad52009-12-19 22:23:05 +01001553 switch (bts->type) {
1554 case GSM_BTS_TYPE_NANOBTS:
1555 if (ipacc_rtp_direct) {
1556 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1557 return -EINVAL;
1558 }
1559 /* in case, we don't have a RTP socket yet, we note this
1560 * in the transaction and try later */
1561 if (!lchan->abis_ip.rtp_socket) {
1562 trans->tch_recv = enable;
1563 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1564 return 0;
1565 }
1566 if (enable) {
1567 /* connect the TCH's to our RTP proxy */
Harald Welte9947d9f2009-12-20 16:51:09 +01001568 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte3971ad52009-12-19 22:23:05 +01001569 if (rc < 0)
1570 return rc;
1571 /* assign socket to application interface */
1572 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1573 net, callref);
1574 } else
1575 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1576 net, 0);
1577 break;
1578 case GSM_BTS_TYPE_BS11:
1579 if (enable)
1580 return trau_recv_lchan(lchan, callref);
1581 return trau_mux_unmap(NULL, callref);
1582 break;
1583 default:
1584 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1585 return -EINVAL;
1586 }
1587
1588 return 0;
Harald Welte03740842009-06-10 23:11:52 +08001589}
1590
Harald Welte03740842009-06-10 23:11:52 +08001591static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1592{
1593 DEBUGP(DCC, "-> STATUS ENQ\n");
1594 return gsm48_cc_tx_status(trans, msg);
1595}
1596
1597static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1598static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1599
1600static void gsm48_cc_timeout(void *arg)
1601{
1602 struct gsm_trans *trans = arg;
1603 int disconnect = 0, release = 0;
Harald Weltebbc636a2009-06-11 14:23:20 +08001604 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1605 int mo_location = GSM48_CAUSE_LOC_USER;
1606 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1607 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte03740842009-06-10 23:11:52 +08001608 struct gsm_mncc mo_rel, l4_rel;
1609
1610 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1611 mo_rel.callref = trans->callref;
1612 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1613 l4_rel.callref = trans->callref;
1614
Harald Weltec2189a62009-07-23 18:56:43 +02001615 switch(trans->cc.Tcurrent) {
Harald Welte03740842009-06-10 23:11:52 +08001616 case 0x303:
1617 release = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001618 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001619 break;
1620 case 0x310:
1621 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001622 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001623 break;
1624 case 0x313:
1625 disconnect = 1;
1626 /* unknown, did not find it in the specs */
1627 break;
1628 case 0x301:
1629 disconnect = 1;
Harald Weltebbc636a2009-06-11 14:23:20 +08001630 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte03740842009-06-10 23:11:52 +08001631 break;
1632 case 0x308:
Harald Weltec2189a62009-07-23 18:56:43 +02001633 if (!trans->cc.T308_second) {
Harald Welte03740842009-06-10 23:11:52 +08001634 /* restart T308 a second time */
Harald Weltec2189a62009-07-23 18:56:43 +02001635 gsm48_cc_tx_release(trans, &trans->cc.msg);
1636 trans->cc.T308_second = 1;
Harald Welte03740842009-06-10 23:11:52 +08001637 break; /* stay in release state */
1638 }
Harald Weltec2189a62009-07-23 18:56:43 +02001639 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08001640 return;
1641// release = 1;
1642// l4_cause = 14;
1643// break;
1644 case 0x306:
1645 release = 1;
Harald Weltec2189a62009-07-23 18:56:43 +02001646 mo_cause = trans->cc.msg.cause.value;
1647 mo_location = trans->cc.msg.cause.location;
Harald Welte03740842009-06-10 23:11:52 +08001648 break;
1649 case 0x323:
1650 disconnect = 1;
1651 break;
1652 default:
1653 release = 1;
1654 }
1655
1656 if (release && trans->callref) {
1657 /* process release towards layer 4 */
Harald Welte0abe5a62009-07-23 19:06:52 +02001658 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte03740842009-06-10 23:11:52 +08001659 l4_location, l4_cause);
1660 trans->callref = 0;
1661 }
1662
1663 if (disconnect && trans->callref) {
1664 /* process disconnect towards layer 4 */
1665 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte0abe5a62009-07-23 19:06:52 +02001666 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte03740842009-06-10 23:11:52 +08001667 }
1668
1669 /* process disconnect towards mobile station */
1670 if (disconnect || release) {
1671 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltec2189a62009-07-23 18:56:43 +02001672 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1673 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1674 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte03740842009-06-10 23:11:52 +08001675 mo_rel.cause.diag_len = 3;
1676
1677 if (disconnect)
1678 gsm48_cc_tx_disconnect(trans, &mo_rel);
1679 if (release)
1680 gsm48_cc_tx_release(trans, &mo_rel);
1681 }
1682
1683}
1684
1685static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1686 int sec, int micro)
1687{
1688 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltec2189a62009-07-23 18:56:43 +02001689 trans->cc.timer.cb = gsm48_cc_timeout;
1690 trans->cc.timer.data = trans;
1691 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1692 trans->cc.Tcurrent = current;
Harald Welte03740842009-06-10 23:11:52 +08001693}
1694
1695static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1696{
1697 struct gsm48_hdr *gh = msgb_l3(msg);
1698 u_int8_t msg_type = gh->msg_type & 0xbf;
1699 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1700 struct tlv_parsed tp;
1701 struct gsm_mncc setup;
1702
1703 memset(&setup, 0, sizeof(struct gsm_mncc));
1704 setup.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001705 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001706 /* emergency setup is identified by msg_type */
1707 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1708 setup.emergency = 1;
1709
1710 /* use subscriber as calling party number */
1711 if (trans->subscr) {
1712 setup.fields |= MNCC_F_CALLING;
1713 strncpy(setup.calling.number, trans->subscr->extension,
1714 sizeof(setup.calling.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02001715 strncpy(setup.imsi, trans->subscr->imsi,
1716 sizeof(setup.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08001717 }
1718 /* bearer capability */
1719 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1720 setup.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001721 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001722 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1723 }
1724 /* facility */
1725 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1726 setup.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001727 gsm48_decode_facility(&setup.facility,
Harald Welte03740842009-06-10 23:11:52 +08001728 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1729 }
1730 /* called party bcd number */
1731 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1732 setup.fields |= MNCC_F_CALLED;
Harald Weltefdc93d92010-03-07 23:40:35 +01001733 gsm48_decode_called(&setup.called,
Harald Welte03740842009-06-10 23:11:52 +08001734 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1735 }
1736 /* user-user */
1737 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1738 setup.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01001739 gsm48_decode_useruser(&setup.useruser,
Harald Welte03740842009-06-10 23:11:52 +08001740 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1741 }
1742 /* ss-version */
1743 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1744 setup.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001745 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001746 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1747 }
1748 /* CLIR suppression */
1749 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1750 setup.clir.sup = 1;
1751 /* CLIR invocation */
1752 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1753 setup.clir.inv = 1;
1754 /* cc cap */
1755 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1756 setup.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001757 gsm48_decode_cccap(&setup.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001758 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1759 }
1760
Harald Welte03740842009-06-10 23:11:52 +08001761 new_cc_state(trans, GSM_CSTATE_INITIATED);
1762
Harald Welte (local)5b722d42009-12-26 19:45:03 +01001763 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1764 subscr_name(trans->subscr), trans->subscr->extension,
1765 setup.called.number);
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
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001842 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte59b04682009-06-10 05:40:52 +08001843}
1844
Harald Welte03740842009-06-10 23:11:52 +08001845static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1846{
1847 struct gsm48_hdr *gh = msgb_l3(msg);
1848 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1849 struct tlv_parsed tp;
1850 struct gsm_mncc call_conf;
1851
1852 gsm48_stop_cc_timer(trans);
1853 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1854
1855 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1856 call_conf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001857 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001858#if 0
1859 /* repeat */
1860 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1861 call_conf.repeat = 1;
1862 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1863 call_conf.repeat = 2;
1864#endif
1865 /* bearer capability */
1866 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1867 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001868 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08001869 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1870 }
1871 /* cause */
1872 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1873 call_conf.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01001874 gsm48_decode_cause(&call_conf.cause,
Harald Welte03740842009-06-10 23:11:52 +08001875 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1876 }
1877 /* cc cap */
1878 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1879 call_conf.fields |= MNCC_F_CCCAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01001880 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte03740842009-06-10 23:11:52 +08001881 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1882 }
1883
1884 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1885
Harald Welte0abe5a62009-07-23 19:06:52 +02001886 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1887 &call_conf);
Harald Welte03740842009-06-10 23:11:52 +08001888}
1889
1890static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1891{
1892 struct gsm_mncc *proceeding = arg;
1893 struct msgb *msg = gsm48_msgb_alloc();
1894 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1895
Harald Welte03740842009-06-10 23:11:52 +08001896 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1897
1898 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1899
1900 /* bearer capability */
1901 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Weltefdc93d92010-03-07 23:40:35 +01001902 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08001903 /* facility */
1904 if (proceeding->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001905 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte03740842009-06-10 23:11:52 +08001906 /* progress */
1907 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001908 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte03740842009-06-10 23:11:52 +08001909
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001910 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001911}
1912
1913static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1914{
1915 struct gsm48_hdr *gh = msgb_l3(msg);
1916 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1917 struct tlv_parsed tp;
1918 struct gsm_mncc alerting;
1919
1920 gsm48_stop_cc_timer(trans);
1921 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1922
1923 memset(&alerting, 0, sizeof(struct gsm_mncc));
1924 alerting.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01001925 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08001926 /* facility */
1927 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1928 alerting.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01001929 gsm48_decode_facility(&alerting.facility,
Harald Welte03740842009-06-10 23:11:52 +08001930 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1931 }
1932
1933 /* progress */
1934 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1935 alerting.fields |= MNCC_F_PROGRESS;
Harald Weltefdc93d92010-03-07 23:40:35 +01001936 gsm48_decode_progress(&alerting.progress,
Harald Welte03740842009-06-10 23:11:52 +08001937 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1938 }
1939 /* ss-version */
1940 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1941 alerting.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01001942 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08001943 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1944 }
1945
1946 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1947
Harald Welte0abe5a62009-07-23 19:06:52 +02001948 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1949 &alerting);
Harald Welte03740842009-06-10 23:11:52 +08001950}
1951
1952static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1953{
1954 struct gsm_mncc *alerting = arg;
1955 struct msgb *msg = gsm48_msgb_alloc();
1956 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1957
Harald Welte03740842009-06-10 23:11:52 +08001958 gh->msg_type = GSM48_MT_CC_ALERTING;
1959
1960 /* facility */
1961 if (alerting->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01001962 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte03740842009-06-10 23:11:52 +08001963 /* progress */
1964 if (alerting->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01001965 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte03740842009-06-10 23:11:52 +08001966 /* user-user */
1967 if (alerting->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001968 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001969
1970 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1971
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001972 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001973}
1974
1975static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1976{
1977 struct gsm_mncc *progress = arg;
1978 struct msgb *msg = gsm48_msgb_alloc();
1979 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1980
Harald Welte03740842009-06-10 23:11:52 +08001981 gh->msg_type = GSM48_MT_CC_PROGRESS;
1982
1983 /* progress */
Harald Weltefdc93d92010-03-07 23:40:35 +01001984 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte03740842009-06-10 23:11:52 +08001985 /* user-user */
1986 if (progress->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01001987 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte03740842009-06-10 23:11:52 +08001988
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08001989 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08001990}
1991
1992static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1993{
1994 struct gsm_mncc *connect = arg;
1995 struct msgb *msg = gsm48_msgb_alloc();
1996 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1997
Harald Welte03740842009-06-10 23:11:52 +08001998 gh->msg_type = GSM48_MT_CC_CONNECT;
1999
2000 gsm48_stop_cc_timer(trans);
2001 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
2002
2003 /* facility */
2004 if (connect->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002005 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte03740842009-06-10 23:11:52 +08002006 /* progress */
2007 if (connect->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002008 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte03740842009-06-10 23:11:52 +08002009 /* connected number */
2010 if (connect->fields & MNCC_F_CONNECTED)
Harald Weltefdc93d92010-03-07 23:40:35 +01002011 gsm48_encode_connected(msg, &connect->connected);
Harald Welte03740842009-06-10 23:11:52 +08002012 /* user-user */
2013 if (connect->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002014 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002015
2016 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2017
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002018 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002019}
2020
2021static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2022{
2023 struct gsm48_hdr *gh = msgb_l3(msg);
2024 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2025 struct tlv_parsed tp;
2026 struct gsm_mncc connect;
2027
2028 gsm48_stop_cc_timer(trans);
2029
2030 memset(&connect, 0, sizeof(struct gsm_mncc));
2031 connect.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002032 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002033 /* use subscriber as connected party number */
2034 if (trans->subscr) {
2035 connect.fields |= MNCC_F_CONNECTED;
2036 strncpy(connect.connected.number, trans->subscr->extension,
2037 sizeof(connect.connected.number)-1);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002038 strncpy(connect.imsi, trans->subscr->imsi,
2039 sizeof(connect.imsi)-1);
Harald Welte03740842009-06-10 23:11:52 +08002040 }
2041 /* facility */
2042 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2043 connect.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002044 gsm48_decode_facility(&connect.facility,
Harald Welte03740842009-06-10 23:11:52 +08002045 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2046 }
2047 /* user-user */
2048 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2049 connect.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002050 gsm48_decode_useruser(&connect.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002051 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2052 }
2053 /* ss-version */
2054 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2055 connect.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002056 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002057 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2058 }
2059
2060 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2061
Harald Welte0abe5a62009-07-23 19:06:52 +02002062 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte03740842009-06-10 23:11:52 +08002063}
2064
2065
2066static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2067{
2068 struct gsm_mncc connect_ack;
2069
2070 gsm48_stop_cc_timer(trans);
2071
2072 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2073
2074 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2075 connect_ack.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002076 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte03740842009-06-10 23:11:52 +08002077 &connect_ack);
2078}
2079
2080static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2081{
2082 struct msgb *msg = gsm48_msgb_alloc();
2083 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2084
Harald Welte03740842009-06-10 23:11:52 +08002085 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2086
2087 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2088
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002089 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002090}
2091
2092static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2093{
2094 struct gsm48_hdr *gh = msgb_l3(msg);
2095 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2096 struct tlv_parsed tp;
2097 struct gsm_mncc disc;
2098
2099 gsm48_stop_cc_timer(trans);
2100
2101 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2102
2103 memset(&disc, 0, sizeof(struct gsm_mncc));
2104 disc.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002105 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte03740842009-06-10 23:11:52 +08002106 /* cause */
2107 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2108 disc.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002109 gsm48_decode_cause(&disc.cause,
Harald Welte03740842009-06-10 23:11:52 +08002110 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2111 }
2112 /* facility */
2113 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2114 disc.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002115 gsm48_decode_facility(&disc.facility,
Harald Welte03740842009-06-10 23:11:52 +08002116 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2117 }
2118 /* user-user */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2120 disc.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002121 gsm48_decode_useruser(&disc.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002122 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2123 }
2124 /* ss-version */
2125 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2126 disc.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002127 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002128 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2129 }
2130
Harald Welte0abe5a62009-07-23 19:06:52 +02002131 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte03740842009-06-10 23:11:52 +08002132
2133}
2134
Harald Weltebbc636a2009-06-11 14:23:20 +08002135static struct gsm_mncc_cause default_cause = {
2136 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2137 .coding = 0,
2138 .rec = 0,
2139 .rec_val = 0,
2140 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2141 .diag_len = 0,
2142 .diag = { 0 },
2143};
Harald Welte03740842009-06-10 23:11:52 +08002144
2145static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2146{
2147 struct gsm_mncc *disc = arg;
2148 struct msgb *msg = gsm48_msgb_alloc();
2149 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2150
Harald Welte03740842009-06-10 23:11:52 +08002151 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2152
2153 gsm48_stop_cc_timer(trans);
2154 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2155
2156 /* cause */
2157 if (disc->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002158 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte03740842009-06-10 23:11:52 +08002159 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002160 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002161
2162 /* facility */
2163 if (disc->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002164 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte03740842009-06-10 23:11:52 +08002165 /* progress */
2166 if (disc->fields & MNCC_F_PROGRESS)
Harald Weltefdc93d92010-03-07 23:40:35 +01002167 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte03740842009-06-10 23:11:52 +08002168 /* user-user */
2169 if (disc->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002170 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002171
2172 /* store disconnect cause for T306 expiry */
Harald Weltec2189a62009-07-23 18:56:43 +02002173 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002174
2175 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2176
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002177 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002178}
2179
2180static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2181{
2182 struct gsm48_hdr *gh = msgb_l3(msg);
2183 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2184 struct tlv_parsed tp;
2185 struct gsm_mncc rel;
2186 int rc;
2187
2188 gsm48_stop_cc_timer(trans);
2189
2190 memset(&rel, 0, sizeof(struct gsm_mncc));
2191 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002192 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002193 /* cause */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2195 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002196 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2198 }
2199 /* facility */
2200 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2201 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002202 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002203 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2204 }
2205 /* user-user */
2206 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2207 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002208 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002209 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2210 }
2211 /* ss-version */
2212 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2213 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002214 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002215 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2216 }
2217
Harald Weltec2189a62009-07-23 18:56:43 +02002218 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002219 /* release collision 5.4.5 */
Harald Welte0abe5a62009-07-23 19:06:52 +02002220 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002221 } else {
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08002222 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02002223 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte0abe5a62009-07-23 19:06:52 +02002224 GSM48_MT_CC_RELEASE_COMPL);
2225 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte03740842009-06-10 23:11:52 +08002226 }
2227
2228 new_cc_state(trans, GSM_CSTATE_NULL);
2229
2230 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002231 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002232
2233 return rc;
2234}
2235
2236static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2237{
2238 struct gsm_mncc *rel = arg;
2239 struct msgb *msg = gsm48_msgb_alloc();
2240 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2241
Harald Welte03740842009-06-10 23:11:52 +08002242 gh->msg_type = GSM48_MT_CC_RELEASE;
2243
2244 trans->callref = 0;
2245
2246 gsm48_stop_cc_timer(trans);
2247 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2248
2249 /* cause */
2250 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002251 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002252 /* facility */
2253 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002254 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002255 /* user-user */
2256 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002257 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002258
Harald Weltec2189a62009-07-23 18:56:43 +02002259 trans->cc.T308_second = 0;
2260 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte03740842009-06-10 23:11:52 +08002261
Harald Weltec2189a62009-07-23 18:56:43 +02002262 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte03740842009-06-10 23:11:52 +08002263 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2264
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002265 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002266}
2267
2268static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2269{
2270 struct gsm48_hdr *gh = msgb_l3(msg);
2271 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2272 struct tlv_parsed tp;
2273 struct gsm_mncc rel;
2274 int rc = 0;
2275
2276 gsm48_stop_cc_timer(trans);
2277
2278 memset(&rel, 0, sizeof(struct gsm_mncc));
2279 rel.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002280 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002281 /* cause */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2283 rel.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002284 gsm48_decode_cause(&rel.cause,
Harald Welte03740842009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2286 }
2287 /* facility */
2288 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2289 rel.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002290 gsm48_decode_facility(&rel.facility,
Harald Welte03740842009-06-10 23:11:52 +08002291 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2292 }
2293 /* user-user */
2294 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2295 rel.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002296 gsm48_decode_useruser(&rel.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002297 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2298 }
2299 /* ss-version */
2300 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2301 rel.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002302 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002303 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2304 }
2305
2306 if (trans->callref) {
Harald Weltec2189a62009-07-23 18:56:43 +02002307 switch (trans->cc.state) {
Harald Welte03740842009-06-10 23:11:52 +08002308 case GSM_CSTATE_CALL_PRESENT:
Harald Welte0abe5a62009-07-23 19:06:52 +02002309 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002310 MNCC_REJ_IND, &rel);
2311 break;
2312 case GSM_CSTATE_RELEASE_REQ:
Harald Welte0abe5a62009-07-23 19:06:52 +02002313 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002314 MNCC_REL_CNF, &rel);
2315 break;
2316 default:
Harald Welte0abe5a62009-07-23 19:06:52 +02002317 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte03740842009-06-10 23:11:52 +08002318 MNCC_REL_IND, &rel);
2319 }
2320 }
2321
2322 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002323 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002324
2325 return rc;
2326}
2327
2328static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2329{
2330 struct gsm_mncc *rel = arg;
2331 struct msgb *msg = gsm48_msgb_alloc();
2332 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2333
Harald Welte03740842009-06-10 23:11:52 +08002334 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2335
2336 trans->callref = 0;
2337
2338 gsm48_stop_cc_timer(trans);
2339
2340 /* cause */
2341 if (rel->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002342 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte03740842009-06-10 23:11:52 +08002343 /* facility */
2344 if (rel->fields & MNCC_F_FACILITY)
Harald Weltefdc93d92010-03-07 23:40:35 +01002345 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte03740842009-06-10 23:11:52 +08002346 /* user-user */
2347 if (rel->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002348 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002349
Harald Weltec2189a62009-07-23 18:56:43 +02002350 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002351
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002352 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002353}
2354
2355static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2356{
2357 struct gsm48_hdr *gh = msgb_l3(msg);
2358 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2359 struct tlv_parsed tp;
2360 struct gsm_mncc fac;
2361
2362 memset(&fac, 0, sizeof(struct gsm_mncc));
2363 fac.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002364 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte03740842009-06-10 23:11:52 +08002365 /* facility */
2366 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2367 fac.fields |= MNCC_F_FACILITY;
Harald Weltefdc93d92010-03-07 23:40:35 +01002368 gsm48_decode_facility(&fac.facility,
Harald Welte03740842009-06-10 23:11:52 +08002369 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2370 }
2371 /* ss-version */
2372 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2373 fac.fields |= MNCC_F_SSVERSION;
Harald Weltefdc93d92010-03-07 23:40:35 +01002374 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte03740842009-06-10 23:11:52 +08002375 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2376 }
2377
Harald Welte0abe5a62009-07-23 19:06:52 +02002378 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte03740842009-06-10 23:11:52 +08002379}
2380
2381static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2382{
2383 struct gsm_mncc *fac = arg;
2384 struct msgb *msg = gsm48_msgb_alloc();
2385 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2386
Harald Welte03740842009-06-10 23:11:52 +08002387 gh->msg_type = GSM48_MT_CC_FACILITY;
2388
2389 /* facility */
Harald Weltefdc93d92010-03-07 23:40:35 +01002390 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte03740842009-06-10 23:11:52 +08002391
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002392 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002393}
2394
2395static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2396{
2397 struct gsm_mncc hold;
2398
2399 memset(&hold, 0, sizeof(struct gsm_mncc));
2400 hold.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002401 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte03740842009-06-10 23:11:52 +08002402}
2403
2404static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2405{
2406 struct msgb *msg = gsm48_msgb_alloc();
2407 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2408
Harald Welte03740842009-06-10 23:11:52 +08002409 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2410
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002411 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002412}
2413
2414static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2415{
2416 struct gsm_mncc *hold_rej = arg;
2417 struct msgb *msg = gsm48_msgb_alloc();
2418 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2419
Harald Welte03740842009-06-10 23:11:52 +08002420 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2421
2422 /* cause */
2423 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002424 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002425 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002426 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002427
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002428 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002429}
2430
2431static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2432{
2433 struct gsm_mncc retrieve;
2434
2435 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2436 retrieve.callref = trans->callref;
Harald Welte0abe5a62009-07-23 19:06:52 +02002437 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2438 &retrieve);
Harald Welte03740842009-06-10 23:11:52 +08002439}
2440
2441static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2442{
2443 struct msgb *msg = gsm48_msgb_alloc();
2444 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2445
Harald Welte03740842009-06-10 23:11:52 +08002446 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2447
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002448 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002449}
2450
2451static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2452{
2453 struct gsm_mncc *retrieve_rej = arg;
2454 struct msgb *msg = gsm48_msgb_alloc();
2455 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2456
Harald Welte03740842009-06-10 23:11:52 +08002457 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2458
2459 /* cause */
2460 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002461 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte03740842009-06-10 23:11:52 +08002462 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002463 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002464
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002465 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002466}
2467
2468static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2469{
2470 struct gsm48_hdr *gh = msgb_l3(msg);
2471 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2472 struct tlv_parsed tp;
2473 struct gsm_mncc dtmf;
2474
2475 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2476 dtmf.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002477 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte03740842009-06-10 23:11:52 +08002478 /* keypad facility */
2479 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2480 dtmf.fields |= MNCC_F_KEYPAD;
Harald Weltefdc93d92010-03-07 23:40:35 +01002481 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte03740842009-06-10 23:11:52 +08002482 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2483 }
2484
Harald Welte0abe5a62009-07-23 19:06:52 +02002485 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002486}
2487
2488static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2489{
2490 struct gsm_mncc *dtmf = arg;
2491 struct msgb *msg = gsm48_msgb_alloc();
2492 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2493
Harald Welte03740842009-06-10 23:11:52 +08002494 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2495
2496 /* keypad */
2497 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Weltefdc93d92010-03-07 23:40:35 +01002498 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte03740842009-06-10 23:11:52 +08002499
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002500 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002501}
2502
2503static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2504{
2505 struct gsm_mncc *dtmf = arg;
2506 struct msgb *msg = gsm48_msgb_alloc();
2507 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2508
Harald Welte03740842009-06-10 23:11:52 +08002509 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2510
2511 /* cause */
2512 if (dtmf->fields & MNCC_F_CAUSE)
Harald Weltefdc93d92010-03-07 23:40:35 +01002513 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte03740842009-06-10 23:11:52 +08002514 else
Harald Weltefdc93d92010-03-07 23:40:35 +01002515 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte03740842009-06-10 23:11:52 +08002516
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002517 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002518}
2519
2520static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2521{
2522 struct msgb *msg = gsm48_msgb_alloc();
2523 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2524
Harald Welte03740842009-06-10 23:11:52 +08002525 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2526
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002527 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002528}
2529
2530static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2531{
2532 struct gsm_mncc dtmf;
2533
2534 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2535 dtmf.callref = trans->callref;
2536
Harald Welte0abe5a62009-07-23 19:06:52 +02002537 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte03740842009-06-10 23:11:52 +08002538}
2539
2540static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2541{
2542 struct gsm48_hdr *gh = msgb_l3(msg);
2543 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2544 struct tlv_parsed tp;
2545 struct gsm_mncc modify;
2546
2547 memset(&modify, 0, sizeof(struct gsm_mncc));
2548 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002549 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002550 /* bearer capability */
2551 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2552 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002553 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002554 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2555 }
2556
2557 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2558
Harald Welte0abe5a62009-07-23 19:06:52 +02002559 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002560}
2561
2562static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2563{
2564 struct gsm_mncc *modify = arg;
2565 struct msgb *msg = gsm48_msgb_alloc();
2566 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2567
Harald Welte03740842009-06-10 23:11:52 +08002568 gh->msg_type = GSM48_MT_CC_MODIFY;
2569
2570 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2571
2572 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002573 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002574
2575 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2576
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002577 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002578}
2579
2580static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2581{
2582 struct gsm48_hdr *gh = msgb_l3(msg);
2583 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2584 struct tlv_parsed tp;
2585 struct gsm_mncc modify;
2586
2587 gsm48_stop_cc_timer(trans);
2588
2589 memset(&modify, 0, sizeof(struct gsm_mncc));
2590 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002591 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte03740842009-06-10 23:11:52 +08002592 /* bearer capability */
2593 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2594 modify.fields |= MNCC_F_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002595 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002596 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2597 }
2598
2599 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2600
Harald Welte0abe5a62009-07-23 19:06:52 +02002601 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002602}
2603
2604static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2605{
2606 struct gsm_mncc *modify = arg;
2607 struct msgb *msg = gsm48_msgb_alloc();
2608 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2609
Harald Welte03740842009-06-10 23:11:52 +08002610 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2611
2612 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002613 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002614
2615 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2616
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002617 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002618}
2619
2620static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2621{
2622 struct gsm48_hdr *gh = msgb_l3(msg);
2623 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2624 struct tlv_parsed tp;
2625 struct gsm_mncc modify;
2626
2627 gsm48_stop_cc_timer(trans);
2628
2629 memset(&modify, 0, sizeof(struct gsm_mncc));
2630 modify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002631 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte03740842009-06-10 23:11:52 +08002632 /* bearer capability */
2633 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2634 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Weltefdc93d92010-03-07 23:40:35 +01002635 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte03740842009-06-10 23:11:52 +08002636 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2637 }
2638 /* cause */
2639 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2640 modify.fields |= MNCC_F_CAUSE;
Harald Weltefdc93d92010-03-07 23:40:35 +01002641 gsm48_decode_cause(&modify.cause,
Harald Welte03740842009-06-10 23:11:52 +08002642 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2643 }
2644
2645 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2646
Harald Welte0abe5a62009-07-23 19:06:52 +02002647 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte03740842009-06-10 23:11:52 +08002648}
2649
2650static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2651{
2652 struct gsm_mncc *modify = arg;
2653 struct msgb *msg = gsm48_msgb_alloc();
2654 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2655
Harald Welte03740842009-06-10 23:11:52 +08002656 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2657
2658 /* bearer capability */
Harald Weltefdc93d92010-03-07 23:40:35 +01002659 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte03740842009-06-10 23:11:52 +08002660 /* cause */
Harald Weltefdc93d92010-03-07 23:40:35 +01002661 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte03740842009-06-10 23:11:52 +08002662
2663 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2664
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002665 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002666}
2667
2668static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2669{
2670 struct gsm_mncc *notify = arg;
2671 struct msgb *msg = gsm48_msgb_alloc();
2672 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2673
Harald Welte03740842009-06-10 23:11:52 +08002674 gh->msg_type = GSM48_MT_CC_NOTIFY;
2675
2676 /* notify */
Harald Weltefdc93d92010-03-07 23:40:35 +01002677 gsm48_encode_notify(msg, notify->notify);
Harald Welte03740842009-06-10 23:11:52 +08002678
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002679 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002680}
2681
2682static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2683{
2684 struct gsm48_hdr *gh = msgb_l3(msg);
2685 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2686// struct tlv_parsed tp;
2687 struct gsm_mncc notify;
2688
2689 memset(&notify, 0, sizeof(struct gsm_mncc));
2690 notify.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002691// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte03740842009-06-10 23:11:52 +08002692 if (payload_len >= 1)
Harald Weltefdc93d92010-03-07 23:40:35 +01002693 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte03740842009-06-10 23:11:52 +08002694
Harald Welte0abe5a62009-07-23 19:06:52 +02002695 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte03740842009-06-10 23:11:52 +08002696}
2697
2698static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2699{
2700 struct gsm_mncc *user = arg;
2701 struct msgb *msg = gsm48_msgb_alloc();
2702 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2703
Harald Welte03740842009-06-10 23:11:52 +08002704 gh->msg_type = GSM48_MT_CC_USER_INFO;
2705
2706 /* user-user */
2707 if (user->fields & MNCC_F_USERUSER)
Harald Weltefdc93d92010-03-07 23:40:35 +01002708 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte03740842009-06-10 23:11:52 +08002709 /* more data */
2710 if (user->more)
Harald Weltefdc93d92010-03-07 23:40:35 +01002711 gsm48_encode_more(msg);
Harald Welte03740842009-06-10 23:11:52 +08002712
Holger Hans Peter Freyther5bc4d1d2010-06-15 13:57:40 +08002713 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte03740842009-06-10 23:11:52 +08002714}
2715
2716static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2717{
2718 struct gsm48_hdr *gh = msgb_l3(msg);
2719 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2720 struct tlv_parsed tp;
2721 struct gsm_mncc user;
2722
2723 memset(&user, 0, sizeof(struct gsm_mncc));
2724 user.callref = trans->callref;
Harald Welte705f2942010-03-02 23:18:30 +01002725 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte03740842009-06-10 23:11:52 +08002726 /* user-user */
2727 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2728 user.fields |= MNCC_F_USERUSER;
Harald Weltefdc93d92010-03-07 23:40:35 +01002729 gsm48_decode_useruser(&user.useruser,
Harald Welte03740842009-06-10 23:11:52 +08002730 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2731 }
2732 /* more data */
2733 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2734 user.more = 1;
2735
Harald Welte0abe5a62009-07-23 19:06:52 +02002736 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte03740842009-06-10 23:11:52 +08002737}
2738
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002739static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte03740842009-06-10 23:11:52 +08002740{
2741 struct gsm_mncc *mode = arg;
2742
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002743 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte03740842009-06-10 23:11:52 +08002744}
2745
2746static struct downstate {
2747 u_int32_t states;
2748 int type;
2749 int (*rout) (struct gsm_trans *trans, void *arg);
2750} downstatelist[] = {
2751 /* mobile originating call establishment */
2752 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2753 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2754 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2755 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2756 {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 */
2757 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2758 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2759 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2760 /* mobile terminating call establishment */
2761 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2762 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2763 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2764 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2765 /* signalling during call */
2766 {SBIT(GSM_CSTATE_ACTIVE),
2767 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2768 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2769 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2770 {ALL_STATES,
2771 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2772 {ALL_STATES,
2773 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2774 {ALL_STATES,
2775 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2776 {SBIT(GSM_CSTATE_ACTIVE),
2777 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2778 {SBIT(GSM_CSTATE_ACTIVE),
2779 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2780 {SBIT(GSM_CSTATE_ACTIVE),
2781 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2782 {SBIT(GSM_CSTATE_ACTIVE),
2783 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2784 {SBIT(GSM_CSTATE_ACTIVE),
2785 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2786 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2787 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2788 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2789 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2790 {SBIT(GSM_CSTATE_ACTIVE),
2791 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2792 /* clearing */
2793 {SBIT(GSM_CSTATE_INITIATED),
2794 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2795 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2796 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2797 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2798 MNCC_REL_REQ, gsm48_cc_tx_release},
2799 /* special */
2800 {ALL_STATES,
Holger Hans Peter Freythercf0f3362009-10-22 15:13:00 +02002801 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte03740842009-06-10 23:11:52 +08002802};
2803
2804#define DOWNSLLEN \
2805 (sizeof(downstatelist) / sizeof(struct downstate))
2806
2807
2808int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2809{
Harald Welteaa60edb2009-08-09 18:52:33 +02002810 int i, rc = 0;
Harald Welte03740842009-06-10 23:11:52 +08002811 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002812 struct gsm_subscriber_connection *conn = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002813 struct gsm_bts *bts = NULL;
Harald Welte03740842009-06-10 23:11:52 +08002814 struct gsm_mncc *data = arg, rel;
2815
2816 /* handle special messages */
2817 switch(msg_type) {
2818 case MNCC_BRIDGE:
2819 return tch_bridge(net, arg);
2820 case MNCC_FRAME_DROP:
Harald Welte3971ad52009-12-19 22:23:05 +01002821 return tch_recv_mncc(net, data->callref, 0);
Harald Welte03740842009-06-10 23:11:52 +08002822 case MNCC_FRAME_RECV:
Harald Welte3971ad52009-12-19 22:23:05 +01002823 return tch_recv_mncc(net, data->callref, 1);
2824 case GSM_TCHF_FRAME:
2825 /* Find callref */
2826 trans = trans_find_by_callref(net, data->callref);
2827 if (!trans)
2828 return -EIO;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002829 if (!trans->conn)
Harald Welte3971ad52009-12-19 22:23:05 +01002830 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002831 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welte3971ad52009-12-19 22:23:05 +01002832 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002833 bts = trans->conn->lchan->ts->trx->bts;
Harald Welte3971ad52009-12-19 22:23:05 +01002834 switch (bts->type) {
2835 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002836 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welte3971ad52009-12-19 22:23:05 +01002837 return 0;
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002838 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002839 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002840 return trau_send_frame(trans->conn->lchan, arg);
Harald Welte3971ad52009-12-19 22:23:05 +01002841 default:
2842 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2843 }
2844 return -EINVAL;
Harald Welte03740842009-06-10 23:11:52 +08002845 }
2846
2847 memset(&rel, 0, sizeof(struct gsm_mncc));
2848 rel.callref = data->callref;
2849
2850 /* Find callref */
Harald Weltec2189a62009-07-23 18:56:43 +02002851 trans = trans_find_by_callref(net, data->callref);
Harald Welte03740842009-06-10 23:11:52 +08002852
2853 /* Callref unknown */
2854 if (!trans) {
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002855 struct gsm_subscriber *subscr;
2856
Harald Welte6e1536e2009-07-04 10:11:24 +02002857 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte03740842009-06-10 23:11:52 +08002858 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2859 "Received '%s' from MNCC with "
2860 "unknown callref %d\n", data->called.number,
2861 get_mncc_name(msg_type), data->callref);
2862 /* Invalid call reference */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002863 return mncc_release_ind(net, NULL, data->callref,
2864 GSM48_CAUSE_LOC_PRN_S_LU,
2865 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte03740842009-06-10 23:11:52 +08002866 }
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002867 if (!data->called.number[0] && !data->imsi[0]) {
2868 DEBUGP(DCC, "(bts - trx - ts - ti) "
2869 "Received '%s' from MNCC with "
2870 "no number or IMSI\n", get_mncc_name(msg_type));
2871 /* Invalid number */
2872 return mncc_release_ind(net, NULL, data->callref,
2873 GSM48_CAUSE_LOC_PRN_S_LU,
2874 GSM48_CC_CAUSE_INV_NR_FORMAT);
2875 }
Harald Welte03740842009-06-10 23:11:52 +08002876 /* New transaction due to setup, find subscriber */
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002877 if (data->called.number[0])
Harald Welte75350412009-07-23 18:46:00 +02002878 subscr = subscr_get_by_extension(net,
2879 data->called.number);
Andreas Eversberg9eaa5da2009-06-15 23:22:09 +02002880 else
Harald Welte75350412009-07-23 18:46:00 +02002881 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte03740842009-06-10 23:11:52 +08002882 /* If subscriber is not found */
2883 if (!subscr) {
2884 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2885 "Received '%s' from MNCC with "
2886 "unknown subscriber %s\n", data->called.number,
2887 get_mncc_name(msg_type), data->called.number);
2888 /* Unknown subscriber */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002889 return mncc_release_ind(net, NULL, data->callref,
2890 GSM48_CAUSE_LOC_PRN_S_LU,
2891 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte03740842009-06-10 23:11:52 +08002892 }
2893 /* If subscriber is not "attached" */
2894 if (!subscr->lac) {
2895 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2896 "Received '%s' from MNCC with "
2897 "detached subscriber %s\n", data->called.number,
2898 get_mncc_name(msg_type), data->called.number);
2899 subscr_put(subscr);
2900 /* Temporarily out of order */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002901 return mncc_release_ind(net, NULL, data->callref,
2902 GSM48_CAUSE_LOC_PRN_S_LU,
2903 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte03740842009-06-10 23:11:52 +08002904 }
2905 /* Create transaction */
Harald Weltec2189a62009-07-23 18:56:43 +02002906 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2907 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08002908 DEBUGP(DCC, "No memory for trans.\n");
2909 subscr_put(subscr);
2910 /* Ressource unavailable */
Andreas Eversbergb992a8a2009-06-14 22:14:12 +08002911 mncc_release_ind(net, NULL, data->callref,
2912 GSM48_CAUSE_LOC_PRN_S_LU,
2913 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte03740842009-06-10 23:11:52 +08002914 return -ENOMEM;
2915 }
Harald Welte03740842009-06-10 23:11:52 +08002916 /* Find lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002917 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01002918
Harald Welte03740842009-06-10 23:11:52 +08002919 /* If subscriber has no lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002920 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002921 /* find transaction with this subscriber already paging */
2922 llist_for_each_entry(transt, &net->trans_list, entry) {
2923 /* Transaction of our lchan? */
2924 if (transt == trans ||
2925 transt->subscr != subscr)
2926 continue;
2927 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2928 "Received '%s' from MNCC with "
2929 "unallocated channel, paging already "
2930 "started.\n", bts->nr,
2931 data->called.number,
2932 get_mncc_name(msg_type));
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002933 subscr_put(subscr);
2934 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002935 return 0;
2936 }
2937 /* store setup informations until paging was successfull */
Harald Weltec2189a62009-07-23 18:56:43 +02002938 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Welte2a3a81b2009-08-01 19:31:47 +02002939 /* Trigger paging */
2940 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2941 setup_trig_pag_evt, subscr);
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002942 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002943 return 0;
2944 }
2945 /* Assign lchan */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002946 trans->conn = conn;
Holger Hans Peter Freythere2a3c9f2009-10-27 14:21:14 +01002947 subscr_put(subscr);
Harald Welte03740842009-06-10 23:11:52 +08002948 }
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002949
2950 if (trans->conn)
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002951 conn = trans->conn;
Harald Welte03740842009-06-10 23:11:52 +08002952
2953 /* if paging did not respond yet */
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002954 if (!conn) {
Harald Welte03740842009-06-10 23:11:52 +08002955 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02002956 "Received '%s' from MNCC in paging state\n",
Harald Welte03740842009-06-10 23:11:52 +08002957 (trans->subscr)?(trans->subscr->extension):"-",
2958 get_mncc_name(msg_type));
Harald Weltebbc636a2009-06-11 14:23:20 +08002959 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2960 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte03740842009-06-10 23:11:52 +08002961 if (msg_type == MNCC_REL_REQ)
2962 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2963 else
2964 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2965 trans->callref = 0;
Harald Weltec2189a62009-07-23 18:56:43 +02002966 trans_free(trans);
Harald Welte03740842009-06-10 23:11:52 +08002967 return rc;
2968 }
2969
2970 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2971 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freyther2f4af772010-06-16 13:23:55 +08002972 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte03740842009-06-10 23:11:52 +08002973 trans->transaction_id,
Holger Hans Peter Freyther4009da42010-03-23 07:00:22 +01002974 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltec2189a62009-07-23 18:56:43 +02002975 get_mncc_name(msg_type), trans->cc.state,
Harald Welteb30935e2010-03-25 12:13:02 +08002976 gsm48_cc_state_name(trans->cc.state));
Harald Welte03740842009-06-10 23:11:52 +08002977
2978 /* Find function for current state and message */
2979 for (i = 0; i < DOWNSLLEN; i++)
2980 if ((msg_type == downstatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02002981 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08002982 break;
2983 if (i == DOWNSLLEN) {
2984 DEBUGP(DCC, "Message unhandled at this state.\n");
2985 return 0;
2986 }
2987
2988 rc = downstatelist[i].rout(trans, arg);
2989
2990 return rc;
2991}
2992
2993
2994static struct datastate {
2995 u_int32_t states;
2996 int type;
2997 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2998} datastatelist[] = {
2999 /* mobile originating call establishment */
3000 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3001 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3002 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3003 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3004 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3005 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3006 /* mobile terminating call establishment */
3007 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3008 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3009 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3010 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freyther71135142010-03-29 08:47:44 +02003011 {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 +08003012 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3013 /* signalling during call */
3014 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3015 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3016 {SBIT(GSM_CSTATE_ACTIVE),
3017 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3018 {ALL_STATES,
3019 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3020 {ALL_STATES,
3021 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3022 {ALL_STATES,
3023 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3024 {SBIT(GSM_CSTATE_ACTIVE),
3025 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3026 {SBIT(GSM_CSTATE_ACTIVE),
3027 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3028 {SBIT(GSM_CSTATE_ACTIVE),
3029 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3030 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3031 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3032 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3033 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3034 {SBIT(GSM_CSTATE_ACTIVE),
3035 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3036 /* clearing */
3037 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3038 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3039 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3040 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3041 {ALL_STATES, /* 5.4.3.4 */
3042 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3043};
3044
3045#define DATASLLEN \
3046 (sizeof(datastatelist) / sizeof(struct datastate))
3047
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003048static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003049{
3050 struct gsm48_hdr *gh = msgb_l3(msg);
3051 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte4861c822009-07-23 21:21:14 +02003052 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltec2189a62009-07-23 18:56:43 +02003053 struct gsm_trans *trans = NULL;
Harald Welte03740842009-06-10 23:11:52 +08003054 int i, rc = 0;
Harald Welte59b04682009-06-10 05:40:52 +08003055
Harald Welte03740842009-06-10 23:11:52 +08003056 if (msg_type & 0x80) {
3057 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3058 return -EINVAL;
Harald Welte59b04682009-06-10 05:40:52 +08003059 }
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003060
Harald Welte03740842009-06-10 23:11:52 +08003061 /* Find transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003062 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltec2189a62009-07-23 18:56:43 +02003063
Harald Welte4861c822009-07-23 21:21:14 +02003064 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte03740842009-06-10 23:11:52 +08003065 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003066 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003067 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Welteb30935e2010-03-25 12:13:02 +08003068 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3069 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte03740842009-06-10 23:11:52 +08003070
3071 /* Create transaction */
3072 if (!trans) {
Harald Welte4861c822009-07-23 21:21:14 +02003073 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte03740842009-06-10 23:11:52 +08003074 "creating new trans.\n", transaction_id);
3075 /* Create transaction */
Holger Hans Peter Freyther065b8112010-03-23 06:41:45 +01003076 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltec2189a62009-07-23 18:56:43 +02003077 transaction_id, new_callref++);
3078 if (!trans) {
Harald Welte03740842009-06-10 23:11:52 +08003079 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freyther5a38f922010-06-16 12:30:50 +08003080 rc = gsm48_tx_simple(trans->conn,
Harald Welte4861c822009-07-23 21:21:14 +02003081 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte03740842009-06-10 23:11:52 +08003082 GSM48_MT_CC_RELEASE_COMPL);
3083 return -ENOMEM;
3084 }
Harald Welte03740842009-06-10 23:11:52 +08003085 /* Assign transaction */
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003086 trans->conn = conn;
Harald Welte03740842009-06-10 23:11:52 +08003087 }
3088
3089 /* find function for current state and message */
3090 for (i = 0; i < DATASLLEN; i++)
3091 if ((msg_type == datastatelist[i].type)
Harald Weltec2189a62009-07-23 18:56:43 +02003092 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte03740842009-06-10 23:11:52 +08003093 break;
3094 if (i == DATASLLEN) {
3095 DEBUGP(DCC, "Message unhandled at this state.\n");
3096 return 0;
3097 }
3098
3099 rc = datastatelist[i].rout(trans, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003100
3101 return rc;
3102}
3103
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003104/* Create a dummy to wait five seconds */
3105static void release_anchor(struct gsm_subscriber_connection *conn)
3106{
3107 if (!conn->anch_operation)
3108 return;
3109
3110 bsc_del_timer(&conn->anch_operation->timeout);
3111 talloc_free(conn->anch_operation);
3112 conn->anch_operation = NULL;
3113}
3114
3115static void anchor_timeout(void *_data)
3116{
3117 struct gsm_subscriber_connection *con = _data;
3118
3119 release_anchor(con);
3120 msc_release_connection(con);
3121}
3122
3123int gsm0408_new_conn(struct gsm_subscriber_connection *conn)
3124{
3125 conn->anch_operation = talloc_zero(conn, struct gsm_anchor_operation);
3126 if (!conn->anch_operation)
3127 return -1;
3128
3129 conn->anch_operation->timeout.data = conn;
3130 conn->anch_operation->timeout.cb = anchor_timeout;
3131 bsc_schedule_timer(&conn->anch_operation->timeout, 5, 0);
3132 return 0;
3133}
3134
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003135/* here we get data from the BSC level... */
3136int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte59b04682009-06-10 05:40:52 +08003137{
3138 struct gsm48_hdr *gh = msgb_l3(msg);
3139 u_int8_t pdisc = gh->proto_discr & 0x0f;
3140 int rc = 0;
Harald Weltef6a38ec2009-12-29 11:49:12 +01003141
Holger Hans Peter Freyther48549392010-06-21 10:34:03 +08003142 if (silent_call_reroute(conn, msg))
3143 return silent_call_rx(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003144
3145 switch (pdisc) {
3146 case GSM48_PDISC_CC:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003147 release_anchor(conn);
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003148 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003149 break;
3150 case GSM48_PDISC_MM:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003151 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003152 break;
3153 case GSM48_PDISC_RR:
Holger Hans Peter Freyther96aeb2b2010-06-17 17:14:35 +08003154 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003155 break;
3156 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003157 release_anchor(conn);
Holger Hans Peter Freytherb9795792010-06-17 16:41:25 +08003158 rc = gsm0411_rcv_sms(conn, msg);
Harald Welte59b04682009-06-10 05:40:52 +08003159 break;
3160 case GSM48_PDISC_MM_GPRS:
3161 case GSM48_PDISC_SM_GPRS:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003162 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3163 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003164 break;
Harald Welte03115042009-10-16 08:32:58 +02003165 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freyther425cfc42010-07-05 15:34:16 +08003166 release_anchor(conn);
Holger Hans Peter Freyther320c5e12010-06-17 17:35:57 +08003167 rc = handle_rcv_ussd(conn, msg);
Harald Welte03115042009-10-16 08:32:58 +02003168 break;
Harald Welte59b04682009-06-10 05:40:52 +08003169 default:
Harald Welte60e5f4f2009-12-24 12:13:17 +01003170 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3171 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte59b04682009-06-10 05:40:52 +08003172 break;
3173 }
3174
3175 return rc;
3176}
3177
Harald Welte3c062072009-07-28 18:25:29 +02003178/*
3179 * This will be ran by the linker when loading the DSO. We use it to
3180 * do system initialization, e.g. registration of signal handlers.
3181 */
3182static __attribute__((constructor)) void on_dso_load_0408(void)
3183{
Harald Welte3c062072009-07-28 18:25:29 +02003184 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3185}