blob: 5774995f109223aa6fa1d168015d06ec89262bf6 [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 */
258 rc = gsm48_tx_mm_info(lchan);
259 }
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 */
Harald Welte8470bf22008-12-25 23:28:35 +0000309int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000310{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100311 struct gsm_subscriber_connection *conn;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100312 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000313 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000314 struct gsm48_hdr *gh;
315
Harald Welte8470bf22008-12-25 23:28:35 +0000316 msg->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100317 conn = &lchan->conn;
Harald Welte52b1f982008-12-23 20:25:15 +0000318
319 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
320 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000321 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000322 gh->data[0] = cause;
323
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100324 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100325 "LAC=%u BTS=%u\n", conn->subscr ?
326 subscr_name(conn->subscr) : "unknown",
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100327 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100328
Harald Welteffa55a42009-12-22 19:07:32 +0100329 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000330
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800331 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000332}
333
334/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800335int gsm0408_loc_upd_acc(struct gsm_subscriber_connection *conn, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000336{
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800337 struct gsm_bts *bts = conn->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000338 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000339 struct gsm48_hdr *gh;
340 struct gsm48_loc_area_id *lai;
341 u_int8_t *mid;
342
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800343 msg->lchan = conn->lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000344
345 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
346 gh->proto_discr = GSM48_PDISC_MM;
347 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
348
349 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100350 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000351 bts->network->network_code, bts->location_area_code);
352
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200353 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200354 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000355
356 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
357
Harald Welteffa55a42009-12-22 19:07:32 +0100358 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100359
Holger Hans Peter Freytherdc5db242010-06-15 14:07:27 +0800360 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000361}
362
Harald Weltebf5e8df2009-02-03 12:59:45 +0000363/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000364static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
365{
366 struct msgb *msg = gsm48_msgb_alloc();
367 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000368
Harald Welte231ad4f2008-12-27 11:15:38 +0000369 msg->lchan = lchan;
370
371 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
372 gh->proto_discr = GSM48_PDISC_MM;
373 gh->msg_type = GSM48_MT_MM_ID_REQ;
374 gh->data[0] = id_type;
375
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800376 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000377}
378
Harald Welte231ad4f2008-12-27 11:15:38 +0000379
Harald Weltebf5e8df2009-02-03 12:59:45 +0000380/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000381static int mm_rx_id_resp(struct msgb *msg)
382{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100383 struct gsm_subscriber_connection *conn;
Harald Welte231ad4f2008-12-27 11:15:38 +0000384 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000385 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200386 struct gsm_bts *bts = lchan->ts->trx->bts;
387 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000388 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200389 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000390
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200391 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000392 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000393 mi_type, mi_string);
394
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100395 conn = &lchan->conn;
396
Harald Welte7659de12009-12-13 12:39:18 +0100397 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
398
Harald Welte75a983f2008-12-27 21:34:06 +0000399 switch (mi_type) {
400 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200401 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100402 if (!conn->subscr) {
403 conn->subscr = subscr_get_by_imsi(net, mi_string);
404 if (!conn->subscr)
405 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200406 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100407 if (conn->loc_operation)
408 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000409 break;
410 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000411 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000412 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100413 if (conn->subscr) {
414 db_subscriber_assoc_imei(conn->subscr, mi_string);
415 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200416 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100417 if (conn->loc_operation)
418 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000419 break;
420 }
Holger Freyther73487a22008-12-31 18:53:57 +0000421
422 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100423 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000424}
425
Harald Welte255539c2008-12-28 02:26:27 +0000426
427static void loc_upd_rej_cb(void *data)
428{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100429 struct gsm_subscriber_connection *conn = data;
430 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100431 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000432
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100433 release_loc_updating_req(conn);
Harald Welte1085c092009-11-18 20:33:19 +0100434 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000435 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000436}
437
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100438static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000439{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100440 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
441 conn->loc_operation->updating_timer.data = conn;
442 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000443}
444
Harald Welte2a139372009-02-22 21:14:55 +0000445static const char *lupd_name(u_int8_t type)
446{
447 switch (type) {
448 case GSM48_LUPD_NORMAL:
449 return "NORMAL";
450 case GSM48_LUPD_PERIODIC:
451 return "PEROIDOC";
452 case GSM48_LUPD_IMSI_ATT:
453 return "IMSI ATTACH";
454 default:
455 return "UNKNOWN";
456 }
457}
458
Harald Weltebf5e8df2009-02-03 12:59:45 +0000459/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000460static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000461{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100462 struct gsm_subscriber_connection *conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000463 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000464 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800465 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000466 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200467 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000468 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200469 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000470 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000471
Harald Welte8470bf22008-12-25 23:28:35 +0000472 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100473 conn = &lchan->conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000474
475 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000476
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200477 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000478
Harald Weltea0368542009-06-27 02:58:43 +0200479 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000480 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000481
Harald Welte7659de12009-12-13 12:39:18 +0100482 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
483
Harald Welte24ff6ee2009-12-22 00:41:05 +0100484 switch (lu->type) {
485 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100486 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100487 break;
488 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100489 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100490 break;
491 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100492 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100493 break;
494 }
495
Holger Freythereaf04692009-06-06 13:54:44 +0000496 /*
497 * Pseudo Spoof detection: Just drop a second/concurrent
498 * location updating request.
499 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100500 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200501 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100502 conn->loc_operation);
Holger Freythereaf04692009-06-06 13:54:44 +0000503 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
504 return 0;
505 }
506
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100507 allocate_loc_updating_req(&lchan->conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000508
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200509 conn->loc_operation->key_seq = lu->key_seq;
510
Harald Welte52b1f982008-12-23 20:25:15 +0000511 switch (mi_type) {
512 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200513 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000514 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000515 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100516 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000517
Jan Luebbe370b41d2009-08-12 10:19:34 +0200518 /* look up subscriber based on IMSI, create if not found */
519 subscr = subscr_get_by_imsi(bts->network, mi_string);
520 if (!subscr) {
521 subscr = db_create_subscriber(bts->network, mi_string);
522 }
Harald Welte4b634542008-12-27 01:55:51 +0000523 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000524 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200525 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000526 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200527 subscr = subscr_get_by_tmsi(bts->network,
528 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000529 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000530 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000531 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100532 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000533 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200534 /* we always want the IMEI, too */
535 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
536 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000537 break;
538 case GSM_MI_TYPE_IMEI:
539 case GSM_MI_TYPE_IMEISV:
540 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200541 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000542 break;
543 default:
Harald Weltea0368542009-06-27 02:58:43 +0200544 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000545 break;
546 }
547
Harald Welte24516ea2009-07-04 10:18:00 +0200548 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100549 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200550
Harald Welte4bfdfe72009-06-10 23:11:52 +0800551 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200552 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800553 /* FIXME: request id? close channel? */
554 return -EINVAL;
555 }
556
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100557 conn->subscr = subscr;
558 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000559
Harald Welte24516ea2009-07-04 10:18:00 +0200560 /* check if we can let the subscriber into our network immediately
561 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100562 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000563}
564
Harald Welte4bfdfe72009-06-10 23:11:52 +0800565#if 0
566static u_int8_t to_bcd8(u_int8_t val)
567{
568 return ((val / 10) << 4) | (val % 10);
569}
570#endif
571
Harald Weltedb253af2008-12-30 17:56:55 +0000572/* Section 9.2.15a */
573int gsm48_tx_mm_info(struct gsm_lchan *lchan)
574{
575 struct msgb *msg = gsm48_msgb_alloc();
576 struct gsm48_hdr *gh;
577 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000578 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200579 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800580#if 0
581 time_t cur_t;
582 struct tm* cur_time;
583 int tz15min;
584#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000585
586 msg->lchan = lchan;
587
588 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
589 gh->proto_discr = GSM48_PDISC_MM;
590 gh->msg_type = GSM48_MT_MM_INFO;
591
592 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200593#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000594 name_len = strlen(net->name_long);
595 /* 10.5.3.5a */
596 ptr8 = msgb_put(msg, 3);
597 ptr8[0] = GSM48_IE_NAME_LONG;
598 ptr8[1] = name_len*2 +1;
599 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
600
601 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
602 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000603 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000604
605 /* FIXME: Use Cell Broadcast, not UCS-2, since
606 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200607#endif
608 name_len = (strlen(net->name_long)*7)/8;
609 name_pad = (8 - strlen(net->name_long)*7)%8;
610 if (name_pad > 0)
611 name_len++;
612 /* 10.5.3.5a */
613 ptr8 = msgb_put(msg, 3);
614 ptr8[0] = GSM48_IE_NAME_LONG;
615 ptr8[1] = name_len +1;
616 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
617
618 ptr8 = msgb_put(msg, name_len);
619 gsm_7bit_encode(ptr8, net->name_long);
620
Harald Weltedb253af2008-12-30 17:56:55 +0000621 }
622
623 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200624#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000625 name_len = strlen(net->name_short);
626 /* 10.5.3.5a */
627 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200628 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000629 ptr8[1] = name_len*2 + 1;
630 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
631
Harald Weltee872cb12009-01-01 00:33:37 +0000632 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000633 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000634 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200635#endif
636 name_len = (strlen(net->name_short)*7)/8;
637 name_pad = (8 - strlen(net->name_short)*7)%8;
638 if (name_pad > 0)
639 name_len++;
640 /* 10.5.3.5a */
641 ptr8 = (u_int8_t *) msgb_put(msg, 3);
642 ptr8[0] = GSM48_IE_NAME_SHORT;
643 ptr8[1] = name_len +1;
644 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
645
646 ptr8 = msgb_put(msg, name_len);
647 gsm_7bit_encode(ptr8, net->name_short);
648
Harald Weltedb253af2008-12-30 17:56:55 +0000649 }
650
651#if 0
652 /* Section 10.5.3.9 */
653 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800654 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000655 ptr8 = msgb_put(msg, 8);
656 ptr8[0] = GSM48_IE_NET_TIME_TZ;
657 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
658 ptr8[2] = to_bcd8(cur_time->tm_mon);
659 ptr8[3] = to_bcd8(cur_time->tm_mday);
660 ptr8[4] = to_bcd8(cur_time->tm_hour);
661 ptr8[5] = to_bcd8(cur_time->tm_min);
662 ptr8[6] = to_bcd8(cur_time->tm_sec);
663 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
664 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800665 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000666 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800667 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000668#endif
669
Daniel Willmanneea93372009-08-13 03:42:07 +0200670 DEBUGP(DMM, "-> MM INFO\n");
671
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800672 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000673}
674
Harald Welte7984d5c2009-08-12 22:56:50 +0200675/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100676int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200677{
678 struct msgb *msg = gsm48_msgb_alloc();
679 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200680 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200681
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100682 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200683
684 msg->lchan = lchan;
685 gh->proto_discr = GSM48_PDISC_MM;
686 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
687
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100688 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200689
Harald Welte7984d5c2009-08-12 22:56:50 +0200690 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200691 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200692 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200693
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800694 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte7984d5c2009-08-12 22:56:50 +0200695}
696
697/* Section 9.2.1 */
698int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
699{
700 DEBUGP(DMM, "-> AUTH REJECT\n");
701 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
702}
703
Harald Welte4b634542008-12-27 01:55:51 +0000704static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
705{
Harald Welte4b634542008-12-27 01:55:51 +0000706 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000707 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000708}
Harald Welteba4cf162009-01-10 01:49:35 +0000709
710/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100711static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000712 enum gsm48_reject_value value)
713{
714 struct msgb *msg = gsm48_msgb_alloc();
715 struct gsm48_hdr *gh;
716
717 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
718
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100719 msg->lchan = conn->lchan;
720 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000721
722 gh->proto_discr = GSM48_PDISC_MM;
723 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
724 gh->data[0] = value;
725 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
726
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +0800727 return gsm48_conn_sendmsg(msg, conn, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000728}
729
Sylvain Munautba87f452009-12-24 00:28:46 +0100730static int _gsm48_rx_mm_serv_req_sec_cb(
731 unsigned int hooknum, unsigned int event,
732 struct msgb *msg, void *data, void *param)
733{
734 struct gsm_lchan *lchan = data;
735 int rc = 0;
736
737 switch (event) {
738 case GSM_SECURITY_AUTH_FAILED:
739 /* Nothing to do */
740 break;
741
742 case GSM_SECURITY_NOAVAIL:
743 rc = gsm48_tx_mm_serv_ack(lchan);
744 break;
745
746 case GSM_SECURITY_SUCCEEDED:
747 /* nothing to do. CIPHER MODE COMMAND is
748 * implicit CM SERV ACK */
749 break;
750
751 default:
752 rc = -EINVAL;
753 };
754
755 return rc;
756}
757
Harald Welte4ed0e922009-01-10 03:17:30 +0000758/*
759 * Handle CM Service Requests
760 * a) Verify that the packet is long enough to contain the information
761 * we require otherwsie reject with INCORRECT_MESSAGE
762 * b) Try to parse the TMSI. If we do not have one reject
763 * c) Check that we know the subscriber with the TMSI otherwise reject
764 * with a HLR cause
765 * d) Set the subscriber on the gsm_lchan and accept
766 */
Harald Welte4b634542008-12-27 01:55:51 +0000767static int gsm48_rx_mm_serv_req(struct msgb *msg)
768{
Harald Welteba4cf162009-01-10 01:49:35 +0000769 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200770 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000771
Harald Welte9176bd42009-07-23 18:46:00 +0200772 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000773 struct gsm_subscriber *subscr;
774 struct gsm48_hdr *gh = msgb_l3(msg);
775 struct gsm48_service_request *req =
776 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800777 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000778 u_int8_t classmark2_len = gh->data[1];
779 u_int8_t *classmark2 = gh->data+2;
780 u_int8_t mi_len = *(classmark2 + classmark2_len);
781 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000782
Harald Weltec9e02182009-05-01 19:07:53 +0000783 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000784 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000785 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100786 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000787 GSM48_REJECT_INCORRECT_MESSAGE);
788 }
789
790 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000791 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100792 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000793 GSM48_REJECT_INCORRECT_MESSAGE);
794 }
795
Harald Weltec9e02182009-05-01 19:07:53 +0000796 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000797 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000798 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100799 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000800 GSM48_REJECT_INCORRECT_MESSAGE);
801 }
802
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200803 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000804 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000805 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000806
Harald Welte7659de12009-12-13 12:39:18 +0100807 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
808
Harald Welte3ac7f102009-08-10 10:12:45 +0200809 if (is_siemens_bts(bts))
810 send_siemens_mrpci(msg->lchan, classmark2-1);
811
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200812 subscr = subscr_get_by_tmsi(bts->network,
813 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000814
Harald Welte2a139372009-02-22 21:14:55 +0000815 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000816 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100817 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000818 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
819
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100820 if (!msg->lchan->conn.subscr)
821 msg->lchan->conn.subscr = subscr;
822 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100823 subscr_put(subscr); /* lchan already has a ref, don't need another one */
824 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000825 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
826 subscr_put(subscr);
827 }
828
Harald Weltec2e302d2009-07-05 14:08:13 +0200829 subscr->equipment.classmark2_len = classmark2_len;
830 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
831 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000832
Sylvain Munautba87f452009-12-24 00:28:46 +0100833 return gsm48_secure_channel(msg->lchan, req->cipher_key_seq,
834 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000835}
836
Harald Welte2a139372009-02-22 21:14:55 +0000837static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
838{
Harald Welte9176bd42009-07-23 18:46:00 +0200839 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000840 struct gsm48_hdr *gh = msgb_l3(msg);
841 struct gsm48_imsi_detach_ind *idi =
842 (struct gsm48_imsi_detach_ind *) gh->data;
843 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200844 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800845 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000846
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200847 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000848 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
849 mi_type, mi_string);
850
Harald Welteffa55a42009-12-22 19:07:32 +0100851 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100852
Harald Welte2a139372009-02-22 21:14:55 +0000853 switch (mi_type) {
854 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200855 subscr = subscr_get_by_tmsi(bts->network,
856 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000857 break;
858 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200859 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000860 break;
861 case GSM_MI_TYPE_IMEI:
862 case GSM_MI_TYPE_IMEISV:
863 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000864 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000865 break;
866 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000867 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000868 break;
869 }
870
Holger Freyther4a49e772009-04-12 05:37:29 +0000871 if (subscr) {
872 subscr_update(subscr, msg->trx->bts,
873 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100874 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200875
876 subscr->equipment.classmark1 = idi->classmark1;
877 db_sync_equipment(&subscr->equipment);
878
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000879 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000880 } else
Harald Welte2a139372009-02-22 21:14:55 +0000881 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
882
Harald Welte31981a02009-12-20 09:58:40 +0100883 /* FIXME: iterate over all transactions and release them,
884 * imagine an IMSI DETACH happening during an active call! */
885
Harald Welteb83d9382009-12-12 21:00:48 +0100886 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100887 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100888
Harald Welte2a139372009-02-22 21:14:55 +0000889 return 0;
890}
891
Harald Welted2a7f5a2009-06-05 20:08:20 +0000892static int gsm48_rx_mm_status(struct msgb *msg)
893{
894 struct gsm48_hdr *gh = msgb_l3(msg);
895
896 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
897
898 return 0;
899}
900
Sylvain Munaut30a15382009-12-24 00:27:26 +0100901/* Chapter 9.2.3: Authentication Response */
902static int gsm48_rx_mm_auth_resp(struct msgb *msg)
903{
904 struct gsm48_hdr *gh = msgb_l3(msg);
905 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
906 struct gsm_lchan *lchan = msg->lchan;
907 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
908 struct gsm_network *net = lchan->ts->trx->bts->network;
909
Sylvain Munautc593cf12010-06-10 23:51:41 +0200910 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100911 hexdump(ar->sres, 4));
912
913 /* Safety check */
914 if (!conn->sec_operation) {
915 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
916 return -EIO;
917 }
918
919 /* Validate SRES */
920 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
921 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200922
923 DEBUGPC(DMM, "Invalid (expected %s)\n",
924 hexdump(conn->sec_operation->atuple.sres, 4));
925
Sylvain Munaut30a15382009-12-24 00:27:26 +0100926 if (cb)
927 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
928 NULL, lchan, conn->sec_operation->cb_data);
929
930 release_security_operation(conn);
931 return gsm48_tx_mm_auth_rej(lchan);
932 }
933
Sylvain Munautc593cf12010-06-10 23:51:41 +0200934 DEBUGPC(DMM, "OK\n");
935
Sylvain Munaut30a15382009-12-24 00:27:26 +0100936 /* Start ciphering */
937 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
938 lchan->encr.key_len = 8;
939 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
940
941 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
942}
943
Harald Weltebf5e8df2009-02-03 12:59:45 +0000944/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000945static int gsm0408_rcv_mm(struct msgb *msg)
946{
947 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800948 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000949
950 switch (gh->msg_type & 0xbf) {
951 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200952 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000953 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000954 break;
955 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000956 rc = mm_rx_id_resp(msg);
957 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000958 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000959 rc = gsm48_rx_mm_serv_req(msg);
960 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000961 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000962 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000963 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000964 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000965 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100966 msg->lchan->conn.subscr ?
967 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000968 "unknown subscriber");
969 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000970 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000971 rc = gsm48_rx_mm_imsi_detach_ind(msg);
972 break;
973 case GSM48_MT_MM_CM_REEST_REQ:
974 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
975 break;
976 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut30a15382009-12-24 00:27:26 +0100977 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000978 break;
979 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100980 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000981 gh->msg_type);
982 break;
983 }
984
985 return rc;
986}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000987
Harald Welte2d35ae62009-02-06 12:02:13 +0000988/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200989static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000990{
Harald Welte9176bd42009-07-23 18:46:00 +0200991 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000992 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800993 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000994 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200995 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200996 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800997 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000998 int rc = 0;
999
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +08001000 resp = (struct gsm48_pag_resp *) &gh->data[0];
1001 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
1002 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +00001003 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
1004 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +02001005
Harald Weltefe18d8f2009-02-22 21:14:24 +00001006 switch (mi_type) {
1007 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +02001008 subscr = subscr_get_by_tmsi(bts->network,
1009 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +00001010 break;
1011 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +02001012 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +00001013 break;
1014 }
Harald Welte2d35ae62009-02-06 12:02:13 +00001015
1016 if (!subscr) {
1017 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +00001018 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +00001019 return -EINVAL;
1020 }
1021 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +02001022 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +00001023
Harald Weltec2e302d2009-07-05 14:08:13 +02001024 subscr->equipment.classmark2_len = *classmark2_lv;
1025 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
1026 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001027
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +02001028 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +00001029 return rc;
1030}
1031
Harald Weltef7c43522009-06-09 20:24:21 +00001032static int gsm48_rx_rr_classmark(struct msgb *msg)
1033{
1034 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001035 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001036 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1037 u_int8_t cm2_len, cm3_len = 0;
1038 u_int8_t *cm2, *cm3 = NULL;
1039
1040 DEBUGP(DRR, "CLASSMARK CHANGE ");
1041
1042 /* classmark 2 */
1043 cm2_len = gh->data[0];
1044 cm2 = &gh->data[1];
1045 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1046
1047 if (payload_len > cm2_len + 1) {
1048 /* we must have a classmark3 */
1049 if (gh->data[cm2_len+1] != 0x20) {
1050 DEBUGPC(DRR, "ERR CM3 TAG\n");
1051 return -EINVAL;
1052 }
1053 if (cm2_len > 3) {
1054 DEBUGPC(DRR, "CM2 too long!\n");
1055 return -EINVAL;
1056 }
1057
1058 cm3_len = gh->data[cm2_len+2];
1059 cm3 = &gh->data[cm2_len+3];
1060 if (cm3_len > 14) {
1061 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1062 return -EINVAL;
1063 }
1064 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1065 }
1066 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001067 subscr->equipment.classmark2_len = cm2_len;
1068 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001069 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001070 subscr->equipment.classmark3_len = cm3_len;
1071 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001072 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001073 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001074 }
1075
Harald Weltef7c43522009-06-09 20:24:21 +00001076 return 0;
1077}
1078
Harald Weltecf5b3592009-05-01 18:28:42 +00001079static int gsm48_rx_rr_status(struct msgb *msg)
1080{
1081 struct gsm48_hdr *gh = msgb_l3(msg);
1082
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001083 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001084 rr_cause_name(gh->data[0]));
1085
1086 return 0;
1087}
1088
Harald Weltef7c43522009-06-09 20:24:21 +00001089static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1090{
Harald Welted12b0fd2009-12-15 21:36:05 +01001091 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001092
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001093 /* This shouldn't actually end up here, as RSL treats
1094 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1095 * directly into gsm48_parse_meas_rep */
1096 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001097 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001098
1099 return 0;
1100}
1101
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001102static int gsm48_rx_rr_app_info(struct msgb *msg)
1103{
1104 struct gsm48_hdr *gh = msgb_l3(msg);
1105 u_int8_t apdu_id_flags;
1106 u_int8_t apdu_len;
1107 u_int8_t *apdu_data;
1108
1109 apdu_id_flags = gh->data[0];
1110 apdu_len = gh->data[1];
1111 apdu_data = gh->data+2;
1112
1113 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1114 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1115
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001116 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 +02001117}
1118
Sylvain Munaut30a15382009-12-24 00:27:26 +01001119/* Chapter 9.1.10 Ciphering Mode Complete */
1120static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1121{
1122 struct gsm_lchan *lchan = msg->lchan;
1123 struct gsm_subscriber_connection *conn = &lchan->conn;
1124 gsm_cbfn *cb;
1125 int rc = 0;
1126
1127 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1128
1129 /* Safety check */
1130 if (!conn->sec_operation) {
1131 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1132 return -EIO;
1133 }
1134
1135 /* FIXME: check for MI (if any) */
1136
1137 /* Call back whatever was in progress (if anything) ... */
1138 cb = conn->sec_operation->cb;
1139 if (cb) {
1140 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1141 NULL, lchan, conn->sec_operation->cb_data);
1142 }
1143
1144 /* Complete the operation */
1145 release_security_operation(conn);
1146
1147 return rc;
1148}
1149
Harald Welted011e8b2009-11-29 22:45:52 +01001150/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001151static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001152{
1153 struct gsm48_hdr *gh = msgb_l3(msg);
1154
1155 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1156 rr_cause_name(gh->data[0]));
1157
1158 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1159 /* FIXME: release old channel */
1160
1161 return 0;
1162}
1163
1164/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001165static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001166{
1167 struct gsm48_hdr *gh = msgb_l3(msg);
1168
1169 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1170 rr_cause_name(gh->data[0]));
1171
1172 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1173 /* FIXME: release allocated new channel */
1174
1175 return 0;
1176}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001177
Harald Weltebf5e8df2009-02-03 12:59:45 +00001178/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001179static int gsm0408_rcv_rr(struct msgb *msg)
1180{
1181 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001182 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001183
1184 switch (gh->msg_type) {
1185 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001186 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001187 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001188 case GSM48_MT_RR_GPRS_SUSP_REQ:
1189 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1190 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001191 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001192 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001193 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001194 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001195 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001196 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001197 case GSM48_MT_RR_STATUS:
1198 rc = gsm48_rx_rr_status(msg);
1199 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001200 case GSM48_MT_RR_MEAS_REP:
1201 rc = gsm48_rx_rr_meas_rep(msg);
1202 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001203 case GSM48_MT_RR_APP_INFO:
1204 rc = gsm48_rx_rr_app_info(msg);
1205 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001206 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut30a15382009-12-24 00:27:26 +01001207 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001208 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001209 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001210 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001211 break;
1212 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001213 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001214 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001215 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001216 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1217 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001218 break;
1219 }
1220
Harald Welte2d35ae62009-02-06 12:02:13 +00001221 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001222}
1223
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001224int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001225 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001226{
1227 struct msgb *msg = gsm48_msgb_alloc();
1228 struct gsm48_hdr *gh;
1229
1230 msg->lchan = lchan;
1231
1232 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1233 apdu_id, apdu_len);
1234
1235 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1236 gh->proto_discr = GSM48_PDISC_RR;
1237 gh->msg_type = GSM48_MT_RR_APP_INFO;
1238 gh->data[0] = apdu_id;
1239 gh->data[1] = apdu_len;
1240 memcpy(gh->data+2, apdu, apdu_len);
1241
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001242 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001243}
1244
Harald Welte4bc90a12008-12-27 16:32:52 +00001245/* Call Control */
1246
Harald Welte7584aea2009-02-11 11:44:12 +00001247/* The entire call control code is written in accordance with Figure 7.10c
1248 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1249 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1250 * it for voice */
1251
Harald Welte4bfdfe72009-06-10 23:11:52 +08001252static void new_cc_state(struct gsm_trans *trans, int state)
1253{
1254 if (state > 31 || state < 0)
1255 return;
1256
1257 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001258 gsm48_cc_state_name(trans->cc.state),
1259 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001260
Harald Weltedcaf5652009-07-23 18:56:43 +02001261 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001262}
1263
1264static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001265{
1266 struct msgb *msg = gsm48_msgb_alloc();
1267 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1268 u_int8_t *cause, *call_state;
1269
Harald Welte4bc90a12008-12-27 16:32:52 +00001270 gh->msg_type = GSM48_MT_CC_STATUS;
1271
1272 cause = msgb_put(msg, 3);
1273 cause[0] = 2;
1274 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1275 cause[2] = 0x80 | 30; /* response to status inquiry */
1276
1277 call_state = msgb_put(msg, 1);
1278 call_state[0] = 0xc0 | 0x00;
1279
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001280 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001281}
1282
Harald Welte6f4b7532008-12-29 00:39:37 +00001283static int gsm48_tx_simple(struct gsm_lchan *lchan,
1284 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001285{
1286 struct msgb *msg = gsm48_msgb_alloc();
1287 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1288
1289 msg->lchan = lchan;
1290
Harald Welte6f4b7532008-12-29 00:39:37 +00001291 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001292 gh->msg_type = msg_type;
1293
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001294 return gsm48_conn_sendmsg(msg, &lchan->conn, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001295}
1296
Harald Welte4bfdfe72009-06-10 23:11:52 +08001297static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1298{
Harald Weltedcaf5652009-07-23 18:56:43 +02001299 if (bsc_timer_pending(&trans->cc.timer)) {
1300 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1301 bsc_del_timer(&trans->cc.timer);
1302 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001303 }
1304}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001305
Harald Welte4bfdfe72009-06-10 23:11:52 +08001306static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1307 int msg_type, struct gsm_mncc *mncc)
1308{
1309 struct msgb *msg;
1310
1311 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001312 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001313 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001315 trans->conn->lchan->ts->trx->bts->nr,
1316 trans->conn->lchan->ts->trx->nr,
1317 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001318 (trans->subscr)?(trans->subscr->extension):"-",
1319 get_mncc_name(msg_type));
1320 else
1321 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1322 "Sending '%s' to MNCC.\n",
1323 (trans->subscr)?(trans->subscr->extension):"-",
1324 get_mncc_name(msg_type));
1325 else
1326 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1327 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1328
1329 mncc->msg_type = msg_type;
1330
Harald Welte966636f2009-06-26 19:39:35 +02001331 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001332 if (!msg)
1333 return -ENOMEM;
1334 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1335 msgb_enqueue(&net->upqueue, msg);
1336
1337 return 0;
1338}
1339
1340int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1341 u_int32_t callref, int location, int value)
1342{
1343 struct gsm_mncc rel;
1344
Harald Welte92f70c52009-06-12 01:54:08 +08001345 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001346 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001347 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001348 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1349}
1350
Harald Weltedcaf5652009-07-23 18:56:43 +02001351/* Call Control Specific transaction release.
1352 * gets called by trans_free, DO NOT CALL YOURSELF! */
1353void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001354{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001355 gsm48_stop_cc_timer(trans);
1356
1357 /* send release to L4, if callref still exists */
1358 if (trans->callref) {
1359 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001360 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001361 GSM48_CAUSE_LOC_PRN_S_LU,
1362 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001364 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001365 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001366 if (trans->conn)
1367 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001368}
1369
1370static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001371
Harald Welte09e38af2009-02-16 22:52:23 +00001372/* call-back from paging the B-end of the connection */
1373static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001374 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001375{
Harald Welte7ccf7782009-02-17 01:43:01 +00001376 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377 struct gsm_subscriber *subscr = param;
1378 struct gsm_trans *transt, *tmp;
1379 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001380
Harald Welte09e38af2009-02-16 22:52:23 +00001381 if (hooknum != GSM_HOOK_RR_PAGING)
1382 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001383
Harald Welte4bfdfe72009-06-10 23:11:52 +08001384 if (!subscr)
1385 return -EINVAL;
1386 net = subscr->net;
1387 if (!net) {
1388 DEBUGP(DCC, "Error Network not set!\n");
1389 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001390 }
Harald Welte7584aea2009-02-11 11:44:12 +00001391
Harald Welte4bfdfe72009-06-10 23:11:52 +08001392 /* check all tranactions (without lchan) for subscriber */
1393 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001394 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001395 continue;
1396 switch (event) {
1397 case GSM_PAGING_SUCCEEDED:
1398 if (!lchan) // paranoid
1399 break;
1400 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1401 subscr->extension);
1402 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001403 if (!transt->conn) {
1404 transt->conn = &lchan->conn;
1405 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001406 }
1407 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001408 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001409 break;
1410 case GSM_PAGING_EXPIRED:
1411 DEBUGP(DCC, "Paging subscr %s expired!\n",
1412 subscr->extension);
1413 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001414 mncc_release_ind(transt->subscr->net, transt,
1415 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001416 GSM48_CAUSE_LOC_PRN_S_LU,
1417 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001418 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001419 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001420 break;
1421 }
1422 }
Harald Welte09e38af2009-02-16 22:52:23 +00001423 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001424}
Harald Welte7584aea2009-02-11 11:44:12 +00001425
Harald Welteda7ab742009-12-19 22:23:05 +01001426static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1427
Harald Welte805f6442009-07-28 18:25:29 +02001428/* some other part of the code sends us a signal */
1429static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1430 void *handler_data, void *signal_data)
1431{
1432 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001433 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001434 struct gsm_network *net;
1435 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001436
1437 if (subsys != SS_ABISIP)
1438 return 0;
1439
1440 /* in case we use direct BTS-to-BTS RTP */
1441 if (ipacc_rtp_direct)
1442 return 0;
1443
Harald Welte805f6442009-07-28 18:25:29 +02001444 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001445 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001446 /* check if any transactions on this lchan still have
1447 * a tch_recv_mncc request pending */
1448 net = lchan->ts->trx->bts->network;
1449 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001450 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001451 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1452 tch_recv_mncc(net, trans->callref, 1);
1453 }
1454 }
Harald Welte805f6442009-07-28 18:25:29 +02001455 break;
Harald Welte805f6442009-07-28 18:25:29 +02001456 }
1457
1458 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001459}
1460
Harald Welte49f48b82009-02-17 15:29:33 +00001461/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001462static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001463{
Harald Welte11fa29c2009-02-19 17:24:39 +00001464 struct gsm_bts *bts = lchan->ts->trx->bts;
1465 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001466 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001467
Harald Welte11fa29c2009-02-19 17:24:39 +00001468 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1469 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1470 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1471
1472 if (bts->type != remote_bts->type) {
1473 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1474 return -EINVAL;
1475 }
Harald Welteda7ab742009-12-19 22:23:05 +01001476
1477 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001478 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001479 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001480 if (!ipacc_rtp_direct) {
1481 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001482 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001483 if (rc < 0)
1484 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001485 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001486#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001487
1488 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301489 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1490 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001491 } else {
1492 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301493 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1494 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301495 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001496 if (rc < 0)
1497 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301498 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1499 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301500 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001501 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001502 break;
1503 case GSM_BTS_TYPE_BS11:
1504 trau_mux_map_lchan(lchan, remote_lchan);
1505 break;
1506 default:
1507 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001508 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001509 }
Harald Welte49f48b82009-02-17 15:29:33 +00001510
1511 return 0;
1512}
1513
Harald Welte4bfdfe72009-06-10 23:11:52 +08001514/* bridge channels of two transactions */
1515static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001516{
Harald Weltedcaf5652009-07-23 18:56:43 +02001517 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1518 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001519
Harald Welte4bfdfe72009-06-10 23:11:52 +08001520 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001521 return -EIO;
1522
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001523 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001524 return -EIO;
1525
1526 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001527 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001528}
1529
Harald Welteda7ab742009-12-19 22:23:05 +01001530/* enable receive of channels to MNCC upqueue */
1531static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001532{
1533 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001534 struct gsm_lchan *lchan;
1535 struct gsm_bts *bts;
1536 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001537
Harald Welte4bfdfe72009-06-10 23:11:52 +08001538 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001539 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001540 if (!trans)
1541 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001542 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001543 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001544 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001545 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001546
Harald Welteda7ab742009-12-19 22:23:05 +01001547 switch (bts->type) {
1548 case GSM_BTS_TYPE_NANOBTS:
1549 if (ipacc_rtp_direct) {
1550 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1551 return -EINVAL;
1552 }
1553 /* in case, we don't have a RTP socket yet, we note this
1554 * in the transaction and try later */
1555 if (!lchan->abis_ip.rtp_socket) {
1556 trans->tch_recv = enable;
1557 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1558 return 0;
1559 }
1560 if (enable) {
1561 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001562 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001563 if (rc < 0)
1564 return rc;
1565 /* assign socket to application interface */
1566 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1567 net, callref);
1568 } else
1569 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1570 net, 0);
1571 break;
1572 case GSM_BTS_TYPE_BS11:
1573 if (enable)
1574 return trau_recv_lchan(lchan, callref);
1575 return trau_mux_unmap(NULL, callref);
1576 break;
1577 default:
1578 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1579 return -EINVAL;
1580 }
1581
1582 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001583}
1584
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1586{
1587 DEBUGP(DCC, "-> STATUS ENQ\n");
1588 return gsm48_cc_tx_status(trans, msg);
1589}
1590
1591static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1592static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1593
1594static void gsm48_cc_timeout(void *arg)
1595{
1596 struct gsm_trans *trans = arg;
1597 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001598 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1599 int mo_location = GSM48_CAUSE_LOC_USER;
1600 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1601 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001602 struct gsm_mncc mo_rel, l4_rel;
1603
1604 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1605 mo_rel.callref = trans->callref;
1606 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1607 l4_rel.callref = trans->callref;
1608
Harald Weltedcaf5652009-07-23 18:56:43 +02001609 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001610 case 0x303:
1611 release = 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 0x310:
1615 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001616 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001617 break;
1618 case 0x313:
1619 disconnect = 1;
1620 /* unknown, did not find it in the specs */
1621 break;
1622 case 0x301:
1623 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001624 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001625 break;
1626 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001627 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001628 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001629 gsm48_cc_tx_release(trans, &trans->cc.msg);
1630 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001631 break; /* stay in release state */
1632 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001633 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001634 return;
1635// release = 1;
1636// l4_cause = 14;
1637// break;
1638 case 0x306:
1639 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001640 mo_cause = trans->cc.msg.cause.value;
1641 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001642 break;
1643 case 0x323:
1644 disconnect = 1;
1645 break;
1646 default:
1647 release = 1;
1648 }
1649
1650 if (release && trans->callref) {
1651 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001652 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001653 l4_location, l4_cause);
1654 trans->callref = 0;
1655 }
1656
1657 if (disconnect && trans->callref) {
1658 /* process disconnect towards layer 4 */
1659 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001660 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001661 }
1662
1663 /* process disconnect towards mobile station */
1664 if (disconnect || release) {
1665 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001666 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1667 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1668 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001669 mo_rel.cause.diag_len = 3;
1670
1671 if (disconnect)
1672 gsm48_cc_tx_disconnect(trans, &mo_rel);
1673 if (release)
1674 gsm48_cc_tx_release(trans, &mo_rel);
1675 }
1676
1677}
1678
1679static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1680 int sec, int micro)
1681{
1682 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001683 trans->cc.timer.cb = gsm48_cc_timeout;
1684 trans->cc.timer.data = trans;
1685 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1686 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001687}
1688
1689static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1690{
1691 struct gsm48_hdr *gh = msgb_l3(msg);
1692 u_int8_t msg_type = gh->msg_type & 0xbf;
1693 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1694 struct tlv_parsed tp;
1695 struct gsm_mncc setup;
1696
1697 memset(&setup, 0, sizeof(struct gsm_mncc));
1698 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001699 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001700 /* emergency setup is identified by msg_type */
1701 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1702 setup.emergency = 1;
1703
1704 /* use subscriber as calling party number */
1705 if (trans->subscr) {
1706 setup.fields |= MNCC_F_CALLING;
1707 strncpy(setup.calling.number, trans->subscr->extension,
1708 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001709 strncpy(setup.imsi, trans->subscr->imsi,
1710 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001711 }
1712 /* bearer capability */
1713 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1714 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001715 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001716 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1717 }
1718 /* facility */
1719 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1720 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001721 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001722 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1723 }
1724 /* called party bcd number */
1725 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1726 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001727 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001728 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1729 }
1730 /* user-user */
1731 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1732 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001733 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001734 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1735 }
1736 /* ss-version */
1737 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1738 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001739 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001740 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1741 }
1742 /* CLIR suppression */
1743 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1744 setup.clir.sup = 1;
1745 /* CLIR invocation */
1746 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1747 setup.clir.inv = 1;
1748 /* cc cap */
1749 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1750 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001751 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001752 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1753 }
1754
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 new_cc_state(trans, GSM_CSTATE_INITIATED);
1756
Harald Welte (local)47df3992009-12-26 19:45:03 +01001757 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1758 subscr_name(trans->subscr), trans->subscr->extension,
1759 setup.called.number);
1760
Harald Welte4bfdfe72009-06-10 23:11:52 +08001761 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001762 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763
Harald Welte13cac662009-07-29 12:10:35 +02001764 /* MNCC code will modify the channel asynchronously, we should
1765 * ipaccess-bind only after the modification has been made to the
1766 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 return 0;
1768}
1769
1770static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001771{
1772 struct msgb *msg = gsm48_msgb_alloc();
1773 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001774 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001775 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001776
Harald Welte7ccf7782009-02-17 01:43:01 +00001777 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001778
Harald Welte4bfdfe72009-06-10 23:11:52 +08001779 /* transaction id must not be assigned */
1780 if (trans->transaction_id != 0xff) { /* unasssigned */
1781 DEBUGP(DCC, "TX Setup with assigned transaction. "
1782 "This is not allowed!\n");
1783 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001784 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001785 GSM48_CAUSE_LOC_PRN_S_LU,
1786 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001787 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001788 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 return rc;
1790 }
1791
1792 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001793 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1794 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001796 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001797 GSM48_CAUSE_LOC_PRN_S_LU,
1798 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001799 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001800 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801 return rc;
1802 }
Harald Welte78283ef2009-07-23 21:36:44 +02001803 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001804
Harald Welte65e74cc2008-12-29 01:55:35 +00001805 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001806
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001808
Harald Welte4bfdfe72009-06-10 23:11:52 +08001809 /* bearer capability */
1810 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001811 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001812 /* facility */
1813 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001814 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001815 /* progress */
1816 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001817 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001818 /* calling party BCD number */
1819 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001820 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001821 /* called party BCD number */
1822 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001823 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001824 /* user-user */
1825 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001826 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001827 /* redirecting party BCD number */
1828 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001829 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001830 /* signal */
1831 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001832 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001833
1834 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001835
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001836 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001837}
1838
Harald Welte4bfdfe72009-06-10 23:11:52 +08001839static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1840{
1841 struct gsm48_hdr *gh = msgb_l3(msg);
1842 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1843 struct tlv_parsed tp;
1844 struct gsm_mncc call_conf;
1845
1846 gsm48_stop_cc_timer(trans);
1847 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1848
1849 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1850 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001851 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001852#if 0
1853 /* repeat */
1854 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1855 call_conf.repeat = 1;
1856 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1857 call_conf.repeat = 2;
1858#endif
1859 /* bearer capability */
1860 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1861 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001862 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001863 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1864 }
1865 /* cause */
1866 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1867 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001868 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001869 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1870 }
1871 /* cc cap */
1872 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1873 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001874 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001875 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1876 }
1877
1878 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1879
Harald Welte596fed42009-07-23 19:06:52 +02001880 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1881 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001882}
1883
1884static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1885{
1886 struct gsm_mncc *proceeding = arg;
1887 struct msgb *msg = gsm48_msgb_alloc();
1888 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1889
Harald Welte4bfdfe72009-06-10 23:11:52 +08001890 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1891
1892 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1893
1894 /* bearer capability */
1895 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001896 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001897 /* facility */
1898 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001899 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001900 /* progress */
1901 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001902 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001903
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001904 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905}
1906
1907static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1908{
1909 struct gsm48_hdr *gh = msgb_l3(msg);
1910 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1911 struct tlv_parsed tp;
1912 struct gsm_mncc alerting;
1913
1914 gsm48_stop_cc_timer(trans);
1915 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1916
1917 memset(&alerting, 0, sizeof(struct gsm_mncc));
1918 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001919 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920 /* facility */
1921 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1922 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001923 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001924 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1925 }
1926
1927 /* progress */
1928 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1929 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001930 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1932 }
1933 /* ss-version */
1934 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1935 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001936 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001937 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1938 }
1939
1940 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1941
Harald Welte596fed42009-07-23 19:06:52 +02001942 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1943 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001944}
1945
1946static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1947{
1948 struct gsm_mncc *alerting = arg;
1949 struct msgb *msg = gsm48_msgb_alloc();
1950 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1951
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952 gh->msg_type = GSM48_MT_CC_ALERTING;
1953
1954 /* facility */
1955 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001956 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001957 /* progress */
1958 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001959 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 /* user-user */
1961 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001962 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001963
1964 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1965
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001966 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001967}
1968
1969static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1970{
1971 struct gsm_mncc *progress = arg;
1972 struct msgb *msg = gsm48_msgb_alloc();
1973 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1974
Harald Welte4bfdfe72009-06-10 23:11:52 +08001975 gh->msg_type = GSM48_MT_CC_PROGRESS;
1976
1977 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001978 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001979 /* user-user */
1980 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001981 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001982
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08001983 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001984}
1985
1986static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1987{
1988 struct gsm_mncc *connect = arg;
1989 struct msgb *msg = gsm48_msgb_alloc();
1990 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1991
Harald Welte4bfdfe72009-06-10 23:11:52 +08001992 gh->msg_type = GSM48_MT_CC_CONNECT;
1993
1994 gsm48_stop_cc_timer(trans);
1995 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1996
1997 /* facility */
1998 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001999 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002000 /* progress */
2001 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002002 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002003 /* connected number */
2004 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01002005 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002006 /* user-user */
2007 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002008 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002009
2010 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
2011
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002012 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013}
2014
2015static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
2016{
2017 struct gsm48_hdr *gh = msgb_l3(msg);
2018 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2019 struct tlv_parsed tp;
2020 struct gsm_mncc connect;
2021
2022 gsm48_stop_cc_timer(trans);
2023
2024 memset(&connect, 0, sizeof(struct gsm_mncc));
2025 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002026 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002027 /* use subscriber as connected party number */
2028 if (trans->subscr) {
2029 connect.fields |= MNCC_F_CONNECTED;
2030 strncpy(connect.connected.number, trans->subscr->extension,
2031 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002032 strncpy(connect.imsi, trans->subscr->imsi,
2033 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002034 }
2035 /* facility */
2036 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2037 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002038 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2040 }
2041 /* user-user */
2042 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2043 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002044 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002045 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2046 }
2047 /* ss-version */
2048 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2049 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002050 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002051 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2052 }
2053
2054 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2055
Harald Welte596fed42009-07-23 19:06:52 +02002056 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002057}
2058
2059
2060static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2061{
2062 struct gsm_mncc connect_ack;
2063
2064 gsm48_stop_cc_timer(trans);
2065
2066 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2067
2068 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2069 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002070 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002071 &connect_ack);
2072}
2073
2074static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2075{
2076 struct msgb *msg = gsm48_msgb_alloc();
2077 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2078
Harald Welte4bfdfe72009-06-10 23:11:52 +08002079 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2080
2081 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2082
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002083 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084}
2085
2086static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2087{
2088 struct gsm48_hdr *gh = msgb_l3(msg);
2089 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2090 struct tlv_parsed tp;
2091 struct gsm_mncc disc;
2092
2093 gsm48_stop_cc_timer(trans);
2094
2095 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2096
2097 memset(&disc, 0, sizeof(struct gsm_mncc));
2098 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002099 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002100 /* cause */
2101 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2102 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002103 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002104 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2105 }
2106 /* facility */
2107 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2108 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002109 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002110 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2111 }
2112 /* user-user */
2113 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2114 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002115 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002116 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2117 }
2118 /* ss-version */
2119 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2120 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002121 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002122 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2123 }
2124
Harald Welte596fed42009-07-23 19:06:52 +02002125 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002126
2127}
2128
Harald Weltec66b71c2009-06-11 14:23:20 +08002129static struct gsm_mncc_cause default_cause = {
2130 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2131 .coding = 0,
2132 .rec = 0,
2133 .rec_val = 0,
2134 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2135 .diag_len = 0,
2136 .diag = { 0 },
2137};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002138
2139static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2140{
2141 struct gsm_mncc *disc = arg;
2142 struct msgb *msg = gsm48_msgb_alloc();
2143 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2144
Harald Welte4bfdfe72009-06-10 23:11:52 +08002145 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2146
2147 gsm48_stop_cc_timer(trans);
2148 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2149
2150 /* cause */
2151 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002152 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002153 else
Harald Welte55c8f352010-03-07 23:40:35 +01002154 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155
2156 /* facility */
2157 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002158 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 /* progress */
2160 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002161 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002162 /* user-user */
2163 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002164 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165
2166 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002167 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002168
2169 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2170
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002171 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002172}
2173
2174static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2175{
2176 struct gsm48_hdr *gh = msgb_l3(msg);
2177 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2178 struct tlv_parsed tp;
2179 struct gsm_mncc rel;
2180 int rc;
2181
2182 gsm48_stop_cc_timer(trans);
2183
2184 memset(&rel, 0, sizeof(struct gsm_mncc));
2185 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002186 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002187 /* cause */
2188 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2189 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002190 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002191 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2192 }
2193 /* facility */
2194 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2195 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002196 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002197 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2198 }
2199 /* user-user */
2200 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2201 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002202 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002203 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2204 }
2205 /* ss-version */
2206 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2207 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002208 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002209 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2210 }
2211
Harald Weltedcaf5652009-07-23 18:56:43 +02002212 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002213 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002214 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002215 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002216 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002217 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002218 GSM48_MT_CC_RELEASE_COMPL);
2219 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220 }
2221
2222 new_cc_state(trans, GSM_CSTATE_NULL);
2223
2224 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002225 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002226
2227 return rc;
2228}
2229
2230static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2231{
2232 struct gsm_mncc *rel = arg;
2233 struct msgb *msg = gsm48_msgb_alloc();
2234 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2235
Harald Welte4bfdfe72009-06-10 23:11:52 +08002236 gh->msg_type = GSM48_MT_CC_RELEASE;
2237
2238 trans->callref = 0;
2239
2240 gsm48_stop_cc_timer(trans);
2241 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2242
2243 /* cause */
2244 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002245 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002246 /* facility */
2247 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002248 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002249 /* user-user */
2250 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002251 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002252
Harald Weltedcaf5652009-07-23 18:56:43 +02002253 trans->cc.T308_second = 0;
2254 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002255
Harald Weltedcaf5652009-07-23 18:56:43 +02002256 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002257 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2258
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002259 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002260}
2261
2262static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2263{
2264 struct gsm48_hdr *gh = msgb_l3(msg);
2265 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2266 struct tlv_parsed tp;
2267 struct gsm_mncc rel;
2268 int rc = 0;
2269
2270 gsm48_stop_cc_timer(trans);
2271
2272 memset(&rel, 0, sizeof(struct gsm_mncc));
2273 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002274 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002275 /* cause */
2276 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2277 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002278 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002279 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2280 }
2281 /* facility */
2282 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2283 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002284 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002285 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2286 }
2287 /* user-user */
2288 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2289 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002290 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002291 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2292 }
2293 /* ss-version */
2294 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2295 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002296 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002297 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2298 }
2299
2300 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002301 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002302 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002303 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002304 MNCC_REJ_IND, &rel);
2305 break;
2306 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002307 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002309 /* FIXME: in case of multiple calls, we can't simply
2310 * hang up here ! */
2311 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002312 break;
2313 default:
Harald Welte596fed42009-07-23 19:06:52 +02002314 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002315 MNCC_REL_IND, &rel);
2316 }
2317 }
2318
2319 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002320 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002321
2322 return rc;
2323}
2324
2325static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2326{
2327 struct gsm_mncc *rel = arg;
2328 struct msgb *msg = gsm48_msgb_alloc();
2329 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2330
Harald Welte4bfdfe72009-06-10 23:11:52 +08002331 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2332
2333 trans->callref = 0;
2334
2335 gsm48_stop_cc_timer(trans);
2336
2337 /* cause */
2338 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002339 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 /* facility */
2341 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002342 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002343 /* user-user */
2344 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002345 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002346
Harald Weltedcaf5652009-07-23 18:56:43 +02002347 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002348
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002349 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002350}
2351
2352static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2353{
2354 struct gsm48_hdr *gh = msgb_l3(msg);
2355 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2356 struct tlv_parsed tp;
2357 struct gsm_mncc fac;
2358
2359 memset(&fac, 0, sizeof(struct gsm_mncc));
2360 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002361 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002362 /* facility */
2363 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2364 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002365 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002366 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2367 }
2368 /* ss-version */
2369 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2370 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002371 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002372 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2373 }
2374
Harald Welte596fed42009-07-23 19:06:52 +02002375 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002376}
2377
2378static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2379{
2380 struct gsm_mncc *fac = arg;
2381 struct msgb *msg = gsm48_msgb_alloc();
2382 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2383
Harald Welte4bfdfe72009-06-10 23:11:52 +08002384 gh->msg_type = GSM48_MT_CC_FACILITY;
2385
2386 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002387 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002388
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002389 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390}
2391
2392static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2393{
2394 struct gsm_mncc hold;
2395
2396 memset(&hold, 0, sizeof(struct gsm_mncc));
2397 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002398 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002399}
2400
2401static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2402{
2403 struct msgb *msg = gsm48_msgb_alloc();
2404 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2405
Harald Welte4bfdfe72009-06-10 23:11:52 +08002406 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2407
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002408 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002409}
2410
2411static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2412{
2413 struct gsm_mncc *hold_rej = arg;
2414 struct msgb *msg = gsm48_msgb_alloc();
2415 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2416
Harald Welte4bfdfe72009-06-10 23:11:52 +08002417 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2418
2419 /* cause */
2420 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002421 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422 else
Harald Welte55c8f352010-03-07 23:40:35 +01002423 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002424
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002425 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002426}
2427
2428static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2429{
2430 struct gsm_mncc retrieve;
2431
2432 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2433 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002434 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2435 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002436}
2437
2438static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2439{
2440 struct msgb *msg = gsm48_msgb_alloc();
2441 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2442
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2444
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002445 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002446}
2447
2448static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2449{
2450 struct gsm_mncc *retrieve_rej = arg;
2451 struct msgb *msg = gsm48_msgb_alloc();
2452 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2453
Harald Welte4bfdfe72009-06-10 23:11:52 +08002454 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2455
2456 /* cause */
2457 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002458 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 else
Harald Welte55c8f352010-03-07 23:40:35 +01002460 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002461
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002462 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002463}
2464
2465static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2466{
2467 struct gsm48_hdr *gh = msgb_l3(msg);
2468 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2469 struct tlv_parsed tp;
2470 struct gsm_mncc dtmf;
2471
2472 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2473 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002474 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002475 /* keypad facility */
2476 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2477 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2480 }
2481
Harald Welte596fed42009-07-23 19:06:52 +02002482 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483}
2484
2485static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2486{
2487 struct gsm_mncc *dtmf = arg;
2488 struct msgb *msg = gsm48_msgb_alloc();
2489 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2490
Harald Welte4bfdfe72009-06-10 23:11:52 +08002491 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2492
2493 /* keypad */
2494 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002495 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002496
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002497 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002498}
2499
2500static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2501{
2502 struct gsm_mncc *dtmf = arg;
2503 struct msgb *msg = gsm48_msgb_alloc();
2504 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2505
Harald Welte4bfdfe72009-06-10 23:11:52 +08002506 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2507
2508 /* cause */
2509 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002510 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002511 else
Harald Welte55c8f352010-03-07 23:40:35 +01002512 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002513
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002514 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515}
2516
2517static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2518{
2519 struct msgb *msg = gsm48_msgb_alloc();
2520 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2521
Harald Welte4bfdfe72009-06-10 23:11:52 +08002522 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2523
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002524 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525}
2526
2527static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2528{
2529 struct gsm_mncc dtmf;
2530
2531 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2532 dtmf.callref = trans->callref;
2533
Harald Welte596fed42009-07-23 19:06:52 +02002534 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002535}
2536
2537static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2538{
2539 struct gsm48_hdr *gh = msgb_l3(msg);
2540 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2541 struct tlv_parsed tp;
2542 struct gsm_mncc modify;
2543
2544 memset(&modify, 0, sizeof(struct gsm_mncc));
2545 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002546 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002547 /* bearer capability */
2548 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2549 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002550 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002551 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2552 }
2553
2554 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2555
Harald Welte596fed42009-07-23 19:06:52 +02002556 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557}
2558
2559static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2560{
2561 struct gsm_mncc *modify = arg;
2562 struct msgb *msg = gsm48_msgb_alloc();
2563 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2564
Harald Welte4bfdfe72009-06-10 23:11:52 +08002565 gh->msg_type = GSM48_MT_CC_MODIFY;
2566
2567 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2568
2569 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002570 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002571
2572 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2573
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002574 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575}
2576
2577static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2578{
2579 struct gsm48_hdr *gh = msgb_l3(msg);
2580 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2581 struct tlv_parsed tp;
2582 struct gsm_mncc modify;
2583
2584 gsm48_stop_cc_timer(trans);
2585
2586 memset(&modify, 0, sizeof(struct gsm_mncc));
2587 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002588 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002589 /* bearer capability */
2590 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2591 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002592 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002593 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2594 }
2595
2596 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2597
Harald Welte596fed42009-07-23 19:06:52 +02002598 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002599}
2600
2601static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2602{
2603 struct gsm_mncc *modify = arg;
2604 struct msgb *msg = gsm48_msgb_alloc();
2605 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2606
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2608
2609 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002610 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002611
2612 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2613
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002614 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002615}
2616
2617static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2618{
2619 struct gsm48_hdr *gh = msgb_l3(msg);
2620 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2621 struct tlv_parsed tp;
2622 struct gsm_mncc modify;
2623
2624 gsm48_stop_cc_timer(trans);
2625
2626 memset(&modify, 0, sizeof(struct gsm_mncc));
2627 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002628 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002629 /* bearer capability */
2630 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2631 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002632 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002633 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2634 }
2635 /* cause */
2636 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2637 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002638 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2640 }
2641
2642 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2643
Harald Welte596fed42009-07-23 19:06:52 +02002644 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645}
2646
2647static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2648{
2649 struct gsm_mncc *modify = arg;
2650 struct msgb *msg = gsm48_msgb_alloc();
2651 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2652
Harald Welte4bfdfe72009-06-10 23:11:52 +08002653 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2654
2655 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002656 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002658 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659
2660 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2661
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002662 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002663}
2664
2665static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2666{
2667 struct gsm_mncc *notify = arg;
2668 struct msgb *msg = gsm48_msgb_alloc();
2669 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2670
Harald Welte4bfdfe72009-06-10 23:11:52 +08002671 gh->msg_type = GSM48_MT_CC_NOTIFY;
2672
2673 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002674 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002675
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002676 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677}
2678
2679static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2680{
2681 struct gsm48_hdr *gh = msgb_l3(msg);
2682 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2683// struct tlv_parsed tp;
2684 struct gsm_mncc notify;
2685
2686 memset(&notify, 0, sizeof(struct gsm_mncc));
2687 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002688// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002689 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002690 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691
Harald Welte596fed42009-07-23 19:06:52 +02002692 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002693}
2694
2695static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2696{
2697 struct gsm_mncc *user = arg;
2698 struct msgb *msg = gsm48_msgb_alloc();
2699 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2700
Harald Welte4bfdfe72009-06-10 23:11:52 +08002701 gh->msg_type = GSM48_MT_CC_USER_INFO;
2702
2703 /* user-user */
2704 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002705 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002706 /* more data */
2707 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002708 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709
Holger Hans Peter Freyther9c137a72010-06-15 13:57:40 +08002710 return gsm48_conn_sendmsg(msg, trans->conn, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002711}
2712
2713static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2714{
2715 struct gsm48_hdr *gh = msgb_l3(msg);
2716 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2717 struct tlv_parsed tp;
2718 struct gsm_mncc user;
2719
2720 memset(&user, 0, sizeof(struct gsm_mncc));
2721 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002722 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002723 /* user-user */
2724 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2725 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002726 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002727 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2728 }
2729 /* more data */
2730 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2731 user.more = 1;
2732
Harald Welte596fed42009-07-23 19:06:52 +02002733 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002734}
2735
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002736static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002737{
2738 struct gsm_mncc *mode = arg;
2739
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002740 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002741}
2742
2743static struct downstate {
2744 u_int32_t states;
2745 int type;
2746 int (*rout) (struct gsm_trans *trans, void *arg);
2747} downstatelist[] = {
2748 /* mobile originating call establishment */
2749 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2750 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2751 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2752 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2753 {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 */
2754 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2755 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2756 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2757 /* mobile terminating call establishment */
2758 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2759 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2760 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2761 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2762 /* signalling during call */
2763 {SBIT(GSM_CSTATE_ACTIVE),
2764 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2765 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2766 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2767 {ALL_STATES,
2768 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2769 {ALL_STATES,
2770 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2771 {ALL_STATES,
2772 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2773 {SBIT(GSM_CSTATE_ACTIVE),
2774 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2775 {SBIT(GSM_CSTATE_ACTIVE),
2776 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2777 {SBIT(GSM_CSTATE_ACTIVE),
2778 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2779 {SBIT(GSM_CSTATE_ACTIVE),
2780 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2781 {SBIT(GSM_CSTATE_ACTIVE),
2782 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2783 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2784 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2785 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2786 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2787 {SBIT(GSM_CSTATE_ACTIVE),
2788 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2789 /* clearing */
2790 {SBIT(GSM_CSTATE_INITIATED),
2791 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2792 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2793 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2794 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2795 MNCC_REL_REQ, gsm48_cc_tx_release},
2796 /* special */
2797 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002798 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002799};
2800
2801#define DOWNSLLEN \
2802 (sizeof(downstatelist) / sizeof(struct downstate))
2803
2804
2805int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2806{
Harald Welte1a6f7982009-08-09 18:52:33 +02002807 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002808 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002809 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002811 struct gsm_mncc *data = arg, rel;
2812
2813 /* handle special messages */
2814 switch(msg_type) {
2815 case MNCC_BRIDGE:
2816 return tch_bridge(net, arg);
2817 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002818 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002819 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002820 return tch_recv_mncc(net, data->callref, 1);
2821 case GSM_TCHF_FRAME:
2822 /* Find callref */
2823 trans = trans_find_by_callref(net, data->callref);
2824 if (!trans)
2825 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002826 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002827 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002828 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002829 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002830 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002831 switch (bts->type) {
2832 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002833 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002834 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002835 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002836 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002837 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002838 default:
2839 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2840 }
2841 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002842 }
2843
2844 memset(&rel, 0, sizeof(struct gsm_mncc));
2845 rel.callref = data->callref;
2846
2847 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002848 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002849
2850 /* Callref unknown */
2851 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002852 struct gsm_subscriber *subscr;
2853
Harald Welte4a3464c2009-07-04 10:11:24 +02002854 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002855 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2856 "Received '%s' from MNCC with "
2857 "unknown callref %d\n", data->called.number,
2858 get_mncc_name(msg_type), data->callref);
2859 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002860 return mncc_release_ind(net, NULL, data->callref,
2861 GSM48_CAUSE_LOC_PRN_S_LU,
2862 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002863 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002864 if (!data->called.number[0] && !data->imsi[0]) {
2865 DEBUGP(DCC, "(bts - trx - ts - ti) "
2866 "Received '%s' from MNCC with "
2867 "no number or IMSI\n", get_mncc_name(msg_type));
2868 /* Invalid number */
2869 return mncc_release_ind(net, NULL, data->callref,
2870 GSM48_CAUSE_LOC_PRN_S_LU,
2871 GSM48_CC_CAUSE_INV_NR_FORMAT);
2872 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002874 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002875 subscr = subscr_get_by_extension(net,
2876 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002877 else
Harald Welte9176bd42009-07-23 18:46:00 +02002878 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 /* If subscriber is not found */
2880 if (!subscr) {
2881 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2882 "Received '%s' from MNCC with "
2883 "unknown subscriber %s\n", data->called.number,
2884 get_mncc_name(msg_type), data->called.number);
2885 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002886 return mncc_release_ind(net, NULL, data->callref,
2887 GSM48_CAUSE_LOC_PRN_S_LU,
2888 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002889 }
2890 /* If subscriber is not "attached" */
2891 if (!subscr->lac) {
2892 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2893 "Received '%s' from MNCC with "
2894 "detached subscriber %s\n", data->called.number,
2895 get_mncc_name(msg_type), data->called.number);
2896 subscr_put(subscr);
2897 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002898 return mncc_release_ind(net, NULL, data->callref,
2899 GSM48_CAUSE_LOC_PRN_S_LU,
2900 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002901 }
2902 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002903 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2904 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002905 DEBUGP(DCC, "No memory for trans.\n");
2906 subscr_put(subscr);
2907 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002908 mncc_release_ind(net, NULL, data->callref,
2909 GSM48_CAUSE_LOC_PRN_S_LU,
2910 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002911 return -ENOMEM;
2912 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002913 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002914 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002915
Harald Welte4bfdfe72009-06-10 23:11:52 +08002916 /* If subscriber has no lchan */
2917 if (!lchan) {
2918 /* find transaction with this subscriber already paging */
2919 llist_for_each_entry(transt, &net->trans_list, entry) {
2920 /* Transaction of our lchan? */
2921 if (transt == trans ||
2922 transt->subscr != subscr)
2923 continue;
2924 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2925 "Received '%s' from MNCC with "
2926 "unallocated channel, paging already "
2927 "started.\n", bts->nr,
2928 data->called.number,
2929 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002930 subscr_put(subscr);
2931 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002932 return 0;
2933 }
2934 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002935 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002936 /* Trigger paging */
2937 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2938 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002939 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002940 return 0;
2941 }
2942 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002943 trans->conn = &lchan->conn;
2944 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002945 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002946 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002947
2948 if (trans->conn)
2949 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002950
2951 /* if paging did not respond yet */
2952 if (!lchan) {
2953 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002954 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002955 (trans->subscr)?(trans->subscr->extension):"-",
2956 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002957 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2958 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002959 if (msg_type == MNCC_REL_REQ)
2960 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2961 else
2962 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2963 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002964 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002965 return rc;
2966 }
2967
2968 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2969 "Received '%s' from MNCC in state %d (%s)\n",
2970 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2971 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002972 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002973 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002974 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002975
2976 /* Find function for current state and message */
2977 for (i = 0; i < DOWNSLLEN; i++)
2978 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002979 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002980 break;
2981 if (i == DOWNSLLEN) {
2982 DEBUGP(DCC, "Message unhandled at this state.\n");
2983 return 0;
2984 }
2985
2986 rc = downstatelist[i].rout(trans, arg);
2987
2988 return rc;
2989}
2990
2991
2992static struct datastate {
2993 u_int32_t states;
2994 int type;
2995 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2996} datastatelist[] = {
2997 /* mobile originating call establishment */
2998 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2999 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
3000 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
3001 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
3002 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
3003 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
3004 /* mobile terminating call establishment */
3005 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
3006 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
3007 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
3008 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02003009 {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 +08003010 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
3011 /* signalling during call */
3012 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
3013 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
3014 {SBIT(GSM_CSTATE_ACTIVE),
3015 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
3016 {ALL_STATES,
3017 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
3018 {ALL_STATES,
3019 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
3020 {ALL_STATES,
3021 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
3022 {SBIT(GSM_CSTATE_ACTIVE),
3023 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
3024 {SBIT(GSM_CSTATE_ACTIVE),
3025 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
3026 {SBIT(GSM_CSTATE_ACTIVE),
3027 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
3028 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3029 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
3030 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
3031 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3032 {SBIT(GSM_CSTATE_ACTIVE),
3033 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3034 /* clearing */
3035 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3036 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3037 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3038 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3039 {ALL_STATES, /* 5.4.3.4 */
3040 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3041};
3042
3043#define DATASLLEN \
3044 (sizeof(datastatelist) / sizeof(struct datastate))
3045
Harald Welte4bc90a12008-12-27 16:32:52 +00003046static int gsm0408_rcv_cc(struct msgb *msg)
3047{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003048 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00003049 struct gsm48_hdr *gh = msgb_l3(msg);
3050 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003051 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003053 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003054 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003055
Harald Welte4bfdfe72009-06-10 23:11:52 +08003056 if (msg_type & 0x80) {
3057 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3058 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003059 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003060
3061 conn = &lchan->conn;
3062
Harald Welte4bfdfe72009-06-10 23:11:52 +08003063 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003064 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003065
Harald Welte6f5aee02009-07-23 21:21:14 +02003066 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003067 "Received '%s' from MS in state %d (%s)\n",
3068 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003069 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003070 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3071 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003072
3073 /* Create transaction */
3074 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003075 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003076 "creating new trans.\n", transaction_id);
3077 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003078 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003079 transaction_id, new_callref++);
3080 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003081 DEBUGP(DCC, "No memory for trans.\n");
3082 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003083 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003084 GSM48_MT_CC_RELEASE_COMPL);
3085 return -ENOMEM;
3086 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003087 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003088 trans->conn = &lchan->conn;
3089 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003090 }
3091
3092 /* find function for current state and message */
3093 for (i = 0; i < DATASLLEN; i++)
3094 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003095 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003096 break;
3097 if (i == DATASLLEN) {
3098 DEBUGP(DCC, "Message unhandled at this state.\n");
3099 return 0;
3100 }
3101
3102 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003103
3104 return rc;
3105}
3106
Harald Welte52b1f982008-12-23 20:25:15 +00003107/* 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 +02003108int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003109{
3110 struct gsm48_hdr *gh = msgb_l3(msg);
3111 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003112 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003113
3114 if (silent_call_reroute(msg))
3115 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003116
3117 switch (pdisc) {
3118 case GSM48_PDISC_CC:
3119 rc = gsm0408_rcv_cc(msg);
3120 break;
3121 case GSM48_PDISC_MM:
3122 rc = gsm0408_rcv_mm(msg);
3123 break;
3124 case GSM48_PDISC_RR:
3125 rc = gsm0408_rcv_rr(msg);
3126 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003127 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003128 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003129 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003130 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003131 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003132 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3133 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003134 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003135 case GSM48_PDISC_NC_SS:
3136 rc = handle_rcv_ussd(msg);
3137 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003138 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003139 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3140 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003141 break;
3142 }
3143
3144 return rc;
3145}
Harald Welte8470bf22008-12-25 23:28:35 +00003146
Harald Welte805f6442009-07-28 18:25:29 +02003147/*
3148 * This will be ran by the linker when loading the DSO. We use it to
3149 * do system initialization, e.g. registration of signal handlers.
3150 */
3151static __attribute__((constructor)) void on_dso_load_0408(void)
3152{
Harald Welte805f6442009-07-28 18:25:29 +02003153 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3154}