blob: 8dece0b320f8f2a4c1e9de6cf264b5c3579e66e4 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
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>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Sylvain Munaut30a15382009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000034#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/msgb.h>
36#include <osmocore/bitvec.h>
37#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010040#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
44#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000045#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000046#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000047#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000048#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000049#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020050#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010051#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010052#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020053#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020054#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010055#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080056#include <openbsc/bsc_api.h>
Harald Welte52b1f982008-12-23 20:25:15 +000057
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010059void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020060
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080061int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080062static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte65e74cc2008-12-29 01:55:35 +000063 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010064static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000065
Harald Welte52b1f982008-12-23 20:25:15 +000066struct gsm_lai {
67 u_int16_t mcc;
68 u_int16_t mnc;
69 u_int16_t lac;
70};
71
Harald Welte4bfdfe72009-06-10 23:11:52 +080072static u_int32_t new_callref = 0x80000001;
73
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080074static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
75 struct gsm_trans *trans)
76{
77 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
78
79 /* if we get passed a transaction reference, do some common
80 * work that the caller no longer has to do */
81 if (trans) {
82 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
83 msg->lchan = trans->conn->lchan;
84 }
85
86
87 if (msg->lchan) {
88 msg->trx = msg->lchan->ts->trx;
89 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
90 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
91 "Sending '%s' to MS.\n", msg->trx->bts->nr,
92 msg->trx->nr, msg->lchan->ts->nr,
93 gh->proto_discr & 0xf0,
94 gsm48_cc_msg_name(gh->msg_type));
95 else
96 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
97 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
98 msg->trx->nr, msg->lchan->ts->nr,
99 gh->proto_discr, gh->msg_type);
100 }
101
102 msg->l3h = msg->data;
103 return gsm0808_submit_dtap(conn, msg, 0);
104}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100105
106static void release_security_operation(struct gsm_subscriber_connection *conn)
107{
108 if (!conn->sec_operation)
109 return;
110
111 talloc_free(conn->sec_operation);
112 conn->sec_operation = NULL;
113 put_subscr_con(conn);
114}
115
116static void allocate_security_operation(struct gsm_subscriber_connection *conn)
117{
118 use_subscr_con(conn)
119
120 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
121 struct gsm_security_operation);
122}
123
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800124static int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
125 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100126{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800127 struct gsm_network *net = conn->bts->network;
128 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100129 struct gsm_security_operation *op;
130 struct gsm_auth_tuple atuple;
131 int status = -1, rc;
132
133 /* Check if we _can_ enable encryption. Cases where we can't:
134 * - Encryption disabled in config
135 * - Channel already secured (nothing to do)
136 * - Subscriber equipment doesn't support configured encryption
137 */
138 if (!net->a5_encryption) {
139 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800140 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100141 DEBUGP(DMM, "Requesting to secure an already secure channel");
142 status = GSM_SECURITY_SUCCEEDED;
143 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
144 net->a5_encryption)) {
145 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
146 status = GSM_SECURITY_NOAVAIL;
147 }
148
149 /* If not done yet, try to get info for this user */
150 if (status < 0) {
151 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
152 if (rc <= 0)
153 status = GSM_SECURITY_NOAVAIL;
154 }
155
156 /* Are we done yet ? */
157 if (status >= 0)
158 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800159 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100160 0;
161
162 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800163 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100164 return -EBUSY;
165
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800166 allocate_security_operation(conn);
167 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100168 op->cb = cb;
169 op->cb_data = cb_data;
170 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
171
172 /* FIXME: Should start a timer for completion ... */
173
174 /* Then do whatever is needed ... */
175 if (rc == 1) {
176 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800177 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100178 } else if (rc == 2) {
179 /* Start ciphering directly */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800180 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
181 conn->lchan->encr.key_len = 8;
182 memcpy(conn->lchan->encr.key, op->atuple.kc, 8);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800184 return gsm48_send_rr_ciph_mode(conn->lchan, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100185 }
186
187 return -EINVAL; /* not reached */
188}
189
Holger Freyther73487a22008-12-31 18:53:57 +0000190static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
191 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000192{
193 if (!subscriber)
194 return 0;
195
Holger Freyther73487a22008-12-31 18:53:57 +0000196 /*
197 * Do not send accept yet as more information should arrive. Some
198 * phones will not send us the information and we will have to check
199 * what we want to do with that.
200 */
201 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
202 return 0;
203
Jan Luebbe06513f22009-08-12 12:48:00 +0200204 switch (subscriber->net->auth_policy) {
205 case GSM_AUTH_POLICY_CLOSED:
206 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200207 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200208 if (subscriber->authorized)
209 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200210 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200211 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000212 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200213 default:
214 return 0;
215 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000216}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000217
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100218static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000219{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100220 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000221 return;
222
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100223 bsc_del_timer(&conn->loc_operation->updating_timer);
224 talloc_free(conn->loc_operation);
225 conn->loc_operation = 0;
226 put_subscr_con(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000227}
228
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100229static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000230{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100231 use_subscr_con(conn)
232 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000233
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100234 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200235 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000236}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000237
Sylvain Munaut267fba02009-12-24 00:28:01 +0100238static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
239 struct msgb *msg, void *data, void *param)
240{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800241 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100242 int rc = 0;
243
244 switch (event) {
245 case GSM_SECURITY_AUTH_FAILED:
246 release_loc_updating_req(conn);
247 break;
248
249 case GSM_SECURITY_NOAVAIL:
250 case GSM_SECURITY_SUCCEEDED:
251 /* We're all good */
252 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800253 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800254 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100255 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800256 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100257 }
258
259 /* call subscr_update after putting the loc_upd_acc
260 * in the transmit queue, since S_SUBSCR_ATTACHED might
261 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800262 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100263 GSM_SUBSCRIBER_UPDATE_ATTACHED);
264
265 /* try to close channel ASAP */
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800266 release_loc_updating_req(conn);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800267 lchan_auto_release(conn->lchan);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100268 break;
269
270 default:
271 rc = -EINVAL;
272 };
273
274 return rc;
275}
276
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100277static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000278{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100279 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800280 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100281 conn->loc_operation->key_seq,
282 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000283 return 0;
284}
285
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800286void gsm0408_clear_request(struct gsm_subscriber_connection* conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000287{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800288 struct gsm_trans *trans, *temp;
Holger Freyther7c19f742009-06-06 13:54:35 +0000289 /*
290 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800291 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000292 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800293 release_loc_updating_req(conn);
294 release_security_operation(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000295
Harald Welte4bfdfe72009-06-10 23:11:52 +0800296 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200297 /* FIXME: this is not neccessarily the right thing to do, we should
298 * only set trans->lchan to NULL and wait for another lchan to be
299 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800300 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
301 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200302 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800303 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000304}
305
Holger Freyther429e7762008-12-30 13:28:30 +0000306/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800307int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000308{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800309 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800310 struct msgb *msg;
311
312 counter_inc(bts->network->stats.loc_upd_resp.reject);
313
314 msg = gsm48_create_loc_upd_rej(cause);
315 if (!msg) {
316 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
317 return -1;
318 }
Harald Welte52b1f982008-12-23 20:25:15 +0000319
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800320 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000321
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100322 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100323 "LAC=%u BTS=%u\n", conn->subscr ?
324 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800325 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100326
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800327 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000328}
329
330/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800331int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000332{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800333 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000334 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000335 struct gsm48_hdr *gh;
336 struct gsm48_loc_area_id *lai;
337 u_int8_t *mid;
338
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800339 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000340
341 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
342 gh->proto_discr = GSM48_PDISC_MM;
343 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
344
345 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100346 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000347 bts->network->network_code, bts->location_area_code);
348
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200349 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200350 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000351
352 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
353
Harald Welteffa55a42009-12-22 19:07:32 +0100354 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100355
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800356 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000357}
358
Harald Weltebf5e8df2009-02-03 12:59:45 +0000359/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800360static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000361{
362 struct msgb *msg = gsm48_msgb_alloc();
363 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000364
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800365 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000366
367 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
368 gh->proto_discr = GSM48_PDISC_MM;
369 gh->msg_type = GSM48_MT_MM_ID_REQ;
370 gh->data[0] = id_type;
371
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800372 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000373}
374
Harald Welte231ad4f2008-12-27 11:15:38 +0000375
Harald Weltebf5e8df2009-02-03 12:59:45 +0000376/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800377static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000378{
379 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000380 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200381 struct gsm_bts *bts = lchan->ts->trx->bts;
382 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000383 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200384 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000385
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200386 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000387 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000388 mi_type, mi_string);
389
Harald Welte7659de12009-12-13 12:39:18 +0100390 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
391
Harald Welte75a983f2008-12-27 21:34:06 +0000392 switch (mi_type) {
393 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200394 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100395 if (!conn->subscr) {
396 conn->subscr = subscr_get_by_imsi(net, mi_string);
397 if (!conn->subscr)
398 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200399 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100400 if (conn->loc_operation)
401 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000402 break;
403 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000404 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000405 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100406 if (conn->subscr) {
407 db_subscriber_assoc_imei(conn->subscr, mi_string);
408 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200409 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100410 if (conn->loc_operation)
411 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000412 break;
413 }
Holger Freyther73487a22008-12-31 18:53:57 +0000414
415 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100416 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000417}
418
Harald Welte255539c2008-12-28 02:26:27 +0000419
420static void loc_upd_rej_cb(void *data)
421{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100422 struct gsm_subscriber_connection *conn = data;
423 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100424 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000425
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800426 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800427 release_loc_updating_req(conn);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000428 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000429}
430
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100431static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000432{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100433 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
434 conn->loc_operation->updating_timer.data = conn;
435 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000436}
437
Harald Welte2a139372009-02-22 21:14:55 +0000438static const char *lupd_name(u_int8_t type)
439{
440 switch (type) {
441 case GSM48_LUPD_NORMAL:
442 return "NORMAL";
443 case GSM48_LUPD_PERIODIC:
444 return "PEROIDOC";
445 case GSM48_LUPD_IMSI_ATT:
446 return "IMSI ATTACH";
447 default:
448 return "UNKNOWN";
449 }
450}
451
Harald Weltebf5e8df2009-02-03 12:59:45 +0000452/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800453static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000454{
Harald Welte8470bf22008-12-25 23:28:35 +0000455 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000456 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800457 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800458 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000459 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200460 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000461 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000462
Harald Welte8470bf22008-12-25 23:28:35 +0000463 lu = (struct gsm48_loc_upd_req *) gh->data;
464
465 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000466
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200467 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000468
Harald Weltea0368542009-06-27 02:58:43 +0200469 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000470 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000471
Harald Welte7659de12009-12-13 12:39:18 +0100472 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
473
Harald Welte24ff6ee2009-12-22 00:41:05 +0100474 switch (lu->type) {
475 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100476 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100477 break;
478 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100479 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100480 break;
481 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100482 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100483 break;
484 }
485
Holger Freythereaf04692009-06-06 13:54:44 +0000486 /*
487 * Pseudo Spoof detection: Just drop a second/concurrent
488 * location updating request.
489 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100490 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200491 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100492 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800493 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000494 return 0;
495 }
496
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800497 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000498
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200499 conn->loc_operation->key_seq = lu->key_seq;
500
Harald Welte52b1f982008-12-23 20:25:15 +0000501 switch (mi_type) {
502 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200503 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000504 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800505 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100506 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000507
Jan Luebbe370b41d2009-08-12 10:19:34 +0200508 /* look up subscriber based on IMSI, create if not found */
509 subscr = subscr_get_by_imsi(bts->network, mi_string);
510 if (!subscr) {
511 subscr = db_create_subscriber(bts->network, mi_string);
512 }
Harald Welte4b634542008-12-27 01:55:51 +0000513 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000514 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200515 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000516 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200517 subscr = subscr_get_by_tmsi(bts->network,
518 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000519 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000520 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800521 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100522 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000523 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200524 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800525 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200526 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000527 break;
528 case GSM_MI_TYPE_IMEI:
529 case GSM_MI_TYPE_IMEISV:
530 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200531 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000532 break;
533 default:
Harald Weltea0368542009-06-27 02:58:43 +0200534 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000535 break;
536 }
537
Harald Welte24516ea2009-07-04 10:18:00 +0200538 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100539 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200540
Harald Welte4bfdfe72009-06-10 23:11:52 +0800541 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200542 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800543 /* FIXME: request id? close channel? */
544 return -EINVAL;
545 }
546
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100547 conn->subscr = subscr;
548 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000549
Harald Welte24516ea2009-07-04 10:18:00 +0200550 /* check if we can let the subscriber into our network immediately
551 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100552 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000553}
554
Harald Welte4bfdfe72009-06-10 23:11:52 +0800555#if 0
556static u_int8_t to_bcd8(u_int8_t val)
557{
558 return ((val / 10) << 4) | (val % 10);
559}
560#endif
561
Harald Weltedb253af2008-12-30 17:56:55 +0000562/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800563int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000564{
565 struct msgb *msg = gsm48_msgb_alloc();
566 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800567 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000568 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200569 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800570#if 0
571 time_t cur_t;
572 struct tm* cur_time;
573 int tz15min;
574#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000575
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800576 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000577
578 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
579 gh->proto_discr = GSM48_PDISC_MM;
580 gh->msg_type = GSM48_MT_MM_INFO;
581
582 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200583#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000584 name_len = strlen(net->name_long);
585 /* 10.5.3.5a */
586 ptr8 = msgb_put(msg, 3);
587 ptr8[0] = GSM48_IE_NAME_LONG;
588 ptr8[1] = name_len*2 +1;
589 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
590
591 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
592 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000593 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000594
595 /* FIXME: Use Cell Broadcast, not UCS-2, since
596 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200597#endif
598 name_len = (strlen(net->name_long)*7)/8;
599 name_pad = (8 - strlen(net->name_long)*7)%8;
600 if (name_pad > 0)
601 name_len++;
602 /* 10.5.3.5a */
603 ptr8 = msgb_put(msg, 3);
604 ptr8[0] = GSM48_IE_NAME_LONG;
605 ptr8[1] = name_len +1;
606 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
607
608 ptr8 = msgb_put(msg, name_len);
609 gsm_7bit_encode(ptr8, net->name_long);
610
Harald Weltedb253af2008-12-30 17:56:55 +0000611 }
612
613 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200614#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000615 name_len = strlen(net->name_short);
616 /* 10.5.3.5a */
617 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200618 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000619 ptr8[1] = name_len*2 + 1;
620 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
621
Harald Weltee872cb12009-01-01 00:33:37 +0000622 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000623 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000624 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200625#endif
626 name_len = (strlen(net->name_short)*7)/8;
627 name_pad = (8 - strlen(net->name_short)*7)%8;
628 if (name_pad > 0)
629 name_len++;
630 /* 10.5.3.5a */
631 ptr8 = (u_int8_t *) msgb_put(msg, 3);
632 ptr8[0] = GSM48_IE_NAME_SHORT;
633 ptr8[1] = name_len +1;
634 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
635
636 ptr8 = msgb_put(msg, name_len);
637 gsm_7bit_encode(ptr8, net->name_short);
638
Harald Weltedb253af2008-12-30 17:56:55 +0000639 }
640
641#if 0
642 /* Section 10.5.3.9 */
643 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800644 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000645 ptr8 = msgb_put(msg, 8);
646 ptr8[0] = GSM48_IE_NET_TIME_TZ;
647 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
648 ptr8[2] = to_bcd8(cur_time->tm_mon);
649 ptr8[3] = to_bcd8(cur_time->tm_mday);
650 ptr8[4] = to_bcd8(cur_time->tm_hour);
651 ptr8[5] = to_bcd8(cur_time->tm_min);
652 ptr8[6] = to_bcd8(cur_time->tm_sec);
653 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
654 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800655 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000656 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800657 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000658#endif
659
Daniel Willmanneea93372009-08-13 03:42:07 +0200660 DEBUGP(DMM, "-> MM INFO\n");
661
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800662 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000663}
664
Harald Welte7984d5c2009-08-12 22:56:50 +0200665/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800666int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200667{
668 struct msgb *msg = gsm48_msgb_alloc();
669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200670 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200671
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100672 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200673
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800674 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200675 gh->proto_discr = GSM48_PDISC_MM;
676 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
677
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100678 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200679
Harald Welte7984d5c2009-08-12 22:56:50 +0200680 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200681 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200682 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200683
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800684 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200685}
686
687/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800688int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200689{
690 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800691 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200692}
693
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800694static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000695{
Harald Welte4b634542008-12-27 01:55:51 +0000696 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800697 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000698}
Harald Welteba4cf162009-01-10 01:49:35 +0000699
700/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100701static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000702 enum gsm48_reject_value value)
703{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800704 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000705
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800706 msg = gsm48_create_mm_serv_rej(value);
707 if (!msg) {
708 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
709 return -1;
710 }
Harald Welteba4cf162009-01-10 01:49:35 +0000711
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800712 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100713 msg->lchan = conn->lchan;
714 use_subscr_con(conn);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800715 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000716}
717
Sylvain Munautba87f452009-12-24 00:28:46 +0100718static int _gsm48_rx_mm_serv_req_sec_cb(
719 unsigned int hooknum, unsigned int event,
720 struct msgb *msg, void *data, void *param)
721{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800722 struct gsm_subscriber_connection *conn = data;
Sylvain Munautba87f452009-12-24 00:28:46 +0100723 int rc = 0;
724
725 switch (event) {
726 case GSM_SECURITY_AUTH_FAILED:
727 /* Nothing to do */
728 break;
729
730 case GSM_SECURITY_NOAVAIL:
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800731 rc = gsm48_tx_mm_serv_ack(conn);
Sylvain Munautba87f452009-12-24 00:28:46 +0100732 break;
733
734 case GSM_SECURITY_SUCCEEDED:
735 /* nothing to do. CIPHER MODE COMMAND is
736 * implicit CM SERV ACK */
737 break;
738
739 default:
740 rc = -EINVAL;
741 };
742
743 return rc;
744}
745
Harald Welte4ed0e922009-01-10 03:17:30 +0000746/*
747 * Handle CM Service Requests
748 * a) Verify that the packet is long enough to contain the information
749 * we require otherwsie reject with INCORRECT_MESSAGE
750 * b) Try to parse the TMSI. If we do not have one reject
751 * c) Check that we know the subscriber with the TMSI otherwise reject
752 * with a HLR cause
753 * d) Set the subscriber on the gsm_lchan and accept
754 */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800755static int gsm48_rx_mm_serv_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4b634542008-12-27 01:55:51 +0000756{
Harald Welteba4cf162009-01-10 01:49:35 +0000757 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200758 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000759
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800760 struct gsm_bts *bts = conn->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000761 struct gsm_subscriber *subscr;
762 struct gsm48_hdr *gh = msgb_l3(msg);
763 struct gsm48_service_request *req =
764 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800765 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000766 u_int8_t classmark2_len = gh->data[1];
767 u_int8_t *classmark2 = gh->data+2;
768 u_int8_t mi_len = *(classmark2 + classmark2_len);
769 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000770
Harald Weltec9e02182009-05-01 19:07:53 +0000771 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000772 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000773 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800774 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000775 GSM48_REJECT_INCORRECT_MESSAGE);
776 }
777
778 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000779 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800780 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000781 GSM48_REJECT_INCORRECT_MESSAGE);
782 }
783
Harald Weltec9e02182009-05-01 19:07:53 +0000784 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000785 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000786 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800787 return gsm48_tx_mm_serv_rej(conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000788 GSM48_REJECT_INCORRECT_MESSAGE);
789 }
790
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200791 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000792 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000793 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000794
Harald Welte7659de12009-12-13 12:39:18 +0100795 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
796
Harald Welte3ac7f102009-08-10 10:12:45 +0200797 if (is_siemens_bts(bts))
798 send_siemens_mrpci(msg->lchan, classmark2-1);
799
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200800 subscr = subscr_get_by_tmsi(bts->network,
801 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000802
Harald Welte2a139372009-02-22 21:14:55 +0000803 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000804 if (!subscr)
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800805 return gsm48_tx_mm_serv_rej(conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000806 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
807
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800808 if (!conn->subscr)
809 conn->subscr = subscr;
810 else if (conn->subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100811 subscr_put(subscr); /* lchan already has a ref, don't need another one */
812 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000813 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
814 subscr_put(subscr);
815 }
816
Harald Weltec2e302d2009-07-05 14:08:13 +0200817 subscr->equipment.classmark2_len = classmark2_len;
818 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
819 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000820
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800821 return gsm48_secure_channel(conn, req->cipher_key_seq,
Sylvain Munautba87f452009-12-24 00:28:46 +0100822 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000823}
824
Harald Welte2a139372009-02-22 21:14:55 +0000825static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
826{
Harald Welte9176bd42009-07-23 18:46:00 +0200827 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000828 struct gsm48_hdr *gh = msgb_l3(msg);
829 struct gsm48_imsi_detach_ind *idi =
830 (struct gsm48_imsi_detach_ind *) gh->data;
831 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200832 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800833 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000834
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200835 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000836 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
837 mi_type, mi_string);
838
Harald Welteffa55a42009-12-22 19:07:32 +0100839 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100840
Harald Welte2a139372009-02-22 21:14:55 +0000841 switch (mi_type) {
842 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200843 subscr = subscr_get_by_tmsi(bts->network,
844 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000845 break;
846 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200847 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000848 break;
849 case GSM_MI_TYPE_IMEI:
850 case GSM_MI_TYPE_IMEISV:
851 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000852 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000853 break;
854 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000855 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000856 break;
857 }
858
Holger Freyther4a49e772009-04-12 05:37:29 +0000859 if (subscr) {
860 subscr_update(subscr, msg->trx->bts,
861 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100862 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200863
864 subscr->equipment.classmark1 = idi->classmark1;
865 db_sync_equipment(&subscr->equipment);
866
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000867 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000868 } else
Harald Welte2a139372009-02-22 21:14:55 +0000869 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
870
Harald Welte31981a02009-12-20 09:58:40 +0100871 /* FIXME: iterate over all transactions and release them,
872 * imagine an IMSI DETACH happening during an active call! */
873
Harald Welteb83d9382009-12-12 21:00:48 +0100874 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100875 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100876
Harald Welte2a139372009-02-22 21:14:55 +0000877 return 0;
878}
879
Harald Welted2a7f5a2009-06-05 20:08:20 +0000880static int gsm48_rx_mm_status(struct msgb *msg)
881{
882 struct gsm48_hdr *gh = msgb_l3(msg);
883
884 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
885
886 return 0;
887}
888
Sylvain Munaut30a15382009-12-24 00:27:26 +0100889/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800890static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100891{
892 struct gsm48_hdr *gh = msgb_l3(msg);
893 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800894 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100895
Sylvain Munautc593cf12010-06-10 23:51:41 +0200896 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100897 hexdump(ar->sres, 4));
898
899 /* Safety check */
900 if (!conn->sec_operation) {
901 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
902 return -EIO;
903 }
904
905 /* Validate SRES */
906 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
907 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200908
909 DEBUGPC(DMM, "Invalid (expected %s)\n",
910 hexdump(conn->sec_operation->atuple.sres, 4));
911
Sylvain Munaut30a15382009-12-24 00:27:26 +0100912 if (cb)
913 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800914 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100915
916 release_security_operation(conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800917 return gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100918 }
919
Sylvain Munautc593cf12010-06-10 23:51:41 +0200920 DEBUGPC(DMM, "OK\n");
921
Sylvain Munaut30a15382009-12-24 00:27:26 +0100922 /* Start ciphering */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800923 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
924 conn->lchan->encr.key_len = 8;
925 memcpy(conn->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100926
927 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
928}
929
Harald Weltebf5e8df2009-02-03 12:59:45 +0000930/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800931static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000932{
933 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800934 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000935
936 switch (gh->msg_type & 0xbf) {
937 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200938 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800939 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000940 break;
941 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800942 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000943 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000944 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800945 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000946 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000947 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000948 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000949 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000950 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000951 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800952 conn->subscr ?
953 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000954 "unknown subscriber");
955 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000956 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000957 rc = gsm48_rx_mm_imsi_detach_ind(msg);
958 break;
959 case GSM48_MT_MM_CM_REEST_REQ:
960 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
961 break;
962 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800963 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000964 break;
965 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100966 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000967 gh->msg_type);
968 break;
969 }
970
971 return rc;
972}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000973
Harald Welte2d35ae62009-02-06 12:02:13 +0000974/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200975static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000976{
Harald Welte9176bd42009-07-23 18:46:00 +0200977 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000978 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800979 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000980 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200981 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200982 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800983 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000984 int rc = 0;
985
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800986 resp = (struct gsm48_pag_resp *) &gh->data[0];
987 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
988 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000989 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
990 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200991
Harald Weltefe18d8f2009-02-22 21:14:24 +0000992 switch (mi_type) {
993 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200994 subscr = subscr_get_by_tmsi(bts->network,
995 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000996 break;
997 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200998 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000999 break;
1000 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001001
1002 if (!subscr) {
1003 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001004 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001005 return -EINVAL;
1006 }
1007 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001008 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001009
Harald Weltec2e302d2009-07-05 14:08:13 +02001010 subscr->equipment.classmark2_len = *classmark2_lv;
1011 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1012 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001013
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001014 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001015 return rc;
1016}
1017
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001018static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001019{
1020 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001021 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001022 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1023 u_int8_t cm2_len, cm3_len = 0;
1024 u_int8_t *cm2, *cm3 = NULL;
1025
1026 DEBUGP(DRR, "CLASSMARK CHANGE ");
1027
1028 /* classmark 2 */
1029 cm2_len = gh->data[0];
1030 cm2 = &gh->data[1];
1031 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1032
1033 if (payload_len > cm2_len + 1) {
1034 /* we must have a classmark3 */
1035 if (gh->data[cm2_len+1] != 0x20) {
1036 DEBUGPC(DRR, "ERR CM3 TAG\n");
1037 return -EINVAL;
1038 }
1039 if (cm2_len > 3) {
1040 DEBUGPC(DRR, "CM2 too long!\n");
1041 return -EINVAL;
1042 }
1043
1044 cm3_len = gh->data[cm2_len+2];
1045 cm3 = &gh->data[cm2_len+3];
1046 if (cm3_len > 14) {
1047 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1048 return -EINVAL;
1049 }
1050 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1051 }
1052 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001053 subscr->equipment.classmark2_len = cm2_len;
1054 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001055 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001056 subscr->equipment.classmark3_len = cm3_len;
1057 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001058 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001059 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001060 }
1061
Harald Weltef7c43522009-06-09 20:24:21 +00001062 return 0;
1063}
1064
Harald Weltecf5b3592009-05-01 18:28:42 +00001065static int gsm48_rx_rr_status(struct msgb *msg)
1066{
1067 struct gsm48_hdr *gh = msgb_l3(msg);
1068
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001069 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001070 rr_cause_name(gh->data[0]));
1071
1072 return 0;
1073}
1074
Harald Weltef7c43522009-06-09 20:24:21 +00001075static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1076{
Harald Welted12b0fd2009-12-15 21:36:05 +01001077 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001078
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001079 /* This shouldn't actually end up here, as RSL treats
1080 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1081 * directly into gsm48_parse_meas_rep */
1082 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001083 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001084
1085 return 0;
1086}
1087
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001088static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001089{
1090 struct gsm48_hdr *gh = msgb_l3(msg);
1091 u_int8_t apdu_id_flags;
1092 u_int8_t apdu_len;
1093 u_int8_t *apdu_data;
1094
1095 apdu_id_flags = gh->data[0];
1096 apdu_len = gh->data[1];
1097 apdu_data = gh->data+2;
1098
1099 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1100 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1101
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001102 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001103}
1104
Sylvain Munaut30a15382009-12-24 00:27:26 +01001105/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001106static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001107{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001108 gsm_cbfn *cb;
1109 int rc = 0;
1110
1111 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1112
1113 /* Safety check */
1114 if (!conn->sec_operation) {
1115 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1116 return -EIO;
1117 }
1118
1119 /* FIXME: check for MI (if any) */
1120
1121 /* Call back whatever was in progress (if anything) ... */
1122 cb = conn->sec_operation->cb;
1123 if (cb) {
1124 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001125 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001126 }
1127
1128 /* Complete the operation */
1129 release_security_operation(conn);
1130
1131 return rc;
1132}
1133
Harald Welted011e8b2009-11-29 22:45:52 +01001134/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001135static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001136{
1137 struct gsm48_hdr *gh = msgb_l3(msg);
1138
1139 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1140 rr_cause_name(gh->data[0]));
1141
1142 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1143 /* FIXME: release old channel */
1144
1145 return 0;
1146}
1147
1148/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001149static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001150{
1151 struct gsm48_hdr *gh = msgb_l3(msg);
1152
1153 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1154 rr_cause_name(gh->data[0]));
1155
1156 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1157 /* FIXME: release allocated new channel */
1158
1159 return 0;
1160}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001161
Harald Weltebf5e8df2009-02-03 12:59:45 +00001162/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001163static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001164{
1165 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001166 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001167
1168 switch (gh->msg_type) {
1169 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001170 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001171 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001172 case GSM48_MT_RR_GPRS_SUSP_REQ:
1173 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1174 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001175 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001176 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001177 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001178 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001179 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001180 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001181 case GSM48_MT_RR_STATUS:
1182 rc = gsm48_rx_rr_status(msg);
1183 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001184 case GSM48_MT_RR_MEAS_REP:
1185 rc = gsm48_rx_rr_meas_rep(msg);
1186 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001187 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001188 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001189 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001190 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001191 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001192 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001193 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001194 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001195 break;
1196 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001197 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001198 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001199 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001200 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1201 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001202 break;
1203 }
1204
Harald Welte2d35ae62009-02-06 12:02:13 +00001205 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001206}
1207
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001208int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001209 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001210{
1211 struct msgb *msg = gsm48_msgb_alloc();
1212 struct gsm48_hdr *gh;
1213
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001214 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001215
1216 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1217 apdu_id, apdu_len);
1218
1219 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1220 gh->proto_discr = GSM48_PDISC_RR;
1221 gh->msg_type = GSM48_MT_RR_APP_INFO;
1222 gh->data[0] = apdu_id;
1223 gh->data[1] = apdu_len;
1224 memcpy(gh->data+2, apdu, apdu_len);
1225
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001226 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001227}
1228
Harald Welte4bc90a12008-12-27 16:32:52 +00001229/* Call Control */
1230
Harald Welte7584aea2009-02-11 11:44:12 +00001231/* The entire call control code is written in accordance with Figure 7.10c
1232 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1233 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1234 * it for voice */
1235
Harald Welte4bfdfe72009-06-10 23:11:52 +08001236static void new_cc_state(struct gsm_trans *trans, int state)
1237{
1238 if (state > 31 || state < 0)
1239 return;
1240
1241 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001242 gsm48_cc_state_name(trans->cc.state),
1243 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001244
Harald Weltedcaf5652009-07-23 18:56:43 +02001245 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001246}
1247
1248static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001249{
1250 struct msgb *msg = gsm48_msgb_alloc();
1251 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1252 u_int8_t *cause, *call_state;
1253
Harald Welte4bc90a12008-12-27 16:32:52 +00001254 gh->msg_type = GSM48_MT_CC_STATUS;
1255
1256 cause = msgb_put(msg, 3);
1257 cause[0] = 2;
1258 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1259 cause[2] = 0x80 | 30; /* response to status inquiry */
1260
1261 call_state = msgb_put(msg, 1);
1262 call_state[0] = 0xc0 | 0x00;
1263
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001264 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001265}
1266
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001267static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001268 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001269{
1270 struct msgb *msg = gsm48_msgb_alloc();
1271 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1272
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001273 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001274
Harald Welte6f4b7532008-12-29 00:39:37 +00001275 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001276 gh->msg_type = msg_type;
1277
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001278 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001279}
1280
Harald Welte4bfdfe72009-06-10 23:11:52 +08001281static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1282{
Harald Weltedcaf5652009-07-23 18:56:43 +02001283 if (bsc_timer_pending(&trans->cc.timer)) {
1284 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1285 bsc_del_timer(&trans->cc.timer);
1286 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001287 }
1288}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001289
Harald Welte4bfdfe72009-06-10 23:11:52 +08001290static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1291 int msg_type, struct gsm_mncc *mncc)
1292{
1293 struct msgb *msg;
1294
1295 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001296 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001297 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001298 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001299 trans->conn->lchan->ts->trx->bts->nr,
1300 trans->conn->lchan->ts->trx->nr,
1301 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302 (trans->subscr)?(trans->subscr->extension):"-",
1303 get_mncc_name(msg_type));
1304 else
1305 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1306 "Sending '%s' to MNCC.\n",
1307 (trans->subscr)?(trans->subscr->extension):"-",
1308 get_mncc_name(msg_type));
1309 else
1310 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1311 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1312
1313 mncc->msg_type = msg_type;
1314
Harald Welte966636f2009-06-26 19:39:35 +02001315 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001316 if (!msg)
1317 return -ENOMEM;
1318 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1319 msgb_enqueue(&net->upqueue, msg);
1320
1321 return 0;
1322}
1323
1324int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1325 u_int32_t callref, int location, int value)
1326{
1327 struct gsm_mncc rel;
1328
Harald Welte92f70c52009-06-12 01:54:08 +08001329 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001331 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001332 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1333}
1334
Harald Weltedcaf5652009-07-23 18:56:43 +02001335/* Call Control Specific transaction release.
1336 * gets called by trans_free, DO NOT CALL YOURSELF! */
1337void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001338{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001339 gsm48_stop_cc_timer(trans);
1340
1341 /* send release to L4, if callref still exists */
1342 if (trans->callref) {
1343 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001344 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001345 GSM48_CAUSE_LOC_PRN_S_LU,
1346 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001348 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001349 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001350 if (trans->conn)
1351 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352}
1353
1354static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001355
Harald Welte09e38af2009-02-16 22:52:23 +00001356/* call-back from paging the B-end of the connection */
1357static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001358 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001359{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001360 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 struct gsm_subscriber *subscr = param;
1362 struct gsm_trans *transt, *tmp;
1363 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001364
Harald Welte09e38af2009-02-16 22:52:23 +00001365 if (hooknum != GSM_HOOK_RR_PAGING)
1366 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001367
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368 if (!subscr)
1369 return -EINVAL;
1370 net = subscr->net;
1371 if (!net) {
1372 DEBUGP(DCC, "Error Network not set!\n");
1373 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001374 }
Harald Welte7584aea2009-02-11 11:44:12 +00001375
Harald Welte4bfdfe72009-06-10 23:11:52 +08001376 /* check all tranactions (without lchan) for subscriber */
1377 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001378 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001379 continue;
1380 switch (event) {
1381 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001382 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001383 break;
1384 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1385 subscr->extension);
1386 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001387 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001388 transt->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001389 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 }
1391 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001392 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001393 break;
1394 case GSM_PAGING_EXPIRED:
1395 DEBUGP(DCC, "Paging subscr %s expired!\n",
1396 subscr->extension);
1397 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001398 mncc_release_ind(transt->subscr->net, transt,
1399 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001400 GSM48_CAUSE_LOC_PRN_S_LU,
1401 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001403 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001404 break;
1405 }
1406 }
Harald Welte09e38af2009-02-16 22:52:23 +00001407 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001408}
Harald Welte7584aea2009-02-11 11:44:12 +00001409
Harald Welteda7ab742009-12-19 22:23:05 +01001410static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1411
Harald Welte805f6442009-07-28 18:25:29 +02001412/* some other part of the code sends us a signal */
1413static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1414 void *handler_data, void *signal_data)
1415{
1416 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001417 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001418 struct gsm_network *net;
1419 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001420
1421 if (subsys != SS_ABISIP)
1422 return 0;
1423
1424 /* in case we use direct BTS-to-BTS RTP */
1425 if (ipacc_rtp_direct)
1426 return 0;
1427
Harald Welte805f6442009-07-28 18:25:29 +02001428 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001429 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001430 /* check if any transactions on this lchan still have
1431 * a tch_recv_mncc request pending */
1432 net = lchan->ts->trx->bts->network;
1433 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001434 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001435 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1436 tch_recv_mncc(net, trans->callref, 1);
1437 }
1438 }
Harald Welte805f6442009-07-28 18:25:29 +02001439 break;
Harald Welte805f6442009-07-28 18:25:29 +02001440 }
1441
1442 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001443}
1444
Harald Welte49f48b82009-02-17 15:29:33 +00001445/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001446static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001447{
Harald Welte11fa29c2009-02-19 17:24:39 +00001448 struct gsm_bts *bts = lchan->ts->trx->bts;
1449 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001450 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001451
Harald Welte11fa29c2009-02-19 17:24:39 +00001452 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1453 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1454 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1455
1456 if (bts->type != remote_bts->type) {
1457 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1458 return -EINVAL;
1459 }
Harald Welteda7ab742009-12-19 22:23:05 +01001460
1461 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001462 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001463 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001464 if (!ipacc_rtp_direct) {
1465 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001466 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001467 if (rc < 0)
1468 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001469 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001470#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001471
1472 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301473 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1474 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001475 } else {
1476 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301477 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1478 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301479 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001480 if (rc < 0)
1481 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301482 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1483 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301484 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001485 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001486 break;
1487 case GSM_BTS_TYPE_BS11:
1488 trau_mux_map_lchan(lchan, remote_lchan);
1489 break;
1490 default:
1491 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001492 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001493 }
Harald Welte49f48b82009-02-17 15:29:33 +00001494
1495 return 0;
1496}
1497
Harald Welte4bfdfe72009-06-10 23:11:52 +08001498/* bridge channels of two transactions */
1499static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001500{
Harald Weltedcaf5652009-07-23 18:56:43 +02001501 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1502 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001503
Harald Welte4bfdfe72009-06-10 23:11:52 +08001504 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001505 return -EIO;
1506
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001507 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001508 return -EIO;
1509
1510 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001511 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001512}
1513
Harald Welteda7ab742009-12-19 22:23:05 +01001514/* enable receive of channels to MNCC upqueue */
1515static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001516{
1517 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001518 struct gsm_lchan *lchan;
1519 struct gsm_bts *bts;
1520 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001521
Harald Welte4bfdfe72009-06-10 23:11:52 +08001522 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001523 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524 if (!trans)
1525 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001526 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001527 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001528 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001529 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001530
Harald Welteda7ab742009-12-19 22:23:05 +01001531 switch (bts->type) {
1532 case GSM_BTS_TYPE_NANOBTS:
1533 if (ipacc_rtp_direct) {
1534 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1535 return -EINVAL;
1536 }
1537 /* in case, we don't have a RTP socket yet, we note this
1538 * in the transaction and try later */
1539 if (!lchan->abis_ip.rtp_socket) {
1540 trans->tch_recv = enable;
1541 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1542 return 0;
1543 }
1544 if (enable) {
1545 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001546 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001547 if (rc < 0)
1548 return rc;
1549 /* assign socket to application interface */
1550 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1551 net, callref);
1552 } else
1553 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1554 net, 0);
1555 break;
1556 case GSM_BTS_TYPE_BS11:
1557 if (enable)
1558 return trau_recv_lchan(lchan, callref);
1559 return trau_mux_unmap(NULL, callref);
1560 break;
1561 default:
1562 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1563 return -EINVAL;
1564 }
1565
1566 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001567}
1568
Harald Welte4bfdfe72009-06-10 23:11:52 +08001569static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1570{
1571 DEBUGP(DCC, "-> STATUS ENQ\n");
1572 return gsm48_cc_tx_status(trans, msg);
1573}
1574
1575static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1576static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1577
1578static void gsm48_cc_timeout(void *arg)
1579{
1580 struct gsm_trans *trans = arg;
1581 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001582 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1583 int mo_location = GSM48_CAUSE_LOC_USER;
1584 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1585 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001586 struct gsm_mncc mo_rel, l4_rel;
1587
1588 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1589 mo_rel.callref = trans->callref;
1590 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1591 l4_rel.callref = trans->callref;
1592
Harald Weltedcaf5652009-07-23 18:56:43 +02001593 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001594 case 0x303:
1595 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001596 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001597 break;
1598 case 0x310:
1599 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001600 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001601 break;
1602 case 0x313:
1603 disconnect = 1;
1604 /* unknown, did not find it in the specs */
1605 break;
1606 case 0x301:
1607 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001608 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001609 break;
1610 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001611 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001612 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001613 gsm48_cc_tx_release(trans, &trans->cc.msg);
1614 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001615 break; /* stay in release state */
1616 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001617 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001618 return;
1619// release = 1;
1620// l4_cause = 14;
1621// break;
1622 case 0x306:
1623 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001624 mo_cause = trans->cc.msg.cause.value;
1625 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001626 break;
1627 case 0x323:
1628 disconnect = 1;
1629 break;
1630 default:
1631 release = 1;
1632 }
1633
1634 if (release && trans->callref) {
1635 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001636 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 l4_location, l4_cause);
1638 trans->callref = 0;
1639 }
1640
1641 if (disconnect && trans->callref) {
1642 /* process disconnect towards layer 4 */
1643 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001644 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001645 }
1646
1647 /* process disconnect towards mobile station */
1648 if (disconnect || release) {
1649 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001650 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1651 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1652 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 mo_rel.cause.diag_len = 3;
1654
1655 if (disconnect)
1656 gsm48_cc_tx_disconnect(trans, &mo_rel);
1657 if (release)
1658 gsm48_cc_tx_release(trans, &mo_rel);
1659 }
1660
1661}
1662
1663static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1664 int sec, int micro)
1665{
1666 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001667 trans->cc.timer.cb = gsm48_cc_timeout;
1668 trans->cc.timer.data = trans;
1669 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1670 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001671}
1672
1673static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1674{
1675 struct gsm48_hdr *gh = msgb_l3(msg);
1676 u_int8_t msg_type = gh->msg_type & 0xbf;
1677 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1678 struct tlv_parsed tp;
1679 struct gsm_mncc setup;
1680
1681 memset(&setup, 0, sizeof(struct gsm_mncc));
1682 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001683 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 /* emergency setup is identified by msg_type */
1685 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1686 setup.emergency = 1;
1687
1688 /* use subscriber as calling party number */
1689 if (trans->subscr) {
1690 setup.fields |= MNCC_F_CALLING;
1691 strncpy(setup.calling.number, trans->subscr->extension,
1692 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001693 strncpy(setup.imsi, trans->subscr->imsi,
1694 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001695 }
1696 /* bearer capability */
1697 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1698 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001699 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1701 }
1702 /* facility */
1703 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1704 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001705 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001706 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1707 }
1708 /* called party bcd number */
1709 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1710 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001711 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1713 }
1714 /* user-user */
1715 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1716 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001717 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1719 }
1720 /* ss-version */
1721 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1722 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001723 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1725 }
1726 /* CLIR suppression */
1727 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1728 setup.clir.sup = 1;
1729 /* CLIR invocation */
1730 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1731 setup.clir.inv = 1;
1732 /* cc cap */
1733 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1734 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001735 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1737 }
1738
Harald Welte4bfdfe72009-06-10 23:11:52 +08001739 new_cc_state(trans, GSM_CSTATE_INITIATED);
1740
Harald Welte (local)47df3992009-12-26 19:45:03 +01001741 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1742 subscr_name(trans->subscr), trans->subscr->extension,
1743 setup.called.number);
1744
Harald Welte4bfdfe72009-06-10 23:11:52 +08001745 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001746 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747
Harald Welte13cac662009-07-29 12:10:35 +02001748 /* MNCC code will modify the channel asynchronously, we should
1749 * ipaccess-bind only after the modification has been made to the
1750 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 return 0;
1752}
1753
1754static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001755{
1756 struct msgb *msg = gsm48_msgb_alloc();
1757 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001758 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001759 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001760
Harald Welte7ccf7782009-02-17 01:43:01 +00001761 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001762
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 /* transaction id must not be assigned */
1764 if (trans->transaction_id != 0xff) { /* unasssigned */
1765 DEBUGP(DCC, "TX Setup with assigned transaction. "
1766 "This is not allowed!\n");
1767 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001768 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001769 GSM48_CAUSE_LOC_PRN_S_LU,
1770 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001771 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001772 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001773 return rc;
1774 }
1775
1776 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001777 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1778 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001780 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001781 GSM48_CAUSE_LOC_PRN_S_LU,
1782 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001784 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 return rc;
1786 }
Harald Welte78283ef2009-07-23 21:36:44 +02001787 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001788
Harald Welte65e74cc2008-12-29 01:55:35 +00001789 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001790
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001792
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793 /* bearer capability */
1794 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001795 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 /* facility */
1797 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001798 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 /* progress */
1800 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001801 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 /* calling party BCD number */
1803 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001804 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 /* called party BCD number */
1806 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001807 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* user-user */
1809 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001810 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 /* redirecting party BCD number */
1812 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001813 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 /* signal */
1815 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001816 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817
1818 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001819
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001820 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001821}
1822
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1824{
1825 struct gsm48_hdr *gh = msgb_l3(msg);
1826 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1827 struct tlv_parsed tp;
1828 struct gsm_mncc call_conf;
1829
1830 gsm48_stop_cc_timer(trans);
1831 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1832
1833 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1834 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001835 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001836#if 0
1837 /* repeat */
1838 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1839 call_conf.repeat = 1;
1840 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1841 call_conf.repeat = 2;
1842#endif
1843 /* bearer capability */
1844 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1845 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001846 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001847 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1848 }
1849 /* cause */
1850 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1851 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001852 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001853 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1854 }
1855 /* cc cap */
1856 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1857 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001858 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1860 }
1861
1862 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1863
Harald Welte596fed42009-07-23 19:06:52 +02001864 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1865 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001866}
1867
1868static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1869{
1870 struct gsm_mncc *proceeding = arg;
1871 struct msgb *msg = gsm48_msgb_alloc();
1872 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1873
Harald Welte4bfdfe72009-06-10 23:11:52 +08001874 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1875
1876 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1877
1878 /* bearer capability */
1879 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* facility */
1882 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884 /* progress */
1885 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001886 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001887
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001888 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001889}
1890
1891static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1892{
1893 struct gsm48_hdr *gh = msgb_l3(msg);
1894 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1895 struct tlv_parsed tp;
1896 struct gsm_mncc alerting;
1897
1898 gsm48_stop_cc_timer(trans);
1899 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1900
1901 memset(&alerting, 0, sizeof(struct gsm_mncc));
1902 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001903 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001904 /* facility */
1905 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1906 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001907 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001908 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1909 }
1910
1911 /* progress */
1912 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1913 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001914 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001915 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1916 }
1917 /* ss-version */
1918 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1919 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001920 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001921 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1922 }
1923
1924 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1925
Harald Welte596fed42009-07-23 19:06:52 +02001926 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1927 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928}
1929
1930static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1931{
1932 struct gsm_mncc *alerting = arg;
1933 struct msgb *msg = gsm48_msgb_alloc();
1934 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1935
Harald Welte4bfdfe72009-06-10 23:11:52 +08001936 gh->msg_type = GSM48_MT_CC_ALERTING;
1937
1938 /* facility */
1939 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001940 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 /* progress */
1942 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001943 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944 /* user-user */
1945 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001946 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947
1948 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1949
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001950 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001951}
1952
1953static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1954{
1955 struct gsm_mncc *progress = arg;
1956 struct msgb *msg = gsm48_msgb_alloc();
1957 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1958
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959 gh->msg_type = GSM48_MT_CC_PROGRESS;
1960
1961 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963 /* user-user */
1964 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001965 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001966
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001967 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968}
1969
1970static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1971{
1972 struct gsm_mncc *connect = arg;
1973 struct msgb *msg = gsm48_msgb_alloc();
1974 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1975
Harald Welte4bfdfe72009-06-10 23:11:52 +08001976 gh->msg_type = GSM48_MT_CC_CONNECT;
1977
1978 gsm48_stop_cc_timer(trans);
1979 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1980
1981 /* facility */
1982 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001983 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 /* progress */
1985 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001986 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987 /* connected number */
1988 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001989 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990 /* user-user */
1991 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001992 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001993
1994 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1995
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001996 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001997}
1998
1999static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2000{
2001 struct gsm48_hdr *gh = msgb_l3(msg);
2002 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2003 struct tlv_parsed tp;
2004 struct gsm_mncc connect;
2005
2006 gsm48_stop_cc_timer(trans);
2007
2008 memset(&connect, 0, sizeof(struct gsm_mncc));
2009 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002010 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002011 /* use subscriber as connected party number */
2012 if (trans->subscr) {
2013 connect.fields |= MNCC_F_CONNECTED;
2014 strncpy(connect.connected.number, trans->subscr->extension,
2015 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002016 strncpy(connect.imsi, trans->subscr->imsi,
2017 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002018 }
2019 /* facility */
2020 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2021 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002022 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2024 }
2025 /* user-user */
2026 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2027 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002028 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002029 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2030 }
2031 /* ss-version */
2032 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2033 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2036 }
2037
2038 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2039
Harald Welte596fed42009-07-23 19:06:52 +02002040 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041}
2042
2043
2044static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2045{
2046 struct gsm_mncc connect_ack;
2047
2048 gsm48_stop_cc_timer(trans);
2049
2050 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2051
2052 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2053 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002054 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002055 &connect_ack);
2056}
2057
2058static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2059{
2060 struct msgb *msg = gsm48_msgb_alloc();
2061 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2062
Harald Welte4bfdfe72009-06-10 23:11:52 +08002063 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2064
2065 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2066
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002067 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068}
2069
2070static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2071{
2072 struct gsm48_hdr *gh = msgb_l3(msg);
2073 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2074 struct tlv_parsed tp;
2075 struct gsm_mncc disc;
2076
2077 gsm48_stop_cc_timer(trans);
2078
2079 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2080
2081 memset(&disc, 0, sizeof(struct gsm_mncc));
2082 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002083 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084 /* cause */
2085 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2086 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002087 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002088 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2089 }
2090 /* facility */
2091 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2092 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002093 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2095 }
2096 /* user-user */
2097 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2098 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002099 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2101 }
2102 /* ss-version */
2103 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2104 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002105 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2107 }
2108
Harald Welte596fed42009-07-23 19:06:52 +02002109 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110
2111}
2112
Harald Weltec66b71c2009-06-11 14:23:20 +08002113static struct gsm_mncc_cause default_cause = {
2114 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2115 .coding = 0,
2116 .rec = 0,
2117 .rec_val = 0,
2118 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2119 .diag_len = 0,
2120 .diag = { 0 },
2121};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122
2123static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2124{
2125 struct gsm_mncc *disc = arg;
2126 struct msgb *msg = gsm48_msgb_alloc();
2127 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2128
Harald Welte4bfdfe72009-06-10 23:11:52 +08002129 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2130
2131 gsm48_stop_cc_timer(trans);
2132 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2133
2134 /* cause */
2135 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002136 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002137 else
Harald Welte55c8f352010-03-07 23:40:35 +01002138 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002139
2140 /* facility */
2141 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002142 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143 /* progress */
2144 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002145 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146 /* user-user */
2147 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002148 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149
2150 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002151 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002152
2153 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2154
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002155 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002156}
2157
2158static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2159{
2160 struct gsm48_hdr *gh = msgb_l3(msg);
2161 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2162 struct tlv_parsed tp;
2163 struct gsm_mncc rel;
2164 int rc;
2165
2166 gsm48_stop_cc_timer(trans);
2167
2168 memset(&rel, 0, sizeof(struct gsm_mncc));
2169 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002170 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 /* cause */
2172 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2173 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002174 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002175 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2176 }
2177 /* facility */
2178 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2179 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002180 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2182 }
2183 /* user-user */
2184 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2185 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2188 }
2189 /* ss-version */
2190 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2191 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2194 }
2195
Harald Weltedcaf5652009-07-23 18:56:43 +02002196 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002198 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002200 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002201 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002202 GSM48_MT_CC_RELEASE_COMPL);
2203 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 }
2205
2206 new_cc_state(trans, GSM_CSTATE_NULL);
2207
2208 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002209 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002210
2211 return rc;
2212}
2213
2214static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2215{
2216 struct gsm_mncc *rel = arg;
2217 struct msgb *msg = gsm48_msgb_alloc();
2218 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2219
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 gh->msg_type = GSM48_MT_CC_RELEASE;
2221
2222 trans->callref = 0;
2223
2224 gsm48_stop_cc_timer(trans);
2225 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2226
2227 /* cause */
2228 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002229 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 /* facility */
2231 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002232 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233 /* user-user */
2234 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002235 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236
Harald Weltedcaf5652009-07-23 18:56:43 +02002237 trans->cc.T308_second = 0;
2238 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002239
Harald Weltedcaf5652009-07-23 18:56:43 +02002240 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2242
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002243 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002244}
2245
2246static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2247{
2248 struct gsm48_hdr *gh = msgb_l3(msg);
2249 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2250 struct tlv_parsed tp;
2251 struct gsm_mncc rel;
2252 int rc = 0;
2253
2254 gsm48_stop_cc_timer(trans);
2255
2256 memset(&rel, 0, sizeof(struct gsm_mncc));
2257 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002258 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 /* cause */
2260 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2261 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002262 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002263 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2264 }
2265 /* facility */
2266 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2267 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002268 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002269 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2270 }
2271 /* user-user */
2272 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2273 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2276 }
2277 /* ss-version */
2278 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2279 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002280 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2282 }
2283
2284 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002285 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002286 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002287 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002288 MNCC_REJ_IND, &rel);
2289 break;
2290 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002291 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002292 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002293 /* FIXME: in case of multiple calls, we can't simply
2294 * hang up here ! */
2295 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002296 break;
2297 default:
Harald Welte596fed42009-07-23 19:06:52 +02002298 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 MNCC_REL_IND, &rel);
2300 }
2301 }
2302
2303 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002304 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002305
2306 return rc;
2307}
2308
2309static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2310{
2311 struct gsm_mncc *rel = arg;
2312 struct msgb *msg = gsm48_msgb_alloc();
2313 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2314
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2316
2317 trans->callref = 0;
2318
2319 gsm48_stop_cc_timer(trans);
2320
2321 /* cause */
2322 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324 /* facility */
2325 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002326 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 /* user-user */
2328 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002329 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330
Harald Weltedcaf5652009-07-23 18:56:43 +02002331 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002332
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002333 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334}
2335
2336static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2337{
2338 struct gsm48_hdr *gh = msgb_l3(msg);
2339 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2340 struct tlv_parsed tp;
2341 struct gsm_mncc fac;
2342
2343 memset(&fac, 0, sizeof(struct gsm_mncc));
2344 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002345 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346 /* facility */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2348 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002349 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2351 }
2352 /* ss-version */
2353 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2354 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002355 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2357 }
2358
Harald Welte596fed42009-07-23 19:06:52 +02002359 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002360}
2361
2362static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2363{
2364 struct gsm_mncc *fac = arg;
2365 struct msgb *msg = gsm48_msgb_alloc();
2366 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2367
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368 gh->msg_type = GSM48_MT_CC_FACILITY;
2369
2370 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002373 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374}
2375
2376static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2377{
2378 struct gsm_mncc hold;
2379
2380 memset(&hold, 0, sizeof(struct gsm_mncc));
2381 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002382 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002383}
2384
2385static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2386{
2387 struct msgb *msg = gsm48_msgb_alloc();
2388 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2389
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2391
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002392 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002393}
2394
2395static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2396{
2397 struct gsm_mncc *hold_rej = arg;
2398 struct msgb *msg = gsm48_msgb_alloc();
2399 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2400
Harald Welte4bfdfe72009-06-10 23:11:52 +08002401 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2402
2403 /* cause */
2404 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002405 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 else
Harald Welte55c8f352010-03-07 23:40:35 +01002407 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002408
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002409 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002410}
2411
2412static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2413{
2414 struct gsm_mncc retrieve;
2415
2416 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2417 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002418 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2419 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420}
2421
2422static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2423{
2424 struct msgb *msg = gsm48_msgb_alloc();
2425 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2426
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2428
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002429 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002430}
2431
2432static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2433{
2434 struct gsm_mncc *retrieve_rej = arg;
2435 struct msgb *msg = gsm48_msgb_alloc();
2436 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2437
Harald Welte4bfdfe72009-06-10 23:11:52 +08002438 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2439
2440 /* cause */
2441 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002442 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 else
Harald Welte55c8f352010-03-07 23:40:35 +01002444 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002445
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002446 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447}
2448
2449static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2450{
2451 struct gsm48_hdr *gh = msgb_l3(msg);
2452 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2453 struct tlv_parsed tp;
2454 struct gsm_mncc dtmf;
2455
2456 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2457 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002458 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 /* keypad facility */
2460 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2461 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002462 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2464 }
2465
Harald Welte596fed42009-07-23 19:06:52 +02002466 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002467}
2468
2469static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2470{
2471 struct gsm_mncc *dtmf = arg;
2472 struct msgb *msg = gsm48_msgb_alloc();
2473 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2474
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2476
2477 /* keypad */
2478 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002479 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002480
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002481 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002482}
2483
2484static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2485{
2486 struct gsm_mncc *dtmf = arg;
2487 struct msgb *msg = gsm48_msgb_alloc();
2488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2489
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2491
2492 /* cause */
2493 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002494 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002495 else
Harald Welte55c8f352010-03-07 23:40:35 +01002496 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002497
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002498 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002499}
2500
2501static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2502{
2503 struct msgb *msg = gsm48_msgb_alloc();
2504 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2505
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2507
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002508 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509}
2510
2511static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2512{
2513 struct gsm_mncc dtmf;
2514
2515 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2516 dtmf.callref = trans->callref;
2517
Harald Welte596fed42009-07-23 19:06:52 +02002518 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519}
2520
2521static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2522{
2523 struct gsm48_hdr *gh = msgb_l3(msg);
2524 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2525 struct tlv_parsed tp;
2526 struct gsm_mncc modify;
2527
2528 memset(&modify, 0, sizeof(struct gsm_mncc));
2529 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002530 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531 /* bearer capability */
2532 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2533 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002534 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2536 }
2537
2538 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2539
Harald Welte596fed42009-07-23 19:06:52 +02002540 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002541}
2542
2543static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2544{
2545 struct gsm_mncc *modify = arg;
2546 struct msgb *msg = gsm48_msgb_alloc();
2547 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2548
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549 gh->msg_type = GSM48_MT_CC_MODIFY;
2550
2551 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2552
2553 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002554 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002555
2556 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2557
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002558 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002559}
2560
2561static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2562{
2563 struct gsm48_hdr *gh = msgb_l3(msg);
2564 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2565 struct tlv_parsed tp;
2566 struct gsm_mncc modify;
2567
2568 gsm48_stop_cc_timer(trans);
2569
2570 memset(&modify, 0, sizeof(struct gsm_mncc));
2571 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002572 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002573 /* bearer capability */
2574 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2575 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002576 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2578 }
2579
2580 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2581
Harald Welte596fed42009-07-23 19:06:52 +02002582 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583}
2584
2585static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2586{
2587 struct gsm_mncc *modify = arg;
2588 struct msgb *msg = gsm48_msgb_alloc();
2589 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2590
Harald Welte4bfdfe72009-06-10 23:11:52 +08002591 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2592
2593 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002594 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595
2596 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2597
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002598 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599}
2600
2601static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2602{
2603 struct gsm48_hdr *gh = msgb_l3(msg);
2604 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2605 struct tlv_parsed tp;
2606 struct gsm_mncc modify;
2607
2608 gsm48_stop_cc_timer(trans);
2609
2610 memset(&modify, 0, sizeof(struct gsm_mncc));
2611 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002612 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613 /* bearer capability */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2615 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002616 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2618 }
2619 /* cause */
2620 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2621 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002622 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2624 }
2625
2626 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2627
Harald Welte596fed42009-07-23 19:06:52 +02002628 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629}
2630
2631static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2632{
2633 struct gsm_mncc *modify = arg;
2634 struct msgb *msg = gsm48_msgb_alloc();
2635 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2636
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2638
2639 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002640 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002642 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643
2644 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2645
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002646 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002647}
2648
2649static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2650{
2651 struct gsm_mncc *notify = arg;
2652 struct msgb *msg = gsm48_msgb_alloc();
2653 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2654
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655 gh->msg_type = GSM48_MT_CC_NOTIFY;
2656
2657 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002658 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002660 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661}
2662
2663static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2664{
2665 struct gsm48_hdr *gh = msgb_l3(msg);
2666 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2667// struct tlv_parsed tp;
2668 struct gsm_mncc notify;
2669
2670 memset(&notify, 0, sizeof(struct gsm_mncc));
2671 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002672// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675
Harald Welte596fed42009-07-23 19:06:52 +02002676 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677}
2678
2679static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2680{
2681 struct gsm_mncc *user = arg;
2682 struct msgb *msg = gsm48_msgb_alloc();
2683 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2684
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685 gh->msg_type = GSM48_MT_CC_USER_INFO;
2686
2687 /* user-user */
2688 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002689 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002690 /* more data */
2691 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002692 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002694 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695}
2696
2697static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2698{
2699 struct gsm48_hdr *gh = msgb_l3(msg);
2700 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2701 struct tlv_parsed tp;
2702 struct gsm_mncc user;
2703
2704 memset(&user, 0, sizeof(struct gsm_mncc));
2705 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002706 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707 /* user-user */
2708 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2709 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002710 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2712 }
2713 /* more data */
2714 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2715 user.more = 1;
2716
Harald Welte596fed42009-07-23 19:06:52 +02002717 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002718}
2719
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002720static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002721{
2722 struct gsm_mncc *mode = arg;
2723
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002724 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002725}
2726
2727static struct downstate {
2728 u_int32_t states;
2729 int type;
2730 int (*rout) (struct gsm_trans *trans, void *arg);
2731} downstatelist[] = {
2732 /* mobile originating call establishment */
2733 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2734 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2735 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2736 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2737 {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 */
2738 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2739 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2740 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2741 /* mobile terminating call establishment */
2742 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2743 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2744 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2745 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2746 /* signalling during call */
2747 {SBIT(GSM_CSTATE_ACTIVE),
2748 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2749 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2750 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2751 {ALL_STATES,
2752 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2753 {ALL_STATES,
2754 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2755 {ALL_STATES,
2756 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2757 {SBIT(GSM_CSTATE_ACTIVE),
2758 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2759 {SBIT(GSM_CSTATE_ACTIVE),
2760 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2761 {SBIT(GSM_CSTATE_ACTIVE),
2762 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2763 {SBIT(GSM_CSTATE_ACTIVE),
2764 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2765 {SBIT(GSM_CSTATE_ACTIVE),
2766 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2767 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2768 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2769 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2770 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2771 {SBIT(GSM_CSTATE_ACTIVE),
2772 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2773 /* clearing */
2774 {SBIT(GSM_CSTATE_INITIATED),
2775 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2776 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2777 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2778 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2779 MNCC_REL_REQ, gsm48_cc_tx_release},
2780 /* special */
2781 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002782 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002783};
2784
2785#define DOWNSLLEN \
2786 (sizeof(downstatelist) / sizeof(struct downstate))
2787
2788
2789int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2790{
Harald Welte1a6f7982009-08-09 18:52:33 +02002791 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002792 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002793 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002794 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795 struct gsm_mncc *data = arg, rel;
2796
2797 /* handle special messages */
2798 switch(msg_type) {
2799 case MNCC_BRIDGE:
2800 return tch_bridge(net, arg);
2801 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002802 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002803 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002804 return tch_recv_mncc(net, data->callref, 1);
2805 case GSM_TCHF_FRAME:
2806 /* Find callref */
2807 trans = trans_find_by_callref(net, data->callref);
2808 if (!trans)
2809 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002810 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002811 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002812 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002813 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002814 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002815 switch (bts->type) {
2816 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002817 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002818 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002819 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002820 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002821 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002822 default:
2823 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2824 }
2825 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002826 }
2827
2828 memset(&rel, 0, sizeof(struct gsm_mncc));
2829 rel.callref = data->callref;
2830
2831 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002832 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833
2834 /* Callref unknown */
2835 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002836 struct gsm_subscriber *subscr;
2837
Harald Welte4a3464c2009-07-04 10:11:24 +02002838 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002839 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2840 "Received '%s' from MNCC with "
2841 "unknown callref %d\n", data->called.number,
2842 get_mncc_name(msg_type), data->callref);
2843 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002844 return mncc_release_ind(net, NULL, data->callref,
2845 GSM48_CAUSE_LOC_PRN_S_LU,
2846 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002848 if (!data->called.number[0] && !data->imsi[0]) {
2849 DEBUGP(DCC, "(bts - trx - ts - ti) "
2850 "Received '%s' from MNCC with "
2851 "no number or IMSI\n", get_mncc_name(msg_type));
2852 /* Invalid number */
2853 return mncc_release_ind(net, NULL, data->callref,
2854 GSM48_CAUSE_LOC_PRN_S_LU,
2855 GSM48_CC_CAUSE_INV_NR_FORMAT);
2856 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002858 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002859 subscr = subscr_get_by_extension(net,
2860 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002861 else
Harald Welte9176bd42009-07-23 18:46:00 +02002862 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863 /* If subscriber is not found */
2864 if (!subscr) {
2865 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2866 "Received '%s' from MNCC with "
2867 "unknown subscriber %s\n", data->called.number,
2868 get_mncc_name(msg_type), data->called.number);
2869 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002870 return mncc_release_ind(net, NULL, data->callref,
2871 GSM48_CAUSE_LOC_PRN_S_LU,
2872 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873 }
2874 /* If subscriber is not "attached" */
2875 if (!subscr->lac) {
2876 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2877 "Received '%s' from MNCC with "
2878 "detached subscriber %s\n", data->called.number,
2879 get_mncc_name(msg_type), data->called.number);
2880 subscr_put(subscr);
2881 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002882 return mncc_release_ind(net, NULL, data->callref,
2883 GSM48_CAUSE_LOC_PRN_S_LU,
2884 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885 }
2886 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002887 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2888 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 DEBUGP(DCC, "No memory for trans.\n");
2890 subscr_put(subscr);
2891 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002892 mncc_release_ind(net, NULL, data->callref,
2893 GSM48_CAUSE_LOC_PRN_S_LU,
2894 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002895 return -ENOMEM;
2896 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002898 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002899
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002901 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002902 /* find transaction with this subscriber already paging */
2903 llist_for_each_entry(transt, &net->trans_list, entry) {
2904 /* Transaction of our lchan? */
2905 if (transt == trans ||
2906 transt->subscr != subscr)
2907 continue;
2908 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2909 "Received '%s' from MNCC with "
2910 "unallocated channel, paging already "
2911 "started.\n", bts->nr,
2912 data->called.number,
2913 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002914 subscr_put(subscr);
2915 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002916 return 0;
2917 }
2918 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002919 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002920 /* Trigger paging */
2921 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2922 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002923 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002924 return 0;
2925 }
2926 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002927 trans->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002928 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002929 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002931
2932 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002933 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002934
2935 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002936 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002937 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002938 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002939 (trans->subscr)?(trans->subscr->extension):"-",
2940 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002941 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2942 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943 if (msg_type == MNCC_REL_REQ)
2944 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2945 else
2946 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2947 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002948 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002949 return rc;
2950 }
2951
2952 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2953 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002954 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002956 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002957 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002958 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959
2960 /* Find function for current state and message */
2961 for (i = 0; i < DOWNSLLEN; i++)
2962 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002963 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002964 break;
2965 if (i == DOWNSLLEN) {
2966 DEBUGP(DCC, "Message unhandled at this state.\n");
2967 return 0;
2968 }
2969
2970 rc = downstatelist[i].rout(trans, arg);
2971
2972 return rc;
2973}
2974
2975
2976static struct datastate {
2977 u_int32_t states;
2978 int type;
2979 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2980} datastatelist[] = {
2981 /* mobile originating call establishment */
2982 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2983 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2984 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2985 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2986 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2987 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2988 /* mobile terminating call establishment */
2989 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2990 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2991 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2992 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002993 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF) | SBIT(GSM_CSTATE_CALL_RECEIVED), /* (5.2.2.6) | 5.2.2.6 | 5.2.2.6 */
Harald Welte4bfdfe72009-06-10 23:11:52 +08002994 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2995 /* signalling during call */
2996 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2997 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2998 {SBIT(GSM_CSTATE_ACTIVE),
2999 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3000 {ALL_STATES,
3001 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3002 {ALL_STATES,
3003 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3004 {ALL_STATES,
3005 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3006 {SBIT(GSM_CSTATE_ACTIVE),
3007 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3008 {SBIT(GSM_CSTATE_ACTIVE),
3009 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3010 {SBIT(GSM_CSTATE_ACTIVE),
3011 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3012 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3013 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3014 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3015 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3016 {SBIT(GSM_CSTATE_ACTIVE),
3017 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3018 /* clearing */
3019 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3020 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3021 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3022 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3023 {ALL_STATES, /* 5.4.3.4 */
3024 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3025};
3026
3027#define DATASLLEN \
3028 (sizeof(datastatelist) / sizeof(struct datastate))
3029
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003030static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003031{
3032 struct gsm48_hdr *gh = msgb_l3(msg);
3033 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003034 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003035 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003037
Harald Welte4bfdfe72009-06-10 23:11:52 +08003038 if (msg_type & 0x80) {
3039 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3040 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003041 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003042
Harald Welte4bfdfe72009-06-10 23:11:52 +08003043 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003044 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003045
Harald Welte6f5aee02009-07-23 21:21:14 +02003046 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003047 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003048 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003049 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003050 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3051 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052
3053 /* Create transaction */
3054 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003055 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 "creating new trans.\n", transaction_id);
3057 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003058 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003059 transaction_id, new_callref++);
3060 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003061 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003062 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003063 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 GSM48_MT_CC_RELEASE_COMPL);
3065 return -ENOMEM;
3066 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003068 trans->conn = conn;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003069 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003070 }
3071
3072 /* find function for current state and message */
3073 for (i = 0; i < DATASLLEN; i++)
3074 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003075 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003076 break;
3077 if (i == DATASLLEN) {
3078 DEBUGP(DCC, "Message unhandled at this state.\n");
3079 return 0;
3080 }
3081
3082 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003083
3084 return rc;
3085}
3086
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003087/* here we get data from the BSC level... */
3088int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003089{
3090 struct gsm48_hdr *gh = msgb_l3(msg);
3091 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003092 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003093
3094 if (silent_call_reroute(msg))
3095 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003096
3097 switch (pdisc) {
3098 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003099 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003100 break;
3101 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003102 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003103 break;
3104 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003105 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003106 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003107 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003108 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003109 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003110 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003111 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003112 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3113 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003114 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003115 case GSM48_PDISC_NC_SS:
3116 rc = handle_rcv_ussd(msg);
3117 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003118 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003119 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3120 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003121 break;
3122 }
3123
3124 return rc;
3125}
Harald Welte8470bf22008-12-25 23:28:35 +00003126
Harald Welte805f6442009-07-28 18:25:29 +02003127/*
3128 * This will be ran by the linker when loading the DSO. We use it to
3129 * do system initialization, e.g. registration of signal handlers.
3130 */
3131static __attribute__((constructor)) void on_dso_load_0408(void)
3132{
Harald Welte805f6442009-07-28 18:25:29 +02003133 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3134}