blob: 21bdfc51a0af30c70e5e9c0cf9c215d6f2c509f8 [file] [log] [blame]
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001/* GSM Mobile Radio Interface Layer 3 messages on the A-bis interface
Harald Welte52b1f982008-12-23 20:25:15 +00002 * 3GPP TS 04.08 version 7.21.0 Release 1998 / ETSI TS 100 940 V7.21.0 */
3
Harald Weltebf5e8df2009-02-03 12:59:45 +00004/* (C) 2008-2009 by Harald Welte <laforge@gnumonks.org>
Harald Welte498b0bb2009-01-09 21:27:43 +00005 * (C) 2008, 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welte8470bf22008-12-25 23:28:35 +00006 *
Harald Welte52b1f982008-12-23 20:25:15 +00007 * All Rights Reserved
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
Harald Weltedb253af2008-12-30 17:56:55 +000030#include <time.h>
Harald Welte4b634542008-12-27 01:55:51 +000031#include <netinet/in.h>
Harald Welte52b1f982008-12-23 20:25:15 +000032
Sylvain Munaut30a15382009-12-24 00:27:26 +010033#include <openbsc/auth.h>
Harald Welte75a983f2008-12-27 21:34:06 +000034#include <openbsc/db.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010035#include <osmocore/msgb.h>
36#include <osmocore/bitvec.h>
37#include <osmocore/tlv.h>
Harald Welte8470bf22008-12-25 23:28:35 +000038#include <openbsc/debug.h>
39#include <openbsc/gsm_data.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010040#include <osmocore/gsm_utils.h>
Harald Welte8470bf22008-12-25 23:28:35 +000041#include <openbsc/gsm_subscriber.h>
Daniel Willmann8b3390e2008-12-28 00:31:09 +000042#include <openbsc/gsm_04_11.h>
Harald Welte8470bf22008-12-25 23:28:35 +000043#include <openbsc/gsm_04_08.h>
44#include <openbsc/abis_rsl.h>
Holger Freytherca362a62009-01-04 21:05:01 +000045#include <openbsc/chan_alloc.h>
Harald Welte0b4c34e2009-02-09 17:54:43 +000046#include <openbsc/paging.h>
Holger Freyther053e09d2009-02-14 22:51:06 +000047#include <openbsc/signal.h>
Harald Welte45b407a2009-05-23 15:51:12 +000048#include <openbsc/trau_frame.h>
Harald Welte11fa29c2009-02-19 17:24:39 +000049#include <openbsc/trau_mux.h>
Harald Welte805f6442009-07-28 18:25:29 +020050#include <openbsc/rtp_proxy.h>
Harald Weltedfe6c7d2010-02-20 16:24:02 +010051#include <osmocore/talloc.h>
Harald Welte55c8f352010-03-07 23:40:35 +010052#include <osmocore/gsm48.h>
Harald Weltedcaf5652009-07-23 18:56:43 +020053#include <openbsc/transaction.h>
Harald Welte6eafe912009-10-16 08:32:58 +020054#include <openbsc/ussd.h>
Harald Welte51008772009-12-29 11:49:12 +010055#include <openbsc/silent_call.h>
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080056#include <openbsc/bsc_api.h>
Harald Welte52b1f982008-12-23 20:25:15 +000057
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020058void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010059void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020060
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +080061int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000062static int gsm48_tx_simple(struct gsm_lchan *lchan,
63 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010064static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000065
Harald Welte52b1f982008-12-23 20:25:15 +000066struct gsm_lai {
67 u_int16_t mcc;
68 u_int16_t mnc;
69 u_int16_t lac;
70};
71
Harald Welte4bfdfe72009-06-10 23:11:52 +080072static u_int32_t new_callref = 0x80000001;
73
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +080074static int gsm48_conn_sendmsg(struct msgb *msg, struct gsm_subscriber_connection *conn,
75 struct gsm_trans *trans)
76{
77 struct gsm48_hdr *gh = (struct gsm48_hdr *) msg->data;
78
79 /* if we get passed a transaction reference, do some common
80 * work that the caller no longer has to do */
81 if (trans) {
82 gh->proto_discr = trans->protocol | (trans->transaction_id << 4);
83 msg->lchan = trans->conn->lchan;
84 }
85
86
87 if (msg->lchan) {
88 msg->trx = msg->lchan->ts->trx;
89 if ((gh->proto_discr & GSM48_PDISC_MASK) == GSM48_PDISC_CC)
90 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x) "
91 "Sending '%s' to MS.\n", msg->trx->bts->nr,
92 msg->trx->nr, msg->lchan->ts->nr,
93 gh->proto_discr & 0xf0,
94 gsm48_cc_msg_name(gh->msg_type));
95 else
96 DEBUGP(DCC, "(bts %d trx %d ts %d pd %02x) "
97 "Sending 0x%02x to MS.\n", msg->trx->bts->nr,
98 msg->trx->nr, msg->lchan->ts->nr,
99 gh->proto_discr, gh->msg_type);
100 }
101
102 msg->l3h = msg->data;
103 return gsm0808_submit_dtap(conn, msg, 0);
104}
Sylvain Munaut30a15382009-12-24 00:27:26 +0100105
106static void release_security_operation(struct gsm_subscriber_connection *conn)
107{
108 if (!conn->sec_operation)
109 return;
110
111 talloc_free(conn->sec_operation);
112 conn->sec_operation = NULL;
113 put_subscr_con(conn);
114}
115
116static void allocate_security_operation(struct gsm_subscriber_connection *conn)
117{
118 use_subscr_con(conn)
119
120 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
121 struct gsm_security_operation);
122}
123
124int gsm48_secure_channel(struct gsm_lchan *lchan, int key_seq,
125 gsm_cbfn *cb, void *cb_data)
126{
127 struct gsm_network *net = lchan->ts->trx->bts->network;
128 struct gsm_subscriber *subscr = lchan->conn.subscr;
129 struct gsm_security_operation *op;
130 struct gsm_auth_tuple atuple;
131 int status = -1, rc;
132
133 /* Check if we _can_ enable encryption. Cases where we can't:
134 * - Encryption disabled in config
135 * - Channel already secured (nothing to do)
136 * - Subscriber equipment doesn't support configured encryption
137 */
138 if (!net->a5_encryption) {
139 status = GSM_SECURITY_NOAVAIL;
140 } else if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
141 DEBUGP(DMM, "Requesting to secure an already secure channel");
142 status = GSM_SECURITY_SUCCEEDED;
143 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
144 net->a5_encryption)) {
145 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
146 status = GSM_SECURITY_NOAVAIL;
147 }
148
149 /* If not done yet, try to get info for this user */
150 if (status < 0) {
151 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
152 if (rc <= 0)
153 status = GSM_SECURITY_NOAVAIL;
154 }
155
156 /* Are we done yet ? */
157 if (status >= 0)
158 return cb ?
159 cb(GSM_HOOK_RR_SECURITY, status, NULL, lchan, cb_data) :
160 0;
161
162 /* Start an operation (can't have more than one pending !!!) */
163 if (lchan->conn.sec_operation)
164 return -EBUSY;
165
166 allocate_security_operation(&lchan->conn);
167 op = lchan->conn.sec_operation;
168 op->cb = cb;
169 op->cb_data = cb_data;
170 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
171
172 /* FIXME: Should start a timer for completion ... */
173
174 /* Then do whatever is needed ... */
175 if (rc == 1) {
176 /* Start authentication */
177 return gsm48_tx_mm_auth_req(lchan, op->atuple.rand, op->atuple.key_seq);
178 } else if (rc == 2) {
179 /* Start ciphering directly */
180 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
181 lchan->encr.key_len = 8;
182 memcpy(lchan->encr.key, op->atuple.kc, 8);
183
184 return gsm48_send_rr_ciph_mode(lchan, 0);
185 }
186
187 return -EINVAL; /* not reached */
188}
189
Holger Freyther73487a22008-12-31 18:53:57 +0000190static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
191 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000192{
193 if (!subscriber)
194 return 0;
195
Holger Freyther73487a22008-12-31 18:53:57 +0000196 /*
197 * Do not send accept yet as more information should arrive. Some
198 * phones will not send us the information and we will have to check
199 * what we want to do with that.
200 */
201 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
202 return 0;
203
Jan Luebbe06513f22009-08-12 12:48:00 +0200204 switch (subscriber->net->auth_policy) {
205 case GSM_AUTH_POLICY_CLOSED:
206 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200207 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200208 if (subscriber->authorized)
209 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200210 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200211 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000212 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200213 default:
214 return 0;
215 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000216}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000217
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100218static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000219{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100220 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000221 return;
222
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100223 bsc_del_timer(&conn->loc_operation->updating_timer);
224 talloc_free(conn->loc_operation);
225 conn->loc_operation = 0;
226 put_subscr_con(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000227}
228
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100229static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000230{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100231 use_subscr_con(conn)
232 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000233
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100234 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200235 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000236}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000237
Sylvain Munaut267fba02009-12-24 00:28:01 +0100238static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
239 struct msgb *msg, void *data, void *param)
240{
241 struct gsm_lchan *lchan = data;
242 struct gsm_subscriber_connection *conn = &lchan->conn;
243 int rc = 0;
244
245 switch (event) {
246 case GSM_SECURITY_AUTH_FAILED:
247 release_loc_updating_req(conn);
248 break;
249
250 case GSM_SECURITY_NOAVAIL:
251 case GSM_SECURITY_SUCCEEDED:
252 /* We're all good */
253 db_subscriber_alloc_tmsi(conn->subscr);
254 release_loc_updating_req(conn);
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800255 rc = gsm0408_loc_upd_acc(conn, conn->subscr->tmsi);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100256 if (lchan->ts->trx->bts->network->send_mm_info) {
257 /* send MM INFO with network name */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800258 rc = gsm48_tx_mm_info(conn);
Sylvain Munaut267fba02009-12-24 00:28:01 +0100259 }
260
261 /* call subscr_update after putting the loc_upd_acc
262 * in the transmit queue, since S_SUBSCR_ATTACHED might
263 * trigger further action like SMS delivery */
264 subscr_update(conn->subscr, lchan->ts->trx->bts,
265 GSM_SUBSCRIBER_UPDATE_ATTACHED);
266
267 /* try to close channel ASAP */
268 lchan_auto_release(lchan);
269
270 break;
271
272 default:
273 rc = -EINVAL;
274 };
275
276 return rc;
277}
278
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100279static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000280{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100281 if (authorize_subscriber(conn->loc_operation, conn->subscr))
282 return gsm48_secure_channel(conn->lchan,
283 conn->loc_operation->key_seq,
284 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000285 return 0;
286}
287
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800288void gsm0408_clear_request(struct gsm_subscriber_connection* conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000289{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800290 struct gsm_trans *trans, *temp;
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;
Harald Welte8470bf22008-12-25 23:28:35 +0000312 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000313 struct gsm48_hdr *gh;
314
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800315 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000316
317 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
318 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000319 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000320 gh->data[0] = cause;
321
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100322 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100323 "LAC=%u BTS=%u\n", conn->subscr ?
324 subscr_name(conn->subscr) : "unknown",
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800325 bts->location_area_code, bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100326
Harald Welteffa55a42009-12-22 19:07:32 +0100327 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000328
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 */
Harald Welte231ad4f2008-12-27 11:15:38 +0000379static int mm_rx_id_resp(struct msgb *msg)
380{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100381 struct gsm_subscriber_connection *conn;
Harald Welte231ad4f2008-12-27 11:15:38 +0000382 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000383 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200384 struct gsm_bts *bts = lchan->ts->trx->bts;
385 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000386 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200387 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000388
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200389 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000390 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000391 mi_type, mi_string);
392
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100393 conn = &lchan->conn;
394
Harald Welte7659de12009-12-13 12:39:18 +0100395 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
396
Harald Welte75a983f2008-12-27 21:34:06 +0000397 switch (mi_type) {
398 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200399 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100400 if (!conn->subscr) {
401 conn->subscr = subscr_get_by_imsi(net, mi_string);
402 if (!conn->subscr)
403 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200404 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100405 if (conn->loc_operation)
406 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000407 break;
408 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000409 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000410 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100411 if (conn->subscr) {
412 db_subscriber_assoc_imei(conn->subscr, mi_string);
413 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200414 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100415 if (conn->loc_operation)
416 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000417 break;
418 }
Holger Freyther73487a22008-12-31 18:53:57 +0000419
420 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100421 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000422}
423
Harald Welte255539c2008-12-28 02:26:27 +0000424
425static void loc_upd_rej_cb(void *data)
426{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100427 struct gsm_subscriber_connection *conn = data;
428 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100429 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000430
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100431 release_loc_updating_req(conn);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800432 gsm0408_loc_upd_rej(conn, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000433 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000434}
435
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100436static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000437{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100438 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
439 conn->loc_operation->updating_timer.data = conn;
440 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000441}
442
Harald Welte2a139372009-02-22 21:14:55 +0000443static const char *lupd_name(u_int8_t type)
444{
445 switch (type) {
446 case GSM48_LUPD_NORMAL:
447 return "NORMAL";
448 case GSM48_LUPD_PERIODIC:
449 return "PEROIDOC";
450 case GSM48_LUPD_IMSI_ATT:
451 return "IMSI ATTACH";
452 default:
453 return "UNKNOWN";
454 }
455}
456
Harald Weltebf5e8df2009-02-03 12:59:45 +0000457/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000458static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000459{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100460 struct gsm_subscriber_connection *conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000461 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000462 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800463 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000464 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200465 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000466 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200467 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000468 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000469
Harald Welte8470bf22008-12-25 23:28:35 +0000470 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100471 conn = &lchan->conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000472
473 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000474
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200475 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000476
Harald Weltea0368542009-06-27 02:58:43 +0200477 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000478 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000479
Harald Welte7659de12009-12-13 12:39:18 +0100480 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
481
Harald Welte24ff6ee2009-12-22 00:41:05 +0100482 switch (lu->type) {
483 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100484 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100485 break;
486 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100487 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100488 break;
489 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100490 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100491 break;
492 }
493
Holger Freythereaf04692009-06-06 13:54:44 +0000494 /*
495 * Pseudo Spoof detection: Just drop a second/concurrent
496 * location updating request.
497 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100498 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200499 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100500 conn->loc_operation);
Holger Hans Peter Freyther7bc5ba32010-06-15 14:09:34 +0800501 gsm0408_loc_upd_rej(conn, GSM48_REJECT_PROTOCOL_ERROR);
Holger Freythereaf04692009-06-06 13:54:44 +0000502 return 0;
503 }
504
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100505 allocate_loc_updating_req(&lchan->conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000506
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200507 conn->loc_operation->key_seq = lu->key_seq;
508
Harald Welte52b1f982008-12-23 20:25:15 +0000509 switch (mi_type) {
510 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200511 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000512 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800513 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100514 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000515
Jan Luebbe370b41d2009-08-12 10:19:34 +0200516 /* look up subscriber based on IMSI, create if not found */
517 subscr = subscr_get_by_imsi(bts->network, mi_string);
518 if (!subscr) {
519 subscr = db_create_subscriber(bts->network, mi_string);
520 }
Harald Welte4b634542008-12-27 01:55:51 +0000521 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000522 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200523 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000524 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200525 subscr = subscr_get_by_tmsi(bts->network,
526 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000527 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000528 /* send IDENTITY REQUEST message to get IMSI */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800529 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100530 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000531 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200532 /* we always want the IMEI, too */
Holger Hans Peter Freytherd521d972010-06-15 14:11:01 +0800533 rc = mm_tx_identity_req(conn, GSM_MI_TYPE_IMEI);
Harald Weltef5f512c2010-06-07 17:56:32 +0200534 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000535 break;
536 case GSM_MI_TYPE_IMEI:
537 case GSM_MI_TYPE_IMEISV:
538 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200539 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000540 break;
541 default:
Harald Weltea0368542009-06-27 02:58:43 +0200542 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000543 break;
544 }
545
Harald Welte24516ea2009-07-04 10:18:00 +0200546 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100547 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200548
Harald Welte4bfdfe72009-06-10 23:11:52 +0800549 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200550 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 /* FIXME: request id? close channel? */
552 return -EINVAL;
553 }
554
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100555 conn->subscr = subscr;
556 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000557
Harald Welte24516ea2009-07-04 10:18:00 +0200558 /* check if we can let the subscriber into our network immediately
559 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100560 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000561}
562
Harald Welte4bfdfe72009-06-10 23:11:52 +0800563#if 0
564static u_int8_t to_bcd8(u_int8_t val)
565{
566 return ((val / 10) << 4) | (val % 10);
567}
568#endif
569
Harald Weltedb253af2008-12-30 17:56:55 +0000570/* Section 9.2.15a */
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800571int gsm48_tx_mm_info(struct gsm_subscriber_connection *conn)
Harald Weltedb253af2008-12-30 17:56:55 +0000572{
573 struct msgb *msg = gsm48_msgb_alloc();
574 struct gsm48_hdr *gh;
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800575 struct gsm_network *net = conn->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000576 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200577 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800578#if 0
579 time_t cur_t;
580 struct tm* cur_time;
581 int tz15min;
582#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000583
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800584 msg->lchan = conn->lchan;
Harald Weltedb253af2008-12-30 17:56:55 +0000585
586 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
587 gh->proto_discr = GSM48_PDISC_MM;
588 gh->msg_type = GSM48_MT_MM_INFO;
589
590 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200591#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000592 name_len = strlen(net->name_long);
593 /* 10.5.3.5a */
594 ptr8 = msgb_put(msg, 3);
595 ptr8[0] = GSM48_IE_NAME_LONG;
596 ptr8[1] = name_len*2 +1;
597 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
598
599 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
600 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000601 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000602
603 /* FIXME: Use Cell Broadcast, not UCS-2, since
604 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200605#endif
606 name_len = (strlen(net->name_long)*7)/8;
607 name_pad = (8 - strlen(net->name_long)*7)%8;
608 if (name_pad > 0)
609 name_len++;
610 /* 10.5.3.5a */
611 ptr8 = msgb_put(msg, 3);
612 ptr8[0] = GSM48_IE_NAME_LONG;
613 ptr8[1] = name_len +1;
614 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
615
616 ptr8 = msgb_put(msg, name_len);
617 gsm_7bit_encode(ptr8, net->name_long);
618
Harald Weltedb253af2008-12-30 17:56:55 +0000619 }
620
621 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200622#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000623 name_len = strlen(net->name_short);
624 /* 10.5.3.5a */
625 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200626 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000627 ptr8[1] = name_len*2 + 1;
628 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
629
Harald Weltee872cb12009-01-01 00:33:37 +0000630 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000631 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000632 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200633#endif
634 name_len = (strlen(net->name_short)*7)/8;
635 name_pad = (8 - strlen(net->name_short)*7)%8;
636 if (name_pad > 0)
637 name_len++;
638 /* 10.5.3.5a */
639 ptr8 = (u_int8_t *) msgb_put(msg, 3);
640 ptr8[0] = GSM48_IE_NAME_SHORT;
641 ptr8[1] = name_len +1;
642 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
643
644 ptr8 = msgb_put(msg, name_len);
645 gsm_7bit_encode(ptr8, net->name_short);
646
Harald Weltedb253af2008-12-30 17:56:55 +0000647 }
648
649#if 0
650 /* Section 10.5.3.9 */
651 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800652 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000653 ptr8 = msgb_put(msg, 8);
654 ptr8[0] = GSM48_IE_NET_TIME_TZ;
655 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
656 ptr8[2] = to_bcd8(cur_time->tm_mon);
657 ptr8[3] = to_bcd8(cur_time->tm_mday);
658 ptr8[4] = to_bcd8(cur_time->tm_hour);
659 ptr8[5] = to_bcd8(cur_time->tm_min);
660 ptr8[6] = to_bcd8(cur_time->tm_sec);
661 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
662 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800663 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000664 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800665 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000666#endif
667
Daniel Willmanneea93372009-08-13 03:42:07 +0200668 DEBUGP(DMM, "-> MM INFO\n");
669
Holger Hans Peter Freyther91401742010-06-15 14:16:02 +0800670 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000671}
672
Harald Welte7984d5c2009-08-12 22:56:50 +0200673/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100674int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200675{
676 struct msgb *msg = gsm48_msgb_alloc();
677 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200678 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200679
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100680 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200681
682 msg->lchan = lchan;
683 gh->proto_discr = GSM48_PDISC_MM;
684 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
685
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100686 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200687
Harald Welte7984d5c2009-08-12 22:56:50 +0200688 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200689 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200690 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200691
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800692 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200693}
694
695/* Section 9.2.1 */
696int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
697{
698 DEBUGP(DMM, "-> AUTH REJECT\n");
699 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
700}
701
Harald Welte4b634542008-12-27 01:55:51 +0000702static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
703{
Harald Welte4b634542008-12-27 01:55:51 +0000704 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000705 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000706}
Harald Welteba4cf162009-01-10 01:49:35 +0000707
708/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100709static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000710 enum gsm48_reject_value value)
711{
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800712 struct msgb *msg;
Harald Welteba4cf162009-01-10 01:49:35 +0000713
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800714 msg = gsm48_create_mm_serv_rej(value);
715 if (!msg) {
716 LOGP(DMM, LOGL_ERROR, "Failed to allocate CM Service Reject.\n");
717 return -1;
718 }
Harald Welteba4cf162009-01-10 01:49:35 +0000719
Holger Hans Peter Freyther2c0f1662010-06-15 19:39:27 +0800720 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100721 msg->lchan = conn->lchan;
722 use_subscr_con(conn);
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800723 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000724}
725
Sylvain Munautba87f452009-12-24 00:28:46 +0100726static int _gsm48_rx_mm_serv_req_sec_cb(
727 unsigned int hooknum, unsigned int event,
728 struct msgb *msg, void *data, void *param)
729{
730 struct gsm_lchan *lchan = data;
731 int rc = 0;
732
733 switch (event) {
734 case GSM_SECURITY_AUTH_FAILED:
735 /* Nothing to do */
736 break;
737
738 case GSM_SECURITY_NOAVAIL:
739 rc = gsm48_tx_mm_serv_ack(lchan);
740 break;
741
742 case GSM_SECURITY_SUCCEEDED:
743 /* nothing to do. CIPHER MODE COMMAND is
744 * implicit CM SERV ACK */
745 break;
746
747 default:
748 rc = -EINVAL;
749 };
750
751 return rc;
752}
753
Harald Welte4ed0e922009-01-10 03:17:30 +0000754/*
755 * Handle CM Service Requests
756 * a) Verify that the packet is long enough to contain the information
757 * we require otherwsie reject with INCORRECT_MESSAGE
758 * b) Try to parse the TMSI. If we do not have one reject
759 * c) Check that we know the subscriber with the TMSI otherwise reject
760 * with a HLR cause
761 * d) Set the subscriber on the gsm_lchan and accept
762 */
Harald Welte4b634542008-12-27 01:55:51 +0000763static int gsm48_rx_mm_serv_req(struct msgb *msg)
764{
Harald Welteba4cf162009-01-10 01:49:35 +0000765 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200766 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000767
Harald Welte9176bd42009-07-23 18:46:00 +0200768 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000769 struct gsm_subscriber *subscr;
770 struct gsm48_hdr *gh = msgb_l3(msg);
771 struct gsm48_service_request *req =
772 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800773 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000774 u_int8_t classmark2_len = gh->data[1];
775 u_int8_t *classmark2 = gh->data+2;
776 u_int8_t mi_len = *(classmark2 + classmark2_len);
777 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000778
Harald Weltec9e02182009-05-01 19:07:53 +0000779 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000780 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000781 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100782 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000783 GSM48_REJECT_INCORRECT_MESSAGE);
784 }
785
786 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000787 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100788 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000789 GSM48_REJECT_INCORRECT_MESSAGE);
790 }
791
Harald Weltec9e02182009-05-01 19:07:53 +0000792 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000793 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000794 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100795 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000796 GSM48_REJECT_INCORRECT_MESSAGE);
797 }
798
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200799 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000800 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000801 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000802
Harald Welte7659de12009-12-13 12:39:18 +0100803 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
804
Harald Welte3ac7f102009-08-10 10:12:45 +0200805 if (is_siemens_bts(bts))
806 send_siemens_mrpci(msg->lchan, classmark2-1);
807
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200808 subscr = subscr_get_by_tmsi(bts->network,
809 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000810
Harald Welte2a139372009-02-22 21:14:55 +0000811 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000812 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100813 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000814 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
815
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100816 if (!msg->lchan->conn.subscr)
817 msg->lchan->conn.subscr = subscr;
818 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100819 subscr_put(subscr); /* lchan already has a ref, don't need another one */
820 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000821 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
822 subscr_put(subscr);
823 }
824
Harald Weltec2e302d2009-07-05 14:08:13 +0200825 subscr->equipment.classmark2_len = classmark2_len;
826 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
827 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000828
Sylvain Munautba87f452009-12-24 00:28:46 +0100829 return gsm48_secure_channel(msg->lchan, req->cipher_key_seq,
830 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000831}
832
Harald Welte2a139372009-02-22 21:14:55 +0000833static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
834{
Harald Welte9176bd42009-07-23 18:46:00 +0200835 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000836 struct gsm48_hdr *gh = msgb_l3(msg);
837 struct gsm48_imsi_detach_ind *idi =
838 (struct gsm48_imsi_detach_ind *) gh->data;
839 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200840 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800841 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000842
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200843 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000844 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
845 mi_type, mi_string);
846
Harald Welteffa55a42009-12-22 19:07:32 +0100847 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100848
Harald Welte2a139372009-02-22 21:14:55 +0000849 switch (mi_type) {
850 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200851 subscr = subscr_get_by_tmsi(bts->network,
852 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000853 break;
854 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200855 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000856 break;
857 case GSM_MI_TYPE_IMEI:
858 case GSM_MI_TYPE_IMEISV:
859 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000860 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000861 break;
862 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000863 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000864 break;
865 }
866
Holger Freyther4a49e772009-04-12 05:37:29 +0000867 if (subscr) {
868 subscr_update(subscr, msg->trx->bts,
869 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100870 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200871
872 subscr->equipment.classmark1 = idi->classmark1;
873 db_sync_equipment(&subscr->equipment);
874
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000875 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000876 } else
Harald Welte2a139372009-02-22 21:14:55 +0000877 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
878
Harald Welte31981a02009-12-20 09:58:40 +0100879 /* FIXME: iterate over all transactions and release them,
880 * imagine an IMSI DETACH happening during an active call! */
881
Harald Welteb83d9382009-12-12 21:00:48 +0100882 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100883 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100884
Harald Welte2a139372009-02-22 21:14:55 +0000885 return 0;
886}
887
Harald Welted2a7f5a2009-06-05 20:08:20 +0000888static int gsm48_rx_mm_status(struct msgb *msg)
889{
890 struct gsm48_hdr *gh = msgb_l3(msg);
891
892 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
893
894 return 0;
895}
896
Sylvain Munaut30a15382009-12-24 00:27:26 +0100897/* Chapter 9.2.3: Authentication Response */
898static int gsm48_rx_mm_auth_resp(struct msgb *msg)
899{
900 struct gsm48_hdr *gh = msgb_l3(msg);
901 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
902 struct gsm_lchan *lchan = msg->lchan;
903 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
904 struct gsm_network *net = lchan->ts->trx->bts->network;
905
Sylvain Munautc593cf12010-06-10 23:51:41 +0200906 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100907 hexdump(ar->sres, 4));
908
909 /* Safety check */
910 if (!conn->sec_operation) {
911 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
912 return -EIO;
913 }
914
915 /* Validate SRES */
916 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
917 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200918
919 DEBUGPC(DMM, "Invalid (expected %s)\n",
920 hexdump(conn->sec_operation->atuple.sres, 4));
921
Sylvain Munaut30a15382009-12-24 00:27:26 +0100922 if (cb)
923 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
924 NULL, lchan, conn->sec_operation->cb_data);
925
926 release_security_operation(conn);
927 return gsm48_tx_mm_auth_rej(lchan);
928 }
929
Sylvain Munautc593cf12010-06-10 23:51:41 +0200930 DEBUGPC(DMM, "OK\n");
931
Sylvain Munaut30a15382009-12-24 00:27:26 +0100932 /* Start ciphering */
933 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
934 lchan->encr.key_len = 8;
935 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
936
937 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
938}
939
Harald Weltebf5e8df2009-02-03 12:59:45 +0000940/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000941static int gsm0408_rcv_mm(struct msgb *msg)
942{
943 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800944 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000945
946 switch (gh->msg_type & 0xbf) {
947 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200948 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000949 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000950 break;
951 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000952 rc = mm_rx_id_resp(msg);
953 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000954 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000955 rc = gsm48_rx_mm_serv_req(msg);
956 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000957 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000958 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000959 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000960 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000961 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100962 msg->lchan->conn.subscr ?
963 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000964 "unknown subscriber");
965 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000966 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000967 rc = gsm48_rx_mm_imsi_detach_ind(msg);
968 break;
969 case GSM48_MT_MM_CM_REEST_REQ:
970 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
971 break;
972 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut30a15382009-12-24 00:27:26 +0100973 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000974 break;
975 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100976 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000977 gh->msg_type);
978 break;
979 }
980
981 return rc;
982}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000983
Harald Welte2d35ae62009-02-06 12:02:13 +0000984/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200985static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000986{
Harald Welte9176bd42009-07-23 18:46:00 +0200987 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000988 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800989 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000990 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200991 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200992 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800993 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000994 int rc = 0;
995
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800996 resp = (struct gsm48_pag_resp *) &gh->data[0];
997 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
998 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000999 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1000 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001001
Harald Weltefe18d8f2009-02-22 21:14:24 +00001002 switch (mi_type) {
1003 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001004 subscr = subscr_get_by_tmsi(bts->network,
1005 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001006 break;
1007 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001008 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001009 break;
1010 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001011
1012 if (!subscr) {
1013 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001014 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001015 return -EINVAL;
1016 }
1017 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001018 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001019
Harald Weltec2e302d2009-07-05 14:08:13 +02001020 subscr->equipment.classmark2_len = *classmark2_lv;
1021 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1022 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001023
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001024 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001025 return rc;
1026}
1027
Harald Weltef7c43522009-06-09 20:24:21 +00001028static int gsm48_rx_rr_classmark(struct msgb *msg)
1029{
1030 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001031 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001032 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1033 u_int8_t cm2_len, cm3_len = 0;
1034 u_int8_t *cm2, *cm3 = NULL;
1035
1036 DEBUGP(DRR, "CLASSMARK CHANGE ");
1037
1038 /* classmark 2 */
1039 cm2_len = gh->data[0];
1040 cm2 = &gh->data[1];
1041 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1042
1043 if (payload_len > cm2_len + 1) {
1044 /* we must have a classmark3 */
1045 if (gh->data[cm2_len+1] != 0x20) {
1046 DEBUGPC(DRR, "ERR CM3 TAG\n");
1047 return -EINVAL;
1048 }
1049 if (cm2_len > 3) {
1050 DEBUGPC(DRR, "CM2 too long!\n");
1051 return -EINVAL;
1052 }
1053
1054 cm3_len = gh->data[cm2_len+2];
1055 cm3 = &gh->data[cm2_len+3];
1056 if (cm3_len > 14) {
1057 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1058 return -EINVAL;
1059 }
1060 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1061 }
1062 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001063 subscr->equipment.classmark2_len = cm2_len;
1064 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001065 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001066 subscr->equipment.classmark3_len = cm3_len;
1067 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001068 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001069 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001070 }
1071
Harald Weltef7c43522009-06-09 20:24:21 +00001072 return 0;
1073}
1074
Harald Weltecf5b3592009-05-01 18:28:42 +00001075static int gsm48_rx_rr_status(struct msgb *msg)
1076{
1077 struct gsm48_hdr *gh = msgb_l3(msg);
1078
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001079 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001080 rr_cause_name(gh->data[0]));
1081
1082 return 0;
1083}
1084
Harald Weltef7c43522009-06-09 20:24:21 +00001085static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1086{
Harald Welted12b0fd2009-12-15 21:36:05 +01001087 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001088
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001089 /* This shouldn't actually end up here, as RSL treats
1090 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1091 * directly into gsm48_parse_meas_rep */
1092 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001093 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001094
1095 return 0;
1096}
1097
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001098static int gsm48_rx_rr_app_info(struct msgb *msg)
1099{
1100 struct gsm48_hdr *gh = msgb_l3(msg);
1101 u_int8_t apdu_id_flags;
1102 u_int8_t apdu_len;
1103 u_int8_t *apdu_data;
1104
1105 apdu_id_flags = gh->data[0];
1106 apdu_len = gh->data[1];
1107 apdu_data = gh->data+2;
1108
1109 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1110 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1111
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001112 return db_apdu_blob_store(msg->lchan->conn.subscr, apdu_id_flags, apdu_len, apdu_data);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001113}
1114
Sylvain Munaut30a15382009-12-24 00:27:26 +01001115/* Chapter 9.1.10 Ciphering Mode Complete */
1116static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1117{
1118 struct gsm_lchan *lchan = msg->lchan;
1119 struct gsm_subscriber_connection *conn = &lchan->conn;
1120 gsm_cbfn *cb;
1121 int rc = 0;
1122
1123 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1124
1125 /* Safety check */
1126 if (!conn->sec_operation) {
1127 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1128 return -EIO;
1129 }
1130
1131 /* FIXME: check for MI (if any) */
1132
1133 /* Call back whatever was in progress (if anything) ... */
1134 cb = conn->sec_operation->cb;
1135 if (cb) {
1136 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1137 NULL, lchan, conn->sec_operation->cb_data);
1138 }
1139
1140 /* Complete the operation */
1141 release_security_operation(conn);
1142
1143 return rc;
1144}
1145
Harald Welted011e8b2009-11-29 22:45:52 +01001146/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001147static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001148{
1149 struct gsm48_hdr *gh = msgb_l3(msg);
1150
1151 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1152 rr_cause_name(gh->data[0]));
1153
1154 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1155 /* FIXME: release old channel */
1156
1157 return 0;
1158}
1159
1160/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001161static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001162{
1163 struct gsm48_hdr *gh = msgb_l3(msg);
1164
1165 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1166 rr_cause_name(gh->data[0]));
1167
1168 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1169 /* FIXME: release allocated new channel */
1170
1171 return 0;
1172}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001173
Harald Weltebf5e8df2009-02-03 12:59:45 +00001174/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001175static int gsm0408_rcv_rr(struct msgb *msg)
1176{
1177 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001178 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001179
1180 switch (gh->msg_type) {
1181 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001182 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001183 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001184 case GSM48_MT_RR_GPRS_SUSP_REQ:
1185 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1186 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001187 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001188 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001189 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001190 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001191 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001192 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001193 case GSM48_MT_RR_STATUS:
1194 rc = gsm48_rx_rr_status(msg);
1195 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001196 case GSM48_MT_RR_MEAS_REP:
1197 rc = gsm48_rx_rr_meas_rep(msg);
1198 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001199 case GSM48_MT_RR_APP_INFO:
1200 rc = gsm48_rx_rr_app_info(msg);
1201 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001202 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut30a15382009-12-24 00:27:26 +01001203 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001204 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001205 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001206 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001207 break;
1208 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001209 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001210 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001211 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001212 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1213 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001214 break;
1215 }
1216
Harald Welte2d35ae62009-02-06 12:02:13 +00001217 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001218}
1219
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001220int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001221 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001222{
1223 struct msgb *msg = gsm48_msgb_alloc();
1224 struct gsm48_hdr *gh;
1225
1226 msg->lchan = lchan;
1227
1228 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1229 apdu_id, apdu_len);
1230
1231 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1232 gh->proto_discr = GSM48_PDISC_RR;
1233 gh->msg_type = GSM48_MT_RR_APP_INFO;
1234 gh->data[0] = apdu_id;
1235 gh->data[1] = apdu_len;
1236 memcpy(gh->data+2, apdu, apdu_len);
1237
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001238 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001239}
1240
Harald Welte4bc90a12008-12-27 16:32:52 +00001241/* Call Control */
1242
Harald Welte7584aea2009-02-11 11:44:12 +00001243/* The entire call control code is written in accordance with Figure 7.10c
1244 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1245 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1246 * it for voice */
1247
Harald Welte4bfdfe72009-06-10 23:11:52 +08001248static void new_cc_state(struct gsm_trans *trans, int state)
1249{
1250 if (state > 31 || state < 0)
1251 return;
1252
1253 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001254 gsm48_cc_state_name(trans->cc.state),
1255 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001256
Harald Weltedcaf5652009-07-23 18:56:43 +02001257 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001258}
1259
1260static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001261{
1262 struct msgb *msg = gsm48_msgb_alloc();
1263 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1264 u_int8_t *cause, *call_state;
1265
Harald Welte4bc90a12008-12-27 16:32:52 +00001266 gh->msg_type = GSM48_MT_CC_STATUS;
1267
1268 cause = msgb_put(msg, 3);
1269 cause[0] = 2;
1270 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1271 cause[2] = 0x80 | 30; /* response to status inquiry */
1272
1273 call_state = msgb_put(msg, 1);
1274 call_state[0] = 0xc0 | 0x00;
1275
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001276 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001277}
1278
Harald Welte6f4b7532008-12-29 00:39:37 +00001279static int gsm48_tx_simple(struct gsm_lchan *lchan,
1280 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001281{
1282 struct msgb *msg = gsm48_msgb_alloc();
1283 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1284
1285 msg->lchan = lchan;
1286
Harald Welte6f4b7532008-12-29 00:39:37 +00001287 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001288 gh->msg_type = msg_type;
1289
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001290 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001291}
1292
Harald Welte4bfdfe72009-06-10 23:11:52 +08001293static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1294{
Harald Weltedcaf5652009-07-23 18:56:43 +02001295 if (bsc_timer_pending(&trans->cc.timer)) {
1296 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1297 bsc_del_timer(&trans->cc.timer);
1298 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001299 }
1300}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001301
Harald Welte4bfdfe72009-06-10 23:11:52 +08001302static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1303 int msg_type, struct gsm_mncc *mncc)
1304{
1305 struct msgb *msg;
1306
1307 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001308 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001309 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001310 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001311 trans->conn->lchan->ts->trx->bts->nr,
1312 trans->conn->lchan->ts->trx->nr,
1313 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 (trans->subscr)?(trans->subscr->extension):"-",
1315 get_mncc_name(msg_type));
1316 else
1317 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1318 "Sending '%s' to MNCC.\n",
1319 (trans->subscr)?(trans->subscr->extension):"-",
1320 get_mncc_name(msg_type));
1321 else
1322 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1323 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1324
1325 mncc->msg_type = msg_type;
1326
Harald Welte966636f2009-06-26 19:39:35 +02001327 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001328 if (!msg)
1329 return -ENOMEM;
1330 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1331 msgb_enqueue(&net->upqueue, msg);
1332
1333 return 0;
1334}
1335
1336int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1337 u_int32_t callref, int location, int value)
1338{
1339 struct gsm_mncc rel;
1340
Harald Welte92f70c52009-06-12 01:54:08 +08001341 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001342 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001343 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001344 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1345}
1346
Harald Weltedcaf5652009-07-23 18:56:43 +02001347/* Call Control Specific transaction release.
1348 * gets called by trans_free, DO NOT CALL YOURSELF! */
1349void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001350{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001351 gsm48_stop_cc_timer(trans);
1352
1353 /* send release to L4, if callref still exists */
1354 if (trans->callref) {
1355 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001356 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001357 GSM48_CAUSE_LOC_PRN_S_LU,
1358 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001359 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001360 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001361 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001362 if (trans->conn)
1363 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001364}
1365
1366static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001367
Harald Welte09e38af2009-02-16 22:52:23 +00001368/* call-back from paging the B-end of the connection */
1369static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001370 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001371{
Harald Welte7ccf7782009-02-17 01:43:01 +00001372 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001373 struct gsm_subscriber *subscr = param;
1374 struct gsm_trans *transt, *tmp;
1375 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001376
Harald Welte09e38af2009-02-16 22:52:23 +00001377 if (hooknum != GSM_HOOK_RR_PAGING)
1378 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001379
Harald Welte4bfdfe72009-06-10 23:11:52 +08001380 if (!subscr)
1381 return -EINVAL;
1382 net = subscr->net;
1383 if (!net) {
1384 DEBUGP(DCC, "Error Network not set!\n");
1385 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001386 }
Harald Welte7584aea2009-02-11 11:44:12 +00001387
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388 /* check all tranactions (without lchan) for subscriber */
1389 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001390 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001391 continue;
1392 switch (event) {
1393 case GSM_PAGING_SUCCEEDED:
1394 if (!lchan) // paranoid
1395 break;
1396 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1397 subscr->extension);
1398 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001399 if (!transt->conn) {
1400 transt->conn = &lchan->conn;
1401 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001402 }
1403 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001404 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001405 break;
1406 case GSM_PAGING_EXPIRED:
1407 DEBUGP(DCC, "Paging subscr %s expired!\n",
1408 subscr->extension);
1409 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001410 mncc_release_ind(transt->subscr->net, transt,
1411 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001412 GSM48_CAUSE_LOC_PRN_S_LU,
1413 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001414 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001415 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001416 break;
1417 }
1418 }
Harald Welte09e38af2009-02-16 22:52:23 +00001419 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001420}
Harald Welte7584aea2009-02-11 11:44:12 +00001421
Harald Welteda7ab742009-12-19 22:23:05 +01001422static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1423
Harald Welte805f6442009-07-28 18:25:29 +02001424/* some other part of the code sends us a signal */
1425static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1426 void *handler_data, void *signal_data)
1427{
1428 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001429 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001430 struct gsm_network *net;
1431 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001432
1433 if (subsys != SS_ABISIP)
1434 return 0;
1435
1436 /* in case we use direct BTS-to-BTS RTP */
1437 if (ipacc_rtp_direct)
1438 return 0;
1439
Harald Welte805f6442009-07-28 18:25:29 +02001440 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001441 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001442 /* check if any transactions on this lchan still have
1443 * a tch_recv_mncc request pending */
1444 net = lchan->ts->trx->bts->network;
1445 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001446 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001447 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1448 tch_recv_mncc(net, trans->callref, 1);
1449 }
1450 }
Harald Welte805f6442009-07-28 18:25:29 +02001451 break;
Harald Welte805f6442009-07-28 18:25:29 +02001452 }
1453
1454 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001455}
1456
Harald Welte49f48b82009-02-17 15:29:33 +00001457/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001458static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001459{
Harald Welte11fa29c2009-02-19 17:24:39 +00001460 struct gsm_bts *bts = lchan->ts->trx->bts;
1461 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001462 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001463
Harald Welte11fa29c2009-02-19 17:24:39 +00001464 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1465 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1466 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1467
1468 if (bts->type != remote_bts->type) {
1469 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1470 return -EINVAL;
1471 }
Harald Welteda7ab742009-12-19 22:23:05 +01001472
1473 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001474 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001475 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001476 if (!ipacc_rtp_direct) {
1477 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001478 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001479 if (rc < 0)
1480 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001481 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001482#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001483
1484 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301485 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1486 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001487 } else {
1488 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301489 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1490 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301491 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001492 if (rc < 0)
1493 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301494 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1495 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301496 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001497 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001498 break;
1499 case GSM_BTS_TYPE_BS11:
1500 trau_mux_map_lchan(lchan, remote_lchan);
1501 break;
1502 default:
1503 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001504 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001505 }
Harald Welte49f48b82009-02-17 15:29:33 +00001506
1507 return 0;
1508}
1509
Harald Welte4bfdfe72009-06-10 23:11:52 +08001510/* bridge channels of two transactions */
1511static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001512{
Harald Weltedcaf5652009-07-23 18:56:43 +02001513 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1514 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001515
Harald Welte4bfdfe72009-06-10 23:11:52 +08001516 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001517 return -EIO;
1518
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001519 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001520 return -EIO;
1521
1522 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001523 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001524}
1525
Harald Welteda7ab742009-12-19 22:23:05 +01001526/* enable receive of channels to MNCC upqueue */
1527static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001528{
1529 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001530 struct gsm_lchan *lchan;
1531 struct gsm_bts *bts;
1532 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001533
Harald Welte4bfdfe72009-06-10 23:11:52 +08001534 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001535 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001536 if (!trans)
1537 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001538 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001539 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001540 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001541 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001542
Harald Welteda7ab742009-12-19 22:23:05 +01001543 switch (bts->type) {
1544 case GSM_BTS_TYPE_NANOBTS:
1545 if (ipacc_rtp_direct) {
1546 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1547 return -EINVAL;
1548 }
1549 /* in case, we don't have a RTP socket yet, we note this
1550 * in the transaction and try later */
1551 if (!lchan->abis_ip.rtp_socket) {
1552 trans->tch_recv = enable;
1553 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1554 return 0;
1555 }
1556 if (enable) {
1557 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001558 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001559 if (rc < 0)
1560 return rc;
1561 /* assign socket to application interface */
1562 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1563 net, callref);
1564 } else
1565 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1566 net, 0);
1567 break;
1568 case GSM_BTS_TYPE_BS11:
1569 if (enable)
1570 return trau_recv_lchan(lchan, callref);
1571 return trau_mux_unmap(NULL, callref);
1572 break;
1573 default:
1574 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1575 return -EINVAL;
1576 }
1577
1578 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001579}
1580
Harald Welte4bfdfe72009-06-10 23:11:52 +08001581static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1582{
1583 DEBUGP(DCC, "-> STATUS ENQ\n");
1584 return gsm48_cc_tx_status(trans, msg);
1585}
1586
1587static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1588static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1589
1590static void gsm48_cc_timeout(void *arg)
1591{
1592 struct gsm_trans *trans = arg;
1593 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001594 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1595 int mo_location = GSM48_CAUSE_LOC_USER;
1596 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1597 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001598 struct gsm_mncc mo_rel, l4_rel;
1599
1600 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1601 mo_rel.callref = trans->callref;
1602 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1603 l4_rel.callref = trans->callref;
1604
Harald Weltedcaf5652009-07-23 18:56:43 +02001605 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001606 case 0x303:
1607 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001608 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001609 break;
1610 case 0x310:
1611 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001612 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001613 break;
1614 case 0x313:
1615 disconnect = 1;
1616 /* unknown, did not find it in the specs */
1617 break;
1618 case 0x301:
1619 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001620 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621 break;
1622 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001623 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001624 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001625 gsm48_cc_tx_release(trans, &trans->cc.msg);
1626 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001627 break; /* stay in release state */
1628 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001629 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001630 return;
1631// release = 1;
1632// l4_cause = 14;
1633// break;
1634 case 0x306:
1635 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001636 mo_cause = trans->cc.msg.cause.value;
1637 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001638 break;
1639 case 0x323:
1640 disconnect = 1;
1641 break;
1642 default:
1643 release = 1;
1644 }
1645
1646 if (release && trans->callref) {
1647 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001648 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001649 l4_location, l4_cause);
1650 trans->callref = 0;
1651 }
1652
1653 if (disconnect && trans->callref) {
1654 /* process disconnect towards layer 4 */
1655 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001656 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001657 }
1658
1659 /* process disconnect towards mobile station */
1660 if (disconnect || release) {
1661 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001662 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1663 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1664 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001665 mo_rel.cause.diag_len = 3;
1666
1667 if (disconnect)
1668 gsm48_cc_tx_disconnect(trans, &mo_rel);
1669 if (release)
1670 gsm48_cc_tx_release(trans, &mo_rel);
1671 }
1672
1673}
1674
1675static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1676 int sec, int micro)
1677{
1678 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001679 trans->cc.timer.cb = gsm48_cc_timeout;
1680 trans->cc.timer.data = trans;
1681 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1682 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001683}
1684
1685static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1686{
1687 struct gsm48_hdr *gh = msgb_l3(msg);
1688 u_int8_t msg_type = gh->msg_type & 0xbf;
1689 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1690 struct tlv_parsed tp;
1691 struct gsm_mncc setup;
1692
1693 memset(&setup, 0, sizeof(struct gsm_mncc));
1694 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001695 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696 /* emergency setup is identified by msg_type */
1697 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1698 setup.emergency = 1;
1699
1700 /* use subscriber as calling party number */
1701 if (trans->subscr) {
1702 setup.fields |= MNCC_F_CALLING;
1703 strncpy(setup.calling.number, trans->subscr->extension,
1704 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001705 strncpy(setup.imsi, trans->subscr->imsi,
1706 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001707 }
1708 /* bearer capability */
1709 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1710 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001711 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001712 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1713 }
1714 /* facility */
1715 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1716 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001717 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001718 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1719 }
1720 /* called party bcd number */
1721 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1722 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001723 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001724 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1725 }
1726 /* user-user */
1727 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1728 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001729 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001730 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1731 }
1732 /* ss-version */
1733 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1734 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001735 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001736 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1737 }
1738 /* CLIR suppression */
1739 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1740 setup.clir.sup = 1;
1741 /* CLIR invocation */
1742 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1743 setup.clir.inv = 1;
1744 /* cc cap */
1745 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1746 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001747 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001748 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1749 }
1750
Harald Welte4bfdfe72009-06-10 23:11:52 +08001751 new_cc_state(trans, GSM_CSTATE_INITIATED);
1752
Harald Welte (local)47df3992009-12-26 19:45:03 +01001753 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1754 subscr_name(trans->subscr), trans->subscr->extension,
1755 setup.called.number);
1756
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001758 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001759
Harald Welte13cac662009-07-29 12:10:35 +02001760 /* MNCC code will modify the channel asynchronously, we should
1761 * ipaccess-bind only after the modification has been made to the
1762 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 return 0;
1764}
1765
1766static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001767{
1768 struct msgb *msg = gsm48_msgb_alloc();
1769 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001770 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001771 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001772
Harald Welte7ccf7782009-02-17 01:43:01 +00001773 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001774
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 /* transaction id must not be assigned */
1776 if (trans->transaction_id != 0xff) { /* unasssigned */
1777 DEBUGP(DCC, "TX Setup with assigned transaction. "
1778 "This is not allowed!\n");
1779 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001780 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001781 GSM48_CAUSE_LOC_PRN_S_LU,
1782 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001784 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001785 return rc;
1786 }
1787
1788 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001789 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1790 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001791 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001792 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001793 GSM48_CAUSE_LOC_PRN_S_LU,
1794 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001796 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001797 return rc;
1798 }
Harald Welte78283ef2009-07-23 21:36:44 +02001799 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001800
Harald Welte65e74cc2008-12-29 01:55:35 +00001801 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001802
Harald Welte4bfdfe72009-06-10 23:11:52 +08001803 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001804
Harald Welte4bfdfe72009-06-10 23:11:52 +08001805 /* bearer capability */
1806 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001807 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001808 /* facility */
1809 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001810 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001811 /* progress */
1812 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001813 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001814 /* calling party BCD number */
1815 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001816 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001817 /* called party BCD number */
1818 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001819 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820 /* user-user */
1821 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001822 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001823 /* redirecting party BCD number */
1824 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001825 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001826 /* signal */
1827 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001828 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001829
1830 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001831
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001832 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001833}
1834
Harald Welte4bfdfe72009-06-10 23:11:52 +08001835static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1836{
1837 struct gsm48_hdr *gh = msgb_l3(msg);
1838 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1839 struct tlv_parsed tp;
1840 struct gsm_mncc call_conf;
1841
1842 gsm48_stop_cc_timer(trans);
1843 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1844
1845 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1846 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001847 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001848#if 0
1849 /* repeat */
1850 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1851 call_conf.repeat = 1;
1852 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1853 call_conf.repeat = 2;
1854#endif
1855 /* bearer capability */
1856 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1857 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001858 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001859 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1860 }
1861 /* cause */
1862 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1863 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001864 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1866 }
1867 /* cc cap */
1868 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1869 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001870 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1872 }
1873
1874 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1875
Harald Welte596fed42009-07-23 19:06:52 +02001876 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1877 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001878}
1879
1880static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1881{
1882 struct gsm_mncc *proceeding = arg;
1883 struct msgb *msg = gsm48_msgb_alloc();
1884 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1885
Harald Welte4bfdfe72009-06-10 23:11:52 +08001886 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1887
1888 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1889
1890 /* bearer capability */
1891 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001892 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001893 /* facility */
1894 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001895 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001896 /* progress */
1897 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001900 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001901}
1902
1903static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1904{
1905 struct gsm48_hdr *gh = msgb_l3(msg);
1906 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1907 struct tlv_parsed tp;
1908 struct gsm_mncc alerting;
1909
1910 gsm48_stop_cc_timer(trans);
1911 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1912
1913 memset(&alerting, 0, sizeof(struct gsm_mncc));
1914 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001915 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001916 /* facility */
1917 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1918 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001919 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1921 }
1922
1923 /* progress */
1924 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1925 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001926 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001927 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1928 }
1929 /* ss-version */
1930 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1931 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001932 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001933 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1934 }
1935
1936 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1937
Harald Welte596fed42009-07-23 19:06:52 +02001938 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1939 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001940}
1941
1942static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1943{
1944 struct gsm_mncc *alerting = arg;
1945 struct msgb *msg = gsm48_msgb_alloc();
1946 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1947
Harald Welte4bfdfe72009-06-10 23:11:52 +08001948 gh->msg_type = GSM48_MT_CC_ALERTING;
1949
1950 /* facility */
1951 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001952 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001953 /* progress */
1954 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001955 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001956 /* user-user */
1957 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001958 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001959
1960 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1961
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001962 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963}
1964
1965static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1966{
1967 struct gsm_mncc *progress = arg;
1968 struct msgb *msg = gsm48_msgb_alloc();
1969 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1970
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 gh->msg_type = GSM48_MT_CC_PROGRESS;
1972
1973 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001974 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 /* user-user */
1976 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001977 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001978
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001979 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001980}
1981
1982static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1983{
1984 struct gsm_mncc *connect = arg;
1985 struct msgb *msg = gsm48_msgb_alloc();
1986 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1987
Harald Welte4bfdfe72009-06-10 23:11:52 +08001988 gh->msg_type = GSM48_MT_CC_CONNECT;
1989
1990 gsm48_stop_cc_timer(trans);
1991 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1992
1993 /* facility */
1994 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001995 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001996 /* progress */
1997 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001998 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001999 /* connected number */
2000 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002001 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 /* user-user */
2003 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002004 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002005
2006 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2007
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002008 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009}
2010
2011static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2012{
2013 struct gsm48_hdr *gh = msgb_l3(msg);
2014 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2015 struct tlv_parsed tp;
2016 struct gsm_mncc connect;
2017
2018 gsm48_stop_cc_timer(trans);
2019
2020 memset(&connect, 0, sizeof(struct gsm_mncc));
2021 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002022 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002023 /* use subscriber as connected party number */
2024 if (trans->subscr) {
2025 connect.fields |= MNCC_F_CONNECTED;
2026 strncpy(connect.connected.number, trans->subscr->extension,
2027 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002028 strncpy(connect.imsi, trans->subscr->imsi,
2029 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002030 }
2031 /* facility */
2032 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2033 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002034 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002035 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2036 }
2037 /* user-user */
2038 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2039 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002040 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002041 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2042 }
2043 /* ss-version */
2044 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2045 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002046 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2048 }
2049
2050 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2051
Harald Welte596fed42009-07-23 19:06:52 +02002052 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002053}
2054
2055
2056static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2057{
2058 struct gsm_mncc connect_ack;
2059
2060 gsm48_stop_cc_timer(trans);
2061
2062 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2063
2064 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2065 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002066 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002067 &connect_ack);
2068}
2069
2070static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2071{
2072 struct msgb *msg = gsm48_msgb_alloc();
2073 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2074
Harald Welte4bfdfe72009-06-10 23:11:52 +08002075 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2076
2077 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2078
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002079 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002080}
2081
2082static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2083{
2084 struct gsm48_hdr *gh = msgb_l3(msg);
2085 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2086 struct tlv_parsed tp;
2087 struct gsm_mncc disc;
2088
2089 gsm48_stop_cc_timer(trans);
2090
2091 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2092
2093 memset(&disc, 0, sizeof(struct gsm_mncc));
2094 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002095 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002096 /* cause */
2097 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2098 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002099 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2101 }
2102 /* facility */
2103 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2104 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002105 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2107 }
2108 /* user-user */
2109 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2110 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002111 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002112 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2113 }
2114 /* ss-version */
2115 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2116 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002117 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002118 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2119 }
2120
Harald Welte596fed42009-07-23 19:06:52 +02002121 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122
2123}
2124
Harald Weltec66b71c2009-06-11 14:23:20 +08002125static struct gsm_mncc_cause default_cause = {
2126 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2127 .coding = 0,
2128 .rec = 0,
2129 .rec_val = 0,
2130 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2131 .diag_len = 0,
2132 .diag = { 0 },
2133};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002134
2135static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2136{
2137 struct gsm_mncc *disc = arg;
2138 struct msgb *msg = gsm48_msgb_alloc();
2139 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2140
Harald Welte4bfdfe72009-06-10 23:11:52 +08002141 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2142
2143 gsm48_stop_cc_timer(trans);
2144 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2145
2146 /* cause */
2147 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002148 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002149 else
Harald Welte55c8f352010-03-07 23:40:35 +01002150 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002151
2152 /* facility */
2153 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002154 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 /* progress */
2156 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002157 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002158 /* user-user */
2159 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002160 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002161
2162 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002163 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002164
2165 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2166
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002167 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168}
2169
2170static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2171{
2172 struct gsm48_hdr *gh = msgb_l3(msg);
2173 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2174 struct tlv_parsed tp;
2175 struct gsm_mncc rel;
2176 int rc;
2177
2178 gsm48_stop_cc_timer(trans);
2179
2180 memset(&rel, 0, sizeof(struct gsm_mncc));
2181 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002182 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 /* cause */
2184 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2185 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002186 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2188 }
2189 /* facility */
2190 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2191 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002192 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002193 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2194 }
2195 /* user-user */
2196 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2197 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002198 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002199 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2200 }
2201 /* ss-version */
2202 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2203 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002204 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002205 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2206 }
2207
Harald Weltedcaf5652009-07-23 18:56:43 +02002208 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002210 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002211 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002212 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002213 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002214 GSM48_MT_CC_RELEASE_COMPL);
2215 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002216 }
2217
2218 new_cc_state(trans, GSM_CSTATE_NULL);
2219
2220 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002221 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002222
2223 return rc;
2224}
2225
2226static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2227{
2228 struct gsm_mncc *rel = arg;
2229 struct msgb *msg = gsm48_msgb_alloc();
2230 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2231
Harald Welte4bfdfe72009-06-10 23:11:52 +08002232 gh->msg_type = GSM48_MT_CC_RELEASE;
2233
2234 trans->callref = 0;
2235
2236 gsm48_stop_cc_timer(trans);
2237 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2238
2239 /* cause */
2240 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002241 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002242 /* facility */
2243 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002244 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002245 /* user-user */
2246 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002247 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002248
Harald Weltedcaf5652009-07-23 18:56:43 +02002249 trans->cc.T308_second = 0;
2250 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002251
Harald Weltedcaf5652009-07-23 18:56:43 +02002252 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2254
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002255 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002256}
2257
2258static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2259{
2260 struct gsm48_hdr *gh = msgb_l3(msg);
2261 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2262 struct tlv_parsed tp;
2263 struct gsm_mncc rel;
2264 int rc = 0;
2265
2266 gsm48_stop_cc_timer(trans);
2267
2268 memset(&rel, 0, sizeof(struct gsm_mncc));
2269 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002270 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002271 /* cause */
2272 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2273 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002274 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2276 }
2277 /* facility */
2278 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2279 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002280 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002281 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2282 }
2283 /* user-user */
2284 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2285 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002286 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002287 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2288 }
2289 /* ss-version */
2290 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2291 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002292 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002293 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2294 }
2295
2296 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002297 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002298 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002299 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002300 MNCC_REJ_IND, &rel);
2301 break;
2302 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002303 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002305 /* FIXME: in case of multiple calls, we can't simply
2306 * hang up here ! */
2307 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 break;
2309 default:
Harald Welte596fed42009-07-23 19:06:52 +02002310 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311 MNCC_REL_IND, &rel);
2312 }
2313 }
2314
2315 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002316 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002317
2318 return rc;
2319}
2320
2321static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2322{
2323 struct gsm_mncc *rel = arg;
2324 struct msgb *msg = gsm48_msgb_alloc();
2325 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2326
Harald Welte4bfdfe72009-06-10 23:11:52 +08002327 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2328
2329 trans->callref = 0;
2330
2331 gsm48_stop_cc_timer(trans);
2332
2333 /* cause */
2334 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002335 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002336 /* facility */
2337 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002338 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002339 /* user-user */
2340 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002341 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002342
Harald Weltedcaf5652009-07-23 18:56:43 +02002343 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002345 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346}
2347
2348static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2349{
2350 struct gsm48_hdr *gh = msgb_l3(msg);
2351 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2352 struct tlv_parsed tp;
2353 struct gsm_mncc fac;
2354
2355 memset(&fac, 0, sizeof(struct gsm_mncc));
2356 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002357 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358 /* facility */
2359 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2360 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002361 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2363 }
2364 /* ss-version */
2365 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2366 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002367 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002368 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2369 }
2370
Harald Welte596fed42009-07-23 19:06:52 +02002371 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372}
2373
2374static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2375{
2376 struct gsm_mncc *fac = arg;
2377 struct msgb *msg = gsm48_msgb_alloc();
2378 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2379
Harald Welte4bfdfe72009-06-10 23:11:52 +08002380 gh->msg_type = GSM48_MT_CC_FACILITY;
2381
2382 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002383 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002385 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002386}
2387
2388static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2389{
2390 struct gsm_mncc hold;
2391
2392 memset(&hold, 0, sizeof(struct gsm_mncc));
2393 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002394 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002395}
2396
2397static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2398{
2399 struct msgb *msg = gsm48_msgb_alloc();
2400 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2401
Harald Welte4bfdfe72009-06-10 23:11:52 +08002402 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2403
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002404 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002405}
2406
2407static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2408{
2409 struct gsm_mncc *hold_rej = arg;
2410 struct msgb *msg = gsm48_msgb_alloc();
2411 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2412
Harald Welte4bfdfe72009-06-10 23:11:52 +08002413 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2414
2415 /* cause */
2416 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002417 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002418 else
Harald Welte55c8f352010-03-07 23:40:35 +01002419 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002420
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002421 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422}
2423
2424static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2425{
2426 struct gsm_mncc retrieve;
2427
2428 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2429 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002430 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2431 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002432}
2433
2434static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2435{
2436 struct msgb *msg = gsm48_msgb_alloc();
2437 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2438
Harald Welte4bfdfe72009-06-10 23:11:52 +08002439 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2440
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002441 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002442}
2443
2444static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2445{
2446 struct gsm_mncc *retrieve_rej = arg;
2447 struct msgb *msg = gsm48_msgb_alloc();
2448 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2449
Harald Welte4bfdfe72009-06-10 23:11:52 +08002450 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2451
2452 /* cause */
2453 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002454 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002455 else
Harald Welte55c8f352010-03-07 23:40:35 +01002456 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002457
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002458 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459}
2460
2461static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2462{
2463 struct gsm48_hdr *gh = msgb_l3(msg);
2464 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2465 struct tlv_parsed tp;
2466 struct gsm_mncc dtmf;
2467
2468 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2469 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002470 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002471 /* keypad facility */
2472 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2473 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002474 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2476 }
2477
Harald Welte596fed42009-07-23 19:06:52 +02002478 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479}
2480
2481static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2482{
2483 struct gsm_mncc *dtmf = arg;
2484 struct msgb *msg = gsm48_msgb_alloc();
2485 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2486
Harald Welte4bfdfe72009-06-10 23:11:52 +08002487 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2488
2489 /* keypad */
2490 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002491 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002492
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002493 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002494}
2495
2496static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2497{
2498 struct gsm_mncc *dtmf = arg;
2499 struct msgb *msg = gsm48_msgb_alloc();
2500 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2501
Harald Welte4bfdfe72009-06-10 23:11:52 +08002502 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2503
2504 /* cause */
2505 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002506 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002507 else
Harald Welte55c8f352010-03-07 23:40:35 +01002508 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002509
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002510 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511}
2512
2513static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2514{
2515 struct msgb *msg = gsm48_msgb_alloc();
2516 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2517
Harald Welte4bfdfe72009-06-10 23:11:52 +08002518 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2519
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002520 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002521}
2522
2523static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2524{
2525 struct gsm_mncc dtmf;
2526
2527 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2528 dtmf.callref = trans->callref;
2529
Harald Welte596fed42009-07-23 19:06:52 +02002530 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002531}
2532
2533static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2534{
2535 struct gsm48_hdr *gh = msgb_l3(msg);
2536 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2537 struct tlv_parsed tp;
2538 struct gsm_mncc modify;
2539
2540 memset(&modify, 0, sizeof(struct gsm_mncc));
2541 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002542 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543 /* bearer capability */
2544 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2545 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002546 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2548 }
2549
2550 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2551
Harald Welte596fed42009-07-23 19:06:52 +02002552 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002553}
2554
2555static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2556{
2557 struct gsm_mncc *modify = arg;
2558 struct msgb *msg = gsm48_msgb_alloc();
2559 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2560
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 gh->msg_type = GSM48_MT_CC_MODIFY;
2562
2563 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2564
2565 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002566 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567
2568 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2569
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002570 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571}
2572
2573static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2574{
2575 struct gsm48_hdr *gh = msgb_l3(msg);
2576 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2577 struct tlv_parsed tp;
2578 struct gsm_mncc modify;
2579
2580 gsm48_stop_cc_timer(trans);
2581
2582 memset(&modify, 0, sizeof(struct gsm_mncc));
2583 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002584 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002585 /* bearer capability */
2586 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2587 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002588 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2590 }
2591
2592 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2593
Harald Welte596fed42009-07-23 19:06:52 +02002594 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002595}
2596
2597static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2598{
2599 struct gsm_mncc *modify = arg;
2600 struct msgb *msg = gsm48_msgb_alloc();
2601 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2602
Harald Welte4bfdfe72009-06-10 23:11:52 +08002603 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2604
2605 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002606 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607
2608 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2609
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002610 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611}
2612
2613static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2614{
2615 struct gsm48_hdr *gh = msgb_l3(msg);
2616 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2617 struct tlv_parsed tp;
2618 struct gsm_mncc modify;
2619
2620 gsm48_stop_cc_timer(trans);
2621
2622 memset(&modify, 0, sizeof(struct gsm_mncc));
2623 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002624 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 /* bearer capability */
2626 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2627 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002628 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2630 }
2631 /* cause */
2632 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2633 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002634 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002635 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2636 }
2637
2638 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2639
Harald Welte596fed42009-07-23 19:06:52 +02002640 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002641}
2642
2643static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2644{
2645 struct gsm_mncc *modify = 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_MODIFY_REJECT;
2650
2651 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002652 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002654 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002655
2656 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2657
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002658 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659}
2660
2661static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2662{
2663 struct gsm_mncc *notify = arg;
2664 struct msgb *msg = gsm48_msgb_alloc();
2665 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2666
Harald Welte4bfdfe72009-06-10 23:11:52 +08002667 gh->msg_type = GSM48_MT_CC_NOTIFY;
2668
2669 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002670 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002672 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002673}
2674
2675static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2676{
2677 struct gsm48_hdr *gh = msgb_l3(msg);
2678 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2679// struct tlv_parsed tp;
2680 struct gsm_mncc notify;
2681
2682 memset(&notify, 0, sizeof(struct gsm_mncc));
2683 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002684// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002685 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002686 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002687
Harald Welte596fed42009-07-23 19:06:52 +02002688 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689}
2690
2691static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2692{
2693 struct gsm_mncc *user = arg;
2694 struct msgb *msg = gsm48_msgb_alloc();
2695 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2696
Harald Welte4bfdfe72009-06-10 23:11:52 +08002697 gh->msg_type = GSM48_MT_CC_USER_INFO;
2698
2699 /* user-user */
2700 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002701 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702 /* more data */
2703 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002704 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002706 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002707}
2708
2709static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2710{
2711 struct gsm48_hdr *gh = msgb_l3(msg);
2712 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2713 struct tlv_parsed tp;
2714 struct gsm_mncc user;
2715
2716 memset(&user, 0, sizeof(struct gsm_mncc));
2717 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002718 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002719 /* user-user */
2720 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2721 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002722 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2724 }
2725 /* more data */
2726 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2727 user.more = 1;
2728
Harald Welte596fed42009-07-23 19:06:52 +02002729 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002730}
2731
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002732static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002733{
2734 struct gsm_mncc *mode = arg;
2735
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002736 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737}
2738
2739static struct downstate {
2740 u_int32_t states;
2741 int type;
2742 int (*rout) (struct gsm_trans *trans, void *arg);
2743} downstatelist[] = {
2744 /* mobile originating call establishment */
2745 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2746 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2747 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2748 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2749 {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 */
2750 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2751 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2752 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2753 /* mobile terminating call establishment */
2754 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2755 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2756 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2757 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2758 /* signalling during call */
2759 {SBIT(GSM_CSTATE_ACTIVE),
2760 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2761 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2762 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2763 {ALL_STATES,
2764 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2765 {ALL_STATES,
2766 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2767 {ALL_STATES,
2768 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2769 {SBIT(GSM_CSTATE_ACTIVE),
2770 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2771 {SBIT(GSM_CSTATE_ACTIVE),
2772 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2773 {SBIT(GSM_CSTATE_ACTIVE),
2774 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2775 {SBIT(GSM_CSTATE_ACTIVE),
2776 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2777 {SBIT(GSM_CSTATE_ACTIVE),
2778 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2779 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2780 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2781 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2782 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2783 {SBIT(GSM_CSTATE_ACTIVE),
2784 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2785 /* clearing */
2786 {SBIT(GSM_CSTATE_INITIATED),
2787 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2788 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2789 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2790 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2791 MNCC_REL_REQ, gsm48_cc_tx_release},
2792 /* special */
2793 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002794 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002795};
2796
2797#define DOWNSLLEN \
2798 (sizeof(downstatelist) / sizeof(struct downstate))
2799
2800
2801int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2802{
Harald Welte1a6f7982009-08-09 18:52:33 +02002803 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002804 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002805 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002806 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002807 struct gsm_mncc *data = arg, rel;
2808
2809 /* handle special messages */
2810 switch(msg_type) {
2811 case MNCC_BRIDGE:
2812 return tch_bridge(net, arg);
2813 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002814 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002815 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002816 return tch_recv_mncc(net, data->callref, 1);
2817 case GSM_TCHF_FRAME:
2818 /* Find callref */
2819 trans = trans_find_by_callref(net, data->callref);
2820 if (!trans)
2821 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002822 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002823 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002824 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002825 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002826 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002827 switch (bts->type) {
2828 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002829 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002830 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002831 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002832 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002833 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002834 default:
2835 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2836 }
2837 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002838 }
2839
2840 memset(&rel, 0, sizeof(struct gsm_mncc));
2841 rel.callref = data->callref;
2842
2843 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002844 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002845
2846 /* Callref unknown */
2847 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002848 struct gsm_subscriber *subscr;
2849
Harald Welte4a3464c2009-07-04 10:11:24 +02002850 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002851 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2852 "Received '%s' from MNCC with "
2853 "unknown callref %d\n", data->called.number,
2854 get_mncc_name(msg_type), data->callref);
2855 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002856 return mncc_release_ind(net, NULL, data->callref,
2857 GSM48_CAUSE_LOC_PRN_S_LU,
2858 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002859 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002860 if (!data->called.number[0] && !data->imsi[0]) {
2861 DEBUGP(DCC, "(bts - trx - ts - ti) "
2862 "Received '%s' from MNCC with "
2863 "no number or IMSI\n", get_mncc_name(msg_type));
2864 /* Invalid number */
2865 return mncc_release_ind(net, NULL, data->callref,
2866 GSM48_CAUSE_LOC_PRN_S_LU,
2867 GSM48_CC_CAUSE_INV_NR_FORMAT);
2868 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002870 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002871 subscr = subscr_get_by_extension(net,
2872 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002873 else
Harald Welte9176bd42009-07-23 18:46:00 +02002874 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002875 /* If subscriber is not found */
2876 if (!subscr) {
2877 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2878 "Received '%s' from MNCC with "
2879 "unknown subscriber %s\n", data->called.number,
2880 get_mncc_name(msg_type), data->called.number);
2881 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002882 return mncc_release_ind(net, NULL, data->callref,
2883 GSM48_CAUSE_LOC_PRN_S_LU,
2884 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002885 }
2886 /* If subscriber is not "attached" */
2887 if (!subscr->lac) {
2888 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2889 "Received '%s' from MNCC with "
2890 "detached subscriber %s\n", data->called.number,
2891 get_mncc_name(msg_type), data->called.number);
2892 subscr_put(subscr);
2893 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002894 return mncc_release_ind(net, NULL, data->callref,
2895 GSM48_CAUSE_LOC_PRN_S_LU,
2896 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002897 }
2898 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002899 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2900 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 DEBUGP(DCC, "No memory for trans.\n");
2902 subscr_put(subscr);
2903 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002904 mncc_release_ind(net, NULL, data->callref,
2905 GSM48_CAUSE_LOC_PRN_S_LU,
2906 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002907 return -ENOMEM;
2908 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002909 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002910 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002911
Harald Welte4bfdfe72009-06-10 23:11:52 +08002912 /* If subscriber has no lchan */
2913 if (!lchan) {
2914 /* find transaction with this subscriber already paging */
2915 llist_for_each_entry(transt, &net->trans_list, entry) {
2916 /* Transaction of our lchan? */
2917 if (transt == trans ||
2918 transt->subscr != subscr)
2919 continue;
2920 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2921 "Received '%s' from MNCC with "
2922 "unallocated channel, paging already "
2923 "started.\n", bts->nr,
2924 data->called.number,
2925 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002926 subscr_put(subscr);
2927 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002928 return 0;
2929 }
2930 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002931 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002932 /* Trigger paging */
2933 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2934 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002935 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002936 return 0;
2937 }
2938 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002939 trans->conn = &lchan->conn;
2940 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002941 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002942 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002943
2944 if (trans->conn)
2945 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002946
2947 /* if paging did not respond yet */
2948 if (!lchan) {
2949 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002950 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002951 (trans->subscr)?(trans->subscr->extension):"-",
2952 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002953 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2954 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 if (msg_type == MNCC_REL_REQ)
2956 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2957 else
2958 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2959 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002960 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002961 return rc;
2962 }
2963
2964 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2965 "Received '%s' from MNCC in state %d (%s)\n",
2966 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2967 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002968 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002969 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002970 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002971
2972 /* Find function for current state and message */
2973 for (i = 0; i < DOWNSLLEN; i++)
2974 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002975 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002976 break;
2977 if (i == DOWNSLLEN) {
2978 DEBUGP(DCC, "Message unhandled at this state.\n");
2979 return 0;
2980 }
2981
2982 rc = downstatelist[i].rout(trans, arg);
2983
2984 return rc;
2985}
2986
2987
2988static struct datastate {
2989 u_int32_t states;
2990 int type;
2991 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2992} datastatelist[] = {
2993 /* mobile originating call establishment */
2994 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2995 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2996 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2997 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2998 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2999 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3000 /* mobile terminating call establishment */
3001 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3002 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3003 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3004 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003005 {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 +08003006 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3007 /* signalling during call */
3008 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3009 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3010 {SBIT(GSM_CSTATE_ACTIVE),
3011 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3012 {ALL_STATES,
3013 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3014 {ALL_STATES,
3015 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3016 {ALL_STATES,
3017 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3018 {SBIT(GSM_CSTATE_ACTIVE),
3019 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3020 {SBIT(GSM_CSTATE_ACTIVE),
3021 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3022 {SBIT(GSM_CSTATE_ACTIVE),
3023 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3024 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3025 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3026 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3027 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3028 {SBIT(GSM_CSTATE_ACTIVE),
3029 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3030 /* clearing */
3031 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3032 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3033 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3034 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3035 {ALL_STATES, /* 5.4.3.4 */
3036 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3037};
3038
3039#define DATASLLEN \
3040 (sizeof(datastatelist) / sizeof(struct datastate))
3041
Harald Welte4bc90a12008-12-27 16:32:52 +00003042static int gsm0408_rcv_cc(struct msgb *msg)
3043{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003044 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00003045 struct gsm48_hdr *gh = msgb_l3(msg);
3046 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003047 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003048 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003049 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003050 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003051
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052 if (msg_type & 0x80) {
3053 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3054 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003055 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003056
3057 conn = &lchan->conn;
3058
Harald Welte4bfdfe72009-06-10 23:11:52 +08003059 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003060 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003061
Harald Welte6f5aee02009-07-23 21:21:14 +02003062 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 "Received '%s' from MS in state %d (%s)\n",
3064 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003065 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003066 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3067 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003068
3069 /* Create transaction */
3070 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003071 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003072 "creating new trans.\n", transaction_id);
3073 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003074 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003075 transaction_id, new_callref++);
3076 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003077 DEBUGP(DCC, "No memory for trans.\n");
3078 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003079 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003080 GSM48_MT_CC_RELEASE_COMPL);
3081 return -ENOMEM;
3082 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003083 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003084 trans->conn = &lchan->conn;
3085 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003086 }
3087
3088 /* find function for current state and message */
3089 for (i = 0; i < DATASLLEN; i++)
3090 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003091 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003092 break;
3093 if (i == DATASLLEN) {
3094 DEBUGP(DCC, "Message unhandled at this state.\n");
3095 return 0;
3096 }
3097
3098 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003099
3100 return rc;
3101}
3102
Harald Welte52b1f982008-12-23 20:25:15 +00003103/* here we pass in a msgb from the RSL->RLL. We expect the l3 pointer to be set */
Harald Welte (local)daef6062009-08-14 11:41:12 +02003104int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003105{
3106 struct gsm48_hdr *gh = msgb_l3(msg);
3107 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003108 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003109
3110 if (silent_call_reroute(msg))
3111 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003112
3113 switch (pdisc) {
3114 case GSM48_PDISC_CC:
3115 rc = gsm0408_rcv_cc(msg);
3116 break;
3117 case GSM48_PDISC_MM:
3118 rc = gsm0408_rcv_mm(msg);
3119 break;
3120 case GSM48_PDISC_RR:
3121 rc = gsm0408_rcv_rr(msg);
3122 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003123 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003124 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003125 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003126 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003127 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003128 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3129 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003130 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003131 case GSM48_PDISC_NC_SS:
3132 rc = handle_rcv_ussd(msg);
3133 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003134 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003135 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3136 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003137 break;
3138 }
3139
3140 return rc;
3141}
Harald Welte8470bf22008-12-25 23:28:35 +00003142
Harald Welte805f6442009-07-28 18:25:29 +02003143/*
3144 * This will be ran by the linker when loading the DSO. We use it to
3145 * do system initialization, e.g. registration of signal handlers.
3146 */
3147static __attribute__((constructor)) void on_dso_load_0408(void)
3148{
Harald Welte805f6442009-07-28 18:25:29 +02003149 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3150}