blob: 4cc86675b893384ba4dd357294fed9699f4d575d [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>
Harald Welte52b1f982008-12-23 20:25:15 +000056
Harald Welte (local)d19e58b2009-08-15 02:30:58 +020057void *tall_locop_ctx;
Sylvain Munaut30a15382009-12-24 00:27:26 +010058void *tall_authciphop_ctx;
Harald Welte2cf161b2009-06-20 22:36:41 +020059
Holger Freytherd51524f2009-06-09 08:27:07 +000060int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi);
Harald Welte65e74cc2008-12-29 01:55:35 +000061static int gsm48_tx_simple(struct gsm_lchan *lchan,
62 u_int8_t pdisc, u_int8_t msg_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +010063static void schedule_reject(struct gsm_subscriber_connection *conn);
Harald Welte65e74cc2008-12-29 01:55:35 +000064
Harald Welte52b1f982008-12-23 20:25:15 +000065struct gsm_lai {
66 u_int16_t mcc;
67 u_int16_t mnc;
68 u_int16_t lac;
69};
70
Harald Welte4bfdfe72009-06-10 23:11:52 +080071static u_int32_t new_callref = 0x80000001;
72
Sylvain Munaut30a15382009-12-24 00:27:26 +010073
74static void release_security_operation(struct gsm_subscriber_connection *conn)
75{
76 if (!conn->sec_operation)
77 return;
78
79 talloc_free(conn->sec_operation);
80 conn->sec_operation = NULL;
81 put_subscr_con(conn);
82}
83
84static void allocate_security_operation(struct gsm_subscriber_connection *conn)
85{
86 use_subscr_con(conn)
87
88 conn->sec_operation = talloc_zero(tall_authciphop_ctx,
89 struct gsm_security_operation);
90}
91
92int gsm48_secure_channel(struct gsm_lchan *lchan, int key_seq,
93 gsm_cbfn *cb, void *cb_data)
94{
95 struct gsm_network *net = lchan->ts->trx->bts->network;
96 struct gsm_subscriber *subscr = lchan->conn.subscr;
97 struct gsm_security_operation *op;
98 struct gsm_auth_tuple atuple;
99 int status = -1, rc;
100
101 /* Check if we _can_ enable encryption. Cases where we can't:
102 * - Encryption disabled in config
103 * - Channel already secured (nothing to do)
104 * - Subscriber equipment doesn't support configured encryption
105 */
106 if (!net->a5_encryption) {
107 status = GSM_SECURITY_NOAVAIL;
108 } else if (lchan->encr.alg_id > RSL_ENC_ALG_A5(0)) {
109 DEBUGP(DMM, "Requesting to secure an already secure channel");
110 status = GSM_SECURITY_SUCCEEDED;
111 } else if (!ms_cm2_a5n_support(subscr->equipment.classmark2,
112 net->a5_encryption)) {
113 DEBUGP(DMM, "Subscriber equipment doesn't support requested encryption");
114 status = GSM_SECURITY_NOAVAIL;
115 }
116
117 /* If not done yet, try to get info for this user */
118 if (status < 0) {
119 rc = auth_get_tuple_for_subscr(&atuple, subscr, key_seq);
120 if (rc <= 0)
121 status = GSM_SECURITY_NOAVAIL;
122 }
123
124 /* Are we done yet ? */
125 if (status >= 0)
126 return cb ?
127 cb(GSM_HOOK_RR_SECURITY, status, NULL, lchan, cb_data) :
128 0;
129
130 /* Start an operation (can't have more than one pending !!!) */
131 if (lchan->conn.sec_operation)
132 return -EBUSY;
133
134 allocate_security_operation(&lchan->conn);
135 op = lchan->conn.sec_operation;
136 op->cb = cb;
137 op->cb_data = cb_data;
138 memcpy(&op->atuple, &atuple, sizeof(struct gsm_auth_tuple));
139
140 /* FIXME: Should start a timer for completion ... */
141
142 /* Then do whatever is needed ... */
143 if (rc == 1) {
144 /* Start authentication */
145 return gsm48_tx_mm_auth_req(lchan, op->atuple.rand, op->atuple.key_seq);
146 } else if (rc == 2) {
147 /* Start ciphering directly */
148 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
149 lchan->encr.key_len = 8;
150 memcpy(lchan->encr.key, op->atuple.kc, 8);
151
152 return gsm48_send_rr_ciph_mode(lchan, 0);
153 }
154
155 return -EINVAL; /* not reached */
156}
157
Holger Freyther73487a22008-12-31 18:53:57 +0000158static int authorize_subscriber(struct gsm_loc_updating_operation *loc,
159 struct gsm_subscriber *subscriber)
Holger Freyther89824fc2008-12-30 16:18:18 +0000160{
161 if (!subscriber)
162 return 0;
163
Holger Freyther73487a22008-12-31 18:53:57 +0000164 /*
165 * Do not send accept yet as more information should arrive. Some
166 * phones will not send us the information and we will have to check
167 * what we want to do with that.
168 */
169 if (loc && (loc->waiting_for_imsi || loc->waiting_for_imei))
170 return 0;
171
Jan Luebbe06513f22009-08-12 12:48:00 +0200172 switch (subscriber->net->auth_policy) {
173 case GSM_AUTH_POLICY_CLOSED:
174 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200175 case GSM_AUTH_POLICY_TOKEN:
Harald Welte (local)ee9afe32009-08-13 20:44:23 +0200176 if (subscriber->authorized)
177 return subscriber->authorized;
Harald Welte (local)aa9dc192009-08-13 13:49:51 +0200178 return (subscriber->flags & GSM_SUBSCRIBER_FIRST_CONTACT);
Jan Luebbe06513f22009-08-12 12:48:00 +0200179 case GSM_AUTH_POLICY_ACCEPT_ALL:
Holger Freyther89824fc2008-12-30 16:18:18 +0000180 return 1;
Jan Luebbe06513f22009-08-12 12:48:00 +0200181 default:
182 return 0;
183 }
Holger Freyther89824fc2008-12-30 16:18:18 +0000184}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000185
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100186static void release_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000187{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100188 if (!conn->loc_operation)
Holger Freyther73487a22008-12-31 18:53:57 +0000189 return;
190
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100191 bsc_del_timer(&conn->loc_operation->updating_timer);
192 talloc_free(conn->loc_operation);
193 conn->loc_operation = 0;
194 put_subscr_con(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000195}
196
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100197static void allocate_loc_updating_req(struct gsm_subscriber_connection *conn)
Holger Freyther73487a22008-12-31 18:53:57 +0000198{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100199 use_subscr_con(conn)
200 release_loc_updating_req(conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000201
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100202 conn->loc_operation = talloc_zero(tall_locop_ctx,
Harald Welte470ec292009-06-26 20:25:23 +0200203 struct gsm_loc_updating_operation);
Holger Freyther73487a22008-12-31 18:53:57 +0000204}
Holger Freyther07cc8d82008-12-29 06:23:46 +0000205
Sylvain Munaut267fba02009-12-24 00:28:01 +0100206static int _gsm0408_authorize_sec_cb(unsigned int hooknum, unsigned int event,
207 struct msgb *msg, void *data, void *param)
208{
209 struct gsm_lchan *lchan = data;
210 struct gsm_subscriber_connection *conn = &lchan->conn;
211 int rc = 0;
212
213 switch (event) {
214 case GSM_SECURITY_AUTH_FAILED:
215 release_loc_updating_req(conn);
216 break;
217
218 case GSM_SECURITY_NOAVAIL:
219 case GSM_SECURITY_SUCCEEDED:
220 /* We're all good */
221 db_subscriber_alloc_tmsi(conn->subscr);
222 release_loc_updating_req(conn);
223 rc = gsm0408_loc_upd_acc(lchan, conn->subscr->tmsi);
224 if (lchan->ts->trx->bts->network->send_mm_info) {
225 /* send MM INFO with network name */
226 rc = gsm48_tx_mm_info(lchan);
227 }
228
229 /* call subscr_update after putting the loc_upd_acc
230 * in the transmit queue, since S_SUBSCR_ATTACHED might
231 * trigger further action like SMS delivery */
232 subscr_update(conn->subscr, lchan->ts->trx->bts,
233 GSM_SUBSCRIBER_UPDATE_ATTACHED);
234
235 /* try to close channel ASAP */
236 lchan_auto_release(lchan);
237
238 break;
239
240 default:
241 rc = -EINVAL;
242 };
243
244 return rc;
245}
246
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100247static int gsm0408_authorize(struct gsm_subscriber_connection *conn, struct msgb *msg)
Holger Freytherd51524f2009-06-09 08:27:07 +0000248{
Sylvain Munaut267fba02009-12-24 00:28:01 +0100249 if (authorize_subscriber(conn->loc_operation, conn->subscr))
250 return gsm48_secure_channel(conn->lchan,
251 conn->loc_operation->key_seq,
252 _gsm0408_authorize_sec_cb, NULL);
Holger Freytherd51524f2009-06-09 08:27:07 +0000253 return 0;
254}
255
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800256void gsm0408_clear_request(struct gsm_subscriber_connection* conn, uint32_t cause)
Holger Freyther7c19f742009-06-06 13:54:35 +0000257{
Harald Welte4bfdfe72009-06-10 23:11:52 +0800258 struct gsm_trans *trans, *temp;
Holger Freyther7c19f742009-06-06 13:54:35 +0000259 /*
260 * Cancel any outstanding location updating request
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800261 * operation taking place on the subscriber connection.
Holger Freyther7c19f742009-06-06 13:54:35 +0000262 */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800263 release_loc_updating_req(conn);
264 release_security_operation(conn);
Holger Freyther7c19f742009-06-06 13:54:35 +0000265
Harald Welte4bfdfe72009-06-10 23:11:52 +0800266 /* Free all transactions that are associated with the released lchan */
Harald Weltedcaf5652009-07-23 18:56:43 +0200267 /* FIXME: this is not neccessarily the right thing to do, we should
268 * only set trans->lchan to NULL and wait for another lchan to be
269 * established to the same MM entity (phone/subscriber) */
Holger Hans Peter Freytherf6fb3ef2010-06-15 13:16:52 +0800270 llist_for_each_entry_safe(trans, temp, &conn->bts->network->trans_list, entry) {
271 if (trans->conn == conn)
Harald Weltedcaf5652009-07-23 18:56:43 +0200272 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800273 }
Holger Freyther7c19f742009-06-06 13:54:35 +0000274}
275
Holger Freyther429e7762008-12-30 13:28:30 +0000276/* Chapter 9.2.14 : Send LOCATION UPDATING REJECT */
Harald Welte8470bf22008-12-25 23:28:35 +0000277int gsm0408_loc_upd_rej(struct gsm_lchan *lchan, u_int8_t cause)
Harald Welte52b1f982008-12-23 20:25:15 +0000278{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100279 struct gsm_subscriber_connection *conn;
Harald Welte24ff6ee2009-12-22 00:41:05 +0100280 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000281 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000282 struct gsm48_hdr *gh;
283
Harald Welte8470bf22008-12-25 23:28:35 +0000284 msg->lchan = lchan;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100285 conn = &lchan->conn;
Harald Welte52b1f982008-12-23 20:25:15 +0000286
287 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
288 gh->proto_discr = GSM48_PDISC_MM;
Harald Welte10b487b2008-12-27 19:53:37 +0000289 gh->msg_type = GSM48_MT_MM_LOC_UPD_REJECT;
Harald Welte52b1f982008-12-23 20:25:15 +0000290 gh->data[0] = cause;
291
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100292 LOGP(DMM, LOGL_INFO, "Subscriber %s: LOCATION UPDATING REJECT "
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100293 "LAC=%u BTS=%u\n", conn->subscr ?
294 subscr_name(conn->subscr) : "unknown",
Harald Welte (local)198d5ad2009-12-26 22:15:28 +0100295 lchan->ts->trx->bts->location_area_code, lchan->ts->trx->bts->nr);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100296
Harald Welteffa55a42009-12-22 19:07:32 +0100297 counter_inc(bts->network->stats.loc_upd_resp.reject);
Harald Weltedb253af2008-12-30 17:56:55 +0000298
Harald Welte39e2ead2009-07-23 21:13:03 +0200299 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000300}
301
302/* Chapter 9.2.13 : Send LOCATION UPDATE ACCEPT */
Harald Welte75a983f2008-12-27 21:34:06 +0000303int gsm0408_loc_upd_acc(struct gsm_lchan *lchan, u_int32_t tmsi)
Harald Welte52b1f982008-12-23 20:25:15 +0000304{
Harald Welte8470bf22008-12-25 23:28:35 +0000305 struct gsm_bts *bts = lchan->ts->trx->bts;
306 struct msgb *msg = gsm48_msgb_alloc();
Harald Welte52b1f982008-12-23 20:25:15 +0000307 struct gsm48_hdr *gh;
308 struct gsm48_loc_area_id *lai;
309 u_int8_t *mid;
310
Harald Welte8470bf22008-12-25 23:28:35 +0000311 msg->lchan = lchan;
Harald Welte52b1f982008-12-23 20:25:15 +0000312
313 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
314 gh->proto_discr = GSM48_PDISC_MM;
315 gh->msg_type = GSM48_MT_MM_LOC_UPD_ACCEPT;
316
317 lai = (struct gsm48_loc_area_id *) msgb_put(msg, sizeof(*lai));
Harald Welteafedeab2010-03-04 10:55:40 +0100318 gsm48_generate_lai(lai, bts->network->country_code,
Harald Welte52b1f982008-12-23 20:25:15 +0000319 bts->network->network_code, bts->location_area_code);
320
Holger Hans Peter Freyther1494a762009-08-01 07:26:59 +0200321 mid = msgb_put(msg, GSM48_MID_TMSI_LEN);
Holger Hans Peter Freyther5d0e56f2009-08-20 08:41:24 +0200322 gsm48_generate_mid_from_tmsi(mid, tmsi);
Harald Welte52b1f982008-12-23 20:25:15 +0000323
324 DEBUGP(DMM, "-> LOCATION UPDATE ACCEPT\n");
325
Harald Welteffa55a42009-12-22 19:07:32 +0100326 counter_inc(bts->network->stats.loc_upd_resp.accept);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100327
Harald Welteb83d9382009-12-12 21:00:48 +0100328 return gsm48_sendmsg(msg, NULL);
Harald Welte52b1f982008-12-23 20:25:15 +0000329}
330
Harald Weltebf5e8df2009-02-03 12:59:45 +0000331/* Transmit Chapter 9.2.10 Identity Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000332static int mm_tx_identity_req(struct gsm_lchan *lchan, u_int8_t id_type)
333{
334 struct msgb *msg = gsm48_msgb_alloc();
335 struct gsm48_hdr *gh;
Harald Weltefc977a82008-12-27 10:19:37 +0000336
Harald Welte231ad4f2008-12-27 11:15:38 +0000337 msg->lchan = lchan;
338
339 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
340 gh->proto_discr = GSM48_PDISC_MM;
341 gh->msg_type = GSM48_MT_MM_ID_REQ;
342 gh->data[0] = id_type;
343
Harald Welte39e2ead2009-07-23 21:13:03 +0200344 return gsm48_sendmsg(msg, NULL);
Harald Welte231ad4f2008-12-27 11:15:38 +0000345}
346
Harald Welte231ad4f2008-12-27 11:15:38 +0000347
Harald Weltebf5e8df2009-02-03 12:59:45 +0000348/* Parse Chapter 9.2.11 Identity Response */
Harald Welte231ad4f2008-12-27 11:15:38 +0000349static int mm_rx_id_resp(struct msgb *msg)
350{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100351 struct gsm_subscriber_connection *conn;
Harald Welte231ad4f2008-12-27 11:15:38 +0000352 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte75a983f2008-12-27 21:34:06 +0000353 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200354 struct gsm_bts *bts = lchan->ts->trx->bts;
355 struct gsm_network *net = bts->network;
Harald Welte231ad4f2008-12-27 11:15:38 +0000356 u_int8_t mi_type = gh->data[1] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200357 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000358
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200359 gsm48_mi_to_string(mi_string, sizeof(mi_string), &gh->data[1], gh->data[0]);
Harald Welte61253062008-12-27 11:25:50 +0000360 DEBUGP(DMM, "IDENTITY RESPONSE: mi_type=0x%02x MI(%s)\n",
Harald Welte231ad4f2008-12-27 11:15:38 +0000361 mi_type, mi_string);
362
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100363 conn = &lchan->conn;
364
Harald Welte7659de12009-12-13 12:39:18 +0100365 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, gh->data);
366
Harald Welte75a983f2008-12-27 21:34:06 +0000367 switch (mi_type) {
368 case GSM_MI_TYPE_IMSI:
Jan Luebbe370b41d2009-08-12 10:19:34 +0200369 /* look up subscriber based on IMSI, create if not found */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100370 if (!conn->subscr) {
371 conn->subscr = subscr_get_by_imsi(net, mi_string);
372 if (!conn->subscr)
373 conn->subscr = db_create_subscriber(net, mi_string);
Jan Luebbeb0dfc312009-08-12 10:12:52 +0200374 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100375 if (conn->loc_operation)
376 conn->loc_operation->waiting_for_imsi = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000377 break;
378 case GSM_MI_TYPE_IMEI:
Harald Welte255539c2008-12-28 02:26:27 +0000379 case GSM_MI_TYPE_IMEISV:
Harald Welte75a983f2008-12-27 21:34:06 +0000380 /* update subscribe <-> IMEI mapping */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100381 if (conn->subscr) {
382 db_subscriber_assoc_imei(conn->subscr, mi_string);
383 db_sync_equipment(&conn->subscr->equipment);
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200384 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100385 if (conn->loc_operation)
386 conn->loc_operation->waiting_for_imei = 0;
Harald Welte75a983f2008-12-27 21:34:06 +0000387 break;
388 }
Holger Freyther73487a22008-12-31 18:53:57 +0000389
390 /* Check if we can let the mobile station enter */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100391 return gsm0408_authorize(conn, msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000392}
393
Harald Welte255539c2008-12-28 02:26:27 +0000394
395static void loc_upd_rej_cb(void *data)
396{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100397 struct gsm_subscriber_connection *conn = data;
398 struct gsm_lchan *lchan = conn->lchan;
Harald Welte1085c092009-11-18 20:33:19 +0100399 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte255539c2008-12-28 02:26:27 +0000400
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100401 release_loc_updating_req(conn);
Harald Welte1085c092009-11-18 20:33:19 +0100402 gsm0408_loc_upd_rej(lchan, bts->network->reject_cause);
Holger Freyther67b4b9a2009-01-01 03:46:11 +0000403 lchan_auto_release(lchan);
Harald Welte255539c2008-12-28 02:26:27 +0000404}
405
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100406static void schedule_reject(struct gsm_subscriber_connection *conn)
Holger Freytherb7193e42008-12-29 17:44:08 +0000407{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100408 conn->loc_operation->updating_timer.cb = loc_upd_rej_cb;
409 conn->loc_operation->updating_timer.data = conn;
410 bsc_schedule_timer(&conn->loc_operation->updating_timer, 5, 0);
Holger Freytherb7193e42008-12-29 17:44:08 +0000411}
412
Harald Welte2a139372009-02-22 21:14:55 +0000413static const char *lupd_name(u_int8_t type)
414{
415 switch (type) {
416 case GSM48_LUPD_NORMAL:
417 return "NORMAL";
418 case GSM48_LUPD_PERIODIC:
419 return "PEROIDOC";
420 case GSM48_LUPD_IMSI_ATT:
421 return "IMSI ATTACH";
422 default:
423 return "UNKNOWN";
424 }
425}
426
Harald Weltebf5e8df2009-02-03 12:59:45 +0000427/* Chapter 9.2.15: Receive Location Updating Request */
Harald Welte231ad4f2008-12-27 11:15:38 +0000428static int mm_rx_loc_upd_req(struct msgb *msg)
Harald Welte52b1f982008-12-23 20:25:15 +0000429{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100430 struct gsm_subscriber_connection *conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000431 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000432 struct gsm48_loc_upd_req *lu;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800433 struct gsm_subscriber *subscr = NULL;
Harald Welte255539c2008-12-28 02:26:27 +0000434 struct gsm_lchan *lchan = msg->lchan;
Harald Welte9176bd42009-07-23 18:46:00 +0200435 struct gsm_bts *bts = lchan->ts->trx->bts;
Harald Welte8470bf22008-12-25 23:28:35 +0000436 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200437 char mi_string[GSM48_MI_SIZE];
Harald Welte231ad4f2008-12-27 11:15:38 +0000438 int rc;
Harald Welte52b1f982008-12-23 20:25:15 +0000439
Harald Welte8470bf22008-12-25 23:28:35 +0000440 lu = (struct gsm48_loc_upd_req *) gh->data;
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100441 conn = &lchan->conn;
Harald Welte8470bf22008-12-25 23:28:35 +0000442
443 mi_type = lu->mi[0] & GSM_MI_TYPE_MASK;
Harald Welte52b1f982008-12-23 20:25:15 +0000444
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200445 gsm48_mi_to_string(mi_string, sizeof(mi_string), lu->mi, lu->mi_len);
Harald Weltefc977a82008-12-27 10:19:37 +0000446
Harald Weltea0368542009-06-27 02:58:43 +0200447 DEBUGPC(DMM, "mi_type=0x%02x MI(%s) type=%s ", mi_type, mi_string,
Harald Welte2a139372009-02-22 21:14:55 +0000448 lupd_name(lu->type));
Holger Freyther73487a22008-12-31 18:53:57 +0000449
Harald Welte7659de12009-12-13 12:39:18 +0100450 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, &lu->mi_len);
451
Harald Welte24ff6ee2009-12-22 00:41:05 +0100452 switch (lu->type) {
453 case GSM48_LUPD_NORMAL:
Harald Welteffa55a42009-12-22 19:07:32 +0100454 counter_inc(bts->network->stats.loc_upd_type.normal);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100455 break;
456 case GSM48_LUPD_IMSI_ATT:
Harald Welteffa55a42009-12-22 19:07:32 +0100457 counter_inc(bts->network->stats.loc_upd_type.attach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100458 break;
459 case GSM48_LUPD_PERIODIC:
Harald Welteffa55a42009-12-22 19:07:32 +0100460 counter_inc(bts->network->stats.loc_upd_type.periodic);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100461 break;
462 }
463
Holger Freythereaf04692009-06-06 13:54:44 +0000464 /*
465 * Pseudo Spoof detection: Just drop a second/concurrent
466 * location updating request.
467 */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100468 if (conn->loc_operation) {
Harald Weltea0368542009-06-27 02:58:43 +0200469 DEBUGPC(DMM, "ignoring request due an existing one: %p.\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100470 conn->loc_operation);
Holger Freythereaf04692009-06-06 13:54:44 +0000471 gsm0408_loc_upd_rej(lchan, GSM48_REJECT_PROTOCOL_ERROR);
472 return 0;
473 }
474
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100475 allocate_loc_updating_req(&lchan->conn);
Holger Freyther73487a22008-12-31 18:53:57 +0000476
Sylvain Munaut2030a2a2010-06-10 13:36:59 +0200477 conn->loc_operation->key_seq = lu->key_seq;
478
Harald Welte52b1f982008-12-23 20:25:15 +0000479 switch (mi_type) {
480 case GSM_MI_TYPE_IMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200481 DEBUGPC(DMM, "\n");
Harald Welte231ad4f2008-12-27 11:15:38 +0000482 /* we always want the IMEI, too */
Harald Welte015b9ad2009-02-28 18:22:03 +0000483 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100484 conn->loc_operation->waiting_for_imei = 1;
Holger Freytherc6ea9db2008-12-30 19:18:21 +0000485
Jan Luebbe370b41d2009-08-12 10:19:34 +0200486 /* look up subscriber based on IMSI, create if not found */
487 subscr = subscr_get_by_imsi(bts->network, mi_string);
488 if (!subscr) {
489 subscr = db_create_subscriber(bts->network, mi_string);
490 }
Harald Welte4b634542008-12-27 01:55:51 +0000491 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000492 case GSM_MI_TYPE_TMSI:
Harald Weltea0368542009-06-27 02:58:43 +0200493 DEBUGPC(DMM, "\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000494 /* look up the subscriber based on TMSI, request IMSI if it fails */
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200495 subscr = subscr_get_by_tmsi(bts->network,
496 tmsi_from_string(mi_string));
Harald Welte52b1f982008-12-23 20:25:15 +0000497 if (!subscr) {
Harald Welte231ad4f2008-12-27 11:15:38 +0000498 /* send IDENTITY REQUEST message to get IMSI */
Harald Welte255539c2008-12-28 02:26:27 +0000499 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMSI);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100500 conn->loc_operation->waiting_for_imsi = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000501 }
Harald Weltef5f512c2010-06-07 17:56:32 +0200502 /* we always want the IMEI, too */
503 rc = mm_tx_identity_req(lchan, GSM_MI_TYPE_IMEI);
504 conn->loc_operation->waiting_for_imei = 1;
Harald Welte52b1f982008-12-23 20:25:15 +0000505 break;
506 case GSM_MI_TYPE_IMEI:
507 case GSM_MI_TYPE_IMEISV:
508 /* no sim card... FIXME: what to do ? */
Harald Weltea0368542009-06-27 02:58:43 +0200509 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000510 break;
511 default:
Harald Weltea0368542009-06-27 02:58:43 +0200512 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte52b1f982008-12-23 20:25:15 +0000513 break;
514 }
515
Harald Welte24516ea2009-07-04 10:18:00 +0200516 /* schedule the reject timer */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100517 schedule_reject(conn);
Harald Welte24516ea2009-07-04 10:18:00 +0200518
Harald Welte4bfdfe72009-06-10 23:11:52 +0800519 if (!subscr) {
Harald Weltea0368542009-06-27 02:58:43 +0200520 DEBUGPC(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte4bfdfe72009-06-10 23:11:52 +0800521 /* FIXME: request id? close channel? */
522 return -EINVAL;
523 }
524
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100525 conn->subscr = subscr;
526 conn->subscr->equipment.classmark1 = lu->classmark1;
Harald Welte255539c2008-12-28 02:26:27 +0000527
Harald Welte24516ea2009-07-04 10:18:00 +0200528 /* check if we can let the subscriber into our network immediately
529 * or if we need to wait for identity responses. */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100530 return gsm0408_authorize(conn, msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000531}
532
Harald Welte4bfdfe72009-06-10 23:11:52 +0800533#if 0
534static u_int8_t to_bcd8(u_int8_t val)
535{
536 return ((val / 10) << 4) | (val % 10);
537}
538#endif
539
Harald Weltedb253af2008-12-30 17:56:55 +0000540/* Section 9.2.15a */
541int gsm48_tx_mm_info(struct gsm_lchan *lchan)
542{
543 struct msgb *msg = gsm48_msgb_alloc();
544 struct gsm48_hdr *gh;
545 struct gsm_network *net = lchan->ts->trx->bts->network;
Harald Weltedb253af2008-12-30 17:56:55 +0000546 u_int8_t *ptr8;
Daniel Willmanneea93372009-08-13 03:42:07 +0200547 int name_len, name_pad;
Harald Welte4bfdfe72009-06-10 23:11:52 +0800548#if 0
549 time_t cur_t;
550 struct tm* cur_time;
551 int tz15min;
552#endif
Harald Weltedb253af2008-12-30 17:56:55 +0000553
554 msg->lchan = lchan;
555
556 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
557 gh->proto_discr = GSM48_PDISC_MM;
558 gh->msg_type = GSM48_MT_MM_INFO;
559
560 if (net->name_long) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200561#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000562 name_len = strlen(net->name_long);
563 /* 10.5.3.5a */
564 ptr8 = msgb_put(msg, 3);
565 ptr8[0] = GSM48_IE_NAME_LONG;
566 ptr8[1] = name_len*2 +1;
567 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
568
569 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
570 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000571 ptr16[i] = htons(net->name_long[i]);
Harald Weltedb253af2008-12-30 17:56:55 +0000572
573 /* FIXME: Use Cell Broadcast, not UCS-2, since
574 * UCS-2 is only supported by later revisions of the spec */
Daniel Willmanneea93372009-08-13 03:42:07 +0200575#endif
576 name_len = (strlen(net->name_long)*7)/8;
577 name_pad = (8 - strlen(net->name_long)*7)%8;
578 if (name_pad > 0)
579 name_len++;
580 /* 10.5.3.5a */
581 ptr8 = msgb_put(msg, 3);
582 ptr8[0] = GSM48_IE_NAME_LONG;
583 ptr8[1] = name_len +1;
584 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
585
586 ptr8 = msgb_put(msg, name_len);
587 gsm_7bit_encode(ptr8, net->name_long);
588
Harald Weltedb253af2008-12-30 17:56:55 +0000589 }
590
591 if (net->name_short) {
Daniel Willmanneea93372009-08-13 03:42:07 +0200592#if 0
Harald Weltedb253af2008-12-30 17:56:55 +0000593 name_len = strlen(net->name_short);
594 /* 10.5.3.5a */
595 ptr8 = (u_int8_t *) msgb_put(msg, 3);
Harald Welte7543eb72009-07-19 17:51:36 +0200596 ptr8[0] = GSM48_IE_NAME_SHORT;
Harald Weltedb253af2008-12-30 17:56:55 +0000597 ptr8[1] = name_len*2 + 1;
598 ptr8[2] = 0x90; /* UCS2, no spare bits, no CI */
599
Harald Weltee872cb12009-01-01 00:33:37 +0000600 ptr16 = (u_int16_t *) msgb_put(msg, name_len*2);
Harald Weltedb253af2008-12-30 17:56:55 +0000601 for (i = 0; i < name_len; i++)
Harald Welte179f0642008-12-31 23:59:18 +0000602 ptr16[i] = htons(net->name_short[i]);
Daniel Willmanneea93372009-08-13 03:42:07 +0200603#endif
604 name_len = (strlen(net->name_short)*7)/8;
605 name_pad = (8 - strlen(net->name_short)*7)%8;
606 if (name_pad > 0)
607 name_len++;
608 /* 10.5.3.5a */
609 ptr8 = (u_int8_t *) msgb_put(msg, 3);
610 ptr8[0] = GSM48_IE_NAME_SHORT;
611 ptr8[1] = name_len +1;
612 ptr8[2] = 0x80 | name_pad; /* Cell Broadcast DCS, no CI */
613
614 ptr8 = msgb_put(msg, name_len);
615 gsm_7bit_encode(ptr8, net->name_short);
616
Harald Weltedb253af2008-12-30 17:56:55 +0000617 }
618
619#if 0
620 /* Section 10.5.3.9 */
621 cur_t = time(NULL);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800622 cur_time = gmtime(&cur_t);
Harald Weltedb253af2008-12-30 17:56:55 +0000623 ptr8 = msgb_put(msg, 8);
624 ptr8[0] = GSM48_IE_NET_TIME_TZ;
625 ptr8[1] = to_bcd8(cur_time->tm_year % 100);
626 ptr8[2] = to_bcd8(cur_time->tm_mon);
627 ptr8[3] = to_bcd8(cur_time->tm_mday);
628 ptr8[4] = to_bcd8(cur_time->tm_hour);
629 ptr8[5] = to_bcd8(cur_time->tm_min);
630 ptr8[6] = to_bcd8(cur_time->tm_sec);
631 /* 02.42: coded as BCD encoded signed value in units of 15 minutes */
632 tz15min = (cur_time->tm_gmtoff)/(60*15);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800633 ptr8[7] = to_bcd8(tz15min);
Harald Weltedb253af2008-12-30 17:56:55 +0000634 if (tz15min < 0)
Harald Welte4bfdfe72009-06-10 23:11:52 +0800635 ptr8[7] |= 0x80;
Harald Weltedb253af2008-12-30 17:56:55 +0000636#endif
637
Daniel Willmanneea93372009-08-13 03:42:07 +0200638 DEBUGP(DMM, "-> MM INFO\n");
639
Harald Welte39e2ead2009-07-23 21:13:03 +0200640 return gsm48_sendmsg(msg, NULL);
Harald Weltedb253af2008-12-30 17:56:55 +0000641}
642
Harald Welte7984d5c2009-08-12 22:56:50 +0200643/* Section 9.2.2 */
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100644int gsm48_tx_mm_auth_req(struct gsm_lchan *lchan, u_int8_t *rand, int key_seq)
Harald Welte7984d5c2009-08-12 22:56:50 +0200645{
646 struct msgb *msg = gsm48_msgb_alloc();
647 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Sylvain Munaut849f5542009-09-27 11:10:17 +0200648 struct gsm48_auth_req *ar = (struct gsm48_auth_req *) msgb_put(msg, sizeof(*ar));
Harald Welte7984d5c2009-08-12 22:56:50 +0200649
Sylvain Munaut8608e7c2009-12-24 00:24:29 +0100650 DEBUGP(DMM, "-> AUTH REQ (rand = %s)\n", hexdump(rand, 16));
Harald Welte7984d5c2009-08-12 22:56:50 +0200651
652 msg->lchan = lchan;
653 gh->proto_discr = GSM48_PDISC_MM;
654 gh->msg_type = GSM48_MT_MM_AUTH_REQ;
655
Sylvain Munautbd55a6d2009-12-24 00:23:46 +0100656 ar->key_seq = key_seq;
Sylvain Munaut849f5542009-09-27 11:10:17 +0200657
Harald Welte7984d5c2009-08-12 22:56:50 +0200658 /* 16 bytes RAND parameters */
Harald Welte7984d5c2009-08-12 22:56:50 +0200659 if (rand)
Sylvain Munaut849f5542009-09-27 11:10:17 +0200660 memcpy(ar->rand, rand, 16);
Harald Welte7984d5c2009-08-12 22:56:50 +0200661
662 return gsm48_sendmsg(msg, NULL);
663}
664
665/* Section 9.2.1 */
666int gsm48_tx_mm_auth_rej(struct gsm_lchan *lchan)
667{
668 DEBUGP(DMM, "-> AUTH REJECT\n");
669 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_AUTH_REJ);
670}
671
Harald Welte4b634542008-12-27 01:55:51 +0000672static int gsm48_tx_mm_serv_ack(struct gsm_lchan *lchan)
673{
Harald Welte4b634542008-12-27 01:55:51 +0000674 DEBUGP(DMM, "-> CM SERVICE ACK\n");
Harald Welte65e74cc2008-12-29 01:55:35 +0000675 return gsm48_tx_simple(lchan, GSM48_PDISC_MM, GSM48_MT_MM_CM_SERV_ACC);
Harald Welte4b634542008-12-27 01:55:51 +0000676}
Harald Welteba4cf162009-01-10 01:49:35 +0000677
678/* 9.2.6 CM service reject */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100679static int gsm48_tx_mm_serv_rej(struct gsm_subscriber_connection *conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000680 enum gsm48_reject_value value)
681{
682 struct msgb *msg = gsm48_msgb_alloc();
683 struct gsm48_hdr *gh;
684
685 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 1);
686
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100687 msg->lchan = conn->lchan;
688 use_subscr_con(conn);
Harald Welteba4cf162009-01-10 01:49:35 +0000689
690 gh->proto_discr = GSM48_PDISC_MM;
691 gh->msg_type = GSM48_MT_MM_CM_SERV_REJ;
692 gh->data[0] = value;
693 DEBUGP(DMM, "-> CM SERVICE Reject cause: %d\n", value);
694
Harald Welte39e2ead2009-07-23 21:13:03 +0200695 return gsm48_sendmsg(msg, NULL);
Harald Welteba4cf162009-01-10 01:49:35 +0000696}
697
Sylvain Munautba87f452009-12-24 00:28:46 +0100698static int _gsm48_rx_mm_serv_req_sec_cb(
699 unsigned int hooknum, unsigned int event,
700 struct msgb *msg, void *data, void *param)
701{
702 struct gsm_lchan *lchan = data;
703 int rc = 0;
704
705 switch (event) {
706 case GSM_SECURITY_AUTH_FAILED:
707 /* Nothing to do */
708 break;
709
710 case GSM_SECURITY_NOAVAIL:
711 rc = gsm48_tx_mm_serv_ack(lchan);
712 break;
713
714 case GSM_SECURITY_SUCCEEDED:
715 /* nothing to do. CIPHER MODE COMMAND is
716 * implicit CM SERV ACK */
717 break;
718
719 default:
720 rc = -EINVAL;
721 };
722
723 return rc;
724}
725
Harald Welte4ed0e922009-01-10 03:17:30 +0000726/*
727 * Handle CM Service Requests
728 * a) Verify that the packet is long enough to contain the information
729 * we require otherwsie reject with INCORRECT_MESSAGE
730 * b) Try to parse the TMSI. If we do not have one reject
731 * c) Check that we know the subscriber with the TMSI otherwise reject
732 * with a HLR cause
733 * d) Set the subscriber on the gsm_lchan and accept
734 */
Harald Welte4b634542008-12-27 01:55:51 +0000735static int gsm48_rx_mm_serv_req(struct msgb *msg)
736{
Harald Welteba4cf162009-01-10 01:49:35 +0000737 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200738 char mi_string[GSM48_MI_SIZE];
Harald Welte4b634542008-12-27 01:55:51 +0000739
Harald Welte9176bd42009-07-23 18:46:00 +0200740 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welteba4cf162009-01-10 01:49:35 +0000741 struct gsm_subscriber *subscr;
742 struct gsm48_hdr *gh = msgb_l3(msg);
743 struct gsm48_service_request *req =
744 (struct gsm48_service_request *)gh->data;
Holger Hans Peter Freyther5d658062010-05-14 08:02:08 +0800745 /* unfortunately in Phase1 the classmark2 length is variable */
Harald Weltec9e02182009-05-01 19:07:53 +0000746 u_int8_t classmark2_len = gh->data[1];
747 u_int8_t *classmark2 = gh->data+2;
748 u_int8_t mi_len = *(classmark2 + classmark2_len);
749 u_int8_t *mi = (classmark2 + classmark2_len + 1);
Harald Welteba4cf162009-01-10 01:49:35 +0000750
Harald Weltec9e02182009-05-01 19:07:53 +0000751 DEBUGP(DMM, "<- CM SERVICE REQUEST ");
Harald Welteba4cf162009-01-10 01:49:35 +0000752 if (msg->data_len < sizeof(struct gsm48_service_request*)) {
Harald Weltec9e02182009-05-01 19:07:53 +0000753 DEBUGPC(DMM, "wrong sized message\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100754 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000755 GSM48_REJECT_INCORRECT_MESSAGE);
756 }
757
758 if (msg->data_len < req->mi_len + 6) {
Harald Weltec9e02182009-05-01 19:07:53 +0000759 DEBUGPC(DMM, "does not fit in packet\n");
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100760 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000761 GSM48_REJECT_INCORRECT_MESSAGE);
762 }
763
Harald Weltec9e02182009-05-01 19:07:53 +0000764 mi_type = mi[0] & GSM_MI_TYPE_MASK;
Harald Welteba4cf162009-01-10 01:49:35 +0000765 if (mi_type != GSM_MI_TYPE_TMSI) {
Harald Weltec9e02182009-05-01 19:07:53 +0000766 DEBUGPC(DMM, "mi_type is not TMSI: %d\n", mi_type);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100767 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welteba4cf162009-01-10 01:49:35 +0000768 GSM48_REJECT_INCORRECT_MESSAGE);
769 }
770
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200771 gsm48_mi_to_string(mi_string, sizeof(mi_string), mi, mi_len);
Harald Weltec9e02182009-05-01 19:07:53 +0000772 DEBUGPC(DMM, "serv_type=0x%02x mi_type=0x%02x M(%s)\n",
Harald Welte4ed0e922009-01-10 03:17:30 +0000773 req->cm_service_type, mi_type, mi_string);
Harald Weltebcae43f2008-12-27 21:45:37 +0000774
Harald Welte7659de12009-12-13 12:39:18 +0100775 dispatch_signal(SS_SUBSCR, S_SUBSCR_IDENTITY, (classmark2 + classmark2_len));
776
Harald Welte3ac7f102009-08-10 10:12:45 +0200777 if (is_siemens_bts(bts))
778 send_siemens_mrpci(msg->lchan, classmark2-1);
779
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200780 subscr = subscr_get_by_tmsi(bts->network,
781 tmsi_from_string(mi_string));
Holger Freythereb443982009-06-04 13:58:42 +0000782
Harald Welte2a139372009-02-22 21:14:55 +0000783 /* FIXME: if we don't know the TMSI, inquire abit IMSI and allocate new TMSI */
Harald Welte4ed0e922009-01-10 03:17:30 +0000784 if (!subscr)
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100785 return gsm48_tx_mm_serv_rej(&msg->lchan->conn,
Harald Welte4ed0e922009-01-10 03:17:30 +0000786 GSM48_REJECT_IMSI_UNKNOWN_IN_HLR);
787
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100788 if (!msg->lchan->conn.subscr)
789 msg->lchan->conn.subscr = subscr;
790 else if (msg->lchan->conn.subscr == subscr)
Sylvain Munautea3f6742009-12-18 18:28:10 +0100791 subscr_put(subscr); /* lchan already has a ref, don't need another one */
792 else {
Harald Welte9bb7c702009-01-10 03:21:41 +0000793 DEBUGP(DMM, "<- CM Channel already owned by someone else?\n");
794 subscr_put(subscr);
795 }
796
Harald Weltec2e302d2009-07-05 14:08:13 +0200797 subscr->equipment.classmark2_len = classmark2_len;
798 memcpy(subscr->equipment.classmark2, classmark2, classmark2_len);
799 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000800
Sylvain Munautba87f452009-12-24 00:28:46 +0100801 return gsm48_secure_channel(msg->lchan, req->cipher_key_seq,
802 _gsm48_rx_mm_serv_req_sec_cb, NULL);
Harald Welte4b634542008-12-27 01:55:51 +0000803}
804
Harald Welte2a139372009-02-22 21:14:55 +0000805static int gsm48_rx_mm_imsi_detach_ind(struct msgb *msg)
806{
Harald Welte9176bd42009-07-23 18:46:00 +0200807 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2a139372009-02-22 21:14:55 +0000808 struct gsm48_hdr *gh = msgb_l3(msg);
809 struct gsm48_imsi_detach_ind *idi =
810 (struct gsm48_imsi_detach_ind *) gh->data;
811 u_int8_t mi_type = idi->mi[0] & GSM_MI_TYPE_MASK;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200812 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800813 struct gsm_subscriber *subscr = NULL;
Harald Welte2a139372009-02-22 21:14:55 +0000814
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200815 gsm48_mi_to_string(mi_string, sizeof(mi_string), idi->mi, idi->mi_len);
Harald Welte2a139372009-02-22 21:14:55 +0000816 DEBUGP(DMM, "IMSI DETACH INDICATION: mi_type=0x%02x MI(%s): ",
817 mi_type, mi_string);
818
Harald Welteffa55a42009-12-22 19:07:32 +0100819 counter_inc(bts->network->stats.loc_upd_type.detach);
Harald Welte24ff6ee2009-12-22 00:41:05 +0100820
Harald Welte2a139372009-02-22 21:14:55 +0000821 switch (mi_type) {
822 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200823 subscr = subscr_get_by_tmsi(bts->network,
824 tmsi_from_string(mi_string));
Harald Welte2a139372009-02-22 21:14:55 +0000825 break;
826 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200827 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Welte2a139372009-02-22 21:14:55 +0000828 break;
829 case GSM_MI_TYPE_IMEI:
830 case GSM_MI_TYPE_IMEISV:
831 /* no sim card... FIXME: what to do ? */
Holger Freyther79f4ae62009-06-02 03:25:04 +0000832 DEBUGPC(DMM, "unimplemented mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000833 break;
834 default:
Holger Freyther79f4ae62009-06-02 03:25:04 +0000835 DEBUGPC(DMM, "unknown mobile identity type\n");
Harald Welte2a139372009-02-22 21:14:55 +0000836 break;
837 }
838
Holger Freyther4a49e772009-04-12 05:37:29 +0000839 if (subscr) {
840 subscr_update(subscr, msg->trx->bts,
841 GSM_SUBSCRIBER_UPDATE_DETACHED);
Harald Welte2e6d4682009-12-24 14:50:24 +0100842 DEBUGP(DMM, "Subscriber: %s\n", subscr_name(subscr));
Harald Welte (local)ee4410a2009-08-17 09:39:55 +0200843
844 subscr->equipment.classmark1 = idi->classmark1;
845 db_sync_equipment(&subscr->equipment);
846
Holger Freytherc21cfbc2009-06-02 02:54:57 +0000847 subscr_put(subscr);
Holger Freyther4a49e772009-04-12 05:37:29 +0000848 } else
Harald Welte2a139372009-02-22 21:14:55 +0000849 DEBUGP(DMM, "Unknown Subscriber ?!?\n");
850
Harald Welte31981a02009-12-20 09:58:40 +0100851 /* FIXME: iterate over all transactions and release them,
852 * imagine an IMSI DETACH happening during an active call! */
853
Harald Welteb83d9382009-12-12 21:00:48 +0100854 /* subscriber is detached: should we release lchan? */
Harald Weltef7c28b02009-12-21 13:30:17 +0100855 lchan_auto_release(msg->lchan);
Harald Welteb83d9382009-12-12 21:00:48 +0100856
Harald Welte2a139372009-02-22 21:14:55 +0000857 return 0;
858}
859
Harald Welted2a7f5a2009-06-05 20:08:20 +0000860static int gsm48_rx_mm_status(struct msgb *msg)
861{
862 struct gsm48_hdr *gh = msgb_l3(msg);
863
864 DEBUGP(DMM, "MM STATUS (reject cause 0x%02x)\n", gh->data[0]);
865
866 return 0;
867}
868
Sylvain Munaut30a15382009-12-24 00:27:26 +0100869/* Chapter 9.2.3: Authentication Response */
870static int gsm48_rx_mm_auth_resp(struct msgb *msg)
871{
872 struct gsm48_hdr *gh = msgb_l3(msg);
873 struct gsm48_auth_resp *ar = (struct gsm48_auth_resp*) gh->data;
874 struct gsm_lchan *lchan = msg->lchan;
875 struct gsm_subscriber_connection *conn = &msg->lchan->conn;
876 struct gsm_network *net = lchan->ts->trx->bts->network;
877
Sylvain Munautc593cf12010-06-10 23:51:41 +0200878 DEBUGP(DMM, "MM AUTHENTICATION RESPONSE (sres = %s): ",
Sylvain Munaut30a15382009-12-24 00:27:26 +0100879 hexdump(ar->sres, 4));
880
881 /* Safety check */
882 if (!conn->sec_operation) {
883 DEBUGP(DMM, "No authentication/cipher operation in progress !!!\n");
884 return -EIO;
885 }
886
887 /* Validate SRES */
888 if (memcmp(conn->sec_operation->atuple.sres, ar->sres,4)) {
889 gsm_cbfn *cb = conn->sec_operation->cb;
Sylvain Munautc593cf12010-06-10 23:51:41 +0200890
891 DEBUGPC(DMM, "Invalid (expected %s)\n",
892 hexdump(conn->sec_operation->atuple.sres, 4));
893
Sylvain Munaut30a15382009-12-24 00:27:26 +0100894 if (cb)
895 cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_AUTH_FAILED,
896 NULL, lchan, conn->sec_operation->cb_data);
897
898 release_security_operation(conn);
899 return gsm48_tx_mm_auth_rej(lchan);
900 }
901
Sylvain Munautc593cf12010-06-10 23:51:41 +0200902 DEBUGPC(DMM, "OK\n");
903
Sylvain Munaut30a15382009-12-24 00:27:26 +0100904 /* Start ciphering */
905 lchan->encr.alg_id = RSL_ENC_ALG_A5(net->a5_encryption);
906 lchan->encr.key_len = 8;
907 memcpy(msg->lchan->encr.key, conn->sec_operation->atuple.kc, 8);
908
909 return gsm48_send_rr_ciph_mode(msg->lchan, 0);
910}
911
Harald Weltebf5e8df2009-02-03 12:59:45 +0000912/* Receive a GSM 04.08 Mobility Management (MM) message */
Harald Welte52b1f982008-12-23 20:25:15 +0000913static int gsm0408_rcv_mm(struct msgb *msg)
914{
915 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +0800916 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +0000917
918 switch (gh->msg_type & 0xbf) {
919 case GSM48_MT_MM_LOC_UPD_REQUEST:
Harald Weltea0368542009-06-27 02:58:43 +0200920 DEBUGP(DMM, "LOCATION UPDATING REQUEST: ");
Harald Welte231ad4f2008-12-27 11:15:38 +0000921 rc = mm_rx_loc_upd_req(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000922 break;
923 case GSM48_MT_MM_ID_RESP:
Harald Welte231ad4f2008-12-27 11:15:38 +0000924 rc = mm_rx_id_resp(msg);
925 break;
Harald Welte52b1f982008-12-23 20:25:15 +0000926 case GSM48_MT_MM_CM_SERV_REQ:
Harald Welte4b634542008-12-27 01:55:51 +0000927 rc = gsm48_rx_mm_serv_req(msg);
928 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000929 case GSM48_MT_MM_STATUS:
Harald Welted2a7f5a2009-06-05 20:08:20 +0000930 rc = gsm48_rx_mm_status(msg);
Harald Welte231ad4f2008-12-27 11:15:38 +0000931 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000932 case GSM48_MT_MM_TMSI_REALL_COMPL:
Harald Welte69b2af22009-01-06 19:47:00 +0000933 DEBUGP(DMM, "TMSI Reallocation Completed. Subscriber: %s\n",
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +0100934 msg->lchan->conn.subscr ?
935 subscr_name(msg->lchan->conn.subscr) :
Harald Welte69b2af22009-01-06 19:47:00 +0000936 "unknown subscriber");
937 break;
Harald Welte231ad4f2008-12-27 11:15:38 +0000938 case GSM48_MT_MM_IMSI_DETACH_IND:
Harald Welte2a139372009-02-22 21:14:55 +0000939 rc = gsm48_rx_mm_imsi_detach_ind(msg);
940 break;
941 case GSM48_MT_MM_CM_REEST_REQ:
942 DEBUGP(DMM, "CM REESTABLISH REQUEST: Not implemented\n");
943 break;
944 case GSM48_MT_MM_AUTH_RESP:
Sylvain Munaut30a15382009-12-24 00:27:26 +0100945 rc = gsm48_rx_mm_auth_resp(msg);
Harald Welte52b1f982008-12-23 20:25:15 +0000946 break;
947 default:
Harald Welte5d24ba12009-12-24 12:13:17 +0100948 LOGP(DMM, LOGL_NOTICE, "Unknown GSM 04.08 MM msg type 0x%02x\n",
Harald Welte52b1f982008-12-23 20:25:15 +0000949 gh->msg_type);
950 break;
951 }
952
953 return rc;
954}
Harald Weltebf5e8df2009-02-03 12:59:45 +0000955
Harald Welte2d35ae62009-02-06 12:02:13 +0000956/* Receive a PAGING RESPONSE message from the MS */
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +0200957static int gsm48_rx_rr_pag_resp(struct msgb *msg)
Harald Welte2d35ae62009-02-06 12:02:13 +0000958{
Harald Welte9176bd42009-07-23 18:46:00 +0200959 struct gsm_bts *bts = msg->lchan->ts->trx->bts;
Harald Welte2d35ae62009-02-06 12:02:13 +0000960 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800961 struct gsm48_pag_resp *resp;
Harald Welte61548982009-02-22 21:26:29 +0000962 u_int8_t *classmark2_lv = gh->data + 1;
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200963 u_int8_t mi_type;
Holger Hans Peter Freytherd1862d72009-08-19 07:54:59 +0200964 char mi_string[GSM48_MI_SIZE];
Harald Welte4bfdfe72009-06-10 23:11:52 +0800965 struct gsm_subscriber *subscr = NULL;
Harald Welte2d35ae62009-02-06 12:02:13 +0000966 int rc = 0;
967
Holger Hans Peter Freytherf6903de2010-05-16 01:51:14 +0800968 resp = (struct gsm48_pag_resp *) &gh->data[0];
969 gsm48_paging_extract_mi(resp, msgb_l3len(msg) - sizeof(*gh),
970 mi_string, &mi_type);
Harald Welte2d35ae62009-02-06 12:02:13 +0000971 DEBUGP(DRR, "PAGING RESPONSE: mi_type=0x%02x MI(%s)\n",
972 mi_type, mi_string);
Harald Welte3ac7f102009-08-10 10:12:45 +0200973
Harald Weltefe18d8f2009-02-22 21:14:24 +0000974 switch (mi_type) {
975 case GSM_MI_TYPE_TMSI:
Holger Hans Peter Freyther22230252009-08-19 12:53:57 +0200976 subscr = subscr_get_by_tmsi(bts->network,
977 tmsi_from_string(mi_string));
Harald Weltefe18d8f2009-02-22 21:14:24 +0000978 break;
979 case GSM_MI_TYPE_IMSI:
Harald Welte9176bd42009-07-23 18:46:00 +0200980 subscr = subscr_get_by_imsi(bts->network, mi_string);
Harald Weltefe18d8f2009-02-22 21:14:24 +0000981 break;
982 }
Harald Welte2d35ae62009-02-06 12:02:13 +0000983
984 if (!subscr) {
985 DEBUGP(DRR, "<- Can't find any subscriber for this ID\n");
Harald Welte09e38af2009-02-16 22:52:23 +0000986 /* FIXME: request id? close channel? */
Harald Welte2d35ae62009-02-06 12:02:13 +0000987 return -EINVAL;
988 }
989 DEBUGP(DRR, "<- Channel was requested by %s\n",
Harald Welte76042182009-08-08 16:03:15 +0200990 subscr->name && strlen(subscr->name) ? subscr->name : subscr->imsi);
Holger Freyther053e09d2009-02-14 22:51:06 +0000991
Harald Weltec2e302d2009-07-05 14:08:13 +0200992 subscr->equipment.classmark2_len = *classmark2_lv;
993 memcpy(subscr->equipment.classmark2, classmark2_lv+1, *classmark2_lv);
994 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +0000995
Holger Hans Peter Freyther3ee5d3e2009-08-21 05:18:21 +0200996 rc = gsm48_handle_paging_resp(msg, subscr);
Harald Welte2d35ae62009-02-06 12:02:13 +0000997 return rc;
998}
999
Harald Weltef7c43522009-06-09 20:24:21 +00001000static int gsm48_rx_rr_classmark(struct msgb *msg)
1001{
1002 struct gsm48_hdr *gh = msgb_l3(msg);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001003 struct gsm_subscriber *subscr = msg->lchan->conn.subscr;
Harald Weltef7c43522009-06-09 20:24:21 +00001004 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1005 u_int8_t cm2_len, cm3_len = 0;
1006 u_int8_t *cm2, *cm3 = NULL;
1007
1008 DEBUGP(DRR, "CLASSMARK CHANGE ");
1009
1010 /* classmark 2 */
1011 cm2_len = gh->data[0];
1012 cm2 = &gh->data[1];
1013 DEBUGPC(DRR, "CM2(len=%u) ", cm2_len);
1014
1015 if (payload_len > cm2_len + 1) {
1016 /* we must have a classmark3 */
1017 if (gh->data[cm2_len+1] != 0x20) {
1018 DEBUGPC(DRR, "ERR CM3 TAG\n");
1019 return -EINVAL;
1020 }
1021 if (cm2_len > 3) {
1022 DEBUGPC(DRR, "CM2 too long!\n");
1023 return -EINVAL;
1024 }
1025
1026 cm3_len = gh->data[cm2_len+2];
1027 cm3 = &gh->data[cm2_len+3];
1028 if (cm3_len > 14) {
1029 DEBUGPC(DRR, "CM3 len %u too long!\n", cm3_len);
1030 return -EINVAL;
1031 }
1032 DEBUGPC(DRR, "CM3(len=%u)\n", cm3_len);
1033 }
1034 if (subscr) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001035 subscr->equipment.classmark2_len = cm2_len;
1036 memcpy(subscr->equipment.classmark2, cm2, cm2_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001037 if (cm3) {
Harald Weltec2e302d2009-07-05 14:08:13 +02001038 subscr->equipment.classmark3_len = cm3_len;
1039 memcpy(subscr->equipment.classmark3, cm3, cm3_len);
Harald Weltef7c43522009-06-09 20:24:21 +00001040 }
Harald Weltec2e302d2009-07-05 14:08:13 +02001041 db_sync_equipment(&subscr->equipment);
Harald Weltef7c43522009-06-09 20:24:21 +00001042 }
1043
Harald Weltef7c43522009-06-09 20:24:21 +00001044 return 0;
1045}
1046
Harald Weltecf5b3592009-05-01 18:28:42 +00001047static int gsm48_rx_rr_status(struct msgb *msg)
1048{
1049 struct gsm48_hdr *gh = msgb_l3(msg);
1050
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001051 DEBUGP(DRR, "STATUS rr_cause = %s\n",
Harald Weltecf5b3592009-05-01 18:28:42 +00001052 rr_cause_name(gh->data[0]));
1053
1054 return 0;
1055}
1056
Harald Weltef7c43522009-06-09 20:24:21 +00001057static int gsm48_rx_rr_meas_rep(struct msgb *msg)
1058{
Harald Welted12b0fd2009-12-15 21:36:05 +01001059 struct gsm_meas_rep *meas_rep = lchan_next_meas_rep(msg->lchan);
Harald Weltef7c43522009-06-09 20:24:21 +00001060
Harald Welte3c7dc6e2009-11-29 19:07:28 +01001061 /* This shouldn't actually end up here, as RSL treats
1062 * L3 Info of 08.58 MEASUREMENT REPORT different by calling
1063 * directly into gsm48_parse_meas_rep */
1064 DEBUGP(DMEAS, "DIRECT GSM48 MEASUREMENT REPORT ?!? ");
Harald Welted12b0fd2009-12-15 21:36:05 +01001065 gsm48_parse_meas_rep(meas_rep, msg);
Harald Weltef7c43522009-06-09 20:24:21 +00001066
1067 return 0;
1068}
1069
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001070static int gsm48_rx_rr_app_info(struct msgb *msg)
1071{
1072 struct gsm48_hdr *gh = msgb_l3(msg);
1073 u_int8_t apdu_id_flags;
1074 u_int8_t apdu_len;
1075 u_int8_t *apdu_data;
1076
1077 apdu_id_flags = gh->data[0];
1078 apdu_len = gh->data[1];
1079 apdu_data = gh->data+2;
1080
1081 DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
1082 apdu_id_flags, apdu_len, hexdump(apdu_data, apdu_len));
1083
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01001084 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 +02001085}
1086
Sylvain Munaut30a15382009-12-24 00:27:26 +01001087/* Chapter 9.1.10 Ciphering Mode Complete */
1088static int gsm48_rx_rr_ciph_m_compl(struct msgb *msg)
1089{
1090 struct gsm_lchan *lchan = msg->lchan;
1091 struct gsm_subscriber_connection *conn = &lchan->conn;
1092 gsm_cbfn *cb;
1093 int rc = 0;
1094
1095 DEBUGP(DRR, "CIPHERING MODE COMPLETE\n");
1096
1097 /* Safety check */
1098 if (!conn->sec_operation) {
1099 DEBUGP(DRR, "No authentication/cipher operation in progress !!!\n");
1100 return -EIO;
1101 }
1102
1103 /* FIXME: check for MI (if any) */
1104
1105 /* Call back whatever was in progress (if anything) ... */
1106 cb = conn->sec_operation->cb;
1107 if (cb) {
1108 rc = cb(GSM_HOOK_RR_SECURITY, GSM_SECURITY_SUCCEEDED,
1109 NULL, lchan, conn->sec_operation->cb_data);
1110 }
1111
1112 /* Complete the operation */
1113 release_security_operation(conn);
1114
1115 return rc;
1116}
1117
Harald Welted011e8b2009-11-29 22:45:52 +01001118/* Chapter 9.1.16 Handover complete */
Harald Welte1e191c52009-12-14 17:51:15 +01001119static int gsm48_rx_rr_ho_compl(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001120{
1121 struct gsm48_hdr *gh = msgb_l3(msg);
1122
1123 DEBUGP(DRR, "HANDOVER COMPLETE cause = %s\n",
1124 rr_cause_name(gh->data[0]));
1125
1126 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_COMPL, msg->lchan);
1127 /* FIXME: release old channel */
1128
1129 return 0;
1130}
1131
1132/* Chapter 9.1.17 Handover Failure */
Harald Welte1e191c52009-12-14 17:51:15 +01001133static int gsm48_rx_rr_ho_fail(struct msgb *msg)
Harald Welted011e8b2009-11-29 22:45:52 +01001134{
1135 struct gsm48_hdr *gh = msgb_l3(msg);
1136
1137 DEBUGP(DRR, "HANDOVER FAILED cause = %s\n",
1138 rr_cause_name(gh->data[0]));
1139
1140 dispatch_signal(SS_LCHAN, S_LCHAN_HANDOVER_FAIL, msg->lchan);
1141 /* FIXME: release allocated new channel */
1142
1143 return 0;
1144}
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001145
Harald Weltebf5e8df2009-02-03 12:59:45 +00001146/* Receive a GSM 04.08 Radio Resource (RR) message */
Harald Welte52b1f982008-12-23 20:25:15 +00001147static int gsm0408_rcv_rr(struct msgb *msg)
1148{
1149 struct gsm48_hdr *gh = msgb_l3(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001150 int rc = 0;
Harald Welte52b1f982008-12-23 20:25:15 +00001151
1152 switch (gh->msg_type) {
1153 case GSM48_MT_RR_CLSM_CHG:
Harald Weltef7c43522009-06-09 20:24:21 +00001154 rc = gsm48_rx_rr_classmark(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00001155 break;
Harald Weltefc977a82008-12-27 10:19:37 +00001156 case GSM48_MT_RR_GPRS_SUSP_REQ:
1157 DEBUGP(DRR, "GRPS SUSPEND REQUEST\n");
1158 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001159 case GSM48_MT_RR_PAG_RESP:
Holger Hans Peter Freytherca0fcbe2009-10-23 13:48:54 +02001160 rc = gsm48_rx_rr_pag_resp(msg);
Harald Welte2d35ae62009-02-06 12:02:13 +00001161 break;
Harald Welte7ccf7782009-02-17 01:43:01 +00001162 case GSM48_MT_RR_CHAN_MODE_MODIF_ACK:
Holger Hans Peter Freytherf520e642009-10-22 15:23:11 +02001163 rc = gsm48_rx_rr_modif_ack(msg);
Harald Welte7ccf7782009-02-17 01:43:01 +00001164 break;
Harald Weltecf5b3592009-05-01 18:28:42 +00001165 case GSM48_MT_RR_STATUS:
1166 rc = gsm48_rx_rr_status(msg);
1167 break;
Harald Weltef7c43522009-06-09 20:24:21 +00001168 case GSM48_MT_RR_MEAS_REP:
1169 rc = gsm48_rx_rr_meas_rep(msg);
1170 break;
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001171 case GSM48_MT_RR_APP_INFO:
1172 rc = gsm48_rx_rr_app_info(msg);
1173 break;
Harald Welte08d91a52009-08-30 15:37:11 +09001174 case GSM48_MT_RR_CIPH_M_COMPL:
Sylvain Munaut30a15382009-12-24 00:27:26 +01001175 rc = gsm48_rx_rr_ciph_m_compl(msg);
Harald Welte08d91a52009-08-30 15:37:11 +09001176 break;
Harald Welteccd5a882009-11-29 20:02:20 +01001177 case GSM48_MT_RR_HANDO_COMPL:
Harald Welted011e8b2009-11-29 22:45:52 +01001178 rc = gsm48_rx_rr_ho_compl(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001179 break;
1180 case GSM48_MT_RR_HANDO_FAIL:
Harald Welted011e8b2009-11-29 22:45:52 +01001181 rc = gsm48_rx_rr_ho_fail(msg);
Harald Welteccd5a882009-11-29 20:02:20 +01001182 break;
Harald Welte52b1f982008-12-23 20:25:15 +00001183 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01001184 LOGP(DRR, LOGL_NOTICE, "Unimplemented "
1185 "GSM 04.08 RR msg type 0x%02x\n", gh->msg_type);
Harald Welte52b1f982008-12-23 20:25:15 +00001186 break;
1187 }
1188
Harald Welte2d35ae62009-02-06 12:02:13 +00001189 return rc;
Harald Welte52b1f982008-12-23 20:25:15 +00001190}
1191
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001192int gsm48_send_rr_app_info(struct gsm_lchan *lchan, u_int8_t apdu_id,
Holger Hans Peter Freyther8b77a342009-10-22 15:42:19 +02001193 u_int8_t apdu_len, const u_int8_t *apdu)
Harald Welte (local)6eef5642009-08-15 23:32:44 +02001194{
1195 struct msgb *msg = gsm48_msgb_alloc();
1196 struct gsm48_hdr *gh;
1197
1198 msg->lchan = lchan;
1199
1200 DEBUGP(DRR, "TX APPLICATION INFO id=0x%02x, len=%u\n",
1201 apdu_id, apdu_len);
1202
1203 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh) + 2 + apdu_len);
1204 gh->proto_discr = GSM48_PDISC_RR;
1205 gh->msg_type = GSM48_MT_RR_APP_INFO;
1206 gh->data[0] = apdu_id;
1207 gh->data[1] = apdu_len;
1208 memcpy(gh->data+2, apdu, apdu_len);
1209
1210 return gsm48_sendmsg(msg, NULL);
1211}
1212
Harald Welte4bc90a12008-12-27 16:32:52 +00001213/* Call Control */
1214
Harald Welte7584aea2009-02-11 11:44:12 +00001215/* The entire call control code is written in accordance with Figure 7.10c
1216 * for 'very early assignment', i.e. we allocate a TCH/F during IMMEDIATE
1217 * ASSIGN, then first use that TCH/F for signalling and later MODE MODIFY
1218 * it for voice */
1219
Harald Welte4bfdfe72009-06-10 23:11:52 +08001220static void new_cc_state(struct gsm_trans *trans, int state)
1221{
1222 if (state > 31 || state < 0)
1223 return;
1224
1225 DEBUGP(DCC, "new state %s -> %s\n",
Harald Weltee95daf12010-03-25 12:13:02 +08001226 gsm48_cc_state_name(trans->cc.state),
1227 gsm48_cc_state_name(state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001228
Harald Weltedcaf5652009-07-23 18:56:43 +02001229 trans->cc.state = state;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001230}
1231
1232static int gsm48_cc_tx_status(struct gsm_trans *trans, void *arg)
Harald Welte4bc90a12008-12-27 16:32:52 +00001233{
1234 struct msgb *msg = gsm48_msgb_alloc();
1235 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1236 u_int8_t *cause, *call_state;
1237
Harald Welte4bc90a12008-12-27 16:32:52 +00001238 gh->msg_type = GSM48_MT_CC_STATUS;
1239
1240 cause = msgb_put(msg, 3);
1241 cause[0] = 2;
1242 cause[1] = GSM48_CAUSE_CS_GSM | GSM48_CAUSE_LOC_USER;
1243 cause[2] = 0x80 | 30; /* response to status inquiry */
1244
1245 call_state = msgb_put(msg, 1);
1246 call_state[0] = 0xc0 | 0x00;
1247
Harald Welte39e2ead2009-07-23 21:13:03 +02001248 return gsm48_sendmsg(msg, trans);
Harald Welte4bc90a12008-12-27 16:32:52 +00001249}
1250
Harald Welte6f4b7532008-12-29 00:39:37 +00001251static int gsm48_tx_simple(struct gsm_lchan *lchan,
1252 u_int8_t pdisc, u_int8_t msg_type)
Harald Welte4bc90a12008-12-27 16:32:52 +00001253{
1254 struct msgb *msg = gsm48_msgb_alloc();
1255 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1256
1257 msg->lchan = lchan;
1258
Harald Welte6f4b7532008-12-29 00:39:37 +00001259 gh->proto_discr = pdisc;
Harald Welte4bc90a12008-12-27 16:32:52 +00001260 gh->msg_type = msg_type;
1261
Harald Welte39e2ead2009-07-23 21:13:03 +02001262 return gsm48_sendmsg(msg, NULL);
Harald Welte4bc90a12008-12-27 16:32:52 +00001263}
1264
Harald Welte4bfdfe72009-06-10 23:11:52 +08001265static void gsm48_stop_cc_timer(struct gsm_trans *trans)
1266{
Harald Weltedcaf5652009-07-23 18:56:43 +02001267 if (bsc_timer_pending(&trans->cc.timer)) {
1268 DEBUGP(DCC, "stopping pending timer T%x\n", trans->cc.Tcurrent);
1269 bsc_del_timer(&trans->cc.timer);
1270 trans->cc.Tcurrent = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001271 }
1272}
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001273
Harald Welte4bfdfe72009-06-10 23:11:52 +08001274static int mncc_recvmsg(struct gsm_network *net, struct gsm_trans *trans,
1275 int msg_type, struct gsm_mncc *mncc)
1276{
1277 struct msgb *msg;
1278
1279 if (trans)
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001280 if (trans->conn)
Harald Welte6f5aee02009-07-23 21:21:14 +02001281 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08001282 "Sending '%s' to MNCC.\n",
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001283 trans->conn->lchan->ts->trx->bts->nr,
1284 trans->conn->lchan->ts->trx->nr,
1285 trans->conn->lchan->ts->nr, trans->transaction_id,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001286 (trans->subscr)?(trans->subscr->extension):"-",
1287 get_mncc_name(msg_type));
1288 else
1289 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
1290 "Sending '%s' to MNCC.\n",
1291 (trans->subscr)?(trans->subscr->extension):"-",
1292 get_mncc_name(msg_type));
1293 else
1294 DEBUGP(DCC, "(bts - trx - ts - ti -- sub -) "
1295 "Sending '%s' to MNCC.\n", get_mncc_name(msg_type));
1296
1297 mncc->msg_type = msg_type;
1298
Harald Welte966636f2009-06-26 19:39:35 +02001299 msg = msgb_alloc(sizeof(struct gsm_mncc), "MNCC");
Harald Welte4bfdfe72009-06-10 23:11:52 +08001300 if (!msg)
1301 return -ENOMEM;
1302 memcpy(msg->data, mncc, sizeof(struct gsm_mncc));
1303 msgb_enqueue(&net->upqueue, msg);
1304
1305 return 0;
1306}
1307
1308int mncc_release_ind(struct gsm_network *net, struct gsm_trans *trans,
1309 u_int32_t callref, int location, int value)
1310{
1311 struct gsm_mncc rel;
1312
Harald Welte92f70c52009-06-12 01:54:08 +08001313 memset(&rel, 0, sizeof(rel));
Harald Welte4bfdfe72009-06-10 23:11:52 +08001314 rel.callref = callref;
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001315 mncc_set_cause(&rel, location, value);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001316 return mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
1317}
1318
Harald Weltedcaf5652009-07-23 18:56:43 +02001319/* Call Control Specific transaction release.
1320 * gets called by trans_free, DO NOT CALL YOURSELF! */
1321void _gsm48_cc_trans_free(struct gsm_trans *trans)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001322{
Harald Welte4bfdfe72009-06-10 23:11:52 +08001323 gsm48_stop_cc_timer(trans);
1324
1325 /* send release to L4, if callref still exists */
1326 if (trans->callref) {
1327 /* Ressource unavailable */
Harald Welte596fed42009-07-23 19:06:52 +02001328 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001329 GSM48_CAUSE_LOC_PRN_S_LU,
1330 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001331 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001332 if (trans->cc.state != GSM_CSTATE_NULL)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001333 new_cc_state(trans, GSM_CSTATE_NULL);
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001334 if (trans->conn)
1335 trau_mux_unmap(&trans->conn->lchan->ts->e1_link, trans->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001336}
1337
1338static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg);
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001339
Harald Welte09e38af2009-02-16 22:52:23 +00001340/* call-back from paging the B-end of the connection */
1341static int setup_trig_pag_evt(unsigned int hooknum, unsigned int event,
Harald Welte7ccf7782009-02-17 01:43:01 +00001342 struct msgb *msg, void *_lchan, void *param)
Harald Welte09e38af2009-02-16 22:52:23 +00001343{
Harald Welte7ccf7782009-02-17 01:43:01 +00001344 struct gsm_lchan *lchan = _lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001345 struct gsm_subscriber *subscr = param;
1346 struct gsm_trans *transt, *tmp;
1347 struct gsm_network *net;
Harald Weltec05677b2009-06-26 20:17:06 +02001348
Harald Welte09e38af2009-02-16 22:52:23 +00001349 if (hooknum != GSM_HOOK_RR_PAGING)
1350 return -EINVAL;
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02001351
Harald Welte4bfdfe72009-06-10 23:11:52 +08001352 if (!subscr)
1353 return -EINVAL;
1354 net = subscr->net;
1355 if (!net) {
1356 DEBUGP(DCC, "Error Network not set!\n");
1357 return -EINVAL;
Harald Welte5a065df2009-02-22 21:13:18 +00001358 }
Harald Welte7584aea2009-02-11 11:44:12 +00001359
Harald Welte4bfdfe72009-06-10 23:11:52 +08001360 /* check all tranactions (without lchan) for subscriber */
1361 llist_for_each_entry_safe(transt, tmp, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001362 if (transt->subscr != subscr || transt->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001363 continue;
1364 switch (event) {
1365 case GSM_PAGING_SUCCEEDED:
1366 if (!lchan) // paranoid
1367 break;
1368 DEBUGP(DCC, "Paging subscr %s succeeded!\n",
1369 subscr->extension);
1370 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001371 if (!transt->conn) {
1372 transt->conn = &lchan->conn;
1373 use_subscr_con(transt->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001374 }
1375 /* send SETUP request to called party */
Harald Weltedcaf5652009-07-23 18:56:43 +02001376 gsm48_cc_tx_setup(transt, &transt->cc.msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001377 break;
1378 case GSM_PAGING_EXPIRED:
1379 DEBUGP(DCC, "Paging subscr %s expired!\n",
1380 subscr->extension);
1381 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001382 mncc_release_ind(transt->subscr->net, transt,
1383 transt->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001384 GSM48_CAUSE_LOC_PRN_S_LU,
1385 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001386 transt->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001387 trans_free(transt);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001388 break;
1389 }
1390 }
Harald Welte09e38af2009-02-16 22:52:23 +00001391 return 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00001392}
Harald Welte7584aea2009-02-11 11:44:12 +00001393
Harald Welteda7ab742009-12-19 22:23:05 +01001394static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable);
1395
Harald Welte805f6442009-07-28 18:25:29 +02001396/* some other part of the code sends us a signal */
1397static int handle_abisip_signal(unsigned int subsys, unsigned int signal,
1398 void *handler_data, void *signal_data)
1399{
1400 struct gsm_lchan *lchan = signal_data;
Harald Welte805f6442009-07-28 18:25:29 +02001401 int rc;
Harald Welteda7ab742009-12-19 22:23:05 +01001402 struct gsm_network *net;
1403 struct gsm_trans *trans;
Harald Welte805f6442009-07-28 18:25:29 +02001404
1405 if (subsys != SS_ABISIP)
1406 return 0;
1407
1408 /* in case we use direct BTS-to-BTS RTP */
1409 if (ipacc_rtp_direct)
1410 return 0;
1411
Harald Welte805f6442009-07-28 18:25:29 +02001412 switch (signal) {
Holger Hans Peter Freyther231163d2009-11-18 21:06:12 +01001413 case S_ABISIP_CRCX_ACK:
Harald Welteda7ab742009-12-19 22:23:05 +01001414 /* check if any transactions on this lchan still have
1415 * a tch_recv_mncc request pending */
1416 net = lchan->ts->trx->bts->network;
1417 llist_for_each_entry(trans, &net->trans_list, entry) {
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001418 if (trans->conn && trans->conn->lchan == lchan && trans->tch_recv) {
Harald Welteda7ab742009-12-19 22:23:05 +01001419 DEBUGP(DCC, "pending tch_recv_mncc request\n");
1420 tch_recv_mncc(net, trans->callref, 1);
1421 }
1422 }
Harald Welte805f6442009-07-28 18:25:29 +02001423 break;
Harald Welte805f6442009-07-28 18:25:29 +02001424 }
1425
1426 return 0;
Harald Welte805f6442009-07-28 18:25:29 +02001427}
1428
Harald Welte49f48b82009-02-17 15:29:33 +00001429/* map two ipaccess RTP streams onto each other */
Harald Welte11fa29c2009-02-19 17:24:39 +00001430static int tch_map(struct gsm_lchan *lchan, struct gsm_lchan *remote_lchan)
Harald Welte49f48b82009-02-17 15:29:33 +00001431{
Harald Welte11fa29c2009-02-19 17:24:39 +00001432 struct gsm_bts *bts = lchan->ts->trx->bts;
1433 struct gsm_bts *remote_bts = remote_lchan->ts->trx->bts;
Harald Welte805f6442009-07-28 18:25:29 +02001434 int rc;
Harald Welte49f48b82009-02-17 15:29:33 +00001435
Harald Welte11fa29c2009-02-19 17:24:39 +00001436 DEBUGP(DCC, "Setting up TCH map between (bts=%u,trx=%u,ts=%u) and (bts=%u,trx=%u,ts=%u)\n",
1437 bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
1438 remote_bts->nr, remote_lchan->ts->trx->nr, remote_lchan->ts->nr);
1439
1440 if (bts->type != remote_bts->type) {
1441 DEBUGP(DCC, "Cannot switch calls between different BTS types yet\n");
1442 return -EINVAL;
1443 }
Harald Welteda7ab742009-12-19 22:23:05 +01001444
1445 // todo: map between different bts types
Harald Welte11fa29c2009-02-19 17:24:39 +00001446 switch (bts->type) {
Mike Habene2d82272009-10-02 12:19:34 +01001447 case GSM_BTS_TYPE_NANOBTS:
Harald Welte805f6442009-07-28 18:25:29 +02001448 if (!ipacc_rtp_direct) {
1449 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001450 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welte805f6442009-07-28 18:25:29 +02001451 if (rc < 0)
1452 return rc;
Harald Weltea72273e2009-12-20 16:51:09 +01001453 rc = rsl_ipacc_mdcx_to_rtpsock(remote_lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001454#warning do we need a check of rc here?
Harald Welte805f6442009-07-28 18:25:29 +02001455
1456 /* connect them with each other */
Harald Welte2c828992009-12-02 01:56:49 +05301457 rtp_socket_proxy(lchan->abis_ip.rtp_socket,
1458 remote_lchan->abis_ip.rtp_socket);
Harald Welte805f6442009-07-28 18:25:29 +02001459 } else {
1460 /* directly connect TCH RTP streams to each other */
Harald Welte2c828992009-12-02 01:56:49 +05301461 rc = rsl_ipacc_mdcx(lchan, remote_lchan->abis_ip.bound_ip,
1462 remote_lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301463 remote_lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001464 if (rc < 0)
1465 return rc;
Harald Welte2c828992009-12-02 01:56:49 +05301466 rc = rsl_ipacc_mdcx(remote_lchan, lchan->abis_ip.bound_ip,
1467 lchan->abis_ip.bound_port,
Harald Welte2c828992009-12-02 01:56:49 +05301468 lchan->abis_ip.rtp_payload2);
Harald Welte805f6442009-07-28 18:25:29 +02001469 }
Harald Welte11fa29c2009-02-19 17:24:39 +00001470 break;
1471 case GSM_BTS_TYPE_BS11:
1472 trau_mux_map_lchan(lchan, remote_lchan);
1473 break;
1474 default:
1475 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
Harald Welteda7ab742009-12-19 22:23:05 +01001476 return -EINVAL;
Harald Welte11fa29c2009-02-19 17:24:39 +00001477 }
Harald Welte49f48b82009-02-17 15:29:33 +00001478
1479 return 0;
1480}
1481
Harald Welte4bfdfe72009-06-10 23:11:52 +08001482/* bridge channels of two transactions */
1483static int tch_bridge(struct gsm_network *net, u_int32_t *refs)
Harald Welte7ccf7782009-02-17 01:43:01 +00001484{
Harald Weltedcaf5652009-07-23 18:56:43 +02001485 struct gsm_trans *trans1 = trans_find_by_callref(net, refs[0]);
1486 struct gsm_trans *trans2 = trans_find_by_callref(net, refs[1]);
Harald Welte7ccf7782009-02-17 01:43:01 +00001487
Harald Welte4bfdfe72009-06-10 23:11:52 +08001488 if (!trans1 || !trans2)
Harald Welte7ccf7782009-02-17 01:43:01 +00001489 return -EIO;
1490
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001491 if (!trans1->conn || !trans2->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001492 return -EIO;
1493
1494 /* through-connect channel */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001495 return tch_map(trans1->conn->lchan, trans2->conn->lchan);
Harald Welte7ccf7782009-02-17 01:43:01 +00001496}
1497
Harald Welteda7ab742009-12-19 22:23:05 +01001498/* enable receive of channels to MNCC upqueue */
1499static int tch_recv_mncc(struct gsm_network *net, u_int32_t callref, int enable)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001500{
1501 struct gsm_trans *trans;
Harald Welteda7ab742009-12-19 22:23:05 +01001502 struct gsm_lchan *lchan;
1503 struct gsm_bts *bts;
1504 int rc;
Harald Welte7ccf7782009-02-17 01:43:01 +00001505
Harald Welte4bfdfe72009-06-10 23:11:52 +08001506 /* Find callref */
Harald Welteda7ab742009-12-19 22:23:05 +01001507 trans = trans_find_by_callref(net, callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001508 if (!trans)
1509 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001510 if (!trans->conn)
Harald Welte4bfdfe72009-06-10 23:11:52 +08001511 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01001512 lchan = trans->conn->lchan;
Harald Welteda7ab742009-12-19 22:23:05 +01001513 bts = lchan->ts->trx->bts;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001514
Harald Welteda7ab742009-12-19 22:23:05 +01001515 switch (bts->type) {
1516 case GSM_BTS_TYPE_NANOBTS:
1517 if (ipacc_rtp_direct) {
1518 DEBUGP(DCC, "Error: RTP proxy is disabled\n");
1519 return -EINVAL;
1520 }
1521 /* in case, we don't have a RTP socket yet, we note this
1522 * in the transaction and try later */
1523 if (!lchan->abis_ip.rtp_socket) {
1524 trans->tch_recv = enable;
1525 DEBUGP(DCC, "queue tch_recv_mncc request (%d)\n", enable);
1526 return 0;
1527 }
1528 if (enable) {
1529 /* connect the TCH's to our RTP proxy */
Harald Weltea72273e2009-12-20 16:51:09 +01001530 rc = rsl_ipacc_mdcx_to_rtpsock(lchan);
Harald Welteda7ab742009-12-19 22:23:05 +01001531 if (rc < 0)
1532 return rc;
1533 /* assign socket to application interface */
1534 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1535 net, callref);
1536 } else
1537 rtp_socket_upstream(lchan->abis_ip.rtp_socket,
1538 net, 0);
1539 break;
1540 case GSM_BTS_TYPE_BS11:
1541 if (enable)
1542 return trau_recv_lchan(lchan, callref);
1543 return trau_mux_unmap(NULL, callref);
1544 break;
1545 default:
1546 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
1547 return -EINVAL;
1548 }
1549
1550 return 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001551}
1552
Harald Welte4bfdfe72009-06-10 23:11:52 +08001553static int gsm48_cc_rx_status_enq(struct gsm_trans *trans, struct msgb *msg)
1554{
1555 DEBUGP(DCC, "-> STATUS ENQ\n");
1556 return gsm48_cc_tx_status(trans, msg);
1557}
1558
1559static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg);
1560static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg);
1561
1562static void gsm48_cc_timeout(void *arg)
1563{
1564 struct gsm_trans *trans = arg;
1565 int disconnect = 0, release = 0;
Harald Weltec66b71c2009-06-11 14:23:20 +08001566 int mo_cause = GSM48_CC_CAUSE_RECOVERY_TIMER;
1567 int mo_location = GSM48_CAUSE_LOC_USER;
1568 int l4_cause = GSM48_CC_CAUSE_NORMAL_UNSPEC;
1569 int l4_location = GSM48_CAUSE_LOC_PRN_S_LU;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001570 struct gsm_mncc mo_rel, l4_rel;
1571
1572 memset(&mo_rel, 0, sizeof(struct gsm_mncc));
1573 mo_rel.callref = trans->callref;
1574 memset(&l4_rel, 0, sizeof(struct gsm_mncc));
1575 l4_rel.callref = trans->callref;
1576
Harald Weltedcaf5652009-07-23 18:56:43 +02001577 switch(trans->cc.Tcurrent) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001578 case 0x303:
1579 release = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001580 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001581 break;
1582 case 0x310:
1583 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001584 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001585 break;
1586 case 0x313:
1587 disconnect = 1;
1588 /* unknown, did not find it in the specs */
1589 break;
1590 case 0x301:
1591 disconnect = 1;
Harald Weltec66b71c2009-06-11 14:23:20 +08001592 l4_cause = GSM48_CC_CAUSE_USER_NOTRESPOND;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001593 break;
1594 case 0x308:
Harald Weltedcaf5652009-07-23 18:56:43 +02001595 if (!trans->cc.T308_second) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001596 /* restart T308 a second time */
Harald Weltedcaf5652009-07-23 18:56:43 +02001597 gsm48_cc_tx_release(trans, &trans->cc.msg);
1598 trans->cc.T308_second = 1;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001599 break; /* stay in release state */
1600 }
Harald Weltedcaf5652009-07-23 18:56:43 +02001601 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001602 return;
1603// release = 1;
1604// l4_cause = 14;
1605// break;
1606 case 0x306:
1607 release = 1;
Harald Weltedcaf5652009-07-23 18:56:43 +02001608 mo_cause = trans->cc.msg.cause.value;
1609 mo_location = trans->cc.msg.cause.location;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001610 break;
1611 case 0x323:
1612 disconnect = 1;
1613 break;
1614 default:
1615 release = 1;
1616 }
1617
1618 if (release && trans->callref) {
1619 /* process release towards layer 4 */
Harald Welte596fed42009-07-23 19:06:52 +02001620 mncc_release_ind(trans->subscr->net, trans, trans->callref,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001621 l4_location, l4_cause);
1622 trans->callref = 0;
1623 }
1624
1625 if (disconnect && trans->callref) {
1626 /* process disconnect towards layer 4 */
1627 mncc_set_cause(&l4_rel, l4_location, l4_cause);
Harald Welte596fed42009-07-23 19:06:52 +02001628 mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &l4_rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001629 }
1630
1631 /* process disconnect towards mobile station */
1632 if (disconnect || release) {
1633 mncc_set_cause(&mo_rel, mo_location, mo_cause);
Harald Weltedcaf5652009-07-23 18:56:43 +02001634 mo_rel.cause.diag[0] = ((trans->cc.Tcurrent & 0xf00) >> 8) + '0';
1635 mo_rel.cause.diag[1] = ((trans->cc.Tcurrent & 0x0f0) >> 4) + '0';
1636 mo_rel.cause.diag[2] = (trans->cc.Tcurrent & 0x00f) + '0';
Harald Welte4bfdfe72009-06-10 23:11:52 +08001637 mo_rel.cause.diag_len = 3;
1638
1639 if (disconnect)
1640 gsm48_cc_tx_disconnect(trans, &mo_rel);
1641 if (release)
1642 gsm48_cc_tx_release(trans, &mo_rel);
1643 }
1644
1645}
1646
1647static void gsm48_start_cc_timer(struct gsm_trans *trans, int current,
1648 int sec, int micro)
1649{
1650 DEBUGP(DCC, "starting timer T%x with %d seconds\n", current, sec);
Harald Weltedcaf5652009-07-23 18:56:43 +02001651 trans->cc.timer.cb = gsm48_cc_timeout;
1652 trans->cc.timer.data = trans;
1653 bsc_schedule_timer(&trans->cc.timer, sec, micro);
1654 trans->cc.Tcurrent = current;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001655}
1656
1657static int gsm48_cc_rx_setup(struct gsm_trans *trans, struct msgb *msg)
1658{
1659 struct gsm48_hdr *gh = msgb_l3(msg);
1660 u_int8_t msg_type = gh->msg_type & 0xbf;
1661 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1662 struct tlv_parsed tp;
1663 struct gsm_mncc setup;
1664
1665 memset(&setup, 0, sizeof(struct gsm_mncc));
1666 setup.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001667 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001668 /* emergency setup is identified by msg_type */
1669 if (msg_type == GSM48_MT_CC_EMERG_SETUP)
1670 setup.emergency = 1;
1671
1672 /* use subscriber as calling party number */
1673 if (trans->subscr) {
1674 setup.fields |= MNCC_F_CALLING;
1675 strncpy(setup.calling.number, trans->subscr->extension,
1676 sizeof(setup.calling.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02001677 strncpy(setup.imsi, trans->subscr->imsi,
1678 sizeof(setup.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001679 }
1680 /* bearer capability */
1681 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1682 setup.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001683 gsm48_decode_bearer_cap(&setup.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001684 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1685 }
1686 /* facility */
1687 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1688 setup.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001689 gsm48_decode_facility(&setup.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001690 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1691 }
1692 /* called party bcd number */
1693 if (TLVP_PRESENT(&tp, GSM48_IE_CALLED_BCD)) {
1694 setup.fields |= MNCC_F_CALLED;
Harald Welte55c8f352010-03-07 23:40:35 +01001695 gsm48_decode_called(&setup.called,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001696 TLVP_VAL(&tp, GSM48_IE_CALLED_BCD)-1);
1697 }
1698 /* user-user */
1699 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
1700 setup.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01001701 gsm48_decode_useruser(&setup.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001702 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
1703 }
1704 /* ss-version */
1705 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1706 setup.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001707 gsm48_decode_ssversion(&setup.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001708 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1709 }
1710 /* CLIR suppression */
1711 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_SUPP))
1712 setup.clir.sup = 1;
1713 /* CLIR invocation */
1714 if (TLVP_PRESENT(&tp, GSM48_IE_CLIR_INVOC))
1715 setup.clir.inv = 1;
1716 /* cc cap */
1717 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1718 setup.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001719 gsm48_decode_cccap(&setup.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001720 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1721 }
1722
Harald Welte4bfdfe72009-06-10 23:11:52 +08001723 new_cc_state(trans, GSM_CSTATE_INITIATED);
1724
Harald Welte (local)47df3992009-12-26 19:45:03 +01001725 LOGP(DCC, LOGL_INFO, "Subscriber %s (%s) sends SETUP to %s\n",
1726 subscr_name(trans->subscr), trans->subscr->extension,
1727 setup.called.number);
1728
Harald Welte4bfdfe72009-06-10 23:11:52 +08001729 /* indicate setup to MNCC */
Harald Welte596fed42009-07-23 19:06:52 +02001730 mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_IND, &setup);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001731
Harald Welte13cac662009-07-29 12:10:35 +02001732 /* MNCC code will modify the channel asynchronously, we should
1733 * ipaccess-bind only after the modification has been made to the
1734 * lchan->tch_mode */
Harald Welte4bfdfe72009-06-10 23:11:52 +08001735 return 0;
1736}
1737
1738static int gsm48_cc_tx_setup(struct gsm_trans *trans, void *arg)
Harald Welte65e74cc2008-12-29 01:55:35 +00001739{
1740 struct msgb *msg = gsm48_msgb_alloc();
1741 struct gsm48_hdr *gh;
Harald Welte4bfdfe72009-06-10 23:11:52 +08001742 struct gsm_mncc *setup = arg;
Harald Welte78283ef2009-07-23 21:36:44 +02001743 int rc, trans_id;
Harald Welte65e74cc2008-12-29 01:55:35 +00001744
Harald Welte7ccf7782009-02-17 01:43:01 +00001745 gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
Harald Welte65e74cc2008-12-29 01:55:35 +00001746
Harald Welte4bfdfe72009-06-10 23:11:52 +08001747 /* transaction id must not be assigned */
1748 if (trans->transaction_id != 0xff) { /* unasssigned */
1749 DEBUGP(DCC, "TX Setup with assigned transaction. "
1750 "This is not allowed!\n");
1751 /* Temporarily out of order */
Harald Welte596fed42009-07-23 19:06:52 +02001752 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001753 GSM48_CAUSE_LOC_PRN_S_LU,
1754 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001755 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001756 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001757 return rc;
1758 }
1759
1760 /* Get free transaction_id */
Harald Welte78283ef2009-07-23 21:36:44 +02001761 trans_id = trans_assign_trans_id(trans->subscr, GSM48_PDISC_CC, 0);
1762 if (trans_id < 0) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08001763 /* no free transaction ID */
Harald Welte596fed42009-07-23 19:06:52 +02001764 rc = mncc_release_ind(trans->subscr->net, trans, trans->callref,
Andreas Eversberg7563ac92009-06-14 22:14:12 +08001765 GSM48_CAUSE_LOC_PRN_S_LU,
1766 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001767 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02001768 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001769 return rc;
1770 }
Harald Welte78283ef2009-07-23 21:36:44 +02001771 trans->transaction_id = trans_id;
Harald Welte49f48b82009-02-17 15:29:33 +00001772
Harald Welte65e74cc2008-12-29 01:55:35 +00001773 gh->msg_type = GSM48_MT_CC_SETUP;
Harald Welte09e38af2009-02-16 22:52:23 +00001774
Harald Welte4bfdfe72009-06-10 23:11:52 +08001775 gsm48_start_cc_timer(trans, 0x303, GSM48_T303);
Harald Welte65e74cc2008-12-29 01:55:35 +00001776
Harald Welte4bfdfe72009-06-10 23:11:52 +08001777 /* bearer capability */
1778 if (setup->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001779 gsm48_encode_bearer_cap(msg, 0, &setup->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001780 /* facility */
1781 if (setup->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001782 gsm48_encode_facility(msg, 0, &setup->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001783 /* progress */
1784 if (setup->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001785 gsm48_encode_progress(msg, 0, &setup->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001786 /* calling party BCD number */
1787 if (setup->fields & MNCC_F_CALLING)
Harald Welte55c8f352010-03-07 23:40:35 +01001788 gsm48_encode_calling(msg, &setup->calling);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001789 /* called party BCD number */
1790 if (setup->fields & MNCC_F_CALLED)
Harald Welte55c8f352010-03-07 23:40:35 +01001791 gsm48_encode_called(msg, &setup->called);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001792 /* user-user */
1793 if (setup->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001794 gsm48_encode_useruser(msg, 0, &setup->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001795 /* redirecting party BCD number */
1796 if (setup->fields & MNCC_F_REDIRECTING)
Harald Welte55c8f352010-03-07 23:40:35 +01001797 gsm48_encode_redirecting(msg, &setup->redirecting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001798 /* signal */
1799 if (setup->fields & MNCC_F_SIGNAL)
Harald Welte55c8f352010-03-07 23:40:35 +01001800 gsm48_encode_signal(msg, setup->signal);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001801
1802 new_cc_state(trans, GSM_CSTATE_CALL_PRESENT);
Harald Welte65e74cc2008-12-29 01:55:35 +00001803
Harald Welte39e2ead2009-07-23 21:13:03 +02001804 return gsm48_sendmsg(msg, trans);
Harald Welte65e74cc2008-12-29 01:55:35 +00001805}
1806
Harald Welte4bfdfe72009-06-10 23:11:52 +08001807static int gsm48_cc_rx_call_conf(struct gsm_trans *trans, struct msgb *msg)
1808{
1809 struct gsm48_hdr *gh = msgb_l3(msg);
1810 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1811 struct tlv_parsed tp;
1812 struct gsm_mncc call_conf;
1813
1814 gsm48_stop_cc_timer(trans);
1815 gsm48_start_cc_timer(trans, 0x310, GSM48_T310);
1816
1817 memset(&call_conf, 0, sizeof(struct gsm_mncc));
1818 call_conf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001819 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001820#if 0
1821 /* repeat */
1822 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_CIR))
1823 call_conf.repeat = 1;
1824 if (TLVP_PRESENT(&tp, GSM48_IE_REPEAT_SEQ))
1825 call_conf.repeat = 2;
1826#endif
1827 /* bearer capability */
1828 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
1829 call_conf.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001830 gsm48_decode_bearer_cap(&call_conf.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001831 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
1832 }
1833 /* cause */
1834 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
1835 call_conf.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01001836 gsm48_decode_cause(&call_conf.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001837 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
1838 }
1839 /* cc cap */
1840 if (TLVP_PRESENT(&tp, GSM48_IE_CC_CAP)) {
1841 call_conf.fields |= MNCC_F_CCCAP;
Harald Welte55c8f352010-03-07 23:40:35 +01001842 gsm48_decode_cccap(&call_conf.cccap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001843 TLVP_VAL(&tp, GSM48_IE_CC_CAP)-1);
1844 }
1845
1846 new_cc_state(trans, GSM_CSTATE_MO_TERM_CALL_CONF);
1847
Harald Welte596fed42009-07-23 19:06:52 +02001848 return mncc_recvmsg(trans->subscr->net, trans, MNCC_CALL_CONF_IND,
1849 &call_conf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001850}
1851
1852static int gsm48_cc_tx_call_proc(struct gsm_trans *trans, void *arg)
1853{
1854 struct gsm_mncc *proceeding = arg;
1855 struct msgb *msg = gsm48_msgb_alloc();
1856 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1857
Harald Welte4bfdfe72009-06-10 23:11:52 +08001858 gh->msg_type = GSM48_MT_CC_CALL_PROC;
1859
1860 new_cc_state(trans, GSM_CSTATE_MO_CALL_PROC);
1861
1862 /* bearer capability */
1863 if (proceeding->fields & MNCC_F_BEARER_CAP)
Harald Welte55c8f352010-03-07 23:40:35 +01001864 gsm48_encode_bearer_cap(msg, 0, &proceeding->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001865 /* facility */
1866 if (proceeding->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001867 gsm48_encode_facility(msg, 0, &proceeding->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001868 /* progress */
1869 if (proceeding->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001870 gsm48_encode_progress(msg, 0, &proceeding->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001871
Harald Welte39e2ead2009-07-23 21:13:03 +02001872 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001873}
1874
1875static int gsm48_cc_rx_alerting(struct gsm_trans *trans, struct msgb *msg)
1876{
1877 struct gsm48_hdr *gh = msgb_l3(msg);
1878 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1879 struct tlv_parsed tp;
1880 struct gsm_mncc alerting;
1881
1882 gsm48_stop_cc_timer(trans);
1883 gsm48_start_cc_timer(trans, 0x301, GSM48_T301);
1884
1885 memset(&alerting, 0, sizeof(struct gsm_mncc));
1886 alerting.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001887 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001888 /* facility */
1889 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
1890 alerting.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01001891 gsm48_decode_facility(&alerting.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001892 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
1893 }
1894
1895 /* progress */
1896 if (TLVP_PRESENT(&tp, GSM48_IE_PROGR_IND)) {
1897 alerting.fields |= MNCC_F_PROGRESS;
Harald Welte55c8f352010-03-07 23:40:35 +01001898 gsm48_decode_progress(&alerting.progress,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001899 TLVP_VAL(&tp, GSM48_IE_PROGR_IND)-1);
1900 }
1901 /* ss-version */
1902 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
1903 alerting.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01001904 gsm48_decode_ssversion(&alerting.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08001905 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
1906 }
1907
1908 new_cc_state(trans, GSM_CSTATE_CALL_RECEIVED);
1909
Harald Welte596fed42009-07-23 19:06:52 +02001910 return mncc_recvmsg(trans->subscr->net, trans, MNCC_ALERT_IND,
1911 &alerting);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001912}
1913
1914static int gsm48_cc_tx_alerting(struct gsm_trans *trans, void *arg)
1915{
1916 struct gsm_mncc *alerting = arg;
1917 struct msgb *msg = gsm48_msgb_alloc();
1918 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1919
Harald Welte4bfdfe72009-06-10 23:11:52 +08001920 gh->msg_type = GSM48_MT_CC_ALERTING;
1921
1922 /* facility */
1923 if (alerting->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001924 gsm48_encode_facility(msg, 0, &alerting->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001925 /* progress */
1926 if (alerting->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001927 gsm48_encode_progress(msg, 0, &alerting->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001928 /* user-user */
1929 if (alerting->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001930 gsm48_encode_useruser(msg, 0, &alerting->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001931
1932 new_cc_state(trans, GSM_CSTATE_CALL_DELIVERED);
1933
Harald Welte39e2ead2009-07-23 21:13:03 +02001934 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001935}
1936
1937static int gsm48_cc_tx_progress(struct gsm_trans *trans, void *arg)
1938{
1939 struct gsm_mncc *progress = arg;
1940 struct msgb *msg = gsm48_msgb_alloc();
1941 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1942
Harald Welte4bfdfe72009-06-10 23:11:52 +08001943 gh->msg_type = GSM48_MT_CC_PROGRESS;
1944
1945 /* progress */
Harald Welte55c8f352010-03-07 23:40:35 +01001946 gsm48_encode_progress(msg, 1, &progress->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001947 /* user-user */
1948 if (progress->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001949 gsm48_encode_useruser(msg, 0, &progress->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001950
Harald Welte39e2ead2009-07-23 21:13:03 +02001951 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001952}
1953
1954static int gsm48_cc_tx_connect(struct gsm_trans *trans, void *arg)
1955{
1956 struct gsm_mncc *connect = arg;
1957 struct msgb *msg = gsm48_msgb_alloc();
1958 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
1959
Harald Welte4bfdfe72009-06-10 23:11:52 +08001960 gh->msg_type = GSM48_MT_CC_CONNECT;
1961
1962 gsm48_stop_cc_timer(trans);
1963 gsm48_start_cc_timer(trans, 0x313, GSM48_T313);
1964
1965 /* facility */
1966 if (connect->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01001967 gsm48_encode_facility(msg, 0, &connect->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001968 /* progress */
1969 if (connect->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01001970 gsm48_encode_progress(msg, 0, &connect->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001971 /* connected number */
1972 if (connect->fields & MNCC_F_CONNECTED)
Harald Welte55c8f352010-03-07 23:40:35 +01001973 gsm48_encode_connected(msg, &connect->connected);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001974 /* user-user */
1975 if (connect->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01001976 gsm48_encode_useruser(msg, 0, &connect->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001977
1978 new_cc_state(trans, GSM_CSTATE_CONNECT_IND);
1979
Harald Welte39e2ead2009-07-23 21:13:03 +02001980 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001981}
1982
1983static int gsm48_cc_rx_connect(struct gsm_trans *trans, struct msgb *msg)
1984{
1985 struct gsm48_hdr *gh = msgb_l3(msg);
1986 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
1987 struct tlv_parsed tp;
1988 struct gsm_mncc connect;
1989
1990 gsm48_stop_cc_timer(trans);
1991
1992 memset(&connect, 0, sizeof(struct gsm_mncc));
1993 connect.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01001994 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08001995 /* use subscriber as connected party number */
1996 if (trans->subscr) {
1997 connect.fields |= MNCC_F_CONNECTED;
1998 strncpy(connect.connected.number, trans->subscr->extension,
1999 sizeof(connect.connected.number)-1);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002000 strncpy(connect.imsi, trans->subscr->imsi,
2001 sizeof(connect.imsi)-1);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002002 }
2003 /* facility */
2004 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2005 connect.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002006 gsm48_decode_facility(&connect.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002007 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2008 }
2009 /* user-user */
2010 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2011 connect.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002012 gsm48_decode_useruser(&connect.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002013 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2014 }
2015 /* ss-version */
2016 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2017 connect.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002018 gsm48_decode_ssversion(&connect.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002019 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2020 }
2021
2022 new_cc_state(trans, GSM_CSTATE_CONNECT_REQUEST);
2023
Harald Welte596fed42009-07-23 19:06:52 +02002024 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_CNF, &connect);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002025}
2026
2027
2028static int gsm48_cc_rx_connect_ack(struct gsm_trans *trans, struct msgb *msg)
2029{
2030 struct gsm_mncc connect_ack;
2031
2032 gsm48_stop_cc_timer(trans);
2033
2034 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2035
2036 memset(&connect_ack, 0, sizeof(struct gsm_mncc));
2037 connect_ack.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002038 return mncc_recvmsg(trans->subscr->net, trans, MNCC_SETUP_COMPL_IND,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002039 &connect_ack);
2040}
2041
2042static int gsm48_cc_tx_connect_ack(struct gsm_trans *trans, void *arg)
2043{
2044 struct msgb *msg = gsm48_msgb_alloc();
2045 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2046
Harald Welte4bfdfe72009-06-10 23:11:52 +08002047 gh->msg_type = GSM48_MT_CC_CONNECT_ACK;
2048
2049 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2050
Harald Welte39e2ead2009-07-23 21:13:03 +02002051 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002052}
2053
2054static int gsm48_cc_rx_disconnect(struct gsm_trans *trans, struct msgb *msg)
2055{
2056 struct gsm48_hdr *gh = msgb_l3(msg);
2057 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2058 struct tlv_parsed tp;
2059 struct gsm_mncc disc;
2060
2061 gsm48_stop_cc_timer(trans);
2062
2063 new_cc_state(trans, GSM_CSTATE_DISCONNECT_REQ);
2064
2065 memset(&disc, 0, sizeof(struct gsm_mncc));
2066 disc.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002067 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_CAUSE, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002068 /* cause */
2069 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2070 disc.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002071 gsm48_decode_cause(&disc.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002072 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2073 }
2074 /* facility */
2075 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2076 disc.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002077 gsm48_decode_facility(&disc.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002078 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2079 }
2080 /* user-user */
2081 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2082 disc.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002083 gsm48_decode_useruser(&disc.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002084 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2085 }
2086 /* ss-version */
2087 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2088 disc.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002089 gsm48_decode_ssversion(&disc.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002090 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2091 }
2092
Harald Welte596fed42009-07-23 19:06:52 +02002093 return mncc_recvmsg(trans->subscr->net, trans, MNCC_DISC_IND, &disc);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002094
2095}
2096
Harald Weltec66b71c2009-06-11 14:23:20 +08002097static struct gsm_mncc_cause default_cause = {
2098 .location = GSM48_CAUSE_LOC_PRN_S_LU,
2099 .coding = 0,
2100 .rec = 0,
2101 .rec_val = 0,
2102 .value = GSM48_CC_CAUSE_NORMAL_UNSPEC,
2103 .diag_len = 0,
2104 .diag = { 0 },
2105};
Harald Welte4bfdfe72009-06-10 23:11:52 +08002106
2107static int gsm48_cc_tx_disconnect(struct gsm_trans *trans, void *arg)
2108{
2109 struct gsm_mncc *disc = arg;
2110 struct msgb *msg = gsm48_msgb_alloc();
2111 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2112
Harald Welte4bfdfe72009-06-10 23:11:52 +08002113 gh->msg_type = GSM48_MT_CC_DISCONNECT;
2114
2115 gsm48_stop_cc_timer(trans);
2116 gsm48_start_cc_timer(trans, 0x306, GSM48_T306);
2117
2118 /* cause */
2119 if (disc->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002120 gsm48_encode_cause(msg, 1, &disc->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002121 else
Harald Welte55c8f352010-03-07 23:40:35 +01002122 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002123
2124 /* facility */
2125 if (disc->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002126 gsm48_encode_facility(msg, 0, &disc->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002127 /* progress */
2128 if (disc->fields & MNCC_F_PROGRESS)
Harald Welte55c8f352010-03-07 23:40:35 +01002129 gsm48_encode_progress(msg, 0, &disc->progress);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002130 /* user-user */
2131 if (disc->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002132 gsm48_encode_useruser(msg, 0, &disc->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002133
2134 /* store disconnect cause for T306 expiry */
Harald Weltedcaf5652009-07-23 18:56:43 +02002135 memcpy(&trans->cc.msg, disc, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002136
2137 new_cc_state(trans, GSM_CSTATE_DISCONNECT_IND);
2138
Harald Welte39e2ead2009-07-23 21:13:03 +02002139 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002140}
2141
2142static int gsm48_cc_rx_release(struct gsm_trans *trans, struct msgb *msg)
2143{
2144 struct gsm48_hdr *gh = msgb_l3(msg);
2145 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2146 struct tlv_parsed tp;
2147 struct gsm_mncc rel;
2148 int rc;
2149
2150 gsm48_stop_cc_timer(trans);
2151
2152 memset(&rel, 0, sizeof(struct gsm_mncc));
2153 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002154 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002155 /* cause */
2156 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2157 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002158 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002159 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2160 }
2161 /* facility */
2162 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2163 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002164 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002165 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2166 }
2167 /* user-user */
2168 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2169 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002170 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002171 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2172 }
2173 /* ss-version */
2174 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2175 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002176 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002177 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2178 }
2179
Harald Weltedcaf5652009-07-23 18:56:43 +02002180 if (trans->cc.state == GSM_CSTATE_RELEASE_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002181 /* release collision 5.4.5 */
Harald Welte596fed42009-07-23 19:06:52 +02002182 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_CNF, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002183 } else {
Harald Welte596fed42009-07-23 19:06:52 +02002184 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02002185 GSM48_PDISC_CC | (trans->transaction_id << 4),
Harald Welte596fed42009-07-23 19:06:52 +02002186 GSM48_MT_CC_RELEASE_COMPL);
2187 rc = mncc_recvmsg(trans->subscr->net, trans, MNCC_REL_IND, &rel);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002188 }
2189
2190 new_cc_state(trans, GSM_CSTATE_NULL);
2191
2192 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002193 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002194
2195 return rc;
2196}
2197
2198static int gsm48_cc_tx_release(struct gsm_trans *trans, void *arg)
2199{
2200 struct gsm_mncc *rel = arg;
2201 struct msgb *msg = gsm48_msgb_alloc();
2202 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2203
Harald Welte4bfdfe72009-06-10 23:11:52 +08002204 gh->msg_type = GSM48_MT_CC_RELEASE;
2205
2206 trans->callref = 0;
2207
2208 gsm48_stop_cc_timer(trans);
2209 gsm48_start_cc_timer(trans, 0x308, GSM48_T308);
2210
2211 /* cause */
2212 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002213 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002214 /* facility */
2215 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002216 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002217 /* user-user */
2218 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002219 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002220
Harald Weltedcaf5652009-07-23 18:56:43 +02002221 trans->cc.T308_second = 0;
2222 memcpy(&trans->cc.msg, rel, sizeof(struct gsm_mncc));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002223
Harald Weltedcaf5652009-07-23 18:56:43 +02002224 if (trans->cc.state != GSM_CSTATE_RELEASE_REQ)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002225 new_cc_state(trans, GSM_CSTATE_RELEASE_REQ);
2226
Harald Welte39e2ead2009-07-23 21:13:03 +02002227 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002228}
2229
2230static int gsm48_cc_rx_release_compl(struct gsm_trans *trans, struct msgb *msg)
2231{
2232 struct gsm48_hdr *gh = msgb_l3(msg);
2233 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2234 struct tlv_parsed tp;
2235 struct gsm_mncc rel;
2236 int rc = 0;
2237
2238 gsm48_stop_cc_timer(trans);
2239
2240 memset(&rel, 0, sizeof(struct gsm_mncc));
2241 rel.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002242 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002243 /* cause */
2244 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2245 rel.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002246 gsm48_decode_cause(&rel.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002247 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2248 }
2249 /* facility */
2250 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2251 rel.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002252 gsm48_decode_facility(&rel.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002253 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2254 }
2255 /* user-user */
2256 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2257 rel.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002258 gsm48_decode_useruser(&rel.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002259 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2260 }
2261 /* ss-version */
2262 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2263 rel.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002264 gsm48_decode_ssversion(&rel.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002265 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2266 }
2267
2268 if (trans->callref) {
Harald Weltedcaf5652009-07-23 18:56:43 +02002269 switch (trans->cc.state) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002270 case GSM_CSTATE_CALL_PRESENT:
Harald Welte596fed42009-07-23 19:06:52 +02002271 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002272 MNCC_REJ_IND, &rel);
2273 break;
2274 case GSM_CSTATE_RELEASE_REQ:
Harald Welte596fed42009-07-23 19:06:52 +02002275 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002276 MNCC_REL_CNF, &rel);
Harald Welteb83d9382009-12-12 21:00:48 +01002277 /* FIXME: in case of multiple calls, we can't simply
2278 * hang up here ! */
2279 lchan_auto_release(msg->lchan);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002280 break;
2281 default:
Harald Welte596fed42009-07-23 19:06:52 +02002282 rc = mncc_recvmsg(trans->subscr->net, trans,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002283 MNCC_REL_IND, &rel);
2284 }
2285 }
2286
2287 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002288 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002289
2290 return rc;
2291}
2292
2293static int gsm48_cc_tx_release_compl(struct gsm_trans *trans, void *arg)
2294{
2295 struct gsm_mncc *rel = arg;
2296 struct msgb *msg = gsm48_msgb_alloc();
2297 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2298
Harald Welte4bfdfe72009-06-10 23:11:52 +08002299 gh->msg_type = GSM48_MT_CC_RELEASE_COMPL;
2300
2301 trans->callref = 0;
2302
2303 gsm48_stop_cc_timer(trans);
2304
2305 /* cause */
2306 if (rel->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002307 gsm48_encode_cause(msg, 0, &rel->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002308 /* facility */
2309 if (rel->fields & MNCC_F_FACILITY)
Harald Welte55c8f352010-03-07 23:40:35 +01002310 gsm48_encode_facility(msg, 0, &rel->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002311 /* user-user */
2312 if (rel->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002313 gsm48_encode_useruser(msg, 0, &rel->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002314
Harald Weltedcaf5652009-07-23 18:56:43 +02002315 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002316
Harald Welte39e2ead2009-07-23 21:13:03 +02002317 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002318}
2319
2320static int gsm48_cc_rx_facility(struct gsm_trans *trans, struct msgb *msg)
2321{
2322 struct gsm48_hdr *gh = msgb_l3(msg);
2323 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2324 struct tlv_parsed tp;
2325 struct gsm_mncc fac;
2326
2327 memset(&fac, 0, sizeof(struct gsm_mncc));
2328 fac.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002329 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_FACILITY, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002330 /* facility */
2331 if (TLVP_PRESENT(&tp, GSM48_IE_FACILITY)) {
2332 fac.fields |= MNCC_F_FACILITY;
Harald Welte55c8f352010-03-07 23:40:35 +01002333 gsm48_decode_facility(&fac.facility,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002334 TLVP_VAL(&tp, GSM48_IE_FACILITY)-1);
2335 }
2336 /* ss-version */
2337 if (TLVP_PRESENT(&tp, GSM48_IE_SS_VERS)) {
2338 fac.fields |= MNCC_F_SSVERSION;
Harald Welte55c8f352010-03-07 23:40:35 +01002339 gsm48_decode_ssversion(&fac.ssversion,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002340 TLVP_VAL(&tp, GSM48_IE_SS_VERS)-1);
2341 }
2342
Harald Welte596fed42009-07-23 19:06:52 +02002343 return mncc_recvmsg(trans->subscr->net, trans, MNCC_FACILITY_IND, &fac);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002344}
2345
2346static int gsm48_cc_tx_facility(struct gsm_trans *trans, void *arg)
2347{
2348 struct gsm_mncc *fac = arg;
2349 struct msgb *msg = gsm48_msgb_alloc();
2350 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2351
Harald Welte4bfdfe72009-06-10 23:11:52 +08002352 gh->msg_type = GSM48_MT_CC_FACILITY;
2353
2354 /* facility */
Harald Welte55c8f352010-03-07 23:40:35 +01002355 gsm48_encode_facility(msg, 1, &fac->facility);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002356
Harald Welte39e2ead2009-07-23 21:13:03 +02002357 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002358}
2359
2360static int gsm48_cc_rx_hold(struct gsm_trans *trans, struct msgb *msg)
2361{
2362 struct gsm_mncc hold;
2363
2364 memset(&hold, 0, sizeof(struct gsm_mncc));
2365 hold.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002366 return mncc_recvmsg(trans->subscr->net, trans, MNCC_HOLD_IND, &hold);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002367}
2368
2369static int gsm48_cc_tx_hold_ack(struct gsm_trans *trans, void *arg)
2370{
2371 struct msgb *msg = gsm48_msgb_alloc();
2372 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2373
Harald Welte4bfdfe72009-06-10 23:11:52 +08002374 gh->msg_type = GSM48_MT_CC_HOLD_ACK;
2375
Harald Welte39e2ead2009-07-23 21:13:03 +02002376 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002377}
2378
2379static int gsm48_cc_tx_hold_rej(struct gsm_trans *trans, void *arg)
2380{
2381 struct gsm_mncc *hold_rej = arg;
2382 struct msgb *msg = gsm48_msgb_alloc();
2383 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2384
Harald Welte4bfdfe72009-06-10 23:11:52 +08002385 gh->msg_type = GSM48_MT_CC_HOLD_REJ;
2386
2387 /* cause */
2388 if (hold_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002389 gsm48_encode_cause(msg, 1, &hold_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002390 else
Harald Welte55c8f352010-03-07 23:40:35 +01002391 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002392
Harald Welte39e2ead2009-07-23 21:13:03 +02002393 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002394}
2395
2396static int gsm48_cc_rx_retrieve(struct gsm_trans *trans, struct msgb *msg)
2397{
2398 struct gsm_mncc retrieve;
2399
2400 memset(&retrieve, 0, sizeof(struct gsm_mncc));
2401 retrieve.callref = trans->callref;
Harald Welte596fed42009-07-23 19:06:52 +02002402 return mncc_recvmsg(trans->subscr->net, trans, MNCC_RETRIEVE_IND,
2403 &retrieve);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002404}
2405
2406static int gsm48_cc_tx_retrieve_ack(struct gsm_trans *trans, void *arg)
2407{
2408 struct msgb *msg = gsm48_msgb_alloc();
2409 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2410
Harald Welte4bfdfe72009-06-10 23:11:52 +08002411 gh->msg_type = GSM48_MT_CC_RETR_ACK;
2412
Harald Welte39e2ead2009-07-23 21:13:03 +02002413 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002414}
2415
2416static int gsm48_cc_tx_retrieve_rej(struct gsm_trans *trans, void *arg)
2417{
2418 struct gsm_mncc *retrieve_rej = arg;
2419 struct msgb *msg = gsm48_msgb_alloc();
2420 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2421
Harald Welte4bfdfe72009-06-10 23:11:52 +08002422 gh->msg_type = GSM48_MT_CC_RETR_REJ;
2423
2424 /* cause */
2425 if (retrieve_rej->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002426 gsm48_encode_cause(msg, 1, &retrieve_rej->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002427 else
Harald Welte55c8f352010-03-07 23:40:35 +01002428 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002429
Harald Welte39e2ead2009-07-23 21:13:03 +02002430 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002431}
2432
2433static int gsm48_cc_rx_start_dtmf(struct gsm_trans *trans, struct msgb *msg)
2434{
2435 struct gsm48_hdr *gh = msgb_l3(msg);
2436 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2437 struct tlv_parsed tp;
2438 struct gsm_mncc dtmf;
2439
2440 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2441 dtmf.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002442 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, 0, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002443 /* keypad facility */
2444 if (TLVP_PRESENT(&tp, GSM48_IE_KPD_FACILITY)) {
2445 dtmf.fields |= MNCC_F_KEYPAD;
Harald Welte55c8f352010-03-07 23:40:35 +01002446 gsm48_decode_keypad(&dtmf.keypad,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002447 TLVP_VAL(&tp, GSM48_IE_KPD_FACILITY)-1);
2448 }
2449
Harald Welte596fed42009-07-23 19:06:52 +02002450 return mncc_recvmsg(trans->subscr->net, trans, MNCC_START_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002451}
2452
2453static int gsm48_cc_tx_start_dtmf_ack(struct gsm_trans *trans, void *arg)
2454{
2455 struct gsm_mncc *dtmf = arg;
2456 struct msgb *msg = gsm48_msgb_alloc();
2457 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2458
Harald Welte4bfdfe72009-06-10 23:11:52 +08002459 gh->msg_type = GSM48_MT_CC_START_DTMF_ACK;
2460
2461 /* keypad */
2462 if (dtmf->fields & MNCC_F_KEYPAD)
Harald Welte55c8f352010-03-07 23:40:35 +01002463 gsm48_encode_keypad(msg, dtmf->keypad);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002464
Harald Welte39e2ead2009-07-23 21:13:03 +02002465 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002466}
2467
2468static int gsm48_cc_tx_start_dtmf_rej(struct gsm_trans *trans, void *arg)
2469{
2470 struct gsm_mncc *dtmf = arg;
2471 struct msgb *msg = gsm48_msgb_alloc();
2472 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2473
Harald Welte4bfdfe72009-06-10 23:11:52 +08002474 gh->msg_type = GSM48_MT_CC_START_DTMF_REJ;
2475
2476 /* cause */
2477 if (dtmf->fields & MNCC_F_CAUSE)
Harald Welte55c8f352010-03-07 23:40:35 +01002478 gsm48_encode_cause(msg, 1, &dtmf->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002479 else
Harald Welte55c8f352010-03-07 23:40:35 +01002480 gsm48_encode_cause(msg, 1, &default_cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002481
Harald Welte39e2ead2009-07-23 21:13:03 +02002482 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002483}
2484
2485static int gsm48_cc_tx_stop_dtmf_ack(struct gsm_trans *trans, void *arg)
2486{
2487 struct msgb *msg = gsm48_msgb_alloc();
2488 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2489
Harald Welte4bfdfe72009-06-10 23:11:52 +08002490 gh->msg_type = GSM48_MT_CC_STOP_DTMF_ACK;
2491
Harald Welte39e2ead2009-07-23 21:13:03 +02002492 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002493}
2494
2495static int gsm48_cc_rx_stop_dtmf(struct gsm_trans *trans, struct msgb *msg)
2496{
2497 struct gsm_mncc dtmf;
2498
2499 memset(&dtmf, 0, sizeof(struct gsm_mncc));
2500 dtmf.callref = trans->callref;
2501
Harald Welte596fed42009-07-23 19:06:52 +02002502 return mncc_recvmsg(trans->subscr->net, trans, MNCC_STOP_DTMF_IND, &dtmf);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002503}
2504
2505static int gsm48_cc_rx_modify(struct gsm_trans *trans, struct msgb *msg)
2506{
2507 struct gsm48_hdr *gh = msgb_l3(msg);
2508 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2509 struct tlv_parsed tp;
2510 struct gsm_mncc modify;
2511
2512 memset(&modify, 0, sizeof(struct gsm_mncc));
2513 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002514 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002515 /* bearer capability */
2516 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2517 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002518 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002519 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2520 }
2521
2522 new_cc_state(trans, GSM_CSTATE_MO_ORIG_MODIFY);
2523
Harald Welte596fed42009-07-23 19:06:52 +02002524 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_IND, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002525}
2526
2527static int gsm48_cc_tx_modify(struct gsm_trans *trans, void *arg)
2528{
2529 struct gsm_mncc *modify = arg;
2530 struct msgb *msg = gsm48_msgb_alloc();
2531 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2532
Harald Welte4bfdfe72009-06-10 23:11:52 +08002533 gh->msg_type = GSM48_MT_CC_MODIFY;
2534
2535 gsm48_start_cc_timer(trans, 0x323, GSM48_T323);
2536
2537 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002538 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002539
2540 new_cc_state(trans, GSM_CSTATE_MO_TERM_MODIFY);
2541
Harald Welte39e2ead2009-07-23 21:13:03 +02002542 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002543}
2544
2545static int gsm48_cc_rx_modify_complete(struct gsm_trans *trans, struct msgb *msg)
2546{
2547 struct gsm48_hdr *gh = msgb_l3(msg);
2548 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2549 struct tlv_parsed tp;
2550 struct gsm_mncc modify;
2551
2552 gsm48_stop_cc_timer(trans);
2553
2554 memset(&modify, 0, sizeof(struct gsm_mncc));
2555 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002556 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002557 /* bearer capability */
2558 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2559 modify.fields |= MNCC_F_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002560 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002561 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2562 }
2563
2564 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2565
Harald Welte596fed42009-07-23 19:06:52 +02002566 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_CNF, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002567}
2568
2569static int gsm48_cc_tx_modify_complete(struct gsm_trans *trans, void *arg)
2570{
2571 struct gsm_mncc *modify = arg;
2572 struct msgb *msg = gsm48_msgb_alloc();
2573 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2574
Harald Welte4bfdfe72009-06-10 23:11:52 +08002575 gh->msg_type = GSM48_MT_CC_MODIFY_COMPL;
2576
2577 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002578 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002579
2580 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2581
Harald Welte39e2ead2009-07-23 21:13:03 +02002582 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002583}
2584
2585static int gsm48_cc_rx_modify_reject(struct gsm_trans *trans, struct msgb *msg)
2586{
2587 struct gsm48_hdr *gh = msgb_l3(msg);
2588 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2589 struct tlv_parsed tp;
2590 struct gsm_mncc modify;
2591
2592 gsm48_stop_cc_timer(trans);
2593
2594 memset(&modify, 0, sizeof(struct gsm_mncc));
2595 modify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002596 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_BEARER_CAP, GSM48_IE_CAUSE);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002597 /* bearer capability */
2598 if (TLVP_PRESENT(&tp, GSM48_IE_BEARER_CAP)) {
2599 modify.fields |= GSM48_IE_BEARER_CAP;
Harald Welte55c8f352010-03-07 23:40:35 +01002600 gsm48_decode_bearer_cap(&modify.bearer_cap,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002601 TLVP_VAL(&tp, GSM48_IE_BEARER_CAP)-1);
2602 }
2603 /* cause */
2604 if (TLVP_PRESENT(&tp, GSM48_IE_CAUSE)) {
2605 modify.fields |= MNCC_F_CAUSE;
Harald Welte55c8f352010-03-07 23:40:35 +01002606 gsm48_decode_cause(&modify.cause,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002607 TLVP_VAL(&tp, GSM48_IE_CAUSE)-1);
2608 }
2609
2610 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2611
Harald Welte596fed42009-07-23 19:06:52 +02002612 return mncc_recvmsg(trans->subscr->net, trans, MNCC_MODIFY_REJ, &modify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002613}
2614
2615static int gsm48_cc_tx_modify_reject(struct gsm_trans *trans, void *arg)
2616{
2617 struct gsm_mncc *modify = arg;
2618 struct msgb *msg = gsm48_msgb_alloc();
2619 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2620
Harald Welte4bfdfe72009-06-10 23:11:52 +08002621 gh->msg_type = GSM48_MT_CC_MODIFY_REJECT;
2622
2623 /* bearer capability */
Harald Welte55c8f352010-03-07 23:40:35 +01002624 gsm48_encode_bearer_cap(msg, 1, &modify->bearer_cap);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002625 /* cause */
Harald Welte55c8f352010-03-07 23:40:35 +01002626 gsm48_encode_cause(msg, 1, &modify->cause);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002627
2628 new_cc_state(trans, GSM_CSTATE_ACTIVE);
2629
Harald Welte39e2ead2009-07-23 21:13:03 +02002630 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002631}
2632
2633static int gsm48_cc_tx_notify(struct gsm_trans *trans, void *arg)
2634{
2635 struct gsm_mncc *notify = arg;
2636 struct msgb *msg = gsm48_msgb_alloc();
2637 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2638
Harald Welte4bfdfe72009-06-10 23:11:52 +08002639 gh->msg_type = GSM48_MT_CC_NOTIFY;
2640
2641 /* notify */
Harald Welte55c8f352010-03-07 23:40:35 +01002642 gsm48_encode_notify(msg, notify->notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002643
Harald Welte39e2ead2009-07-23 21:13:03 +02002644 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002645}
2646
2647static int gsm48_cc_rx_notify(struct gsm_trans *trans, struct msgb *msg)
2648{
2649 struct gsm48_hdr *gh = msgb_l3(msg);
2650 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2651// struct tlv_parsed tp;
2652 struct gsm_mncc notify;
2653
2654 memset(&notify, 0, sizeof(struct gsm_mncc));
2655 notify.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002656// tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002657 if (payload_len >= 1)
Harald Welte55c8f352010-03-07 23:40:35 +01002658 gsm48_decode_notify(&notify.notify, gh->data);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002659
Harald Welte596fed42009-07-23 19:06:52 +02002660 return mncc_recvmsg(trans->subscr->net, trans, MNCC_NOTIFY_IND, &notify);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002661}
2662
2663static int gsm48_cc_tx_userinfo(struct gsm_trans *trans, void *arg)
2664{
2665 struct gsm_mncc *user = arg;
2666 struct msgb *msg = gsm48_msgb_alloc();
2667 struct gsm48_hdr *gh = (struct gsm48_hdr *) msgb_put(msg, sizeof(*gh));
2668
Harald Welte4bfdfe72009-06-10 23:11:52 +08002669 gh->msg_type = GSM48_MT_CC_USER_INFO;
2670
2671 /* user-user */
2672 if (user->fields & MNCC_F_USERUSER)
Harald Welte55c8f352010-03-07 23:40:35 +01002673 gsm48_encode_useruser(msg, 1, &user->useruser);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002674 /* more data */
2675 if (user->more)
Harald Welte55c8f352010-03-07 23:40:35 +01002676 gsm48_encode_more(msg);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002677
Harald Welte39e2ead2009-07-23 21:13:03 +02002678 return gsm48_sendmsg(msg, trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002679}
2680
2681static int gsm48_cc_rx_userinfo(struct gsm_trans *trans, struct msgb *msg)
2682{
2683 struct gsm48_hdr *gh = msgb_l3(msg);
2684 unsigned int payload_len = msgb_l3len(msg) - sizeof(*gh);
2685 struct tlv_parsed tp;
2686 struct gsm_mncc user;
2687
2688 memset(&user, 0, sizeof(struct gsm_mncc));
2689 user.callref = trans->callref;
Harald Welte474d19f2010-03-02 23:18:30 +01002690 tlv_parse(&tp, &gsm48_att_tlvdef, gh->data, payload_len, GSM48_IE_USER_USER, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002691 /* user-user */
2692 if (TLVP_PRESENT(&tp, GSM48_IE_USER_USER)) {
2693 user.fields |= MNCC_F_USERUSER;
Harald Welte55c8f352010-03-07 23:40:35 +01002694 gsm48_decode_useruser(&user.useruser,
Harald Welte4bfdfe72009-06-10 23:11:52 +08002695 TLVP_VAL(&tp, GSM48_IE_USER_USER)-1);
2696 }
2697 /* more data */
2698 if (TLVP_PRESENT(&tp, GSM48_IE_MORE_DATA))
2699 user.more = 1;
2700
Harald Welte596fed42009-07-23 19:06:52 +02002701 return mncc_recvmsg(trans->subscr->net, trans, MNCC_USERINFO_IND, &user);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002702}
2703
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002704static int _gsm48_lchan_modify(struct gsm_trans *trans, void *arg)
Harald Welte4bfdfe72009-06-10 23:11:52 +08002705{
2706 struct gsm_mncc *mode = arg;
2707
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002708 return gsm48_lchan_modify(trans->conn->lchan, mode->lchan_mode);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002709}
2710
2711static struct downstate {
2712 u_int32_t states;
2713 int type;
2714 int (*rout) (struct gsm_trans *trans, void *arg);
2715} downstatelist[] = {
2716 /* mobile originating call establishment */
2717 {SBIT(GSM_CSTATE_INITIATED), /* 5.2.1.2 */
2718 MNCC_CALL_PROC_REQ, gsm48_cc_tx_call_proc},
2719 {SBIT(GSM_CSTATE_INITIATED) | SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.2 | 5.2.1.5 */
2720 MNCC_ALERT_REQ, gsm48_cc_tx_alerting},
2721 {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 */
2722 MNCC_SETUP_RSP, gsm48_cc_tx_connect},
2723 {SBIT(GSM_CSTATE_MO_CALL_PROC), /* 5.2.1.4.2 */
2724 MNCC_PROGRESS_REQ, gsm48_cc_tx_progress},
2725 /* mobile terminating call establishment */
2726 {SBIT(GSM_CSTATE_NULL), /* 5.2.2.1 */
2727 MNCC_SETUP_REQ, gsm48_cc_tx_setup},
2728 {SBIT(GSM_CSTATE_CONNECT_REQUEST),
2729 MNCC_SETUP_COMPL_REQ, gsm48_cc_tx_connect_ack},
2730 /* signalling during call */
2731 {SBIT(GSM_CSTATE_ACTIVE),
2732 MNCC_NOTIFY_REQ, gsm48_cc_tx_notify},
2733 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ),
2734 MNCC_FACILITY_REQ, gsm48_cc_tx_facility},
2735 {ALL_STATES,
2736 MNCC_START_DTMF_RSP, gsm48_cc_tx_start_dtmf_ack},
2737 {ALL_STATES,
2738 MNCC_START_DTMF_REJ, gsm48_cc_tx_start_dtmf_rej},
2739 {ALL_STATES,
2740 MNCC_STOP_DTMF_RSP, gsm48_cc_tx_stop_dtmf_ack},
2741 {SBIT(GSM_CSTATE_ACTIVE),
2742 MNCC_HOLD_CNF, gsm48_cc_tx_hold_ack},
2743 {SBIT(GSM_CSTATE_ACTIVE),
2744 MNCC_HOLD_REJ, gsm48_cc_tx_hold_rej},
2745 {SBIT(GSM_CSTATE_ACTIVE),
2746 MNCC_RETRIEVE_CNF, gsm48_cc_tx_retrieve_ack},
2747 {SBIT(GSM_CSTATE_ACTIVE),
2748 MNCC_RETRIEVE_REJ, gsm48_cc_tx_retrieve_rej},
2749 {SBIT(GSM_CSTATE_ACTIVE),
2750 MNCC_MODIFY_REQ, gsm48_cc_tx_modify},
2751 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2752 MNCC_MODIFY_RSP, gsm48_cc_tx_modify_complete},
2753 {SBIT(GSM_CSTATE_MO_ORIG_MODIFY),
2754 MNCC_MODIFY_REJ, gsm48_cc_tx_modify_reject},
2755 {SBIT(GSM_CSTATE_ACTIVE),
2756 MNCC_USERINFO_REQ, gsm48_cc_tx_userinfo},
2757 /* clearing */
2758 {SBIT(GSM_CSTATE_INITIATED),
2759 MNCC_REJ_REQ, gsm48_cc_tx_release_compl},
2760 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_DISCONNECT_IND) - SBIT(GSM_CSTATE_RELEASE_REQ) - SBIT(GSM_CSTATE_DISCONNECT_REQ), /* 5.4.4 */
2761 MNCC_DISC_REQ, gsm48_cc_tx_disconnect},
2762 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
2763 MNCC_REL_REQ, gsm48_cc_tx_release},
2764 /* special */
2765 {ALL_STATES,
Holger Hans Peter Freytherff3f2602009-10-22 15:13:00 +02002766 MNCC_LCHAN_MODIFY, _gsm48_lchan_modify},
Harald Welte4bfdfe72009-06-10 23:11:52 +08002767};
2768
2769#define DOWNSLLEN \
2770 (sizeof(downstatelist) / sizeof(struct downstate))
2771
2772
2773int mncc_send(struct gsm_network *net, int msg_type, void *arg)
2774{
Harald Welte1a6f7982009-08-09 18:52:33 +02002775 int i, rc = 0;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002776 struct gsm_trans *trans = NULL, *transt;
Harald Welte1a6f7982009-08-09 18:52:33 +02002777 struct gsm_lchan *lchan = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002778 struct gsm_bts *bts = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002779 struct gsm_mncc *data = arg, rel;
2780
2781 /* handle special messages */
2782 switch(msg_type) {
2783 case MNCC_BRIDGE:
2784 return tch_bridge(net, arg);
2785 case MNCC_FRAME_DROP:
Harald Welteda7ab742009-12-19 22:23:05 +01002786 return tch_recv_mncc(net, data->callref, 0);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002787 case MNCC_FRAME_RECV:
Harald Welteda7ab742009-12-19 22:23:05 +01002788 return tch_recv_mncc(net, data->callref, 1);
2789 case GSM_TCHF_FRAME:
2790 /* Find callref */
2791 trans = trans_find_by_callref(net, data->callref);
2792 if (!trans)
2793 return -EIO;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002794 if (!trans->conn)
Harald Welteda7ab742009-12-19 22:23:05 +01002795 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002796 if (trans->conn->lchan->type != GSM_LCHAN_TCH_F)
Harald Welteda7ab742009-12-19 22:23:05 +01002797 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002798 bts = trans->conn->lchan->ts->trx->bts;
Harald Welteda7ab742009-12-19 22:23:05 +01002799 switch (bts->type) {
2800 case GSM_BTS_TYPE_NANOBTS:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002801 if (!trans->conn->lchan->abis_ip.rtp_socket)
Harald Welteda7ab742009-12-19 22:23:05 +01002802 return 0;
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002803 return rtp_send_frame(trans->conn->lchan->abis_ip.rtp_socket, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002804 case GSM_BTS_TYPE_BS11:
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002805 return trau_send_frame(trans->conn->lchan, arg);
Harald Welteda7ab742009-12-19 22:23:05 +01002806 default:
2807 DEBUGP(DCC, "Unknown BTS type %u\n", bts->type);
2808 }
2809 return -EINVAL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002810 }
2811
2812 memset(&rel, 0, sizeof(struct gsm_mncc));
2813 rel.callref = data->callref;
2814
2815 /* Find callref */
Harald Weltedcaf5652009-07-23 18:56:43 +02002816 trans = trans_find_by_callref(net, data->callref);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002817
2818 /* Callref unknown */
2819 if (!trans) {
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002820 struct gsm_subscriber *subscr;
2821
Harald Welte4a3464c2009-07-04 10:11:24 +02002822 if (msg_type != MNCC_SETUP_REQ) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002823 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2824 "Received '%s' from MNCC with "
2825 "unknown callref %d\n", data->called.number,
2826 get_mncc_name(msg_type), data->callref);
2827 /* Invalid call reference */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002828 return mncc_release_ind(net, NULL, data->callref,
2829 GSM48_CAUSE_LOC_PRN_S_LU,
2830 GSM48_CC_CAUSE_INVAL_TRANS_ID);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002831 }
Andreas Eversbergc079be42009-06-15 23:22:09 +02002832 if (!data->called.number[0] && !data->imsi[0]) {
2833 DEBUGP(DCC, "(bts - trx - ts - ti) "
2834 "Received '%s' from MNCC with "
2835 "no number or IMSI\n", get_mncc_name(msg_type));
2836 /* Invalid number */
2837 return mncc_release_ind(net, NULL, data->callref,
2838 GSM48_CAUSE_LOC_PRN_S_LU,
2839 GSM48_CC_CAUSE_INV_NR_FORMAT);
2840 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002841 /* New transaction due to setup, find subscriber */
Andreas Eversbergc079be42009-06-15 23:22:09 +02002842 if (data->called.number[0])
Harald Welte9176bd42009-07-23 18:46:00 +02002843 subscr = subscr_get_by_extension(net,
2844 data->called.number);
Andreas Eversbergc079be42009-06-15 23:22:09 +02002845 else
Harald Welte9176bd42009-07-23 18:46:00 +02002846 subscr = subscr_get_by_imsi(net, data->imsi);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002847 /* If subscriber is not found */
2848 if (!subscr) {
2849 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2850 "Received '%s' from MNCC with "
2851 "unknown subscriber %s\n", data->called.number,
2852 get_mncc_name(msg_type), data->called.number);
2853 /* Unknown subscriber */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002854 return mncc_release_ind(net, NULL, data->callref,
2855 GSM48_CAUSE_LOC_PRN_S_LU,
2856 GSM48_CC_CAUSE_UNASSIGNED_NR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002857 }
2858 /* If subscriber is not "attached" */
2859 if (!subscr->lac) {
2860 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
2861 "Received '%s' from MNCC with "
2862 "detached subscriber %s\n", data->called.number,
2863 get_mncc_name(msg_type), data->called.number);
2864 subscr_put(subscr);
2865 /* Temporarily out of order */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002866 return mncc_release_ind(net, NULL, data->callref,
2867 GSM48_CAUSE_LOC_PRN_S_LU,
2868 GSM48_CC_CAUSE_DEST_OOO);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002869 }
2870 /* Create transaction */
Harald Weltedcaf5652009-07-23 18:56:43 +02002871 trans = trans_alloc(subscr, GSM48_PDISC_CC, 0xff, data->callref);
2872 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08002873 DEBUGP(DCC, "No memory for trans.\n");
2874 subscr_put(subscr);
2875 /* Ressource unavailable */
Andreas Eversberg7563ac92009-06-14 22:14:12 +08002876 mncc_release_ind(net, NULL, data->callref,
2877 GSM48_CAUSE_LOC_PRN_S_LU,
2878 GSM48_CC_CAUSE_RESOURCE_UNAVAIL);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002879 return -ENOMEM;
2880 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08002881 /* Find lchan */
Harald Welte1a6f7982009-08-09 18:52:33 +02002882 lchan = lchan_for_subscr(subscr);
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01002883
Harald Welte4bfdfe72009-06-10 23:11:52 +08002884 /* If subscriber has no lchan */
2885 if (!lchan) {
2886 /* find transaction with this subscriber already paging */
2887 llist_for_each_entry(transt, &net->trans_list, entry) {
2888 /* Transaction of our lchan? */
2889 if (transt == trans ||
2890 transt->subscr != subscr)
2891 continue;
2892 DEBUGP(DCC, "(bts %d trx - ts - ti -- sub %s) "
2893 "Received '%s' from MNCC with "
2894 "unallocated channel, paging already "
2895 "started.\n", bts->nr,
2896 data->called.number,
2897 get_mncc_name(msg_type));
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002898 subscr_put(subscr);
2899 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002900 return 0;
2901 }
2902 /* store setup informations until paging was successfull */
Harald Weltedcaf5652009-07-23 18:56:43 +02002903 memcpy(&trans->cc.msg, data, sizeof(struct gsm_mncc));
Harald Weltea1b28582009-08-01 19:31:47 +02002904 /* Trigger paging */
2905 paging_request(net, subscr, RSL_CHANNEED_TCH_F,
2906 setup_trig_pag_evt, subscr);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002907 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002908 return 0;
2909 }
2910 /* Assign lchan */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002911 trans->conn = &lchan->conn;
2912 use_subscr_con(trans->conn);
Holger Hans Peter Freytherccf53c62009-10-27 14:21:14 +01002913 subscr_put(subscr);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002914 }
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002915
2916 if (trans->conn)
2917 lchan = trans->conn->lchan;
Harald Welte4bfdfe72009-06-10 23:11:52 +08002918
2919 /* if paging did not respond yet */
2920 if (!lchan) {
2921 DEBUGP(DCC, "(bts - trx - ts - ti -- sub %s) "
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002922 "Received '%s' from MNCC in paging state\n",
Harald Welte4bfdfe72009-06-10 23:11:52 +08002923 (trans->subscr)?(trans->subscr->extension):"-",
2924 get_mncc_name(msg_type));
Harald Weltec66b71c2009-06-11 14:23:20 +08002925 mncc_set_cause(&rel, GSM48_CAUSE_LOC_PRN_S_LU,
2926 GSM48_CC_CAUSE_NORM_CALL_CLEAR);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002927 if (msg_type == MNCC_REL_REQ)
2928 rc = mncc_recvmsg(net, trans, MNCC_REL_CNF, &rel);
2929 else
2930 rc = mncc_recvmsg(net, trans, MNCC_REL_IND, &rel);
2931 trans->callref = 0;
Harald Weltedcaf5652009-07-23 18:56:43 +02002932 trans_free(trans);
Harald Welte4bfdfe72009-06-10 23:11:52 +08002933 return rc;
2934 }
2935
2936 DEBUGP(DCC, "(bts %d trx %d ts %d ti %02x sub %s) "
2937 "Received '%s' from MNCC in state %d (%s)\n",
2938 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
2939 trans->transaction_id,
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01002940 (trans->conn->subscr)?(trans->conn->subscr->extension):"-",
Harald Weltedcaf5652009-07-23 18:56:43 +02002941 get_mncc_name(msg_type), trans->cc.state,
Harald Weltee95daf12010-03-25 12:13:02 +08002942 gsm48_cc_state_name(trans->cc.state));
Harald Welte4bfdfe72009-06-10 23:11:52 +08002943
2944 /* Find function for current state and message */
2945 for (i = 0; i < DOWNSLLEN; i++)
2946 if ((msg_type == downstatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02002947 && ((1 << trans->cc.state) & downstatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08002948 break;
2949 if (i == DOWNSLLEN) {
2950 DEBUGP(DCC, "Message unhandled at this state.\n");
2951 return 0;
2952 }
2953
2954 rc = downstatelist[i].rout(trans, arg);
2955
2956 return rc;
2957}
2958
2959
2960static struct datastate {
2961 u_int32_t states;
2962 int type;
2963 int (*rout) (struct gsm_trans *trans, struct msgb *msg);
2964} datastatelist[] = {
2965 /* mobile originating call establishment */
2966 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2967 GSM48_MT_CC_SETUP, gsm48_cc_rx_setup},
2968 {SBIT(GSM_CSTATE_NULL), /* 5.2.1.2 */
2969 GSM48_MT_CC_EMERG_SETUP, gsm48_cc_rx_setup},
2970 {SBIT(GSM_CSTATE_CONNECT_IND), /* 5.2.1.2 */
2971 GSM48_MT_CC_CONNECT_ACK, gsm48_cc_rx_connect_ack},
2972 /* mobile terminating call establishment */
2973 {SBIT(GSM_CSTATE_CALL_PRESENT), /* 5.2.2.3.2 */
2974 GSM48_MT_CC_CALL_CONF, gsm48_cc_rx_call_conf},
2975 {SBIT(GSM_CSTATE_CALL_PRESENT) | SBIT(GSM_CSTATE_MO_TERM_CALL_CONF), /* ???? | 5.2.2.3.2 */
2976 GSM48_MT_CC_ALERTING, gsm48_cc_rx_alerting},
Holger Hans Peter Freytheracf8a0c2010-03-29 08:47:44 +02002977 {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 +08002978 GSM48_MT_CC_CONNECT, gsm48_cc_rx_connect},
2979 /* signalling during call */
2980 {ALL_STATES - SBIT(GSM_CSTATE_NULL),
2981 GSM48_MT_CC_FACILITY, gsm48_cc_rx_facility},
2982 {SBIT(GSM_CSTATE_ACTIVE),
2983 GSM48_MT_CC_NOTIFY, gsm48_cc_rx_notify},
2984 {ALL_STATES,
2985 GSM48_MT_CC_START_DTMF, gsm48_cc_rx_start_dtmf},
2986 {ALL_STATES,
2987 GSM48_MT_CC_STOP_DTMF, gsm48_cc_rx_stop_dtmf},
2988 {ALL_STATES,
2989 GSM48_MT_CC_STATUS_ENQ, gsm48_cc_rx_status_enq},
2990 {SBIT(GSM_CSTATE_ACTIVE),
2991 GSM48_MT_CC_HOLD, gsm48_cc_rx_hold},
2992 {SBIT(GSM_CSTATE_ACTIVE),
2993 GSM48_MT_CC_RETR, gsm48_cc_rx_retrieve},
2994 {SBIT(GSM_CSTATE_ACTIVE),
2995 GSM48_MT_CC_MODIFY, gsm48_cc_rx_modify},
2996 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2997 GSM48_MT_CC_MODIFY_COMPL, gsm48_cc_rx_modify_complete},
2998 {SBIT(GSM_CSTATE_MO_TERM_MODIFY),
2999 GSM48_MT_CC_MODIFY_REJECT, gsm48_cc_rx_modify_reject},
3000 {SBIT(GSM_CSTATE_ACTIVE),
3001 GSM48_MT_CC_USER_INFO, gsm48_cc_rx_userinfo},
3002 /* clearing */
3003 {ALL_STATES - SBIT(GSM_CSTATE_NULL) - SBIT(GSM_CSTATE_RELEASE_REQ), /* 5.4.3.2 */
3004 GSM48_MT_CC_DISCONNECT, gsm48_cc_rx_disconnect},
3005 {ALL_STATES - SBIT(GSM_CSTATE_NULL), /* 5.4.4.1.2.2 */
3006 GSM48_MT_CC_RELEASE, gsm48_cc_rx_release},
3007 {ALL_STATES, /* 5.4.3.4 */
3008 GSM48_MT_CC_RELEASE_COMPL, gsm48_cc_rx_release_compl},
3009};
3010
3011#define DATASLLEN \
3012 (sizeof(datastatelist) / sizeof(struct datastate))
3013
Harald Welte4bc90a12008-12-27 16:32:52 +00003014static int gsm0408_rcv_cc(struct msgb *msg)
3015{
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003016 struct gsm_subscriber_connection *conn;
Harald Welte4bc90a12008-12-27 16:32:52 +00003017 struct gsm48_hdr *gh = msgb_l3(msg);
3018 u_int8_t msg_type = gh->msg_type & 0xbf;
Harald Welte6f5aee02009-07-23 21:21:14 +02003019 u_int8_t transaction_id = ((gh->proto_discr & 0xf0) ^ 0x80) >> 4; /* flip */
Harald Welte4bfdfe72009-06-10 23:11:52 +08003020 struct gsm_lchan *lchan = msg->lchan;
Harald Weltedcaf5652009-07-23 18:56:43 +02003021 struct gsm_trans *trans = NULL;
Harald Welte4bfdfe72009-06-10 23:11:52 +08003022 int i, rc = 0;
Harald Welte4bc90a12008-12-27 16:32:52 +00003023
Harald Welte4bfdfe72009-06-10 23:11:52 +08003024 if (msg_type & 0x80) {
3025 DEBUGP(DCC, "MSG 0x%2x not defined for PD error\n", msg_type);
3026 return -EINVAL;
Harald Welte4bc90a12008-12-27 16:32:52 +00003027 }
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003028
3029 conn = &lchan->conn;
3030
Harald Welte4bfdfe72009-06-10 23:11:52 +08003031 /* Find transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003032 trans = trans_find_by_id(conn->subscr, GSM48_PDISC_CC, transaction_id);
Harald Weltedcaf5652009-07-23 18:56:43 +02003033
Harald Welte6f5aee02009-07-23 21:21:14 +02003034 DEBUGP(DCC, "(bts %d trx %d ts %d ti %x sub %s) "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003035 "Received '%s' from MS in state %d (%s)\n",
3036 lchan->ts->trx->bts->nr, lchan->ts->trx->nr, lchan->ts->nr,
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003037 transaction_id, (conn->subscr)?(conn->subscr->extension):"-",
Harald Weltee95daf12010-03-25 12:13:02 +08003038 gsm48_cc_msg_name(msg_type), trans?(trans->cc.state):0,
3039 gsm48_cc_state_name(trans?(trans->cc.state):0));
Harald Welte4bfdfe72009-06-10 23:11:52 +08003040
3041 /* Create transaction */
3042 if (!trans) {
Harald Welte6f5aee02009-07-23 21:21:14 +02003043 DEBUGP(DCC, "Unknown transaction ID %x, "
Harald Welte4bfdfe72009-06-10 23:11:52 +08003044 "creating new trans.\n", transaction_id);
3045 /* Create transaction */
Holger Hans Peter Freyther68884aa2010-03-23 06:41:45 +01003046 trans = trans_alloc(conn->subscr, GSM48_PDISC_CC,
Harald Weltedcaf5652009-07-23 18:56:43 +02003047 transaction_id, new_callref++);
3048 if (!trans) {
Harald Welte4bfdfe72009-06-10 23:11:52 +08003049 DEBUGP(DCC, "No memory for trans.\n");
3050 rc = gsm48_tx_simple(msg->lchan,
Harald Welte6f5aee02009-07-23 21:21:14 +02003051 GSM48_PDISC_CC | (transaction_id << 4),
Harald Welte4bfdfe72009-06-10 23:11:52 +08003052 GSM48_MT_CC_RELEASE_COMPL);
3053 return -ENOMEM;
3054 }
Harald Welte4bfdfe72009-06-10 23:11:52 +08003055 /* Assign transaction */
Holger Hans Peter Freythere95d4822010-03-23 07:00:22 +01003056 trans->conn = &lchan->conn;
3057 use_subscr_con(trans->conn);
Harald Welte4bfdfe72009-06-10 23:11:52 +08003058 }
3059
3060 /* find function for current state and message */
3061 for (i = 0; i < DATASLLEN; i++)
3062 if ((msg_type == datastatelist[i].type)
Harald Weltedcaf5652009-07-23 18:56:43 +02003063 && ((1 << trans->cc.state) & datastatelist[i].states))
Harald Welte4bfdfe72009-06-10 23:11:52 +08003064 break;
3065 if (i == DATASLLEN) {
3066 DEBUGP(DCC, "Message unhandled at this state.\n");
3067 return 0;
3068 }
3069
3070 rc = datastatelist[i].rout(trans, msg);
Harald Welte4bc90a12008-12-27 16:32:52 +00003071
3072 return rc;
3073}
3074
Harald Welte52b1f982008-12-23 20:25:15 +00003075/* 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 +02003076int gsm0408_rcvmsg(struct msgb *msg, u_int8_t link_id)
Harald Welte52b1f982008-12-23 20:25:15 +00003077{
3078 struct gsm48_hdr *gh = msgb_l3(msg);
3079 u_int8_t pdisc = gh->proto_discr & 0x0f;
Harald Welte8470bf22008-12-25 23:28:35 +00003080 int rc = 0;
Harald Welte51008772009-12-29 11:49:12 +01003081
3082 if (silent_call_reroute(msg))
3083 return silent_call_rx(msg);
Harald Welte52b1f982008-12-23 20:25:15 +00003084
3085 switch (pdisc) {
3086 case GSM48_PDISC_CC:
3087 rc = gsm0408_rcv_cc(msg);
3088 break;
3089 case GSM48_PDISC_MM:
3090 rc = gsm0408_rcv_mm(msg);
3091 break;
3092 case GSM48_PDISC_RR:
3093 rc = gsm0408_rcv_rr(msg);
3094 break;
Harald Weltebcae43f2008-12-27 21:45:37 +00003095 case GSM48_PDISC_SMS:
Harald Welte (local)daef6062009-08-14 11:41:12 +02003096 rc = gsm0411_rcv_sms(msg, link_id);
Harald Weltebcae43f2008-12-27 21:45:37 +00003097 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003098 case GSM48_PDISC_MM_GPRS:
Harald Weltebcae43f2008-12-27 21:45:37 +00003099 case GSM48_PDISC_SM_GPRS:
Harald Welte5d24ba12009-12-24 12:13:17 +01003100 LOGP(DRLL, LOGL_NOTICE, "Unimplemented "
3101 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003102 break;
Harald Welte6eafe912009-10-16 08:32:58 +02003103 case GSM48_PDISC_NC_SS:
3104 rc = handle_rcv_ussd(msg);
3105 break;
Harald Welte52b1f982008-12-23 20:25:15 +00003106 default:
Harald Welte5d24ba12009-12-24 12:13:17 +01003107 LOGP(DRLL, LOGL_NOTICE, "Unknown "
3108 "GSM 04.08 discriminator 0x%02x\n", pdisc);
Harald Welte52b1f982008-12-23 20:25:15 +00003109 break;
3110 }
3111
3112 return rc;
3113}
Harald Welte8470bf22008-12-25 23:28:35 +00003114
Harald Welte805f6442009-07-28 18:25:29 +02003115/*
3116 * This will be ran by the linker when loading the DSO. We use it to
3117 * do system initialization, e.g. registration of signal handlers.
3118 */
3119static __attribute__((constructor)) void on_dso_load_0408(void)
3120{
Harald Welte805f6442009-07-28 18:25:29 +02003121 register_signal_handler(SS_ABISIP, handle_abisip_signal, NULL);
3122}