blob: 7bf30e79820b5e1a3d18e24eb8c97a1056316d98 [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>
Holger Hans Peter Freyther88519ea2010-06-30 12:44:07 +080057#include <openbsc/osmo_msc.h>
Harald Welte52b1f982008-12-23 20:25:15 +000058
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020059void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010060void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020061
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080062int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +080063static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte65e74cc2008-12-29 01:55:35 +000064 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010065static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000066
Harald Welte52b1f982008-12-23 20:25:15 +000067struct gsm_lai {
68 u_int16_t mcc;
69 u_int16_t mnc;
70 u_int16_t lac;
71};
72
Harald Welte4bfdfe72009-06-10 23:11:52 +080073static u_int32_t new_callref = 0x80000001;
74
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080075static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
76 struct gsm_trans *trans)
77{
78 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
79
80 /* if we get passed a transaction reference, do some common
81 * work that the caller no longer has to do */
82 if (trans) {
83 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
84 msg->lchan = trans->conn->lchan;
85 }
86
87
88 if (msg->lchan) {
89 msg->trx = msg->lchan->ts->trx;
90 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
91 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
92 "Sending '%s' to MS.\n", msg->trx->bts->nr,
93 msg->trx->nr, msg->lchan->ts->nr,
94 gh->proto_discr & 0xf0,
95 gsm48_cc_msg_name(gh->msg_type));
96 else
97 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
98 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
99 msg->trx->nr, msg->lchan->ts->nr,
100 gh->proto_discr, gh->msg_type);
101 }
102
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800103 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;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800113 msc_release_connection(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100114}
115
116static void allocate_security_operation(struct gsm_subscriber_connection *conn)
117{
Sylvain Munaut30a15382009-12-24 00:27:26 +0100118 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
119 struct gsm_security_operation);
120}
121
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800122static int gsm48_secure_channel(struct gsm_subscriber_connection *conn, int key_seq,
123 gsm_cbfn *cb, void *cb_data)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100124{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800125 struct gsm_network *net = conn->bts->network;
126 struct gsm_subscriber *subscr = conn->subscr;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100127 struct gsm_security_operation *op;
128 struct gsm_auth_tuple atuple;
129 int status = -1, rc;
130
131 /* Check if we _can_ enable encryption. Cases where we can't:
132 * - Encryption disabled in config
133 * - Channel already secured (nothing to do)
134 * - Subscriber equipment doesn't support configured encryption
135 */
136 if (!net->a5_encryption) {
137 status = GSM_SECURITY_NOAVAIL;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800138 } else if (conn->lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
Sylvain Munaut30a15382009-12-24 00:27:26 +0100139 DEBUGP(DMM, "Requesting to secure an already secure channel");
140 status = GSM_SECURITY_SUCCEEDED;
141 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
142 net->a5_encryption)) {
143 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
144 status = GSM_SECURITY_NOAVAIL;
145 }
146
147 /* If not done yet, try to get info for this user */
148 if (status < 0) {
149 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
150 if (rc <= 0)
151 status = GSM_SECURITY_NOAVAIL;
152 }
153
154 /* Are we done yet ? */
155 if (status >= 0)
156 return cb ?
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800157 cb(GSM_HOOK_RR_SECURITY, status, NULL, conn, cb_data) :
Sylvain Munaut30a15382009-12-24 00:27:26 +0100158 0;
159
160 /* Start an operation (can't have more than one pending !!!) */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800161 if (conn->sec_operation)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100162 return -EBUSY;
163
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800164 allocate_security_operation(conn);
165 op = conn->sec_operation;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100166 op->cb = cb;
167 op->cb_data = cb_data;
168 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
169
170 /* FIXME: Should start a timer for completion ... */
171
172 /* Then do whatever is needed ... */
173 if (rc == 1) {
174 /* Start authentication */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800175 return gsm48_tx_mm_auth_req(conn, op->atuple.rand, op->atuple.key_seq);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100176 } else if (rc == 2) {
177 /* Start ciphering directly */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800178 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
179 conn->lchan->encr.key_len = 8;
180 memcpy(conn->lchan->encr.key, op->atuple.kc, 8);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100181
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800182 return gsm48_send_rr_ciph_mode(conn->lchan, 0);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100183 }
184
185 return -EINVAL; /* not reached */
186}
187
Holger Freyther73487a22008-12-31 18:53:57 +0000188static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
189 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000190{
191 if (!subscriber)
192 return 0;
193
Holger Freyther73487a22008-12-31 18:53:57 +0000194 /*
195 * Do not send accept yet as more information should arrive. Some
196 * phones will not send us the information and we will have to check
197 * what we want to do with that.
198 */
199 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
200 return 0;
201
Jan Luebbe06513f22009-08-12 12:48:00 +0200202 switch (subscriber->net->auth_policy) {
203 case GSM_AUTH_POLICY_CLOSED:
204 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200205 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200206 if (subscriber->authorized)
207 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200208 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200209 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000210 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200211 default:
212 return 0;
213 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000214}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000215
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100216static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000217{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100218 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000219 return;
220
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100221 bsc_del_timer(&conn->loc_operation->updating_timer);
222 talloc_free(conn->loc_operation);
223 conn->loc_operation = 0;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800224 msc_release_connection(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000225}
226
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100227static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000228{
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800229 if (conn->loc_operation)
230 LOGP(DMM, LOGL_ERROR, "Connection already had operation.\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100231 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000232
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100233 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200234 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000235}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000236
Sylvain Munaut267fba02009-12-24 00:28:01 +0100237static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
238 struct msgb *msg, void *data, void *param)
239{
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800240 struct gsm_subscriber_connection *conn = data;
Sylvain Munaut267fba02009-12-24 00:28:01 +0100241 int rc = 0;
242
243 switch (event) {
244 case GSM_SECURITY_AUTH_FAILED:
245 release_loc_updating_req(conn);
246 break;
247
248 case GSM_SECURITY_NOAVAIL:
249 case GSM_SECURITY_SUCCEEDED:
250 /* We're all good */
251 db_subscriber_alloc_tmsi(conn->subscr);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800252 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800253 if (conn->bts->network->send_mm_info) {
Sylvain Munaut267fba02009-12-24 00:28:01 +0100254 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800255 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100256 }
257
258 /* call subscr_update after putting the loc_upd_acc
259 * in the transmit queue, since S_SUBSCR_ATTACHED might
260 * trigger further action like SMS delivery */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800261 subscr_update(conn->subscr, conn->bts,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100262 GSM_SUBSCRIBER_UPDATE_ATTACHED);
263
264 /* try to close channel ASAP */
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800265 release_loc_updating_req(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100266 break;
267
268 default:
269 rc = -EINVAL;
270 };
271
272 return rc;
273}
274
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100275static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000276{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100277 if (authorize_subscriber(conn->loc_operation, conn->subscr))
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800278 return gsm48_secure_channel(conn,
Sylvain Munaut267fba02009-12-24 00:28:01 +0100279 conn->loc_operation->key_seq,
280 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000281 return 0;
282}
283
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800284void gsm0408_clear_request(struct gsm_subscriber_connection* conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000285{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800286 struct gsm_trans *trans, *temp;
Holger Hans Peter Freyther40494552010-06-28 17:09:29 +0800287
288 /* avoid someone issuing a clear */
289 conn->in_release = 1;
290
Holger Freyther7c19f742009-06-06 13:54:35 +0000291 /*
292 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800293 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000294 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800295 release_loc_updating_req(conn);
296 release_security_operation(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000297
Harald Welte4bfdfe72009-06-10 23:11:52 +0800298 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200299 /* FIXME: this is not neccessarily the right thing to do, we should
300 * only set trans->lchan to NULL and wait for another lchan to be
301 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800302 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
303 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200304 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800305 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000306}
307
Holger Freyther429e7762008-12-30 13:28:30 +0000308/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800309int gsm0408_loc_upd_rej(struct gsm_subscriber_connection *conn, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000310{
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800311 struct gsm_bts *bts = conn->bts;
Holger Hans Peter Freyther230a4d82010-06-15 19:40:05 +0800312 struct msgb *msg;
313
314 counter_inc(bts->network->stats.loc_upd_resp.reject);
315
316 msg = gsm48_create_loc_upd_rej(cause);
317 if (!msg) {
318 LOGP(DMM, LOGL_ERROR, "Failed to create msg for LOCATION UPDATING REJECT.\n");
319 return -1;
320 }
Harald Welte52b1f982008-12-23 20:25:15 +0000321
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800322 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000323
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100324 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100325 "LAC=%u BTS=%u\n", conn->subscr ?
326 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800327 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100328
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800329 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000330}
331
332/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800333int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000334{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800335 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000336 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000337 struct gsm48_hdr *gh;
338 struct gsm48_loc_area_id *lai;
339 u_int8_t *mid;
340
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800341 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000342
343 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
344 gh->proto_discr = GSM48_PDISC_MM;
345 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
346
347 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100348 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000349 bts->network->network_code, bts->location_area_code);
350
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200351 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200352 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000353
354 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
355
Harald Welteffa55a42009-12-22 19:07:32 +0100356 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100357
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800358 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000359}
360
Harald Weltebf5e8df2009-02-03 12:59:45 +0000361/* Transmit Chapter 9.2.10 Identity Request */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800362static int mm_tx_identity_req(struct gsm_subscriber_connection *conn, u_int8_t id_type)
Harald Welte231ad4f2008-12-27 11:15:38 +0000363{
364 struct msgb *msg = gsm48_msgb_alloc();
365 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000366
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800367 msg->lchan = conn->lchan;
Harald Welte231ad4f2008-12-27 11:15:38 +0000368
369 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
370 gh->proto_discr = GSM48_PDISC_MM;
371 gh->msg_type = GSM48_MT_MM_ID_REQ;
372 gh->data[0] = id_type;
373
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800374 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000375}
376
Harald Welte231ad4f2008-12-27 11:15:38 +0000377
Harald Weltebf5e8df2009-02-03 12:59:45 +0000378/* Parse Chapter 9.2.11 Identity Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800379static int mm_rx_id_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte231ad4f2008-12-27 11:15:38 +0000380{
381 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000382 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200383 struct gsm_bts *bts = lchan->ts->trx->bts;
384 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000385 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200386 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000387
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200388 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000389 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000390 mi_type, mi_string);
391
Harald Welte7659de12009-12-13 12:39:18 +0100392 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
393
Harald Welte75a983f2008-12-27 21:34:06 +0000394 switch (mi_type) {
395 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200396 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100397 if (!conn->subscr) {
398 conn->subscr = subscr_get_by_imsi(net, mi_string);
399 if (!conn->subscr)
400 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200401 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100402 if (conn->loc_operation)
403 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000404 break;
405 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000406 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000407 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100408 if (conn->subscr) {
409 db_subscriber_assoc_imei(conn->subscr, mi_string);
410 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200411 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100412 if (conn->loc_operation)
413 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000414 break;
415 }
Holger Freyther73487a22008-12-31 18:53:57 +0000416
417 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100418 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000419}
420
Harald Welte255539c2008-12-28 02:26:27 +0000421
422static void loc_upd_rej_cb(void *data)
423{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100424 struct gsm_subscriber_connection *conn = data;
425 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100426 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000427
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800428 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Hans Peter Freyther01288432010-06-16 12:52:28 +0800429 release_loc_updating_req(conn);
Harald Welte255539c2008-12-28 02:26:27 +0000430}
431
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100432static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000433{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100434 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
435 conn->loc_operation->updating_timer.data = conn;
436 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000437}
438
Harald Welte2a139372009-02-22 21:14:55 +0000439static const char *lupd_name(u_int8_t type)
440{
441 switch (type) {
442 case GSM48_LUPD_NORMAL:
443 return "NORMAL";
444 case GSM48_LUPD_PERIODIC:
445 return "PEROIDOC";
446 case GSM48_LUPD_IMSI_ATT:
447 return "IMSI ATTACH";
448 default:
449 return "UNKNOWN";
450 }
451}
452
Harald Weltebf5e8df2009-02-03 12:59:45 +0000453/* Chapter 9.2.15: Receive Location Updating Request */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800454static int mm_rx_loc_upd_req(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000455{
Harald Welte8470bf22008-12-25 23:28:35 +0000456 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000457 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800458 struct gsm_subscriber *subscr = NULL;
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800459 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000460 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200461 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000462 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000463
Harald Welte8470bf22008-12-25 23:28:35 +0000464 lu = (struct gsm48_loc_upd_req *) gh->data;
465
466 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000467
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200468 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000469
Harald Weltea0368542009-06-27 02:58:43 +0200470 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000471 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000472
Harald Welte7659de12009-12-13 12:39:18 +0100473 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
474
Harald Welte24ff6ee2009-12-22 00:41:05 +0100475 switch (lu->type) {
476 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100477 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100478 break;
479 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100480 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100481 break;
482 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100483 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100484 break;
485 }
486
Holger Freythereaf04692009-06-06 13:54:44 +0000487 /*
488 * Pseudo Spoof detection: Just drop a second/concurrent
489 * location updating request.
490 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100491 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200492 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100493 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800494 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000495 return 0;
496 }
497
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800498 allocate_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000499
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200500 conn->loc_operation->key_seq = lu->key_seq;
501
Harald Welte52b1f982008-12-23 20:25:15 +0000502 switch (mi_type) {
503 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200504 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000505 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800506 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100507 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000508
Jan Luebbe370b41d2009-08-12 10:19:34 +0200509 /* look up subscriber based on IMSI, create if not found */
510 subscr = subscr_get_by_imsi(bts->network, mi_string);
511 if (!subscr) {
512 subscr = db_create_subscriber(bts->network, mi_string);
513 }
Harald Welte4b634542008-12-27 01:55:51 +0000514 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000515 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200516 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000517 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200518 subscr = subscr_get_by_tmsi(bts->network,
519 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000520 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000521 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800522 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100523 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000524 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200525 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800526 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200527 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000528 break;
529 case GSM_MI_TYPE_IMEI:
530 case GSM_MI_TYPE_IMEISV:
531 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200532 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000533 break;
534 default:
Harald Weltea0368542009-06-27 02:58:43 +0200535 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000536 break;
537 }
538
Harald Welte24516ea2009-07-04 10:18:00 +0200539 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100540 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200541
Harald Welte4bfdfe72009-06-10 23:11:52 +0800542 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200543 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800544 /* FIXME: request id? close channel? */
545 return -EINVAL;
546 }
547
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100548 conn->subscr = subscr;
549 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000550
Harald Welte24516ea2009-07-04 10:18:00 +0200551 /* check if we can let the subscriber into our network immediately
552 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100553 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000554}
555
Harald Welte4bfdfe72009-06-10 23:11:52 +0800556#if 0
557static u_int8_t to_bcd8(u_int8_t val)
558{
559 return ((val / 10) << 4) | (val % 10);
560}
561#endif
562
Harald Weltedb253af2008-12-30 17:56:55 +0000563/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800564int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000565{
566 struct msgb *msg = gsm48_msgb_alloc();
567 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800568 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000569 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200570 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800571#if 0
572 time_t cur_t;
573 struct tm* cur_time;
574 int tz15min;
575#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000576
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800577 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000578
579 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
580 gh->proto_discr = GSM48_PDISC_MM;
581 gh->msg_type = GSM48_MT_MM_INFO;
582
583 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200584#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000585 name_len = strlen(net->name_long);
586 /* 10.5.3.5a */
587 ptr8 = msgb_put(msg, 3);
588 ptr8[0] = GSM48_IE_NAME_LONG;
589 ptr8[1] = name_len*2 +1;
590 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
591
592 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
593 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000594 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000595
596 /* FIXME: Use Cell Broadcast, not UCS-2, since
597 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200598#endif
599 name_len = (strlen(net->name_long)*7)/8;
600 name_pad = (8 - strlen(net->name_long)*7)%8;
601 if (name_pad > 0)
602 name_len++;
603 /* 10.5.3.5a */
604 ptr8 = msgb_put(msg, 3);
605 ptr8[0] = GSM48_IE_NAME_LONG;
606 ptr8[1] = name_len +1;
607 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
608
609 ptr8 = msgb_put(msg, name_len);
610 gsm_7bit_encode(ptr8, net->name_long);
611
Harald Weltedb253af2008-12-30 17:56:55 +0000612 }
613
614 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200615#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000616 name_len = strlen(net->name_short);
617 /* 10.5.3.5a */
618 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200619 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000620 ptr8[1] = name_len*2 + 1;
621 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
622
Harald Weltee872cb12009-01-01 00:33:37 +0000623 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000624 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000625 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200626#endif
627 name_len = (strlen(net->name_short)*7)/8;
628 name_pad = (8 - strlen(net->name_short)*7)%8;
629 if (name_pad > 0)
630 name_len++;
631 /* 10.5.3.5a */
632 ptr8 = (u_int8_t *) msgb_put(msg, 3);
633 ptr8[0] = GSM48_IE_NAME_SHORT;
634 ptr8[1] = name_len +1;
635 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
636
637 ptr8 = msgb_put(msg, name_len);
638 gsm_7bit_encode(ptr8, net->name_short);
639
Harald Weltedb253af2008-12-30 17:56:55 +0000640 }
641
642#if 0
643 /* Section 10.5.3.9 */
644 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800645 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000646 ptr8 = msgb_put(msg, 8);
647 ptr8[0] = GSM48_IE_NET_TIME_TZ;
648 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
649 ptr8[2] = to_bcd8(cur_time->tm_mon);
650 ptr8[3] = to_bcd8(cur_time->tm_mday);
651 ptr8[4] = to_bcd8(cur_time->tm_hour);
652 ptr8[5] = to_bcd8(cur_time->tm_min);
653 ptr8[6] = to_bcd8(cur_time->tm_sec);
654 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
655 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800656 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000657 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800658 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000659#endif
660
Daniel Willmanneea93372009-08-13 03:42:07 +0200661 DEBUGP(DMM, "-> MM INFO\n");
662
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800663 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000664}
665
Harald Welte7984d5c2009-08-12 22:56:50 +0200666/* Section 9.2.2 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800667int gsm48_tx_mm_auth_req(struct gsm_subscriber_connection *conn, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200668{
669 struct msgb *msg = gsm48_msgb_alloc();
670 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200671 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200672
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100673 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200674
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800675 msg->lchan = conn->lchan;
Harald Welte7984d5c2009-08-12 22:56:50 +0200676 gh->proto_discr = GSM48_PDISC_MM;
677 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
678
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100679 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200680
Harald Welte7984d5c2009-08-12 22:56:50 +0200681 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200682 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200683 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200684
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800685 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200686}
687
688/* Section 9.2.1 */
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800689int gsm48_tx_mm_auth_rej(struct gsm_subscriber_connection *conn)
Harald Welte7984d5c2009-08-12 22:56:50 +0200690{
691 DEBUGP(DMM, "-> AUTH REJECT\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800692 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
Harald Welte7984d5c2009-08-12 22:56:50 +0200693}
694
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800695static int gsm48_tx_mm_serv_ack(struct gsm_subscriber_connection *conn)
Harald Welte4b634542008-12-27 01:55:51 +0000696{
Harald Welte4b634542008-12-27 01:55:51 +0000697 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800698 return gsm48_tx_simple(conn, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000699}
Harald Welteba4cf162009-01-10 01:49:35 +0000700
701/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100702static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000703 enum gsm48_reject_value value)
704{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800705 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000706
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800707 msg = gsm48_create_mm_serv_rej(value);
708 if (!msg) {
709 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
710 return -1;
711 }
Harald Welteba4cf162009-01-10 01:49:35 +0000712
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800713 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100714 msg->lchan = conn->lchan;
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 Welte2a139372009-02-22 21:14:55 +0000875 return 0;
876}
877
Harald Welted2a7f5a2009-06-05 20:08:20 +0000878static int gsm48_rx_mm_status(struct msgb *msg)
879{
880 struct gsm48_hdr *gh = msgb_l3(msg);
881
882 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
883
884 return 0;
885}
886
Sylvain Munaut30a15382009-12-24 00:27:26 +0100887/* Chapter 9.2.3: Authentication Response */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800888static int gsm48_rx_mm_auth_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +0100889{
890 struct gsm48_hdr *gh = msgb_l3(msg);
891 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800892 struct gsm_network *net = conn->bts->network;
Sylvain Munaut30a15382009-12-24 00:27:26 +0100893
Sylvain Munautc593cf12010-06-10 23:51:41 +0200894 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100895 hexdump(ar->sres, 4));
896
897 /* Safety check */
898 if (!conn->sec_operation) {
899 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
900 return -EIO;
901 }
902
903 /* Validate SRES */
904 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
905 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200906
907 DEBUGPC(DMM, "Invalid (expected %s)\n",
908 hexdump(conn->sec_operation->atuple.sres, 4));
909
Sylvain Munaut30a15382009-12-24 00:27:26 +0100910 if (cb)
911 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800912 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100913
914 release_security_operation(conn);
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +0800915 return gsm48_tx_mm_auth_rej(conn);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100916 }
917
Sylvain Munautc593cf12010-06-10 23:51:41 +0200918 DEBUGPC(DMM, "OK\n");
919
Sylvain Munaut30a15382009-12-24 00:27:26 +0100920 /* Start ciphering */
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +0800921 conn->lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
922 conn->lchan->encr.key_len = 8;
923 memcpy(conn->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
Sylvain Munaut30a15382009-12-24 00:27:26 +0100924
925 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
926}
927
Harald Weltebf5e8df2009-02-03 12:59:45 +0000928/* Receive a GSM 04.08 Mobility Management (MM) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800929static int gsm0408_rcv_mm(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000930{
931 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800932 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000933
934 switch (gh->msg_type & 0xbf) {
935 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200936 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800937 rc = mm_rx_loc_upd_req(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000938 break;
939 case GSM48_MT_MM_ID_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800940 rc = mm_rx_id_resp(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000941 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000942 case GSM48_MT_MM_CM_SERV_REQ:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800943 rc = gsm48_rx_mm_serv_req(conn, msg);
Harald Welte4b634542008-12-27 01:55:51 +0000944 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000945 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000946 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000947 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000948 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000949 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800950 conn->subscr ?
951 subscr_name(conn->subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000952 "unknown subscriber");
953 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000954 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000955 rc = gsm48_rx_mm_imsi_detach_ind(msg);
956 break;
957 case GSM48_MT_MM_CM_REEST_REQ:
958 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
959 break;
960 case GSM48_MT_MM_AUTH_RESP:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +0800961 rc = gsm48_rx_mm_auth_resp(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000962 break;
963 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100964 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000965 gh->msg_type);
966 break;
967 }
968
969 return rc;
970}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000971
Harald Welte2d35ae62009-02-06 12:02:13 +0000972/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800973static int gsm48_rx_rr_pag_resp(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000974{
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +0800975 struct gsm_bts *bts = conn->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000976 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800977 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000978 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200979 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200980 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800981 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000982 int rc = 0;
983
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800984 resp = (struct gsm48_pag_resp *) &gh->data[0];
985 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
986 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000987 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
988 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200989
Harald Weltefe18d8f2009-02-22 21:14:24 +0000990 switch (mi_type) {
991 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200992 subscr = subscr_get_by_tmsi(bts->network,
993 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000994 break;
995 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200996 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000997 break;
998 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000999
1000 if (!subscr) {
1001 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001002 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001003 return -EINVAL;
1004 }
1005 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001006 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001007
Harald Weltec2e302d2009-07-05 14:08:13 +02001008 subscr->equipment.classmark2_len = *classmark2_lv;
1009 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1010 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001011
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001012 rc = gsm48_handle_paging_resp(conn, msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001013 return rc;
1014}
1015
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001016static int gsm48_rx_rr_classmark(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Weltef7c43522009-06-09 20:24:21 +00001017{
1018 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001019 struct gsm_subscriber *subscr = conn->subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001020 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1021 u_int8_t cm2_len, cm3_len = 0;
1022 u_int8_t *cm2, *cm3 = NULL;
1023
1024 DEBUGP(DRR, "CLASSMARK CHANGE ");
1025
1026 /* classmark 2 */
1027 cm2_len = gh->data[0];
1028 cm2 = &gh->data[1];
1029 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1030
1031 if (payload_len > cm2_len + 1) {
1032 /* we must have a classmark3 */
1033 if (gh->data[cm2_len+1] != 0x20) {
1034 DEBUGPC(DRR, "ERR CM3 TAG\n");
1035 return -EINVAL;
1036 }
1037 if (cm2_len > 3) {
1038 DEBUGPC(DRR, "CM2 too long!\n");
1039 return -EINVAL;
1040 }
1041
1042 cm3_len = gh->data[cm2_len+2];
1043 cm3 = &gh->data[cm2_len+3];
1044 if (cm3_len > 14) {
1045 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1046 return -EINVAL;
1047 }
1048 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1049 }
1050 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001051 subscr->equipment.classmark2_len = cm2_len;
1052 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001053 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001054 subscr->equipment.classmark3_len = cm3_len;
1055 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001056 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001057 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001058 }
1059
Harald Weltef7c43522009-06-09 20:24:21 +00001060 return 0;
1061}
1062
Harald Weltecf5b3592009-05-01 18:28:42 +00001063static int gsm48_rx_rr_status(struct msgb *msg)
1064{
1065 struct gsm48_hdr *gh = msgb_l3(msg);
1066
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001067 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001068 rr_cause_name(gh->data[0]));
1069
1070 return 0;
1071}
1072
Harald Weltef7c43522009-06-09 20:24:21 +00001073static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1074{
Harald Welted12b0fd2009-12-15 21:36:05 +01001075 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001076
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001077 /* This shouldn't actually end up here, as RSL treats
1078 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1079 * directly into gsm48_parse_meas_rep */
1080 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001081 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001082
1083 return 0;
1084}
1085
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001086static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001087{
1088 struct gsm48_hdr *gh = msgb_l3(msg);
1089 u_int8_t apdu_id_flags;
1090 u_int8_t apdu_len;
1091 u_int8_t *apdu_data;
1092
1093 apdu_id_flags = gh->data[0];
1094 apdu_len = gh->data[1];
1095 apdu_data = gh->data+2;
1096
1097 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1098 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1099
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001100 return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001101}
1102
Sylvain Munaut30a15382009-12-24 00:27:26 +01001103/* Chapter 9.1.10 Ciphering Mode Complete */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001104static int gsm48_rx_rr_ciph_m_compl(struct gsm_subscriber_connection *conn, struct msgb *msg)
Sylvain Munaut30a15382009-12-24 00:27:26 +01001105{
Sylvain Munaut30a15382009-12-24 00:27:26 +01001106 gsm_cbfn *cb;
1107 int rc = 0;
1108
1109 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1110
1111 /* Safety check */
1112 if (!conn->sec_operation) {
1113 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1114 return -EIO;
1115 }
1116
1117 /* FIXME: check for MI (if any) */
1118
1119 /* Call back whatever was in progress (if anything) ... */
1120 cb = conn->sec_operation->cb;
1121 if (cb) {
1122 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
Holger Hans Peter Freyther228c1052010-06-16 12:47:59 +08001123 NULL, conn, conn->sec_operation->cb_data);
Sylvain Munaut30a15382009-12-24 00:27:26 +01001124 }
1125
1126 /* Complete the operation */
1127 release_security_operation(conn);
1128
1129 return rc;
1130}
1131
Harald Welted011e8b2009-11-29 22:45:52 +01001132/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001133static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001134{
1135 struct gsm48_hdr *gh = msgb_l3(msg);
1136
1137 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1138 rr_cause_name(gh->data[0]));
1139
1140 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1141 /* FIXME: release old channel */
1142
1143 return 0;
1144}
1145
1146/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001147static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001148{
1149 struct gsm48_hdr *gh = msgb_l3(msg);
1150
1151 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1152 rr_cause_name(gh->data[0]));
1153
1154 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1155 /* FIXME: release allocated new channel */
1156
1157 return 0;
1158}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001159
Harald Weltebf5e8df2009-02-03 12:59:45 +00001160/* Receive a GSM 04.08 Radio Resource (RR) message */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001161static int gsm0408_rcv_rr(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00001162{
1163 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001164 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001165
1166 switch (gh->msg_type) {
1167 case GSM48_MT_RR_CLSM_CHG:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001168 rc = gsm48_rx_rr_classmark(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001169 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001170 case GSM48_MT_RR_GPRS_SUSP_REQ:
1171 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1172 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001173 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherdb4ef0d2010-06-21 10:46:44 +08001174 rc = gsm48_rx_rr_pag_resp(conn, msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001175 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001176 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001177 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001178 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001179 case GSM48_MT_RR_STATUS:
1180 rc = gsm48_rx_rr_status(msg);
1181 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001182 case GSM48_MT_RR_MEAS_REP:
1183 rc = gsm48_rx_rr_meas_rep(msg);
1184 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001185 case GSM48_MT_RR_APP_INFO:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001186 rc = gsm48_rx_rr_app_info(conn, msg);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001187 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001188 case GSM48_MT_RR_CIPH_M_COMPL:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08001189 rc = gsm48_rx_rr_ciph_m_compl(conn, msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001190 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001191 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001192 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001193 break;
1194 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001195 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001196 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001197 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001198 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1199 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001200 break;
1201 }
1202
Harald Welte2d35ae62009-02-06 12:02:13 +00001203 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001204}
1205
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001206int gsm48_send_rr_app_info(struct gsm_subscriber_connection *conn, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001207 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001208{
1209 struct msgb *msg = gsm48_msgb_alloc();
1210 struct gsm48_hdr *gh;
1211
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001212 msg->lchan = conn->lchan;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001213
1214 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1215 apdu_id, apdu_len);
1216
1217 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1218 gh->proto_discr = GSM48_PDISC_RR;
1219 gh->msg_type = GSM48_MT_RR_APP_INFO;
1220 gh->data[0] = apdu_id;
1221 gh->data[1] = apdu_len;
1222 memcpy(gh->data+2, apdu, apdu_len);
1223
Holger Hans Peter Freyther9ce1b272010-06-16 13:52:55 +08001224 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001225}
1226
Harald Welte4bc90a12008-12-27 16:32:52 +00001227/* Call Control */
1228
Harald Welte7584aea2009-02-11 11:44:12 +00001229/* The entire call control code is written in accordance with Figure 7.10c
1230 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1231 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1232 * it for voice */
1233
Harald Welte4bfdfe72009-06-10 23:11:52 +08001234static void new_cc_state(struct gsm_trans *trans, int state)
1235{
1236 if (state > 31 || state < 0)
1237 return;
1238
1239 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001240 gsm48_cc_state_name(trans->cc.state),
1241 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001242
Harald Weltedcaf5652009-07-23 18:56:43 +02001243 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001244}
1245
1246static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001247{
1248 struct msgb *msg = gsm48_msgb_alloc();
1249 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1250 u_int8_t *cause, *call_state;
1251
Harald Welte4bc90a12008-12-27 16:32:52 +00001252 gh->msg_type = GSM48_MT_CC_STATUS;
1253
1254 cause = msgb_put(msg, 3);
1255 cause[0] = 2;
1256 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1257 cause[2] = 0x80 | 30; /* response to status inquiry */
1258
1259 call_state = msgb_put(msg, 1);
1260 call_state[0] = 0xc0 | 0x00;
1261
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001262 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001263}
1264
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001265static int gsm48_tx_simple(struct gsm_subscriber_connection *conn,
Harald Welte6f4b7532008-12-29 00:39:37 +00001266 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001267{
1268 struct msgb *msg = gsm48_msgb_alloc();
1269 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1270
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001271 msg->lchan = conn->lchan;
Harald Welte4bc90a12008-12-27 16:32:52 +00001272
Harald Welte6f4b7532008-12-29 00:39:37 +00001273 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001274 gh->msg_type = msg_type;
1275
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08001276 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001277}
1278
Harald Welte4bfdfe72009-06-10 23:11:52 +08001279static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1280{
Harald Weltedcaf5652009-07-23 18:56:43 +02001281 if (bsc_timer_pending(&trans->cc.timer)) {
1282 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1283 bsc_del_timer(&trans->cc.timer);
1284 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001285 }
1286}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001287
Harald Welte4bfdfe72009-06-10 23:11:52 +08001288static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1289 int msg_type, struct gsm_mncc *mncc)
1290{
1291 struct msgb *msg;
1292
1293 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001294 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001295 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001296 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001297 trans->conn->lchan->ts->trx->bts->nr,
1298 trans->conn->lchan->ts->trx->nr,
1299 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300 (trans->subscr)?(trans->subscr->extension):"-",
1301 get_mncc_name(msg_type));
1302 else
1303 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1304 "Sending '%s' to MNCC.\n",
1305 (trans->subscr)?(trans->subscr->extension):"-",
1306 get_mncc_name(msg_type));
1307 else
1308 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1309 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1310
1311 mncc->msg_type = msg_type;
1312
Harald Welte966636f2009-06-26 19:39:35 +02001313 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 if (!msg)
1315 return -ENOMEM;
1316 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1317 msgb_enqueue(&net->upqueue, msg);
1318
1319 return 0;
1320}
1321
1322int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1323 u_int32_t callref, int location, int value)
1324{
1325 struct gsm_mncc rel;
1326
Harald Welte92f70c52009-06-12 01:54:08 +08001327 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001329 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001330 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1331}
1332
Harald Weltedcaf5652009-07-23 18:56:43 +02001333/* Call Control Specific transaction release.
1334 * gets called by trans_free, DO NOT CALL YOURSELF! */
1335void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001337 gsm48_stop_cc_timer(trans);
1338
1339 /* send release to L4, if callref still exists */
1340 if (trans->callref) {
1341 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001342 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001343 GSM48_CAUSE_LOC_PRN_S_LU,
1344 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001346 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001347 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001348 if (trans->conn)
1349 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350}
1351
1352static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001353
Harald Welte09e38af2009-02-16 22:52:23 +00001354/* call-back from paging the B-end of the connection */
1355static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001356 struct msgb *msg, void *_conn, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001357{
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001358 struct gsm_subscriber_connection *conn = _conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 struct gsm_subscriber *subscr = param;
1360 struct gsm_trans *transt, *tmp;
1361 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001362
Harald Welte09e38af2009-02-16 22:52:23 +00001363 if (hooknum != GSM_HOOK_RR_PAGING)
1364 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001365
Harald Welte4bfdfe72009-06-10 23:11:52 +08001366 if (!subscr)
1367 return -EINVAL;
1368 net = subscr->net;
1369 if (!net) {
1370 DEBUGP(DCC, "Error Network not set!\n");
1371 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001372 }
Harald Welte7584aea2009-02-11 11:44:12 +00001373
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 /* check all tranactions (without lchan) for subscriber */
1375 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001376 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377 continue;
1378 switch (event) {
1379 case GSM_PAGING_SUCCEEDED:
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001380 if (!conn) // paranoid
Harald Welte4bfdfe72009-06-10 23:11:52 +08001381 break;
1382 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1383 subscr->extension);
1384 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001385 if (!transt->conn) {
Holger Hans Peter Freyther86481c22010-06-17 15:05:57 +08001386 transt->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001387 }
1388 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001389 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001390 break;
1391 case GSM_PAGING_EXPIRED:
1392 DEBUGP(DCC, "Paging subscr %s expired!\n",
1393 subscr->extension);
1394 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001395 mncc_release_ind(transt->subscr->net, transt,
1396 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001397 GSM48_CAUSE_LOC_PRN_S_LU,
1398 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001399 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001400 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001401 break;
1402 }
1403 }
Harald Welte09e38af2009-02-16 22:52:23 +00001404 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001405}
Harald Welte7584aea2009-02-11 11:44:12 +00001406
Harald Welteda7ab742009-12-19 22:23:05 +01001407static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1408
Harald Welte805f6442009-07-28 18:25:29 +02001409/* some other part of the code sends us a signal */
1410static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1411 void *handler_data, void *signal_data)
1412{
1413 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001414 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001415 struct gsm_network *net;
1416 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001417
1418 if (subsys != SS_ABISIP)
1419 return 0;
1420
1421 /* in case we use direct BTS-to-BTS RTP */
1422 if (ipacc_rtp_direct)
1423 return 0;
1424
Harald Welte805f6442009-07-28 18:25:29 +02001425 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001426 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001427 /* check if any transactions on this lchan still have
1428 * a tch_recv_mncc request pending */
1429 net = lchan->ts->trx->bts->network;
1430 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001431 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001432 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1433 tch_recv_mncc(net, trans->callref, 1);
1434 }
1435 }
Harald Welte805f6442009-07-28 18:25:29 +02001436 break;
Harald Welte805f6442009-07-28 18:25:29 +02001437 }
1438
1439 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001440}
1441
Harald Welte49f48b82009-02-17 15:29:33 +00001442/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001443static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001444{
Harald Welte11fa29c2009-02-19 17:24:39 +00001445 struct gsm_bts *bts = lchan->ts->trx->bts;
1446 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001447 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001448
Harald Welte11fa29c2009-02-19 17:24:39 +00001449 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1450 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1451 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1452
1453 if (bts->type != remote_bts->type) {
1454 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1455 return -EINVAL;
1456 }
Harald Welteda7ab742009-12-19 22:23:05 +01001457
1458 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001459 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001460 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001461 if (!ipacc_rtp_direct) {
1462 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001463 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001464 if (rc < 0)
1465 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001466 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001467#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001468
1469 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301470 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1471 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001472 } else {
1473 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301474 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1475 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301476 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001477 if (rc < 0)
1478 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301479 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1480 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301481 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001482 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001483 break;
1484 case GSM_BTS_TYPE_BS11:
1485 trau_mux_map_lchan(lchan, remote_lchan);
1486 break;
1487 default:
1488 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001489 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001490 }
Harald Welte49f48b82009-02-17 15:29:33 +00001491
1492 return 0;
1493}
1494
Harald Welte4bfdfe72009-06-10 23:11:52 +08001495/* bridge channels of two transactions */
1496static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001497{
Harald Weltedcaf5652009-07-23 18:56:43 +02001498 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1499 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001500
Harald Welte4bfdfe72009-06-10 23:11:52 +08001501 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001502 return -EIO;
1503
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001504 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001505 return -EIO;
1506
1507 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001508 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001509}
1510
Harald Welteda7ab742009-12-19 22:23:05 +01001511/* enable receive of channels to MNCC upqueue */
1512static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001513{
1514 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001515 struct gsm_lchan *lchan;
1516 struct gsm_bts *bts;
1517 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001518
Harald Welte4bfdfe72009-06-10 23:11:52 +08001519 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001520 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001521 if (!trans)
1522 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001523 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001525 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001526 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001527
Harald Welteda7ab742009-12-19 22:23:05 +01001528 switch (bts->type) {
1529 case GSM_BTS_TYPE_NANOBTS:
1530 if (ipacc_rtp_direct) {
1531 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1532 return -EINVAL;
1533 }
1534 /* in case, we don't have a RTP socket yet, we note this
1535 * in the transaction and try later */
1536 if (!lchan->abis_ip.rtp_socket) {
1537 trans->tch_recv = enable;
1538 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1539 return 0;
1540 }
1541 if (enable) {
1542 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001543 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001544 if (rc < 0)
1545 return rc;
1546 /* assign socket to application interface */
1547 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1548 net, callref);
1549 } else
1550 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1551 net, 0);
1552 break;
1553 case GSM_BTS_TYPE_BS11:
1554 if (enable)
1555 return trau_recv_lchan(lchan, callref);
1556 return trau_mux_unmap(NULL, callref);
1557 break;
1558 default:
1559 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1560 return -EINVAL;
1561 }
1562
1563 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001564}
1565
Harald Welte4bfdfe72009-06-10 23:11:52 +08001566static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1567{
1568 DEBUGP(DCC, "-> STATUS ENQ\n");
1569 return gsm48_cc_tx_status(trans, msg);
1570}
1571
1572static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1573static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1574
1575static void gsm48_cc_timeout(void *arg)
1576{
1577 struct gsm_trans *trans = arg;
1578 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001579 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1580 int mo_location = GSM48_CAUSE_LOC_USER;
1581 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1582 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583 struct gsm_mncc mo_rel, l4_rel;
1584
1585 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1586 mo_rel.callref = trans->callref;
1587 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1588 l4_rel.callref = trans->callref;
1589
Harald Weltedcaf5652009-07-23 18:56:43 +02001590 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001591 case 0x303:
1592 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001593 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001594 break;
1595 case 0x310:
1596 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001597 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001598 break;
1599 case 0x313:
1600 disconnect = 1;
1601 /* unknown, did not find it in the specs */
1602 break;
1603 case 0x301:
1604 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001605 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001606 break;
1607 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001608 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001609 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001610 gsm48_cc_tx_release(trans, &trans->cc.msg);
1611 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001612 break; /* stay in release state */
1613 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001614 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001615 return;
1616// release = 1;
1617// l4_cause = 14;
1618// break;
1619 case 0x306:
1620 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001621 mo_cause = trans->cc.msg.cause.value;
1622 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001623 break;
1624 case 0x323:
1625 disconnect = 1;
1626 break;
1627 default:
1628 release = 1;
1629 }
1630
1631 if (release && trans->callref) {
1632 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001633 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 l4_location, l4_cause);
1635 trans->callref = 0;
1636 }
1637
1638 if (disconnect && trans->callref) {
1639 /* process disconnect towards layer 4 */
1640 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001641 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 }
1643
1644 /* process disconnect towards mobile station */
1645 if (disconnect || release) {
1646 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001647 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1648 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1649 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001650 mo_rel.cause.diag_len = 3;
1651
1652 if (disconnect)
1653 gsm48_cc_tx_disconnect(trans, &mo_rel);
1654 if (release)
1655 gsm48_cc_tx_release(trans, &mo_rel);
1656 }
1657
1658}
1659
1660static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1661 int sec, int micro)
1662{
1663 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001664 trans->cc.timer.cb = gsm48_cc_timeout;
1665 trans->cc.timer.data = trans;
1666 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1667 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668}
1669
1670static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1671{
1672 struct gsm48_hdr *gh = msgb_l3(msg);
1673 u_int8_t msg_type = gh->msg_type & 0xbf;
1674 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1675 struct tlv_parsed tp;
1676 struct gsm_mncc setup;
1677
1678 memset(&setup, 0, sizeof(struct gsm_mncc));
1679 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001680 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001681 /* emergency setup is identified by msg_type */
1682 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1683 setup.emergency = 1;
1684
1685 /* use subscriber as calling party number */
1686 if (trans->subscr) {
1687 setup.fields |= MNCC_F_CALLING;
1688 strncpy(setup.calling.number, trans->subscr->extension,
1689 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001690 strncpy(setup.imsi, trans->subscr->imsi,
1691 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001692 }
1693 /* bearer capability */
1694 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1695 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001696 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001697 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1698 }
1699 /* facility */
1700 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1701 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001702 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001703 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1704 }
1705 /* called party bcd number */
1706 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1707 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001708 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001709 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1710 }
1711 /* user-user */
1712 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1713 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001714 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001715 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1716 }
1717 /* ss-version */
1718 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1719 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001720 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001721 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1722 }
1723 /* CLIR suppression */
1724 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1725 setup.clir.sup = 1;
1726 /* CLIR invocation */
1727 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1728 setup.clir.inv = 1;
1729 /* cc cap */
1730 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1731 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001732 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001733 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1734 }
1735
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 new_cc_state(trans, GSM_CSTATE_INITIATED);
1737
Harald Welte (local)47df3992009-12-26 19:45:03 +01001738 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1739 subscr_name(trans->subscr), trans->subscr->extension,
1740 setup.called.number);
1741
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001743 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001744
Harald Welte13cac662009-07-29 12:10:35 +02001745 /* MNCC code will modify the channel asynchronously, we should
1746 * ipaccess-bind only after the modification has been made to the
1747 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 return 0;
1749}
1750
1751static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001752{
1753 struct msgb *msg = gsm48_msgb_alloc();
1754 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001756 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001757
Harald Welte7ccf7782009-02-17 01:43:01 +00001758 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001759
Harald Welte4bfdfe72009-06-10 23:11:52 +08001760 /* transaction id must not be assigned */
1761 if (trans->transaction_id != 0xff) { /* unasssigned */
1762 DEBUGP(DCC, "TX Setup with assigned transaction. "
1763 "This is not allowed!\n");
1764 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001765 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001766 GSM48_CAUSE_LOC_PRN_S_LU,
1767 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001768 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001769 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 return rc;
1771 }
1772
1773 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001774 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1775 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001776 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001777 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001778 GSM48_CAUSE_LOC_PRN_S_LU,
1779 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001781 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001782 return rc;
1783 }
Harald Welte78283ef2009-07-23 21:36:44 +02001784 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001785
Harald Welte65e74cc2008-12-29 01:55:35 +00001786 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001787
Harald Welte4bfdfe72009-06-10 23:11:52 +08001788 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001789
Harald Welte4bfdfe72009-06-10 23:11:52 +08001790 /* bearer capability */
1791 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001792 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001793 /* facility */
1794 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001795 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001796 /* progress */
1797 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001798 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 /* calling party BCD number */
1800 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001801 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001802 /* called party BCD number */
1803 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001804 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 /* user-user */
1806 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001807 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* redirecting party BCD number */
1809 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001810 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 /* signal */
1812 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001813 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814
1815 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001816
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001817 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001818}
1819
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1821{
1822 struct gsm48_hdr *gh = msgb_l3(msg);
1823 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1824 struct tlv_parsed tp;
1825 struct gsm_mncc call_conf;
1826
1827 gsm48_stop_cc_timer(trans);
1828 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1829
1830 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1831 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001832 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833#if 0
1834 /* repeat */
1835 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1836 call_conf.repeat = 1;
1837 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1838 call_conf.repeat = 2;
1839#endif
1840 /* bearer capability */
1841 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1842 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001843 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001844 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1845 }
1846 /* cause */
1847 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1848 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001849 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1851 }
1852 /* cc cap */
1853 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1854 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001855 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001856 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1857 }
1858
1859 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1860
Harald Welte596fed42009-07-23 19:06:52 +02001861 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1862 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863}
1864
1865static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1866{
1867 struct gsm_mncc *proceeding = arg;
1868 struct msgb *msg = gsm48_msgb_alloc();
1869 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1870
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1872
1873 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1874
1875 /* bearer capability */
1876 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001877 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878 /* facility */
1879 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001880 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001881 /* progress */
1882 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001883 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001884
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001885 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886}
1887
1888static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1889{
1890 struct gsm48_hdr *gh = msgb_l3(msg);
1891 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1892 struct tlv_parsed tp;
1893 struct gsm_mncc alerting;
1894
1895 gsm48_stop_cc_timer(trans);
1896 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1897
1898 memset(&alerting, 0, sizeof(struct gsm_mncc));
1899 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001900 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901 /* facility */
1902 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1903 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1906 }
1907
1908 /* progress */
1909 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1910 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001911 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1913 }
1914 /* ss-version */
1915 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1916 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001917 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001918 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1919 }
1920
1921 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1922
Harald Welte596fed42009-07-23 19:06:52 +02001923 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1924 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925}
1926
1927static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1928{
1929 struct gsm_mncc *alerting = arg;
1930 struct msgb *msg = gsm48_msgb_alloc();
1931 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1932
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 gh->msg_type = GSM48_MT_CC_ALERTING;
1934
1935 /* facility */
1936 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001937 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001938 /* progress */
1939 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001940 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001941 /* user-user */
1942 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001943 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944
1945 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1946
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001947 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948}
1949
1950static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1951{
1952 struct gsm_mncc *progress = arg;
1953 struct msgb *msg = gsm48_msgb_alloc();
1954 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1955
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 gh->msg_type = GSM48_MT_CC_PROGRESS;
1957
1958 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* user-user */
1961 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001964 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001965}
1966
1967static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1968{
1969 struct gsm_mncc *connect = arg;
1970 struct msgb *msg = gsm48_msgb_alloc();
1971 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1972
Harald Welte4bfdfe72009-06-10 23:11:52 +08001973 gh->msg_type = GSM48_MT_CC_CONNECT;
1974
1975 gsm48_stop_cc_timer(trans);
1976 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1977
1978 /* facility */
1979 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001980 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981 /* progress */
1982 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001983 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984 /* connected number */
1985 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001986 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001987 /* user-user */
1988 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001989 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001990
1991 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1992
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001993 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001994}
1995
1996static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1997{
1998 struct gsm48_hdr *gh = msgb_l3(msg);
1999 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2000 struct tlv_parsed tp;
2001 struct gsm_mncc connect;
2002
2003 gsm48_stop_cc_timer(trans);
2004
2005 memset(&connect, 0, sizeof(struct gsm_mncc));
2006 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002007 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002008 /* use subscriber as connected party number */
2009 if (trans->subscr) {
2010 connect.fields |= MNCC_F_CONNECTED;
2011 strncpy(connect.connected.number, trans->subscr->extension,
2012 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002013 strncpy(connect.imsi, trans->subscr->imsi,
2014 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002015 }
2016 /* facility */
2017 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2018 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002019 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002020 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2021 }
2022 /* user-user */
2023 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2024 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002025 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002026 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2027 }
2028 /* ss-version */
2029 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2030 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002031 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002032 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2033 }
2034
2035 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2036
Harald Welte596fed42009-07-23 19:06:52 +02002037 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002038}
2039
2040
2041static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2042{
2043 struct gsm_mncc connect_ack;
2044
2045 gsm48_stop_cc_timer(trans);
2046
2047 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2048
2049 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2050 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002051 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052 &connect_ack);
2053}
2054
2055static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2056{
2057 struct msgb *msg = gsm48_msgb_alloc();
2058 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2059
Harald Welte4bfdfe72009-06-10 23:11:52 +08002060 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2061
2062 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2063
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002064 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002065}
2066
2067static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2068{
2069 struct gsm48_hdr *gh = msgb_l3(msg);
2070 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2071 struct tlv_parsed tp;
2072 struct gsm_mncc disc;
2073
2074 gsm48_stop_cc_timer(trans);
2075
2076 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2077
2078 memset(&disc, 0, sizeof(struct gsm_mncc));
2079 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002080 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002081 /* cause */
2082 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2083 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002084 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002085 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2086 }
2087 /* facility */
2088 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2089 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002090 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002091 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2092 }
2093 /* user-user */
2094 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2095 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002096 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002097 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2098 }
2099 /* ss-version */
2100 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2101 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002102 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002103 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2104 }
2105
Harald Welte596fed42009-07-23 19:06:52 +02002106 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002107
2108}
2109
Harald Weltec66b71c2009-06-11 14:23:20 +08002110static struct gsm_mncc_cause default_cause = {
2111 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2112 .coding = 0,
2113 .rec = 0,
2114 .rec_val = 0,
2115 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2116 .diag_len = 0,
2117 .diag = { 0 },
2118};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002119
2120static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2121{
2122 struct gsm_mncc *disc = arg;
2123 struct msgb *msg = gsm48_msgb_alloc();
2124 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2125
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2127
2128 gsm48_stop_cc_timer(trans);
2129 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2130
2131 /* cause */
2132 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002133 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134 else
Harald Welte55c8f352010-03-07 23:40:35 +01002135 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136
2137 /* facility */
2138 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002139 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140 /* progress */
2141 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002142 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002143 /* user-user */
2144 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002145 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002146
2147 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002148 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149
2150 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2151
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002152 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153}
2154
2155static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2156{
2157 struct gsm48_hdr *gh = msgb_l3(msg);
2158 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2159 struct tlv_parsed tp;
2160 struct gsm_mncc rel;
2161 int rc;
2162
2163 gsm48_stop_cc_timer(trans);
2164
2165 memset(&rel, 0, sizeof(struct gsm_mncc));
2166 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002167 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168 /* cause */
2169 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2170 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002171 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2173 }
2174 /* facility */
2175 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2176 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002177 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002178 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2179 }
2180 /* user-user */
2181 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2182 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002183 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002184 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2185 }
2186 /* ss-version */
2187 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2188 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002189 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002190 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2191 }
2192
Harald Weltedcaf5652009-07-23 18:56:43 +02002193 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002195 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002196 } else {
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08002197 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02002198 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002199 GSM48_MT_CC_RELEASE_COMPL);
2200 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002201 }
2202
2203 new_cc_state(trans, GSM_CSTATE_NULL);
2204
2205 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002206 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002207
2208 return rc;
2209}
2210
2211static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2212{
2213 struct gsm_mncc *rel = arg;
2214 struct msgb *msg = gsm48_msgb_alloc();
2215 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2216
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 gh->msg_type = GSM48_MT_CC_RELEASE;
2218
2219 trans->callref = 0;
2220
2221 gsm48_stop_cc_timer(trans);
2222 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2223
2224 /* cause */
2225 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002226 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002227 /* facility */
2228 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002229 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002230 /* user-user */
2231 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002232 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002233
Harald Weltedcaf5652009-07-23 18:56:43 +02002234 trans->cc.T308_second = 0;
2235 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236
Harald Weltedcaf5652009-07-23 18:56:43 +02002237 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002238 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2239
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002240 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002241}
2242
2243static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2244{
2245 struct gsm48_hdr *gh = msgb_l3(msg);
2246 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2247 struct tlv_parsed tp;
2248 struct gsm_mncc rel;
2249 int rc = 0;
2250
2251 gsm48_stop_cc_timer(trans);
2252
2253 memset(&rel, 0, sizeof(struct gsm_mncc));
2254 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002255 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256 /* cause */
2257 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2258 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002259 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2261 }
2262 /* facility */
2263 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2264 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002265 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002266 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2267 }
2268 /* user-user */
2269 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2270 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002271 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2273 }
2274 /* ss-version */
2275 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2276 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002277 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002278 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2279 }
2280
2281 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002282 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002284 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 MNCC_REJ_IND, &rel);
2286 break;
2287 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002288 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289 MNCC_REL_CNF, &rel);
2290 break;
2291 default:
Harald Welte596fed42009-07-23 19:06:52 +02002292 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 MNCC_REL_IND, &rel);
2294 }
2295 }
2296
2297 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002298 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299
2300 return rc;
2301}
2302
2303static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2304{
2305 struct gsm_mncc *rel = arg;
2306 struct msgb *msg = gsm48_msgb_alloc();
2307 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2308
Harald Welte4bfdfe72009-06-10 23:11:52 +08002309 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2310
2311 trans->callref = 0;
2312
2313 gsm48_stop_cc_timer(trans);
2314
2315 /* cause */
2316 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002317 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318 /* facility */
2319 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002320 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321 /* user-user */
2322 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002323 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002324
Harald Weltedcaf5652009-07-23 18:56:43 +02002325 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002326
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002327 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002328}
2329
2330static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2331{
2332 struct gsm48_hdr *gh = msgb_l3(msg);
2333 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2334 struct tlv_parsed tp;
2335 struct gsm_mncc fac;
2336
2337 memset(&fac, 0, sizeof(struct gsm_mncc));
2338 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002339 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 /* facility */
2341 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2342 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002343 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2345 }
2346 /* ss-version */
2347 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2348 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002349 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2351 }
2352
Harald Welte596fed42009-07-23 19:06:52 +02002353 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002354}
2355
2356static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2357{
2358 struct gsm_mncc *fac = arg;
2359 struct msgb *msg = gsm48_msgb_alloc();
2360 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2361
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362 gh->msg_type = GSM48_MT_CC_FACILITY;
2363
2364 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002365 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002367 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368}
2369
2370static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2371{
2372 struct gsm_mncc hold;
2373
2374 memset(&hold, 0, sizeof(struct gsm_mncc));
2375 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002376 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377}
2378
2379static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2380{
2381 struct msgb *msg = gsm48_msgb_alloc();
2382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2383
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2385
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002386 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002387}
2388
2389static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2390{
2391 struct gsm_mncc *hold_rej = arg;
2392 struct msgb *msg = gsm48_msgb_alloc();
2393 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2394
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2396
2397 /* cause */
2398 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002399 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002400 else
Harald Welte55c8f352010-03-07 23:40:35 +01002401 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002403 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404}
2405
2406static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2407{
2408 struct gsm_mncc retrieve;
2409
2410 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2411 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002412 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2413 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414}
2415
2416static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2417{
2418 struct msgb *msg = gsm48_msgb_alloc();
2419 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2420
Harald Welte4bfdfe72009-06-10 23:11:52 +08002421 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2422
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002423 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424}
2425
2426static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2427{
2428 struct gsm_mncc *retrieve_rej = arg;
2429 struct msgb *msg = gsm48_msgb_alloc();
2430 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2431
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2433
2434 /* cause */
2435 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002436 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002437 else
Harald Welte55c8f352010-03-07 23:40:35 +01002438 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002440 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002441}
2442
2443static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2444{
2445 struct gsm48_hdr *gh = msgb_l3(msg);
2446 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2447 struct tlv_parsed tp;
2448 struct gsm_mncc dtmf;
2449
2450 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2451 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002452 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002453 /* keypad facility */
2454 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2455 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2458 }
2459
Harald Welte596fed42009-07-23 19:06:52 +02002460 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461}
2462
2463static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2464{
2465 struct gsm_mncc *dtmf = arg;
2466 struct msgb *msg = gsm48_msgb_alloc();
2467 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2468
Harald Welte4bfdfe72009-06-10 23:11:52 +08002469 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2470
2471 /* keypad */
2472 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002473 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002475 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002476}
2477
2478static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2479{
2480 struct gsm_mncc *dtmf = arg;
2481 struct msgb *msg = gsm48_msgb_alloc();
2482 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2483
Harald Welte4bfdfe72009-06-10 23:11:52 +08002484 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2485
2486 /* cause */
2487 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002488 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002489 else
Harald Welte55c8f352010-03-07 23:40:35 +01002490 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002492 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2496{
2497 struct msgb *msg = gsm48_msgb_alloc();
2498 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2499
Harald Welte4bfdfe72009-06-10 23:11:52 +08002500 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2501
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002502 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503}
2504
2505static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2506{
2507 struct gsm_mncc dtmf;
2508
2509 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2510 dtmf.callref = trans->callref;
2511
Harald Welte596fed42009-07-23 19:06:52 +02002512 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513}
2514
2515static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2516{
2517 struct gsm48_hdr *gh = msgb_l3(msg);
2518 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2519 struct tlv_parsed tp;
2520 struct gsm_mncc modify;
2521
2522 memset(&modify, 0, sizeof(struct gsm_mncc));
2523 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002524 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525 /* bearer capability */
2526 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2527 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002528 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002529 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2530 }
2531
2532 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2533
Harald Welte596fed42009-07-23 19:06:52 +02002534 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535}
2536
2537static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2538{
2539 struct gsm_mncc *modify = arg;
2540 struct msgb *msg = gsm48_msgb_alloc();
2541 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2542
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 gh->msg_type = GSM48_MT_CC_MODIFY;
2544
2545 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2546
2547 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002548 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002549
2550 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2551
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002552 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002553}
2554
2555static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2556{
2557 struct gsm48_hdr *gh = msgb_l3(msg);
2558 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2559 struct tlv_parsed tp;
2560 struct gsm_mncc modify;
2561
2562 gsm48_stop_cc_timer(trans);
2563
2564 memset(&modify, 0, sizeof(struct gsm_mncc));
2565 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002566 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567 /* bearer capability */
2568 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2569 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002570 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2572 }
2573
2574 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2575
Harald Welte596fed42009-07-23 19:06:52 +02002576 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002577}
2578
2579static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2580{
2581 struct gsm_mncc *modify = arg;
2582 struct msgb *msg = gsm48_msgb_alloc();
2583 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2584
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2586
2587 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002588 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589
2590 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2591
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002592 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593}
2594
2595static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2596{
2597 struct gsm48_hdr *gh = msgb_l3(msg);
2598 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2599 struct tlv_parsed tp;
2600 struct gsm_mncc modify;
2601
2602 gsm48_stop_cc_timer(trans);
2603
2604 memset(&modify, 0, sizeof(struct gsm_mncc));
2605 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002606 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607 /* bearer capability */
2608 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2609 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002610 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2612 }
2613 /* cause */
2614 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2615 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002616 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002617 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2618 }
2619
2620 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2621
Harald Welte596fed42009-07-23 19:06:52 +02002622 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002623}
2624
2625static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2626{
2627 struct gsm_mncc *modify = arg;
2628 struct msgb *msg = gsm48_msgb_alloc();
2629 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2630
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2632
2633 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002634 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002636 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002637
2638 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2639
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002640 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641}
2642
2643static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2644{
2645 struct gsm_mncc *notify = arg;
2646 struct msgb *msg = gsm48_msgb_alloc();
2647 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2648
Harald Welte4bfdfe72009-06-10 23:11:52 +08002649 gh->msg_type = GSM48_MT_CC_NOTIFY;
2650
2651 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002652 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002654 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655}
2656
2657static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2658{
2659 struct gsm48_hdr *gh = msgb_l3(msg);
2660 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2661// struct tlv_parsed tp;
2662 struct gsm_mncc notify;
2663
2664 memset(&notify, 0, sizeof(struct gsm_mncc));
2665 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002666// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002668 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669
Harald Welte596fed42009-07-23 19:06:52 +02002670 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671}
2672
2673static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2674{
2675 struct gsm_mncc *user = arg;
2676 struct msgb *msg = gsm48_msgb_alloc();
2677 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2678
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679 gh->msg_type = GSM48_MT_CC_USER_INFO;
2680
2681 /* user-user */
2682 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002683 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002684 /* more data */
2685 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002686 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002688 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689}
2690
2691static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2692{
2693 struct gsm48_hdr *gh = msgb_l3(msg);
2694 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2695 struct tlv_parsed tp;
2696 struct gsm_mncc user;
2697
2698 memset(&user, 0, sizeof(struct gsm_mncc));
2699 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002700 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701 /* user-user */
2702 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2703 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002704 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2706 }
2707 /* more data */
2708 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2709 user.more = 1;
2710
Harald Welte596fed42009-07-23 19:06:52 +02002711 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002712}
2713
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002714static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002715{
2716 struct gsm_mncc *mode = arg;
2717
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002718 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719}
2720
2721static struct downstate {
2722 u_int32_t states;
2723 int type;
2724 int (*rout) (struct gsm_trans *trans, void *arg);
2725} downstatelist[] = {
2726 /* mobile originating call establishment */
2727 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2728 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2729 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2730 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2731 {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 */
2732 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2733 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2734 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2735 /* mobile terminating call establishment */
2736 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2737 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2738 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2739 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2740 /* signalling during call */
2741 {SBIT(GSM_CSTATE_ACTIVE),
2742 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2743 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2744 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2745 {ALL_STATES,
2746 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2747 {ALL_STATES,
2748 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2749 {ALL_STATES,
2750 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2751 {SBIT(GSM_CSTATE_ACTIVE),
2752 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2753 {SBIT(GSM_CSTATE_ACTIVE),
2754 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2755 {SBIT(GSM_CSTATE_ACTIVE),
2756 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2757 {SBIT(GSM_CSTATE_ACTIVE),
2758 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2759 {SBIT(GSM_CSTATE_ACTIVE),
2760 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2761 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2762 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2763 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2764 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2765 {SBIT(GSM_CSTATE_ACTIVE),
2766 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2767 /* clearing */
2768 {SBIT(GSM_CSTATE_INITIATED),
2769 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2770 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2771 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2772 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2773 MNCC_REL_REQ, gsm48_cc_tx_release},
2774 /* special */
2775 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002776 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002777};
2778
2779#define DOWNSLLEN \
2780 (sizeof(downstatelist) / sizeof(struct downstate))
2781
2782
2783int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2784{
Harald Welte1a6f7982009-08-09 18:52:33 +02002785 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002786 struct gsm_trans *trans = NULL, *transt;
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002787 struct gsm_subscriber_connection *conn = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002788 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002789 struct gsm_mncc *data = arg, rel;
2790
2791 /* handle special messages */
2792 switch(msg_type) {
2793 case MNCC_BRIDGE:
2794 return tch_bridge(net, arg);
2795 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002796 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002797 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002798 return tch_recv_mncc(net, data->callref, 1);
2799 case GSM_TCHF_FRAME:
2800 /* Find callref */
2801 trans = trans_find_by_callref(net, data->callref);
2802 if (!trans)
2803 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002804 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002805 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002806 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002807 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002808 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002809 switch (bts->type) {
2810 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002811 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002812 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002813 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002814 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002815 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002816 default:
2817 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2818 }
2819 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002820 }
2821
2822 memset(&rel, 0, sizeof(struct gsm_mncc));
2823 rel.callref = data->callref;
2824
2825 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002826 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002827
2828 /* Callref unknown */
2829 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002830 struct gsm_subscriber *subscr;
2831
Harald Welte4a3464c2009-07-04 10:11:24 +02002832 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002833 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2834 "Received '%s' from MNCC with "
2835 "unknown callref %d\n", data->called.number,
2836 get_mncc_name(msg_type), data->callref);
2837 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002838 return mncc_release_ind(net, NULL, data->callref,
2839 GSM48_CAUSE_LOC_PRN_S_LU,
2840 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002842 if (!data->called.number[0] && !data->imsi[0]) {
2843 DEBUGP(DCC, "(bts - trx - ts - ti) "
2844 "Received '%s' from MNCC with "
2845 "no number or IMSI\n", get_mncc_name(msg_type));
2846 /* Invalid number */
2847 return mncc_release_ind(net, NULL, data->callref,
2848 GSM48_CAUSE_LOC_PRN_S_LU,
2849 GSM48_CC_CAUSE_INV_NR_FORMAT);
2850 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002852 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002853 subscr = subscr_get_by_extension(net,
2854 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002855 else
Harald Welte9176bd42009-07-23 18:46:00 +02002856 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857 /* If subscriber is not found */
2858 if (!subscr) {
2859 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2860 "Received '%s' from MNCC with "
2861 "unknown subscriber %s\n", data->called.number,
2862 get_mncc_name(msg_type), data->called.number);
2863 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002864 return mncc_release_ind(net, NULL, data->callref,
2865 GSM48_CAUSE_LOC_PRN_S_LU,
2866 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002867 }
2868 /* If subscriber is not "attached" */
2869 if (!subscr->lac) {
2870 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2871 "Received '%s' from MNCC with "
2872 "detached subscriber %s\n", data->called.number,
2873 get_mncc_name(msg_type), data->called.number);
2874 subscr_put(subscr);
2875 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002876 return mncc_release_ind(net, NULL, data->callref,
2877 GSM48_CAUSE_LOC_PRN_S_LU,
2878 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 }
2880 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002881 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2882 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002883 DEBUGP(DCC, "No memory for trans.\n");
2884 subscr_put(subscr);
2885 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002886 mncc_release_ind(net, NULL, data->callref,
2887 GSM48_CAUSE_LOC_PRN_S_LU,
2888 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 return -ENOMEM;
2890 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002891 /* Find lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002892 conn = connection_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002893
Harald Welte4bfdfe72009-06-10 23:11:52 +08002894 /* If subscriber has no lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002895 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002896 /* find transaction with this subscriber already paging */
2897 llist_for_each_entry(transt, &net->trans_list, entry) {
2898 /* Transaction of our lchan? */
2899 if (transt == trans ||
2900 transt->subscr != subscr)
2901 continue;
2902 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2903 "Received '%s' from MNCC with "
2904 "unallocated channel, paging already "
2905 "started.\n", bts->nr,
2906 data->called.number,
2907 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002908 subscr_put(subscr);
2909 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002910 return 0;
2911 }
2912 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002913 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002914 /* Trigger paging */
2915 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2916 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002917 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918 return 0;
2919 }
2920 /* Assign lchan */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002921 trans->conn = conn;
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002922 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002924
2925 if (trans->conn)
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002926 conn = trans->conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927
2928 /* if paging did not respond yet */
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002929 if (!conn) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002930 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002931 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 (trans->subscr)?(trans->subscr->extension):"-",
2933 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002934 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2935 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 if (msg_type == MNCC_REL_REQ)
2937 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2938 else
2939 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2940 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002941 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 return rc;
2943 }
2944
2945 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2946 "Received '%s' from MNCC in state %d (%s)\n",
Holger Hans Peter Freytherb2be1952010-06-16 13:23:55 +08002947 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002949 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002950 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002951 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002952
2953 /* Find function for current state and message */
2954 for (i = 0; i < DOWNSLLEN; i++)
2955 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002956 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002957 break;
2958 if (i == DOWNSLLEN) {
2959 DEBUGP(DCC, "Message unhandled at this state.\n");
2960 return 0;
2961 }
2962
2963 rc = downstatelist[i].rout(trans, arg);
2964
2965 return rc;
2966}
2967
2968
2969static struct datastate {
2970 u_int32_t states;
2971 int type;
2972 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2973} datastatelist[] = {
2974 /* mobile originating call establishment */
2975 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2976 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2977 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2978 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2979 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2980 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2981 /* mobile terminating call establishment */
2982 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2983 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2984 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2985 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002986 {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 +08002987 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2988 /* signalling during call */
2989 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2990 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2991 {SBIT(GSM_CSTATE_ACTIVE),
2992 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2993 {ALL_STATES,
2994 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2995 {ALL_STATES,
2996 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2997 {ALL_STATES,
2998 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2999 {SBIT(GSM_CSTATE_ACTIVE),
3000 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3001 {SBIT(GSM_CSTATE_ACTIVE),
3002 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3003 {SBIT(GSM_CSTATE_ACTIVE),
3004 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3005 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3006 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3007 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3008 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3009 {SBIT(GSM_CSTATE_ACTIVE),
3010 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3011 /* clearing */
3012 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3013 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3014 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3015 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3016 {ALL_STATES, /* 5.4.3.4 */
3017 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3018};
3019
3020#define DATASLLEN \
3021 (sizeof(datastatelist) / sizeof(struct datastate))
3022
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003023static int gsm0408_rcv_cc(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte4bc90a12008-12-27 16:32:52 +00003024{
3025 struct gsm48_hdr *gh = msgb_l3(msg);
3026 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003027 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Weltedcaf5652009-07-23 18:56:43 +02003028 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003029 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003030
Harald Welte4bfdfe72009-06-10 23:11:52 +08003031 if (msg_type & 0x80) {
3032 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3033 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003034 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003035
Harald Welte4bfdfe72009-06-10 23:11:52 +08003036 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003037 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003038
Harald Welte6f5aee02009-07-23 21:21:14 +02003039 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040 "Received '%s' from MS in state %d (%s)\n",
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003041 conn->bts->nr, conn->lchan->ts->trx->nr, conn->lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003042 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003043 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3044 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003045
3046 /* Create transaction */
3047 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003048 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 "creating new trans.\n", transaction_id);
3050 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003051 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003052 transaction_id, new_callref++);
3053 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003054 DEBUGP(DCC, "No memory for trans.\n");
Holger Hans Peter Freythere9ed3402010-06-16 12:30:50 +08003055 rc = gsm48_tx_simple(trans->conn,
Harald Welte6f5aee02009-07-23 21:21:14 +02003056 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003057 GSM48_MT_CC_RELEASE_COMPL);
3058 return -ENOMEM;
3059 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003060 /* Assign transaction */
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003061 trans->conn = conn;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003062 }
3063
3064 /* find function for current state and message */
3065 for (i = 0; i < DATASLLEN; i++)
3066 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003067 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068 break;
3069 if (i == DATASLLEN) {
3070 DEBUGP(DCC, "Message unhandled at this state.\n");
3071 return 0;
3072 }
3073
3074 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003075
3076 return rc;
3077}
3078
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003079/* here we get data from the BSC level... */
3080int gsm0408_dispatch(struct gsm_subscriber_connection *conn, struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +00003081{
3082 struct gsm48_hdr *gh = msgb_l3(msg);
3083 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003084 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003085
Holger Hans Peter Freyther758f4df2010-06-21 10:34:03 +08003086 if (silent_call_reroute(conn, msg))
3087 return silent_call_rx(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003088
3089 switch (pdisc) {
3090 case GSM48_PDISC_CC:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003091 rc = gsm0408_rcv_cc(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003092 break;
3093 case GSM48_PDISC_MM:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003094 rc = gsm0408_rcv_mm(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003095 break;
3096 case GSM48_PDISC_RR:
Holger Hans Peter Freyther3f122be2010-06-17 17:14:35 +08003097 rc = gsm0408_rcv_rr(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003098 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003099 case GSM48_PDISC_SMS:
Holger Hans Peter Freyther97643312010-06-17 16:41:25 +08003100 rc = gsm0411_rcv_sms(conn, msg);
Harald Weltebcae43f2008-12-27 21:45:37 +00003101 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003102 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003103 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003104 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3105 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003106 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003107 case GSM48_PDISC_NC_SS:
Holger Hans Peter Freytherd42c3f22010-06-17 17:35:57 +08003108 rc = handle_rcv_ussd(conn, msg);
Harald Welte6eafe912009-10-16 08:32:58 +02003109 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003110 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003111 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3112 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003113 break;
3114 }
3115
3116 return rc;
3117}
Harald Welte8470bf22008-12-25 23:28:35 +00003118
Harald Welte805f6442009-07-28 18:25:29 +02003119/*
3120 * This will be ran by the linker when loading the DSO. We use it to
3121 * do system initialization, e.g. registration of signal handlers.
3122 */
3123static __attribute__((constructor)) void on_dso_load_0408(void)
3124{
Harald Welte805f6442009-07-28 18:25:29 +02003125 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3126}